improved CLI
This commit is contained in:
parent
d3f6c09ac4
commit
fbc13a9897
5 changed files with 112 additions and 66 deletions
|
|
@ -79,47 +79,66 @@ ConfigData CollapseConfig(nlohmann::json& configs,nlohmann::json& config) {
|
|||
void Project::Build(std::string configuration) {
|
||||
for(const Configuration& config : configurations) {
|
||||
if(config.name == configuration){
|
||||
if (!std::filesystem::exists(config.buildDir)) {
|
||||
std::filesystem::create_directory(config.buildDir);
|
||||
} else {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(config.buildDir)){
|
||||
std::filesystem::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
if (!std::filesystem::exists(config.outputDir)) {
|
||||
std::filesystem::create_directory(config.outputDir);
|
||||
} else {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(config.outputDir)){
|
||||
std::filesystem::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
for(const std::string& moduleFile : config.moduleFiles){
|
||||
system(std::format("clang++ -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}/{}.pcm", config.standard, moduleFile, config.buildDir, config.buildDir, moduleFile).c_str());
|
||||
}
|
||||
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++) {
|
||||
files+=std::format("{}/{}.o ",config.buildDir, config.moduleFiles[i]);
|
||||
threads[i] = std::thread([i, config](){
|
||||
system(std::format("clang++ -std={} {}/{}.pcm -fprebuilt-module-path={} -c -O{} -o {}/{}.o", config.standard, config.buildDir, config.moduleFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.moduleFiles[i]).c_str());
|
||||
});
|
||||
}
|
||||
for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) {
|
||||
files+=std::format("{}/{}_source.o ",config.buildDir, config.sourceFiles[i]);
|
||||
threads[config.moduleFiles.size()+i] = std::thread([i, config](){
|
||||
system(std::format("clang++ -std={} {}.cpp -fprebuilt-module-path={} -c -O{} -o {}/{}_source.o", config.standard, config.sourceFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.sourceFiles[i]).c_str());
|
||||
});
|
||||
}
|
||||
for(std::thread& thread : threads){
|
||||
thread.join();
|
||||
}
|
||||
system(std::format("clang++ {}-O{} -o {}/{}", files, config.optimizationLevel, config.outputDir, name).c_str());
|
||||
Build(config, config.outputDir);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
||||
}
|
||||
|
||||
void Project::Build(std::string configuration, std::string outputDir) {
|
||||
for(const Configuration& config : configurations) {
|
||||
if(config.name == configuration){
|
||||
Build(config, outputDir);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("Configuration: " + configuration + " not found.");
|
||||
}
|
||||
|
||||
void Project::Build(Configuration configuration) {
|
||||
Build(configuration, configuration.outputDir);
|
||||
}
|
||||
|
||||
void Project::Build(Configuration config, std::string outputDir) {
|
||||
if (!std::filesystem::exists(config.buildDir)) {
|
||||
std::filesystem::create_directory(config.buildDir);
|
||||
} else {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(config.buildDir)){
|
||||
std::filesystem::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
if (!std::filesystem::exists(outputDir)) {
|
||||
std::filesystem::create_directory(outputDir);
|
||||
} else {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(outputDir)){
|
||||
std::filesystem::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
for(const std::string& moduleFile : config.moduleFiles){
|
||||
system(std::format("clang++ -std={} {}.cppm --precompile -fprebuilt-module-path={} -o {}/{}.pcm", config.standard, moduleFile, config.buildDir, config.buildDir, moduleFile).c_str());
|
||||
}
|
||||
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++) {
|
||||
files+=std::format("{}/{}.o ",config.buildDir, config.moduleFiles[i]);
|
||||
threads[i] = std::thread([i, config](){
|
||||
system(std::format("clang++ -std={} {}/{}.pcm -fprebuilt-module-path={} -c -O{} -o {}/{}.o", config.standard, config.buildDir, config.moduleFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.moduleFiles[i]).c_str());
|
||||
});
|
||||
}
|
||||
for(std::int_fast32_t i = 0; i < config.sourceFiles.size(); i++) {
|
||||
files+=std::format("{}/{}_source.o ",config.buildDir, config.sourceFiles[i]);
|
||||
threads[config.moduleFiles.size()+i] = std::thread([i, config](){
|
||||
system(std::format("clang++ -std={} {}.cpp -fprebuilt-module-path={} -c -O{} -o {}/{}_source.o", config.standard, config.sourceFiles[i], config.buildDir, config.optimizationLevel, config.buildDir, config.sourceFiles[i]).c_str());
|
||||
});
|
||||
}
|
||||
for(std::thread& thread : threads){
|
||||
thread.join();
|
||||
}
|
||||
system(std::format("clang++ {}-O{} -o {}/{}", files, config.optimizationLevel, outputDir, name).c_str());
|
||||
|
||||
}
|
||||
|
||||
Project Project::LoadFromJSON(std::string file) {
|
||||
if (!std::filesystem::exists(file)) {
|
||||
throw std::runtime_error("Project file: " + file + " not found.");
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ export namespace Crafter::Build {
|
|||
std::vector<Configuration> configurations;
|
||||
Project(std::string name, std::vector<Configuration> configurations);
|
||||
void Build(std::string configuration);
|
||||
void Build(std::string configuration, std::string outputDir);
|
||||
void Build(Configuration configuration);
|
||||
void Build(Configuration configuration, std::string outputDir);
|
||||
void SaveToJSON(std::string path);
|
||||
static Project LoadFromJSON(std::string file);
|
||||
};
|
||||
|
|
|
|||
19
README.md
19
README.md
|
|
@ -55,22 +55,15 @@ int main() {
|
|||
std::println("Hello World!");
|
||||
}
|
||||
```
|
||||
Save and close, then run ``crafter-build debug``. Now you can run the ``hello-world`` executable that has appeared in the ``build`` folder
|
||||
Save and close, then run ``crafter-build debug``. Now you can run the ``hello-world`` executable that has appeared in the ``bin`` folder
|
||||
|
||||
## CLI arguments
|
||||
```bash
|
||||
crafter-build <configuration>
|
||||
```
|
||||
builds the project with the specified configuration, using the defualt project filename ``project.json``
|
||||
|
||||
```bash
|
||||
crafter-build <configuration> <filename>
|
||||
```
|
||||
builds the project with the specified configuration, using the specified project filename
|
||||
``--help`` Displays this help message.
|
||||
|
||||
```bash
|
||||
crafter-build --help
|
||||
```
|
||||
displays this help message
|
||||
``-c`` The name of the configuration to build.
|
||||
|
||||
``-p`` The name of the project file.
|
||||
|
||||
``-o`` Overrides the output folder.
|
||||
|
||||
|
|
|
|||
43
main.cpp
43
main.cpp
|
|
@ -1,3 +1,4 @@
|
|||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <print>
|
||||
|
|
@ -5,22 +6,32 @@
|
|||
import Crafter.Build;
|
||||
using namespace Crafter::Build;
|
||||
int main(int argc, char* argv[]) {
|
||||
if(argc == 1){
|
||||
std::println("No configuration provided, use --help for help");
|
||||
} else if(argc == 2) {
|
||||
if(std::string(argv[1]) == "--help"){
|
||||
std::println("<configuration> to build this configuration with the defualt project filename \"project.json\"\n<configuration> <project> to build this configuration with the specified project filename\nhelp displays this help message");
|
||||
} else {
|
||||
Project project = Project::LoadFromJSON("./project.json");
|
||||
project.Build(argv[1]);
|
||||
if(argc == 1) {
|
||||
std::println("No arguments provided, use --help for help");
|
||||
}
|
||||
std::string filename = "project.json";
|
||||
std::string configuration;
|
||||
std::string outputDir;
|
||||
for (std::uint_fast32_t i = 1; i < argc; i++) {
|
||||
std::string arg = std::string(argv[i]);
|
||||
if(arg == "--help"){
|
||||
std::println("--help\tDisplays this help message.\n-c The name of the configuration to build.\n-p\nThe name of the project file.\n-o Overrides the output folder.\n");
|
||||
return 0;
|
||||
} else if(arg == "-c"){
|
||||
configuration = argv[++i];
|
||||
} else if(arg == "-o"){
|
||||
outputDir = argv[++i];
|
||||
} else if(arg == "-p"){
|
||||
filename = argv[++i];
|
||||
} else{
|
||||
std::println("Unkown argument: {}", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
} else if(argc == 3) {
|
||||
Project project = Project::LoadFromJSON(argv[2]);
|
||||
project.Build(argv[1]);
|
||||
return 0;
|
||||
} else {
|
||||
std::println("Too many arguments provided");
|
||||
return 1;
|
||||
}
|
||||
Project project = Project::LoadFromJSON(filename);
|
||||
if(outputDir.empty()){
|
||||
project.Build(configuration);
|
||||
} else{
|
||||
project.Build(configuration, outputDir);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
24
project.json
24
project.json
|
|
@ -10,13 +10,33 @@
|
|||
"output_dir": "./bin"
|
||||
},
|
||||
{
|
||||
"name": "debug",
|
||||
"name": "executable",
|
||||
"extends": ["base"],
|
||||
"type":"executable"
|
||||
},
|
||||
{
|
||||
"name": "lib",
|
||||
"extends": ["base"],
|
||||
"type":"lib"
|
||||
},
|
||||
{
|
||||
"name": "debug",
|
||||
"extends": ["executable"],
|
||||
"optimization_level": "0"
|
||||
},
|
||||
{
|
||||
"name": "release",
|
||||
"extends": ["base"],
|
||||
"extends": ["executable"],
|
||||
"optimization_level": "3"
|
||||
},
|
||||
{
|
||||
"name": "debug-lib",
|
||||
"extends": ["lib"],
|
||||
"optimization_level": "0"
|
||||
},
|
||||
{
|
||||
"name": "release-lib",
|
||||
"extends": ["lib"],
|
||||
"optimization_level": "3"
|
||||
}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue