nitroLibcxxCmakeModule top-level constant

String const nitroLibcxxCmakeModule

src/native/nitro_libcxx.cmake, rewritten by every nitrogen link.

Decides at configure time, because the answer changes when the plugin's author adds a library: two C++ libraries that link each other share one libc++ through its exported symbols, and hiding those symbols made an exception thrown in one uncatchable by type in the other (measured on an emulator: catch (const std::exception&) missed, std::terminate followed).

Implementation

const String nitroLibcxxCmakeModule = r'''# GENERATED BY nitrogen — `nitrogen link` rewrites this file; do not edit.
#
# Android links libc++ statically into every shared library and by default
# re-exports all of its symbols, so the linker cannot drop the unused ones.
# Hiding them is only safe for a library that shares its libc++ with no other
# library. When one C++ library links against another, it borrows the other's
# exported libc++; hiding that gives each its own copy, and an exception thrown
# in one is then no longer caught by type in the other.
#
# So libc++ is hidden only when no target in the project links a shared library
# other than the NDK's own. Anything this file cannot classify (an imported
# target, a generator expression, a raw flag) leaves the whole project as it
# was; for a library you know stands alone, add the option yourself:
#   target_link_options(<lib> PRIVATE "-Wl,--exclude-libs,libc++_static.a:libc++abi.a")
# This file cannot see other projects: if one links against a library built
# here, comment out the include in src/CMakeLists.txt.
if(NOT ANDROID OR CMAKE_VERSION VERSION_LESS 3.19)
  return()
endif()

function(_nitro_targets_in dir out)
  get_property(_targets DIRECTORY "${dir}" PROPERTY BUILDSYSTEM_TARGETS)
  get_property(_subdirs DIRECTORY "${dir}" PROPERTY SUBDIRECTORIES)
  foreach(_subdir IN LISTS _subdirs)
    _nitro_targets_in("${_subdir}" _more)
    list(APPEND _targets ${_more})
  endforeach()
  set(${out} "${_targets}" PARENT_SCOPE)
endfunction()

function(_nitro_hide_static_libcxx)
  set(_ndk android log m dl c z atomic EGL GLESv1_CM GLESv2 GLESv3 vulkan
      jnigraphics OpenSLES OpenMAXAL mediandk camera2ndk aaudio amidi
      nativewindow binder_ndk neuralnetworks sync icu nativehelper)
  _nitro_targets_in("${CMAKE_SOURCE_DIR}" _targets)
  set(_shared "")
  foreach(_target IN LISTS _targets)
    get_target_property(_type ${_target} TYPE)
    if(_type MATCHES "^(SHARED|MODULE)_LIBRARY$")
      list(APPEND _shared ${_target})
    endif()
    foreach(_property LINK_LIBRARIES INTERFACE_LINK_LIBRARIES)
      get_target_property(_libs ${_target} ${_property})
      if(NOT _libs)
        continue()
      endif()
      foreach(_lib IN LISTS _libs)
        string(REGEX REPLACE "^\\$<LINK_ONLY:(.*)>$" "\\1" _lib "${_lib}")
        # Targets first: a project library is never an NDK one, whatever its
        # name. A static, object or interface library built here ends up inside
        # the library that links it; its own links are checked by the outer loop.
        if(TARGET "${_lib}")
          get_target_property(_lib_type ${_lib} TYPE)
          if(_lib IN_LIST _targets AND _lib_type MATCHES "^(STATIC|OBJECT|INTERFACE)_LIBRARY$")
            continue()
          endif()
          return()
        endif()
        # NDK system libraries, and prebuilt static archives given by path.
        if(_lib IN_LIST _ndk OR _lib MATCHES "\\.a$")
          continue()
        endif()
        return()
      endforeach()
    endforeach()
  endforeach()
  foreach(_target IN LISTS _shared)
    set_property(TARGET ${_target} APPEND PROPERTY LINK_OPTIONS "-Wl,--exclude-libs,libc++_static.a:libc++abi.a")
  endforeach()
endfunction()

cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}" CALL _nitro_hide_static_libcxx)
''';