appleLinkAnchorsTu function

String appleLinkAnchorsTu({
  1. required String pluginName,
  2. required List<String> initSymbols,
  3. required List<String> implAnchors,
})

The anchors translation unit: an Objective-C class whose method references every bridge's <lib>_init_dart_api_dl (initSymbols) and every impl anchor (implAnchors).

CocoaPods adds -ObjC to the app's link whenever a pod is static, and -ObjC loads every archive member that defines an Objective-C class. So this file is always loaded, and its references load the rest. It adds no linker flag and reaches no target that does not already link the pod.

Implementation

String appleLinkAnchorsTu({
  required String pluginName,
  required List<String> initSymbols,
  required List<String> implAnchors,
}) {
  final cls = 'NitroLinkAnchors_${_cIdent(pluginName)}';
  final b = StringBuffer()
    ..writeln('// Generated by nitrogen link — do not edit.')
    ..writeln('//')
    ..writeln('// Release/archive link anchors for $pluginName.')
    ..writeln('//')
    ..writeln('// When CocoaPods builds this pod as a static library, the app\'s link loads')
    ..writeln('// only the archive members something references. Nothing references the')
    ..writeln('// nitro bridges (looked up with dlsym) or the C++ implementations')
    ..writeln('// (registered from a static constructor), so a release/archive build drops')
    ..writeln('// them: the app works in debug and fails at launch from an archive.')
    ..writeln('//')
    ..writeln('// CocoaPods passes -ObjC whenever a pod is static, and -ObjC loads every')
    ..writeln('// archive member that defines an Objective-C class. This file defines one,')
    ..writeln('// so it is always loaded; the references below then load the bridges and')
    ..writeln('// implementations.')
    ..writeln('#import <Foundation/Foundation.h>')
    ..writeln('#include <stdint.h>')
    ..writeln()
    ..writeln('extern "C" {');
  for (final s in initSymbols) {
    b.writeln('intptr_t $s(void* data);');
  }
  for (final s in implAnchors) {
    b.writeln('void $s(void);');
  }
  b
    ..writeln('}')
    ..writeln()
    ..writeln('@interface $cls : NSObject')
    ..writeln('@end')
    ..writeln()
    ..writeln('@implementation $cls')
    ..writeln('+ (const void* const*)anchors {')
    ..writeln('  static const void* const kAnchors[] = {');
  for (final s in [...initSymbols, ...implAnchors]) {
    b.writeln('    (const void*)&$s,');
  }
  b
    ..writeln('  };')
    ..writeln('  return kAnchors;')
    ..writeln('}')
    ..writeln('@end');
  return b.toString();
}