Browse Source

Add the TARGET_DEFINED generator expression

This tests whether the parameter is a usable target.
Stephen Kelly 13 years ago
parent
commit
2bee6f5ba5

+ 1 - 0
Source/cmDocumentGeneratorExpressions.h

@@ -37,6 +37,7 @@
   "target in the same buildsystem. Expands to the empty string "        \
   "target in the same buildsystem. Expands to the empty string "        \
   "otherwise.\n"                                                        \
   "otherwise.\n"                                                        \
   "  $<TARGET_FILE:tgt>        = main file (.exe, .so.1.2, .a)\n"       \
   "  $<TARGET_FILE:tgt>        = main file (.exe, .so.1.2, .a)\n"       \
+  "  $<TARGET_DEFINED:tgt>     = '1' if tgt is a target, else '0'\n"    \
   "  $<TARGET_LINKER_FILE:tgt> = file used to link (.a, .lib, .so)\n"   \
   "  $<TARGET_LINKER_FILE:tgt> = file used to link (.a, .lib, .so)\n"   \
   "  $<TARGET_SONAME_FILE:tgt> = file with soname (.so.3)\n"            \
   "  $<TARGET_SONAME_FILE:tgt> = file with soname (.so.3)\n"            \
   "where \"tgt\" is the name of a target.  "                            \
   "where \"tgt\" is the name of a target.  "                            \

+ 18 - 0
Source/cmGeneratorExpressionEvaluator.cxx

@@ -289,6 +289,22 @@ static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
 } configurationTestNode;
 } configurationTestNode;
 
 
 
 
+static const struct TargetDefinedNode : public cmGeneratorExpressionNode
+{
+  TargetDefinedNode() {}
+
+  virtual int NumExpectedParameters() const { return 1; }
+
+  std::string Evaluate(const std::vector<std::string> &parameters,
+                       cmGeneratorExpressionContext *context,
+                       const GeneratorExpressionContent *,
+                       cmGeneratorExpressionDAGChecker *) const
+  {
+    return context->Makefile->FindTargetToUse(parameters.front().c_str())
+      ? "1" : "0";
+  }
+} targetDefinedNode;
+
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 static const char* targetPropertyTransitiveWhitelist[] = {
 static const char* targetPropertyTransitiveWhitelist[] = {
     "INTERFACE_INCLUDE_DIRECTORIES"
     "INTERFACE_INCLUDE_DIRECTORIES"
@@ -702,6 +718,8 @@ cmGeneratorExpressionNode* GetNode(const std::string &identifier)
     return &buildInterfaceNode;
     return &buildInterfaceNode;
   else if (identifier == "INSTALL_INTERFACE")
   else if (identifier == "INSTALL_INTERFACE")
     return &installInterfaceNode;
     return &installInterfaceNode;
+  else if (identifier == "TARGET_DEFINED")
+    return &targetDefinedNode;
   return 0;
   return 0;
 
 
 }
 }

+ 2 - 0
Tests/CMakeCommands/target_compile_definitions/CMakeLists.txt

@@ -23,4 +23,6 @@ add_executable(consumer
 
 
 target_compile_definitions(consumer
 target_compile_definitions(consumer
   PRIVATE target_compile_definitions importedlib
   PRIVATE target_compile_definitions importedlib
+  $<$<TARGET_DEFINED:notdefined>:SHOULD_NOT_BE_DEFINED>
+  $<$<TARGET_DEFINED:importedlib>:SHOULD_BE_DEFINED>
 )
 )

+ 8 - 0
Tests/CMakeCommands/target_compile_definitions/consumer.cpp

@@ -15,4 +15,12 @@
 #error Expected MY_IMPORTEDINTERFACE_DEFINE
 #error Expected MY_IMPORTEDINTERFACE_DEFINE
 #endif
 #endif
 
 
+#ifdef SHOULD_NOT_BE_DEFINED
+#error Unexpected SHOULD_NOT_BE_DEFINED
+#endif
+
+#ifndef SHOULD_BE_DEFINED
+#error Expected SHOULD_BE_DEFINED
+#endif
+
 int main() { return 0; }
 int main() { return 0; }