-
Notifications
You must be signed in to change notification settings - Fork 74
[native_toolchain_c] Fix treeshaking on mac #2399
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
Open
mosuem
wants to merge
16
commits into
main
Choose a base branch
from
fixTreeshakeMac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−56
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ac25f4d
Fix treeshaking on mac
mosuem 58e769b
Add more docs.
mosuem b8ff85a
Make it nicer
mosuem 4e1d92f
Fix tests
mosuem bbc0db8
Try stuff
mosuem 425417d
Remove debug statement
mosuem 189efe3
fix
mosuem c4acc0d
Remove experiments
mosuem 22f7576
merge
mosuem 1907a4a
add rust
mosuem c08088c
Fix example
mosuem 5032e20
Compile with rust
mosuem a8ce9ee
Skip tests when no rust is installed
mosuem d1483ae
fix
mosuem a01e8a4
Fix version
mosuem ee0e597
Address comments
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) 2025, 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 'dart:io'; | ||
|
||
import 'package:code_assets/code_assets.dart'; | ||
import 'package:hooks/hooks.dart'; | ||
import 'package:native_toolchain_c/native_toolchain_c.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../helpers.dart'; | ||
|
||
Future<void> main() async { | ||
late final LinkInput linkInput; | ||
late final Uri staticLib; | ||
final linkOutputBuilder = LinkOutputBuilder(); | ||
final targetArchitecture = Architecture.current; | ||
final targetOS = OS.current; | ||
late final bool rustToolchainInstalled; | ||
setUpAll(() async { | ||
final tempUri = await tempDirForTest(); | ||
final tempUri2 = await tempDirForTest(); | ||
staticLib = tempUri.resolve(targetOS.staticlibFileName('libtest')); | ||
final processResult = await Process.run('rustc', [ | ||
'--crate-type=staticlib', | ||
'test/clinker/testfiles/linker/test.rs', | ||
'-o', | ||
staticLib.toFilePath(), | ||
]); | ||
rustToolchainInstalled = processResult.exitCode == 0; | ||
if (rustToolchainInstalled) { | ||
await File.fromUri( | ||
staticLib, | ||
).copy(tempUri.resolve('libtest.a').toFilePath()); | ||
} | ||
final linkInputBuilder = LinkInputBuilder() | ||
..setupShared( | ||
packageName: 'testpackage', | ||
packageRoot: tempUri, | ||
outputFile: tempUri.resolve('output.json'), | ||
outputDirectoryShared: tempUri2, | ||
) | ||
..setupLink(assets: [], recordedUsesFile: null) | ||
..addExtension( | ||
CodeAssetExtension( | ||
targetOS: targetOS, | ||
targetArchitecture: targetArchitecture, | ||
linkModePreference: LinkModePreference.dynamic, | ||
), | ||
); | ||
|
||
linkInput = linkInputBuilder.build(); | ||
}); | ||
test('link rust binary with script treeshakes', () async { | ||
if (!rustToolchainInstalled) { | ||
return; | ||
} | ||
final treeshakeOption = LinkerOptions.treeshake( | ||
symbolsToKeep: ['my_other_func'], | ||
); | ||
final symbols = await _link( | ||
staticLib, | ||
treeshakeOption, | ||
linkInput, | ||
linkOutputBuilder, | ||
targetArchitecture, | ||
targetOS, | ||
); | ||
final skipReason = symbols == null | ||
? 'tool to extract symbols unavailable' | ||
: false; | ||
expect(symbols, contains('my_other_func'), skip: skipReason); | ||
expect(symbols, isNot(contains('my_func')), skip: skipReason); | ||
}); | ||
|
||
test('link rust binary without script keeps symbols', () async { | ||
if (!rustToolchainInstalled) { | ||
return; | ||
} | ||
final manualOption = LinkerOptions.manual( | ||
symbolsToKeep: ['my_other_func'], | ||
stripDebug: true, | ||
gcSections: true, | ||
); | ||
final symbols = await _link( | ||
staticLib, | ||
manualOption, | ||
linkInput, | ||
linkOutputBuilder, | ||
targetArchitecture, | ||
targetOS, | ||
); | ||
final skipReason = symbols == null | ||
? 'tool to extract symbols unavailable' | ||
: false; | ||
expect(symbols, contains('my_other_func'), skip: skipReason); | ||
expect(symbols, contains('my_func'), skip: skipReason); | ||
}); | ||
} | ||
|
||
Future<String?> _link( | ||
Uri staticLib, | ||
LinkerOptions manualOption, | ||
LinkInput linkInput, | ||
LinkOutputBuilder linkOutputBuilder, | ||
Architecture targetArchitecture, | ||
OS targetOS, | ||
) async { | ||
await CLinker.library( | ||
name: 'mylibname', | ||
assetName: '', | ||
sources: [staticLib.toFilePath()], | ||
linkerOptions: manualOption, | ||
).run(input: linkInput, output: linkOutputBuilder, logger: logger); | ||
|
||
final linkOutput = linkOutputBuilder.build(); | ||
final asset = linkOutput.assets.code.first; | ||
|
||
await expectMachineArchitecture(asset.file!, targetArchitecture, targetOS); | ||
|
||
return await readSymbols(asset, targetOS); | ||
} |
1 change: 1 addition & 0 deletions
1
pkgs/native_toolchain_c/test/clinker/testfiles/linker/symbols.txt
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 @@ | ||
_my_other_func |
9 changes: 9 additions & 0 deletions
9
pkgs/native_toolchain_c/test/clinker/testfiles/linker/test.rs
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 @@ | ||
#[no_mangle] | ||
pub extern "C" fn my_func() -> u64 { | ||
return 41; | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn my_other_func() -> u64 { | ||
return 42; | ||
} |
Oops, something went wrong.
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.