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