Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Absolute paths (gcc/clang only) #617

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mark_as_advanced(CMAKE_USE_FOLDERS)
# Include files which define target specific functions.
include(${CMAKE_CURRENT_LIST_DIR}/warnings.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/cxxstd.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/embedded_paths.cmake)

# Set a default build type if not passed
get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
Expand Down Expand Up @@ -303,7 +304,9 @@ function(add_flamegpu_executable NAME SRC FLAMEGPU_ROOT PROJECT_ROOT IS_EXAMPLE)
CommonCompilerSettings(TARGET "${NAME}")
# Set the cuda gencodes, potentially using the user-provided CUDA_ARCH
SetCUDAGencodes(TARGET "${PROJECT_NAME}")

# Strip absolute paths from binaries if supported by the compiler.
TargetStripBuildDirectoryInformation(TARGET "${NAME}")

# Enable RDC for the target
set_property(TARGET ${NAME} PROPERTY CUDA_SEPARABLE_COMPILATION ON)

Expand Down
38 changes: 38 additions & 0 deletions cmake/embedded_paths.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Series of cmake rules that remove absoluate paths from binary (i.e. for reprdoucibility)
# See https://reproducible-builds.org/docs/build-path/ for some informatio non this.

# Define a function which modifies absoluate paths for a given target.
function(TargetStripBuildDirectoryInformation)
# Parse the expected arguments, prefixing variables.
cmake_parse_arguments(
STRIP_BUILD_DIR_INFO
""
"TARGET"
""
${ARGN}
)
# Ensure that a target has been passed, and that it is a valid target.
if(NOT STRIP_BUILD_DIR_INFO_TARGET)
message( FATAL_ERROR "EnableCompilerWarnings: 'TARGET' argument required." )
elseif(NOT TARGET ${STRIP_BUILD_DIR_INFO_TARGET} )
message( FATAL_ERROR "EnableCompilerWarnings: TARGET '${STRIP_BUILD_DIR_INFO_TARGET}' is not a valid target" )
endif()
# GCC 8+, clang 10+ supports -ffile-prefix-map as an aliase for -fmacro-prefix-map (__FILE__) and -fdebug-prefix-map (Debug info)
# GCC, clang 3.8+ support -fdebug-prefix-map to strip debug info.
# NVCC/NVHPC/MSVC do not appear to support similar options.
if(
(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8)
OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 10)
)
target_compile_options(${STRIP_BUILD_DIR_INFO_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-ffile-prefix-map=${CMAKE_SOURCE_DIR}=.>")
target_compile_options(${STRIP_BUILD_DIR_INFO_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler -ffile-prefix-map=${CMAKE_SOURCE_DIR}=.>")
elseif(
(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "3.8")
)
target_compile_options(${STRIP_BUILD_DIR_INFO_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.>")
target_compile_options(${STRIP_BUILD_DIR_INFO_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.>")
endif()
endfunction()
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ EnableFLAMEGPUCompilerWarnings(TARGET "${PROJECT_NAME}")
CommonCompilerSettings(TARGET "${PROJECT_NAME}")
# Set the cuda gencodes, potentially using the user-provided CUDA_ARCH
SetCUDAGencodes(TARGET "${PROJECT_NAME}")
# Strip absolute paths from binaries if supported by the compiler.
TargetStripBuildDirectoryInformation(TARGET "${PROJECT_NAME}")

# enable "fpic" for linux to allow shared libraries to be build from the static library (required for swig)
set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand Down