Skip to content

Commit

Permalink
objcopy command now executed via cmake to use architecture specific t…
Browse files Browse the repository at this point in the history
…ool.
  • Loading branch information
gmartin82 committed Oct 22, 2024
1 parent e76b231 commit cfcf202
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
15 changes: 9 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn main() {
// Prefix symbols in Cyclone DDS and Cyclocut libraries to ensure uniqueness
#[cfg(all(target_os = "linux", not(feature = "iceoryx")))]
{
// Prefix = cyclors_<version>_
// Prefix = cyclors_<version>
prefix = env::var("CARGO_PKG_VERSION").unwrap().replace('.', "_");
prefix.insert_str(0, "cyclors_");
prefix.push('_');
Expand Down Expand Up @@ -292,11 +292,14 @@ fn prefix_symbols(
lib_name
));
}
let arg = format!("--redefine-syms={}", symbol_file_path.to_str().unwrap());
match Command::new("objcopy").arg(arg).arg(lib_file_path).output() {
Ok(_) => Ok(()),
Err(_) => Err(format!("Failed to run objcopy on library {}", lib_name)),
}

let mut objcopy = cmake::Config::new("objcopy");
objcopy
.build_target("all")
.define("LIB_PATH", lib_file_path.clone())
.define("SYMBOL_FILE_PATH", symbol_file_path.clone())
.build();
Ok(())
}
Err(_) => Err(format!(
"Failed to create symbol file for library {}",
Expand Down
2 changes: 1 addition & 1 deletion cyclocut/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0.0)
cmake_minimum_required(VERSION 3.5)
project(cdds-util VERSION 0.1.6)

include(CTest)
Expand Down
50 changes: 50 additions & 0 deletions objcopy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
cmake_minimum_required(VERSION 3.12)
project(Objcopy)

# Include the CMakeFindBinUtils module to find objcopy
include(CMakeFindBinUtils)

# Ensure objcopy is available
if(NOT CMAKE_OBJCOPY)
message(FATAL_ERROR "CMAKE_OBJCOPY is not defined. Ensure objcopy is installed on your system!")
else()
message(STATUS "CMAKE_OBJCOPY found: ${CMAKE_OBJCOPY}")
endif()

# Ensure LIB_PATH is defined and exists
if(NOT DEFINED LIB_PATH)
message(FATAL_ERROR "LIB_PATH not specified!")
else()
if(NOT EXISTS ${LIB_PATH})
message(FATAL_ERROR "Library not found: ${LIB_PATH}")
else()
message(STATUS "LIB_PATH: ${LIB_PATH}")
endif()
endif()

# Ensure SYMBOL_FILE_PATH is defined and exists
if(NOT DEFINED SYMBOL_FILE_PATH)
message(FATAL_ERROR "SYMBOL_FILE_PATH not specified!")
else()
if(NOT EXISTS ${SYMBOL_FILE_PATH})
message(FATAL_ERROR "Symbol file not found: ${SYMBOL_FILE_PATH}")
else()
message(STATUS "SYMBOL_FILE_PATH: ${SYMBOL_FILE_PATH}")
endif()
endif()

# Custom target to mangle the library
add_custom_target(mangle_library ALL
COMMAND ${CMAKE_COMMAND} -E echo "Running objcopy --redefine-syms on ${LIB_PATH} with symbols from ${SYMBOL_FILE_PATH}..."

# Run objcopy and redirect stderr to a file
COMMAND ${CMAKE_OBJCOPY} --redefine-syms=${SYMBOL_FILE_PATH} ${LIB_PATH} 2> stderr.txt

# Check if stderr.txt is empty (i.e., no errors were produced)
COMMAND ${CMAKE_COMMAND} -E echo "Checking for objcopy errors..."
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/check_errors.sh

# Clean up stderr.txt after checking for errors
COMMAND ${CMAKE_COMMAND} -E remove stderr.txt
COMMENT "Mangling library with objcopy..."
)
10 changes: 10 additions & 0 deletions objcopy/check_errors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

if [ ! -s stderr.txt ]; then
echo 'Command succeeded with no stderr output.'
exit 0
else
echo 'Command failed with errors:'
cat stderr.txt
exit 1
fi

0 comments on commit cfcf202

Please sign in to comment.