304 lines
12 KiB
C++
304 lines
12 KiB
C++
/*
|
|
Crafter.Build
|
|
Copyright (C) 2025 Catcrafts®
|
|
Catcrafts.net
|
|
|
|
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
|
|
version 3.0 of the License, or (at your option) any later version.
|
|
|
|
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
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
module;
|
|
#include <vector>
|
|
#include <string>
|
|
#include <print>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include "json.hpp"
|
|
#include <filesystem>
|
|
#include <unordered_set>
|
|
#include <thread>
|
|
#include <glslang/SPIRV/GlslangToSpv.h>
|
|
#include <regex>
|
|
#include <mutex>
|
|
#include <tuple>
|
|
module Crafter.Build;
|
|
using namespace Crafter::Build;
|
|
namespace fs = std::filesystem;
|
|
|
|
Project::Project(std::string name, fs::path path, std::vector<Configuration> configurations) : name(name), path(path), configurations(configurations) {
|
|
|
|
}
|
|
|
|
std::tuple<Configuration&, std::vector<ClangError>> Project::Build(std::string configuration) {
|
|
for(Configuration& config : configurations) {
|
|
if(config.name == configuration){
|
|
return {config, Build(config)};
|
|
}
|
|
}
|
|
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
|
}
|
|
|
|
std::tuple<Configuration&, std::vector<ClangError>> Project::Build(std::string configuration, fs::path outputDir) {
|
|
for(Configuration& config : configurations) {
|
|
if(config.name == configuration){
|
|
return {config, Build(config, outputDir)};
|
|
}
|
|
}
|
|
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
|
}
|
|
|
|
std::tuple<Configuration&, std::vector<ClangError>> Project::Build(std::string configuration, fs::path outputDir, fs::path binDir) {
|
|
for(Configuration& config : configurations) {
|
|
if(config.name == configuration){
|
|
return {config, Build(config, outputDir, binDir)};
|
|
}
|
|
}
|
|
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
|
}
|
|
|
|
std::vector<ClangError> Project::Build(Configuration& configuration) {
|
|
return Build(configuration, configuration.outputDir);
|
|
}
|
|
|
|
std::vector<ClangError> Project::Build(Configuration& config, fs::path outputDir) {
|
|
return Build(config, outputDir, outputDir);
|
|
}
|
|
|
|
std::vector<ClangError> Project::Build(Configuration& config, fs::path outputDir, fs::path binDir) {
|
|
if(config.standard.empty()) {
|
|
config.standard = "c++26";
|
|
}
|
|
if(config.march.empty()) {
|
|
config.march = "native";
|
|
}
|
|
if(config.type.empty()) {
|
|
config.type = "executable";
|
|
}
|
|
|
|
if (!fs::exists(config.buildDir)) {
|
|
fs::create_directory(config.buildDir);
|
|
}
|
|
if (!fs::exists(outputDir)) {
|
|
fs::create_directory(outputDir);
|
|
}
|
|
|
|
if (!fs::exists(config.buildDir)) {
|
|
fs::create_directory(config.buildDir);
|
|
}
|
|
std::string buildDir = config.buildDir/fs::path(config.name);
|
|
if (!fs::exists(buildDir)) {
|
|
fs::create_directory(buildDir);
|
|
}
|
|
|
|
std::string target;
|
|
if(!config.target.empty()){
|
|
target = std::format("-target {}", config.target);
|
|
}
|
|
fs::path pcmDir;
|
|
if(config.type == "library" || config.type == "shared-library"){
|
|
pcmDir = outputDir;
|
|
}else{
|
|
pcmDir = buildDir;
|
|
}
|
|
|
|
|
|
std::string libs = " -L/usr/local/lib";
|
|
|
|
if(config.target != "x86_64-w64-mingw64" && config.target != "x86_64-w64-mingw32") {
|
|
libs += " -L/usr/lib/";
|
|
}
|
|
|
|
for(const std::string& path : config.lib_paths) {
|
|
libs += std::format(" -L{}", path);
|
|
}
|
|
|
|
for(const std::string& path : config.includeDirs) {
|
|
libs += std::format(" -I{}", path);
|
|
}
|
|
|
|
for(Shader& shader : config.shaderFiles) {
|
|
shader.Compile(binDir);
|
|
}
|
|
|
|
std::vector<std::thread> depThreads = std::vector<std::thread>(config.dependencies.size());
|
|
|
|
if(config.dependencies.size() > 0){
|
|
libs += std::format(" -L{}", pcmDir.generic_string());
|
|
}
|
|
std::unordered_set<std::string> depLibSet;
|
|
for(std::int_fast32_t i = 0; i < depThreads.size(); i++) {
|
|
if(config.dependencies[i].path.ends_with(".git")) {
|
|
fs::path name = fs::path(config.dependencies[i].path).filename();
|
|
name.replace_extension();
|
|
if(!fs::exists(buildDir/name)) {
|
|
if(!config.dependencies[i].branch.empty()) {
|
|
system(std::format("cd {} && git clone {} && cd {} && git switch {}", buildDir, config.dependencies[i].path, (buildDir/name).generic_string(), config.dependencies[i].branch).c_str());
|
|
} else if(!config.dependencies[i].commit.empty()){
|
|
std::cout << std::format("cd {} && git clone {} && cd {} && git checkout {}", buildDir, config.dependencies[i].path, (buildDir/name).generic_string(), config.dependencies[i].commit).c_str() << std::endl;
|
|
system(std::format("cd {} && git clone {} && cd {} && git checkout {}", buildDir, config.dependencies[i].path, (buildDir/name).generic_string(), config.dependencies[i].commit).c_str());
|
|
} else {
|
|
system(std::format("cd {} && git clone {}", buildDir, config.dependencies[i].path).c_str());
|
|
}
|
|
} else if(config.dependencies[i].commit.empty()) {
|
|
system(std::format("cd {} && git pull", (buildDir/name).generic_string()).c_str());
|
|
}
|
|
config.dependencies[i].path = fs::path(config.dependencies[i].path).filename().replace_extension();
|
|
Project project = Project::LoadFromJSON(fs::path(buildDir)/config.dependencies[i].path/"project.json");
|
|
config.dependencies[i].name = project.name;
|
|
if (depLibSet.insert(project.name).second) {
|
|
libs+=std::format(" -l{}", project.name);
|
|
}
|
|
std::tuple<Configuration&, std::vector<ClangError>> depConfig = project.Build(config.dependencies[i].configuration, pcmDir, binDir);
|
|
if(std::get<1>(depConfig).size() > 0) {
|
|
return std::get<1>(depConfig);
|
|
}
|
|
for(const std::string& lib2 : std::get<0>(depConfig).libs) {
|
|
if (depLibSet.insert(lib2).second) {
|
|
libs+=std::format(" -l{}", lib2);
|
|
}
|
|
}
|
|
for(const Dependency& dep2 : std::get<0>(depConfig).dependencies) {
|
|
if (depLibSet.insert(dep2.name).second) {
|
|
libs+=std::format(" -l{}", dep2.name);
|
|
}
|
|
}
|
|
} else{
|
|
Project project = Project::LoadFromJSON(config.dependencies[i].path);
|
|
config.dependencies[i].name = project.name;
|
|
|
|
if (depLibSet.insert(project.name).second) {
|
|
libs+=std::format(" -l{}", project.name);
|
|
}
|
|
std::tuple<Configuration&, std::vector<ClangError>> depConfig = project.Build(config.dependencies[i].configuration, pcmDir, binDir);
|
|
if(std::get<1>(depConfig).size() > 0) {
|
|
return std::get<1>(depConfig);
|
|
}
|
|
for(const std::string& lib2 : std::get<0>(depConfig).libs) {
|
|
if (depLibSet.insert(lib2).second) {
|
|
libs+=std::format(" -l{}", lib2);
|
|
}
|
|
}
|
|
for(const Dependency& dep2 : std::get<0>(depConfig).dependencies) {
|
|
if (depLibSet.insert(dep2.name).second) {
|
|
libs+=std::format(" -l{}", dep2.name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string name = this->name;
|
|
|
|
std::string clangDir;
|
|
if(config.target == "wasm32-unknown-wasi" || config.target == "wasm64-unknown-wasi"){
|
|
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";
|
|
}
|
|
} else if(config.target == "wasm32" || config.target == "wasm64") {
|
|
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";
|
|
}
|
|
} else {
|
|
clangDir = "clang++";
|
|
}
|
|
|
|
std::string flags;
|
|
for(const std::string& flag : config.flags) {
|
|
flags+=flag;
|
|
}
|
|
|
|
if(config.debug) {
|
|
flags+=" -g";
|
|
}
|
|
|
|
for(const std::string& lib : config.libs) {
|
|
libs+= std::format(" -l{}",lib);
|
|
}
|
|
|
|
std::string march;
|
|
if(config.target != "wasm32-unknown-wasi"){
|
|
march = std::format("-march={}", config.march);
|
|
}
|
|
|
|
std::string files;
|
|
std::tuple<std::vector<Module>, std::vector<ClangError>> modules = Module::GetModules(clangDir, config, pcmDir, target, march, flags, files, buildDir);
|
|
|
|
if(std::get<1>(modules).size() > 0) {
|
|
return std::get<1>(modules);
|
|
}
|
|
|
|
std::vector<ClangError> errors = Source::GetSourceFiles(clangDir, config, pcmDir, target, march, flags, std::get<0>(modules), files, buildDir);
|
|
if(errors.size() > 0) {
|
|
return errors;
|
|
}
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
for(std::uint_fast32_t i = 0; i < config.c_files.size(); i++) {
|
|
files+=std::format("{}_source.o ",(buildDir/config.c_files[i].filename()).generic_string());
|
|
if(!fs::exists((buildDir/config.c_files[i].filename()).generic_string()+"_source.o") || fs::last_write_time(config.c_files[i].generic_string()+".c") > fs::last_write_time((buildDir/config.c_files[i].filename()).generic_string()+"_source.o")) {
|
|
threads.emplace_back([i, &config, pcmDir, target, clangDir, flags, march, &buildDir](){
|
|
RunCommand(std::format("clang {}.c -c -O{} {} {} -o {}_source.o {}", config.c_files[i].generic_string(), config.optimizationLevel, march, flags, (buildDir/config.c_files[i].filename()).generic_string(), target).c_str());
|
|
});
|
|
}
|
|
}
|
|
|
|
for(std::thread& thread : threads){
|
|
thread.join();
|
|
}
|
|
|
|
if(config.type == "executable"){
|
|
std::string command = std::format("{} {} {}-O{} -o {} {} {} -fuse-ld=lld", clangDir, flags, files, config.optimizationLevel, (outputDir/name).generic_string(), target, libs);
|
|
if(config.verbose) {
|
|
std::cout << command << std::endl;
|
|
}
|
|
std::vector<ClangError> errors = RunClang(command);
|
|
if(errors.size() > 0) {
|
|
return errors;
|
|
}
|
|
} else if(config.type == "library"){
|
|
RunCommandIgnore(std::format("ar r {}.a {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str());
|
|
} else if(config.type == "shared-library"){
|
|
RunCommandIgnore(std::format("ar r {}.so {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str());
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
Project Project::LoadFromJSON(fs::path path) {
|
|
if (!fs::exists(path)) {
|
|
throw std::runtime_error(std::format("Project file: {} not found.", path.generic_string()));
|
|
}
|
|
|
|
std::ifstream f(path);
|
|
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"];
|
|
|
|
const fs::path workingDir = path.remove_filename();
|
|
for (nlohmann::json::iterator it = configs.begin(); it != configs.end(); ++it) {
|
|
configurations.emplace_back(configs, (*it), workingDir);
|
|
}
|
|
return Project(name, workingDir, configurations);
|
|
}
|