فهرست منبع

Allow INTERFACE_SOURCES to specify $<TARGET_OBJECTS> (#14970)

Fix cmTarget::GetSourceFiles to set EvaluateForBuildsystem on the
$<TARGET_PROPERTY:...,INTERFACE_SOURCES> generator expression so that
the $<TARGET_OBJECTS> generator expression is allowed within an
INTERFACE_SOURCES value.

Extend the InterfaceLibrary test to cover this case.  Extend the
RunCMake.TargetObjects test to cover failure of $<TARGET_OBJECTS>
when used through $<TARGET_PROPERTY:...,INTERFACE_SOURCES> in a
non-buildsystem context.
Brad King 11 سال پیش
والد
کامیت
affe9d56a2

+ 6 - 0
Help/manual/cmake-buildsystem.7.rst

@@ -107,6 +107,12 @@ they may not be installed, exported, or used in the right hand side of
 :command:`target_link_libraries`.  They also may not be used as the ``TARGET``
 in a use of the :command:`add_custom_command(TARGET)` command signature.
 
+Although object libraries may not be named directly in calls to
+the :command:`target_link_libraries` command, they can be "linked"
+indirectly by using an :ref:`Interface Library <Interface Libraries>`
+whose :prop_tgt:`INTERFACE_SOURCES` target property is set to name
+``$<TARGET_OBJECTS:objlib>``.
+
 Build Specification and Usage Requirements
 ==========================================
 

+ 1 - 0
Source/cmGeneratorExpressionEvaluator.cxx

@@ -1210,6 +1210,7 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
         {
         cmGeneratorExpression ge(&context->Backtrace);
         cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
+        cge->SetEvaluateForBuildsystem(context->EvaluateForBuildsystem);
         std::string result = cge->Evaluate(context->Makefile,
                             context->Config,
                             context->Quiet,

+ 1 - 0
Source/cmTarget.cxx

@@ -6375,6 +6375,7 @@ void cmTargetInternals::AddInterfaceEntries(
           "$<TARGET_PROPERTY:" + *it + "," + prop + ">";
         cmGeneratorExpression ge(&it->Backtrace);
         cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
+        cge->SetEvaluateForBuildsystem(true);
         entries.push_back(
           new cmTargetInternals::TargetPropertyEntry(cge, *it));
         }

+ 5 - 1
Tests/InterfaceLibrary/CMakeLists.txt

@@ -18,8 +18,12 @@ set_property(TARGET imp::iface APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SOMEPRO
 set_property(TARGET imp::iface PROPERTY INTERFACE_SOMEPROP OFF)
 set_property(TARGET imp::iface PROPERTY INTERFACE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/does_not_exist.cpp)
 
+add_library(objlib OBJECT obj.cpp)
+add_library(iface_objlib INTERFACE)
+target_sources(iface_objlib INTERFACE $<TARGET_OBJECTS:objlib>)
+
 add_executable(InterfaceLibrary definetestexe.cpp)
-target_link_libraries(InterfaceLibrary iface_nodepends headeriface subiface)
+target_link_libraries(InterfaceLibrary iface_nodepends headeriface subiface iface_objlib)
 
 add_subdirectory(libsdir)
 

+ 2 - 1
Tests/InterfaceLibrary/definetestexe.cpp

@@ -15,9 +15,10 @@
 #error Expected IFACE_HEADER_BUILDDIR
 #endif
 
+extern int obj();
 extern int sub();
 
 int main(int,char**)
 {
-  return sub();
+  return obj() + sub();
 }

+ 1 - 0
Tests/InterfaceLibrary/obj.cpp

@@ -0,0 +1 @@
+int obj() { return 0; }

+ 12 - 2
Tests/RunCMake/TargetObjects/BadContext-stderr.txt

@@ -1,4 +1,4 @@
-CMake Error at BadContext.cmake:2 \(file\):
+(CMake Error at BadContext.cmake:4 \(file\):
   Error evaluating generator expression:
 
     \$<TARGET_OBJECTS:NoTarget>
@@ -7,7 +7,17 @@ CMake Error at BadContext.cmake:2 \(file\):
   for consumption by CMake.  It is not suitable for writing out elsewhere.
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)
-+
+*)+
+(CMake Error at BadContext.cmake:5 \(file\):
+  Error evaluating generator expression:
+
+    \$<TARGET_OBJECTS:NoTarget>
+
+  The evaluation of the TARGET_OBJECTS generator expression is only suitable
+  for consumption by CMake.  It is not suitable for writing out elsewhere.
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)
+*)+
 CMake Error:
   Error evaluating generator expression:
 

+ 3 - 0
Tests/RunCMake/TargetObjects/BadContext.cmake

@@ -1,4 +1,7 @@
+add_library(iface INTERFACE)
+target_sources(iface INTERFACE $<TARGET_OBJECTS:NoTarget>)
 
 file(GENERATE OUTPUT test_output CONTENT $<TARGET_OBJECTS:NoTarget>)
+file(GENERATE OUTPUT test_output2 CONTENT $<TARGET_PROPERTY:iface,INTERFACE_SOURCES>)
 
 install(FILES $<TARGET_OBJECTS:NoTarget> DESTINATION objects)