diff --git a/bindings/dart/cnativeapi/README.md b/bindings/dart/cnativeapi/README.md index a86341b0..ac8e3485 100644 --- a/bindings/dart/cnativeapi/README.md +++ b/bindings/dart/cnativeapi/README.md @@ -14,6 +14,19 @@ It is a plain Dart package and does not depend on Flutter. Its [build hook](http Building needs a C++17 toolchain for the target: Xcode on macOS and iOS, the Android NDK, Visual Studio on Windows, and on Linux clang or GCC plus the GTK 3, X11 and Xi development packages (found with `pkg-config`). +### Leaving the library out on some platforms + +An app that uses nativeapi on only some of its platforms can skip building and bundling the native library on the others. List them in the app's `pubspec.yaml`, or in the workspace root's if the app is in a pub workspace: + +```yaml +hooks: + user_defines: + cnativeapi: + skip_platforms: [android, ios] +``` + +The names are `android`, `ios`, `linux`, `macos` and `windows`. On a skipped platform, importing the package works, but calling into nativeapi throws an `ArgumentError`, so guard those calls with a platform check. + ## Usage ```dart diff --git a/bindings/dart/cnativeapi/hook/build.dart b/bindings/dart/cnativeapi/hook/build.dart index a6b3c801..74ca2376 100644 --- a/bindings/dart/cnativeapi/hook/build.dart +++ b/bindings/dart/cnativeapi/hook/build.dart @@ -18,6 +18,9 @@ void main(List args) async { return; } final targetOS = input.config.code.targetOS; + if (_skippedPlatforms(input).contains(targetOS.name)) { + return; + } final core = _locateCore(input.packageRoot); final src = core.resolve('src/'); @@ -181,6 +184,26 @@ const _appleLinkerStripFlags = ['-Wl,-dead_strip', '-Wl,-x']; const _linuxPackages = ['gtk+-3.0', 'x11', 'xi']; +/// The platforms the app builds without the native library, from +/// `hooks.user_defines.cnativeapi.skip_platforms` in its pubspec. +Set _skippedPlatforms(BuildInput input) { + final value = input.userDefines['skip_platforms']; + if (value == null) { + return const {}; + } + final known = [ + for (final os in [OS.android, OS.iOS, OS.linux, OS.macOS, OS.windows]) + os.name, + ]; + if (value is! List || !value.every(known.contains)) { + throw FormatException( + 'hooks.user_defines.cnativeapi.skip_platforms must be a list of ' + '${known.join(', ')}; got $value', + ); + } + return value.cast().toSet(); +} + /// The core sources: a published package carries a copy in cxx_impl/ (vendored /// by the release workflow); inside the workspace repository the package /// builds the checkout's core/ directly. diff --git a/bindings/dart/cnativeapi/test/build_hook_test.dart b/bindings/dart/cnativeapi/test/build_hook_test.dart new file mode 100644 index 00000000..9c2d6888 --- /dev/null +++ b/bindings/dart/cnativeapi/test/build_hook_test.dart @@ -0,0 +1,78 @@ +import 'package:code_assets/code_assets.dart'; +import 'package:hooks/hooks.dart'; +import 'package:test/test.dart'; + +import '../hook/build.dart' as hook; + +/// Runs the build hook for [targetOS], with [skipPlatforms] as the app's +/// `skip_platforms`, and returns the native libraries it built. +Future> build(OS targetOS, {Object? skipPlatforms}) async { + late List libraries; + await testCodeBuildHook( + mainMethod: hook.main, + targetOS: targetOS, + userDefines: skipPlatforms == null + ? null + : PackageUserDefines( + workspacePubspec: PackageUserDefinesSource( + defines: {'skip_platforms': skipPlatforms}, + basePath: Uri.file('/'), + ), + ), + check: (input, output) => libraries = output.assets.code.toList(), + ); + return libraries; +} + +void main() { + group('skip_platforms', () { + for (final os in [OS.android, OS.iOS, OS.linux, OS.macOS, OS.windows]) { + test('builds nothing for ${os.name} when it lists it', () async { + expect(await build(os, skipPlatforms: [os.name]), isEmpty); + }); + } + + test('skips each of the platforms it lists', () async { + final skip = ['android', 'ios']; + expect(await build(OS.android, skipPlatforms: skip), isEmpty); + expect(await build(OS.iOS, skipPlatforms: skip), isEmpty); + }); + + // Fuchsia has no sources, so the hook fails once it gets to building: a + // way to see that it wasn't skipped, without compiling core. + for (final skip in [ + null, + [], + ['android', 'ios'], + ]) { + test('goes on to build a platform $skip does not list', () async { + await expectLater( + build(OS.fuchsia, skipPlatforms: skip), + throwsUnsupportedError, + ); + }); + } + + for (final invalid in [ + 'android', + true, + ['beos'], + ['android', 'fuchsia'], + ['iOS'], + [1], + ]) { + test('rejects $invalid, naming the setting', () async { + await expectLater( + build(OS.android, skipPlatforms: invalid), + throwsA( + isFormatException.having( + (e) => e.message, + 'message', + contains('hooks.user_defines.cnativeapi.skip_platforms'), + ), + ), + ); + }); + } + }); +}