mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-05 13:20:47 -06:00
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:
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
1
|
||||
@@ -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\)
|
||||
@@ -1,2 +0,0 @@
|
||||
add_library(AnObjLib OBJECT a.c)
|
||||
target_link_libraries(AnObjLib OtherLib)
|
||||
7
Tests/RunCMake/ObjectLibrary/LinkObjLHSShared.cmake
Normal file
7
Tests/RunCMake/ObjectLibrary/LinkObjLHSShared.cmake
Normal 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)
|
||||
7
Tests/RunCMake/ObjectLibrary/LinkObjLHSStatic.cmake
Normal file
7
Tests/RunCMake/ObjectLibrary/LinkObjLHSStatic.cmake
Normal 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)
|
||||
@@ -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)
|
||||
|
||||
8
Tests/RunCMake/ObjectLibrary/requires.c
Normal file
8
Tests/RunCMake/ObjectLibrary/requires.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifdef REQUIRED
|
||||
int required()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
#error "REQUIRED not defined"
|
||||
#endif
|
||||
@@ -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\)$
|
||||
|
||||
Reference in New Issue
Block a user