autoRegisterPlatformGuard function
Builds the #if guard condition for the auto-register call inside a
src/HybridXxx.cpp stub.
The guard ensures ${lib}_register_impl is only called on platforms where
the C++ bridge is actually compiled — even if the stub file is accidentally
included on a platform that uses a Swift or Kotlin bridge instead.
Returns an empty string when no guard is needed (all compiled platforms are C++).
Implementation
String autoRegisterPlatformGuard({
required bool isNativeCpp,
required bool isAndroidCpp,
required bool iosIsCpp,
required bool macosIsCpp,
}) {
// All apple + android/linux → no guard needed
if (isNativeCpp && macosIsCpp && iosIsCpp) return '';
final conditions = <String>[];
if (isNativeCpp) {
// isNativeCpp is true when Android OR Linux (or both) use C++.
// When only Linux uses C++ (isAndroidCpp is false), Android must be excluded:
// __linux__ is defined on Android NDK too, so `!defined(__APPLE__)` would
// incorrectly compile the auto-register on Android where register_impl is absent.
if (isAndroidCpp) {
conditions.add('!defined(__APPLE__)'); // android + linux + windows
} else {
conditions.add('(defined(__linux__) && !defined(__ANDROID__))'); // linux only
}
}
if (macosIsCpp && iosIsCpp) {
conditions.add('defined(__APPLE__)');
} else if (macosIsCpp) {
conditions.add('(defined(__APPLE__) && TARGET_OS_OSX)');
} else if (iosIsCpp) {
conditions.add('(defined(__APPLE__) && TARGET_OS_IOS)');
}
return conditions.join(' || ');
}