[cmake,gcc,clang] use -fmacro-prefix-map and -ffile-prefix-map

Instead of some bash path substitution only working with Makefiles use
the compiler options to map source and build directories to some
defaults
This commit is contained in:
akallabeth 2024-11-07 16:46:17 +01:00
parent 1520d94d33
commit ebcf4c44a9
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
3 changed files with 10 additions and 13 deletions

View File

@ -239,18 +239,6 @@ if(CMAKE_COMPILER_IS_GNUCC)
endif()
endif()
# When building with Unix Makefiles and doing any release builds
# try to set __FILE__ to relative paths via a make specific macro
if (CMAKE_GENERATOR MATCHES "Unix Makefile*")
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
string(TOUPPER ${CMAKE_BUILD_TYPE} UPPER_BUILD_TYPE)
CHECK_C_COMPILER_FLAG (-Wno-builtin-macro-redefined Wno-builtin-macro-redefined)
if(Wno-builtin-macro-redefined)
set(CMAKE_C_FLAGS_${UPPER_BUILD_TYPE} "${CMAKE_C_FLAGS_${UPPER_BUILD_TYPE}} -Wno-builtin-macro-redefined -D__FILE__='\"$(subst ${PROJECT_BINARY_DIR}/,,$(subst ${PROJECT_SOURCE_DIR}/,,$(abspath $<)))\"'")
endif()
endif()
endif()
if(CMAKE_COMPILER_IS_CLANG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-c11-extensions -Wno-gnu")
endif()

View File

@ -61,6 +61,11 @@ if (ENABLE_WARNING_ERROR)
CheckCXXFlag(-Werror)
endif()
CheckCXXFlag(-fmacro-prefix-map="${CMAKE_SOURCE_DIR}"="./")
CheckCXXFlag(-fmacro-prefix-map="${CMAKE_BINARY_DIR}"="./build/")
CheckCXXFlag(-ffile-prefix-map="${CMAKE_SOURCE_DIR}"="./")
CheckCXXFlag(-ffile-prefix-map="${CMAKE_BINARY_DIR}"="./build")
# https://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c
if (WIN32)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-DNOMINMAX>)

View File

@ -52,10 +52,14 @@ if (ENABLE_WARNING_VERBOSE)
endforeach()
endif()
if (ENABLE_WARNING_ERROR)
CheckCFlag(-Werror)
endif()
CheckCFlag(-fmacro-prefix-map="${CMAKE_SOURCE_DIR}"="./")
CheckCFlag(-fmacro-prefix-map="${CMAKE_BINARY_DIR}"="./build/")
CheckCFlag(-ffile-prefix-map="${CMAKE_SOURCE_DIR}"="./")
CheckCFlag(-ffile-prefix-map="${CMAKE_BINARY_DIR}"="./build")
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "default CFLAGS")
message("Using CFLAGS ${CMAKE_C_FLAGS}")