Skip to content

Commit

Permalink
Use genf/fprintf instead of nob_sb_append_cstr
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Jun 10, 2024
1 parent b079e10 commit 80a284f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
25 changes: 18 additions & 7 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,32 @@ 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);
generate_default_config(&content);
if (!nob_write_entire_file(CONFIG_PATH, content.items, content.count)) return 1;
FILE *f = fopen(CONFIG_PATH, "wb");
if (f == NULL) {
nob_log(NOB_ERROR, "Could not generate %s: %s", CONFIG_PATH, strerror(errno));
return 1;
}
generate_default_config(f);
fclose(f);
} 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;
const char *config_logger_path = "build/config_logger.c";
nob_log(NOB_INFO, "Generating %s", config_logger_path);
{
FILE *f = fopen(config_logger_path, "wb");
if (f == NULL) {
nob_log(NOB_ERROR, "Could not generate %s: %s", config_logger_path, strerror(errno));
return 1;
}
generate_config_logger(f);
fclose(f);
}

Nob_Cmd cmd = {0};
const char *stage2_binary = "build/nob_stage2";
Expand Down
53 changes: 26 additions & 27 deletions src_build/configurer.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,53 +78,52 @@ static Feature_Flag feature_flags[] = {
},
};

void generate_default_config(Nob_String_Builder *content)
#define genf(out, ...) \
do { \
fprintf((out), __VA_ARGS__); \
fprintf((out), " // %s:%d\n", __FILE__, __LINE__); \
} while(0)

void generate_default_config(FILE *f)
{
nob_sb_append_cstr(content, "//// Build target. Pick only one!\n");
fprintf(f, "//// 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 ");
fprintf(f, "#define %s\n", target_flags[i].macro);
} else {
nob_sb_append_cstr(content, "// #define ");
fprintf(f, "// #define %s\n", target_flags[i].macro);
}
nob_sb_append_cstr(content, target_flags[i].macro);
nob_sb_append_cstr(content, "\n");
}

nob_sb_append_cstr(content, "\n");
fprintf(f, "");

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");
fprintf(f, "//// %s\n", feature_flags[i].description);
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 ");
fprintf(f, "#define %s\n", feature_flags[i].macro);
#else
nob_sb_append_cstr(content, "// #define ");
fprintf(f, "// #define %s\n", feature_flags[i].macro);
#endif
} else {
nob_sb_append_cstr(content, "// #define ");
fprintf(f, "// #define %s\n", feature_flags[i].macro);
}
nob_sb_append_cstr(content, feature_flags[i].macro);
nob_sb_append_cstr(content, "\n");
nob_sb_append_cstr(content, "\n");
fprintf(f, "\n");
}
}

void generate_config_logger(Nob_String_Builder *content)
void generate_config_logger(FILE *f)
{
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");
genf(f, "void log_config(Nob_Log_Level level)");
genf(f, "{");
genf(f, " nob_log(level, \"Target: %%s\", MUSIALIZER_TARGET_NAME);");
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");
genf(f, " #ifdef %s", feature_flags[i].macro);
genf(f, " nob_log(level, \"%s: ENABLED\");", feature_flags[i].display);
genf(f, " #else");
genf(f, " nob_log(level, \"%s: DISABLED\");", feature_flags[i].display);
genf(f, " #endif");
}
nob_sb_append_cstr(content, "}\n");
genf(f, "}");
}
6 changes: 0 additions & 6 deletions src_build/nob_stage2.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ void log_available_subcommands(const char *program, Nob_Log_Level level)
nob_log(level, " help");
}

#define genf(out, ...) \
do { \
fprintf((out), __VA_ARGS__); \
fprintf((out), " // %s:%d\n", __FILE__, __LINE__); \
} while(0)

typedef struct {
const char *file_path;
size_t offset;
Expand Down

0 comments on commit 80a284f

Please sign in to comment.