Browse Source

Use `-ffile-prefix-map` for relative `__FILE__`

Fixes #4420
pull/4424/head 1.4.0
Gleb Mazovetskiy 4 years ago committed by Anders Jenbo
parent
commit
0ac787fc05
  1. 3
      CMake/functions/devilutionx_library.cmake
  2. 15
      CMake/functions/set_relative_file_macro.cmake
  3. 14
      CMakeLists.txt

3
CMake/functions/devilutionx_library.cmake

@ -1,4 +1,5 @@
include(functions/genex) include(functions/genex)
include(functions/set_relative_file_macro)
# This function is equivalent to `add_library` but applies DevilutionX-specific # This function is equivalent to `add_library` but applies DevilutionX-specific
# compilation flags to it. # compilation flags to it.
@ -66,6 +67,8 @@ function(add_devilutionx_library NAME)
endif() endif()
target_compile_definitions(${NAME} PUBLIC ${DEVILUTIONX_DEFINITIONS}) target_compile_definitions(${NAME} PUBLIC ${DEVILUTIONX_DEFINITIONS})
set_relative_file_macro(${NAME})
endfunction() endfunction()
# Same as add_devilutionx_library(${NAME} OBJECT) with an additional # Same as add_devilutionx_library(${NAME} OBJECT) with an additional

15
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()

14
CMakeLists.txt

@ -247,18 +247,8 @@ if(BUILD_TESTING)
add_subdirectory(test) add_subdirectory(test)
endif() endif()
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") include(functions/set_relative_file_macro)
# Change __FILE__ to only show the path relative to the project folder set_relative_file_macro(${BIN_TARGET})
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()
if(APPLE) if(APPLE)
set_source_files_properties("./Packaging/apple/AppIcon.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources) set_source_files_properties("./Packaging/apple/AppIcon.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources)

Loading…
Cancel
Save