Build use only stdlib

This commit is contained in:
Nikola Petrov 2024-06-07 22:05:18 +02:00
parent c2a19167bb
commit 10aa689874

View File

@ -2,10 +2,7 @@
#include <cassert>
#include <cstring>
#include <ctime>
#include <sys/wait.h>
#include <sys/stat.h>
#include <thread>
#include <vector>
#include <string>
#include <list>
@ -71,91 +68,30 @@ void print_command(std::vector<std::string> &arguments)
printf("\n");
}
int nob_cmd_run_async(std::vector<std::string> &arguments)
{
std::vector<char *> argument_list;
for (const auto &arg : arguments)
{
argument_list.push_back(const_cast<char *>(arg.c_str()));
}
argument_list.push_back(NULL);
pid_t cpid = fork();
if (cpid < 0)
{
printf("Could not fork child process: %s\n", strerror(errno));
return -1;
}
if (cpid == 0)
{
if (execvp(argument_list[0], argument_list.data()) < 0)
{
print_command(arguments);
printf("Could not exec child process: %s\n", strerror(errno));
exit(1);
}
assert(0 && "unreachable");
}
return cpid;
}
bool nob_proc_wait(int proc)
{
if (proc == -1)
return false;
for (;;)
{
int wstatus = 0;
if (waitpid(proc, &wstatus, 0) < 0)
{
printf("could not wait on command (pid %d): %s\n", proc, strerror(errno));
return false;
}
if (WIFEXITED(wstatus))
{
int exit_status = WEXITSTATUS(wstatus);
if (exit_status != 0)
{
printf("command exited with exit code %d\n", exit_status);
return false;
}
break;
}
if (WIFSIGNALED(wstatus))
{
printf("command process was terminated by %s\n", strsignal(WTERMSIG(wstatus)));
return false;
}
}
return true;
}
bool nob_cmd_run_sync(std::vector<std::string> &arguments)
{
print_command(arguments);
int p = nob_cmd_run_async(arguments);
if (p == -1)
return false;
return nob_proc_wait(p);
std::string command = "";
for (auto &&i : arguments)
{
command += " " + i;
}
return std::system(command.c_str()) == 0;
}
bool check_if_rebuild(std::filesystem::path &org_path, std::filesystem::path &new_path)
{
struct stat file_stat_one;
if (stat(org_path.c_str(), &file_stat_one))
if (!std::filesystem::exists(org_path))
return false;
struct stat file_stat_two;
if (stat(new_path.c_str(), &file_stat_two))
if (!std::filesystem::exists(new_path))
return true;
return file_stat_one.st_ctime > file_stat_two.st_ctime;
auto file_time_one = std::filesystem::last_write_time(org_path);
auto file_time_two = std::filesystem::last_write_time(new_path);
return file_time_one > file_time_two;
}
bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec_path)