Skip to content
Open
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
6 changes: 6 additions & 0 deletions pkgs/code_assets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.0-wip

- **Breaking change**: Overrode `==` and `hashCode` in `OS`, `Architecture`, `Sanitizer`, and `LinkModePreference` to support custom targets. These objects can no longer be used as keys of `const` maps and sets.
Comment thread
dcharkes marked this conversation as resolved.
- *Migration*: Use `final` (runtime) maps and sets instead of `const`, or use the string representations (e.g., `OS.name`, `Architecture.name`) as keys if `const` is required.
- Support custom target OS, architecture, and santizer in protocol extension.

## 1.2.1

- Avoid throwing a null-assertion error/exception when `input.config.code` is
Expand Down
17 changes: 17 additions & 0 deletions pkgs/code_assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,22 @@ void main(List<String> args) async {

See the full example in [example/host_name/](example/host_name/).

### Custom Operating Systems and Architectures

SDK authors can define custom OSes and Architectures that they support in a helper package like:

<!-- file://./../hooks_runner/test/build_runner/custom_os_arch_test.dart -->
```dart
extension CustomOS on OS {
static final ohos = OS.fromString('ohos');
}

extension CustomArchitecture on Architecture {
static final mips = Architecture.fromString('mips');
}
```

The users of those SDKs can import this helper package to use the custom OSes and Architectures in their build hooks.

For more information see [dart.dev/tools/hooks](https://dart.dev/tools/hooks).

4 changes: 2 additions & 2 deletions pkgs/code_assets/example/sqlite_prebuilt/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void main(List<String> args) async {
});
}

