From ad8056ed351d8e3dd13477793bbbe03092981073 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Wed, 2 Oct 2019 13:27:00 +0200 Subject: [PATCH] Add prefix-map compile options GCC starting with version 8 supports a flag called -fmacro-prefix-map that allows you to remap source paths. This will affect the __FILE__ macro. This patch remaps the core directory to . so we leak fewer build system paths into the final binaries. This patch also adds -fdebug-prefix-map parameter to the compiler calls. This is usually something you want which is why it is enabled by default (and maps the same was as -fmacro-prefix-map). To disable the behaviour the new cmake option DEBUG_PREFIX_MAP has been added. This can be set to NO to disable remapping of debug source paths. The patch also disables DEBUG_PREFIX_MAP for RPM and DEB builds, because these build systems rewrite the paths themselves and thus need them unchanged. --- core/CMakeLists.txt | 18 ++++++++++++++++++ core/debian/rules | 1 + core/platforms/packaging/bareos.spec | 17 +++++++++-------- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 0222be93b61..bbb214245f6 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -48,6 +48,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() +include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG(-Wsuggest-override compiler_will_suggest_override) @@ -65,6 +66,23 @@ if (${compiler_error_format_security}) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=format-security") endif() +option(DEBUG_PREFIX_MAP "remap absolute debug paths to relative if compiler supports it" ON) +CHECK_C_COMPILER_FLAG(-fdebug-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=. c_compiler_debug_prefix_map) +CHECK_CXX_COMPILER_FLAG(-fdebug-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=. cxx_compiler_debug_prefix_map) +if(DEBUG_PREFIX_MAP AND c_compiler_debug_prefix_map AND cxx_compiler_debug_prefix_map) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdebug-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.") + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdebug-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.") +endif() + +CHECK_C_COMPILER_FLAG(-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=. c_compiler_macro_prefix_map) +CHECK_CXX_COMPILER_FLAG(-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=. cxx_compiler_macro_prefix_map) +if (c_compiler_macro_prefix_map) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.") +endif() +if (cxx_compiler_macro_prefix_map) + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.") +endif() + # warn on sign-conversion #include(CheckCCompilerFlag) #CHECK_C_COMPILER_FLAG(-Wsign-conversion c_compiler_will_warn_sign_conversion) diff --git a/core/debian/rules b/core/debian/rules index b1c7d2dbe24..b7baaac4cc4 100755 --- a/core/debian/rules +++ b/core/debian/rules @@ -29,6 +29,7 @@ STORAGE_DAEMON_GROUP = $(DAEMON_GROUP) BAREOS_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | sed 's/Version: //g') define CONFIGURE_COMMON + -DDEBUG_PREFIX_MAP:BOOL=OFF \ -Dsbin-perm=755 \ -Dsbindir=/usr/sbin \ -Dbindir=/usr/bin \ diff --git a/core/platforms/packaging/bareos.spec b/core/platforms/packaging/bareos.spec index 4c50b7a285a..3c493e2b8cf 100644 --- a/core/platforms/packaging/bareos.spec +++ b/core/platforms/packaging/bareos.spec @@ -737,14 +737,15 @@ CXXFLAGS="${CXXFLAGS:-%optflags}" ; export CXXFLAGS ; # use our own cmake call instead of cmake macro as it is different on different platforms/versions cmake .. \ - -DCMAKE_VERBOSE_MAKEFILE=ON \ - -DCMAKE_INSTALL_PREFIX:PATH=/usr \ - -DCMAKE_INSTALL_LIBDIR:PATH=/usr/lib \ - -DINCLUDE_INSTALL_DIR:PATH=/usr/include \ - -DLIB_INSTALL_DIR:PATH=/usr/lib \ - -DSYSCONF_INSTALL_DIR:PATH=/etc \ - -DSHARE_INSTALL_PREFIX:PATH=/usr/share \ - -DBUILD_SHARED_LIBS:BOOL=ON \ + -DCMAKE_VERBOSE_MAKEFILE=ON \ + -DCMAKE_INSTALL_PREFIX:PATH=/usr \ + -DCMAKE_INSTALL_LIBDIR:PATH=/usr/lib \ + -DINCLUDE_INSTALL_DIR:PATH=/usr/include \ + -DLIB_INSTALL_DIR:PATH=/usr/lib \ + -DSYSCONF_INSTALL_DIR:PATH=/etc \ + -DSHARE_INSTALL_PREFIX:PATH=/usr/share \ + -DBUILD_SHARED_LIBS:BOOL=ON \ + -DDEBUG_PREFIX_MAP:BOOL=OFF \ -Dprefix=%{_prefix}\ -Dlibdir=%{library_dir} \ -Dsbindir=%{_sbindir} \