navid/haze / commit
454ae72a0f87
Navid M <navidmomtahen@gmail.com> · 2026-12-02
parents: d6acf09bc499
Browse files at this commit · Download source
--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -73,6 +73,17 @@ else() target_compile_options(hazert PRIVATE -fno-exceptions -fno-rtti) endif()++set(EMBEDDED_RUNTIME "${CMAKE_CURRENT_BINARY_DIR}/embedded_runtime.cpp")+add_custom_command(+ OUTPUT "${EMBEDDED_RUNTIME}"+ COMMAND "${CMAKE_COMMAND}"+ "-DINPUT=$<TARGET_FILE:hazert>"+ "-DOUTPUT=${EMBEDDED_RUNTIME}"+ -P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/embed_runtime.cmake"+ DEPENDS hazert "${CMAKE_CURRENT_SOURCE_DIR}/cmake/embed_runtime.cmake"+ COMMENT "Embedding libhazert.a into hz"+ VERBATIM) add_executable(hz src/codegen.cpp@@ -81,6 +92,7 @@ src/parser.cpp src/sema.cpp src/types.cpp+ ${EMBEDDED_RUNTIME} ${UQBE_SOURCES}) target_include_directories(hz PRIVATE src@@ -92,3 +104,6 @@ add_executable(hazert_test EXCLUDE_FROM_ALL tests/runtime_test.cpp) target_link_libraries(hazert_test PRIVATE hazert)++include(GNUInstallDirs)+install(TARGETS hz RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")--- /dev/null+++ b/cmake/embed_runtime.cmake@@ -0,0 +1,17 @@+# Generate a C++ source file containing the runtime archive as a byte array.+# Invoked from CMakeLists.txt as a build-time script.++if(NOT DEFINED INPUT OR NOT DEFINED OUTPUT)+ message(FATAL_ERROR "embed_runtime.cmake requires INPUT and OUTPUT")+endif()++file(SIZE "${INPUT}" archive_size)+file(READ "${INPUT}" archive_hex HEX)+string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," archive_bytes "${archive_hex}")+string(REGEX REPLACE "((0x[0-9a-f][0-9a-f],){16})" "\\1\n" archive_bytes "${archive_bytes}")++file(WRITE "${OUTPUT}" "extern \"C\" const unsigned char haze_embedded_runtime_archive[] = {\n")+file(APPEND "${OUTPUT}" "${archive_bytes}\n")+file(APPEND "${OUTPUT}" "};\n")+file(APPEND "${OUTPUT}"+ "extern \"C\" const unsigned long long haze_embedded_runtime_archive_size = ${archive_size}ull;\n")--- a/src/driver.cpp+++ b/src/driver.cpp@@ -18,6 +18,9 @@ #include <sys/wait.h> #include <unistd.h> #endif++extern "C" const unsigned char haze_embedded_runtime_archive[];+extern "C" const unsigned long long haze_embedded_runtime_archive_size; namespace fs = std::filesystem; @@ -98,6 +101,16 @@ if (!out) throw std::runtime_error("cannot write '" + p.string() + "'"); out << s;+}++void write_embedded_runtime(const fs::path& p) {+ std::ofstream out(p, std::ios::binary);+ if (!out)+ throw std::runtime_error("cannot write '" + p.string() + "'");+ out.write(reinterpret_cast<const char*>(haze_embedded_runtime_archive),+ static_cast<std::streamsize>(haze_embedded_runtime_archive_size));+ if (!out)+ throw std::runtime_error("cannot write '" + p.string() + "'"); } std::string shell_quote(const std::string& s) {@@ -205,7 +218,7 @@ return p.parent_path(); } -std::string find_runtime(const Options& o, const fs::path& exe_dir) {+std::string find_runtime(const Options& o, const fs::path& exe_dir, const fs::path& tmp) { if (!o.runtime.empty()) { if (!is_executable_file(o.runtime)) throw std::runtime_error("runtime archive not found: " + o.runtime);@@ -226,7 +239,10 @@ return fs::weakly_canonical(p, ec).string(); } }- throw std::runtime_error("cannot find libhazert.a; build it with CMake or pass -runtime path");++ fs::path embedded = tmp / "libhazert.a";+ write_embedded_runtime(embedded);+ return embedded.string(); } std::string find_cc(const Options& o) {@@ -455,7 +471,7 @@ std::string cc = find_cc(opt); fs::path exe_dir = executable_dir(argv[0]);- std::string runtime = find_runtime(opt, exe_dir);+ std::string runtime = find_runtime(opt, exe_dir, tmp); if (opt.emit_obj) { std::string out = opt.output.empty() ? default_output(opt.input, ".o") : opt.output;