nitroSizeControlsPreamble top-level constant

String const nitroSizeControlsPreamble

Opt-in binary-size knobs, emitted once into the generated src/CMakeLists.txt preamble. nitro_apply_size_opts(<target>) is then called on each nitro library (guarded by if(COMMAND …) so a standalone/retrofit append is a no-op). Conservative by design: dead-code strip + section GC only remove UNREFERENCED code, so every export you call survives; LTO is separate and off by default. No visibility change (that needs a full export audit).

Implementation

const String nitroSizeControlsPreamble = r'''
# ── Binary-size controls (opt-in) ────────────────────────────────────────────
# -DNITRO_SIZE_OPT=ON (implied by a MinSizeRel build) strips unreferenced code
# and garbage-collects unused sections. Exports are referenced, so nothing you
# call is removed. -DNITRO_LTO=ON adds link-time optimization (smaller output,
# slower build). Both default OFF — they are knobs, not forced behaviour.
option(NITRO_SIZE_OPT "Trim the nitro bridge: dead-code strip + section GC" OFF)
option(NITRO_LTO "Link-time optimization for the nitro bridge (smaller, slower build)" OFF)
function(nitro_apply_size_opts tgt)
  if(NITRO_SIZE_OPT OR CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
    if(MSVC)
      target_link_options(${tgt} PRIVATE /OPT:REF /OPT:ICF)
    else()
      target_compile_options(${tgt} PRIVATE -ffunction-sections -fdata-sections)
      if(APPLE)
        target_link_options(${tgt} PRIVATE "-Wl,-dead_strip")
      else()
        target_link_options(${tgt} PRIVATE "-Wl,--gc-sections")
      endif()
    endif()
  endif()
  if(NITRO_LTO AND NOT MSVC)
    target_compile_options(${tgt} PRIVATE -flto)
    target_link_options(${tgt} PRIVATE -flto)
  endif()
endfunction()
''';