Skip to content

Commit e6332b8

Browse files
amysparknirbheek
authored andcommitted
cmake: Fix target_link_libraries against project targets
These were supported for shared libraries, but for static libraries the link_with property was never populated with `LINK_LIBRARIES` or `INTERFACE_LINK_LIBRARIES`. Fixes #13101
1 parent 98b1980 commit e6332b8

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

mesonbuild/cmake/interpreter.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def __init__(self, target: CMakeTarget, env: 'Environment', for_machine: Machine
223223
self.install = target.install
224224
self.install_dir: T.Optional[Path] = None
225225
self.link_libraries = target.link_libraries
226+
self.link_targets: T.List[str] = []
226227
self.link_flags = target.link_flags + target.link_lang_flags
227228
self.public_link_flags: T.List[str] = []
228229
self.depends_raw: T.List[str] = []
@@ -363,6 +364,8 @@ def postprocess(self, output_target_map: OutputTargetMap, root_src_dir: Path, su
363364
self.public_link_flags += rtgt.public_link_flags
364365
self.public_compile_opts += rtgt.public_compile_opts
365366
self.link_libraries += rtgt.libraries
367+
self.depends_raw += rtgt.target_dependencies
368+
self.link_targets += rtgt.target_dependencies
366369

367370
elif self.type.upper() not in ['EXECUTABLE', 'OBJECT_LIBRARY']:
368371
mlog.warning('CMake: Target', mlog.bold(self.cmake_name), 'not found in CMake trace. This can lead to build errors')
@@ -957,17 +960,27 @@ def analyse(self) -> None:
957960
object_libs += [tgt]
958961
self.languages += [x for x in tgt.languages if x not in self.languages]
959962

960-
# Second pass: Detect object library dependencies
963+
# Second pass: Populate link_with project internal targets
964+
for tgt in self.targets:
965+
for i in tgt.link_targets:
966+
# Handle target-based link libraries
967+
link_with = self.output_target_map.target(i)
968+
if not link_with or isinstance(link_with, ConverterCustomTarget):
969+
# Generated file etc.
970+
continue
971+
tgt.link_with.append(link_with)
972+
973+
# Third pass: Detect object library dependencies
961974
for tgt in self.targets:
962975
tgt.process_object_libs(object_libs, self._object_lib_workaround)
963976

964-
# Third pass: Reassign dependencies to avoid some loops
977+
# Fourth pass: Reassign dependencies to avoid some loops
965978
for tgt in self.targets:
966979
tgt.process_inter_target_dependencies()
967980
for ctgt in self.custom_targets:
968981
ctgt.process_inter_target_dependencies()
969982

970-
# Fourth pass: Remove rassigned dependencies
983+
# Fifth pass: Remove reassigned dependencies
971984
for tgt in self.targets:
972985
tgt.cleanup_dependencies()
973986

mesonbuild/cmake/tracetargets.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self) -> None:
4545
self.public_link_flags: T.List[str] = []
4646
self.public_compile_opts: T.List[str] = []
4747
self.libraries: T.List[str] = []
48+
self.target_dependencies: T.List[str] = []
4849

4950
def resolve_cmake_trace_targets(target_name: str,
5051
trace: 'CMakeTraceParser',
@@ -144,9 +145,13 @@ def resolve_cmake_trace_targets(target_name: str,
144145
targets += [x for x in tgt.properties['IMPORTED_LOCATION'] if x]
145146

146147
if 'LINK_LIBRARIES' in tgt.properties:
147-
targets += [x for x in tgt.properties['LINK_LIBRARIES'] if x]
148+
link_libraries = [x for x in tgt.properties['LINK_LIBRARIES'] if x]
149+
targets += link_libraries
150+
res.target_dependencies += link_libraries
148151
if 'INTERFACE_LINK_LIBRARIES' in tgt.properties:
149-
targets += [x for x in tgt.properties['INTERFACE_LINK_LIBRARIES'] if x]
152+
link_libraries = [x for x in tgt.properties['INTERFACE_LINK_LIBRARIES'] if x]
153+
targets += link_libraries
154+
res.target_dependencies += link_libraries
150155

151156
if f'IMPORTED_LINK_DEPENDENT_LIBRARIES_{cfg}' in tgt.properties:
152157
targets += [x for x in tgt.properties[f'IMPORTED_LINK_DEPENDENT_LIBRARIES_{cfg}'] if x]

0 commit comments

Comments
 (0)