[CMAKE] Fix problem with __RELFILE__ not working when compiler uses relative pathes
authorTimo Kreuzer <timo.kreuzer@reactos.org>
Sun, 9 Dec 2018 16:51:50 +0000 (17:51 +0100)
committerTimo Kreuzer <timo.kreuzer@reactos.org>
Tue, 11 Dec 2018 20:50:58 +0000 (21:50 +0100)
This usually happens when the build dir is a subdir of the source dir. It is now detected during runtime, using the length of the relative path from the build dir to the source dir as the length of the prefix to skip, if __FILE__ starts with a '.'.
Also fix the escaping of REACTOS_*_DIR defines. It was gracefully fixed up by CMake, but resulted in broken syntax highliting.

CORE-14839 #resolve

CMakeLists.txt

index d2e8b86..c931d01 100644 (file)
@@ -57,9 +57,14 @@ endif()
 include(sdk/cmake/compilerflags.cmake)
 
 add_definitions(-D__REACTOS__)
-add_definitions(-DREACTOS_SOURCE_DIR="\\"${REACTOS_SOURCE_DIR}\\"")
-add_definitions(-DREACTOS_BINARY_DIR="\\"${REACTOS_BINARY_DIR}\\"")
-add_compile_flags(-D__RELFILE__="&__FILE__[sizeof REACTOS_SOURCE_DIR]")
+
+# Double escape, since CMake unescapes before putting it on the command-line, where it's unescaped again by GCC/CL.
+add_definitions(-DREACTOS_SOURCE_DIR="\\\"${REACTOS_SOURCE_DIR}\\\"")
+add_definitions(-DREACTOS_BINARY_DIR="\\\"${REACTOS_BINARY_DIR}\\\"")
+
+# There doesn't seem to be a standard for __FILE__ being relative or absolute, so detect it at runtime.
+file(RELATIVE_PATH _PATH_PREFIX ${REACTOS_BINARY_DIR} ${REACTOS_SOURCE_DIR})
+add_compile_flags(-D__RELFILE__="&__FILE__[__FILE__[0] == '.' ? sizeof \\\"${_PATH_PREFIX}\\\" - 1 : sizeof REACTOS_SOURCE_DIR]")
 
 if(MSVC_IDE)
     add_compile_flags("/MP")