2024-12-29 01:00:54 +01:00
/*
Crafter . Build
2025-04-29 00:15:14 +02:00
Copyright ( C ) 2025 Catcrafts ®
2024-12-31 20:32:00 +01:00
Catcrafts . net
2024-12-29 01:00:54 +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
2025-04-29 00:15:14 +02:00
version 3.0 of the License , or ( at your option ) any later version .
2024-12-29 01:00:54 +01:00
2025-04-29 00:15:14 +02:00
This library is distributed in the hope that it will be useful ,
2024-12-29 01:00:54 +01:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2024-12-29 01:38:25 +01:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
2024-12-29 01:00:54 +01:00
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:00:54 +01:00
*/
2024-12-29 00:51:02 +01:00
# include <cstdint>
2024-12-28 21:26:19 +01:00
# include <print>
2025-09-09 23:04:05 +02:00
# include <tuple>
# include <iostream>
2024-12-28 21:00:12 +01:00
# include "json.hpp"
import Crafter . Build ;
using namespace Crafter : : Build ;
2024-12-29 17:53:46 +01:00
namespace fs = std : : filesystem ;
2024-12-28 21:00:12 +01:00
int main ( int argc , char * argv [ ] ) {
2024-12-29 00:51:02 +01:00
if ( argc = = 1 ) {
std : : println ( " No arguments provided, use --help for help " ) ;
}
2024-12-29 17:53:46 +01:00
fs : : path filepath = " project.json " ;
2024-12-29 00:51:02 +01:00
std : : string configuration ;
std : : string outputDir ;
2025-02-03 22:06:54 +01:00
bool run = false ;
2024-12-29 00:51:02 +01:00
for ( std : : uint_fast32_t i = 1 ; i < argc ; i + + ) {
std : : string arg = std : : string ( argv [ i ] ) ;
if ( arg = = " --help " ) {
2025-02-03 22:06:54 +01:00
std : : println ( " --help \t Displays this help message. \n -c The name of the configuration to build. \n -p The path to the project file. defualts to project.json \n -o Overrides the output folder. \n -r Runs the executable after building. " ) ;
2024-12-29 00:51:02 +01:00
return 0 ;
} else if ( arg = = " -c " ) {
configuration = argv [ + + i ] ;
2025-02-03 22:06:54 +01:00
} else if ( arg = = " -r " ) {
run = true ;
2024-12-29 00:51:02 +01:00
} else if ( arg = = " -o " ) {
outputDir = argv [ + + i ] ;
} else if ( arg = = " -p " ) {
2024-12-29 17:53:46 +01:00
filepath = fs : : path ( argv [ + + i ] ) ;
2024-12-29 00:51:02 +01:00
} else {
std : : println ( " Unkown argument: {} " , argv [ i ] ) ;
return 1 ;
2024-12-28 21:26:19 +01:00
}
2024-12-29 00:51:02 +01:00
}
2024-12-29 17:53:46 +01:00
fs : : path projectPath ;
if ( filepath . is_relative ( ) ) {
projectPath = fs : : current_path ( ) / filepath ;
} else {
projectPath = filepath ;
}
2025-09-09 23:04:05 +02:00
std : : vector < ClangError > errors ;
2024-12-29 17:53:46 +01:00
Project project = Project : : LoadFromJSON ( projectPath ) ;
2024-12-29 00:51:02 +01:00
if ( outputDir . empty ( ) ) {
2025-09-09 23:04:05 +02:00
errors = std : : get < 1 > ( project . Build ( configuration ) ) ;
2024-12-29 00:51:02 +01:00
} else {
2025-09-09 23:04:05 +02:00
errors = std : : get < 1 > ( project . Build ( configuration , fs : : path ( outputDir ) ) ) ;
}
if ( errors . size ( ) > 0 ) {
for ( const ClangError & error : errors ) {
std : : cout < < " Filename: " < < error . filename < < std : : endl ;
std : : cout < < " Line: " < < error . line_number < < std : : endl ;
std : : cout < < " Error Message: " < < error . error_message < < std : : endl ;
std : : cout < < " Code: " < < error . code < < std : : endl < < std : : endl ;
}
return 0 ;
2024-12-28 21:26:19 +01:00
}
2025-02-03 22:06:54 +01:00
if ( run ) {
2025-04-24 19:21:09 +02:00
for ( Configuration & config : project . configurations ) {
if ( config . name = = configuration ) {
if ( config . target = = " x86_64-w64-mingw64 " | | config . target = = " x86_64-w64-mingw32 " ) {
project . name + = " .exe " ;
}
2025-04-29 00:15:14 +02:00
system ( std : : format ( " cd {} && ./{} " , ( projectPath / fs : : path ( config . outputDir ) ) . generic_string ( ) , project . name ) . c_str ( ) ) ;
2025-02-03 22:06:54 +01:00
return 0 ;
}
}
}
2024-12-28 21:00:12 +01:00
}