Skip to content

Commit

Permalink
Merge remote-tracking branch 'LainLayer/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Feb 24, 2024
2 parents c880a2a + 8cf74b1 commit 18b45e6
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Before using `nob` you need to bootstrap it. Just compile it with the available

I really recommend to read [nob.c](./nob.c) and [nob.h](./src/nob.h) to get an idea of how it all actually works. The Build System is a work in progress, so if something breaks be ready to dive into it.

### Linux
### Linux and OpenBSD

```console
$ cc -o nob nob.c # ONLY ONCE!!!
Expand Down Expand Up @@ -86,7 +86,7 @@ $ wine ./build/musializer.exe

## Hot Reloading

**Only on Linux for now**
**Only on Linux and OpenBSD for now**

Edit `./build/config.h` and enable `MUSIALIZER_HOTRELOAD`.

Expand Down
26 changes: 22 additions & 4 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

#define RAYLIB_VERSION "5.0"

#define TARGET_LINUX 0
#define TARGET_WIN64_MINGW 1
#define TARGET_WIN64_MSVC 2
#define TARGET_MACOS 3
#define TARGET_OPENBSD 4


static const char *raylib_modules[] = {
"rcore",
"raudio",
Expand All @@ -37,6 +44,8 @@ static const char *raylib_modules[] = {
#include "src/nob_win64_mingw.c"
#elif MUSIALIZER_TARGET == TARGET_WIN64_MSVC
#include "src/nob_win64_msvc.c"
#elif MUSIALIZER_TARGET == TARGET_OPENBSD
#include "src/nob_openbsd.c"
#endif // MUSIALIZER_TARGET

void log_available_subcommands(const char *program, Nob_Log_Level level)
Expand Down Expand Up @@ -283,25 +292,34 @@ void generate_default_config(Nob_String_Builder *content)
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_MACOS\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_OPENBSD\n");
# else
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_LINUX\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_MACOS\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_OPENBSD\n");
# endif
#else
# if defined (__APPLE__) || defined (__MACH__)
#elif defined (__APPLE__) || defined (__MACH__)
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET TARGET_MACOS\n");
# else
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_OPENBSD\n");
#elif defined(__OpenBSD__)
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_MACOS\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET TARGET_OPENBSD\n");
#else
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_MACOS\n");
# endif
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET TARGET_OPENBSD\n");
#endif

nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "//// Moves everything in src/plub.c to a separate \"DLL\" so it can be hotreloaded. Works only for Linux right now\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_HOTRELOAD\n");
Expand Down
166 changes: 166 additions & 0 deletions src/nob_openbsd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#define MUSIALIZER_TARGET_NAME "OpenBSD"

bool build_musializer(void)
{
bool result = true;
Nob_Cmd cmd = {0};
Nob_Procs procs = {0};

#ifdef MUSIALIZER_HOTRELOAD
procs.count = 0;
cmd.count = 0;
// TODO: add a way to replace `cc` with something else GCC compatible on POSIX
// Like `clang` for instance
nob_cmd_append(&cmd, "cc");
nob_cmd_append(&cmd, "-Wall", "-Wextra", "-ggdb");
nob_cmd_append(&cmd, "-I./build/");
nob_cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src/");
nob_cmd_append(&cmd, "-fPIC", "-shared");
nob_cmd_append(&cmd, "-o", "./build/libplug.so");
nob_cmd_append(&cmd,
"./src/plug.c",
"./src/ffmpeg_linux.c");
nob_cmd_append(&cmd,
nob_temp_sprintf("-L./build/raylib/%s", MUSIALIZER_TARGET_NAME),
"-l:libraylib.so");
nob_cmd_append(&cmd, "-lm", "-lpthread");
nob_da_append(&procs, nob_cmd_run_async(cmd));

cmd.count = 0;
nob_cmd_append(&cmd, "cc");
nob_cmd_append(&cmd, "-Wall", "-Wextra", "-ggdb");
nob_cmd_append(&cmd, "-I./build/");
nob_cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src/");
nob_cmd_append(&cmd, "-o", "./build/musializer");
nob_cmd_append(&cmd,
"./src/musializer.c",
"./src/hotreload_linux.c");
nob_cmd_append(&cmd,
"-Wl,-rpath=./build/",
"-Wl,-rpath=./",
nob_temp_sprintf("-Wl,-rpath=./build/raylib/%s", MUSIALIZER_TARGET_NAME),
// NOTE: just in case somebody wants to run musializer from within the ./build/ folder
nob_temp_sprintf("-Wl,-rpath=./raylib/%s", MUSIALIZER_TARGET_NAME));
nob_cmd_append(&cmd,
nob_temp_sprintf("-L./build/raylib/%s", MUSIALIZER_TARGET_NAME),
"-l:libraylib.so");
nob_cmd_append(&cmd, "-lm", "-lpthread");
nob_da_append(&procs, nob_cmd_run_async(cmd));
if (!nob_procs_wait(procs)) nob_return_defer(false);
#else
cmd.count = 0;
nob_cmd_append(&cmd, "cc");
nob_cmd_append(&cmd, "-Wall", "-Wextra", "-ggdb");
nob_cmd_append(&cmd, "-I./build/");
nob_cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src/");
nob_cmd_append(&cmd, "-o", "./build/musializer");
nob_cmd_append(&cmd,
"./src/plug.c",
"./src/ffmpeg_linux.c",
"./src/musializer.c");
nob_cmd_append(&cmd,
nob_temp_sprintf("-L./build/raylib/%s", MUSIALIZER_TARGET_NAME),
"-l:libraylib.a");
nob_cmd_append(&cmd, "-lm", "-lpthread");
if (!nob_cmd_run_sync(cmd)) nob_return_defer(false);
#endif // MUSIALIZER_HOTRELOAD

defer:
nob_cmd_free(cmd);
nob_da_free(procs);
return result;
}

bool build_raylib(void)
{
bool result = true;
Nob_Cmd cmd = {0};
Nob_File_Paths object_files = {0};

if (!nob_mkdir_if_not_exists("./build/raylib")) {
nob_return_defer(false);
}

Nob_Procs procs = {0};

const char *build_path = nob_temp_sprintf("./build/raylib/%s", MUSIALIZER_TARGET_NAME);

if (!nob_mkdir_if_not_exists(build_path)) {
nob_return_defer(false);
}

for (size_t i = 0; i < NOB_ARRAY_LEN(raylib_modules); ++i) {
const char *input_path = nob_temp_sprintf("./raylib/raylib-"RAYLIB_VERSION"/src/%s.c", raylib_modules[i]);
const char *output_path = nob_temp_sprintf("%s/%s.o", build_path, raylib_modules[i]);
output_path = nob_temp_sprintf("%s/%s.o", build_path, raylib_modules[i]);

nob_da_append(&object_files, output_path);

if (nob_needs_rebuild(output_path, &input_path, 1)) {
cmd.count = 0;
nob_cmd_append(&cmd, "cc");
nob_cmd_append(&cmd, "-ggdb", "-DPLATFORM_DESKTOP", "-fPIC");
nob_cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src/external/glfw/include");
nob_cmd_append(&cmd, "-c", input_path);
nob_cmd_append(&cmd, "-o", output_path);
nob_cmd_append(&cmd, "-I/usr/X11R6/include");
Nob_Proc proc = nob_cmd_run_async(cmd);
nob_da_append(&procs, proc);
}
}
cmd.count = 0;

if (!nob_procs_wait(procs)) nob_return_defer(false);

#ifndef MUSIALIZER_HOTRELOAD
const char *libraylib_path = nob_temp_sprintf("%s/libraylib.a", build_path);

if (nob_needs_rebuild(libraylib_path, object_files.items, object_files.count)) {
nob_cmd_append(&cmd, "ar", "-crs", libraylib_path);
for (size_t i = 0; i < NOB_ARRAY_LEN(raylib_modules); ++i) {
const char *input_path = nob_temp_sprintf("%s/%s.o", build_path, raylib_modules[i]);
nob_cmd_append(&cmd, input_path);
}
if (!nob_cmd_run_sync(cmd)) nob_return_defer(false);
}
#else
const char *libraylib_path = nob_temp_sprintf("%s/libraylib.so", build_path);

if (nob_needs_rebuild(libraylib_path, object_files.items, object_files.count)) {
nob_cmd_append(&cmd, "cc");
nob_cmd_append(&cmd, "-shared");
nob_cmd_append(&cmd, "-o", libraylib_path);
for (size_t i = 0; i < NOB_ARRAY_LEN(raylib_modules); ++i) {
const char *input_path = nob_temp_sprintf("%s/%s.o", build_path, raylib_modules[i]);
nob_cmd_append(&cmd, input_path);
}
if (!nob_cmd_run_sync(cmd)) nob_return_defer(false);
}
#endif // MUSIALIZER_HOTRELOAD

defer:
nob_cmd_free(cmd);
nob_da_free(object_files);
return result;
}

bool build_dist()
{
#ifdef MUSIALIZER_HOTRELOAD
nob_log(NOB_ERROR, "We do not ship with hotreload enabled");
return false;
#else
if (!nob_mkdir_if_not_exists("./musializer-openbsd-x86_64/")) return false;
if (!nob_copy_file("./build/musializer", "./musializer-openbsd-x86_64/musializer")) return false;
if (!nob_copy_directory_recursively("./resources/", "./musializer-openbsd-x86_64/resources/")) return false;
// TODO: should we pack ffmpeg with Linux build?
// There are some static executables for Linux
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "tar", "fvcz", "./musializer-openbsd-x86_64.tar.gz", "./musializer-openbsd-x86_64");
bool ok = nob_cmd_run_sync(cmd);
nob_cmd_free(cmd);
if (!ok) return false;

return true;
#endif // MUSIALIZER_HOTRELOAD
}

0 comments on commit 18b45e6

Please sign in to comment.