Преглед на файлове

objlib: Allow `OBJECT` libraries to link to other libraries.

The proper way to use libraries is now through `target_link_libraries`
for things such as usage requirements, compile definitions, include
directories, etc. To facilitate this, allow `OBJECT` libraries to "link"
to other libraries.

Co-Author: Ben Boeckel <[email protected]>
Issue: #14778
Deniz Bahadir преди 8 години
родител
ревизия
51249e69ea

+ 0 - 10
Source/cmTargetLinkLibrariesCommand.cxx

@@ -94,16 +94,6 @@ bool cmTargetLinkLibrariesCommand::InitialPass(
     return true;
     return true;
   }
   }
 
 
-  // OBJECT libraries are not allowed on the LHS of the command.
-  if (this->Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
-    std::ostringstream e;
-    e << "Object library target \"" << args[0] << "\" "
-      << "may not link to anything.";
-    this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
-    cmSystemTools::SetFatalErrorOccured();
-    return true;
-  }
-
   // Having a UTILITY library on the LHS is a bug.
   // Having a UTILITY library on the LHS is a bug.
   if (this->Target->GetType() == cmStateEnums::UTILITY) {
   if (this->Target->GetType() == cmStateEnums::UTILITY) {
     std::ostringstream e;
     std::ostringstream e;

+ 1 - 1
Tests/RunCMake/ObjectLibrary/CMakeLists.txt

@@ -1,3 +1,3 @@
-cmake_minimum_required(VERSION 2.8.4)
+cmake_minimum_required(VERSION 3.10)
 project(${RunCMake_TEST} C)
 project(${RunCMake_TEST} C)
 include(${RunCMake_TEST}.cmake)
 include(${RunCMake_TEST}.cmake)

+ 0 - 1
Tests/RunCMake/ObjectLibrary/LinkObjLHS-result.txt

@@ -1 +0,0 @@
-1

+ 0 - 4
Tests/RunCMake/ObjectLibrary/LinkObjLHS-stderr.txt

@@ -1,4 +0,0 @@
-CMake Error at LinkObjLHS.cmake:2 \(target_link_libraries\):
-  Object library target "AnObjLib" may not link to anything.
-Call Stack \(most recent call first\):
-  CMakeLists.txt:3 \(include\)

+ 0 - 2
Tests/RunCMake/ObjectLibrary/LinkObjLHS.cmake

@@ -1,2 +0,0 @@
-add_library(AnObjLib OBJECT a.c)
-target_link_libraries(AnObjLib OtherLib)

+ 7 - 0
Tests/RunCMake/ObjectLibrary/LinkObjLHSShared.cmake

@@ -0,0 +1,7 @@
+project(LinkObjLHSShared C)
+
+add_library(OtherLib SHARED a.c)
+target_compile_definitions(OtherLib INTERFACE REQUIRED)
+
+add_library(AnObjLib OBJECT requires.c)
+target_link_libraries(AnObjLib OtherLib)

+ 7 - 0
Tests/RunCMake/ObjectLibrary/LinkObjLHSStatic.cmake

@@ -0,0 +1,7 @@
+project(LinkObjLHSStatic C)
+
+add_library(OtherLib STATIC a.c)
+target_compile_definitions(OtherLib INTERFACE REQUIRED)
+
+add_library(AnObjLib OBJECT requires.c)
+target_link_libraries(AnObjLib OtherLib)

+ 14 - 1
Tests/RunCMake/ObjectLibrary/RunCMakeTest.cmake

@@ -13,7 +13,20 @@ else()
   run_cmake(Install)
   run_cmake(Install)
 endif()
 endif()
 run_cmake(Export)
 run_cmake(Export)
-run_cmake(LinkObjLHS)
+
+function (run_object_lib_build name)
+  # Use a single build tree for a few tests without cleaning.
+  set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${name}-build)
+  set(RunCMake_TEST_NO_CLEAN 1)
+  file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
+  file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
+  run_cmake(${name})
+  run_cmake_command(${name}-build ${CMAKE_COMMAND} --build .)
+endfunction ()
+
+run_object_lib_build(LinkObjLHSShared)
+run_object_lib_build(LinkObjLHSStatic)
+
 run_cmake(LinkObjRHS1)
 run_cmake(LinkObjRHS1)
 run_cmake(LinkObjRHS2)
 run_cmake(LinkObjRHS2)
 run_cmake(MissingSource)
 run_cmake(MissingSource)

+ 8 - 0
Tests/RunCMake/ObjectLibrary/requires.c

@@ -0,0 +1,8 @@
+#ifdef REQUIRED
+int required()
+{
+  return 0;
+}
+#else
+#error "REQUIRED not defined"
+#endif

+ 2 - 3
Tests/RunCMake/add_library/OBJECTwithNoSourcesButLinkObjects-stderr.txt

@@ -1,5 +1,4 @@
-^CMake Error at OBJECTwithNoSourcesButLinkObjects.cmake:[0-9]+ \(target_link_libraries\):
-  Object library target \"TestObjectLibWithoutSources\" may not link to
-  anything.
+^CMake Error at OBJECTwithNoSourcesButLinkObjects.cmake:[0-9]+ \(add_library\):
+  No SOURCES given to target: TestObjectLibWithoutSources
 Call Stack \(most recent call first\):
 Call Stack \(most recent call first\):
   CMakeLists.txt:[0-9]+ \(include\)$
   CMakeLists.txt:[0-9]+ \(include\)$