@@ -6,15 +6,25 @@ use cargo_toml::Manifest;
66const AMENT_PREFIX_PATH : & str = "AMENT_PREFIX_PATH" ;
77const ROS_DISTRO : & str = "ROS_DISTRO" ;
88
9- fn get_env_var_or_abort ( env_var : & ' static str ) -> String {
10- if let Ok ( value) = env:: var ( env_var) {
11- value
12- } else {
13- panic ! (
14- "{} environment variable not set - please source ROS 2 installation first." ,
15- env_var
16- ) ;
17- }
9+ fn get_env_or_shim < F > ( var_name : & str , shim_logic : F ) -> String
10+ where
11+ F : FnOnce ( ) -> Result < String , String > ,
12+ {
13+ let res = env:: var ( var_name) . or_else ( |_| {
14+ if env:: var ( "CARGO_FEATURE_USE_ROS_SHIM" ) . is_ok ( ) {
15+ shim_logic ( )
16+ } else {
17+ Err ( format ! (
18+ "{var_name} environment variable not set - please source ROS 2 installation first."
19+ ) )
20+ }
21+ } ) . expect ( format ! ( "Failed to get {var_name}" ) . as_str ( ) ) ;
22+
23+ // Make sure this script will rerun if the environment variable changes.
24+ // If we used `env!` or `option_env!`, we would not need this.
25+ println ! ( "cargo:rerun-if-env-changed={var_name}" ) ;
26+
27+ res
1828}
1929
2030fn marked_reexport ( cargo_toml : String ) -> bool {
@@ -35,44 +45,26 @@ fn main() {
3545 "cargo:rustc-check-cfg=cfg(ros_distro, values(\" {}\" ))" ,
3646 vec![ "humble" , "jazzy" , "kilted" , "rolling" ] . join( "\" , \" " )
3747 ) ;
38- let ros_distro = if let Ok ( value) = env:: var ( ROS_DISTRO ) {
39- value
40- } else {
41- cfg_if:: cfg_if! {
42- if #[ cfg( feature="use_ros_shim" ) ] {
43- use rustflags;
44- // // Look for --cfg ros_distro=<ros_distro>
45- for flag in rustflags:: from_env( ) {
46- if matches!( flag, rustflags:: Flag :: Cfg { ref name, value : _ } if name == "ros_distro" ) {
47- if let rustflags:: Flag :: Cfg { name: _, value: flag_value} = flag {
48- println!( "cargo:rustc-cfg=ros_distro=\" {}\" " , flag_value. unwrap( ) ) ;
49- return ;
50- } else {
51- continue ;
52- }
53- }
54- }
55- let error_msg =
56- "When using the use_ros_shim feature, you must pass the ROS distribution you are targeting as a compiler flag with --cfg ros_distro=\" <ros_distro>\" " ;
57- panic!( "{}" , error_msg) ;
58- } else {
59- let error_msg =
60- "ROS_DISTRO environment variable not set - please source ROS 2 installation first." ;
61- panic!( "{}" , error_msg) ;
62- }
63- }
64- } ;
48+ let ros_distro = get_env_or_shim ( ROS_DISTRO , || {
49+ rustflags:: from_env ( )
50+ . find_map ( |flag| match flag {
51+ rustflags:: Flag :: Cfg { name, value } if & name == "ros_distro" => value,
52+ _ => None ,
53+ } )
54+ . ok_or_else ( || "When using use_ros_shim, you must pass --cfg ros_distro=\" ...\" via RUSTFLAGS" . to_string ( ) )
55+ } ) ;
56+
6557 println ! ( "cargo:rustc-cfg=ros_distro=\" {ros_distro}\" " ) ;
6658
67- let ament_prefix_paths = get_env_var_or_abort ( AMENT_PREFIX_PATH ) ;
59+ let ament_prefix_paths = get_env_or_shim ( AMENT_PREFIX_PATH , || Ok ( String :: new ( ) ) ) ;
60+
6861 for ament_prefix_path in ament_prefix_paths. split ( ':' ) . map ( Path :: new) {
6962 // Link the native libraries
7063 let library_path = ament_prefix_path. join ( "lib" ) ;
7164 println ! ( "cargo:rustc-link-search=native={}" , library_path. display( ) ) ;
7265 }
7366
7467 // Re-export any generated interface crates that we find
75- let ament_prefix_paths = env ! ( "AMENT_PREFIX_PATH" , "AMENT_PREFIX_PATH environment variable not set - please source ROS 2 installation first." ) ;
7668 let export_crate_tomls = ament_prefix_paths
7769 . split ( ':' )
7870 . map ( PathBuf :: from)
@@ -123,7 +115,7 @@ fn main() {
123115 . filter ( |entry| entry. path ( ) . is_file ( ) )
124116 // Ignore lib.rs and any rmw.rs. lib.rs is only used if the crate is consumed
125117 // independently, and rmw.rs files need their top level module
126- // (i.e. msg, srv, action) to exist to be re-exported.
118+ // (i.e., msg, srv, action) to exist to be re-exported.
127119 . filter ( |entry| {
128120 let name = entry. file_name ( ) ;
129121 name != "lib.rs" && name != "rmw.rs"
@@ -154,7 +146,7 @@ fn main() {
154146 let dest_path = PathBuf :: from ( out_dir) . join ( "interfaces.rs" ) ;
155147
156148 // TODO I would like to run rustfmt on this generated code, similar to how bindgen does it
157- fs:: write ( dest_path, content. clone ( ) ) . expect ( "Failed to write interfaces.rs" ) ;
149+ fs:: write ( & dest_path, content. clone ( ) ) . expect ( "Failed to write interfaces.rs" ) ;
158150
159151 println ! ( "cargo:rustc-link-lib=dylib=rcl" ) ;
160152 println ! ( "cargo:rustc-link-lib=dylib=rcl_action" ) ;
0 commit comments