Skip to content

Commit

Permalink
Tuck up some file creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Jun 10, 2024
1 parent 80a284f commit 97c4965
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
30 changes: 7 additions & 23 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ int main(int argc, char **argv)
NOB_GO_REBUILD_URSELF(argc, argv);

const char *program = nob_shift_args(&argc, &argv);
const char *build_conf_path = "./build/build.conf";
int build_conf_exists = nob_file_exists(build_conf_path);

const char *old_build_conf_path = "./build/build.conf";
int build_conf_exists = nob_file_exists(old_build_conf_path);
if (build_conf_exists < 0) return 1;
if (build_conf_exists) {
// @backcomp
nob_log(NOB_ERROR, "We found %s. That means your build folder has an old schema.", build_conf_path);
nob_log(NOB_ERROR, "Instead of %s you are suppose to use %s to configure the build now.", build_conf_path, CONFIG_PATH);
nob_log(NOB_ERROR, "We found %s. That means your build folder has an old schema.", old_build_conf_path);
nob_log(NOB_ERROR, "Instead of %s you are suppose to use %s to configure the build now.", old_build_conf_path, CONFIG_PATH);
nob_log(NOB_ERROR, "Remove your ./build/ folder and run %s again to regenerate the folder with the new schema.", program);
return 1;
}
Expand All @@ -33,29 +34,12 @@ int main(int argc, char **argv)
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);
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);
if (!generate_default_config(CONFIG_PATH)) return 1;
} else {
nob_log(NOB_INFO, "file `%s` already exists", CONFIG_PATH);
}

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);
}
if (!generate_config_logger("build/config_logger.c")) return 1;

Nob_Cmd cmd = {0};
const char *stage2_binary = "build/nob_stage2";
Expand Down
26 changes: 23 additions & 3 deletions src_build/configurer.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@ static Feature_Flag feature_flags[] = {
fprintf((out), " // %s:%d\n", __FILE__, __LINE__); \
} while(0)

void generate_default_config(FILE *f)
bool generate_default_config(const char *file_path)
{
nob_log(NOB_INFO, "Generating %s", file_path);
FILE *f = fopen(file_path, "wb");
if (f == NULL) {
nob_log(NOB_ERROR, "Could not generate %s: %s", file_path, strerror(errno));
return false;
}

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) {
Expand All @@ -95,7 +102,7 @@ void generate_default_config(FILE *f)
}
}

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

for (size_t i = 0; i < NOB_ARRAY_LEN(feature_flags); ++i) {
fprintf(f, "//// %s\n", feature_flags[i].description);
Expand All @@ -111,10 +118,20 @@ void generate_default_config(FILE *f)
}
fprintf(f, "\n");
}

fclose(f);
return true;
}

void generate_config_logger(FILE *f)
bool generate_config_logger(const char *config_logger_path)
{
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 false;
}

genf(f, "void log_config(Nob_Log_Level level)");
genf(f, "{");
genf(f, " nob_log(level, \"Target: %%s\", MUSIALIZER_TARGET_NAME);");
Expand All @@ -126,4 +143,7 @@ void generate_config_logger(FILE *f)
genf(f, " #endif");
}
genf(f, "}");

fclose(f);
return true;
}

0 comments on commit 97c4965

Please sign in to comment.