support C

This commit is contained in:
Venelin
2024-05-27 15:35:24 +03:00
parent cf3663b348
commit 861a0bd1e4
5 changed files with 78 additions and 26 deletions
+4
View File
@@ -1,6 +1,10 @@
#!/bin/sh
set -e
# Test cppipe functions
cppipe test/test.cpp
# Test on a C file
echo OK | cppipe test/c_file.c
echo ALL TESTS PASSED
+62 -19
View File
@@ -14,6 +14,12 @@
using namespace std;
namespace fs = std::filesystem;
enum SrcType
{
C,
CPP
};
namespace
{
@@ -48,13 +54,14 @@ void compile_src_file();
void print_usage();
bool is_src_file(string_view path);
SrcType find_src_type(string_view path);
const char DEBUG_PREFIX[] = "__DBG";
// Context
bool debug = false;
fs::path src_file;
SrcType src_type;
fs::path cache_dir;
fs::path bin; // cache bins to avoid recompiles
@@ -66,6 +73,7 @@ int main(int argc, char* argv[])
// Init context
src_file = find_path_to_cpp( argv[src_arg] );
src_type = find_src_type( argv[src_arg] );
cache_dir = get_cache_dir_path(src_file);
bin = cache_dir / (debug ? DEBUG_PREFIX : "") += src_file.filename();
@@ -110,11 +118,11 @@ int parse_args_until_src(int argc, char* argv[])
"-g debug the binary, asserts are also enabled\n";
exit(0);
}
else if( arg == "-g" )
{
debug = true;
else if( is_src_file(arg) )
}
else // Then treat it as the src_arg
{
src_arg = i;
break;
@@ -203,15 +211,30 @@ MappedFile mapfile_for_writing(const fs::path& file)
bool preprocess_and_compare()
{
Cmd preprocess(
CXX,
"-E", // preprocess only
src_file.c_str(),
CXXFLAGS
src_type == SrcType::C ? CC : CXX,
"-E" // preprocess only
);
if(src_type == SrcType::C)
{
for(const char* flag: { CFLAGS })
preprocess += flag;
}
else
{
for(const char* flag: { CXXFLAGS })
preprocess += flag;
preprocess += "-xc++"; // treat the file as a cpp
}
preprocess += src_file.c_str();
if(!debug)
preprocess += "-DNDEBUG";
fs::path old_pp_path = cache_dir / (debug ? DEBUG_PREFIX : "") += src_file.stem() += ".ii";
fs::path old_pp_path = cache_dir / (debug ? DEBUG_PREFIX : "") += src_file.stem()
+= (src_type == SrcType::C ? ".i" : ".ii");
string new_pp = $(preprocess);
@@ -255,14 +278,27 @@ void compile_src_file()
// Only compile if the source is newer then the bin
if(preprocess_and_compare() || !fs::exists(bin))
{
string preprocessed_file = cache_dir / src_file.stem() += ".ii"; // todo: duplicates with old_pp_path // todo: support C
string preprocessed_file = cache_dir / src_file.stem() +=
(src_type == SrcType::C ? ".i" : ".ii"); // todo: duplicates with old_pp_path
Cmd compile(
CXX,
src_type == SrcType::C ? CC : CXX,
preprocessed_file.c_str(),
"-o", bin.c_str(),
CXXFLAGS
"-o", bin.c_str()
);
if(src_type == SrcType::C)
{
for(const char* flag: { CFLAGS })
compile += flag;
}
else
{
for(const char* flag: { CXXFLAGS })
compile += flag;
}
if(debug)
{
compile.append_args({ DEBUG_FLAGS });
@@ -282,16 +318,23 @@ void print_usage()
cout << "Usage: cppipe [-g] CPP_FILE [ARGUMENTS]...\n";
}
bool is_src_file(const string_view p)
SrcType find_src_type(const string_view p)
{
size_t end = p.size() - 1;
if( (p.size() > 4 && p[end-3] == '.' && p[end-2] == 'c' && p[end-1] == 'p' && p[end] == 'p')
|| (p.size() > 2 && p[end-1] == '.' && p[end] == 'c'))
return true;
// if(p.size() > 4 && p[end-3] == '.' && p[end-2] == 'c' && p[end-1] == 'p' && p[end] == 'p')
// {
// src_type = SrcType::CPP
// }
else
return false;
if( p.size() > 2 && p[end-1] == '.' && p[end] == 'c' )
{
return SrcType::C;
}
else // treat it as cpp
{
return SrcType::CPP;
}
}
}
+7 -5
View File
@@ -3,11 +3,13 @@
int main()
{
int* x;
void* y;
x = y;
void* y = 0;
x = y; /* should only work on C */
if(x == 0)
{
printf("C compilation OK!\n");
}
char s[16];
scanf("%s", s);
printf("%s\n", s);
return 0;
}
@@ -1,3 +1,5 @@
// Test cppipe functions
#include <cppipe/commands.hpp>
#include <sys/wait.h>
@@ -26,7 +28,10 @@ int main(int argc, char* argv[])
string out2 = $(echo + "abc" + "def");
if(auto len = out2.size(); len != 8)
{
cerr << "FAILURE: unexpected output length " << len << endl;
exit(1);
}
Cmd success("echo", "OK 1/11");
Cmd fail("mkdir", ".");
-2
View File
@@ -1,7 +1,5 @@
support C
github
man
.cppipe file extension
tips
?