mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.41k stars 1.55k forks source link

CMake target dependency should not carry private link options #12707

Open J-Vernay opened 6 months ago

J-Vernay commented 6 months ago

Describe the bug When importing a CMake target as a dependency, private link options are passed to the executable linking with this dependency.

To Reproduce Extract bug.zip on a Windows machine. mytest is a CMake project generating a shared library compiled with /NODEFAULTLIB . Do meson setup build.

Notice that in the generated file build\subprojects\mytest\meson.build, the dependency is declared as:

mytest_dep = declare_dependency(
  link_args : ['/NODEFAULTLIB'],
  link_with : mytest,
  compile_args : [],
  include_directories : mytest_inc
)

(whereas in CMakeLists.txt, there was target_link_options(mytest PRIVATE /NODEFAULTLIB))

When doing meson compile -C build -v, main executable is linked with /NODEFAULTLIB .

[5/5] "link"  /MACHINE:x64 /OUT:main.exe main.exe.p/main.c.obj "/release" "/nologo" "/DEBUG" "/PDB:main.pdb" "subprojects\mytest\mytest.lib" "/NODEFAULTLIB" "/SUBSYSTEM:CONSOLE" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib"
FAILED: main.exe main.pdb
"link"  /MACHINE:x64 /OUT:main.exe main.exe.p/main.c.obj "/release" "/nologo" "/DEBUG" "/PDB:main.pdb" "subprojects\mytest\mytest.lib" "/NODEFAULTLIB" "/SUBSYSTEM:CONSOLE" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib"
main.c.obj : error LNK2019: symbole externe non résolu __imp___acrt_iob_func référencé dans la fonction printf
main.c.obj : error LNK2019: symbole externe non résolu __imp___stdio_common_vfprintf référencé dans la fonction _vfprintf_l
LINK : error LNK2001: symbole externe non résolu mainCRTStartup
main.exe : fatal error LNK1120: 3 externes non résolus
ninja: build stopped: subcommand failed.

Expected behavior main.exe should not link with /NODEFAULTLIB .

system parameters

Note: this bug occurs with SDL3, where SDL3-shared is linked with /NODEFAULTLIB on Windows.

LDAP commented 1 month ago

The same happens for private include directories.

This can be reproduced with SDL 2.30.3 where "-I..\subprojects\sdl2\src\video\khronos" is in the final compiler flags but the only public include directories are:

target_include_directories(SDL2-static PUBLIC
      "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include>"
      "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include/SDL2>"
      "$<BUILD_INTERFACE:${SDL2_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>/SDL2>"
      "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
      "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>"
  )

(the SDL2 wrap for that version has the same issue)