linkCppImplStubs function

void linkCppImplStubs(
  1. List<ModuleInfo> moduleInfos, {
  2. String baseDir = '.',
})

Builds the #if guard condition that determines on which platforms the auto-register call should fire inside a src/HybridXxx.cpp stub. For each NativeImpl.cpp module that targets Android, Linux, iOS, or macOS, creates a starter src/Hybrid${Module}.cpp stub if one doesn't already exist.

Windows-only C++ modules get their own stub in windows/src/ (see linkWindowsCppImplStubs) and are excluded here.

The generated stub includes a per-platform #if guard on the auto-register call so it fires only on the platforms where the C++ bridge is actually used.

Implementation

void linkCppImplStubs(List<ModuleInfo> moduleInfos, {String baseDir = '.'}) {
  // Ensure src/ exists before writing stubs (createSync is idempotent).
  Directory(p.join(baseDir, 'src')).createSync(recursive: true);

  // Only create stubs for modules whose src/ file is actually compiled:
  // android/linux (isNativeCpp), iOS (iosIsCpp), or macOS (macosIsCpp).
  // Windows-only modules use windows/src/ instead.
  for (final m in moduleInfos.where(
    (m) => m.isNativeCpp || m.iosIsCpp || m.macosIsCpp,
  )) {
    final className = _toPascalCase(m.lib);
    final stubFile = File(p.join(baseDir, 'src', 'Hybrid$className.cpp'));
    if (stubFile.existsSync()) continue; // never overwrite user code
    stubFile.writeAsStringSync(
      t.cppImplStubContent(
        lib: m.lib,
        className: className,
        isNativeCpp: m.isNativeCpp,
        isAndroidCpp: m.isAndroidCpp,
        iosIsCpp: m.iosIsCpp,
        macosIsCpp: m.macosIsCpp,
      ),
    );
  }
}