Skip to content

Commit

Permalink
Merge pull request #116 from tsoding/configurer
Browse files Browse the repository at this point in the history
Factor out src_build/configurer.c
  • Loading branch information
rexim authored Jun 10, 2024
2 parents 9a617af + 18ae3d4 commit 3ef7b49
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 87 deletions.
66 changes: 7 additions & 59 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,7 @@

#define NOB_IMPLEMENTATION
#include "./nob.h"

#define CONFIG_PATH "./build/config.h"

void generate_default_config(Nob_String_Builder *content)
{
nob_sb_append_cstr(content, "//// Build target. Pick only one!\n");
#ifdef _WIN32
# if defined(_MSC_VER)
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_MACOS\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_OPENBSD\n");
# else
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_LINUX\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_MACOS\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_OPENBSD\n");
# endif
#elif defined (__APPLE__) || defined (__MACH__)
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET_MACOS\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_OPENBSD\n");
#elif defined(__OpenBSD__)
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_MACOS\n");
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET_OPENBSD\n");
#else
nob_sb_append_cstr(content, "#define MUSIALIZER_TARGET_LINUX\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MINGW\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_WIN64_MSVC\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_MACOS\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_TARGET_OPENBSD\n");
#endif

nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "//// Moves everything in src/plug.c to a separate \"DLL\" so it can be hotreloaded. Works only for Linux right now\n");
// TODO: FIX ASAP! This requires bootstrapping nob with additional flags which goes against its philosophy!
#ifdef MUSIALIZER_HOTRELOAD
nob_sb_append_cstr(content, "#define MUSIALIZER_HOTRELOAD\n");
#else
nob_sb_append_cstr(content, "// #define MUSIALIZER_HOTRELOAD\n");
#endif
nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "//// Don't bundle resources/ folder with the executable and load the resources directly from the folder.\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_UNBUNDLE\n");
nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "//// Unfinished feature that enables capturing sound from the mic.\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_MICROPHONE\n");
nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "//// Activate UI buttons on Press instead of Release just as John Carmack explained https://twitter.com/ID_AA_Carmack/status/1787850053912064005\n");
nob_sb_append_cstr(content, "// #define MUSIALIZER_ACT_ON_PRESS\n");
}
#include "./src_build/configurer.c"

int main(int argc, char **argv)
{
Expand All @@ -87,17 +30,22 @@ int main(int argc, char **argv)

if (!nob_mkdir_if_not_exists("build")) return 1;

Nob_String_Builder content = {0};
int config_exists = nob_file_exists(CONFIG_PATH);
if (config_exists < 0) return 1;
if (config_exists == 0) {
nob_log(NOB_INFO, "Generating %s", CONFIG_PATH);
Nob_String_Builder content = {0};
generate_default_config(&content);
if (!nob_write_entire_file(CONFIG_PATH, content.items, content.count)) return 1;
} else {
nob_log(NOB_INFO, "file `%s` already exists", CONFIG_PATH);
}

nob_log(NOB_INFO, "Generating build/config_logger.c");
content.count = 0;
generate_config_logger(&content);
if (!nob_write_entire_file("build/config_logger.c", content.items, content.count)) return 1;

Nob_Cmd cmd = {0};
const char *configured_binary = "build/nob.configured";
nob_cmd_append(&cmd, NOB_REBUILD_URSELF(configured_binary, "./src_build/nob_configured.c"));
Expand Down
130 changes: 130 additions & 0 deletions src_build/configurer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#define RAYLIB_VERSION "5.0"
#define CONFIG_PATH "./build/config.h"

typedef struct {
const char *macro;
bool enabled_by_default;
} Target_Flag;

static Target_Flag target_flags[] = {
{
.macro = "MUSIALIZER_TARGET_LINUX",
#if defined(linux) || defined(__linux) || defined(__linux__)
.enabled_by_default = true,
#else
.enabled_by_default = false,
#endif
},
{
.macro = "MUSIALIZER_TARGET_WIN64_MINGW",
#if (defined(WIN32) || defined(_WIN32)) && defined(__MINGW32__)
.enabled_by_default = true,
#else
.enabled_by_default = false,
#endif
},
{
.macro = "MUSIALIZER_TARGET_WIN64_MSVC",
#if (defined(WIN32) || defined(_WIN32)) && defined(_MSC_VER)
.enabled_by_default = true,
#else
.enabled_by_default = false,
#endif
},
{
.macro = "MUSIALIZER_TARGET_MACOS",
#if defined(__APPLE__) || defined(__MACH__)
.enabled_by_default = true,
#else
.enabled_by_default = false,
#endif
},
{
.macro = "MUSIALIZER_TARGET_OPENBSD",
#if defined(__OpenBSD__)
.enabled_by_default = true,
#else
.enabled_by_default = false,
#endif
},
};

typedef struct {
const char *display;
const char *macro;
const char *description;
} Feature_Flag;

static Feature_Flag feature_flags[] = {
{
.display = "Hotreload",
.macro = "MUSIALIZER_HOTRELOAD",
.description = "Moves everything in src/plug.c to a separate \"DLL\" so it can be hotreloaded.",
},
{
.display = "Unbundle",
.macro = "MUSIALIZER_UNBUNDLE",
.description = "Don't bundle resources/ folder with the executable and load the resources directly from the folder.",
},
{
.display = "Microphone",
.macro = "MUSIALIZER_MICROPHONE",
.description = "Unfinished feature that enables capturing sound from the mic."
},
{
.display = "Act on Press",
.macro = "MUSIALIZER_ACT_ON_PRESS",
.description = "Activate UI buttons on Press instead of Release just as John Carmack explained https://twitter.com/ID_AA_Carmack/status/1787850053912064005"
},
};

void generate_default_config(Nob_String_Builder *content)
{
nob_sb_append_cstr(content, "//// Build target. Pick only one!\n");
for (size_t i = 0; i < NOB_ARRAY_LEN(target_flags); ++i) {
if (target_flags[i].enabled_by_default) {
nob_sb_append_cstr(content, "#define ");
} else {
nob_sb_append_cstr(content, "// #define ");
}
nob_sb_append_cstr(content, target_flags[i].macro);
nob_sb_append_cstr(content, "\n");
}

nob_sb_append_cstr(content, "\n");

for (size_t i = 0; i < NOB_ARRAY_LEN(feature_flags); ++i) {
nob_sb_append_cstr(content, "//// ");
nob_sb_append_cstr(content, feature_flags[i].description);
nob_sb_append_cstr(content, "\n");
if (strcmp(feature_flags[i].macro, "MUSIALIZER_HOTRELOAD") == 0) {
// TODO: FIX ASAP! This requires bootstrapping nob with additional flags which goes against its philosophy!
#ifdef MUSIALIZER_HOTRELOAD
nob_sb_append_cstr(content, "#define ");
#else
nob_sb_append_cstr(content, "// #define ");
#endif
} else {
nob_sb_append_cstr(content, "// #define ");
}
nob_sb_append_cstr(content, feature_flags[i].macro);
nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "\n");
}
}

