summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2019-10-04 14:13:00 +0000
committerKitware Robot <kwrobot@kitware.com>2019-10-04 10:14:20 -0400
commita988cc8789bf00dd8783192f95e02ccbfea78302 (patch)
tree4f8d08899d0e544f05149079fc0e407b3c97824a
parentf9e2c17458d5d9998b8b87a953b50a4837fe80e9 (diff)
parent38c1f2ab222d4174137722a890c2a730d9083e2f (diff)
downloadcmake-a988cc8789bf00dd8783192f95e02ccbfea78302.tar.gz
Merge topic 'pch-multi-language'
38c1f2ab22 PCH: Fix CMake test files indentation 1144a4fa47 PCH: Add multi-language PCH generation support Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !3885
-rw-r--r--Source/cmLocalGenerator.cxx301
-rw-r--r--Tests/RunCMake/PrecompileHeaders/DisabledPch-check.cmake16
-rw-r--r--Tests/RunCMake/PrecompileHeaders/PchMultilanguage-check.cmake17
-rw-r--r--Tests/RunCMake/PrecompileHeaders/PchMultilanguage.cmake9
-rw-r--r--Tests/RunCMake/PrecompileHeaders/PchPrologueEpilogue-check.cmake4
-rw-r--r--Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake1
6 files changed, 195 insertions, 153 deletions
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 154d509cd2..ae5139adb0 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -2254,185 +2254,200 @@ void cmLocalGenerator::AppendFlagEscape(std::string& flags,
void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target,
const std::string& config)
{
- const std::string lang = target->GetLinkerLanguage(config);
const std::string buildType = cmSystemTools::UpperCase(config);
- const std::string pchSource = target->GetPchSource(config, lang);
- const std::string pchHeader = target->GetPchHeader(config, lang);
- if (pchSource.empty() || pchHeader.empty()) {
- return;
- }
+ std::vector<cmSourceFile*> sources;
+ target->GetSourceFiles(sources, buildType);
- const std::string createOptVar =
- cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_CREATE_PCH");
- std::string createOptionList =
- this->Makefile->GetSafeDefinition(createOptVar);
+ for (const std::string& lang : { "C", "CXX" }) {
+ auto langSources =
+ std::count_if(sources.begin(), sources.end(), [lang](cmSourceFile* sf) {
+ return lang == sf->GetLanguage();
+ });
+ if (langSources == 0) {
+ continue;
+ }
- const std::string useOptVar =
- cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_USE_PCH");
- std::string useOptionList = this->Makefile->GetSafeDefinition(useOptVar);
+ const std::string pchSource = target->GetPchSource(config, lang);
+ const std::string pchHeader = target->GetPchHeader(config, lang);
- const std::string pchExtension =
- this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
+ if (pchSource.empty() || pchHeader.empty()) {
+ continue;
+ }
- if (createOptionList.empty() || useOptionList.empty() ||
- pchExtension.empty()) {
- return;
- }
+ const std::string createOptVar =
+ cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_CREATE_PCH");
+ std::string createOptionList =
+ this->Makefile->GetSafeDefinition(createOptVar);
- const char* pchReuseFrom =
- target->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
+ const std::string useOptVar =
+ cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_USE_PCH");
+ std::string useOptionList = this->Makefile->GetSafeDefinition(useOptVar);
- auto pch_sf = this->Makefile->GetOrCreateSource(
- pchSource, false, cmSourceFileLocationKind::Known);
- std::string pchFile = pchHeader;
+ const std::string pchExtension =
+ this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
- if (!this->GetGlobalGenerator()->IsXcode()) {
- if (!pchReuseFrom) {
- target->AddSource(pchSource, true);
+ if (createOptionList.empty() || useOptionList.empty() ||
+ pchExtension.empty()) {
+ continue;
}
- // Exclude the pch files from linking
- if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
+ const char* pchReuseFrom =
+ target->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
- auto replaceExtension = [](const std::string& str,
- const std::string& ext) -> std::string {
- auto dot_pos = str.rfind('.');
- std::string result;
- if (dot_pos != std::string::npos) {
- result = str.substr(0, dot_pos);
- }
- result += ext;
- return result;
- };
+ auto pch_sf = this->Makefile->GetOrCreateSource(
+ pchSource, false, cmSourceFileLocationKind::Known);
+ std::string pchFile = pchHeader;
+ if (!this->GetGlobalGenerator()->IsXcode()) {
if (!pchReuseFrom) {
- std::string pchSourceObj = target->GetPchFileObject(config, lang);
+ target->AddSource(pchSource, true);
+ }
- pchFile = replaceExtension(pchSourceObj, pchExtension);
- pch_sf->SetProperty("OBJECT_OUTPUTS", pchFile.c_str());
- } else {
- auto reuseTarget =
- this->GlobalGenerator->FindGeneratorTarget(pchReuseFrom);
+ // Exclude the pch files from linking
+ if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
- if (this->Makefile->IsOn("CMAKE_PCH_COPY_COMPILE_PDB")) {
+ auto replaceExtension = [](const std::string& str,
+ const std::string& ext) -> std::string {
+ auto dot_pos = str.rfind('.');
+ std::string result;
+ if (dot_pos != std::string::npos) {
+ result = str.substr(0, dot_pos);
+ }
+ result += ext;
+ return result;
+ };
- const std::string pdb_prefix =
- this->GetGlobalGenerator()->IsMultiConfig()
- ? cmStrCat(this->GlobalGenerator->GetCMakeCFGIntDir(), "/")
- : "";
+ if (!pchReuseFrom) {
+ std::string pchSourceObj = target->GetPchFileObject(config, lang);
- const std::string target_compile_pdb_dir =
- cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(),
- "/", target->GetName(), ".dir/");
+ pchFile = replaceExtension(pchSourceObj, pchExtension);
+ pch_sf->SetProperty("OBJECT_OUTPUTS", pchFile.c_str());
+ } else {
+ auto reuseTarget =
+ this->GlobalGenerator->FindGeneratorTarget(pchReuseFrom);
- const std::string copy_script =
- cmStrCat(target_compile_pdb_dir, "copy_idb_pdb.cmake");
- cmGeneratedFileStream file(copy_script);
+ if (this->Makefile->IsOn("CMAKE_PCH_COPY_COMPILE_PDB")) {
- file << "# CMake generated file\n";
- for (auto extension : { ".pdb", ".idb" }) {
- const std::string from_file = cmStrCat(
- reuseTarget->GetLocalGenerator()->GetCurrentBinaryDirectory(),
- "/", pchReuseFrom, ".dir/${PDB_PREFIX}", pchReuseFrom,
- extension);
+ const std::string pdb_prefix =
+ this->GetGlobalGenerator()->IsMultiConfig()
+ ? cmStrCat(this->GlobalGenerator->GetCMakeCFGIntDir(), "/")
+ : "";
- const std::string to_dir = cmStrCat(
+ const std::string target_compile_pdb_dir = cmStrCat(
target->GetLocalGenerator()->GetCurrentBinaryDirectory(), "/",
- target->GetName(), ".dir/${PDB_PREFIX}");
-
- file << "if (EXISTS \"" << from_file << "\")\n";
- file << " file(COPY \"" << from_file << "\""
- << " DESTINATION \"" << to_dir << "\")\n";
- file << "endif()\n";
- }
+ target->GetName(), ".dir/");
+
+ const std::string copy_script =
+ cmStrCat(target_compile_pdb_dir, "copy_idb_pdb.cmake");
+ cmGeneratedFileStream file(copy_script);
+
+ file << "# CMake generated file\n";
+ for (auto extension : { ".pdb", ".idb" }) {
+ const std::string from_file = cmStrCat(
+ reuseTarget->GetLocalGenerator()->GetCurrentBinaryDirectory(),
+ "/", pchReuseFrom, ".dir/${PDB_PREFIX}", pchReuseFrom,
+ extension);
+
+ const std::string to_dir = cmStrCat(
+ target->GetLocalGenerator()->GetCurrentBinaryDirectory(), "/",
+ target->GetName(), ".dir/${PDB_PREFIX}");
+
+ file << "if (EXISTS \"" << from_file << "\")\n";
+ file << " file(COPY \"" << from_file << "\""
+ << " DESTINATION \"" << to_dir << "\")\n";
+ file << "endif()\n";
+ }
- cmCustomCommandLines commandLines;
- cmCustomCommandLine currentLine;
- currentLine.push_back(cmSystemTools::GetCMakeCommand());
- currentLine.push_back(cmStrCat("-DPDB_PREFIX=", pdb_prefix));
- currentLine.push_back("-P");
- currentLine.push_back(copy_script);
- commandLines.push_back(std::move(currentLine));
-
- const std::string no_main_dependency;
- const std::vector<std::string> no_deps;
- const char* no_message = "";
- const char* no_current_dir = nullptr;
- std::vector<std::string> no_byproducts;
-
- std::vector<std::string> outputs;
- outputs.push_back(cmStrCat(target_compile_pdb_dir, pdb_prefix,
- pchReuseFrom, ".pdb"));
-
- if (this->GetGlobalGenerator()->IsMultiConfig()) {
- this->Makefile->AddCustomCommandToTarget(
- target->GetName(), outputs, no_deps, commandLines,
- cmCustomCommandType::PRE_BUILD, no_message, no_current_dir);
- } else {
- cmImplicitDependsList no_implicit_depends;
- cmSourceFile* copy_rule = this->Makefile->AddCustomCommandToOutput(
- outputs, no_byproducts, no_deps, no_main_dependency,
- no_implicit_depends, commandLines, no_message, no_current_dir);
-
- if (copy_rule) {
- target->AddSource(copy_rule->ResolveFullPath());
+ cmCustomCommandLines commandLines;
+ cmCustomCommandLine currentLine;
+ currentLine.push_back(cmSystemTools::GetCMakeCommand());
+ currentLine.push_back(cmStrCat("-DPDB_PREFIX=", pdb_prefix));
+ currentLine.push_back("-P");
+ currentLine.push_back(copy_script);
+ commandLines.push_back(std::move(currentLine));
+
+ const std::string no_main_dependency;
+ const std::vector<std::string> no_deps;
+ const char* no_message = "";
+ const char* no_current_dir = nullptr;
+ std::vector<std::string> no_byproducts;
+
+ std::vector<std::string> outputs;
+ outputs.push_back(cmStrCat(target_compile_pdb_dir, pdb_prefix,
+ pchReuseFrom, ".pdb"));
+
+ if (this->GetGlobalGenerator()->IsMultiConfig()) {
+ this->Makefile->AddCustomCommandToTarget(
+ target->GetName(), outputs, no_deps, commandLines,
+ cmCustomCommandType::PRE_BUILD, no_message, no_current_dir);
+ } else {
+ cmImplicitDependsList no_implicit_depends;
+ cmSourceFile* copy_rule =
+ this->Makefile->AddCustomCommandToOutput(
+ outputs, no_byproducts, no_deps, no_main_dependency,
+ no_implicit_depends, commandLines, no_message,
+ no_current_dir);
+
+ if (copy_rule) {
+ target->AddSource(copy_rule->ResolveFullPath());
+ }
}
- }
- target->Target->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
- target_compile_pdb_dir.c_str());
- }
+ target->Target->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
+ target_compile_pdb_dir.c_str());
+ }
- std::string pchSourceObj = reuseTarget->GetPchFileObject(config, lang);
+ std::string pchSourceObj =
+ reuseTarget->GetPchFileObject(config, lang);
- // Link to the pch object file
- target->Target->SetProperty(
- "LINK_FLAGS",
- this->ConvertToOutputFormat(pchSourceObj, SHELL).c_str());
+ // Link to the pch object file
+ target->Target->SetProperty(
+ "LINK_FLAGS",
+ this->ConvertToOutputFormat(pchSourceObj, SHELL).c_str());
- pchFile = replaceExtension(pchSourceObj, pchExtension);
+ pchFile = replaceExtension(pchSourceObj, pchExtension);
+ }
+ } else {
+ pchFile += pchExtension;
+ pch_sf->SetProperty("PCH_EXTENSION", pchExtension.c_str());
}
- } else {
- pchFile += pchExtension;
- pch_sf->SetProperty("PCH_EXTENSION", pchExtension.c_str());
- }
-
- // Add pchHeader to source files, which will
- // be grouped as "Precompile Header File"
- auto pchHeader_sf = this->Makefile->GetOrCreateSource(
- pchHeader, false, cmSourceFileLocationKind::Known);
- std::string err;
- pchHeader_sf->ResolveFullPath(&err);
- target->AddSource(pchHeader);
- for (auto& str : { std::ref(useOptionList), std::ref(createOptionList) }) {
- cmSystemTools::ReplaceString(str, "<PCH_HEADER>", pchHeader);
- cmSystemTools::ReplaceString(str, "<PCH_FILE>", pchFile);
+ // Add pchHeader to source files, which will
+ // be grouped as "Precompile Header File"
+ auto pchHeader_sf = this->Makefile->GetOrCreateSource(
+ pchHeader, false, cmSourceFileLocationKind::Known);
+ std::string err;
+ pchHeader_sf->ResolveFullPath(&err);
+ target->AddSource(pchHeader);
+
+ for (auto& str :
+ { std::ref(useOptionList), std::ref(createOptionList) }) {
+ cmSystemTools::ReplaceString(str, "<PCH_HEADER>", pchHeader);
+ cmSystemTools::ReplaceString(str, "<PCH_FILE>", pchFile);
+ }
}
- }
- pch_sf->SetProperty("COMPILE_OPTIONS", createOptionList.c_str());
+ pch_sf->SetProperty("COMPILE_OPTIONS", createOptionList.c_str());
- std::vector<cmSourceFile*> sources;
- target->GetSourceFiles(sources, buildType);
- for (cmSourceFile* sf : sources) {
- if (pch_sf == sf || sf->GetLanguage() != lang) {
- continue;
- }
+ for (cmSourceFile* sf : sources) {
+ if (pch_sf == sf || sf->GetLanguage() != lang) {
+ continue;
+ }
- if (sf->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
- if (this->GetGlobalGenerator()->IsXcode()) {
- sf->SetProperty("COMPILE_DEFINITIONS",
- "CMAKE_SKIP_PRECOMPILE_HEADERS");
+ if (sf->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
+ if (this->GetGlobalGenerator()->IsXcode()) {
+ sf->SetProperty("COMPILE_DEFINITIONS",
+ "CMAKE_SKIP_PRECOMPILE_HEADERS");
+ }
+ continue;
}
- continue;
- }
- if (!this->GetGlobalGenerator()->IsXcode()) {
- sf->AppendProperty("OBJECT_DEPENDS", pchFile.c_str());
- sf->AppendProperty("OBJECT_DEPENDS", pchHeader.c_str());
- sf->SetProperty("COMPILE_OPTIONS", useOptionList.c_str());
+ if (!this->GetGlobalGenerator()->IsXcode()) {
+ sf->AppendProperty("OBJECT_DEPENDS", pchFile.c_str());
+ sf->AppendProperty("OBJECT_DEPENDS", pchHeader.c_str());
+ sf->SetProperty("COMPILE_OPTIONS", useOptionList.c_str());
+ }
}
}
}
diff --git a/Tests/RunCMake/PrecompileHeaders/DisabledPch-check.cmake b/Tests/RunCMake/PrecompileHeaders/DisabledPch-check.cmake
index fa37c2c0b9..8cf0fc9928 100644
--- a/Tests/RunCMake/PrecompileHeaders/DisabledPch-check.cmake
+++ b/Tests/RunCMake/PrecompileHeaders/DisabledPch-check.cmake
@@ -1,17 +1,17 @@
if (NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
- set(foo_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foo.dir/CMakeFiles/foo.dir/cmake_pch.h")
- set(foobar_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/CMakeFiles/foobar.dir/cmake_pch.h")
+ set(foo_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foo.dir/CMakeFiles/foo.dir/cmake_pch.h")
+ set(foobar_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/CMakeFiles/foobar.dir/cmake_pch.h")
else()
- set(foo_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foo.dir/cmake_pch.h")
- set(foobar_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/cmake_pch.h")
+ set(foo_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foo.dir/cmake_pch.h")
+ set(foobar_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/cmake_pch.h")
endif()
if (NOT EXISTS ${foo_pch_header})
- set(RunCMake_TEST_FAILED "Generated foo pch header ${foo_pch_header} does not exist")
- return()
+ set(RunCMake_TEST_FAILED "Generated foo pch header ${foo_pch_header} does not exist")
+ return()
endif()
if (EXISTS ${foobar_pch_header})
- set(RunCMake_TEST_FAILED "Generated foobar pch header ${foobar_pch_header} should not exist")
- return()
+ set(RunCMake_TEST_FAILED "Generated foobar pch header ${foobar_pch_header} should not exist")
+ return()
endif()
diff --git a/Tests/RunCMake/PrecompileHeaders/PchMultilanguage-check.cmake b/Tests/RunCMake/PrecompileHeaders/PchMultilanguage-check.cmake
new file mode 100644
index 0000000000..44fe2dafa0
--- /dev/null
+++ b/Tests/RunCMake/PrecompileHeaders/PchMultilanguage-check.cmake
@@ -0,0 +1,17 @@
+if (NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
+ set(foobar_pch_h_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/CMakeFiles/foobar.dir/cmake_pch.h")
+ set(foobar_pch_hxx_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/CMakeFiles/foobar.dir/cmake_pch.hxx")
+else()
+ set(foobar_pch_h_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/cmake_pch.h")
+ set(foobar_pch_hxx_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/foobar.dir/cmake_pch.hxx")
+endif()
+
+if (NOT EXISTS ${foobar_pch_h_header})
+ set(RunCMake_TEST_FAILED "Generated foobar C pch header ${foobar_pch_h_header} does not exist")
+ return()
+endif()
+
+if (NOT EXISTS ${foobar_pch_hxx_header})
+ set(RunCMake_TEST_FAILED "Generated foobar C++ pch header ${foobar_pch_hxx_header} does not exist")
+ return()
+endif()
diff --git a/Tests/RunCMake/PrecompileHeaders/PchMultilanguage.cmake b/Tests/RunCMake/PrecompileHeaders/PchMultilanguage.cmake
new file mode 100644
index 0000000000..7a837daf01
--- /dev/null
+++ b/Tests/RunCMake/PrecompileHeaders/PchMultilanguage.cmake
@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 3.15)
+project(PchMultilanguage C CXX)
+
+add_executable(foobar
+ foo.c
+ main.cpp
+)
+target_include_directories(foobar PUBLIC include)
+target_precompile_headers(foobar PRIVATE "<stddef.h>")
diff --git a/Tests/RunCMake/PrecompileHeaders/PchPrologueEpilogue-check.cmake b/Tests/RunCMake/PrecompileHeaders/PchPrologueEpilogue-check.cmake
index fd82607bcb..ba220f3be8 100644
--- a/Tests/RunCMake/PrecompileHeaders/PchPrologueEpilogue-check.cmake
+++ b/Tests/RunCMake/PrecompileHeaders/PchPrologueEpilogue-check.cmake
@@ -1,7 +1,7 @@
if (NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
- set(main_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/main.dir/CMakeFiles/main.dir/cmake_pch.hxx")
+ set(main_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/main.dir/CMakeFiles/main.dir/cmake_pch.hxx")
else()
- set(main_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/main.dir/cmake_pch.hxx")
+ set(main_pch_header "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/main.dir/cmake_pch.hxx")
endif()
file(STRINGS ${main_pch_header} main_pch_header_strings)
diff --git a/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake b/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake
index bd3b1b82a0..ec13663d5c 100644
--- a/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake
+++ b/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake
@@ -18,3 +18,4 @@ run_cmake(PchPrologueEpilogue)
run_test(SkipPrecompileHeaders)
run_test(PchReuseFrom)
run_test(PchReuseFromSubdir)
+run_cmake(PchMultilanguage)