Skip to content

Commit 7bea320

Browse files
authored
chore: Clean up cross-compilation support (#355)
1 parent f24fccd commit 7bea320

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

apps/cli/lib/src/compiler/api/entrypoint_compiler.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:ffi';
23
import 'dart:io';
34
import 'dart:isolate';
45
import 'dart:typed_data';
@@ -62,18 +63,20 @@ final class EntrypointCompiler {
6263
final bool verbose;
6364
final List<String> enabledExperiments;
6465

65-
Future<EntrypointResult> _crossCompile({
66+
Future<EntrypointResult> _compileExecutable({
6667
required String entrypointPath,
6768
}) async {
68-
logger.fine('Cross-compiling entrypoint: $entrypointPath');
69+
logger.fine('Compiling entrypoint to exe: $entrypointPath');
6970
final outputPath = p.join(p.dirname(entrypointPath), 'main.exe');
7071
final command = <String>[
7172
Sdk.current.dart,
7273
'compile',
7374
'exe',
74-
'--target-os=linux',
75-
'--target-arch=x64',
76-
'--experimental-cross-compilation',
75+
if (Abi.current() != Abi.linuxX64) ...[
76+
'--target-os=linux',
77+
'--target-arch=x64',
78+
'--experimental-cross-compilation',
79+
],
7780
'-o',
7881
outputPath,
7982
entrypointPath,
@@ -87,18 +90,18 @@ final class EntrypointCompiler {
8790
);
8891
final ProcessResult(:exitCode, :stdout as String, :stderr as String) =
8992
result;
90-
logger.fine('Cross-compilation finished with status $exitCode');
93+
logger.fine('Exe compilation finished with status $exitCode');
9194
if (exitCode != 0) {
9295
throw ProcessException(
9396
Sdk.current.dart,
9497
command.sublist(1),
95-
'Cross-compilation failed:\n$stdout\n$stderr',
98+
'Exe compilation failed:\n$stdout\n$stderr',
9699
exitCode,
97100
);
98101
}
99102

100103
final outputDill = await fileSystem.file(outputPath).readAsBytes();
101-
final outputDillDigest = await _computeMd5(outputDill.asUnmodifiableView());
104+
final outputDillDigest = await computeMd5(outputDill.asUnmodifiableView());
102105
return EntrypointResult(
103106
type: proto.ProjectAsset_Type.DART_EXECUTABLE,
104107
outputDillPath: outputPath,
@@ -120,8 +123,9 @@ final class EntrypointCompiler {
120123
}
121124

122125
if (resolvedProject.sdkConfig.targetSdk == SdkType.dart &&
123-
Sdk.current.supportsCrossCompilation) {
124-
return _crossCompile(
126+
(Abi.current() == Abi.linuxX64 ||
127+
Sdk.current.supportsCrossCompilation)) {
128+
return _compileExecutable(
125129
entrypointPath: entrypointPath,
126130
);
127131
}
@@ -187,7 +191,7 @@ final class EntrypointCompiler {
187191
logger.finer('Compilation succeeded');
188192

189193
final outputDill = await fileSystem.file(outputPath).readAsBytes();
190-
final outputDillDigest = await _computeMd5(outputDill.asUnmodifiableView());
194+
final outputDillDigest = await computeMd5(outputDill.asUnmodifiableView());
191195
return EntrypointResult(
192196
type: proto.ProjectAsset_Type.DART_KERNEL,
193197
outputDillPath: outputPath,
@@ -197,6 +201,6 @@ final class EntrypointCompiler {
197201
}
198202
}
199203

200-
Future<Digest> _computeMd5(Uint8List data) async {
204+
Future<Digest> computeMd5(Uint8List data) async {
201205
return Isolate.run(() => md5.convert(data));
202206
}

apps/cli/lib/src/frontend/celest_frontend.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import 'package:celest_cli/src/utils/process.dart';
3333
import 'package:celest_cli/src/utils/recase.dart';
3434
import 'package:celest_cli/src/utils/run.dart';
3535
import 'package:celest_cloud/src/proto.dart' as pb;
36-
import 'package:crypto/crypto.dart';
3736
import 'package:logging/logging.dart';
3837
import 'package:mason_logger/mason_logger.dart' show Progress;
3938
import 'package:stream_transform/stream_transform.dart';
@@ -1015,7 +1014,8 @@ final class CelestFrontend {
10151014
);
10161015
final tarStream = tarFile.openRead();
10171016
final bytes = await collectBytes(tarStream.transform(gzip.encoder));
1018-
return (bytes, md5.convert(bytes).toString());
1017+
final digest = await computeMd5(bytes.asUnmodifiableView());
1018+
return (bytes, digest.toString());
10191019
}
10201020

10211021
Future<(ast.ResolvedProject, Uri)> _deployProject({
@@ -1035,12 +1035,16 @@ final class CelestFrontend {
10351035
),
10361036
_ => (Uint8List(0), ''),
10371037
};
1038+
final gzippedOutput = gzip.encode(output.outputDill) as Uint8List;
1039+
final gzippedDigest = await computeMd5(
1040+
gzippedOutput.asUnmodifiableView(),
1041+
);
10381042
final assets = [
10391043
pb.ProjectAsset(
10401044
type: output.type,
1041-
filename: '${p.basenameWithoutExtension(output.outputDillPath)}.gz',
1042-
inline: gzip.encode(output.outputDill),
1043-
etag: output.outputDillDigest.toString(),
1045+
filename: '${p.basename(output.outputDillPath)}.gz',
1046+
inline: gzippedOutput,
1047+
etag: gzippedDigest.toString(),
10441048
),
10451049
if (resolvedProject.sdkConfig.targetSdk == ast.SdkType.flutter)
10461050
pb.ProjectAsset(

services/celest_cloud_hub/lib/src/deploy/fly/fly_deployment_engine.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ final class FlyDeploymentEngine {
264264
// Deploy using `flyctl deploy`
265265
await _withTempDirectory((dir) async {
266266
if (kernelAsset.etag case final etag?) {
267-
if (etag != md5.convert(kernelAsset.inline).toString()) {
267+
final computed = md5.convert(kernelAsset.inline).toString();
268+
if (etag != computed) {
268269
throw GrpcError.failedPrecondition('Kernel asset has been modified');
269270
}
270271
}
@@ -279,12 +280,14 @@ final class FlyDeploymentEngine {
279280
kernelAssetFilename.length - '.gz'.length,
280281
);
281282
}
283+
_logger.fine('Writing kernel asset to disk: $kernelAssetFilename');
282284
final kernelFile = dir.childFile(kernelAssetFilename);
283285
await kernelFile.writeAsBytes(kernelAssetData);
284286

285287
if (flutterAssetsBundle case final flutterAssetsBundle?) {
286288
if (flutterAssetsBundle.etag case final etag?) {
287-
if (etag != md5.convert(flutterAssetsBundle.inline).toString()) {
289+
final computed = md5.convert(flutterAssetsBundle.inline).toString();
290+
if (etag != computed) {
288291
throw GrpcError.failedPrecondition(
289292
'Flutter assets bundle has been modified',
290293
);

services/celest_cloud_hub/lib/src/services/project_environments_service.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,9 @@ final class ProjectEnvironmentsService extends ProjectEnvironmentsServiceBase
449449
);
450450

451451
final assets = {for (final asset in request.assets) asset.type: asset};
452-
final kernelAsset = assets[pb.ProjectAsset_Type.DART_KERNEL];
452+
final kernelAsset =
453+
assets[pb.ProjectAsset_Type.DART_KERNEL] ??
454+
assets[pb.ProjectAsset_Type.DART_EXECUTABLE];
453455
if (kernelAsset == null) {
454456
throw GrpcError.invalidArgument('Missing Dart kernel asset');
455457
}

0 commit comments

Comments
 (0)