Skip to content

Commit 00b77f0

Browse files
committed
PrecompiledHeader.cmake: fix building pch for sources outside current dir
1 parent 24901dd commit 00b77f0

File tree

1 file changed

+81
-76
lines changed

1 file changed

+81
-76
lines changed

PrecompiledHeader.cmake

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -93,65 +93,70 @@ function(add_precompiled_header _target _input)
9393

9494
if(MSVC)
9595

96-
set(_cxx_path "${CMAKE_CFG_INTDIR}/${_target}_cxx_pch")
97-
set(_c_path "${CMAKE_CFG_INTDIR}/${_target}_c_pch")
98-
make_directory("${_cxx_path}")
99-
make_directory("${_c_path}")
96+
set(_cxx_path "${_target}_cxx_pch")
97+
set(_c_path "${_target}_c_pch")
98+
file(MAKE_DIRECTORY "${_cxx_path}")
99+
file(MAKE_DIRECTORY "${_c_path}")
100100
set(_pch_cxx_header "${_cxx_path}/${_input}")
101-
set(_pch_cxx_pch "${_cxx_path}/${_input_we}.pch")
101+
set(_pch_cxx_pch "${_cxx_path}/${_input_we}-$<CONFIG>.pch")
102102
set(_pch_c_header "${_c_path}/${_input}")
103-
set(_pch_c_pch "${_c_path}/${_input_we}.pch")
103+
set(_pch_c_pch "${_c_path}/${_input_we}-$<CONFIG>.pch")
104+
get_filename_component(_input_abs ${_input} ABSOLUTE)
104105

105106
get_target_property(sources ${_target} SOURCES)
106107
foreach(_source ${sources})
107108
set(_pch_compile_flags "")
108109
if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
109-
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
110-
set(_pch_header "${_input}")
111-
set(_pch "${_pch_cxx_pch}")
112-
else()
113-
set(_pch_header "${_input}")
114-
set(_pch "${_pch_c_pch}")
115-
endif()
116-
117-
if(_source STREQUAL "${_PCH_SOURCE_CXX}")
118-
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" /Yc${_input}")
119-
set(_pch_source_cxx_found TRUE)
110+
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
111+
set(_pch_header "${_input}")
112+
set(_pch "${_pch_cxx_pch}")
113+
else()
114+
set(_pch_header "${_input}")
115+
set(_pch "${_pch_c_pch}")
116+
endif()
117+
118+
get_filename_component(_source_abs ${_source} ABSOLUTE)
119+
get_filename_component(_source_dir ${_source_abs} DIRECTORY)
120+
file(RELATIVE_PATH _input_rel ${_source_dir} ${_input_abs})
121+
122+
if(_source STREQUAL "${_PCH_SOURCE_CXX}")
123+
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" \"/Yc${_input_rel}\"")
124+
set(_pch_source_cxx_found TRUE)
120125
set_source_files_properties("${_source}" PROPERTIES OBJECT_OUTPUTS "${_pch_cxx_pch}")
121-
elseif(_source STREQUAL "${_PCH_SOURCE_C}")
122-
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" /Yc${_input}")
123-
set(_pch_source_c_found TRUE)
126+
elseif(_source STREQUAL "${_PCH_SOURCE_C}")
127+
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" \"/Yc${_input_rel}\"")
128+
set(_pch_source_c_found TRUE)
124129
set_source_files_properties("${_source}" PROPERTIES OBJECT_OUTPUTS "${_pch_c_pch}")
125-
else()
126-
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
127-
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" /Yu${_input}")
128-
set(_pch_source_cxx_needed TRUE)
130+
else()
131+
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
132+
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" \"/Yu${_input_rel}\"")
133+
set(_pch_source_cxx_needed TRUE)
129134
set_source_files_properties("${_source}" PROPERTIES OBJECT_DEPENDS "${_pch_cxx_pch}")
130-
else()
131-
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" /Yu${_input}")
132-
set(_pch_source_c_needed TRUE)
135+
else()
136+
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" \"/Yu${_input_rel}\"")
137+
set(_pch_source_c_needed TRUE)
133138
set_source_files_properties("${_source}" PROPERTIES OBJECT_DEPENDS "${_pch_c_pch}")
134-
endif()
135-
if(_PCH_FORCEINCLUDE)
136-
set(_pch_compile_flags "${_pch_compile_flags} /FI${_input}")
137-
endif(_PCH_FORCEINCLUDE)
138-
endif()
139-
140-
get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
141-
if(NOT _object_depends)
142-
set(_object_depends)
143-
endif()
144-
if(_PCH_FORCEINCLUDE)
145-
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
146-
list(APPEND _object_depends "${_pch_header}")
147-
else()
148-
list(APPEND _object_depends "${_pch_header}")
149-
endif()
150-
endif()
151-
152-
set_source_files_properties(${_source} PROPERTIES
153-
COMPILE_FLAGS "${_pch_compile_flags}"
154-
OBJECT_DEPENDS "${_object_depends}")
139+
endif()
140+
if(_PCH_FORCEINCLUDE)
141+
set(_pch_compile_flags "${_pch_compile_flags} \"/FI${_input_rel}\"")
142+
endif(_PCH_FORCEINCLUDE)
143+
endif()
144+
145+
get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
146+
if(NOT _object_depends)
147+
set(_object_depends)
148+
endif()
149+
if(_PCH_FORCEINCLUDE)
150+
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
151+
list(APPEND _object_depends "${_pch_header}")
152+
else()
153+
list(APPEND _object_depends "${_pch_header}")
154+
endif()
155+
endif()
156+
157+
set_source_files_properties(${_source} PROPERTIES
158+
COMPILE_FLAGS "${_pch_compile_flags}"
159+
OBJECT_DEPENDS "${_object_depends}")
155160
endif()
156161
endforeach()
157162

