-
Notifications
You must be signed in to change notification settings - Fork 56
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
[native_toolchain_c] Move cross architecture compilation into separate test #1458
Merged
+146
−100
Merged
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions
32
pkgs/native_toolchain_c/test/clinker/treeshake_cross_test.dart
This file contains 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,32 @@ | ||
// 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. | ||
|
||
//TODO(mosuem): Enable for windows and mac. | ||
// See https://github.com/dart-lang/native/issues/1376. | ||
@TestOn('linux') | ||
library; | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:native_assets_cli/native_assets_cli.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'treeshake_helper.dart'; | ||
|
||
Future<void> main() async { | ||
if (!Platform.isLinux) { | ||
// Avoid needing status files on Dart SDK CI. | ||
return; | ||
} | ||
|
||
final architectures = [ | ||
Architecture.arm, | ||
Architecture.arm64, | ||
Architecture.ia32, | ||
Architecture.x64, | ||
Architecture.riscv64, | ||
]..remove(Architecture.current); | ||
|
||
await runTests(architectures); | ||
} |
110 changes: 110 additions & 0 deletions
110
pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart
This file contains 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,110 @@ | ||
// 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. | ||
|
||
//TODO(mosuem): Enable for windows and mac. | ||
// See https://github.com/dart-lang/native/issues/1376. | ||
@TestOn('linux') | ||
library; | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:native_assets_cli/native_assets_cli.dart'; | ||
import 'package:native_toolchain_c/native_toolchain_c.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../helpers.dart'; | ||
import 'build_testfiles.dart'; | ||
|
||
Future<void> runTests(List<Architecture> architectures) async { | ||
CLinker linkerManual(List<String> sources) => CLinker.library( | ||
name: 'mylibname', | ||
assetName: '', | ||
sources: sources, | ||
linkerOptions: LinkerOptions.manual( | ||
flags: ['--strip-debug', '-u', 'my_other_func'], | ||
gcSections: true, | ||
linkerScript: | ||
packageUri.resolve('test/clinker/testfiles/linker/symbols.lds'), | ||
), | ||
); | ||
CLinker linkerAuto(List<String> sources) => CLinker.library( | ||
name: 'mylibname', | ||
assetName: '', | ||
sources: sources, | ||
linkerOptions: LinkerOptions.treeshake(symbols: ['my_other_func']), | ||
); | ||
CLinker linkerAutoEmpty(List<String> sources) => CLinker.library( | ||
name: 'mylibname', | ||
assetName: '', | ||
sources: sources, | ||
linkerOptions: LinkerOptions.treeshake(symbols: null), | ||
); | ||
|
||
const os = OS.linux; | ||
|
||
late Map<String, int> sizes; | ||
sizes = <String, int>{}; | ||
for (final architecture in architectures) { | ||
for (final clinker in [ | ||
(name: 'manual', linker: linkerManual), | ||
(name: 'auto', linker: linkerAuto), | ||
(name: 'autoEmpty', linker: linkerAutoEmpty), | ||
]) { | ||
test('link test with CLinker ${clinker.name} and target $architecture', | ||
() async { | ||
final tempUri = await tempDirForTest(); | ||
final testArchive = await buildTestArchive(tempUri, os, architecture); | ||
|
||
final linkOutput = LinkOutput(); | ||
|
||
final config = LinkConfig.build( | ||
outputDirectory: tempUri, | ||
packageName: 'testpackage', | ||
packageRoot: tempUri, | ||
targetArchitecture: architecture, | ||
targetOS: os, | ||
buildMode: BuildMode.release, | ||
linkModePreference: LinkModePreference.dynamic, | ||
assets: [], | ||
); | ||
await clinker.linker([testArchive.toFilePath()]).run( | ||
config: config, | ||
output: linkOutput, | ||
logger: logger, | ||
); | ||
final filePath = linkOutput.assets.first.file!.toFilePath(); | ||
|
||
final machine = await readelfMachine(filePath); | ||
expect(machine, contains(readElfMachine[architecture])); | ||
|
||
final symbols = await readelfSymbols(filePath); | ||
if (clinker.linker != linkerAutoEmpty) { | ||
expect(symbols, matches(r'[0-9]+\smy_other_func')); | ||
expect(symbols, isNot(contains('my_func'))); | ||
} else { | ||
expect(symbols, contains('my_other_func')); | ||
expect(symbols, contains('my_func')); | ||
} | ||
|
||
final du = Process.runSync('du', ['-sb', filePath]).stdout as String; | ||
final sizeInBytes = int.parse(du.split('\t')[0]); | ||
sizes[clinker.name] = sizeInBytes; | ||
}); | ||
} | ||
tearDownAll( | ||
() { | ||
expect( | ||
sizes['manual'], | ||
lessThan(sizes['autoEmpty']!), | ||
reason: 'Tree-shaking reduces size', | ||
); | ||
expect( | ||
sizes['auto'], | ||
lessThan(sizes['autoEmpty']!), | ||
reason: 'Tree-shaking reduces size', | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to something dual to
treeshake_cross_test
?