From bfe2e75d7e3b372e55369677028429990c49bc54 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 20 Oct 2026 11:44:48 +0000 Subject: feat(driver): embed and automatically link the standard library Add -no-std to disable default library loading and linking. Co-Authored-By: Claude Opus 5.5 --- tools/driver/StandardLibrary.cc | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tools/driver/StandardLibrary.cc (limited to 'tools/driver/StandardLibrary.cc') diff --git a/tools/driver/StandardLibrary.cc b/tools/driver/StandardLibrary.cc new file mode 100644 index 0000000..ac88745 --- /dev/null +++ b/tools/driver/StandardLibrary.cc @@ -0,0 +1,43 @@ +#include "StandardLibrary.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" + +namespace { +struct LibraryFile { + const char *Name; + const char *Data; + size_t Size; +}; +#include "StandardLibrary.inc" +} + +StandardLibrary::~StandardLibrary() { + if (!Directory.empty()) llvm::sys::fs::remove_directories(Directory); +} + +bool StandardLibrary::load(std::vector &Inputs) { + auto Error = llvm::sys::fs::createUniqueDirectory("cpsc-std", Directory); + if (Error) { + llvm::errs() << "cpsc: fatal error: cannot extract standard library: " << Error.message() << "\n"; + return false; + } + for (const auto &File : LibraryFiles) { + llvm::SmallString<256> Path(Directory); + llvm::sys::path::append(Path, File.Name); + llvm::raw_fd_ostream Output(Path, Error); + if (!Error) { + Output.write(File.Data, File.Size); + Output.close(); + Error = Output.error(); + } + if (Error) { + llvm::errs() << "cpsc: fatal error: cannot extract standard library file '" + << File.Name << "': " << Error.message() << "\n"; + Output.clear_error(); + return false; + } + if (llvm::sys::path::extension(Path) == ".cps") Inputs.emplace_back(Path); + } + return true; +} -- cgit v1.3.1-4-g156e