Skip to content

Commit 3c8c1e7

Browse files
authored
Use internal libc bindings (#231)
1 parent 65bebe7 commit 3c8c1e7

27 files changed

+1842
-160
lines changed

.github/workflows/io_file.yml

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,45 @@ jobs:
2828
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
2929
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
3030
with:
31-
sdk: stable
31+
sdk: dev
3232
- run: dart pub get
3333
- run: dart analyze --fatal-infos
3434
- run: dart format --output=none --set-exit-if-changed .
3535

36+
check-generation:
37+
# Verify that the generated code is up-to-date. Every platform/arch should
38+
# generate the same API bindings.
39+
strategy:
40+
fail-fast: false
41+
matrix:
42+
os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest]
43+
runs-on: ${{ matrix.os }}
44+
steps:
45+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
46+
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
47+
with:
48+
sdk: dev
49+
- run: dart pub get
50+
- run: dart run ffigen --config ffigen.yaml
51+
- run: dart run ffigen --config constants-ffigen.yaml
52+
- run: dart --enable-experiment=native-assets run tool/build_constants.dart
53+
- run: dart format lib/src/*.dart
54+
- run: git diff --exit-code
55+
3656
desktop-vm-test:
3757
strategy:
3858
fail-fast: false
3959
matrix:
40-
sdk: [stable, dev]
41-
os: [ubuntu-latest, windows-latest, macos-latest]
60+
sdk: [dev]
61+
os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-latest]
4262
runs-on: ${{ matrix.os }}
4363
steps:
4464
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
4565
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
4666
with:
4767
sdk: ${{ matrix.sdk }}
4868

49-
- run: dart test --test-randomize-ordering-seed=random --platform vm
69+
- run: dart --enable-experiment=native-assets test --test-randomize-ordering-seed=random --platform vm
5070

5171
profile-vm-test:
5272
# Ensure that the tests pass when run under the profiler (which sends
@@ -56,14 +76,14 @@ jobs:
5676
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
5777
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
5878
with:
59-
sdk: stable
60-
- run: dart --profiler --profile_period=50 test --test-randomize-ordering-seed=random --platform vm
79+
sdk: dev
80+
- run: dart --enable-experiment=native-assets --profiler --profile_period=50 test --test-randomize-ordering-seed=random --platform vm
6181

6282
desktop-vm-benchmark:
6383
strategy:
6484
fail-fast: false
6585
matrix:
66-
sdk: [stable]
86+
sdk: [dev]
6787
os: [ubuntu-latest, windows-latest, macos-latest]
6888
runs-on: ${{ matrix.os }}
6989
steps:
@@ -73,11 +93,14 @@ jobs:
7393
sdk: ${{ matrix.sdk }}
7494
- run: dart pub get
7595
- name: 🪑 Run Read Benchmarks
76-
run: dart run benchmarks/read_as_bytes.dart
96+
run: dart --enable-experiment=native-assets run benchmarks/read_as_bytes.dart
7797
- name: 🪑 Run Write Benchmarks
78-
run: dart run benchmarks/write_as_bytes.dart
98+
run: dart --enable-experiment=native-assets run benchmarks/write_as_bytes.dart
7999

80100
ios-vm-test:
101+
# TODO(brianquinlan): Disabled because native assets are not supported by
102+
# Flutter integration tests.
103+
if: ${{false}}
81104
runs-on: macos-latest
82105
defaults:
83106
run:
@@ -86,8 +109,7 @@ jobs:
86109
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
87110
- uses: subosito/flutter-action@e938fdf56512cc96ef2f93601a5a40bde3801046
88111
with:
89-
flutter-version: any
90-
channel: 'stable'
112+
channel: master
91113
- uses: futureware-tech/simulator-action@dab10d813144ef59b48d401cd95da151222ef8cd
92114
with:
93115
os: iOS
@@ -103,6 +125,6 @@ jobs:
103125
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
104126
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
105127
with:
106-
sdk: stable
128+
sdk: dev
107129

108-
- run: dart test --test-randomize-ordering-seed=random --platform chrome
130+
- run: dart --enable-experiment=native-assets test --test-randomize-ordering-seed=random --platform chrome

pkgs/io_file/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ See
1212
| :--- | :---: | :---: | :---: | :---: | :----: | :--------: | :----------: |
1313
| canonicalize path | | | | | | | |
1414
| copy file | | | | | | | |
15-
| create directory | || ||| | |
16-
| create hard link | | | | | | | |
17-
| create symbolic link | | | | | | | |
18-
| create tmp directory | || ||| | |
19-
| create tmp file | | | | | | | |
20-
| delete directory | || ||| | |
21-
| delete file | | | | | | | |
22-
| delete tree | | | | || | |
23-
| enum dir contents | | | | | | | |
24-
| exists | | | | | | | |
25-
| get metadata (stat) | | | | | | | |
26-
| identity (same file) | || ||| | |
27-
| open | | | | | | | |
28-
| read file (bytes) | || ||| | |
29-
| read file (lines) | | | | | | | |
30-
| read file (string) | | | | | | | |
31-
| rename | || ||| | |
32-
| set permissions | | | | | | | |
33-
| write file (bytes) | || ||| | |
34-
| write file (string) | || ||| | |
15+
| create directory | || ||| | |
16+
| create hard link | | | | | | | |
17+
| create symbolic link | | | | | | | |
18+
| create tmp directory | || ||| | |
19+
| create tmp file | | | | | | | |
20+
| delete directory | || ||| | |
21+
| delete file | | | | | | |
22+
| delete tree | | | | || | |
23+
| enum dir contents | | | | | | | |
24+
| exists | | | | | | |
25+
| get metadata (stat) | | | | | | | |
26+
| identity (same file) | || ||| | |
27+
| open | | | | | | |
28+
| read file (bytes) | || ||| | |
29+
| read file (lines) | | | | | | | |
30+
| read file (string) | | | | | | | |
31+
| rename | || ||| | |
32+
| set permissions | | | | | | | |
33+
| write file (bytes) | || ||| | |
34+
| write file (string) | || ||| | |
3535

3636
### File
3737

pkgs/io_file/constants-ffigen.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Run with `dart run ffigen --config constants-ffigen.yaml`.
2+
name: NativeAddBindings
3+
description: |
4+
Bindings for `constants.g.h`.
5+
6+
Regenerate bindings with `dart run ffigen --config constants-ffigen.yaml`.
7+
output: 'lib/src/constant_bindings.g.dart'
8+
headers:
9+
entry-points:
10+
- 'src/constants.g.h'
11+
12+
include-directives:
13+
- 'src/constants.g.h'
14+
comments:
15+
style: any
16+
length: full
17+
ffi-native:
18+
asset-id: package:io_file/libc_shim
19+
structs:
20+
rename:
21+
'libc_shim_(.*)': '$1'
22+
functions:
23+
rename:
24+
'libc_shim_(.*)': '$1'
25+
typedefs:
26+
rename:
27+
'libc_shim_(.*)': '$1'

pkgs/io_file/constants.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"<sys/stat.h>": [
3+
"S_IEXEC",
4+
"S_IFBLK",
5+
"S_IFCHR",
6+
"S_IFDIR",
7+
"S_IFIFO",
8+
"S_IFLNK",
9+
"S_IFMT",
10+
"S_IFREG",
11+
"S_IFSOCK",
12+
"S_IFWHT",
13+
"S_IREAD",
14+
"S_IRGRP",
15+
"S_IROTH",
16+
"S_IRUSR",
17+
"S_IRWXG",
18+
"S_IRWXO",
19+
"S_IRWXU",
20+
"S_ISGID",
21+
"S_ISTXT",
22+
"S_ISUID",
23+
"S_ISVTX",
24+
"S_IWGRP",
25+
"S_IWOTH",
26+
"S_IWRITE",
27+
"S_IWUSR",
28+
"S_IXGRP",
29+
"S_IXOTH",
30+
"S_IXUSR",
31+
"UF_APPEND",
32+
"UF_HIDDEN"
33+
],
34+
"<errno.h>": [
35+
"EACCES",
36+
"EEXIST",
37+
"EINTR",
38+
"ENOENT",
39+
"EPERM",
40+
"O_APPEND",
41+
"O_CLOEXEC",
42+
"O_CREAT",
43+
"O_EXCL",
44+
"O_RDONLY",
45+
"O_TRUNC",
46+
"O_WRONLY"
47+
],
48+
"<fcntl.h>": [
49+
"AT_FDCWD",
50+
"AT_REMOVEDIR"
51+
]
52+
}

pkgs/io_file/ffigen.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Run with `dart run ffigen --config ffigen.yaml`.
2+
name: NativeAddBindings
3+
description: |
4+
Bindings for `src/libc_shim.h`.
5+
6+
Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
7+
output: 'lib/src/libc_bindings.g.dart'
8+
headers:
9+
entry-points:
10+
- 'src/libc_shim.h'
11+
12+
include-directives:
13+
- 'src/libc_shim.h'
14+
preamble: |
15+
// ignore_for_file: type=lint, unused_field
16+
comments:
17+
style: any
18+
length: full
19+
ffi-native:
20+
asset-id: package:io_file/libc_shim
21+
structs:
22+
rename:
23+
'libc_shim_(.*)': '$1'
24+
functions:
25+
rename:
26+
'libc_shim_(.*)': '$1'
27+
typedefs:
28+
rename:
29+
'libc_shim_(.*)': '$1'

pkgs/io_file/hook/build.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025, 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:code_assets/code_assets.dart';
6+
import 'package:hooks/hooks.dart';
7+
import 'package:logging/logging.dart';
8+
import 'package:native_toolchain_c/native_toolchain_c.dart';
9+
10+
void main(List<String> args) async {
11+
await build(args, (input, output) async {
12+
final packageName = input.packageName;
13+
if (input.config.code.targetOS == OS.windows) return;
14+
await CBuilder.library(
15+
name: packageName,
16+
assetName: 'libc_shim',
17+
sources: ['src/libc_shim.c', 'src/constants.g.c'],
18+
flags: ['-Weverything'],
19+
).run(
20+
input: input,
21+
output: output,
22+
logger:
23+
Logger('')
24+
..level = Level.ALL
25+
..onRecord.listen((record) => print(record.message)),
26+
);
27+
});
28+
}

0 commit comments

Comments
 (0)