const _windowsDownloadInfo = {
final _windowsDownloadInfo = {
Architecture.arm64: (
url: 'https://sqlite.org/2025/sqlite-dll-win-arm64-3500400.zip',
sha256: 'c4f3d245377f4ee2da5c08e882ecaff376b35a609198dc399dd8cec0add1ea43',
Expand All @@ -50,7 +50,7 @@ const _windowsDownloadInfo = {
),
};

const _linuxPaths = {
final _linuxPaths = {
Architecture.arm64: <String>[],
Architecture.x64: ['/usr/lib/x86_64-linux-gnu/libsqlite3.so'],
};
Expand Down
30 changes: 12 additions & 18 deletions pkgs/code_assets/lib/src/code_assets/architecture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,27 @@ final class Architecture {
///
/// The name can be obtained from [Architecture.name] or
/// [Architecture.toString].
factory Architecture.fromString(String name) =>
ArchitectureSyntaxExtension.fromSyntax(ArchitectureSyntax.fromJson(name));
factory Architecture.fromString(String name) => values.firstWhere(
(e) => e.name == name,
orElse: () => Architecture._(name),
);

/// The current [Architecture].
static final Architecture current = _abiToArch[Abi.current()]!;

@override
bool operator ==(Object other) => other is Architecture && other.name == name;

@override
int get hashCode => name.hashCode;
}

/// Extension methods for [Architecture] to convert to and from the syntax.
extension ArchitectureSyntaxExtension on Architecture {
static final _toSyntax = {
for (final item in Architecture.values)
item: ArchitectureSyntax.fromJson(item.name),
};

static final _fromSyntax = {
for (var entry in _toSyntax.entries) entry.value: entry.key,
};

/// Converts this [Architecture] to its corresponding [ArchitectureSyntax].
ArchitectureSyntax toSyntax() => _toSyntax[this]!;
ArchitectureSyntax toSyntax() => ArchitectureSyntax.fromJson(name);

/// Converts an [ArchitectureSyntax] to its corresponding [Architecture].
static Architecture fromSyntax(ArchitectureSyntax syntax) =>
switch (_fromSyntax[syntax]) {
null => throw FormatException(
'The architecture "${syntax.name}" is not known',
),
final arch => arch,
};
Architecture.fromString(syntax.name);
}
10 changes: 5 additions & 5 deletions pkgs/code_assets/lib/src/code_assets/code_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ extension OSLibraryNaming on OS {
}

/// The default name prefix for dynamic libraries per [OS].
const _dylibPrefix = {
final _dylibPrefix = {
OS.android: 'lib',
OS.fuchsia: 'lib',
OS.iOS: 'lib',
Expand All @@ -199,7 +199,7 @@ const _dylibPrefix = {
};

/// The default extension for dynamic libraries per [OS].
const _dylibExtension = {
final _dylibExtension = {
OS.android: 'so',
OS.fuchsia: 'so',
OS.iOS: 'dylib',
Expand All @@ -209,10 +209,10 @@ const _dylibExtension = {
};

/// The default name prefix for static libraries per [OS].
const _staticlibPrefix = _dylibPrefix;
final _staticlibPrefix = _dylibPrefix;

/// The default extension for static libraries per [OS].
const _staticlibExtension = {
final _staticlibExtension = {
OS.android: 'a',
OS.fuchsia: 'a',
OS.iOS: 'a',
Expand All @@ -222,7 +222,7 @@ const _staticlibExtension = {
};

/// The default extension for executables per [OS].
const _executableExtension = {
final _executableExtension = {
OS.android: '',
OS.fuchsia: '',
OS.iOS: '',
Expand Down
25 changes: 10 additions & 15 deletions pkgs/code_assets/lib/src/code_assets/os.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class OS {
static const List<OS> values = [android, fuchsia, iOS, linux, macOS, windows];

/// Typical cross compilation between OSes.
static const osCrossCompilationDefault = {
static final osCrossCompilationDefault = {
OS.macOS: [OS.macOS, OS.iOS, OS.android],
OS.linux: [OS.linux, OS.android],
OS.windows: [OS.windows, OS.android],
Expand All @@ -57,30 +57,25 @@ final class OS {
///
/// The name can be obtained from [OS.name] or [OS.toString].
factory OS.fromString(String name) =>
OSSyntaxExtension.fromSyntax(OSSyntax.fromJson(name));
values.firstWhere((e) => e.name == name, orElse: () => OS._(name));

/// The current [OS].
///
/// Consistent with the [Platform.version] string.
static final OS current = OS.fromString(Platform.operatingSystem);

@override
bool operator ==(Object other) => other is OS && other.name == name;

@override
int get hashCode => name.hashCode;
}

/// Extension methods for [OS] to convert to and from the syntax model.
extension OSSyntaxExtension on OS {
static final _toSyntax = {
for (final item in OS.values) item: OSSyntax.fromJson(item.name),
};

static final _fromSyntax = {
for (var entry in _toSyntax.entries) entry.value: entry.key,
};

/// Converts this [OS] to its corresponding [OSSyntax].
OSSyntax toSyntax() => _toSyntax[this]!;
OSSyntax toSyntax() => OSSyntax.fromJson(name);

/// Converts an [OSSyntax] to its corresponding [OS].
static OS fromSyntax(OSSyntax syntax) => switch (_fromSyntax[syntax]) {
null => throw FormatException('The OS "${syntax.name}" is not known'),
final e => e,
};
static OS fromSyntax(OSSyntax syntax) => OS.fromString(syntax.name);
}
25 changes: 9 additions & 16 deletions pkgs/code_assets/lib/src/code_assets/sanitizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,21 @@ final class Sanitizer {
///
/// The name can be obtained from [Sanitizer.name] or [Sanitizer.toString].
factory Sanitizer.fromString(String name) =>
SanitizerSyntaxExtension.fromSyntax(SanitizerSyntax.fromJson(name));
values.firstWhere((e) => e.name == name, orElse: () => Sanitizer._(name));

@override
bool operator ==(Object other) => other is Sanitizer && other.name == name;

@override
int get hashCode => name.hashCode;
}

/// Extension methods for [Sanitizer] to convert to and from the syntax model.
extension SanitizerSyntaxExtension on Sanitizer {
static final _toSyntax = {
for (final item in Sanitizer.values)
item: SanitizerSyntax.fromJson(item.name),
};

static final _fromSyntax = {
for (var entry in _toSyntax.entries) entry.value: entry.key,
};

/// Converts this [Sanitizer] to its corresponding [SanitizerSyntax].
SanitizerSyntax toSyntax() =>
_toSyntax[this] ?? SanitizerSyntax.fromJson(name);
SanitizerSyntax toSyntax() => SanitizerSyntax.fromJson(name);

/// Converts an [SanitizerSyntax] to its corresponding [Sanitizer].
static Sanitizer fromSyntax(SanitizerSyntax syntax) =>
switch (_fromSyntax[syntax]) {
null => Sanitizer._(syntax.name),
final e => e,
};
Sanitizer.fromString(syntax.name);
}
2 changes: 1 addition & 1 deletion pkgs/code_assets/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
This library contains the hook protocol specification for bundling native code
with Dart packages.

version: 1.2.1
version: 2.0.0-wip

repository: https://github.com/dart-lang/native/tree/main/pkgs/code_assets

Expand Down
15 changes: 6 additions & 9 deletions pkgs/code_assets/test/code_assets/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,27 +325,24 @@ void main() async {
}
});

test('BuildInput.config.code: invalid architecture', () {
test('BuildInput.config.code: custom architecture', () {
final input = inputJson();
traverseJson<Map<String, Object?>>(input, [
'config',
'extensions',
'code_assets',
])['target_architecture'] = 'invalid_architecture';
expect(
() => BuildInput(input).config.code.targetArchitecture,
throwsFormatException,
);
])['target_architecture'] = 'mips';
expect(BuildInput(input).config.code.targetArchitecture.name, 'mips');
});

test('LinkInput.config.code: invalid os', () {
test('LinkInput.config.code: custom os', () {
final input = inputJson(hookType: 'link');
traverseJson<Map<String, Object?>>(input, [
'config',
'extensions',
'code_assets',
])['target_os'] = 'invalid_os';
expect(() => LinkInput(input).config.code.targetOS, throwsFormatException);
])['target_os'] = 'ohos';
expect(LinkInput(input).config.code.targetOS.name, 'ohos');
});

test('LinkInput.config.code.target_os invalid type', () {
Expand Down
3 changes: 2 additions & 1 deletion pkgs/ffigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 21.0.1-wip
## 21.1.0-wip

- Bump `package:meta` dependency to `^1.19.0` and stop generating
`experimental_member_use` lint ignore in generated bindings when
`@RecordUse()` is used.
- Bump `package:code_assets` dependency to `^2.0.0`.

## 21.0.0

Expand Down
10 changes: 10 additions & 0 deletions pkgs/ffigen/example/add/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ dev_dependencies:
ffigen:
path: '../../'
test: ^1.25.15

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
record_use:
path: ../../../record_use
10 changes: 10 additions & 0 deletions pkgs/ffigen/example/c_json/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ dev_dependencies:
dart_flutter_team_lints: ^3.5.2
ffigen:
path: '../../'

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
record_use:
path: ../../../record_use
10 changes: 10 additions & 0 deletions pkgs/ffigen/example/ffinative/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ dev_dependencies:
dart_flutter_team_lints: ^3.5.2
ffigen:
path: '../../'

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
record_use:
path: ../../../record_use
10 changes: 10 additions & 0 deletions pkgs/ffigen/example/libclang-example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ dev_dependencies:
dart_flutter_team_lints: ^3.5.2
ffigen:
path: '../../'

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
record_use:
path: ../../../record_use
8 changes: 8 additions & 0 deletions pkgs/ffigen/example/objective_c/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ dev_dependencies:
path: ../../

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
objective_c:
path: ../../../objective_c/
record_use:
path: ../../../record_use
12 changes: 12 additions & 0 deletions pkgs/ffigen/example/shared_bindings/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ dev_dependencies:
dart_flutter_team_lints: ^3.5.2
ffigen:
path: '../../'

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
objective_c:
path: ../../../objective_c
record_use:
path: ../../../record_use
10 changes: 10 additions & 0 deletions pkgs/ffigen/example/simple/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ dev_dependencies:
dart_flutter_team_lints: ^3.5.2
ffigen:
path: '../../'

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
record_use:
path: ../../../record_use
8 changes: 8 additions & 0 deletions pkgs/ffigen/example/swift/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ dev_dependencies:
path: ../../

dependency_overrides:
code_assets:
path: ../../../code_assets
hooks:
path: ../../../hooks
native_toolchain_c:
path: ../../../native_toolchain_c
objective_c:
path: ../../../objective_c/
record_use:
path: ../../../record_use
Loading
Loading