void generate_config_logger(Nob_String_Builder *content)
{
nob_sb_append_cstr(content, nob_temp_sprintf("// DO NOT MODIFY THIS FILE. It's auto-generated by %s and it's needed for logging the current configuration.\n", __FILE__));
nob_sb_append_cstr(content, "void log_config(Nob_Log_Level level)\n");
nob_sb_append_cstr(content, "{\n");
nob_sb_append_cstr(content, " nob_log(level, \"Target: %s\", MUSIALIZER_TARGET_NAME);\n");
for (size_t i = 0; i < NOB_ARRAY_LEN(feature_flags); ++i) {
nob_sb_append_cstr(content, nob_temp_sprintf(" #ifdef %s\n", feature_flags[i].macro));
nob_sb_append_cstr(content, nob_temp_sprintf(" nob_log(level, \"%s: ENABLED\");\n", feature_flags[i].display));
nob_sb_append_cstr(content, " #else\n");
nob_sb_append_cstr(content, nob_temp_sprintf(" nob_log(level, \"%s: DISABLED\");\n", feature_flags[i].display));
nob_sb_append_cstr(content, " #endif\n");
}
nob_sb_append_cstr(content, "}\n");
}
31 changes: 3 additions & 28 deletions src_build/nob_configured.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#define NOB_IMPLEMENTATION
#include "../nob.h"
#include "../build/config.h"

#define RAYLIB_VERSION "5.0"
#define CONFIG_PATH "./build/config.h"
#include "./configurer.c"

static const char *raylib_modules[] = {
"rcore",
Expand Down Expand Up @@ -37,6 +35,8 @@ static const char *raylib_modules[] = {
#error "No Musializer Target is defined. Check your ./build/config.h."
#endif // MUSIALIZER_TARGET

#include "../build/config_logger.c"

void log_available_subcommands(const char *program, Nob_Log_Level level)
{
nob_log(level, "Usage: %s [subcommand]", program);
Expand All @@ -47,31 +47,6 @@ void log_available_subcommands(const char *program, Nob_Log_Level level)
nob_log(level, " help");
}

void log_config(Nob_Log_Level level)
{
nob_log(level, "Target: %s", MUSIALIZER_TARGET_NAME);
#ifdef MUSIALIZER_HOTRELOAD
nob_log(level, "Hotreload: ENABLED");
#else
nob_log(level, "Hotreload: DISABLED");
#endif // MUSIALIZER_HOTRELOAD
#ifdef MUSIALIZER_MICROPHONE
nob_log(level, "Microphone: ENABLED");
#else
nob_log(level, "Microphone: DISABLED");
#endif // MUSIALIZER_MICROPHONE
#ifdef MUSIALIZER_UNBUNDLE
nob_log(level, "Unbundle: ENABLED");
#else
nob_log(level, "Unbundle: DISABLED");
#endif // MUSIALIZER_UNBUNDLE
#ifdef MUSIALIZER_ACT_ON_PRESS
nob_log(level, "Act on Press: ENABLED");
#else
nob_log(level, "Act on Press: DISABLED");
#endif // MUSIALIZER_ACT_ON_PRESS
}

#define genf(out, ...) \
do { \
fprintf((out), __VA_ARGS__); \
Expand Down

0 comments on commit 3ef7b49

Please sign in to comment.