Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrecompiledHeader.cmake: Fix setting SOURCE_CXX & SOURCE_C; recognize pch in subdirectories #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
164 changes: 87 additions & 77 deletions PrecompiledHeader.cmake
Expand Up @@ -76,9 +76,14 @@ function(export_all_flags _filename)
endfunction()

function(add_precompiled_header _target _input)
cmake_parse_arguments(_PCH "FORCEINCLUDE" "SOURCE_CXX:SOURCE_C" "" ${ARGN})
cmake_parse_arguments(_PCH "FORCEINCLUDE" "SOURCE_CXX;SOURCE_C" "" ${ARGN})

get_filename_component(_input_dir ${_input} DIRECTORY)
get_filename_component(_input_we ${_input} NAME_WE)
if(_input_dir)
set(_input_we "${_input_dir}/${_input_we}")
endif()

if(NOT _PCH_SOURCE_CXX)
set(_PCH_SOURCE_CXX "${_input_we}.cpp")
endif()
Expand All @@ -88,65 +93,70 @@ function(add_precompiled_header _target _input)

if(MSVC)

set(_cxx_path "${CMAKE_CFG_INTDIR}/${_target}_cxx_pch")
set(_c_path "${CMAKE_CFG_INTDIR}/${_target}_c_pch")
make_directory("${_cxx_path}")
make_directory("${_c_path}")
set(_cxx_path "${_target}_cxx_pch")
set(_c_path "${_target}_c_pch")
file(MAKE_DIRECTORY "${_cxx_path}")
file(MAKE_DIRECTORY "${_c_path}")
set(_pch_cxx_header "${_cxx_path}/${_input}")
set(_pch_cxx_pch "${_cxx_path}/${_input_we}.pch")
set(_pch_cxx_pch "${_cxx_path}/${_input_we}-$<CONFIG>.pch")
set(_pch_c_header "${_c_path}/${_input}")
set(_pch_c_pch "${_c_path}/${_input_we}.pch")
set(_pch_c_pch "${_c_path}/${_input_we}-$<CONFIG>.pch")
get_filename_component(_input_abs ${_input} ABSOLUTE)

get_target_property(sources ${_target} SOURCES)
foreach(_source ${sources})
set(_pch_compile_flags "")
if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
set(_pch_header "${_input}")
set(_pch "${_pch_cxx_pch}")
else()
set(_pch_header "${_input}")
set(_pch "${_pch_c_pch}")
endif()

if(_source STREQUAL "${_PCH_SOURCE_CXX}")
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" /Yc${_input}")
set(_pch_source_cxx_found TRUE)
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
set(_pch_header "${_input}")
set(_pch "${_pch_cxx_pch}")
else()
set(_pch_header "${_input}")
set(_pch "${_pch_c_pch}")
endif()

get_filename_component(_source_abs ${_source} ABSOLUTE)
get_filename_component(_source_dir ${_source_abs} DIRECTORY)
file(RELATIVE_PATH _input_rel ${_source_dir} ${_input_abs})

if(_source STREQUAL "${_PCH_SOURCE_CXX}")
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" \"/Yc${_input_rel}\"")
set(_pch_source_cxx_found TRUE)
set_source_files_properties("${_source}" PROPERTIES OBJECT_OUTPUTS "${_pch_cxx_pch}")
elseif(_source STREQUAL "${_PCH_SOURCE_C}")
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" /Yc${_input}")
set(_pch_source_c_found TRUE)
elseif(_source STREQUAL "${_PCH_SOURCE_C}")
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" \"/Yc${_input_rel}\"")
set(_pch_source_c_found TRUE)
set_source_files_properties("${_source}" PROPERTIES OBJECT_OUTPUTS "${_pch_c_pch}")
else()
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" /Yu${_input}")
set(_pch_source_cxx_needed TRUE)
else()
if(_source MATCHES \\.\(cpp|cxx|cc\)$)
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" \"/Yu${_input_rel}\"")
set(_pch_source_cxx_needed TRUE)
set_source_files_properties("${_source}" PROPERTIES OBJECT_DEPENDS "${_pch_cxx_pch}")
else()
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" /Yu${_input}")
set(_pch_source_c_needed TRUE)
else()
set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" \"/Yu${_input_rel}\"")
set(_pch_source_c_needed TRUE)
set_source_files_properties("${_source}" PROPERTIES OBJECT_DEPENDS "${_pch_c_pch}")
endif()
if(_PCH_FORCEINCLUDE)
set(_pch_compile_flags "${_pch_compile_flags} /FI${_input}")
endif(_PCH_FORCEINCLUDE)
endif()

