XNSim/XNServiceGenServer/XNServiceCompile.cpp

64 lines
1.5 KiB
C++

#include "XNServiceCompile.h"
#include <filesystem>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
namespace fs = std::filesystem;
int XNServiceCompile::Compile(const std::string &srcPath, std::string &errorMsg)
{
// 检查源路径是否存在
if (!fs::exists(srcPath)) {
errorMsg = "源路径不存在: " + srcPath;
return -1;
}
// 检查build目录是否存在
fs::path buildPath = fs::path(srcPath) / "build";
if (fs::exists(buildPath)) {
// 删除build目录
if (fs::remove_all(buildPath) != static_cast<std::uintmax_t>(-1)) {
std::cout << "已删除build目录: " << buildPath.string() << std::endl;
} else {
errorMsg = "无法删除build目录: " + buildPath.string();
return -1;
}
}
// 创建build目录
if (!fs::exists(buildPath)) {
if (!fs::create_directory(buildPath)) {
errorMsg = "无法创建build目录: " + buildPath.string();
return -1;
}
}
// 切换到build目录
if (chdir(buildPath.c_str()) != 0) {
errorMsg = "无法切换到build目录: " + buildPath.string();
return -1;
}
// 执行 cmake ..
int cmakeResult = system("cmake ..");
if (cmakeResult != 0) {
errorMsg = "cmake配置失败";
return -1;
}
// 执行 make
int makeResult = system("make");
if (makeResult != 0) {
errorMsg = "make 编译失败";
return -1;
}
// 执行 make install
int makeInstallResult = system("make install");
if (makeInstallResult != 0) {
errorMsg = "make install失败";
return -1;
}
return 0;
}