Skip to content

Commit 182c37f

Browse files
committed
refactor(std): relax compiler detection to allow shebang scripts
1 parent 9a879f5 commit 182c37f

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

packages/std/sdk.tg.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -803,32 +803,39 @@ export namespace sdk {
803803
);
804804
}
805805

806-
if (foundCC instanceof tg.File) {
807-
// Inspect the file to see which system it should run on.
808-
const metadata = await std.file.executableMetadata(foundCC);
809-
if (metadata.format !== "elf" && metadata.format !== "mach-o") {
810-
throw new Error(`Unexpected compiler format ${metadata.format}.`);
811-
}
812-
let detectedArch: string | undefined;
813-
if (metadata.format === "elf") {
814-
detectedArch = metadata.arch;
815-
} else if (metadata.format === "mach-o") {
816-
detectedArch = metadata.arches[0] ?? "aarch64";
817-
}
806+
// If the file is a symlink, resolve it.
807+
if (foundCC instanceof tg.Symlink) {
808+
foundCC = await foundCC.resolve();
809+
}
810+
tg.assert(foundCC instanceof tg.File);
811+
812+
// Inspect the file to see which system it should run on.
813+
const metadata = await std.file.executableMetadata(foundCC);
814+
let detectedArch: string | undefined;
815+
if (metadata.format === "elf") {
816+
detectedArch = metadata.arch;
817+
} else if (metadata.format === "mach-o") {
818+
detectedArch = metadata.arches[0] ?? "aarch64";
819+
}
820+
if (metadata.format !== "shebang") {
818821
const os = metadata.format === "elf" ? "linux" : "darwin";
819822
const arch = detectedArch ?? "x86_64";
820823
detectedHost = `${arch}-${os}`;
821824
}
822825

823-
// Actually run the compiler on the detected system to ask what host triple it's configured for.
824-
const output = await std.build`${cmd} -dumpmachine > $OUTPUT`
825-
.bootstrap(true)
826-
.env(env_)
827-
.host(std.triple.archAndOs(detectedHost))
828-
.then(tg.File.expect);
829-
const host = (await output.text()).trim();
830-
std.triple.assert(host);
831-
return host;
826+
// For cross compilers, use the host detected. For host compilers, run the compiler on the detected system to ask what host triple it's configured for.
827+
if (isCross) {
828+
return detectedHost;
829+
} else {
830+
const output = await std.build`${cmd} -dumpmachine > $OUTPUT`
831+
.bootstrap(true)
832+
.env(env_)
833+
.host(std.triple.archAndOs(detectedHost))
834+
.then(tg.File.expect);
835+
const host = (await output.text()).trim();
836+
std.triple.assert(host);
837+
return host;
838+
}
832839
};
833840

834841
/** Retreive the full range of targets an SDK supports. */

0 commit comments

Comments
 (0)