Skip to content

Commit 7bca356

Browse files
authored
[native_assets_builder] Fix old build hooks (#1148)
1 parent 9d02079 commit 7bca356

File tree

18 files changed

+204
-6
lines changed

18 files changed

+204
-6
lines changed

.github/workflows/native.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ jobs:
6868
- run: dart pub get -C test_data/native_add/
6969
if: ${{ matrix.package == 'native_assets_builder' }}
7070

71+
- run: dart pub get -C test_data/native_add_v1_0_0/
72+
if: ${{ matrix.package == 'native_assets_builder' }}
73+
7174
- run: dart pub get -C test_data/native_add_add_source/
7275
if: ${{ matrix.package == 'native_assets_builder' }}
7376

pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,12 @@ ${result.stdout}
410410
}
411411

412412
try {
413-
final buildOutput =
414-
HookOutputImpl.readFromFile(file: config.outputFile) ??
415-
HookOutputImpl();
413+
final buildOutput = HookOutputImpl.readFromFile(
414+
file: config.outputFile) ??
415+
(config.outputFileV1_1_0 == null
416+
? null
417+
: HookOutputImpl.readFromFile(file: config.outputFileV1_1_0!)) ??
418+
HookOutputImpl();
416419
//As a link.dart can pipe through assets from other packages.
417420
if (hook == Hook.build) {
418421
success &= validateAssetsPackage(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
7+
import '../helpers.dart';
8+
import 'helpers.dart';
9+
10+
const Timeout longTimeout = Timeout(Duration(minutes: 5));
11+
12+
void main() async {
13+
test('native_add build', timeout: longTimeout, () async {
14+
await inTempDir((tempUri) async {
15+
await copyTestProjects(targetUri: tempUri);
16+
final packageUri = tempUri.resolve('native_add_v1_0_0/');
17+
18+
// First, run `pub get`, we need pub to resolve our dependencies.
19+
await runPubGet(
20+
workingDirectory: packageUri,
21+
logger: logger,
22+
);
23+
24+
{
25+
final result = await build(
26+
packageUri,
27+
logger,
28+
dartExecutable,
29+
);
30+
expect(result.assets.length, 1);
31+
}
32+
});
33+
});
34+
}

pkgs/native_assets_builder/test_data/manifest.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
- cyclic_package_2/pubspec.yaml
77
- dart_app/bin/dart_app.dart
88
- dart_app/pubspec.yaml
9+
- native_add_v1_0_0/build.dart
10+
- native_add_v1_0_0/ffigen.yaml
11+
- native_add_v1_0_0/lib/native_add.dart
12+
- native_add_v1_0_0/lib/src/native_add_bindings_generated.dart
13+
- native_add_v1_0_0/lib/src/native_add.dart
14+
- native_add_v1_0_0/pubspec.yaml
15+
- native_add_v1_0_0/src/native_add.c
16+
- native_add_v1_0_0/src/native_add.h
917
- native_add/ffigen.yaml
1018
- native_add/hook/build.dart
1119
- native_add/lib/native_add.dart
@@ -76,4 +84,4 @@
7684
- add_asset_link/lib/src/add_asset_link_bindings.dart
7785
- add_asset_link/src/native_add.h
7886
- add_asset_link/src/native_add.c
79-
- add_asset_link/hook/build.dart
87+
- add_asset_link/hook/build.dart
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:logging/logging.dart';
6+
import 'package:native_assets_cli/native_assets_cli.dart';
7+
import 'package:native_toolchain_c/native_toolchain_c.dart';
8+
9+
const packageName = 'native_add';
10+
11+
void main(List<String> arguments) async {
12+
final config = await BuildConfig.fromArgs(arguments);
13+
final output = BuildOutput();
14+
final cbuilder = CBuilder.library(
15+
name: packageName,
16+
assetId: 'package/$packageName:src/${packageName}_bindings_generated.dart',
17+
sources: [
18+
'src/$packageName.c',
19+
],
20+
dartBuildFiles: ['build.dart'],
21+
);
22+
await cbuilder.run(
23+
buildConfig: config,
24+
buildOutput: output,
25+
logger: Logger('')
26+
..level = Level.ALL
27+
..onRecord.listen((record) {
28+
print('${record.level.name}: ${record.time}: ${record.message}');
29+
}),
30+
);
31+
await output.writeToFile(outDir: config.outDir);
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Run with `flutter pub run ffigen --config ffigen.yaml`.
2+
name: NativeAddBindings
3+
description: |
4+
Bindings for `src/native_add.h`.
5+
6+
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
7+
output: "lib/src/native_add_bindings_generated.dart"
8+
headers:
9+
entry-points:
10+
- "src/native_add.h"
11+
include-directives:
12+
- "src/native_add.h"
13+
preamble: |
14+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
15+
// for details. All rights reserved. Use of this source code is governed by a
16+
// BSD-style license that can be found in the LICENSE file.
17+
comments:
18+
style: any
19+
length: full
20+
ffi-native:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'src/native_add.dart';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'native_add_bindings_generated.dart' as bindings;
6+
7+
int add(int a, int b) => bindings.add(a, b);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// AUTO GENERATED FILE, DO NOT EDIT.
6+
//
7+
// Generated by `package:ffigen`.
8+
// ignore_for_file: type=lint
9+
import 'dart:ffi' as ffi;
10+
11+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
12+
external int add(
13+
int a,
14+
int b,
15+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- build.dart
2+
- ffigen.yaml
3+
- lib/native_add.dart
4+
- lib/src/native_add_bindings_generated.dart
5+
- lib/src/native_add.dart
6+
- pubspec.yaml
7+
- src/native_add.c
8+
- src/native_add.h
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: native_add_v1_0_0
2+
description: Sums two numbers with native code. Uses an old version of the protocol
3+
version: 0.1.0
4+
5+
publish_to: none
6+
7+
environment:
8+
sdk: '>=3.3.0 <4.0.0'
9+
10+
dependencies:
11+
logging: ^1.1.1
12+
native_assets_cli: ^0.3.0
13+
native_toolchain_c: ^0.3.0
14+
15+
dev_dependencies:
16+
ffigen: ^8.0.2
17+
lints: ^3.0.0
18+
some_dev_dep:
19+
path: ../some_dev_dep/
20+
test: ^1.23.1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include "native_add.h"
6+
7+
int32_t add(int32_t a, int32_t b) {
8+
return a + b;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#include <stdint.h>
6+
7+
#if _WIN32
8+
#define MYLIB_EXPORT __declspec(dllexport)
9+
#else
10+
#define MYLIB_EXPORT
11+
#endif
12+
13+
MYLIB_EXPORT int32_t add(int32_t a, int32_t b);

pkgs/native_assets_cli/lib/src/model/build_config.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ final class BuildConfigImpl extends HookConfigImpl implements BuildConfig {
2121

2222
@override
2323
String get outputName =>
24-
version > Version(1, 1, 0) ? 'build_output.json' : 'build_output.yaml';
24+
version > Version(1, 1, 0) ? 'build_output.json' : outputNameV1_1_0;
25+
26+
@override
27+
String get outputNameV1_1_0 => 'build_output.yaml';
2528

2629
@override
2730
Object? metadatum(String packageName, String key) {

pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Backwards compatibility: JSON is parsable as YAML.
1111
- Changed filename from `build_output.yaml` to `build_output.json`.
1212
Backwards compatibility older SDKs: write to the old file name if an older BuildConfig was passed in.
13+
Backwards compatibility: Try to read yaml file if json doesn't exist.
1314

1415
## 1.1.0
1516

pkgs/native_assets_cli/lib/src/model/hook_config.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ abstract class HookConfigImpl implements HookConfig {
7272
@override
7373
final OSImpl targetOS;
7474

75+
/// Output file name based on the protocol version.
76+
///
77+
/// Makes newer build hooks work with older Dart SDKs.
7578
String get outputName;
7679

80+
/// Legacy output file name.
81+
///
82+
/// Older build hooks output a yaml file, ignoring the newer protocol version
83+
/// in the config.
84+
String? get outputNameV1_1_0;
85+
7786
HookConfigImpl({
7887
required this.hook,
7988
required this.outputDirectory,
@@ -115,6 +124,10 @@ abstract class HookConfigImpl implements HookConfig {
115124

116125
Uri get outputFile => outputDirectory.resolve(outputName);
117126

127+
Uri? get outputFileV1_1_0 => outputNameV1_1_0 == null
128+
? null
129+
: outputDirectory.resolve(outputNameV1_1_0!);
130+
118131
// This is currently overriden by [BuildConfig], do account for older versions
119132
// still using a top-level build.dart.
120133
Uri get script => packageRoot.resolve('hook/').resolve(hook.scriptName);

pkgs/native_assets_cli/lib/src/model/hook_output.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ final class HookOutputImpl implements BuildOutput, LinkOutput {
160160
/// passing the highest supported version of BuildOutput.
161161
static Version latestVersion = HookConfigImpl.latestVersion;
162162

163-
/// Writes the JSON file from [file].
163+
/// Reads the JSON file from [file].
164164
static HookOutputImpl? readFromFile({required Uri file}) {
165165
final buildOutputFile = File.fromUri(file);
166166
if (buildOutputFile.existsSync()) {

pkgs/native_assets_cli/lib/src/model/link_config.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig {
6262

6363
@override
6464
Hook get hook => Hook.link;
65+
6566
@override
6667
String get outputName => 'link_output.json';
6768

69+
@override
70+
String? get outputNameV1_1_0 => null;
71+
6872
@override
6973
Map<String, Object> toJson() => {
7074
...hookToJson(),

0 commit comments

Comments
 (0)