Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: dependencies between CMake targets work only if the dependent target is imported #13101

Open
mmomtchev opened this issue Apr 16, 2024 · 2 comments

Comments

@mmomtchev
Copy link
Contributor

subprojects/cmMod:

cmake_minimum_required(VERSION 3.5)

project(cmMod)
set (CMAKE_CXX_STANDARD 14)

include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_library(cmModLib1 STATIC cmMod1.cpp)
add_library(cmModLib2 STATIC cmMod2.cpp)
# This is what is not working
target_link_libraries(cmModLib1 PUBLIC cmModLib2)

meson.build:

project('cmakeSubTest', ['c', 'cpp'])

cm = import('cmake')

sub_pro = cm.subproject('cmMod')
# require only the first one
sub_dep = sub_pro.dependency('cmModLib1', include_type: 'system')

exe1 = executable('main', ['main.cpp'], dependencies: [sub_dep])
test('test1', exe1)

Result:

INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/bin/ninja -v
[1/4] /usr/local/bin/g++ -Isubprojects/cmMod/libcmModLib1.a.p -Isubprojects/cmMod -I../subprojects/cmMod -Isubprojects/cmMod/__CMake_build -I../subprojects/cmMod/__CMake_build -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=gnu++14 -O0 -g -g -DcmModClass=cmModClass1 -MD -MQ subprojects/cmMod/libcmModLib1.a.p/cmMod.cpp.o -MF subprojects/cmMod/libcmModLib1.a.p/cmMod.cpp.o.d -o subprojects/cmMod/libcmModLib1.a.p/cmMod.cpp.o -c ../subprojects/cmMod/cmMod.cpp
[2/4] /usr/local/bin/g++ -Imain.p -I. -I.. -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -isystem../subprojects/cmMod -isystemsubprojects/cmMod -isystem../subprojects/cmMod/__CMake_build -isystemsubprojects/cmMod/__CMake_build -MD -MQ main.p/main.cpp.o -MF main.p/main.cpp.o.d -o main.p/main.cpp.o -c ../main.cpp
[3/4] rm -f subprojects/cmMod/libcmModLib1.a && gcc-ar csrDT subprojects/cmMod/libcmModLib1.a subprojects/cmMod/libcmModLib1.a.p/cmMod.cpp.o
[4/4] /usr/local/bin/g++  -o main main.p/main.cpp.o -Wl,--as-needed -Wl,--no-undefined subprojects/cmMod/libcmModLib1.a
FAILED: main 
/usr/local/bin/g++  -o main main.p/main.cpp.o -Wl,--as-needed -Wl,--no-undefined subprojects/cmMod/libcmModLib1.a
/usr/bin/ld: main.p/main.cpp.o: in function `main':
/home/mmom/src/meson/test cases/cmake/29 cascading deps/build/../main.cpp:14: undefined reference to `cmModClass2::cmModClass2(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: /home/mmom/src/meson/test cases/cmake/29 cascading deps/build/../main.cpp:16: undefined reference to `cmModClass2::getStr[abi:cxx11]() const'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

The problem is that when resolving the dependencies, only targets that have IMPORTED_LOCATION or IMPORTED_IMPLIB result in an actual library dependency in meson:

https://github.com/mmomtchev/meson/blob/648a3ddd65a6010dbfaf42bf9de624c2ae47bccd/mesonbuild/cmake/tracetargets.py#L118

Only imported targets have these, internal CMake targets have to be included via LINK_LIBRARIES / INTERFACE_LINK_LIBRARIES - in the code, these are used only for recursion.

@mmomtchev
Copy link
Contributor Author

Waiting on #13071 to submit a PR because both modify the same code

@mmomtchev
Copy link
Contributor Author

This is something that seems to have been implemented:

# First handle inter target dependencies

But this code is never triggered, maybe it used to work at some point, then ti was broken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant