From 861a0bd1e4736e7dc66e122569e609013ff65be8 Mon Sep 17 00:00:00 2001 From: Venelin Date: Mon, 27 May 2024 15:35:24 +0300 Subject: [PATCH] support C --- run_tests.sh | 4 ++ src/cppipe.cpp | 81 ++++++++++++++++++------ test/c_file.c | 12 ++-- test/{test.cpp => functions_test.cppipe} | 5 ++ todo.txt | 2 - 5 files changed, 78 insertions(+), 26 deletions(-) rename test/{test.cpp => functions_test.cppipe} (96%) diff --git a/run_tests.sh b/run_tests.sh index d35704f..9981b9b 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -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 diff --git a/src/cppipe.cpp b/src/cppipe.cpp index da8912b..dc9119d 100644 --- a/src/cppipe.cpp +++ b/src/cppipe.cpp @@ -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; + } } } diff --git a/test/c_file.c b/test/c_file.c index de2c04c..f997a5f 100644 --- a/test/c_file.c +++ b/test/c_file.c @@ -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; } diff --git a/test/test.cpp b/test/functions_test.cppipe similarity index 96% rename from test/test.cpp rename to test/functions_test.cppipe index 03c03c7..e00ff2b 100644 --- a/test/test.cpp +++ b/test/functions_test.cppipe @@ -1,3 +1,5 @@ +// Test cppipe functions + #include #include @@ -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", "."); diff --git a/todo.txt b/todo.txt index 982460b..1e3dc34 100644 --- a/todo.txt +++ b/todo.txt @@ -1,7 +1,5 @@ -support C github man -.cppipe file extension tips ?