Skip to content
This repository has been archived by the owner on Feb 12, 2025. It is now read-only.

Add possibility to use with C++ #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/cppexample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define NOBUILD_IMPLEMENTATION
#include "../nobuild.h"

#include <iostream>

int main(int argc, char **argv)
{
GO_REBUILD_URSELF(argc, argv);

std::cout << "Hello, world!" << std::endl;

return 0;
}
15 changes: 15 additions & 0 deletions nobuild.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
});
}

Expand Down
36 changes: 24 additions & 12 deletions nobuild.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)); \
Expand Down Expand Up @@ -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;
Expand All @@ -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';

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down