Skip to content

Commit 0ac787f

Browse files
glebmAJenbo
authored andcommitted
Use -ffile-prefix-map for relative __FILE__
Fixes #4420
1 parent 9b83479 commit 0ac787f

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

CMake/functions/devilutionx_library.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include(functions/genex)
2+
include(functions/set_relative_file_macro)
23

34
# This function is equivalent to `add_library` but applies DevilutionX-specific
45
# compilation flags to it.
@@ -66,6 +67,8 @@ function(add_devilutionx_library NAME)
6667
endif()
6768

6869
target_compile_definitions(${NAME} PUBLIC ${DEVILUTIONX_DEFINITIONS})
70+
71+
set_relative_file_macro(${NAME})
6972
endfunction()
7073

7174
# Same as add_devilutionx_library(${NAME} OBJECT) with an additional
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Sets the __FILE__ macro value to be relative to CMAKE_SOURCE_DIR.
2+
function(set_relative_file_macro TARGET)
3+
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
4+
if((CMAKE_CXX_COMPILER_ID MATCHES "CLANG" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12)
5+
OR (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8))
6+
target_compile_options(${TARGET} PUBLIC "-ffile-prefix-map=${CMAKE_SOURCE_DIR}/=")
7+
else()
8+
get_target_property(_srcs ${TARGET} SOURCES)
9+
foreach(_src ${_srcs})
10+
set_source_files_properties(${_src} PROPERTIES COMPILE_DEFINITIONS __FILE__="${_src}")
11+
endforeach()
12+
target_compile_options(${TARGET} PRIVATE -Wno-builtin-macro-redefined)
13+
endif()
14+
endif()
15+
endfunction()

CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,8 @@ if(BUILD_TESTING)
247247
add_subdirectory(test)
248248
endif()
249249

250-
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
251-
# Change __FILE__ to only show the path relative to the project folder
252-
get_target_property(libdevilutionx_SRCS ${BIN_TARGET} SOURCES)
253-
foreach(SOURCE_FILE ${libdevilutionx_SRCS})
254-
set_source_files_properties(${SOURCE_FILE} PROPERTIES
255-
COMPILE_DEFINITIONS __FILE__="${SOURCE_FILE}"
256-
)
257-
endforeach(SOURCE_FILE)
258-
259-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-builtin-macro-redefined")
260-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-builtin-macro-redefined")
261-
endif()
250+
include(functions/set_relative_file_macro)
251+
set_relative_file_macro(${BIN_TARGET})
262252

263253
if(APPLE)
264254
set_source_files_properties("./Packaging/apple/AppIcon.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources)

0 commit comments

Comments
 (0)