Files
wizapp-stdlib/build.c

126 lines
3.9 KiB
C
Executable File

#include "aliases.h"
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#define COMMAND_BUFFER_SIZE 8192
typedef enum {
BUILD_TYPE_DEBUG,
BUILD_TYPE_RELEASE,
} BuildType;
#ifdef RELEASE
wapp_intern BuildType build_type = BUILD_TYPE_RELEASE;
#else
wapp_intern BuildType build_type = BUILD_TYPE_DEBUG;
#endif /* ifdef RELEASE */
wapp_intern int run_command(const char *cmd);
wapp_intern const char *test_include_directories = "-I./tests/ "
"-I./tests/arena/";
wapp_intern const char *test_source_files = "./tests/arena/test_arena.c "
"./tests/wapptest.c";
wapp_intern const char *lib_include_directories = "-I./src/ "
"-I./src/aliases/ "
"-I./src/cpath/ "
"-I./src/mem/ "
"-I./src/mem/arena/ "
"-I./src/mem/util/ "
"-I./src/misc/ "
"-I./src/platform/ "
"-I./src/strings/ "
"-I./src/strings/dstr/ "
"-I./src/strings/basic_strings/ "
"-I./src/termcolour/ "
"-I./src/tester/";
wapp_intern const char *lib_source_files = "./src/cpath/cpath.c "
"./src/mem/arena/mem_arena.c "
"./src/mem/util/mem_utils.c "
"./src/strings/dstr/dstr.c "
"./src/termcolour/termcolour.c "
"./src/tester/tester.c";
#ifdef WAPP_PLATFORM_POSIX
int main(void) {
const char *compiler = "clang";
const char *cflags = "-Wall -Werror -pedantic";
const char *libflags = "-fPIC -shared";
const char *build_flags = "";
const char *test_out_name = "wapptest";
const char *lib_out_name = "wapp.so";
switch (build_type) {
case BUILD_TYPE_DEBUG:
build_flags = "-g -fsanitize=address -fsanitize=undefined";
break;
case BUILD_TYPE_RELEASE:
build_flags = "-O3";
break;
}
char cmd[COMMAND_BUFFER_SIZE] = {0};
snprintf(cmd, COMMAND_BUFFER_SIZE - 1, "%s %s %s %s %s %s %s -o %s", compiler,
cflags, build_flags, lib_include_directories,
test_include_directories, lib_source_files, test_source_files,
test_out_name);
int status = run_command(cmd);
if (status != EXIT_SUCCESS) {
fprintf(stderr, "Failed to build tests\n");
return EXIT_FAILURE;
}
printf("Running tests\n");
snprintf(cmd, COMMAND_BUFFER_SIZE - 1, "./%s", test_out_name);
status = run_command(cmd);
if (status != EXIT_SUCCESS) {
fprintf(stderr, "Tests failed\n");
return EXIT_FAILURE;
}
snprintf(cmd, COMMAND_BUFFER_SIZE - 1, "%s %s %s %s %s %s -o %s", compiler,
cflags, build_flags, libflags, lib_include_directories,
lib_source_files, lib_out_name);
status = run_command(cmd);
if (status != EXIT_SUCCESS) {
fprintf(stderr, "Failed to build library\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#elif defined(WAPP_PLATFORM_WINDOWS)
#error "Windows build unimplemented"
#else
#error "Unrecognised platform"
#endif /* ifdef WAPP_PLATFORM_POSIX */
wapp_intern int run_command(const char *cmd) {
printf("%s\n", cmd);
FILE *fp = popen(cmd, "r");
if (!fp) {
fprintf(stderr, "Failed to run tests\n");
return EXIT_FAILURE;
}
char out[4096];
while (fgets(out, 4095, fp)) {
printf("%s", out);
}
int st = pclose(fp);
if (!WIFEXITED(st)) {
return EXIT_FAILURE;
}
return WEXITSTATUS(st);
}