From a8fb5f7d3d499c5553512a87d34d15a813f63b2d Mon Sep 17 00:00:00 2001 From: Venelin Date: Fri, 26 Apr 2024 18:50:25 +0300 Subject: [PATCH] config.h tests --- config.h | 25 +++++ install.sh | 1 + run_tests.sh | 6 ++ basicTypes.h => src/basicTypes.h | 0 childProcess.cpp => src/childProcess.cpp | 0 childProcess.hpp => src/childProcess.hpp | 0 commands.cpp => src/commands.cpp | 0 commands.hpp => src/commands.hpp | 0 cppipe.cpp => src/cppipe.cpp | 118 +++++++++++++++-------- test/c_file.c | 9 ++ test.cpp => test/test.cpp | 0 todo.txt | 1 + 12 files changed, 118 insertions(+), 42 deletions(-) create mode 100644 config.h create mode 100755 run_tests.sh rename basicTypes.h => src/basicTypes.h (100%) rename childProcess.cpp => src/childProcess.cpp (100%) rename childProcess.hpp => src/childProcess.hpp (100%) rename commands.cpp => src/commands.cpp (100%) rename commands.hpp => src/commands.hpp (100%) rename cppipe.cpp => src/cppipe.cpp (74%) create mode 100644 test/c_file.c rename test.cpp => test/test.cpp (100%) diff --git a/config.h b/config.h new file mode 100644 index 0000000..fbba624 --- /dev/null +++ b/config.h @@ -0,0 +1,25 @@ +// C compiler +const char* CC = "gcc"; + +// C++ compiler +const char* CXX = "g++"; + +// You can read about gcc options by running "man gcc" +// Compiler options to use... + +// ...when debugging +#define DEBUG_FLAGS "-g" + +// ...when not debugging +#define RELEASE_FLAGS "-Ofast", "-flto", "-s" + +// ...for both C and C++ +#define CPPFLAGS "-pipe", "-march=native", "-Wall", "-Wextra" + +// ...for C +#define CFLAGS CPPFLAGS + +// ...for C++ +// cppipe command functions use C++17 +// Wparentheses is disabled on C++ because of the cppipe functions +#define CXXFLAGS CPPFLAGS, "-std=c++17", "-Wno-parentheses", "-lcppipe" diff --git a/install.sh b/install.sh index e540645..fe962a0 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,6 @@ #!/bin/sh set -e +cd src # Install location PREFIX=/usr/local diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..d35704f --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -e + +echo OK | cppipe test/c_file.c + +echo ALL TESTS PASSED diff --git a/basicTypes.h b/src/basicTypes.h similarity index 100% rename from basicTypes.h rename to src/basicTypes.h diff --git a/childProcess.cpp b/src/childProcess.cpp similarity index 100% rename from childProcess.cpp rename to src/childProcess.cpp diff --git a/childProcess.hpp b/src/childProcess.hpp similarity index 100% rename from childProcess.hpp rename to src/childProcess.hpp diff --git a/commands.cpp b/src/commands.cpp similarity index 100% rename from commands.cpp rename to src/commands.cpp diff --git a/commands.hpp b/src/commands.hpp similarity index 100% rename from commands.hpp rename to src/commands.hpp diff --git a/cppipe.cpp b/src/cppipe.cpp similarity index 74% rename from cppipe.cpp rename to src/cppipe.cpp index bd9c1af..5cdc351 100644 --- a/cppipe.cpp +++ b/src/cppipe.cpp @@ -3,11 +3,13 @@ #include #include #include +#include #include #include #include #include "commands.hpp" +#include "../config.h" using namespace std; namespace fs = std::filesystem; @@ -22,6 +24,9 @@ struct MappedFile // todo: move to utils unsigned len; }; +// process the args until the src file arg is found, return its index +static int parse_args_until_src(int argc, char* argv[]); + // find the cpp to run, if it doesn't exist, exit program static fs::path find_path_to_cpp(string_view src_file); @@ -38,6 +43,10 @@ static bool preprocess_and_compare(); // only recompile if changes are present static void compile_src_file(); +static void print_usage(); + +static bool is_src_file(string_view path); + static const char DEBUG_PREFIX[] = "__DBG"; // Context @@ -48,37 +57,10 @@ static fs::path bin; // cache bins to avoid recompiles int main(int argc, char* argv[]) { - if(argc < 2) - { - cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n"; - return 1; - } - if( !strcmp(argv[1], "--help") ) - { - cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n" - << "Compile and run C++ source FILE that uses the cppipe library.\n" - "Pass the ARGUMENTS to the compiled binary.\n" - "The binaries are cached and recompiled only if the source or it's headers have changed.\n" - "-g debug the binary, asserts are also enabled\n"; - return 0; - - } - - int file_arg = 1; - if( !strcmp(argv[1], "-g") ) - { - if(argc < 3) - { - cout << "Usage: cppipe [-g] FILE [ARGUMENTS]...\n"; - return 1; - } - - debug = true; - file_arg = 2; - } + int src_arg = parse_args_until_src(argc, argv); // Init context - src_file = find_path_to_cpp( argv[file_arg] ); + src_file = find_path_to_cpp( argv[src_arg] ); cache_dir = get_cache_dir_path(src_file); bin = cache_dir / (debug ? DEBUG_PREFIX : "") += src_file.filename(); @@ -94,11 +76,53 @@ int main(int argc, char* argv[]) } run += bin.c_str(); - for(int i = file_arg+1; i < argc; ++i) + for(int i = src_arg+1; i < argc; ++i) run += argv[i]; exec(run); } +static int parse_args_until_src(int argc, char* argv[]) +{ + if(argc < 2) + { + print_usage(); + exit(1); + } + + int src_arg = 0; + for(int i = 1; i < argc; ++i) + { + string_view arg( argv[i] ); + if( arg == "--help" ) + { + print_usage(); + cout << "Compile and run C++ source CPP_FILE that uses the cppipe library.\n" + "Pass the ARGUMENTS to the compiled binary.\n" + "The binaries are cached and recompiled only if the source or it's headers have changed.\n" + "-g debug the binary, asserts are also enabled\n"; + exit(0); + } + + else if( arg == "-g" ) + debug = true; + + else if( is_src_file(arg) ) + { + src_arg = i; + break; + } + + } + + if( !src_arg ) + { + print_usage(); + exit(1); + } + + return src_arg; +} + static fs::path find_path_to_cpp(string_view src_file) { if(fs::exists(src_file)) // found relative to CWD @@ -171,7 +195,7 @@ static MappedFile mapfile_for_writing(const fs::path& file) static bool preprocess_and_compare() { Cmd preprocess( - "g++", + CXX, "-E", // preprocess only "-P", // don't generate linemarkers in the output to reduce file size src_file.c_str() @@ -225,29 +249,39 @@ static void compile_src_file() { string preprocessed_file = cache_dir / src_file.stem() += ".ii"; // todo: duplicates with old_pp_path // todo: support C Cmd compile( - "g++", + CXX, preprocessed_file.c_str(), "-o", bin.c_str(), - "-pipe", "-std=c++17", "-march=native", - "-Wall", "-Wextra", "-Wno-parentheses", - "-lcppipe" + CXXFLAGS ); if(debug) { - compile.append_args({ - "-g" - // "-Wall", "-Wextra", "-Wno-parentheses" - }); + compile.append_args({ DEBUG_FLAGS }); } else { - compile.append_args({ - "-Ofast", "-flto", "-s" - }); + compile.append_args({ RELEASE_FLAGS }); } if( !compile() ) // if failed to compile exit(1); } } + +static void print_usage() +{ + cout << "Usage: cppipe [-g] CPP_FILE [ARGUMENTS]...\n"; +} + +static bool is_src_file(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; + + else + return false; +} diff --git a/test/c_file.c b/test/c_file.c new file mode 100644 index 0000000..6ed0bf5 --- /dev/null +++ b/test/c_file.c @@ -0,0 +1,9 @@ +#include + +int main() +{ + char s[16]; + scanf("%s", s); + printf("%s\n", s); + return 0; +} diff --git a/test.cpp b/test/test.cpp similarity index 100% rename from test.cpp rename to test/test.cpp diff --git a/todo.txt b/todo.txt index 0f347e6..1f6e91d 100644 --- a/todo.txt +++ b/todo.txt @@ -13,3 +13,4 @@ make portable shaded obj uninstall throw when a command couldnt be ran +improve debug (the src file shown is the .ii)