This commit is contained in:
Zengtudor 2024-08-15 22:25:02 +08:00
parent 49c808fe9a
commit 26969b340e
2 changed files with 119 additions and 5 deletions

View File

@ -1,19 +1,66 @@
#include "json/reader.h"
#include "json/value.h"
#include <cstdlib> #include <cstdlib>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <functional>
#include <iostream> #include <iostream>
#include<json/json.h> #include<json/json.h>
#include <ostream>
#include <queue>
#include <string>
using std::cerr,std::endl,std::cout; #ifdef _WIN32
#include<windows.h>
#else
#include "unistd.h"
#endif
using std::cerr,std::endl,std::cout,std::ifstream,std::string,std::vector,std::queue,std::filesystem::path;
#define AS_EQ(a,b){if((a)!=(b)){cerr<<"assert eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}} #define AS_EQ(a,b){if((a)!=(b)){cerr<<"assert eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}}
#define AS_NE(a,b){if((a)!=(b)){cerr<<"assert not eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}} #define AS_NE(a,b){if((a)==(b)){cerr<<"assert not eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl;exit(1);}}
#define LOG(a){cout<<"[LOG]"<<(a)<<endl;} #define LOG(a){cout<<"[LOG]"<<(a)<<endl;}
#define AS_EM(a,b,m){if((a)!=(b)){cerr<<"assert eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl<<"message:"<<(m)<<endl;exit(1);}} #define AS_EM(a,b,m){if((a)!=(b)){cerr<<"assert eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl<<"message:"<<(m)<<endl;exit(1);}}
#define AS_NM(a,b,m){if((a)==(b)){cerr<<"assert not eq failed :"<<endl<<#a<<":"<<(a)<<endl<<#b<<":"<<(b)<<endl<<"message:"<<(m)<<endl;exit(1);}}
#define printValue(v){cout<<#v<<":"<<(v)<<endl;}
#ifdef _WIN32
bool isWindows = true;
#else
bool isWindows=false;
#endif
#ifdef _WIN32
unsigned long getCpuNums(){
SYSTEM_INFO sysInfo;
GetSystemInfo( &sysInfo );
return sysInfo.dwNumberOfProcessors;
}
#elif
unsigned long getCpuNums(){
return sysconf(_SC_NPROCESSORS_ONLN)
}
#endif
std::filesystem::path getAbsolutePath(std::filesystem::path rootPath,std::filesystem::path filePath);
string getProtectPath(const std::filesystem::path &p);
struct Test{
std::filesystem::path in;
std::filesystem::path ans;
friend std::ostream& operator<<(std::ostream& os,Test &t){
os<<"Test{"<<"in:"<<t.in<<",ans:"<<t.ans<<"} ";
return os;
}
};
template<typename T>
T getValueOrPanic(T value);
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
LOG("Zengtudor OI test tools . loading files .. please add a json file in arg"); LOG("Zengtudor OI test tools . loading files .. please add a json file in arg");
LOG("arg[0]") printValue(argv[0])
LOG(argv[0])
AS_EQ(argc,2) AS_EQ(argc,2)
std::filesystem::path jsonPath(argv[1]); std::filesystem::path jsonPath(argv[1]);
if (!jsonPath.is_absolute()) { if (!jsonPath.is_absolute()) {
@ -22,6 +69,7 @@ int main(int argc, char* argv[]) {
} }
LOG("getting json files current dir") LOG("getting json files current dir")
LOG(jsonPath) LOG(jsonPath)
AS_EM(std::filesystem::exists(jsonPath), true, "Please create the json file,the json file was not exist")
LOG("getting project path") LOG("getting project path")
const auto projectPath = jsonPath.parent_path(); const auto projectPath = jsonPath.parent_path();
LOG(projectPath) LOG(projectPath)
@ -32,5 +80,71 @@ int main(int argc, char* argv[]) {
const auto buildDir = projectPath / "build"; const auto buildDir = projectPath / "build";
LOG("getting build dir") LOG("getting build dir")
LOG(buildDir) LOG(buildDir)
LOG("parsing json file")
ifstream jsonFileStream(jsonPath);
Json::Value jsonValue;
Json::Reader jsonReader;
jsonReader.parse(jsonFileStream,jsonValue);
AS_NM(jsonValue["files"], Json::nullValue, "cannot read the json value `files` array")
string projectFilesBuildString;
LOG("parsing files in json values")
for(auto i:jsonValue["files"]){
projectFilesBuildString = projectFilesBuildString + " " +getProtectPath(getAbsolutePath(projectPath, std::filesystem::path(i.asString())));
}
printValue(projectFilesBuildString)
LOG("parsing g++ compiler args")
string compileArgs;
for(auto i:jsonValue["args"]){
compileArgs = compileArgs + " "+i.asString();
}
printValue(compileArgs)
if(std::filesystem::exists(buildDir)==false){
LOG("creating building dir")
std::filesystem::create_directory(buildDir);
}
LOG("compiling")
string compileCommands;
const path exePath = (buildDir/"main").string()+(isWindows==true?".exe":"");
printValue(exePath)
compileCommands=compileCommands+"g++ "+"-o "+getProtectPath(exePath)+" "+projectFilesBuildString+" "+compileArgs;
printValue(compileCommands)
AS_EM(system(compileCommands.c_str()), 0, "compile the program failed")
auto cpuNums = getCpuNums();
printValue(cpuNums)
if(jsonValue["tests"].size()==0)LOG("cannot find tests");
LOG("parsing tests")
vector<Test> tests;
for(auto i:jsonValue["tests"]){
tests.push_back({
.in=getAbsolutePath(projectPath, getValueOrPanic(i["in"]).asString()),
.ans=getAbsolutePath(projectPath,getValueOrPanic(i["ans"]).asString())
});
}
queue<std::function<void()>> tasks;
const string exePathStr = getProtectPath(exePath);
for(auto i:tests){
tasks.push([i,&exePathStr](){
string commands;
commands = commands+exePathStr+" < "+getProtectPath(i.in)+" > ";
});
}
}
string getProtectPath(const std::filesystem::path &p){
return "\""+p.string()+"\"";
}
template<typename T>
T getValueOrPanic(T value){
AS_NE(value, Json::nullValue)
return value;
}
std::filesystem::path getAbsolutePath(std::filesystem::path rootPath,std::filesystem::path filePath){
if(filePath.is_absolute()==false){
filePath = rootPath/filePath;
}
return filePath;
} }

View File

@ -1,6 +1,6 @@
add_rules("mode.debug", "mode.release") add_rules("mode.debug", "mode.release")
add_requires("jsoncpp") add_requires("jsoncpp")
set_languages("c++17") set_languages("c++23")
target("xj") target("xj")
set_rundir("./") set_rundir("./")