diff --git a/examples/cppexample.cpp b/examples/cppexample.cpp new file mode 100644 index 0000000..b9d939d --- /dev/null +++ b/examples/cppexample.cpp @@ -0,0 +1,13 @@ +#define NOBUILD_IMPLEMENTATION +#include "../nobuild.h" + +#include + +int main(int argc, char **argv) +{ + GO_REBUILD_URSELF(argc, argv); + + std::cout << "Hello, world!" << std::endl; + + return 0; +} diff --git a/nobuild.c b/nobuild.c index a9cbd9c..4128530 100644 --- a/nobuild.c +++ b/nobuild.c @@ -2,6 +2,7 @@ #include "./nobuild.h" #define CFLAGS "-Wall", "-Wextra", "-std=c99", "-pedantic" +#define CXXFLAGS "-Wall", "-Wextra", "-std=c++11", "-pedantic" void build_tool(const char *tool) { @@ -33,12 +34,26 @@ void run_example(const char *example) CMD(NOEXT(example_path)); } +void run_example_cxx(const char *example) +{ + Cstr example_path = PATH("examples", example); +#ifndef _WIN32 + CMD("c++", CXXFLAGS, "-o", NOEXT(example_path), example_path); +#else + CMD("cl.exe", "/std:c++20", "/Fe.\\examples\\", example_path); +#endif + CMD(NOEXT(example_path)); +} + void run_examples(void) { FOREACH_FILE_IN_DIR(example, "examples", { if (ENDS_WITH(example, ".c")) { run_example(example); } + if (ENDS_WITH(example, ".cpp")) { + run_example_cxx(example); + } }); } diff --git a/nobuild.h b/nobuild.h index 88da48d..e9b2aab 100644 --- a/nobuild.h +++ b/nobuild.h @@ -203,17 +203,29 @@ void chain_echo(Chain chain); chain_run_sync(chain); \ } while(0) +#ifdef __cplusplus +#define NOBUILD_GCC "g++" +#define NOBUILD_CLANG "clang++" +#define NOBUILD_MSC "cl.exe" +#define NOBUILD_CC "c++" +#else +#define NOBUILD_GCC "gcc" +#define NOBUILD_CLANG "clang" +#define NOBUILD_MSC "cl.exe" +#define NOBUILD_CC "cc" +#endif + #ifndef REBUILD_URSELF # if _WIN32 # if defined(__GNUC__) -# define REBUILD_URSELF(binary_path, source_path) CMD("gcc", "-o", binary_path, source_path) +# define REBUILD_URSELF(binary_path, source_path) CMD(NOBUILD_GCC, "-o", binary_path, source_path) # elif defined(__clang__) -# define REBUILD_URSELF(binary_path, source_path) CMD("clang", "-o", binary_path, source_path) +# define REBUILD_URSELF(binary_path, source_path) CMD(NOBUILD_CLANG, "-o", binary_path, source_path) # elif defined(_MSC_VER) -# define REBUILD_URSELF(binary_path, source_path) CMD("cl.exe", source_path) +# define REBUILD_URSELF(binary_path, source_path) CMD(NOBUILD_MSC, source_path) # endif # else -# define REBUILD_URSELF(binary_path, source_path) CMD("cc", "-o", binary_path, source_path) +# define REBUILD_URSELF(binary_path, source_path) CMD(NOBUILD_CC, "-o", binary_path, source_path) # endif #endif @@ -251,7 +263,7 @@ void chain_echo(Chain chain); Cmd cmd = { \ .line = { \ .elems = (Cstr*) argv, \ - .count = argc, \ + .count = (size_t)argc, \ }, \ }; \ INFO("CMD: %s", cmd_show(cmd)); \ @@ -440,7 +452,7 @@ Cstr_Array cstr_array_append(Cstr_Array cstrs, Cstr cstr) Cstr_Array result = { .count = cstrs.count + 1 }; - result.elems = malloc(sizeof(result.elems[0]) * result.count); + result.elems = (Cstr*)malloc(sizeof(result.elems[0]) * result.count); memcpy(result.elems, cstrs.elems, cstrs.count * sizeof(result.elems[0])); result.elems[cstrs.count] = cstr; return result; @@ -462,7 +474,7 @@ Cstr cstr_no_ext(Cstr path) } if (n > 0) { - char *result = malloc(n); + char *result = (char *)malloc(n); memcpy(result, path, n); result[n - 1] = '\0'; @@ -491,7 +503,7 @@ Cstr_Array cstr_array_make(Cstr first, ...) } va_end(args); - result.elems = malloc(sizeof(result.elems[0]) * result.count); + result.elems = (Cstr*)malloc(sizeof(result.elems[0]) * result.count); if (result.elems == NULL) { PANIC("could not allocate memory: %s", strerror(errno)); } @@ -523,7 +535,7 @@ Cstr cstr_array_join(Cstr sep, Cstr_Array cstrs) } const size_t result_len = (cstrs.count - 1) * sep_len + len + 1; - char *result = malloc(sizeof(char) * result_len); + char *result = (char *)malloc(sizeof(char) * result_len); if (result == NULL) { PANIC("could not allocate memory: %s", strerror(errno)); } @@ -836,7 +848,7 @@ Chain chain_build_from_tokens(Chain_Token first, ...) } va_end(args); - result.cmds.elems = malloc(sizeof(result.cmds.elems[0]) * result.cmds.count); + result.cmds.elems = (Cmd *)malloc(sizeof(result.cmds.elems[0]) * result.cmds.count); if (result.cmds.elems == NULL) { PANIC("could not allocate memory: %s", strerror(errno)); } @@ -861,7 +873,7 @@ void chain_run_sync(Chain chain) return; } - Pid *cpids = malloc(sizeof(Pid) * chain.cmds.count); + Pid *cpids = (Pid *)malloc(sizeof(Pid) * chain.cmds.count); Pipe pip = {0}; Fd fdin = 0; @@ -1049,7 +1061,7 @@ void path_mkdirs(Cstr_Array path) size_t seps_count = path.count - 1; const size_t sep_len = strlen(PATH_SEP); - char *result = malloc(len + seps_count * sep_len + 1); + char *result = (char *)malloc(len + seps_count * sep_len + 1); len = 0; for (size_t i = 0; i < path.count; ++i) {