config.h tests

This commit is contained in:
Venelin
2024-04-26 18:50:25 +03:00
parent 6dbe37e07b
commit a8fb5f7d3d
12 changed files with 118 additions and 42 deletions
+25
View File
@@ -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"
+1
View File
@@ -1,5 +1,6 @@
#!/bin/sh #!/bin/sh
set -e set -e
cd src
# Install location # Install location
PREFIX=/usr/local PREFIX=/usr/local
Executable
+6
View File
@@ -0,0 +1,6 @@
#!/bin/sh
set -e
echo OK | cppipe test/c_file.c
echo ALL TESTS PASSED
View File
View File
View File
+76 -42
View File
@@ -3,11 +3,13 @@
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string_view>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include "commands.hpp" #include "commands.hpp"
#include "../config.h"
using namespace std; using namespace std;
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -22,6 +24,9 @@ struct MappedFile // todo: move to utils
unsigned len; 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 // find the cpp to run, if it doesn't exist, exit program
static fs::path find_path_to_cpp(string_view src_file); 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 // only recompile if changes are present
static void compile_src_file(); static void compile_src_file();
static void print_usage();
static bool is_src_file(string_view path);
static const char DEBUG_PREFIX[] = "__DBG"; static const char DEBUG_PREFIX[] = "__DBG";
// Context // Context
@@ -48,37 +57,10 @@ static fs::path bin; // cache bins to avoid recompiles
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
if(argc < 2) int src_arg = parse_args_until_src(argc, argv);
{
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;
}
// Init context // 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); 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();
@@ -94,11 +76,53 @@ int main(int argc, char* argv[])
} }
run += bin.c_str(); 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]; run += argv[i];
exec(run); 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) static fs::path find_path_to_cpp(string_view src_file)
{ {
if(fs::exists(src_file)) // found relative to CWD 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() static bool preprocess_and_compare()
{ {
Cmd preprocess( Cmd preprocess(
"g++", CXX,
"-E", // preprocess only "-E", // preprocess only
"-P", // don't generate linemarkers in the output to reduce file size "-P", // don't generate linemarkers in the output to reduce file size
src_file.c_str() 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 string preprocessed_file = cache_dir / src_file.stem() += ".ii"; // todo: duplicates with old_pp_path // todo: support C
Cmd compile( Cmd compile(
"g++", CXX,
preprocessed_file.c_str(), preprocessed_file.c_str(),
"-o", bin.c_str(), "-o", bin.c_str(),
"-pipe", "-std=c++17", "-march=native", CXXFLAGS
"-Wall", "-Wextra", "-Wno-parentheses",
"-lcppipe"
); );
if(debug) if(debug)
{ {
compile.append_args({ compile.append_args({ DEBUG_FLAGS });
"-g"
// "-Wall", "-Wextra", "-Wno-parentheses"
});
} }
else else
{ {
compile.append_args({ compile.append_args({ RELEASE_FLAGS });
"-Ofast", "-flto", "-s"
});
} }
if( !compile() ) // if failed to compile if( !compile() ) // if failed to compile
exit(1); 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;
}
+9
View File
@@ -0,0 +1,9 @@
#include <stdio.h>
int main()
{
char s[16];
scanf("%s", s);
printf("%s\n", s);
return 0;
}
View File
+1
View File
@@ -13,3 +13,4 @@ make portable
shaded obj shaded obj
uninstall uninstall
throw when a command couldnt be ran throw when a command couldnt be ran
improve debug (the src file shown is the .ii)