cmake: add libraries from dependencies in the cmake-file-api

pull/12019/head
kiwixz 1 year ago
parent 0bb1647fd1
commit cb263e99fa
  1. 27
      mesonbuild/cmake/fileapi.py
  2. 5
      test cases/cmake/27 cmake static lib subproject/main.cc
  3. 4
      test cases/cmake/27 cmake static lib subproject/meson.build
  4. 4
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/CMakeLists.txt
  5. 3
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/libb/CMakeLists.txt
  6. 1
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/libb/src/libb.cc
  7. 1
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/libb/src/libb.h
  8. 4
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/libc/CMakeLists.txt
  9. 5
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/libc/src/libc.cc
  10. 1
      test cases/cmake/27 cmake static lib subproject/subprojects/liba/libc/src/libc.h

@ -156,9 +156,6 @@ class CMakeFileAPI:
if i['role'] == 'flags':
link_flags += [i['fragment']]
# TODO The `dependencies` entry is new in the file API.
# maybe we can make use of that in addition to the
# implicit dependency detection
tgt_data = {
'artifacts': [Path(x.get('path', '')) for x in tgt.get('artifacts', [])],
'sourceDirectory': src_dir,
@ -172,6 +169,8 @@ class CMakeFileAPI:
'linkFlags': ' '.join(link_flags), # See previous comment block why we join the array
'type': tgt.get('type', 'EXECUTABLE'),
'fileGroups': [],
'cmakeId': tgt.get('id', ''),
'dependencies': [dep.get('id', '') for dep in tgt.get('dependencies', [])],
}
processed_src_idx = []
@ -257,6 +256,27 @@ class CMakeFileAPI:
return pro_data
def find_target(cnf_data: T.Dict[str, T.Any], id: str) -> T.Optional[T.Dict[str, T.Any]]:
for project in cnf_data['projects']:
for target in project['targets']: # type: T.Dict[str, T.Any]
if target['cmakeId'] == id:
return target
return None
def flatten_dependencies(cnf_data: T.Dict[str, T.Any]) -> None:
for project in cnf_data['projects']:
for target in project['targets']:
for dep_id in target.get('dependencies', '[]'):
dep = find_target(cnf_data, dep_id)
if dep is None:
mlog.warning('CMake: unknown target id', mlog.bold(dep_id), 'as a dependency of target', mlog.bold(target['name']))
continue
if dep['type'] == 'STATIC_LIBRARY':
if target['linkLibraries']:
target['linkLibraries'] += ' '
target['linkLibraries'] += ' '.join(str(a) for a in dep['artifacts'])
for cnf in data.get('configurations', []):
cnf_data = {
'name': cnf.get('name', ''),
@ -266,6 +286,7 @@ class CMakeFileAPI:
for pro in cnf.get('projects', []):
cnf_data['projects'] += [parse_project(pro)]
flatten_dependencies(cnf_data)
self.cmake_configurations += [CMakeConfiguration(cnf_data)]
def _parse_cmakeFiles(self, data: T.Dict[str, T.Any]) -> None:

@ -0,0 +1,5 @@
#include <libc.h>
int main() {
c();
}

@ -0,0 +1,4 @@
project('cmake-static-lib-subproject', 'cpp')
libc = import('cmake').subproject('liba').dependency('libc')
executable('main', 'main.cc', dependencies: [libc])

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.20)
project(liba CXX)
add_subdirectory(libb)
add_subdirectory(libc)

@ -0,0 +1,3 @@
project(libb CXX)
add_library(libb src/libb.cc)
target_include_directories(libb INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src)

@ -0,0 +1,4 @@
project(libc CXX)
add_library(libc src/libc.cc)
target_link_libraries(libc libb)
target_include_directories(libc INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src)
Loading…
Cancel
Save