@@ -169,7 +174,7 @@ function(add_precompiled_header _target _input)
169174
set(_pch_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch")
170175
set(_pchfile "${_pch_binary_dir}/${_input}")
171176
set(_outdir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch/${_name}.gch")
172-
make_directory(${_outdir})
177+
file(MAKE_DIRECTORY ${_outdir})
173178
set(_output_cxx "${_outdir}/.c++")
174179
set(_output_c "${_outdir}/.c")
175180

@@ -197,33 +202,33 @@ function(add_precompiled_header _target _input)
197202
set(_pch_compile_flags "")
198203

199204
if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
200-
get_source_file_property(_pch_compile_flags "${_source}" COMPILE_FLAGS)
201-
if(NOT _pch_compile_flags)
202-
set(_pch_compile_flags)
203-
endif()
204-
separate_arguments(_pch_compile_flags)
205-
list(APPEND _pch_compile_flags -Winvalid-pch)
206-
if(_PCH_FORCEINCLUDE)
207-
list(APPEND _pch_compile_flags -include "${_pchfile}")
208-
else(_PCH_FORCEINCLUDE)
209-
list(APPEND _pch_compile_flags "-I${_pch_binary_dir}")
210-
endif(_PCH_FORCEINCLUDE)
211-
212-
get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
213-
if(NOT _object_depends)
214-
set(_object_depends)
215-
endif()
216-
list(APPEND _object_depends "${_pchfile}")
217-
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
218-
list(APPEND _object_depends "${_output_cxx}")
219-
else()
220-
list(APPEND _object_depends "${_output_c}")
221-
endif()
222-
223-
combine_arguments(_pch_compile_flags)
224-
set_source_files_properties(${_source} PROPERTIES
225-
COMPILE_FLAGS "${_pch_compile_flags}"
226-
OBJECT_DEPENDS "${_object_depends}")
205+
get_source_file_property(_pch_compile_flags "${_source}" COMPILE_FLAGS)
206+
if(NOT _pch_compile_flags)
207+
set(_pch_compile_flags)
208+
endif()
209+
separate_arguments(_pch_compile_flags)
210+
list(APPEND _pch_compile_flags -Winvalid-pch)
211+
if(_PCH_FORCEINCLUDE)
212+
list(APPEND _pch_compile_flags -include "${_pchfile}")
213+
else(_PCH_FORCEINCLUDE)
214+
list(APPEND _pch_compile_flags "-I${_pch_binary_dir}")
215+
endif(_PCH_FORCEINCLUDE)
216+
217+
get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
218+
if(NOT _object_depends)
219+
set(_object_depends)
220+
endif()
221+
list(APPEND _object_depends "${_pchfile}")
222+
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
223+
list(APPEND _object_depends "${_output_cxx}")
224+
else()
225+
list(APPEND _object_depends "${_output_c}")
226+
endif()
227+
228+
combine_arguments(_pch_compile_flags)
229+
set_source_files_properties(${_source} PROPERTIES
230+
COMPILE_FLAGS "${_pch_compile_flags}"
231+
OBJECT_DEPENDS "${_object_depends}")
227232
endif()
228233
endforeach()
229234
endif(CMAKE_COMPILER_IS_GNUCXX)

0 commit comments

Comments
 (0)