2024-12-29 01:04:33 +01:00
|
|
|
/*
|
|
|
|
|
Crafter.Build
|
2024-12-30 17:21:04 +01:00
|
|
|
Copyright (C) 2024 Catcrafts
|
2024-12-31 20:32:00 +01:00
|
|
|
Catcrafts.net
|
2024-12-29 01:04:33 +01:00
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
2024-12-31 20:32:00 +01:00
|
|
|
version 3.0 of the License, or (at your option) any later version.
|
2024-12-29 01:04:33 +01:00
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with this library; if not, write to the Free Software
|
2024-12-31 20:32:00 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2024-12-29 01:04:33 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-12-28 21:00:12 +01:00
|
|
|
module;
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
2024-12-30 23:43:28 +01:00
|
|
|
#include <print>
|
2024-12-28 21:00:12 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "json.hpp"
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#include <thread>
|
2024-12-30 20:06:48 +01:00
|
|
|
module Crafter.Build;
|
2024-12-28 21:00:12 +01:00
|
|
|
using namespace Crafter::Build;
|
2024-12-29 17:53:46 +01:00
|
|
|
namespace fs = std::filesystem;
|
2024-12-28 21:00:12 +01:00
|
|
|
|
2024-12-29 17:53:46 +01:00
|
|
|
Project::Project(std::string name, fs::path path, std::vector<Configuration> configurations) : name(name), path(path), configurations(configurations) {
|
2024-12-28 21:00:12 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
void Project::Build(std::string configuration) const {
|
2024-12-28 21:00:12 +01:00
|
|
|
for(const Configuration& config : configurations) {
|
|
|
|
|
if(config.name == configuration){
|
2025-01-02 02:53:39 +01:00
|
|
|
Build(config);
|
2024-12-28 21:26:19 +01:00
|
|
|
return;
|
2024-12-28 21:00:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-28 21:26:19 +01:00
|
|
|
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
2024-12-28 21:00:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
void Project::Build(std::string configuration, fs::path outputDir) const {
|
2024-12-29 00:51:02 +01:00
|
|
|
for(const Configuration& config : configurations) {
|
|
|
|
|
if(config.name == configuration){
|
|
|
|
|
Build(config, outputDir);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-02 02:53:39 +01:00
|
|
|
void Project::Build(std::string configuration, fs::path outputDir, fs::path binDir) const {
|
|
|
|
|
for(const Configuration& config : configurations) {
|
|
|
|
|
if(config.name == configuration){
|
|
|
|
|
Build(config, outputDir, binDir);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
void Project::Build(Configuration configuration) const {
|
2024-12-29 00:51:02 +01:00
|
|
|
Build(configuration, configuration.outputDir);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
void Project::Build(Configuration config, fs::path outputDir) const {
|
2025-01-02 02:53:39 +01:00
|
|
|
Build(config, outputDir, outputDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) const {
|
2025-02-03 22:06:54 +01:00
|
|
|
if(config.standard.empty()) {
|
|
|
|
|
config.standard = "c++26";
|
|
|
|
|
}
|
|
|
|
|
if(config.march.empty()) {
|
|
|
|
|
config.march = "native";
|
|
|
|
|
}
|
|
|
|
|
if(config.type.empty()) {
|
|
|
|
|
config.type = "executable";
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 17:53:46 +01:00
|
|
|
if (!fs::exists(config.buildDir)) {
|
|
|
|
|
fs::create_directory(config.buildDir);
|
2024-12-29 00:51:02 +01:00
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
if (!fs::exists(outputDir)) {
|
|
|
|
|
fs::create_directory(outputDir);
|
2024-12-29 00:51:02 +01:00
|
|
|
}
|
2024-12-29 01:38:25 +01:00
|
|
|
|
|
|
|
|
std::string target;
|
|
|
|
|
if(!config.target.empty()){
|
|
|
|
|
target = std::format("-target {}", config.target);
|
|
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
fs::path pcmDir;
|
2024-12-29 01:38:25 +01:00
|
|
|
if(config.type == "library" || config.type == "shared-library"){
|
|
|
|
|
pcmDir = outputDir;
|
|
|
|
|
}else{
|
|
|
|
|
pcmDir = config.buildDir;
|
|
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
std::string libs;
|
|
|
|
|
|
|
|
|
|
std::vector<std::thread> depThreads = std::vector<std::thread>(config.dependencies.size());
|
|
|
|
|
|
|
|
|
|
if(config.dependencies.size() > 0){
|
|
|
|
|
libs += std::format(" -L{}", pcmDir.generic_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(std::int_fast32_t i = 0; i < depThreads.size(); i++) {
|
2025-02-03 22:06:54 +01:00
|
|
|
if(config.dependencies[i].path.ends_with(".git")) {
|
2025-03-29 22:36:43 +01:00
|
|
|
std::string branch;
|
|
|
|
|
if(!config.dependencies[i].branch.empty()) {
|
|
|
|
|
fs::path name = fs::path(config.dependencies[i].path).filename();
|
|
|
|
|
name.replace_extension();
|
|
|
|
|
branch = std::format(" && cd {} && git switch {}", name.generic_string(), config.dependencies[i].branch);
|
|
|
|
|
} else if(!config.dependencies[i].commit.empty()){
|
|
|
|
|
fs::path name = fs::path(config.dependencies[i].path).filename();
|
|
|
|
|
name.replace_extension();
|
|
|
|
|
branch = std::format(" && cd {} && git checkout {}", name.generic_string(), config.dependencies[i].commit);
|
|
|
|
|
}
|
|
|
|
|
system(std::format("cd {} && git clone {}{}", config.buildDir, config.dependencies[i].path, branch).c_str());
|
2025-02-12 21:35:25 +01:00
|
|
|
config.dependencies[i].path = fs::path(config.dependencies[i].path).filename().replace_extension();
|
|
|
|
|
Project project = Project::LoadFromJSON(fs::path(config.buildDir)/config.dependencies[i].path/"project.json");
|
|
|
|
|
libs+=std::format(" -l{}", project.name);
|
|
|
|
|
depThreads[i] = std::thread([i, pcmDir, config, project, binDir]() {
|
|
|
|
|
project.Build(config.dependencies[i].configuration, pcmDir, binDir);
|
|
|
|
|
});
|
|
|
|
|
} else{
|
|
|
|
|
Project project = Project::LoadFromJSON(config.dependencies[i].path);
|
|
|
|
|
libs+=std::format(" -l{}", project.name);
|
|
|
|
|
depThreads[i] = std::thread([i, pcmDir, config, project, binDir]() {
|
|
|
|
|
project.Build(config.dependencies[i].configuration, pcmDir, binDir);
|
|
|
|
|
});
|
2025-02-03 22:06:54 +01:00
|
|
|
}
|
2025-02-12 21:35:25 +01:00
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string name = this->name;
|
|
|
|
|
|
2024-12-29 03:13:14 +01:00
|
|
|
std::string clangDir;
|
2024-12-29 03:30:22 +01:00
|
|
|
if(config.target == "wasm32-unknown-wasi" || config.target == "wasm64-unknown-wasi"){
|
2025-01-02 02:53:39 +01:00
|
|
|
clangDir = "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -Wno-unused-command-line-argument -Wl,--export-all -fno-exceptions";
|
|
|
|
|
if(config.type != "library") {
|
|
|
|
|
name+=".wasm";
|
|
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
} else if(config.target == "wasm32" || config.target == "wasm64") {
|
2025-01-02 02:53:39 +01:00
|
|
|
clangDir = "${WASI_SDK_PATH}/bin/clang++ --no-standard-libraries -Wl,--no-entry -Wl,--export-all -Wno-unused-command-line-argument -fno-exceptions";
|
|
|
|
|
if(config.type != "library") {
|
|
|
|
|
name+=".wasm";
|
|
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
} else {
|
|
|
|
|
clangDir = "clang++ -Wno-unused-command-line-argument";
|
2024-12-29 03:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 22:06:54 +01:00
|
|
|
std::string flags;
|
|
|
|
|
for(const std::string& flag : config.flags) {
|
|
|
|
|
flags+=flag;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
for(std::thread& thread : depThreads){
|
|
|
|
|
thread.join();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-30 23:43:28 +01:00
|
|
|
std::string files;
|
2025-02-03 22:06:54 +01:00
|
|
|
//std::vector<std::thread> moduleThreads(config.moduleFiles.size());
|
2024-12-31 20:32:00 +01:00
|
|
|
for(std::uint_fast32_t i = 0; i < config.moduleFiles.size(); i++) {
|
2025-02-03 22:06:54 +01:00
|
|
|
//moduleThreads[i] = std::thread([i, &config, pcmDir, target, clangDir](){
|
2024-12-31 20:32:00 +01:00
|
|
|
ModuleFile::CompileModuleFile(config.moduleFiles[i], clangDir, config, pcmDir, target);
|
2025-02-03 22:06:54 +01:00
|
|
|
//});
|
2024-12-31 20:32:00 +01:00
|
|
|
files+=std::format("{}.o ",(config.buildDir/config.moduleFiles[i].filename()).generic_string());
|
|
|
|
|
}
|
2025-02-03 22:06:54 +01:00
|
|
|
// for(std::thread& thread : moduleThreads){
|
|
|
|
|
// thread.join();
|
|
|
|
|
// }
|
2024-12-31 20:32:00 +01:00
|
|
|
|
2025-02-12 21:35:25 +01:00
|
|
|
std::string march;
|
|
|
|
|
if(config.target != "wasm32-unknown-wasi"){
|
|
|
|
|
march = std::format("-march={}", config.march);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-31 20:32:00 +01:00
|
|
|
std::vector<std::thread> threads;
|
|
|
|
|
for(std::uint_fast32_t i = 0; i < config.sourceFiles.size(); i++) {
|
|
|
|
|
files+=std::format("{}_source.o ",(config.buildDir/config.sourceFiles[i].filename()).generic_string());
|
2025-02-03 22:06:54 +01:00
|
|
|
//if(!fs::exists((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o") || fs::last_write_time(config.sourceFiles[i].generic_string()+".cpp") > fs::last_write_time((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o")) {
|
2025-02-12 21:35:25 +01:00
|
|
|
threads.emplace_back([i, &config, pcmDir, target, clangDir, flags, march](){
|
|
|
|
|
system(std::format("{} -std={} {}.cpp -fprebuilt-module-path={} -c -O{} {} {} -o {}_source.o {}", clangDir, config.standard, config.sourceFiles[i].generic_string(), pcmDir.generic_string(), config.optimizationLevel, march, flags, (config.buildDir/config.sourceFiles[i].filename()).generic_string(), target).c_str());
|
2024-12-30 23:43:28 +01:00
|
|
|
});
|
2025-02-03 22:06:54 +01:00
|
|
|
//}
|
2024-12-29 00:51:02 +01:00
|
|
|
}
|
2024-12-31 20:32:00 +01:00
|
|
|
|
2024-12-29 00:51:02 +01:00
|
|
|
for(std::thread& thread : threads){
|
|
|
|
|
thread.join();
|
|
|
|
|
}
|
2024-12-31 20:32:00 +01:00
|
|
|
|
2024-12-29 01:38:25 +01:00
|
|
|
if(config.type == "executable"){
|
2025-02-03 22:06:54 +01:00
|
|
|
system(std::format("{} {}-O{} -o {} {} {} -fuse-ld=lld", clangDir, files, config.optimizationLevel, (outputDir/name).generic_string(), target, libs).c_str());
|
2024-12-29 01:38:25 +01:00
|
|
|
} else if(config.type == "library"){
|
2024-12-29 20:14:49 +01:00
|
|
|
system(std::format("ar r {}.a {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str());
|
2024-12-29 01:38:25 +01:00
|
|
|
} else if(config.type == "shared-library"){
|
2025-02-03 22:06:54 +01:00
|
|
|
system(std::format("ar r {}.so {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str());
|
2024-12-29 01:38:25 +01:00
|
|
|
}
|
2024-12-29 00:51:02 +01:00
|
|
|
|
2025-01-02 02:53:39 +01:00
|
|
|
for(const fs::path& additionalFile : config.additionalFiles){
|
|
|
|
|
if(!fs::exists(binDir/additionalFile.filename())) {
|
|
|
|
|
fs::copy(additionalFile, binDir);
|
|
|
|
|
} else if (fs::last_write_time(additionalFile) > fs::last_write_time(binDir/additionalFile.filename())){
|
|
|
|
|
fs::remove(binDir/additionalFile.filename());
|
|
|
|
|
fs::copy(additionalFile, binDir);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-29 00:51:02 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-29 17:53:46 +01:00
|
|
|
Project Project::LoadFromJSON(fs::path path) {
|
|
|
|
|
if (!fs::exists(path)) {
|
|
|
|
|
throw std::runtime_error(std::format("Project file: {} not found.", path.generic_string()));
|
2024-12-28 21:26:19 +01:00
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
|
|
|
|
|
std::ifstream f(path);
|
2024-12-28 21:00:12 +01:00
|
|
|
nlohmann::json data = nlohmann::json::parse(f);
|
|
|
|
|
const std::string name = data["name"].get<std::string>();
|
|
|
|
|
std::vector<Configuration> configurations;
|
|
|
|
|
nlohmann::json configs = data["configurations"];
|
2024-12-29 17:53:46 +01:00
|
|
|
|
|
|
|
|
const fs::path workingDir = path.remove_filename();
|
2024-12-28 21:00:12 +01:00
|
|
|
for (nlohmann::json::iterator it = configs.begin(); it != configs.end(); ++it) {
|
2024-12-29 17:53:46 +01:00
|
|
|
configurations.emplace_back(configs, (*it), workingDir);
|
2024-12-28 21:00:12 +01:00
|
|
|
}
|
2024-12-29 17:53:46 +01:00
|
|
|
return Project(name, workingDir, configurations);
|
2024-12-28 21:00:12 +01:00
|
|
|
}
|