Skip to content

Commit

Permalink
Merge topic 'restore-link-interface-objlib-with-unity' into release-3.28
Browse files Browse the repository at this point in the history
5b8e9e0 Restore support for TARGET_OBJECTS in link interfaces with unity builds
1313c78 Tests: Update RunCMake.TargetObjects cmake_minimum_required version

Acked-by: Kitware Robot <[email protected]>
Merge-request: !9279
  • Loading branch information
bradking committed Feb 22, 2024
2 parents 1371a28 + 5b8e9e0 commit e98cbbf
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Source/cmGeneratorTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,10 @@ class cmGeneratorTarget
*/
void ClearSourcesCache();

// Do not use. This is only for a specific call site with a FIXME comment.
/**
* Clears cached evaluations of INTERFACE_LINK_LIBRARIES.
* They will be recomputed on demand.
*/
void ClearLinkInterfaceCache();

void AddSource(const std::string& src, bool before = false);
Expand Down
4 changes: 4 additions & 0 deletions Source/cmGlobalGenerator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1897,9 +1897,13 @@ bool cmGlobalGenerator::AddAutomaticSources()
// Clear the source list and classification cache (KindedSources) of all
// targets so that it will be recomputed correctly by the generators later
// now that the above transformations are done for all targets.
// Also clear the link interface cache to support $<TARGET_OBJECTS:objlib>
// in INTERFACE_LINK_LIBRARIES because the list of object files may have
// been changed by conversion to a unity build or addition of a PCH source.
for (const auto& lg : this->LocalGenerators) {
for (const auto& gt : lg->GetGeneratorTargets()) {
gt->ClearSourcesCache();
gt->ClearLinkInterfaceCache();
}
}
return true;
Expand Down
5 changes: 0 additions & 5 deletions Source/cmLocalGenerator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2831,15 +2831,10 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
cm::nullopt, true);
} else if (reuseTarget->GetType() ==
cmStateEnums::OBJECT_LIBRARY) {
// FIXME: This can propagate more than one level, unlike
// the rest of the object files in an object library.
// Find another way to do this.
target->Target->AppendProperty(
"INTERFACE_LINK_LIBRARIES",
cmStrCat("$<$<CONFIG:", config,
">:$<LINK_ONLY:", pchSourceObj, ">>"));
// We updated the link interface, so ensure it is recomputed.
target->ClearLinkInterfaceCache();
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion Tests/RunCMake/TargetObjects/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 3.3)
cmake_minimum_required(VERSION 3.5)
project(${RunCMake_TEST} LANGUAGES NONE)
include(${RunCMake_TEST}.cmake)
8 changes: 8 additions & 0 deletions Tests/RunCMake/TargetObjects/RunCMakeTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ run_cmake(NotObjlibTarget)
if(RunCMake_GENERATOR STREQUAL "Xcode" AND "$ENV{CMAKE_OSX_ARCHITECTURES}" MATCHES "[;$]")
run_cmake(XcodeVariableNoGenexExpansion)
endif()

function(run_cmake_and_build case)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${case}-build)
run_cmake(${case})
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(${case}-build ${CMAKE_COMMAND} --build .)
endfunction()
run_cmake_and_build(Unity)
13 changes: 13 additions & 0 deletions Tests/RunCMake/TargetObjects/Unity.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enable_language(C)

# Test transforming the set of object files provided by an object library.
set(CMAKE_UNITY_BUILD 1)

add_library(UnityObj1 OBJECT UnityObj1.c)
add_library(UnityObj2 OBJECT UnityObj2.c)

add_library(UnityObj2Iface INTERFACE)
target_link_libraries(UnityObj2Iface INTERFACE $<TARGET_OBJECTS:UnityObj2>)

add_executable(UnityMain UnityMain.c)
target_link_libraries(UnityMain PRIVATE UnityObj1 UnityObj2Iface)
7 changes: 7 additions & 0 deletions Tests/RunCMake/TargetObjects/UnityMain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern int UnityObj1(void);
extern int UnityObj2(void);

int main(void)
{
return UnityObj1() + UnityObj2();
}
4 changes: 4 additions & 0 deletions Tests/RunCMake/TargetObjects/UnityObj1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int UnityObj1(void)
{
return 0;
}
4 changes: 4 additions & 0 deletions Tests/RunCMake/TargetObjects/UnityObj2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int UnityObj2(void)
{
return 0;
}

0 comments on commit e98cbbf

Please sign in to comment.