From 32410140a7c592090249db772fd5f18c7808a3aa Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 16 May 2013 15:52:25 +0200 Subject: Add $ generator expression They can't be used when evaluating link libraries, but they can be used for include directories and compile definitions. Later they can be used for compile options. --- Source/cmDocumentGeneratorExpressions.h | 8 +++ Source/cmGeneratorExpressionEvaluator.cxx | 72 ++++++++++++++++++++-- Source/cmTarget.cxx | 10 ++- Source/cmTargetCompileDefinitionsCommand.h | 1 + Source/cmTargetIncludeDirectoriesCommand.h | 1 + Tests/CompileDefinitions/compiletest.c | 19 ++++++ Tests/CompileDefinitions/compiletest.cpp | 15 +++++ Tests/CompileDefinitions/compiletest_mixed_c.c | 19 ++++++ Tests/CompileDefinitions/compiletest_mixed_cxx.cpp | 19 ++++++ .../CompileDefinitions/target_prop/CMakeLists.txt | 20 ++++++ .../TargetIncludeDirectories/CMakeLists.txt | 16 +++++ .../TargetIncludeDirectories/main.c | 7 +++ .../TargetIncludeDirectories/main.cpp | 1 + 13 files changed, 200 insertions(+), 8 deletions(-) create mode 100644 Tests/CompileDefinitions/compiletest.c create mode 100644 Tests/CompileDefinitions/compiletest_mixed_c.c create mode 100644 Tests/CompileDefinitions/compiletest_mixed_cxx.cpp create mode 100644 Tests/IncludeDirectories/TargetIncludeDirectories/main.c diff --git a/Source/cmDocumentGeneratorExpressions.h b/Source/cmDocumentGeneratorExpressions.h index ac52db0..93e9cec 100644 --- a/Source/cmDocumentGeneratorExpressions.h +++ b/Source/cmDocumentGeneratorExpressions.h @@ -74,4 +74,12 @@ "the target on which the generator expression is evaluated.\n" \ "" +#define CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS \ + "Language related expressions:\n" \ + " $ = The link language of the target " \ + "being generated.\n" \ + " $ = '1' if the link language of the " \ + "target being generated matches lang, else '0'.\n" \ + "" + #endif diff --git a/Source/cmGeneratorExpressionEvaluator.cxx b/Source/cmGeneratorExpressionEvaluator.cxx index 6092aa2..7870d37 100644 --- a/Source/cmGeneratorExpressionEvaluator.cxx +++ b/Source/cmGeneratorExpressionEvaluator.cxx @@ -45,6 +45,11 @@ void reportError(cmGeneratorExpressionContext *context, //---------------------------------------------------------------------------- struct cmGeneratorExpressionNode { + enum { + DynamicParameters = 0, + OneOrMoreParameters = -1, + ZeroOrMoreParameters = -2 + }; virtual ~cmGeneratorExpressionNode() {} virtual bool GeneratesContent() const { return true; } @@ -110,8 +115,7 @@ static const struct ZeroNode installInterfaceNode; static const struct OP ## Node : public cmGeneratorExpressionNode \ { \ OP ## Node () {} \ -/* We let -1 carry the meaning 'at least one' */ \ - virtual int NumExpectedParameters() const { return -1; } \ + virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \ \ std::string Evaluate(const std::vector ¶meters, \ cmGeneratorExpressionContext *context, \ @@ -306,6 +310,60 @@ static const struct ConfigurationTestNode : public cmGeneratorExpressionNode } } configurationTestNode; +//---------------------------------------------------------------------------- +static const struct LinkLanguageNode : public cmGeneratorExpressionNode +{ + LinkLanguageNode() {} + + virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; } + + std::string Evaluate(const std::vector ¶meters, + cmGeneratorExpressionContext *context, + const GeneratorExpressionContent *content, + cmGeneratorExpressionDAGChecker *) const + { + if (parameters.size() != 0 && parameters.size() != 1) + { + reportError(context, content->GetOriginalExpression(), + "$ expression requires one or two parameters"); + return std::string(); + } + cmTarget* target = context->HeadTarget; + if (!target) + { + reportError(context, content->GetOriginalExpression(), + "$ may only be used with targets. It may not " + "be used with add_custom_command."); + } + + const char *lang = target->GetLinkerLanguage(context->Config); + if (parameters.size() == 0) + { + return lang ? lang : ""; + } + else + { + cmsys::RegularExpression langValidator; + langValidator.compile("^[A-Za-z0-9_]*$"); + if (!langValidator.find(parameters.begin()->c_str())) + { + reportError(context, content->GetOriginalExpression(), + "Expression syntax not recognized."); + return std::string(); + } + if (!lang) + { + return parameters.front().empty() ? "1" : "0"; + } + + if (strcmp(parameters.begin()->c_str(), lang) == 0) + { + return "1"; + } + return "0"; + } + } +} linkLanguageNode; static const struct JoinNode : public cmGeneratorExpressionNode { @@ -347,7 +405,7 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode TargetPropertyNode() {} // This node handles errors on parameter count itself. - virtual int NumExpectedParameters() const { return -1; } + virtual int NumExpectedParameters() const { return OneOrMoreParameters; } std::string Evaluate(const std::vector ¶meters, cmGeneratorExpressionContext *context, @@ -961,6 +1019,8 @@ cmGeneratorExpressionNode* GetNode(const std::string &identifier) return &configurationNode; else if (identifier == "CONFIG") return &configurationTestNode; + else if (identifier == "LINK_LANGUAGE") + return &linkLanguageNode; else if (identifier == "TARGET_FILE") return &targetFileNode; else if (identifier == "TARGET_LINKER_FILE") @@ -1188,7 +1248,8 @@ std::string GeneratorExpressionContent::EvaluateParameters( } } - if ((numExpected != -1 && (unsigned int)numExpected != parameters.size())) + if ((numExpected > cmGeneratorExpressionNode::DynamicParameters + && (unsigned int)numExpected != parameters.size())) { if (numExpected == 0) { @@ -1213,7 +1274,8 @@ std::string GeneratorExpressionContent::EvaluateParameters( return std::string(); } - if (numExpected == -1 && parameters.empty()) + if (numExpected == cmGeneratorExpressionNode::OneOrMoreParameters + && parameters.empty()) { reportError(context, this->GetOriginalExpression(), "$<" + identifier + "> expression requires at least one parameter."); diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index d14bfca..865a4da 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -277,6 +277,7 @@ void cmTarget::DefineProperties(cmake *cm) "Contents of COMPILE_DEFINITIONS may use \"generator expressions\" with " "the syntax \"$<...>\". " CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS + CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS CM_DOCUMENT_COMPILE_DEFINITIONS_DISCLAIMER); cm->DefineProperty @@ -605,7 +606,8 @@ void cmTarget::DefineProperties(cmake *cm) "See also the include_directories command.\n" "Contents of INCLUDE_DIRECTORIES may use \"generator expressions\" with " "the syntax \"$<...>\". " - CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS); + CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS + CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS); cm->DefineProperty ("INSTALL_NAME_DIR", cmProperty::TARGET, @@ -802,7 +804,8 @@ void cmTarget::DefineProperties(cmake *cm) "as $ to use the " "include directories specified in the interface of 'foo'." "\n" - CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS); + CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS + CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS); cm->DefineProperty ("INTERFACE_COMPILE_DEFINITIONS", cmProperty::TARGET, @@ -813,7 +816,8 @@ void cmTarget::DefineProperties(cmake *cm) "as $ to use the " "compile definitions specified in the interface of 'foo'." "\n" - CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS); + CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS + CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS); cm->DefineProperty ("LINK_INTERFACE_MULTIPLICITY", cmProperty::TARGET, diff --git a/Source/cmTargetCompileDefinitionsCommand.h b/Source/cmTargetCompileDefinitionsCommand.h index ec9b071..22d8fa8 100644 --- a/Source/cmTargetCompileDefinitionsCommand.h +++ b/Source/cmTargetCompileDefinitionsCommand.h @@ -70,6 +70,7 @@ public: "Arguments to target_compile_definitions may use \"generator " "expressions\" with the syntax \"$<...>\". " CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS + CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS ; } diff --git a/Source/cmTargetIncludeDirectoriesCommand.h b/Source/cmTargetIncludeDirectoriesCommand.h index e4bc9cf..4a1a4df 100644 --- a/Source/cmTargetIncludeDirectoriesCommand.h +++ b/Source/cmTargetIncludeDirectoriesCommand.h @@ -75,6 +75,7 @@ public: "Arguments to target_include_directories may use \"generator " "expressions\" with the syntax \"$<...>\". " CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS + CM_DOCUMENT_LANGUAGE_GENERATOR_EXPRESSIONS ; } diff --git a/Tests/CompileDefinitions/compiletest.c b/Tests/CompileDefinitions/compiletest.c new file mode 100644 index 0000000..d7883af --- /dev/null +++ b/Tests/CompileDefinitions/compiletest.c @@ -0,0 +1,19 @@ + +#ifndef LINK_C_DEFINE +#error Expected LINK_C_DEFINE +#endif +#ifndef LINK_LANGUAGE_IS_C +#error Expected LINK_LANGUAGE_IS_C +#endif + +#ifdef LINK_CXX_DEFINE +#error Unexpected LINK_CXX_DEFINE +#endif +#ifdef LINK_LANGUAGE_IS_CXX +#error Unexpected LINK_LANGUAGE_IS_CXX +#endif + +int main(void) +{ + return 0; +} diff --git a/Tests/CompileDefinitions/compiletest.cpp b/Tests/CompileDefinitions/compiletest.cpp index 7379380..7df09df 100644 --- a/Tests/CompileDefinitions/compiletest.cpp +++ b/Tests/CompileDefinitions/compiletest.cpp @@ -56,6 +56,21 @@ enum { #error Expect PREFIX_DEF2 #endif +#ifndef LINK_CXX_DEFINE +#error Expected LINK_CXX_DEFINE +#endif +#ifndef LINK_LANGUAGE_IS_CXX +#error Expected LINK_LANGUAGE_IS_CXX +#endif + +#ifdef LINK_C_DEFINE +#error Unexpected LINK_C_DEFINE +#endif +#ifdef LINK_LANGUAGE_IS_C +#error Unexpected LINK_LANGUAGE_IS_C +#endif + + // TEST_GENERATOR_EXPRESSIONS #endif diff --git a/Tests/CompileDefinitions/compiletest_mixed_c.c b/Tests/CompileDefinitions/compiletest_mixed_c.c new file mode 100644 index 0000000..698c989 --- /dev/null +++ b/Tests/CompileDefinitions/compiletest_mixed_c.c @@ -0,0 +1,19 @@ + +#ifndef LINK_CXX_DEFINE +#error Expected LINK_CXX_DEFINE +#endif +#ifndef LINK_LANGUAGE_IS_CXX +#error Expected LINK_LANGUAGE_IS_CXX +#endif + +#ifdef LINK_C_DEFINE +#error Unexpected LINK_C_DEFINE +#endif +#ifdef LINK_LANGUAGE_IS_C +#error Unexpected LINK_LANGUAGE_IS_C +#endif + +void someFunc(void) +{ + +} diff --git a/Tests/CompileDefinitions/compiletest_mixed_cxx.cpp b/Tests/CompileDefinitions/compiletest_mixed_cxx.cpp new file mode 100644 index 0000000..c686854 --- /dev/null +++ b/Tests/CompileDefinitions/compiletest_mixed_cxx.cpp @@ -0,0 +1,19 @@ + +#ifndef LINK_CXX_DEFINE +#error Expected LINK_CXX_DEFINE +#endif +#ifndef LINK_LANGUAGE_IS_CXX +#error Expected LINK_LANGUAGE_IS_CXX +#endif + +#ifdef LINK_C_DEFINE +#error Unexpected LINK_C_DEFINE +#endif +#ifdef LINK_LANGUAGE_IS_C +#error Unexpected LINK_LANGUAGE_IS_C +#endif + +int main(int argc, char **argv) +{ + return 0; +} diff --git a/Tests/CompileDefinitions/target_prop/CMakeLists.txt b/Tests/CompileDefinitions/target_prop/CMakeLists.txt index 34be917..66a3aa6 100644 --- a/Tests/CompileDefinitions/target_prop/CMakeLists.txt +++ b/Tests/CompileDefinitions/target_prop/CMakeLists.txt @@ -19,9 +19,29 @@ set_property(TARGET target_prop_executable APPEND PROPERTY COMPILE_DEFINITIONS LETTER_LIST3=\"$\" LETTER_LIST4=\"$\" LETTER_LIST5=\"$\" + "$<$:LINK_CXX_DEFINE>" + "$<$:LINK_C_DEFINE>" + "LINK_LANGUAGE_IS_$" ) set_property(TARGET target_prop_executable APPEND PROPERTY COMPILE_DEFINITIONS BUILD_IS_DEBUG=$ BUILD_IS_NOT_DEBUG=$> ) + +add_executable(target_prop_c_executable ../compiletest.c) + +set_property(TARGET target_prop_c_executable APPEND PROPERTY COMPILE_DEFINITIONS + "$<$:LINK_CXX_DEFINE>" + "$<$:LINK_C_DEFINE>" + "LINK_LANGUAGE_IS_$" + ) + +# Resulting link language will be CXX +add_executable(target_prop_mixed_executable ../compiletest_mixed_c.c ../compiletest_mixed_cxx.cpp) + +set_property(TARGET target_prop_mixed_executable APPEND PROPERTY COMPILE_DEFINITIONS + "$<$:LINK_CXX_DEFINE>" + "$<$:LINK_C_DEFINE>" + "LINK_LANGUAGE_IS_$" + ) diff --git a/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt b/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt index 60d4c6a..50f12ee 100644 --- a/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt +++ b/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt @@ -128,3 +128,19 @@ file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/prefix_foo/prefix_bar/prefix_ba file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/prefix_foo/prefix_bar/prefix_bat/prefix_foo_bar_bat.h" "// prefix_foo_bar_bat.h\n") target_include_directories(TargetIncludeDirectories PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/prefix_$") + +# Test that the language generator expressions work +set_property(TARGET TargetIncludeDirectories + APPEND PROPERTY INCLUDE_DIRECTORIES + "$<$:${CMAKE_CURRENT_BINARY_DIR}/bad>" + "$<$:${CMAKE_CURRENT_BINARY_DIR}/good>" + "$<$,CXX>:${CMAKE_CURRENT_BINARY_DIR}/othergood/>" +) + +add_executable(TargetIncludeDirectories_C main.c) +set_property(TARGET TargetIncludeDirectories_C + APPEND PROPERTY INCLUDE_DIRECTORIES + "$<$:${CMAKE_CURRENT_BINARY_DIR}/bad>" + "$<$:${CMAKE_CURRENT_BINARY_DIR}/good>" + "$<$,C>:${CMAKE_CURRENT_BINARY_DIR}/othergood/>" +) diff --git a/Tests/IncludeDirectories/TargetIncludeDirectories/main.c b/Tests/IncludeDirectories/TargetIncludeDirectories/main.c new file mode 100644 index 0000000..a597daa --- /dev/null +++ b/Tests/IncludeDirectories/TargetIncludeDirectories/main.c @@ -0,0 +1,7 @@ + +#include "common.h" + +int main(void) +{ + return 0; +} diff --git a/Tests/IncludeDirectories/TargetIncludeDirectories/main.cpp b/Tests/IncludeDirectories/TargetIncludeDirectories/main.cpp index 5bb34aa..aed0bde 100644 --- a/Tests/IncludeDirectories/TargetIncludeDirectories/main.cpp +++ b/Tests/IncludeDirectories/TargetIncludeDirectories/main.cpp @@ -11,6 +11,7 @@ #include "list.h" #include "target.h" #include "prefix_foo_bar_bat.h" +#include "common.h" int main(int, char**) { -- cgit v0.12