Skip to content

Commit

Permalink
[eclipse-iceoryx#210] Generate the cbindgen header into the target di…
Browse files Browse the repository at this point in the history
…rectory
  • Loading branch information
elBoberido committed Jun 1, 2024
1 parent decaedb commit 4057cd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 4 additions & 6 deletions iceoryx2-lang/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,22 @@ set(RUST_ARCH_TRIPLET_FLAG "")
set(RUST_TARGET_DIR ${CMAKE_BINARY_DIR}/rust)
set(RUST_ARCH_TARGET_DIR ${RUST_TARGET_DIR}/${RUST_ARCH_TRIPLET})

# TODO switch for Windows
set(ICEORYX2_LANG_C_LIB ${RUST_ARCH_TARGET_DIR}/${RUST_BUILD_TYPE}/libiceoryx2_lang_c.a)
set(ICEORYX2_LANG_C_INCLUDE_DIR ${RUST_ARCH_TARGET_DIR}/${RUST_BUILD_TYPE}/iceoryx2_lang_c_cbindgen/include)

# run cargo
add_custom_target(
iceoryx2 ALL
COMMAND cargo build ${RUST_BUILD_TYPE_FLAG} --target-dir=${RUST_TARGET_DIR} ${RUST_ARCH_TRIPLET_FLAG}
BYPRODUCTS
"${CMAKE_CURRENT_SOURCE_DIR}/iceoryx2.h"
${ICEORYX2_LANG_C_INCLUDE_DIR}/iox2/iceoryx2.h
${ICEORYX2_LANG_C_LIB}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
VERBATIM
USES_TERMINAL
)

# TODO remove once generation works as expected ... have a look at iceoryx-rs
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/iceoryx2.h"
"${CMAKE_BINARY_DIR}/generated/include/iox2/iceoryx2.h" @ONLY)

# set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
# list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".so")
# list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
Expand All @@ -56,7 +54,7 @@ add_library(${PROJECT_NAME} INTERFACE)

add_dependencies(${PROJECT_NAME} iceoryx2)

target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_BINARY_DIR}/generated/include)
target_include_directories(${PROJECT_NAME} INTERFACE ${ICEORYX2_LANG_C_INCLUDE_DIR})

target_link_libraries(${PROJECT_NAME} INTERFACE ${ICEORYX2_LANG_C_LIB})

Expand Down
12 changes: 10 additions & 2 deletions iceoryx2-lang/c/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
extern crate cbindgen;

use std::env;
use std::path::Path;

use cbindgen::Config;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
// this is the out dir of the iceoryx2-lang C crate not the workspace out dir,
// therefore we need to traverse to a known location and create the path for the header
let out_dir = env::var("OUT_DIR").expect("Target output directory");

let mut header_path = Path::new(&out_dir).join("../../../").canonicalize().expect("Path to iceoryx2 base dir for header generation");
header_path.push("iceoryx2_lang_c_cbindgen/include/iox2/iceoryx2.h");

let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("Cargo manifest dir");

let mut config = Config::default();
config.language = cbindgen::Language::C;
Expand All @@ -15,5 +23,5 @@ fn main() {
.with_config(config)
.generate()
.expect("Unable to generate c bindings")
.write_to_file("iceoryx2.h");
.write_to_file(header_path);
}

0 comments on commit 4057cd2

Please sign in to comment.