From 7fc96e3eb2fd9f7f272258e07204041aeecfba76 Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Thu, 30 Nov 2023 12:11:56 +0100 Subject: [PATCH] Revert cmake include dirs behaviour --- r2r_cargo.cmake | 10 ++++++++-- r2r_common/src/lib.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/r2r_cargo.cmake b/r2r_cargo.cmake index 184bfaf..a3f40e9 100644 --- a/r2r_cargo.cmake +++ b/r2r_cargo.cmake @@ -76,17 +76,23 @@ function(r2r_cargo) string (REPLACE ";" " " RUSTFLAGS_STR "${RUSTFLAGS}") set(ENV{RUSTFLAGS} ${RUSTFLAGS_STR}) + # get include paths + get_property(includeDirs DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY INCLUDE_DIRECTORIES) + list(REMOVE_DUPLICATES includeDirs) + string (REPLACE ";" ":" CMAKE_INCLUDE_DIRS_STR "${includeDirs}") + set(ENV{CMAKE_INCLUDE_DIRS} ${CMAKE_INCLUDE_DIRS_STR}) + # custom target for building using cargo option(CARGO_CLEAN "Invoke cargo clean before building" OFF) if(CARGO_CLEAN) add_custom_target(cargo_target ALL COMMAND ${CMAKE_COMMAND} "-E" "env" "cargo" "clean" "--profile" "colcon" - COMMAND ${CMAKE_COMMAND} "-E" "env" "RUSTFLAGS=$ENV{RUSTFLAGS}" "CMAKE_IDL_PACKAGES=$ENV{CMAKE_IDL_PACKAGES}" "cargo" "build" "--profile" "colcon" + COMMAND ${CMAKE_COMMAND} "-E" "env" "RUSTFLAGS=$ENV{RUSTFLAGS}" "CMAKE_IDL_PACKAGES=$ENV{CMAKE_IDL_PACKAGES}" "CMAKE_INCLUDE_DIRS=$ENV{CMAKE_INCLUDE_DIRS_PACKAGES}" "cargo" "build" "--profile" "colcon" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ) else() add_custom_target(cargo_target ALL - COMMAND ${CMAKE_COMMAND} "-E" "env" "RUSTFLAGS=$ENV{RUSTFLAGS}" "CMAKE_IDL_PACKAGES=$ENV{CMAKE_IDL_PACKAGES}" "cargo" "build" "--profile" "colcon" + COMMAND ${CMAKE_COMMAND} "-E" "env" "RUSTFLAGS=$ENV{RUSTFLAGS}" "CMAKE_IDL_PACKAGES=$ENV{CMAKE_IDL_PACKAGES}" "CMAKE_INCLUDE_DIRS=$ENV{CMAKE_INCLUDE_DIRS}" "cargo" "build" "--profile" "colcon" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ) endif() diff --git a/r2r_common/src/lib.rs b/r2r_common/src/lib.rs index f2fb892..ebd3d01 100644 --- a/r2r_common/src/lib.rs +++ b/r2r_common/src/lib.rs @@ -49,6 +49,19 @@ pub fn setup_bindgen_builder() -> bindgen::Builder { non_exhaustive: false, }); if !cfg!(feature = "doc-only") { + if let Ok(cmake_includes) = env::var("CMAKE_INCLUDE_DIRS") { + // we are running from cmake, do special thing. + let mut includes = cmake_includes.split(':').collect::>(); + includes.sort_unstable(); + includes.dedup(); + + for x in &includes { + let clang_arg = format!("-I{}", x); + println!("adding clang arg: {}", clang_arg); + builder = builder.clang_arg(clang_arg); + } + } + let ament_prefix_var_name = "AMENT_PREFIX_PATH"; let ament_prefix_var = { let mut ament_str = env::var_os(ament_prefix_var_name).expect("Source your ROS!");