do not invoke compiler; just output .c file

This commit is contained in:
Josh Holtrop 2018-05-22 22:15:39 -04:00
parent 23a92ca9bd
commit 1418907893

View File

@ -9,9 +9,7 @@
#include <getopt.h>
static char preprocessed_fname[] = "/tmp/cxlppXXXXXX";
static char c_fname[] = "/tmp/cxlcXXXXXX.c";
static bool preprocessed_fname_created = false;
static bool c_fname_created = false;
bool preprocess(const char * input_fname)
{
@ -74,40 +72,14 @@ void write_node(FILE * file, Node * node)
}
}
bool emit_c(Node * node)
bool emit_c(Node * node, const char * output_fname)
{
int fd = mkstemps(c_fname, 2);
if (fd < 0)
{
perror("mkstemp");
return false;
}
FILE * file = fdopen(fd, "w");
c_fname_created = true;
FILE * file = fopen(output_fname, "w");
write_node(file, node);
fclose(file);
return true;
}
bool compile(const char * output_fname)
{
pid_t pid = fork();
if (pid < 0)
{
perror("fork");
return false;
}
else if (pid == 0)
{
execlp("gcc", "gcc", "-c", c_fname, "-o", output_fname, NULL);
}
else
{
waitpid(pid, NULL, 0);
}
return true;
}
std::string g_output_fname;
const char * build_output_fname(const char * input_fname)
{
@ -115,11 +87,11 @@ const char * build_output_fname(const char * input_fname)
size_t pos = g_output_fname.rfind(".");
if (pos != std::string::npos)
{
g_output_fname.replace(pos, g_output_fname.size() - pos, ".o");
g_output_fname.replace(pos, g_output_fname.size() - pos, ".c");
}
else
{
g_output_fname += ".o";
g_output_fname += ".c";
}
return g_output_fname.c_str();
}
@ -162,10 +134,7 @@ int main(int argc, char * argv[])
Node * node = parse(preprocessed_fname);
if (node != nullptr)
{
if (emit_c(node))
{
compile(output_fname);
}
emit_c(node, output_fname);
}
}
/* Clean up temporary files. */
@ -173,10 +142,6 @@ int main(int argc, char * argv[])
{
unlink(preprocessed_fname);
}
if (c_fname_created)
{
unlink(c_fname);
}
return 0;
}