This commit is contained in:
Jorijn van der Graaf 2025-09-09 18:10:54 +02:00
commit a36beab2ac
6 changed files with 71 additions and 10 deletions

60
Crafter.Build-Bounce.cppm Normal file
View file

@ -0,0 +1,60 @@
/*
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 <iostream>
#include <memory>
#include <stdexcept>
#include <array>
#include <string>
#include <cstdio>
export module Crafter.Build:Bounce;
export void BounceCommand(const std::string& cmd) {
// std::array<char, 128> buffer;
// std::string result;
// std::string with = cmd + " 2>&1";
// // Open pipe to file
// FILE* pipe = popen(with.c_str(), "r");
// if (!pipe) throw std::runtime_error("popen() failed!");
// // Read till end of process:
// while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
// result += buffer.data();
// }
// // Close pipe
// auto returnCode = pclose(pipe);
// if (returnCode != 0) {
// // Optional: handle non-zero exit status
// }
// std::cout << result;
system(cmd.c_str());
}
export void BounceCommandIgnore(const std::string& cmd) {
std::string with = cmd + " > /dev/null 2>&1";
FILE* pipe = popen(with.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
pclose(pipe);
}

View file

@ -98,7 +98,7 @@ void ModulePartition::Compile(std::string clangDir, const Configuration& config,
if(config.verbose) { if(config.verbose) {
std::cout << command << std::endl; std::cout << command << std::endl;
} }
system(command.c_str()); BounceCommand(command.c_str());
} }
*compiled = true; *compiled = true;
compiled->notify_all(); compiled->notify_all();
@ -112,7 +112,7 @@ void ModulePartition::CompileSource(std::string clangDir, const Configuration& c
if(config.verbose) { if(config.verbose) {
std::cout << command << std::endl; std::cout << command << std::endl;
} }
system(command.c_str()); BounceCommand(command.c_str());
} }
Module::Module(const std::string& name, const fs::path& path, const Configuration& config, const fs::path& pcmDir, std::string& files, const fs::path& buildDir) : name(name), path(path) { Module::Module(const std::string& name, const fs::path& path, const Configuration& config, const fs::path& pcmDir, std::string& files, const fs::path& buildDir) : name(name), path(path) {
@ -187,7 +187,7 @@ void Module::Compile(std::string clangDir, const Configuration& config, fs::path
if(config.verbose) { if(config.verbose) {
std::cout << command << std::endl; std::cout << command << std::endl;
} }
system(command.c_str()); BounceCommand(command.c_str());
} }
std::vector<std::thread> threads2; std::vector<std::thread> threads2;
@ -203,7 +203,7 @@ void Module::Compile(std::string clangDir, const Configuration& config, fs::path
if(config.verbose) { if(config.verbose) {
std::cout << command << std::endl; std::cout << command << std::endl;
} }
system(command.c_str()); BounceCommand(command.c_str());
}); });
for(std::thread& thread : threads2){ for(std::thread& thread : threads2){

View file

@ -239,7 +239,7 @@ void Project::Build(Configuration& config, fs::path outputDir, fs::path binDir)
files+=std::format("{}_source.o ",(buildDir/config.c_files[i].filename()).generic_string()); 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")) { 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](){ threads.emplace_back([i, &config, pcmDir, target, clangDir, flags, march, &buildDir](){
system(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()); BounceCommand(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());
}); });
} }
} }
@ -257,11 +257,11 @@ void Project::Build(Configuration& config, fs::path outputDir, fs::path binDir)
if(config.verbose) { if(config.verbose) {
std::cout << command << std::endl; std::cout << command << std::endl;
} }
system(command.c_str()); BounceCommand(command.c_str());
} else if(config.type == "library"){ } else if(config.type == "library"){
system(std::format("ar r {}.a {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str()); BounceCommandIgnore(std::format("ar r {}.a {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str());
} else if(config.type == "shared-library"){ } else if(config.type == "shared-library"){
system(std::format("ar r {}.so {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str()); BounceCommandIgnore(std::format("ar r {}.so {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str());
} }
for(const fs::path& additionalFile : config.additionalFiles){ for(const fs::path& additionalFile : config.additionalFiles){

View file

@ -43,7 +43,7 @@ void Source::GetSourceFiles(std::string clangDir, const Configuration& config, f
if(config.verbose) { if(config.verbose) {
std::cout << command << std::endl; std::cout << command << std::endl;
} }
system(command.c_str()); BounceCommand(command.c_str());
}); });
} }
} }

View file

@ -25,3 +25,4 @@ export import :Configuration;
export import :ModuleFile; export import :ModuleFile;
export import :SourceFile; export import :SourceFile;
export import :Shader; export import :Shader;
export import :Bounce;

View file

@ -5,7 +5,7 @@
"name": "base", "name": "base",
"standard": "c++26", "standard": "c++26",
"source_files": ["Crafter.Build-Configuration", "Crafter.Build-Project", "Crafter.Build-Dependency", "Crafter.Build-ModuleFile", "Crafter.Build-Shader", "Crafter.Build-SourceFile"], "source_files": ["Crafter.Build-Configuration", "Crafter.Build-Project", "Crafter.Build-Dependency", "Crafter.Build-ModuleFile", "Crafter.Build-Shader", "Crafter.Build-SourceFile"],
"module_files": ["Crafter.Build-Dependency", "Crafter.Build-Configuration", "Crafter.Build-Project", "Crafter.Build-Shader", "Crafter.Build", "Crafter.Build-ModuleFile", "Crafter.Build-SourceFile"], "module_files": ["Crafter.Build-Dependency", "Crafter.Build-Configuration", "Crafter.Build-Project", "Crafter.Build-Shader", "Crafter.Build", "Crafter.Build-ModuleFile", "Crafter.Build-SourceFile", "Crafter.Build-Bounce"],
"build_dir": "build", "build_dir": "build",
"output_dir": "bin", "output_dir": "bin",
"libs": ["vulkan", "MachineIndependent", "OSDependent", "GenericCodeGen", "glslang", "glslang-default-resource-limits", "SPIRV", "SPVRemapper", "tbb"] "libs": ["vulkan", "MachineIndependent", "OSDependent", "GenericCodeGen", "glslang", "glslang-default-resource-limits", "SPIRV", "SPVRemapper", "tbb"]