From 0b945ea9a6a38d1b3ee27cc32afb4268bd571600 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 4 Mar 2015 21:46:42 +0100 Subject: Genex: Allow COMPILE_LANGUAGE when processing compile definitions. Issue an error if this is encountered by an IDE generator. --- Help/manual/cmake-generator-expressions.7.rst | 10 +++++++ Source/cmExtraCodeBlocksGenerator.cxx | 2 +- Source/cmExtraSublimeTextGenerator.cxx | 2 +- Source/cmGeneratorExpressionEvaluator.cxx | 15 ++++++++-- Source/cmGlobalXCodeGenerator.cxx | 2 +- Source/cmLocalGenerator.cxx | 6 ++-- Source/cmLocalGenerator.h | 3 +- Source/cmLocalUnixMakefileGenerator3.cxx | 32 +++++++++++----------- Source/cmLocalVisualStudio6Generator.cxx | 10 +++---- Source/cmLocalVisualStudio7Generator.cxx | 2 +- Source/cmMakefileTargetGenerator.cxx | 2 +- Source/cmNinjaTargetGenerator.cxx | 2 +- Source/cmQtAutoGenerators.cxx | 2 +- Source/cmTarget.cxx | 14 ++++++---- Source/cmTarget.h | 3 +- Source/cmVisualStudio10TargetGenerator.cxx | 3 +- .../target_compile_definitions/CMakeLists.txt | 15 ++++++++++ .../target_compile_definitions/consumer.c | 23 ++++++++++++++++ .../target_compile_definitions/consumer.cpp | 18 ++++++++++++ .../CompileDefinitions-result.txt | 1 + .../CompileDefinitions-stderr-VS.txt | 8 ++++++ .../CompileDefinitions-stderr-Xcode.txt | 9 ++++++ .../CompileDefinitions.cmake | 5 ++++ .../COMPILE_LANGUAGE-genex/RunCMakeTest.cmake | 7 +++++ 24 files changed, 154 insertions(+), 42 deletions(-) create mode 100644 Tests/CMakeCommands/target_compile_definitions/consumer.c create mode 100644 Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-result.txt create mode 100644 Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-VS.txt create mode 100644 Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-Xcode.txt create mode 100644 Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions.cmake diff --git a/Help/manual/cmake-generator-expressions.7.rst b/Help/manual/cmake-generator-expressions.7.rst index b1b1102..b6d97d1 100644 --- a/Help/manual/cmake-generator-expressions.7.rst +++ b/Help/manual/cmake-generator-expressions.7.rst @@ -120,6 +120,16 @@ Available logical expressions are: add_executable(myapp main.cpp) target_link_libraries(myapp myapp_c myapp_cxx) + The ``Makefile`` and ``Ninja`` based generators can also use this + expression to specify compile-language specific compile definitions: + + .. code-block:: cmake + + add_executable(myapp main.cpp foo.c bar.cpp) + target_compile_definitions(myapp + PRIVATE $<$:COMPILING_CXX> + ) + Informational Expressions ========================= diff --git a/Source/cmExtraCodeBlocksGenerator.cxx b/Source/cmExtraCodeBlocksGenerator.cxx index 69857ef..614e7d9 100644 --- a/Source/cmExtraCodeBlocksGenerator.cxx +++ b/Source/cmExtraCodeBlocksGenerator.cxx @@ -599,7 +599,7 @@ void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout, // the compilerdefines for this target std::vector cdefs; - target->GetCompileDefinitions(cdefs, buildType); + target->GetCompileDefinitions(cdefs, buildType, "C"); // Expand the list. for(std::vector::const_iterator di = cdefs.begin(); diff --git a/Source/cmExtraSublimeTextGenerator.cxx b/Source/cmExtraSublimeTextGenerator.cxx index 5fff0fb..25f9005 100644 --- a/Source/cmExtraSublimeTextGenerator.cxx +++ b/Source/cmExtraSublimeTextGenerator.cxx @@ -436,7 +436,7 @@ ComputeDefines(cmSourceFile *source, cmLocalGenerator* lg, cmTarget *target, } // Add preprocessor definitions for this target and configuration. - lg->AddCompileDefinitions(defines, target, config); + lg->AddCompileDefinitions(defines, target, config, language); lg->AppendDefines(defines, source->GetProperty("COMPILE_DEFINITIONS")); { std::string defPropName = "COMPILE_DEFINITIONS_"; diff --git a/Source/cmGeneratorExpressionEvaluator.cxx b/Source/cmGeneratorExpressionEvaluator.cxx index 6a9f251..3ec8595 100644 --- a/Source/cmGeneratorExpressionEvaluator.cxx +++ b/Source/cmGeneratorExpressionEvaluator.cxx @@ -817,7 +817,7 @@ static const struct CompileLanguageNode : public cmGeneratorExpressionNode std::string Evaluate(const std::vector ¶meters, cmGeneratorExpressionContext *context, const GeneratorExpressionContent *content, - cmGeneratorExpressionDAGChecker *) const + cmGeneratorExpressionDAGChecker *dagChecker) const { if(context->Language.empty()) { @@ -849,12 +849,21 @@ static const struct CompileLanguageNode : public cmGeneratorExpressionNode "generators."); return std::string(); } + else if (genName.find("Xcode") != std::string::npos) + { + if (dagChecker && dagChecker->EvaluatingCompileDefinitions()) + { + reportError(context, content->GetOriginalExpression(), + "$ may only be used with COMPILE_OPTIONS " + "with the Xcode generator."); + return std::string(); + } + } else { if(genName.find("Makefiles") == std::string::npos && genName.find("Ninja") == std::string::npos && - genName.find("Watcom WMake") == std::string::npos && - genName.find("Xcode") == std::string::npos) + genName.find("Watcom WMake") == std::string::npos) { reportError(context, content->GetOriginalExpression(), "$ not supported for this generator."); diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index e89161d..bd8a1f5 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -1803,7 +1803,7 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target, } cmGeneratorTarget *gtgt = this->GetGeneratorTarget(&target); std::vector targetDefines; - target.GetCompileDefinitions(targetDefines, configName); + target.GetCompileDefinitions(targetDefines, configName, "C"); this->AppendDefines(ppDefs, targetDefines); buildSettings->AddAttribute ("GCC_PREPROCESSOR_DEFINITIONS", ppDefs.CreateList()); diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index be82085..735dfa9 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1428,11 +1428,11 @@ std::string cmLocalGenerator::GetIncludeFlags( //---------------------------------------------------------------------------- void cmLocalGenerator::AddCompileDefinitions(std::set& defines, cmTarget const* target, - const std::string& config) + const std::string& config, + const std::string& lang) { std::vector targetDefines; - target->GetCompileDefinitions(targetDefines, - config); + target->GetCompileDefinitions(targetDefines, config, lang); this->AppendDefines(defines, targetDefines); } diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 3a9d5be..f1f0da1 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -239,7 +239,8 @@ public: const std::string& lang, const std::string& config); void AddCompileDefinitions(std::set& defines, cmTarget const* target, - const std::string& config); + const std::string& config, + const std::string& lang); /** Compute the language used to compile the given source file. */ std::string GetSourceFileLanguage(const cmSourceFile& source); diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 432cb3a..9ecb029 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -2094,26 +2094,26 @@ void cmLocalUnixMakefileGenerator3 << "set(CMAKE_" << l->first << "_COMPILER_ID \"" << cid << "\")\n"; } - } - // Build a list of preprocessor definitions for the target. - std::set defines; - this->AddCompileDefinitions(defines, &target, - this->ConfigurationName); - if(!defines.empty()) - { - cmakefileStream - << "\n" - << "# Preprocessor definitions for this target.\n" - << "set(CMAKE_TARGET_DEFINITIONS\n"; - for(std::set::const_iterator di = defines.begin(); - di != defines.end(); ++di) + // Build a list of preprocessor definitions for the target. + std::set defines; + this->AddCompileDefinitions(defines, &target, + this->ConfigurationName, l->first); + if(!defines.empty()) { cmakefileStream - << " " << this->EscapeForCMake(*di) << "\n"; + << "\n" + << "# Preprocessor definitions for this target.\n" + << "set(CMAKE_TARGET_DEFINITIONS_" << l->first << "\n"; + for(std::set::const_iterator di = defines.begin(); + di != defines.end(); ++di) + { + cmakefileStream + << " " << this->EscapeForCMake(*di) << "\n"; + } + cmakefileStream + << " )\n"; } - cmakefileStream - << " )\n"; } // Store include transform rule properties. Write the directory diff --git a/Source/cmLocalVisualStudio6Generator.cxx b/Source/cmLocalVisualStudio6Generator.cxx index 0848b03..2b999eb 100644 --- a/Source/cmLocalVisualStudio6Generator.cxx +++ b/Source/cmLocalVisualStudio6Generator.cxx @@ -1778,15 +1778,15 @@ void cmLocalVisualStudio6Generator std::set minsizeDefinesSet; std::set debugrelDefinesSet; - this->AddCompileDefinitions(definesSet, &target, ""); + this->AddCompileDefinitions(definesSet, &target, "", linkLanguage); this->AddCompileDefinitions(debugDefinesSet, &target, - "DEBUG"); + "DEBUG", linkLanguage); this->AddCompileDefinitions(releaseDefinesSet, &target, - "RELEASE"); + "RELEASE", linkLanguage); this->AddCompileDefinitions(minsizeDefinesSet, &target, - "MINSIZEREL"); + "MINSIZEREL", linkLanguage); this->AddCompileDefinitions(debugrelDefinesSet, &target, - "RELWITHDEBINFO"); + "RELWITHDEBINFO", linkLanguage); std::string defines = " "; std::string debugDefines = " "; diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx index ed560aa..f53f825 100644 --- a/Source/cmLocalVisualStudio7Generator.cxx +++ b/Source/cmLocalVisualStudio7Generator.cxx @@ -775,7 +775,7 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout, cmGeneratorTarget* gt = this->GlobalGenerator->GetGeneratorTarget(&target); std::vector targetDefines; - target.GetCompileDefinitions(targetDefines, configName); + target.GetCompileDefinitions(targetDefines, configName, "CXX"); targetOptions.AddDefines(targetDefines); targetOptions.SetVerboseMakefile( this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE")); diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 20207f5..57c49f1 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -329,7 +329,7 @@ std::string cmMakefileTargetGenerator::GetDefines(const std::string &l) // Add preprocessor definitions for this target and configuration. this->LocalGenerator->AddCompileDefinitions(defines, this->Target, - this->LocalGenerator->ConfigurationName); + this->LocalGenerator->ConfigurationName, l); std::string definesString; this->LocalGenerator->JoinDefines(defines, definesString, lang); diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index cfd8937..92fccd3 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -231,7 +231,7 @@ ComputeDefines(cmSourceFile const* source, const std::string& language) // Add preprocessor definitions for this target and configuration. this->LocalGenerator->AddCompileDefinitions(defines, this->Target, - this->GetConfigName()); + this->GetConfigName(), language); this->LocalGenerator->AppendDefines (defines, source->GetProperty("COMPILE_DEFINITIONS")); diff --git a/Source/cmQtAutoGenerators.cxx b/Source/cmQtAutoGenerators.cxx index e18e757..844d708 100644 --- a/Source/cmQtAutoGenerators.cxx +++ b/Source/cmQtAutoGenerators.cxx @@ -507,7 +507,7 @@ static void GetCompileDefinitionsAndDirectories(cmTarget const* target, incs = cmJoin(includeDirs, ";"); std::set defines; - localGen->AddCompileDefinitions(defines, target, config); + localGen->AddCompileDefinitions(defines, target, config, "CXX"); defs += cmJoin(defines, ";"); } diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index c54d694..87dcc99 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -2336,16 +2336,18 @@ static void processCompileDefinitions(cmTarget const* tgt, std::vector &options, UNORDERED_SET &uniqueOptions, cmGeneratorExpressionDAGChecker *dagChecker, - const std::string& config, bool debugOptions) + const std::string& config, bool debugOptions, + std::string const& language) { processCompileOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker, config, debugOptions, - "definitions", std::string()); + "definitions", language); } //---------------------------------------------------------------------------- void cmTarget::GetCompileDefinitions(std::vector &list, - const std::string& config) const + const std::string& config, + const std::string& language) const { UNORDERED_SET uniqueOptions; @@ -2377,7 +2379,8 @@ void cmTarget::GetCompileDefinitions(std::vector &list, uniqueOptions, &dagChecker, config, - debugDefines); + debugDefines, + language); std::vector linkInterfaceCompileDefinitionsEntries; @@ -2424,7 +2427,8 @@ void cmTarget::GetCompileDefinitions(std::vector &list, uniqueOptions, &dagChecker, config, - debugDefines); + debugDefines, + language); deleteAndClear(linkInterfaceCompileDefinitionsEntries); } diff --git a/Source/cmTarget.h b/Source/cmTarget.h index 56db22e..0356c1e 100644 --- a/Source/cmTarget.h +++ b/Source/cmTarget.h @@ -496,7 +496,8 @@ public: const char* GetExportMacro() const; void GetCompileDefinitions(std::vector &result, - const std::string& config) const; + const std::string& config, + const std::string& language) const; // Compute the set of languages compiled by the target. This is // computed every time it is called because the languages can change diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index 04d1487..19444ed 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -1876,7 +1876,8 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions( clOptions.Parse(flags.c_str()); clOptions.Parse(defineFlags.c_str()); std::vector targetDefines; - this->Target->GetCompileDefinitions(targetDefines, configName.c_str()); + this->Target->GetCompileDefinitions(targetDefines, + configName.c_str(), "CXX"); clOptions.AddDefines(targetDefines); if(this->MSTools) { diff --git a/Tests/CMakeCommands/target_compile_definitions/CMakeLists.txt b/Tests/CMakeCommands/target_compile_definitions/CMakeLists.txt index 14d40aa..f96283d 100644 --- a/Tests/CMakeCommands/target_compile_definitions/CMakeLists.txt +++ b/Tests/CMakeCommands/target_compile_definitions/CMakeLists.txt @@ -26,6 +26,21 @@ target_compile_definitions(consumer PRIVATE ) +if (CMAKE_GENERATOR MATCHES "Makefiles" OR CMAKE_GENERATOR MATCHES "Ninja") + target_sources(consumer PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/consumer.c" + ) + target_compile_definitions(consumer + PRIVATE + CONSUMER_LANG_$ + LANG_IS_CXX=$ + LANG_IS_C=$ + ) + target_compile_definitions(consumer + PRIVATE -DTEST_LANG_DEFINES + ) +endif() + add_definitions(-DSOME_DEF) add_library(imp UNKNOWN IMPORTED) get_target_property(_res imp COMPILE_DEFINITIONS) diff --git a/Tests/CMakeCommands/target_compile_definitions/consumer.c b/Tests/CMakeCommands/target_compile_definitions/consumer.c new file mode 100644 index 0000000..5796d96 --- /dev/null +++ b/Tests/CMakeCommands/target_compile_definitions/consumer.c @@ -0,0 +1,23 @@ + +#ifdef TEST_LANG_DEFINES + #ifdef CONSUMER_LANG_CXX + #error Unexpected CONSUMER_LANG_CXX + #endif + + #ifndef CONSUMER_LANG_C + #error Expected CONSUMER_LANG_C + #endif + + #if !LANG_IS_C + #error Expected LANG_IS_C + #endif + + #if LANG_IS_CXX + #error Unexpected LANG_IS_CXX + #endif +#endif + +void consumer_c() +{ + +} diff --git a/Tests/CMakeCommands/target_compile_definitions/consumer.cpp b/Tests/CMakeCommands/target_compile_definitions/consumer.cpp index a391114..778f57e 100644 --- a/Tests/CMakeCommands/target_compile_definitions/consumer.cpp +++ b/Tests/CMakeCommands/target_compile_definitions/consumer.cpp @@ -15,4 +15,22 @@ #error Expected DASH_D_DEFINE #endif +#ifdef TEST_LANG_DEFINES + #ifndef CONSUMER_LANG_CXX + #error Expected CONSUMER_LANG_CXX + #endif + + #ifdef CONSUMER_LANG_C + #error Unexpected CONSUMER_LANG_C + #endif + + #if !LANG_IS_CXX + #error Expected LANG_IS_CXX + #endif + + #if LANG_IS_C + #error Unexpected LANG_IS_C + #endif +#endif + int main() { return 0; } diff --git a/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-result.txt b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-VS.txt b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-VS.txt new file mode 100644 index 0000000..73b66ac --- /dev/null +++ b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-VS.txt @@ -0,0 +1,8 @@ +CMake Error at CompileDefinitions.cmake:5 \(target_compile_definitions\): + Error evaluating generator expression: + + \$ + + \$ may not be used with Visual Studio generators. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-Xcode.txt b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-Xcode.txt new file mode 100644 index 0000000..a1ed633 --- /dev/null +++ b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions-stderr-Xcode.txt @@ -0,0 +1,9 @@ +CMake Error at CompileDefinitions.cmake:5 \(target_compile_definitions\): + Error evaluating generator expression: + + \$ + + \$ may only be used with COMPILE_OPTIONS with the + Xcode generator. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions.cmake b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions.cmake new file mode 100644 index 0000000..7935d88 --- /dev/null +++ b/Tests/RunCMake/COMPILE_LANGUAGE-genex/CompileDefinitions.cmake @@ -0,0 +1,5 @@ + +enable_language(CXX) + +add_executable(main main.cpp) +target_compile_definitions(main PRIVATE $<$:-DANYTHING>) diff --git a/Tests/RunCMake/COMPILE_LANGUAGE-genex/RunCMakeTest.cmake b/Tests/RunCMake/COMPILE_LANGUAGE-genex/RunCMakeTest.cmake index 2c5d9ae..8a32aef 100644 --- a/Tests/RunCMake/COMPILE_LANGUAGE-genex/RunCMakeTest.cmake +++ b/Tests/RunCMake/COMPILE_LANGUAGE-genex/RunCMakeTest.cmake @@ -4,3 +4,10 @@ if (RunCMake_GENERATOR MATCHES "Visual Studio") set(RunCMake-stderr-file CompileOptions-stderr-VS.txt) run_cmake(CompileOptions) endif() +if (RunCMake_GENERATOR STREQUAL "Xcode") + set(RunCMake-stderr-file CompileDefinitions-stderr-Xcode.txt) + run_cmake(CompileDefinitions) +elseif (RunCMake_GENERATOR MATCHES "Visual Studio") + set(RunCMake-stderr-file CompileDefinitions-stderr-VS.txt) + run_cmake(CompileDefinitions) +endif() -- cgit v0.12