-
Notifications
You must be signed in to change notification settings - Fork 69
[native_toolchain_c] Add linking on Linux #987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
7078488
Works
mosuem be86b28
Small changes
mosuem 055fd43
Fix test
mosuem 01a6846
Remove executable from args
mosuem ac1c981
Remove runclinker
mosuem db4d2f5
remove unused options
mosuem afcaee3
Rename flags
mosuem 8aa486b
Fix test
mosuem 74b198e
Remove more executable stuff
mosuem 92a48e5
Add docs
mosuem 60c4de1
Add more docs
mosuem c9c02f0
Add todos
mosuem b1e4592
Use linkoptions as flag
mosuem 3bfe024
Changes as per review
mosuem 287ef2b
Fix analyzer issues
mosuem 64e9085
Hide from API
mosuem d4d7c3b
Changes as per review
mosuem fe11eb5
Add test project
mosuem 29192b4
Add pub get
mosuem 84215ac
Fix ordering
mosuem 01794f1
Add license
mosuem b1878e8
Changes as per review
mosuem 5f09aa2
Renames
mosuem 7346a89
Fix typo
mosuem 7d4643b
target -> host
mosuem 4e7c232
host -> target :)
mosuem 8f685a2
Changes as per review
mosuem a924987
More changes as per review
mosuem b631354
add test name
mosuem 95c67be
Undo rename
mosuem 2e12234
Build for right architecture
mosuem c72761a
Changes as per review
mosuem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
.../native_assets_builder/test_data/treeshaking_native_libs/bin/treeshaking_native_libs.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:treeshaking_native_libs/treeshaking_native_libs.dart'; | ||
|
||
void main(List<String> args) { | ||
print(add(5, 3)); | ||
} |
22 changes: 22 additions & 0 deletions
22
pkgs/native_assets_builder/test_data/treeshaking_native_libs/ffigen.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Run with `flutter pub run ffigen --config ffigen.yaml`. | ||
name: NativeCalcBindings | ||
description: | | ||
Bindings for `src/native_add.h` and `src/native_multiply.h`. | ||
|
||
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`. | ||
output: "lib/src/treeshaking_native_libs_bindings_generated.dart" | ||
headers: | ||
entry-points: | ||
- "src/native_add.h" | ||
- "src/native_multiply.h" | ||
include-directives: | ||
- "src/native_add.h" | ||
- "src/native_multiply.h" | ||
preamble: | | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
comments: | ||
style: any | ||
length: full | ||
ffi-native: |
33 changes: 33 additions & 0 deletions
33
pkgs/native_assets_builder/test_data/treeshaking_native_libs/hook/build.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:logging/logging.dart'; | ||
import 'package:native_assets_cli/native_assets_cli.dart'; | ||
import 'package:native_toolchain_c/native_toolchain_c.dart'; | ||
|
||
void main(List<String> arguments) async { | ||
await build(arguments, (config, output) async { | ||
final cbuilder = CBuilder.library( | ||
name: config.packageName + (config.linkingEnabled ? '_static' : ''), | ||
assetName: 'src/${config.packageName}_bindings_generated.dart', | ||
sources: [ | ||
'src/native_add.c', | ||
'src/native_multiply.c', | ||
], | ||
linkModePreference: config.linkingEnabled | ||
? LinkModePreference.static | ||
: LinkModePreference.dynamic, | ||
); | ||
await cbuilder.run( | ||
config: config, | ||
output: output, | ||
linkInPackage: config.linkingEnabled ? config.packageName : null, | ||
logger: Logger('') | ||
..level = Level.ALL | ||
..onRecord.listen((record) { | ||
print('${record.level.name}: ${record.time}: ${record.message}'); | ||
}), | ||
); | ||
}); | ||
} |
30 changes: 30 additions & 0 deletions
30
pkgs/native_assets_builder/test_data/treeshaking_native_libs/hook/link.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:logging/logging.dart'; | ||
import 'package:native_assets_cli/native_assets_cli.dart'; | ||
import 'package:native_toolchain_c/native_toolchain_c.dart'; | ||
|
||
void main(List<String> arguments) async { | ||
await link( | ||
arguments, | ||
(config, output) async { | ||
final linker = CLinker.library( | ||
name: config.packageName, | ||
assetName: config.assets.single.id.split('/').skip(1).join('/'), | ||
linkerOptions: LinkerOptions.treeshake(symbols: ['add']), | ||
mosuem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sources: [config.assets.single.file!.toFilePath()], | ||
); | ||
await linker.run( | ||
config: config, | ||
output: output, | ||
logger: Logger('') | ||
..level = Level.ALL | ||
..onRecord.listen((record) { | ||
print('${record.level.name}: ${record.time}: ${record.message}'); | ||
}), | ||
); | ||
}, | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
...ive_assets_builder/test_data/treeshaking_native_libs/lib/src/treeshaking_native_libs.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'treeshaking_native_libs_bindings_generated.dart' as bindings; | ||
|
||
int add(int a, int b) => bindings.add(a, b); | ||
int multiply(int a, int b) => bindings.multiply(a, b); |
21 changes: 21 additions & 0 deletions
21
...test_data/treeshaking_native_libs/lib/src/treeshaking_native_libs_bindings_generated.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// AUTO GENERATED FILE, DO NOT EDIT. | ||
// | ||
// Generated by `package:ffigen`. | ||
// ignore_for_file: type=lint | ||
import 'dart:ffi' as ffi; | ||
|
||
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add') | ||
external int add( | ||
int a, | ||
int b, | ||
); | ||
|
||
@ffi.Native<ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr)>(symbol: 'multiply') | ||
external int multiply( | ||
int a, | ||
int b, | ||
); |
5 changes: 5 additions & 0 deletions
5
.../native_assets_builder/test_data/treeshaking_native_libs/lib/treeshaking_native_libs.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
export 'src/treeshaking_native_libs.dart'; |
24 changes: 24 additions & 0 deletions
24
pkgs/native_assets_builder/test_data/treeshaking_native_libs/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: treeshaking_native_libs | ||
description: Treeshake symbols by name from a native library. | ||
version: 0.1.0 | ||
|
||
publish_to: none | ||
|
||
environment: | ||
sdk: '>=3.3.0 <4.0.0' | ||
|
||
dependencies: | ||
logging: ^1.1.1 | ||
# native_assets_cli: ^0.7.1 | ||
native_assets_cli: | ||
path: ../../../native_assets_cli/ | ||
# native_toolchain_c: ^0.5.2 | ||
native_toolchain_c: | ||
path: ../../../native_toolchain_c/ | ||
|
||
dev_dependencies: | ||
ffigen: ^8.0.2 | ||
lints: ^3.0.0 | ||
some_dev_dep: | ||
path: ../some_dev_dep/ | ||
test: ^1.23.1 |
9 changes: 9 additions & 0 deletions
9
pkgs/native_assets_builder/test_data/treeshaking_native_libs/src/native_add.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#include "native_add.h" | ||
|
||
int32_t add(int32_t a, int32_t b) { | ||
return a + b; | ||
} |
13 changes: 13 additions & 0 deletions
13
pkgs/native_assets_builder/test_data/treeshaking_native_libs/src/native_add.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#include <stdint.h> | ||
|
||
#if _WIN32 | ||
#define MYLIB_EXPORT __declspec(dllexport) | ||
#else | ||
#define MYLIB_EXPORT | ||
#endif | ||
|
||
MYLIB_EXPORT int32_t add(int32_t a, int32_t b); |
9 changes: 9 additions & 0 deletions
9
pkgs/native_assets_builder/test_data/treeshaking_native_libs/src/native_multiply.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#include "native_multiply.h" | ||
|
||
MYLIB_EXPORT intptr_t multiply(intptr_t a, intptr_t b) { | ||
return a * b; | ||
} |
13 changes: 13 additions & 0 deletions
13
pkgs/native_assets_builder/test_data/treeshaking_native_libs/src/native_multiply.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#include <stdint.h> | ||
|
||
#if _WIN32 | ||
#define MYLIB_EXPORT __declspec(dllexport) | ||
#else | ||
#define MYLIB_EXPORT | ||
#endif | ||
|
||
MYLIB_EXPORT intptr_t multiply(intptr_t a, intptr_t b); |
13 changes: 13 additions & 0 deletions
13
...e_assets_builder/test_data/treeshaking_native_libs/test/treeshaking_native_libs_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:treeshaking_native_libs/treeshaking_native_libs.dart'; | ||
|
||
void main() { | ||
mosuem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test('native add test', () { | ||
final result = add(4, 6); | ||
expect(result, equals(10)); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.