get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
if(NOT _object_depends)
set(_object_depends)
endif()
if(_PCH_FORCEINCLUDE)
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
list(APPEND _object_depends "${_pch_header}")
else()
list(APPEND _object_depends "${_pch_header}")
endif()
endif()

set_source_files_properties(${_source} PROPERTIES
COMPILE_FLAGS "${_pch_compile_flags}"
OBJECT_DEPENDS "${_object_depends}")
endif()
if(_PCH_FORCEINCLUDE)
set(_pch_compile_flags "${_pch_compile_flags} \"/FI${_input_rel}\"")
endif(_PCH_FORCEINCLUDE)
endif()

get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
if(NOT _object_depends)
set(_object_depends)
endif()
if(_PCH_FORCEINCLUDE)
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
list(APPEND _object_depends "${_pch_header}")
else()
list(APPEND _object_depends "${_pch_header}")
endif()
endif()

set_source_files_properties(${_source} PROPERTIES
COMPILE_FLAGS "${_pch_compile_flags}"
OBJECT_DEPENDS "${_object_depends}")
endif()
endforeach()

Expand All @@ -164,7 +174,7 @@ function(add_precompiled_header _target _input)
set(_pch_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch")
set(_pchfile "${_pch_binary_dir}/${_input}")
set(_outdir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch/${_name}.gch")
make_directory(${_outdir})
file(MAKE_DIRECTORY ${_outdir})
set(_output_cxx "${_outdir}/.c++")
set(_output_c "${_outdir}/.c")

Expand Down Expand Up @@ -192,33 +202,33 @@ function(add_precompiled_header _target _input)
set(_pch_compile_flags "")

if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
get_source_file_property(_pch_compile_flags "${_source}" COMPILE_FLAGS)
if(NOT _pch_compile_flags)
set(_pch_compile_flags)
endif()
separate_arguments(_pch_compile_flags)
list(APPEND _pch_compile_flags -Winvalid-pch)
if(_PCH_FORCEINCLUDE)
list(APPEND _pch_compile_flags -include "${_pchfile}")
else(_PCH_FORCEINCLUDE)
list(APPEND _pch_compile_flags "-I${_pch_binary_dir}")
endif(_PCH_FORCEINCLUDE)

get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
if(NOT _object_depends)
set(_object_depends)
endif()
list(APPEND _object_depends "${_pchfile}")
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
list(APPEND _object_depends "${_output_cxx}")
else()
list(APPEND _object_depends "${_output_c}")
endif()

combine_arguments(_pch_compile_flags)
set_source_files_properties(${_source} PROPERTIES
COMPILE_FLAGS "${_pch_compile_flags}"
OBJECT_DEPENDS "${_object_depends}")
get_source_file_property(_pch_compile_flags "${_source}" COMPILE_FLAGS)
if(NOT _pch_compile_flags)
set(_pch_compile_flags)
endif()
separate_arguments(_pch_compile_flags)
list(APPEND _pch_compile_flags -Winvalid-pch)
if(_PCH_FORCEINCLUDE)
list(APPEND _pch_compile_flags -include "${_pchfile}")
else(_PCH_FORCEINCLUDE)
list(APPEND _pch_compile_flags "-I${_pch_binary_dir}")
endif(_PCH_FORCEINCLUDE)

get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
if(NOT _object_depends)
set(_object_depends)
endif()
list(APPEND _object_depends "${_pchfile}")
if(_source MATCHES \\.\(cc|cxx|cpp\)$)
list(APPEND _object_depends "${_output_cxx}")
else()
list(APPEND _object_depends "${_output_c}")
endif()

combine_arguments(_pch_compile_flags)
set_source_files_properties(${_source} PROPERTIES
COMPILE_FLAGS "${_pch_compile_flags}"
OBJECT_DEPENDS "${_object_depends}")
endif()
endforeach()
endif(CMAKE_COMPILER_IS_GNUCXX)
Expand Down