google / prefab

Prefab is a tool for generating build system integrations for prebuilt C/C++ libraries.
https://google.github.io/prefab/
Apache License 2.0
207 stars 33 forks source link

[BUG] prefab should set LINKER_LANGUAGE property for static libraries #161

Open madebr opened 2 years ago

madebr commented 2 years ago

Describe the bug When a static library uses a C++ STL, the linker language of a library should be set to CXX.

To Reproduce Steps to reproduce the behavior:

  1. Package a C++ static library using prefab (let's name it staticmodule::staticlib). Make sure this library uses symbols from the STL. e.g. std::string, ...
  2. Use the prefab module from step 1 in a C project:
    cmake_minimum_required(VERSION 3.0)
    project(mygame)
    find_package(staticmodule REQUIRED CONFIG)
    add_library(mygame SHARED mygame.c)
    target_link_libraries(mygame PRIVATE staticmodule::staticlib)
  3. Building the mygame library will fail because the C linker will not find the c++ STL symbols.

Expected behavior Linking just works.

Additional context Prefab should add a LINKER_LANGUAGE property: e.g.

add_library(SDL2::SDL2-static STATIC IMPORTED)
set_target_properties(SDL2::SDL2-static PROPERTIES
    IMPORTED_LOCATION "/home/maarten/.gradle/caches/transforms-3/3d84dffbc80dce10e99f69ce2cefa8de/transformed/SDL2-2.25.0/prefab/modules/SDL2-static/libs/android.arm64-v8a/libSDL2.a"
    INTERFACE_INCLUDE_DIRECTORIES "/home/maarten/.gradle/caches/transforms-3/3d84dffbc80dce10e99f69ce2cefa8de/transformed/SDL2-2.25.0/prefab/modules/SDL2-static/include"
    INTERFACE_LINK_LIBRARIES "-ldl;-lGLESv1_CM;-lGLESv2;-llog;-landroid;-lOpenSLES"
    LINKER_LANGUAGE CXX   # This line is currently missing
)
DanAlbert commented 2 years ago

I suspect this is a trivial fix since the abi.json already describes the STL requirement.

madebr commented 2 years ago

As an aside, it would also be useful to check inside the SDL2Config.cmake config file whether the CXX language is actually enabled. Because if it is not, the build process will fail at a later time. See e.g. this issue I created earlier this year.

I think the cmake config file might need something like:

include(CheckLanguage)
check_language(CXX)
if(NOT CMAKE_CXX_COMPILER)
    message(WARNING "SDL2 requires CXX, but it not enabled.")
endif()

CheckLanguage documentation

Or skip the warning and do enable_language(CXX) yourself.

madebr commented 2 years ago

I think, instead of LINKER_LANGUAGE, the imported targets should set IMPORTED_LINK_INTERFACE_LANGUAGES.