Skip to content

Commit cdb263e

Browse files
authored
[code_assets] Change gethostname example to use FFIgen (#2532)
1 parent ca9e9c5 commit cdb263e

File tree

7 files changed

+118
-27
lines changed

7 files changed

+118
-27
lines changed

pkgs/code_assets/example/build/host_name/lib/src/host_name.dart

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,18 @@ import 'dart:io';
77

88
import 'package:ffi/ffi.dart';
99

10-
/// Get the host name on Unix systems.
11-
///
12-
/// C signature: `int gethostname(char *name, size_t size);`
13-
@Native<Int Function(Pointer<Uint8>, Size)>(
14-
symbol: 'gethostname',
15-
assetId: 'package:host_name/src/host_name.dart',
16-
)
17-
external int _gethostnameUnix(Pointer<Uint8> name, int len);
18-
19-
/// Get the host name on Windows systems.
20-
///
21-
/// C signature:
22-
///
23-
/// ```
24-
/// int gethostname(
25-
/// [out] char *name,
26-
/// [in] int namelen
27-
/// );
28-
/// ```
29-
@Native<Int Function(Pointer<Uint8>, Int)>(
30-
symbol: 'gethostname',
31-
assetId: 'package:host_name/src/host_name.dart',
32-
)
33-
external int _gethostnameWindows(Pointer<Uint8> name, int len);
10+
import 'third_party/unix.dart' as unix;
11+
import 'third_party/windows.dart' as windows;
3412

3513
/// The machine's hostname.
3614
///
3715
/// Returns `null` if looking up the machines host name fails.
3816
String? get hostName => using((arena) {
3917
const maxHostNameLength = 256;
40-
final buffer = arena<Uint8>(maxHostNameLength);
18+
final buffer = arena<Char>(maxHostNameLength);
4119
final result = Platform.isWindows
42-
? _gethostnameWindows(buffer, maxHostNameLength)
43-
: _gethostnameUnix(buffer, maxHostNameLength);
20+
? windows.gethostname(buffer, maxHostNameLength)
21+
: unix.gethostname(buffer, maxHostNameLength);
4422

4523
if (result != 0) {
4624
// The `errno` or `WSAGetLastError` are not preserved currently.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 1991-2022 Free Software Foundation, Inc.
2+
// This file is part of the GNU C Library.
3+
//
4+
// The GNU C Library is free software; you can redistribute it and/or
5+
// modify it under the terms of the GNU Lesser General Public
6+
// License as published by the Free Software Foundation; either
7+
// version 2.1 of the License, or (at your option) any later version.
8+
//
9+
// The GNU C Library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
// Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public
15+
// License along with the GNU C Library; if not, see
16+
// <https://www.gnu.org/licenses/>.
17+
18+
// AUTO GENERATED FILE, DO NOT EDIT.
19+
//
20+
// Generated by `package:ffigen`.
21+
// ignore_for_file: type=lint
22+
@ffi.DefaultAsset('package:host_name/src/host_name.dart')
23+
library;
24+
25+
import 'dart:ffi' as ffi;
26+
27+
@ffi.Native<ffi.Int Function(ffi.Pointer<ffi.Char>, ffi.Size)>()
28+
external int gethostname(ffi.Pointer<ffi.Char> arg0, int arg1);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TODO
2+
3+
// AUTO GENERATED FILE, DO NOT EDIT.
4+
//
5+
// Generated by `package:ffigen`.
6+
// ignore_for_file: type=lint
7+
@ffi.DefaultAsset('package:host_name/src/host_name.dart')
8+
library;
9+
10+
import 'dart:ffi' as ffi;
11+
12+
@ffi.Native<ffi.Int Function(ffi.Pointer<ffi.Char>, ffi.Int)>()
13+
external int gethostname(ffi.Pointer<ffi.Char> name, int namelen);

pkgs/code_assets/example/build/host_name/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ dependencies:
1616
hooks: any
1717

1818
dev_dependencies:
19+
ffigen: ^19.1.0
1920
lints: ^6.0.0
2021
test: ^1.25.15
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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+
#include <unistd.h>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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+
#include <Winsock2.h>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 'dart:io';
6+
7+
import 'package:ffigen/ffigen.dart';
8+
9+
void main() {
10+
final packageRoot = Platform.script.resolve('../');
11+
const ffiNativeConfig = FfiNativeConfig(
12+
enabled: true,
13+
assetId: 'package:host_name/src/host_name.dart',
14+
);
15+
final functionDeclarationFilter = DeclarationFilters(
16+
shouldInclude: (declaration) => declaration.originalName == 'gethostname',
17+
);
18+
if (Platform.isWindows) {
19+
FfiGen().run(
20+
Config(
21+
output: packageRoot.resolve('lib/src/third_party/windows.dart'),
22+
entryPoints: [packageRoot.resolve('src/windows.h')],
23+
preamble: '''
24+
// This file includes parts which are Copyright (c) 1982-1986 Regents
25+
// of the University of California. All rights reserved. The
26+
// Berkeley Software License Agreement specifies the terms and
27+
// conditions for redistribution.
28+
''',
29+
functionDecl: functionDeclarationFilter,
30+
ffiNativeConfig: ffiNativeConfig,
31+
),
32+
);
33+
} else {
34+
FfiGen().run(
35+
Config(
36+
output: packageRoot.resolve('lib/src/third_party/unix.dart'),
37+
entryPoints: [packageRoot.resolve('src/unix.h')],
38+
preamble: '''
39+
// Copyright (C) 1991-2022 Free Software Foundation, Inc.
40+
// This file is part of the GNU C Library.
41+
//
42+
// The GNU C Library is free software; you can redistribute it and/or
43+
// modify it under the terms of the GNU Lesser General Public
44+
// License as published by the Free Software Foundation; either
45+
// version 2.1 of the License, or (at your option) any later version.
46+
//
47+
// The GNU C Library is distributed in the hope that it will be useful,
48+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
49+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50+
// Lesser General Public License for more details.
51+
//
52+
// You should have received a copy of the GNU Lesser General Public
53+
// License along with the GNU C Library; if not, see
54+
// <https://www.gnu.org/licenses/>.
55+
''',
56+
functionDecl: functionDeclarationFilter,
57+
ffiNativeConfig: ffiNativeConfig,
58+
),
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)