From 7d877beb523bf6c3805cbac1f9a14712f2fa2da5 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Mon, 23 Feb 2026 04:11:57 +0100 Subject: [PATCH] cross compile --- implementations/Crafter.Build-Project.cpp | 112 +++++++++++++++------- 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/implementations/Crafter.Build-Project.cpp b/implementations/Crafter.Build-Project.cpp index cb8c666..efac395 100644 --- a/implementations/Crafter.Build-Project.cpp +++ b/implementations/Crafter.Build-Project.cpp @@ -287,15 +287,90 @@ namespace Crafter { command += std::format(" -I{}", dir); } - if(config.target != "wasm32-wasi") { + if(config.target == "wasm32-wasi") { + const std::string stdPcm = std::format("{}/wasi-sysroot-28.0/share/libc++/v1/std.pcm", exeDir.string()); + fs::path stdCc = fs::path(std::format("{}/wasi-sysroot-28.0/share/libc++/v1/std.cppm", exeDir.string())); + if(!fs::exists(stdPcm) || fs::last_write_time(stdPcm) < fs::last_write_time(stdCc)) { + std::string result = RunCommand(std::format("clang++ -fno-exceptions --target=wasm32-wasi -nodefaultlibs --sysroot={}/wasi-sysroot-28.0 -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier -fno-c++-static-destructors --precompile {} -o {}", exeDir.string(), stdCc.string(), stdPcm)); + if(result != "") { + throw std::runtime_error(result); + } + } + command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}/wasi-sysroot-28.0/share/libc++/v1", pcmDir.string(), exeDir.string()); + + + const fs::path indexPath = std::format("{}/index.html", exeDir.string()); + const fs::path indexDstPath = binDir/"index.html"; + if(!fs::exists(indexDstPath)) { + fs::copy(indexPath, indexDstPath); + } else if(fs::last_write_time(indexDstPath) < fs::last_write_time(indexPath)) { + fs::remove(indexDstPath); + fs::copy(indexPath, indexDstPath); + } + + const fs::path runtimePath = std::format("{}/runtime.js", exeDir.string()); + const fs::path runtimeDstPath = binDir/"runtime.js"; + if(!fs::exists(runtimeDstPath)) { + fs::copy(runtimePath, runtimeDstPath); + } else if(fs::last_write_time(runtimeDstPath) < fs::last_write_time(runtimePath)) { + fs::remove(runtimeDstPath); + fs::copy(runtimePath, runtimeDstPath); + } + } else if(config.target == "x86_64-w64-mingw32") { + fs::create_directories(exeDir/config.target); + const std::string stdPcm = std::format("{}/{}/std.pcm", exeDir.string(), config.target); + + std::vector folders; + + // Iterate through the directory and collect all subdirectories + for (const auto& entry : fs::directory_iterator("/usr/x86_64-w64-mingw32/include/c++")) { + if (entry.is_directory()) { + folders.push_back(entry.path().filename().string()); + } + } + + // Sort the folders by version in descending order + std::sort(folders.begin(), folders.end(), [](const std::string& a, const std::string& b) { + return std::lexicographical_compare(b.begin(), b.end(), a.begin(), a.end()); + }); + + std::string mingWversion = folders.front(); + + fs::path stdCc = fs::path(std::format("/usr/x86_64-w64-mingw32/include/c++/{}/bits/std.cc", mingWversion)); + + if(!fs::exists(stdPcm) || fs::last_write_time(stdPcm) < fs::last_write_time(stdCc)) { + std::string result = RunCommand(std::format("cp {} {}/std.cppm\nclang++ --target={} -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile {}/std.cppm -o {}", stdCc.string(), exeDir.string(), config.target, exeDir.string(), stdPcm)); + if(result != "") { + throw std::runtime_error(result); + } + } + command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}", pcmDir.string(), (exeDir/config.target).string()); + + try { + // Iterate over the source directory + for (const auto& entry : fs::directory_iterator("/usr/x86_64-w64-mingw32/bin/")) { + // Check if the file is a regular file and ends with ".dll" + if (fs::is_regular_file(entry) && entry.path().extension() == ".dll") { + // Construct the destination file path + fs::path dest_file = binDir / entry.path().filename(); + + // Copy the file + fs::copy(entry.path(), dest_file, fs::copy_options::overwrite_existing); + } + } + } catch (const fs::filesystem_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + } else { #ifndef PLATFORM_WINDOWS - const std::string stdPcm = std::format("{}/std.pcm", exeDir.string()); + fs::create_directories(exeDir/config.target); + const std::string stdPcm = std::format("{}/{}/std.pcm", exeDir.string(), config.target); std::string gccVersion = RunCommand("g++ -dumpversion"); gccVersion.pop_back(); fs::path stdCc = fs::path(std::format("/usr/include/c++/{}/bits/std.cc", gccVersion)); if(!fs::exists(stdPcm) || fs::last_write_time(stdPcm) < fs::last_write_time(stdCc)) { - std::string result = RunCommand(std::format("cp {} {}/std.cppm\nclang++ -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile {}/std.cppm -o {}", stdCc.string(), exeDir.string(), exeDir.string(), stdPcm)); + std::string result = RunCommand(std::format("cp {} {}/std.cppm\nclang++ --target={} -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile {}/std.cppm -o {}", config.target, stdCc.string(), exeDir.string(), config.target, exeDir.string(), stdPcm)); if(result != "") { throw std::runtime_error(result); } @@ -336,36 +411,7 @@ namespace Crafter { std::cerr << "Error copying file: " << e.what() << std::endl; } #endif - command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}", pcmDir.string(), exeDir.string()); - } else { - const std::string stdPcm = std::format("{}/wasi-sysroot-28.0/share/libc++/v1/std.pcm", exeDir.string()); - fs::path stdCc = fs::path(std::format("{}/wasi-sysroot-28.0/share/libc++/v1/std.cppm", exeDir.string())); - if(!fs::exists(stdPcm) || fs::last_write_time(stdPcm) < fs::last_write_time(stdCc)) { - std::string result = RunCommand(std::format("clang++ -fno-exceptions --target=wasm32-wasi -nodefaultlibs --sysroot={}/wasi-sysroot-28.0 -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier -fno-c++-static-destructors --precompile {} -o {}", exeDir.string(), stdCc.string(), stdPcm)); - if(result != "") { - throw std::runtime_error(result); - } - } - command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}/wasi-sysroot-28.0/share/libc++/v1", pcmDir.string(), exeDir.string()); - - - const fs::path indexPath = std::format("{}/index.html", exeDir.string()); - const fs::path indexDstPath = binDir/"index.html"; - if(!fs::exists(indexDstPath)) { - fs::copy(indexPath, indexDstPath); - } else if(fs::last_write_time(indexDstPath) < fs::last_write_time(indexPath)) { - fs::remove(indexDstPath); - fs::copy(indexPath, indexDstPath); - } - - const fs::path runtimePath = std::format("{}/runtime.js", exeDir.string()); - const fs::path runtimeDstPath = binDir/"runtime.js"; - if(!fs::exists(runtimeDstPath)) { - fs::copy(runtimePath, runtimeDstPath); - } else if(fs::last_write_time(runtimeDstPath) < fs::last_write_time(runtimePath)) { - fs::remove(runtimeDstPath); - fs::copy(runtimePath, runtimeDstPath); - } + command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}", pcmDir.string(), (exeDir/config.target).string()); }