Add project
This commit is contained in:
437
src/main.cpp
Normal file
437
src/main.cpp
Normal file
@@ -0,0 +1,437 @@
|
||||
#include <spdlog/async.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <structopt/app.hpp>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define popen _popen
|
||||
#define pclose _pclose
|
||||
#endif
|
||||
|
||||
enum Command {
|
||||
build,
|
||||
configure,
|
||||
clean,
|
||||
del,
|
||||
rebuild,
|
||||
};
|
||||
|
||||
enum BuildType {
|
||||
none,
|
||||
debug,
|
||||
release,
|
||||
relwithdeb,
|
||||
};
|
||||
|
||||
struct Options {
|
||||
Command command;
|
||||
std::optional<BuildType> buildType = BuildType::none;
|
||||
|
||||
std::optional<bool> verbose = false;
|
||||
std::optional<bool> dryrun = false;
|
||||
};
|
||||
STRUCTOPT(Options, command, buildType, verbose, dryrun);
|
||||
|
||||
bool g_DryRun = false;
|
||||
|
||||
void initLogging() {
|
||||
spdlog::init_thread_pool(8192, 1);
|
||||
spdlog::flush_every(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
void createLogger(const char* name, const char* filename, const bool verbose) {
|
||||
std::vector<spdlog::sink_ptr> sinks;
|
||||
|
||||
const auto stdout_sink =
|
||||
std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
|
||||
stdout_sink->set_level(spdlog::level::debug);
|
||||
stdout_sink->set_pattern("%^[%D %r %z] [%n] [%l] %v%$");
|
||||
sinks.push_back(stdout_sink);
|
||||
|
||||
if (filename != nullptr) {
|
||||
const auto file_sink =
|
||||
std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename);
|
||||
file_sink->set_level(spdlog::level::debug);
|
||||
file_sink->set_pattern("[%D %r %z] [%n] [%l] %v");
|
||||
sinks.push_back(file_sink);
|
||||
}
|
||||
|
||||
const auto logger = std::make_shared<spdlog::async_logger>(
|
||||
name, sinks.begin(), sinks.end(), spdlog::thread_pool(),
|
||||
spdlog::async_overflow_policy::block);
|
||||
logger->flush_on(spdlog::level::warn);
|
||||
|
||||
#ifdef DEBUG
|
||||
logger->set_level(spdlog::level::debug);
|
||||
#else
|
||||
if (verbose)
|
||||
logger->set_level(spdlog::level::debug);
|
||||
else
|
||||
logger->set_level(spdlog::level::info);
|
||||
#endif
|
||||
|
||||
spdlog::register_logger(logger);
|
||||
}
|
||||
|
||||
void deleteDir(const std::filesystem::path& path) {
|
||||
if (g_DryRun) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("[DRYRUN] Deleting '{}'", path.string());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")->warn("DELETING '{}' directory.", path.string());
|
||||
spdlog::get("filesystem")->debug("Checking '{}'", path.string());
|
||||
if (!std::filesystem::exists(path) ||
|
||||
!std::filesystem::is_directory(path)) {
|
||||
spdlog::get("filesystem")
|
||||
->info(
|
||||
"The {} directory either doesn't exist or its a file. "
|
||||
"Continuing...",
|
||||
path.string());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")->debug("Deleting '{}'", path.string());
|
||||
std::filesystem::remove_all(path);
|
||||
}
|
||||
|
||||
void createDir(const std::filesystem::path& path) {
|
||||
if (g_DryRun) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("[DRYRUN] Creating '{}'", path.string());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")->debug("Creating '{}'", path.string());
|
||||
if (const bool res = std::filesystem::create_directories(path); !res) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("Failed to create directory: '{}'", path.string());
|
||||
}
|
||||
}
|
||||
|
||||
void checkBuildDirs(const BuildType buildType) {
|
||||
spdlog::get("filesystem")->debug("Checking 'build' directory.");
|
||||
if (!std::filesystem::is_directory("build")) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("'build' is not a directory. Deleting.");
|
||||
deleteDir("build");
|
||||
}
|
||||
if (!std::filesystem::exists("build")) {
|
||||
spdlog::get("filesystem")->info("Creating 'build' directory");
|
||||
createDir("build");
|
||||
}
|
||||
|
||||
switch (buildType) {
|
||||
case none:
|
||||
spdlog::get("filesystem")
|
||||
->debug("Checking 'build/debug' directory.");
|
||||
if (!std::filesystem::is_directory("build/debug")) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("'build/debug' is not a directory. Deleting.");
|
||||
deleteDir("build/debug");
|
||||
}
|
||||
if (!std::filesystem::exists("build/debug")) {
|
||||
spdlog::get("filesystem")
|
||||
->info("Creating 'build/debug' directory.");
|
||||
createDir("build/debug");
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")
|
||||
->debug("Checking 'build/release' directory.");
|
||||
if (!std::filesystem::is_directory("build/release")) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("'build/release' is not a directory. Deleting.");
|
||||
deleteDir("build/release");
|
||||
}
|
||||
if (!std::filesystem::exists("build/release")) {
|
||||
spdlog::get("filesystem")
|
||||
->info("Creating 'build/release' directory.");
|
||||
createDir("build/release");
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")
|
||||
->debug("Checking 'build/relwithdeb' directory.");
|
||||
if (!std::filesystem::is_directory("build/relwithdeb")) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("'build/relwithdeb' is not a directory. Deleting.");
|
||||
deleteDir("build/relwithdeb");
|
||||
}
|
||||
if (!std::filesystem::exists("build/relwithdeb")) {
|
||||
spdlog::get("filesystem")
|
||||
->info("Creating 'build/relwithdeb' directory.");
|
||||
createDir("build/relwithdeb");
|
||||
}
|
||||
break;
|
||||
case debug:
|
||||
spdlog::get("filesystem")
|
||||
->debug("Checking 'build/debug' directory.");
|
||||
if (!std::filesystem::is_directory("build/debug")) {
|
||||
spdlog::get("filsystem")
|
||||
->warn("'build/debug' is not a directory. Deleting.");
|
||||
deleteDir("build/debug");
|
||||
}
|
||||
if (!std::filesystem::exists("build/debug")) {
|
||||
spdlog::get("filesystem")
|
||||
->info("Creating 'build/debug' directory.");
|
||||
createDir("build/debug");
|
||||
}
|
||||
break;
|
||||
case release:
|
||||
spdlog::get("filesystem")
|
||||
->debug("Checking 'build/release' directory.");
|
||||
if (!std::filesystem::is_directory("build/release")) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("'build/release' is not a directory. Deleting.");
|
||||
deleteDir("build/release");
|
||||
}
|
||||
if (!std::filesystem::exists("build/release")) {
|
||||
spdlog::get("filesystem")
|
||||
->info("Creating 'build/release' directory.");
|
||||
createDir("build/release");
|
||||
}
|
||||
break;
|
||||
case relwithdeb:
|
||||
spdlog::get("filesystem")
|
||||
->debug("Checking 'build/relwithdeb' directory.");
|
||||
if (!std::filesystem::is_directory("build/relwithdeb")) {
|
||||
spdlog::get("filesystem")
|
||||
->warn("'build/relwithdeb' is not a directory. Deleting.");
|
||||
deleteDir("build/relwithdeb");
|
||||
}
|
||||
if (!std::filesystem::exists("build/relwithdeb")) {
|
||||
spdlog::get("filesystem")
|
||||
->info("Creating 'build/relwithdeb' directory.");
|
||||
createDir("build/relwithdeb");
|
||||
};
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")->info("All directories checked.");
|
||||
}
|
||||
|
||||
int runCmd(std::string cmd) {
|
||||
if (g_DryRun) {
|
||||
spdlog::get("cmd")->warn("[DRYRUN] Running '{}'", cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
spdlog::get("cmd")->debug("Running '{}'", cmd);
|
||||
spdlog::get("cmd")->debug("Opening pipe");
|
||||
FILE* pipe = popen(cmd.c_str(), "r");
|
||||
if (!pipe) {
|
||||
spdlog::get("cmd")->warn("Failed to start the process");
|
||||
return 1;
|
||||
}
|
||||
|
||||
spdlog::get("cmd")->debug("Buffering and offloading pipe");
|
||||
std::array<char, 256> buffer;
|
||||
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
|
||||
std::cout << buffer.data() << std::flush;
|
||||
}
|
||||
|
||||
spdlog::get("filesystem")->debug("Closing pipe");
|
||||
int exitCode = pclose(pipe);
|
||||
spdlog::get("cmd")->info("Process ended with exit code: {}", exitCode);
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
void buildDir(const std::filesystem::path& path) {
|
||||
if (g_DryRun) {
|
||||
spdlog::get("build")->warn("[DRYRUN] Building '{}'", path.string());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("build")->info("Building '{}'", path.string());
|
||||
if (int exitCode = runCmd(std::format("cd {} && ninja", path.string()));
|
||||
exitCode != 0) {
|
||||
spdlog::get("build")->critical("Command exited with exit code: {}",
|
||||
exitCode);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void configureDir(const std::filesystem::path& path, std::string buildStr) {
|
||||
if (g_DryRun) {
|
||||
spdlog::get("configure")
|
||||
->warn("[DRYRUN] Configuring '{}'", path.string());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("configure")->info("Configuring '{}'", path.string());
|
||||
if (int exitCode = runCmd(
|
||||
std::format("cd {} && cmake ../../ -G Ninja -DCMAKE_BUILD_TYPE={}",
|
||||
path.string(), buildStr));
|
||||
exitCode != 0) {
|
||||
spdlog::get("configure")
|
||||
->critical("Command exited with exit code: {}", exitCode);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void cleanDir(const std::filesystem::path& path) {
|
||||
if (g_DryRun) {
|
||||
spdlog::get("clean")->warn("[DRYRUN] Cleaning '{}'", path.string());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::get("clean")->info("Cleaning '{}'", path.string());
|
||||
if (int exitCode =
|
||||
runCmd(std::format("cd {} && cmake clean", path.string()));
|
||||
exitCode != 0) {
|
||||
spdlog::get("clean")->critical("Command exited with exit code: {}",
|
||||
exitCode);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void buildProject(const BuildType buildType) {
|
||||
checkBuildDirs(buildType);
|
||||
switch (buildType) {
|
||||
case none:
|
||||
configureDir("build/debug", "Debug");
|
||||
buildDir("build/debug");
|
||||
configureDir("build/release", "Release");
|
||||
buildDir("build/release");
|
||||
configureDir("build/relwithdeb", "RelWithDeb");
|
||||
buildDir("build/relwithdeb");
|
||||
break;
|
||||
case debug:
|
||||
configureDir("build/debug", "Debug");
|
||||
buildDir("build/debug");
|
||||
break;
|
||||
case release:
|
||||
configureDir("build/release", "Release");
|
||||
buildDir("build/release");
|
||||
break;
|
||||
case relwithdeb:
|
||||
configureDir("build/relwithdeb", "RelWithDeb");
|
||||
buildDir("build/relwithdeb");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void configureProject(const BuildType buildType) {
|
||||
checkBuildDirs(buildType);
|
||||
switch (buildType) {
|
||||
case none:
|
||||
configureDir("build/debug", "Debug");
|
||||
configureDir("build/release", "Release");
|
||||
configureDir("build/relwithdeb", "RelWithDeb");
|
||||
break;
|
||||
case debug:
|
||||
configureDir("build/debug", "Debug");
|
||||
break;
|
||||
case release:
|
||||
configureDir("build/release", "Release");
|
||||
break;
|
||||
case relwithdeb:
|
||||
configureDir("build/RelWithDeb", "RelWithDeb");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void cleanProject(const BuildType buildType) {
|
||||
checkBuildDirs(buildType);
|
||||
|
||||
switch (buildType) {
|
||||
case none:
|
||||
cleanDir("build/debug");
|
||||
cleanDir("build/release");
|
||||
cleanDir("build/relwithdeb");
|
||||
break;
|
||||
case debug:
|
||||
cleanDir("build/debug");
|
||||
break;
|
||||
case release:
|
||||
cleanDir("build/release");
|
||||
break;
|
||||
case relwithdeb:
|
||||
cleanDir("build/RelWithDeb");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void deleteProject(const BuildType buildType) {
|
||||
switch (buildType) {
|
||||
case none:
|
||||
deleteDir("build");
|
||||
break;
|
||||
case debug:
|
||||
deleteDir("build/debug");
|
||||
break;
|
||||
case release:
|
||||
deleteDir("build/release");
|
||||
break;
|
||||
case relwithdeb:
|
||||
deleteDir("build/relwithdeb");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void rebuildProject(const BuildType buildType) {
|
||||
deleteProject(buildType);
|
||||
checkBuildDirs(buildType);
|
||||
configureProject(buildType);
|
||||
buildProject(buildType);
|
||||
}
|
||||
|
||||
int main(const int argc, char** argv) {
|
||||
Options options;
|
||||
try {
|
||||
options = structopt::app(argv[0], "1.0.0").parse<Options>(argc, argv);
|
||||
} catch (structopt::exception& e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
std::cout << e.help();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
assert(options.verbose.has_value());
|
||||
|
||||
assert(options.dryrun.has_value());
|
||||
assert(options.buildType.has_value());
|
||||
|
||||
g_DryRun = options.dryrun.value();
|
||||
|
||||
initLogging();
|
||||
createLogger("app", nullptr, options.verbose.value());
|
||||
spdlog::get("app")->debug("Creating logger: 'build'.");
|
||||
createLogger("build", nullptr, options.verbose.value());
|
||||
spdlog::get("app")->debug("Creating logger: 'configure'.");
|
||||
createLogger("configure", nullptr, options.verbose.value());
|
||||
spdlog::get("app")->debug("Creating logger: 'clean'.");
|
||||
createLogger("clean", nullptr, options.verbose.value());
|
||||
spdlog::get("app")->debug("Creating logger: 'filesystem'.");
|
||||
createLogger("filesystem", nullptr, options.verbose.value());
|
||||
spdlog::get("app")->debug("Creating logger: 'cmd'.");
|
||||
createLogger("cmd", nullptr, options.verbose.value());
|
||||
spdlog::get("app")->debug("Loggers created.");
|
||||
|
||||
spdlog::get("app")->debug("Running command");
|
||||
switch (options.command) {
|
||||
case build:
|
||||
buildProject(options.buildType.value());
|
||||
break;
|
||||
case configure:
|
||||
configureProject(options.buildType.value());
|
||||
break;
|
||||
case clean:
|
||||
cleanProject(options.buildType.value());
|
||||
break;
|
||||
case del:
|
||||
deleteProject(options.buildType.value());
|
||||
break;
|
||||
case rebuild:
|
||||
rebuildProject(options.buildType.value());
|
||||
break;
|
||||
}
|
||||
|
||||
spdlog::get("app")->info("All work done!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user