Files
scriptc/native/llvm-codegen/src/main.cpp
Chris Tate c41f1a8be9 Expand LLVM native output targets (#280)
* Expand LLVM native output targets

- Add owned helper and runtime-pack contracts for macOS x64, Linux glibc/musl, Windows x64, and WASI.
- Generalize LLVM target selection, package validation, linker plans, and release/CI matrices.
- Document linker boundaries and add focused native-output and runtime-pack coverage.

* Pin Windows LLVM helper CI to VS 2022

The official LLVM archive needs the Visual Studio 2022 CMake generator, which is not guaranteed by windows-latest.

* Align bootstrap cache with LLVM runtime-pack builds

Use the same target-specific helper and linker identity before and after compiler loading so routed executable cache hits remain lightweight.

* Install Windows LLVM CI tools with Chocolatey

windows-2022 includes Visual Studio but not winget; use its available Chocolatey bootstrap for CMake and Ninja.

* Prevent Zig version probes from leaving runtime-pack objects

Run runtime-pack compiler version probes in a private temporary directory and remove the accidental tracked WASI a.o file.

* Use 7-Zip for Windows LLVM setup

Extract the official LLVM development archive with two-stage 7-Zip instead of Windows tar.exe, which timed out while materializing the toolchain tree.

* Define ssize_t for the MSVC runtime pack

Clang's MSVC target does not expose POSIX ssize_t through sys/types.h; define the pointer-sized runtime type without affecting MinGW.

* Build the Windows runtime pack with Zig

Use the MinGW-compatible Windows sysroot required by the runtime while retaining COFF helper output and installing Zig in the Windows native CI lane.

* Give macOS LLVM differential shard time to finish

Shard 1 passed its setup and focused contracts but was cancelled during its cold LLVM differential slice at the 20-minute job limit.
2026-09-01 13:41:34 -05:00

90 lines
3.0 KiB
C++

#include "diagnostics.h"
#include "emit.h"
#include "target.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Host.h"
#include <optional>
#include <cstdlib>
#include <string>
using namespace llvm;
#ifndef SCRIPTC_PACKAGE_VERSION
#define SCRIPTC_PACKAGE_VERSION "0.0.0-dev"
#endif
namespace {
int version(int Argc, char **Argv) {
if (Argc != 3 || StringRef(Argv[2]) != "--format=json")
return scriptc::reportError("usage", "version requires --format=json");
std::string Error;
std::unique_ptr<TargetMachine> Machine = scriptc::createTargetMachine(
scriptc::DefaultTarget, "2", Error);
if (!Machine)
return scriptc::reportError("target_machine_failed", Error);
json::Array Targets;
StringRef Backends = scriptc::TargetBackends;
while (!Backends.empty()) {
auto Split = Backends.split(',');
Targets.push_back(Split.first);
Backends = Split.second;
}
json::Array SupportedTargets;
StringRef TargetsText = scriptc::AllowedTargets;
while (!TargetsText.empty()) {
auto Split = TargetsText.split(',');
SupportedTargets.push_back(Split.first);
TargetsText = Split.second;
}
json::Object Response{
{"ok", true},
{"protocol_version", scriptc::ProtocolVersion},
{"scriptc_package_version", SCRIPTC_PACKAGE_VERSION},
{"llvm_version", LLVM_VERSION_STRING},
{"host_triple", sys::getDefaultTargetTriple()},
{"targets", std::move(Targets)},
{"supported_targets", std::move(SupportedTargets)},
{"default_target", scriptc::DefaultTarget},
{"data_layout", Machine->createDataLayout().getStringRepresentation()},
};
outs() << formatv("{0}\n", json::Value(std::move(Response)));
return 0;
}
} // namespace
int main(int Argc, char **Argv) {
scriptc::installFatalDiagnosticHandler();
// Process-isolated test seam for the fatal handler itself. It is inert in
// every ordinary invocation and ensures a future LLVM fatal never becomes
// an unstructured abort/stack trace in the Node caller.
if (std::getenv("SCRIPTC_LLVM_TEST_FATAL") != nullptr)
report_fatal_error("scriptc LLVM fatal diagnostic self-test");
if (Argc >= 2 && StringRef(Argv[1]) == "version")
return version(Argc, Argv);
if (Argc >= 2 && StringRef(Argv[1]) == "emit") {
std::optional<scriptc::EmitOptions> Options =
scriptc::parseEmitOptions(Argc, Argv);
if (!Options)
return scriptc::reportError(
"usage",
"emit requires --input <file> --output <file> and accepts "
"--filetype <obj|asm> --target <triple> --opt-level <0|1|2|3|s|z> "
"--relocation-model pic --diagnostic-format json "
"--source-path <path>");
return scriptc::emit(*Options);
}
return scriptc::reportError("usage",
"expected 'version --format=json' or 'emit'");
}