Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bindings/dart/cnativeapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions bindings/dart/cnativeapi/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ void main(List<String> 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/');

Expand Down Expand Up @@ -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<String> _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<String>().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.
Expand Down
78 changes: 78 additions & 0 deletions bindings/dart/cnativeapi/test/build_hook_test.dart
Original file line number Diff line number Diff line change
@@ -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<List<CodeAsset>> build(OS targetOS, {Object? skipPlatforms}) async {
late List<CodeAsset> 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 <Object?>[
null,
<String>[],
['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 <Object>[
'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'),
),
),
);
});
}
});
}
Loading