2024-12-29 01:04:33 +01:00
|
|
|
/*
|
|
|
|
|
Crafter.Build
|
2024-12-30 17:21:04 +01:00
|
|
|
Copyright (C) 2024 Catcrafts
|
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
|
|
|
|
|
version 2.1 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
|
|
|
|
|
*/
|
|
|
|
|
|
2024-12-28 21:00:12 +01:00
|
|
|
module;
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "json.hpp"
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#include <thread>
|
2024-12-29 17:53:46 +01:00
|
|
|
#include <stdlib.h>
|
2024-12-28 21:00:12 +01:00
|
|
|
module Crafter.Build:ProjectImpl;
|
|
|
|
|
import :Project;
|
|
|
|
|
import :Configuration;
|
2024-12-29 17:53:46 +01:00
|
|
|
import :Dependency;
|
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){
|
2024-12-29 00:51:02 +01:00
|
|
|
Build(config, config.outputDir);
|
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.");
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
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
|
|
|
} else {
|
2024-12-29 17:53:46 +01:00
|
|
|
for (const auto& entry : fs::directory_iterator(config.buildDir)){
|
|
|
|
|
fs::remove_all(entry.path());
|
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
|
|
|
} else {
|
2024-12-29 17:53:46 +01:00
|
|
|
for (const auto& entry : fs::directory_iterator(outputDir)){
|
|
|
|
|
fs:remove_all(entry.path());
|
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++) {
|
|
|
|
|
Project project = Project::LoadFromJSON(config.dependencies[i].path);
|
|
|
|
|
libs+=std::format(" -l{}", project.name);
|
|
|
|
|
depThreads[i] = std::thread([i, pcmDir, config, project]() {
|
|
|
|
|
project.Build(config.dependencies[i].configuration, pcmDir);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"){
|
2024-12-29 17:53:46 +01:00
|
|
|
clangDir = "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -Wno-unused-command-line-argument";
|
|
|
|
|
name+=".wasm";
|
|
|
|
|
} else if(config.target == "wasm32" || config.target == "wasm64") {
|
2024-12-29 03:30:22 +01:00
|
|
|
clangDir = "${WASI_SDK_PATH}/bin/clang++ --no-standard-libraries -Wl,--no-entry -Wl,--export-all -Wno-unused-command-line-argument";
|
2024-12-29 17:53:46 +01:00
|
|
|
name+=".wasm";
|
|
|
|
|
} else {
|
|
|
|
|
clangDir = "clang++ -Wno-unused-command-line-argument";
|
2024-12-29 03:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-29 20:14:49 +01:00
|
|
|
for(std::thread& thread : depThreads){
|
|
|
|
|
thread.join();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 17:53:46 +01:00
|
|
|
//clangDir+= std::format(" -I {} ", pcmDir);
|
|
|
|
|
|
|
|
|
|
for(const fs::path& moduleFile : config.moduleFiles){
|
|
|
|
|
system(std::format("{} -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}.pcm {}", clangDir, config.standard, moduleFile.generic_string(), pcmDir.generic_string(), (pcmDir/moduleFile.filename()).generic_string(), target).c_str());
|
2024-12-29 00:51:02 +01:00
|
|
|
}
|
|
|
|
|
std::vector<std::thread> threads = std::vector<std::thread>(config.moduleFiles.size() + config.sourceFiles.size());
|
|
|
|
|
std::string files;
|
|
|
|
|
for(std::int_fast32_t i = 0; i < config.moduleFiles.size(); i++) {
|
2024-12-29 17:53:46 +01:00
|
|
|
files+=std::format("{}.o ",(config.buildDir/config.moduleFiles[i].filename()).generic_string());
|
2024-12-29 03:13:14 +01:00
|
|
|
threads[i] = std::thread([i, config, pcmDir, target, clangDir](){
|
2024-12-29 17:53:46 +01:00
|
|
|
system(std::format("{} -std={} {}.pcm -fprebuilt-module-path={} -c -O{} -o {}.o {}", clangDir, config.standard, (pcmDir/config.moduleFiles[i].filename()).generic_string(), pcmDir.generic_string(), config.optimizationLevel, (config.buildDir/config.moduleFiles[i].filename()).generic_string(), target).c_str());
|
2024-12-29 00:51:02 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) {
|
2024-12-29 17:53:46 +01:00
|
|
|
files+=std::format("{}_source.o ",(config.buildDir/config.sourceFiles[i].filename()).generic_string());
|
2024-12-29 03:13:14 +01:00
|
|
|
threads[config.moduleFiles.size()+i] = std::thread([i, config, pcmDir, target, clangDir](){
|
2024-12-29 17:53:46 +01:00
|
|
|
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, (config.buildDir/config.sourceFiles[i].filename()).generic_string(), target).c_str());
|
2024-12-29 00:51:02 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
for(std::thread& thread : threads){
|
|
|
|
|
thread.join();
|
|
|
|
|
}
|
2024-12-29 01:38:25 +01:00
|
|
|
if(config.type == "executable"){
|
2024-12-29 20:14:49 +01:00
|
|
|
system(std::format("{} {}-O{} -o {} {} {}", 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"){
|
2024-12-29 20:14:49 +01:00
|
|
|
system(std::format("ar r {}.so {} -shared", (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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|