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 <ben.boeckel@kitware.com>
Issue: #14778
This commit is contained in:
Deniz Bahadir
2017-11-23 12:59:20 +01:00
committed by Brad King
parent e22c45d4c9
commit 51249e69ea
10 changed files with 39 additions and 22 deletions

View File

@@ -94,16 +94,6 @@ bool cmTargetLinkLibrariesCommand::InitialPass(
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.
if (this->Target->GetType() == cmStateEnums::UTILITY) {
std::ostringstream e;

View File

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

View File

@@ -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\)

View File

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

View File

@@ -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)

View File

@@ -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)

View File

@@ -13,7 +13,20 @@ else()
run_cmake(Install)
endif()
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(LinkObjRHS2)
run_cmake(MissingSource)

View File

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

View File

@@ -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\):
CMakeLists.txt:[0-9]+ \(include\)$