diff --git a/CMake/functions/devilutionx_library.cmake b/CMake/functions/devilutionx_library.cmake index 361326e4e..1d33b2679 100644 --- a/CMake/functions/devilutionx_library.cmake +++ b/CMake/functions/devilutionx_library.cmake @@ -1,4 +1,5 @@ include(functions/genex) +include(functions/set_relative_file_macro) # This function is equivalent to `add_library` but applies DevilutionX-specific # compilation flags to it. @@ -66,6 +67,8 @@ function(add_devilutionx_library NAME) endif() target_compile_definitions(${NAME} PUBLIC ${DEVILUTIONX_DEFINITIONS}) + + set_relative_file_macro(${NAME}) endfunction() # Same as add_devilutionx_library(${NAME} OBJECT) with an additional diff --git a/CMake/functions/set_relative_file_macro.cmake b/CMake/functions/set_relative_file_macro.cmake new file mode 100644 index 000000000..c229ba55b --- /dev/null +++ b/CMake/functions/set_relative_file_macro.cmake @@ -0,0 +1,15 @@ +# Sets the __FILE__ macro value to be relative to CMAKE_SOURCE_DIR. +function(set_relative_file_macro TARGET) + if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + if((CMAKE_CXX_COMPILER_ID MATCHES "CLANG" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12) + OR (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8)) + target_compile_options(${TARGET} PUBLIC "-ffile-prefix-map=${CMAKE_SOURCE_DIR}/=") + else() + get_target_property(_srcs ${TARGET} SOURCES) + foreach(_src ${_srcs}) + set_source_files_properties(${_src} PROPERTIES COMPILE_DEFINITIONS __FILE__="${_src}") + endforeach() + target_compile_options(${TARGET} PRIVATE -Wno-builtin-macro-redefined) + endif() + endif() +endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt index a86559dfa..05d623186 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,18 +247,8 @@ if(BUILD_TESTING) add_subdirectory(test) endif() -if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - # Change __FILE__ to only show the path relative to the project folder - get_target_property(libdevilutionx_SRCS ${BIN_TARGET} SOURCES) - foreach(SOURCE_FILE ${libdevilutionx_SRCS}) - set_source_files_properties(${SOURCE_FILE} PROPERTIES - COMPILE_DEFINITIONS __FILE__="${SOURCE_FILE}" - ) - endforeach(SOURCE_FILE) - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-builtin-macro-redefined") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-builtin-macro-redefined") -endif() +include(functions/set_relative_file_macro) +set_relative_file_macro(${BIN_TARGET}) if(APPLE) set_source_files_properties("./Packaging/apple/AppIcon.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources)