Skip to content

Commit

Permalink
Use -ffile-prefix-map for relative __FILE__
Browse files Browse the repository at this point in the history
Fixes #4420
  • Loading branch information
glebm authored and AJenbo committed Apr 13, 2022
1 parent 9b83479 commit 0ac787f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CMake/functions/devilutionx_library.cmake
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions CMake/functions/set_relative_file_macro.cmake
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0ac787f

Please sign in to comment.