Skip to content

Commit

Permalink
Version 3.2.0-112.0.dev
Browse files Browse the repository at this point in the history
Merge d00706a into dev
  • Loading branch information
Dart CI committed Aug 25, 2023
2 parents 055e2f3 + d00706a commit dcbda9a
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 51 deletions.
20 changes: 20 additions & 0 deletions pkg/dartdev/lib/src/commands/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:front_end/src/api_prototype/compiler_options.dart'
import 'package:native_assets_builder/native_assets_builder.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:path/path.dart' as path;
import 'package:vm/target_os.dart'; // For possible --target-os values.

import '../core.dart';
import '../native_assets.dart';
Expand Down Expand Up @@ -41,6 +42,9 @@ class BuildCommand extends DartdevCommand {
allowed: ['exe', 'aot'],
defaultsTo: 'exe',
)
..addOption('target-os',
help: 'Compile to a specific target operating system.',
allowed: TargetOS.names)
..addOption(
'verbosity',
help: 'Sets the verbosity level of the compilation.',
Expand Down Expand Up @@ -92,6 +96,21 @@ class BuildCommand extends DartdevCommand {
final format = args[formatOptionName] as String;
final outputExeUri = outputUri
.resolve('${sourceUri.pathSegments.last.split('.').first}.$format');
String? targetOS = args['target-os'];
if (format != 'exe') {
assert(format == 'aot');
// If we're generating an AOT snapshot and not an executable, then
// targetOS is allowed to be null for a platform-independent snapshot
// or a different platform than the host.
} else if (targetOS == null) {
targetOS = Platform.operatingSystem;
} else if (targetOS != Platform.operatingSystem) {
stderr.writeln(
"'dart build -f $format' does not support cross-OS compilation.");
stderr.writeln('Host OS: ${Platform.operatingSystem}');
stderr.writeln('Target OS: $targetOS');
return 128;
}

final outputDir = Directory.fromUri(outputUri);
if (await outputDir.exists()) {
Expand Down Expand Up @@ -180,6 +199,7 @@ Use linkMode as dynamic library instead.""");
defines: [],
nativeAssets: nativeAssetsDartUri?.toFilePath(),
packages: packageConfig?.toFilePath(),
targetOS: targetOS,
);

if (tempUri != null) {
Expand Down
20 changes: 18 additions & 2 deletions pkg/dartdev/lib/src/commands/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Remove debugging information from the output and save it separately to the speci
// executable only supports AOT runtimes, so these commands are disabled.
if (Platform.version.contains('ia32')) {
stderr.write(
"'dart compile $format' is not supported on x86 architectures");
"'dart compile $commandName' is not supported on x86 architectures");
return 64;
}
final args = argResults!;
Expand Down Expand Up @@ -337,6 +337,22 @@ Remove debugging information from the output and save it separately to the speci
}
}

String? targetOS = args['target-os'];
if (format != 'exe') {
assert(format == 'aot');
// If we're generating an AOT snapshot and not an executable, then
// targetOS is allowed to be null for a platform-independent snapshot
// or a different platform than the host.
} else if (targetOS == null) {
targetOS = Platform.operatingSystem;
} else if (targetOS != Platform.operatingSystem) {
stderr.writeln(
"'dart compile $commandName' does not support cross-OS compilation.");
stderr.writeln('Host OS: ${Platform.operatingSystem}');
stderr.writeln('Target OS: $targetOS');
return 128;
}

try {
await generateNative(
kind: format,
Expand All @@ -350,7 +366,7 @@ Remove debugging information from the output and save it separately to the speci
verbose: verbose,
verbosity: args['verbosity'],
extraOptions: args['extra-gen-snapshot-options'],
targetOS: args['target-os'],
targetOS: targetOS,
);
return 0;
} catch (e, st) {
Expand Down
141 changes: 133 additions & 8 deletions pkg/dartdev/test/commands/compile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ const String unsoundNullSafetyError =
'Error: the flag --no-sound-null-safety is not supported in Dart 3.';
const String unsoundNullSafetyWarning =
'Warning: the flag --no-sound-null-safety is deprecated and pending removal.';
String usingTargetOSMessageForPlatform(String targetOS) =>
'Specializing Platform getters for target OS $targetOS.';
final String usingTargetOSMessage =
usingTargetOSMessageForPlatform(Platform.operatingSystem);
String crossOSNotAllowedError(String command) =>
"'dart compile $command' does not support cross-OS compilation.";
final String hostOSMessage = 'Host OS: ${Platform.operatingSystem}';
String targetOSMessage(String targetOS) => 'Target OS: $targetOS';

void defineCompileTests() {
final isRunningOnIA32 = Platform.version.contains('ia32');
Expand Down Expand Up @@ -251,6 +259,7 @@ void defineCompileTests() {
'-v',
inFile,
]);
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stderr, isNot(contains(soundNullSafetyMessage)));
expect(result.exitCode, 0);
final file = File(outFile);
Expand All @@ -272,10 +281,13 @@ void defineCompileTests() {
[
'compile',
'exe',
'-v',
inFile,
],
);

// Executables should be (host) OS-specific by default.
expect(result.stdout, contains(usingTargetOSMessage));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
expect(File(outFile).existsSync(), true,
Expand Down Expand Up @@ -337,6 +349,7 @@ void defineCompileTests() {
[
'compile',
'exe',
'-v',
'--define',
'life=42',
'-o',
Expand All @@ -345,6 +358,7 @@ void defineCompileTests() {
],
);

expect(result.stdout, contains(usingTargetOSMessage));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
expect(File(outFile).existsSync(), true,
Expand All @@ -360,6 +374,33 @@ void defineCompileTests() {
expect(result.stdout, contains('42'));
}, skip: isRunningOnIA32);

test('Compile executable cannot compile cross-OS', () async {
final p = project(
mainSrc: 'void main() {print(const String.fromEnvironment("cross"));}');
final inFile = path.canonicalize(path.join(p.dirPath, p.relativeFilePath));
final outFile = path.canonicalize(path.join(p.dirPath, 'myexe'));
final targetOS = Platform.isLinux ? 'macos' : 'linux';

var result = await p.run(
[
'compile',
'exe',
'-v',
'--target-os',
targetOS,
'-o',
outFile,
inFile,
],
);

expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stderr, contains(crossOSNotAllowedError('exe')));
expect(result.stderr, contains(hostOSMessage));
expect(result.stderr, contains(targetOSMessage(targetOS)));
expect(result.exitCode, 128);
}, skip: isRunningOnIA32);

test('Compile and run aot snapshot', () async {
final p = project(mainSrc: 'void main() { print("I love AOT"); }');
final inFile = path.canonicalize(path.join(p.dirPath, p.relativeFilePath));
Expand All @@ -369,12 +410,15 @@ void defineCompileTests() {
[
'compile',
'aot-snapshot',
'-v',
'-o',
'main.aot',
inFile,
],
);

// AOT snapshots should not be OS-specific by default.
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
expect(File(outFile).existsSync(), true,
Expand All @@ -389,10 +433,79 @@ void defineCompileTests() {
expect(result.stdout, contains('I love AOT'));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
},
skip: isRunningOnIA32 ||
// Allow on MacOS after dart-lang/sdk#51707 is fixed.
Platform.isMacOS);
}, skip: isRunningOnIA32);

test('Compile aot snapshot can compile to host platform', () async {
final targetOS = Platform.operatingSystem;
final p = project(mainSrc: 'void main() { print("I love $targetOS"); }');
final inFile = path.canonicalize(path.join(p.dirPath, p.relativeFilePath));
final outFile = path.canonicalize(path.join(p.dirPath, 'main.aot'));

var result = await p.run(
[
'compile',
'aot-snapshot',
'-v',
'--target-os',
targetOS,
'-o',
'main.aot',
inFile,
],
);

expect(result.stdout, contains(usingTargetOSMessageForPlatform(targetOS)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
expect(File(outFile).existsSync(), true,
reason: 'File not found: $outFile');

final Directory binDir = File(Platform.resolvedExecutable).parent;
result = Process.runSync(
path.join(binDir.path, 'dartaotruntime'),
[outFile],
);

expect(result.stdout, contains('I love $targetOS'));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
}, skip: isRunningOnIA32);

test('Compile aot snapshot can compile cross platform', () async {
final targetOS = Platform.isLinux ? 'windows' : 'linux';
final p = project(mainSrc: 'void main() { print("I love $targetOS"); }');
final inFile = path.canonicalize(path.join(p.dirPath, p.relativeFilePath));
final outFile = path.canonicalize(path.join(p.dirPath, 'main.aot'));

var result = await p.run(
[
'compile',
'aot-snapshot',
'-v',
'--target-os',
targetOS,
'-o',
'main.aot',
inFile,
],
);

expect(result.stdout, contains(usingTargetOSMessageForPlatform(targetOS)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
expect(File(outFile).existsSync(), true,
reason: 'File not found: $outFile');

final Directory binDir = File(Platform.resolvedExecutable).parent;
result = Process.runSync(
path.join(binDir.path, 'dartaotruntime'),
[outFile],
);

expect(result.stdout, contains('I love $targetOS'));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
}, skip: isRunningOnIA32);

test('Compile and run kernel snapshot', () async {
final p = project(mainSrc: 'void main() { print("I love kernel"); }');
Expand All @@ -401,13 +514,15 @@ void defineCompileTests() {
[
'compile',
'kernel',
'-v',
'-o',
outFile,
p.relativeFilePath,
],
);
expect(File(outFile).existsSync(), true,
reason: 'File not found: $outFile');
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stderr, isNot(contains(soundNullSafetyMessage)));
expect(result.exitCode, 0);

Expand Down Expand Up @@ -437,6 +552,7 @@ void defineCompileTests() {
'-v',
inFile,
]);
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
final file = File(outFile);
Expand Down Expand Up @@ -601,6 +717,8 @@ void main() {}
],
);

// Only printed when -v/--verbose is used, not --verbosity.
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stdout, isNot(contains(soundNullSafetyMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
Expand Down Expand Up @@ -629,6 +747,8 @@ void main() {
],
);

// Only printed when -v/--verbose is used, not --verbosity.
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stdout, isNot(contains(soundNullSafetyMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
Expand Down Expand Up @@ -795,6 +915,8 @@ void main() {}
],
);

// Only printed when -v/--verbose is used, not --verbosity.
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stdout, isNot(contains(soundNullSafetyMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
Expand Down Expand Up @@ -823,6 +945,8 @@ void main() {
],
);

// Only printed when -v/--verbose is used, not --verbosity.
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stdout, isNot(contains(soundNullSafetyMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
Expand All @@ -849,6 +973,8 @@ void main() {
],
);

// Only printed when -v/--verbose is used, not --verbosity.
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stdout, isNot(contains(soundNullSafetyMessage)));
expect(result.stdout, contains('Warning: '));
expect(result.stderr, isEmpty);
Expand Down Expand Up @@ -893,12 +1019,14 @@ void main() {
[
'compile',
'kernel',
'-v',
'-o',
outFile,
inFile,
],
);

expect(result.stdout, isNot(contains(usingTargetOSMessage)));
expect(result.stderr, isNot(contains(soundNullSafetyMessage)));
expect(result.exitCode, 0);
expect(File(outFile).existsSync(), true,
Expand Down Expand Up @@ -1271,9 +1399,6 @@ void main() {
// Now perform the same basic compile and run test with the signed
// dartaotruntime.
await basicCompileTest();
},
skip: isRunningOnIA32 ||
// Allow on MacOS after dart-lang/sdk#51707 is fixed.
Platform.isMacOS);
}, skip: isRunningOnIA32);
}
}
Loading

0 comments on commit dcbda9a

Please sign in to comment.