From 33baa9a27b9509a2539c755fbcc848af26cb4198 Mon Sep 17 00:00:00 2001 From: Ben <68027587+cello-ben@users.noreply.github.com> Date: Tue, 29 Apr 2025 07:33:56 -0400 Subject: [PATCH 1/3] Switch prefix stripping to opt-in --- nob.c | 51 ++++++++++++++++++++++++++------------------------- nob.h | 54 ++++++++++++++++++++++++++++++++++-------------------- shared.h | 7 ++++--- 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/nob.c b/nob.c index 912e05f..2fa7f8a 100644 --- a/nob.c +++ b/nob.c @@ -1,6 +1,6 @@ #include "shared.h" #define NOB_IMPLEMENTATION -#define NOB_STRIP_PREFIX +#define NOB_ADD_PREFIX #define NOB_EXPERIMENTAL_DELETE_OLD #include "nob.h" @@ -20,30 +20,30 @@ const char *test_names[] = { "sb_appendf", "da_foreach", }; -#define test_names_count ARRAY_LEN(test_names) +#define test_names_count NOB_ARRAY_LEN(test_names) -bool build_and_run_test(Cmd *cmd, const char *test_name) +bool build_and_run_test(Nob_Cmd *cmd, const char *test_name) { - size_t mark = temp_save(); + size_t mark = nob_temp_save(); - const char *bin_path = temp_sprintf("%s%s", BUILD_FOLDER TESTS_FOLDER, test_name); - const char *src_path = temp_sprintf("%s%s.c", TESTS_FOLDER, test_name); + const char *bin_path = nob_temp_sprintf("%s%s", BUILD_FOLDER TESTS_FOLDER, test_name); + const char *src_path = nob_temp_sprintf("%s%s.c", TESTS_FOLDER, test_name); nob_cc(cmd); nob_cc_flags(cmd); nob_cc_output(cmd, bin_path); nob_cc_inputs(cmd, src_path); - if (!cmd_run_sync_and_reset(cmd)) return false; + if (!nob_cmd_run_sync_and_reset(cmd)) return false; - const char *test_cwd_path = temp_sprintf("%s%s%s.cwd", BUILD_FOLDER, TESTS_FOLDER, test_name); - if (!mkdir_if_not_exists(test_cwd_path)) return false; - if (!set_current_dir(test_cwd_path)) return false; - cmd_append(cmd, temp_sprintf("../%s", test_name)); - if (!cmd_run_sync_and_reset(cmd)) return false; - if (!set_current_dir("../../../")) return false; + const char *test_cwd_path = nob_temp_sprintf("%s%s%s.cwd", BUILD_FOLDER, TESTS_FOLDER, test_name); + if (!nob_mkdir_if_not_exists(test_cwd_path)) return false; + if (!nob_set_current_dir(test_cwd_path)) return false; + nob_cmd_append(cmd, nob_temp_sprintf("../%s", test_name)); + if (!nob_cmd_run_sync_and_reset(cmd)) return false; + if (!nob_set_current_dir("../../../")) return false; - nob_log(INFO, "--- %s finished ---", bin_path); + nob_log(NOB_INFO, "--- %s finished ---", bin_path); - temp_rewind(mark); + nob_temp_rewind(mark); return true; } @@ -51,14 +51,14 @@ int main(int argc, char **argv) { NOB_GO_REBUILD_URSELF_PLUS(argc, argv, "nob.h", "shared.h"); - Cmd cmd = {0}; + Nob_Cmd cmd = {0}; - const char *program_name = shift(argv, argc); + const char *program_name = nob_shift(argv, argc); const char *command_name = "test"; - if (argc > 0) command_name = shift(argv, argc); + if (argc > 0) command_name = nob_shift(argv, argc); - if (!mkdir_if_not_exists(BUILD_FOLDER)) return 1; - if (!mkdir_if_not_exists(BUILD_FOLDER TESTS_FOLDER)) return 1; + if (!nob_mkdir_if_not_exists(BUILD_FOLDER)) return 1; + if (!nob_mkdir_if_not_exists(BUILD_FOLDER TESTS_FOLDER)) return 1; if (strcmp(command_name, "test") == 0) { if (argc <= 0) { @@ -69,21 +69,22 @@ int main(int argc, char **argv) } while (argc > 0) { - const char *test_name = shift(argv, argc); + const char *test_name = nob_shift(argv, argc); if (!build_and_run_test(&cmd, test_name)) return 1; } return 0; } if (strcmp(command_name, "list") == 0) { - nob_log(INFO, "Tests:"); + nob_log(NOB_INFO, "Tests:"); for (size_t i = 0; i < test_names_count; ++i) { - nob_log(INFO, " %s", test_names[i]); + nob_log(NOB_INFO, " %s", test_names[i]); } - nob_log(INFO, "Use %s test to run individual tests", program_name); + nob_log(NOB_INFO, "Use %s test to run individual tests", program_name); return 0; } - nob_log(ERROR, "Unknown command %s", command_name); + nob_log(NOB_ERROR, "Unknown command %s", command_name); return 1; } + diff --git a/nob.h b/nob.h index 8ab0cf5..a3695b1 100644 --- a/nob.h +++ b/nob.h @@ -124,17 +124,14 @@ All the Zoo of `nob_cmd_run_*` functions follows the same pattern: sync/async, redirect/no redirect, and_reset/no and_reset. They always come in that order. - # Stripping off `nob_` Prefixes + # Using `nob_` Prefixes - Since Pure C does not have any namespaces we prefix each name of the API with the `nob_` to avoid any - potential conflicts with any other names in your code. But sometimes it is very annoying and makes - the code noisy. If you know that none of the names from nob.h conflict with anything in your code - you can enable NOB_STRIP_PREFIX macro and just drop all the prefixes: + Pure C does not have any namespaces. In some projects, this is not an issue, assuming that no names conflict with those of anything in nob. However, if you do find conflicts, you can define NOB_ADD_PREFIX. This will prefix names with NOB_, Nob_, or nob_. + Without prefixes: ```c // nob.c #define NOB_IMPLEMENTATION - #define NOB_STRIP_PREFIX #include "nob.h" int main(int argc, char **argv) @@ -146,14 +143,29 @@ return 0; } ``` + With prefixes: + ```c + // nob.c + #define NOB_IMPLEMENTATION + #define NOB_ADD_PREFIX + #include "nob.h" + + int main(int argc, char **argv) + { + NOB_GO_REBUILD_URSELF(argc, argv); + Nob_Cmd cmd = {0}; + nob_cmd_append(&cmd, "cc", "-Wall", "-Wextra", "-o", "main", "main.c"); + if (!nob_cmd_run_sync(cmd)) return 1; + return 0; + } + ``` - Not all the names have strippable prefixes. All the redefinable names like `NOB_GO_REBUILD_URSELF` - for instance will retain their prefix even if NOB_STRIP_PREFIX is enabled. Notable exception is the - nob_log() function. Stripping away the prefix results in log() which was historically always referring - to the natural logarithmic function that is already defined in math.h. So there is no reason to strip - off the prefix for nob_log(). + Not all the names have optional prefixes. All the redefinable names like `NOB_GO_REBUILD_URSELF` + for instance will retain their prefix even if NOB_ADD_PREFIX is not defined. Notable exception is the + nob_log() function. Omitting the prefix results in log() which was historically always referring + to the natural logarithmic function that is already defined in math.h. So there is no reason to omit the prefix for nob_log(). - The prefixes are stripped off only on the level of preprocessor. The names of the functions in the + The prefixes are omitted only on the level of preprocessor. The names of the functions in the compiled object file will still retain the `nob_` prefix. Keep that in mind when you FFI with nob.h from other languages (for whatever reason). @@ -1881,15 +1893,15 @@ int closedir(DIR *dirp) #endif // NOB_IMPLEMENTATION -#ifndef NOB_STRIP_PREFIX_GUARD_ -#define NOB_STRIP_PREFIX_GUARD_ +#ifndef NOB_PREFIX_GUARD_ +#define NOB_PREFIX_GUARD_ // NOTE: The name stripping should be part of the header so it's not accidentally included // several times. At the same time, it should be at the end of the file so to not create any // potential conflicts in the NOB_IMPLEMENTATION. The header obviously cannot be at the end // of the file because NOB_IMPLEMENTATION needs the forward declarations from there. So the // solution is to split the header into two parts where the name stripping part is at the // end of the file after the NOB_IMPLEMENTATION. - #ifdef NOB_STRIP_PREFIX + #ifndef NOB_ADD_PREFIX #define TODO NOB_TODO #define UNREACHABLE NOB_UNREACHABLE #define UNUSED NOB_UNUSED @@ -1902,7 +1914,7 @@ int closedir(DIR *dirp) #define Log_Level Nob_Log_Level #define minimal_log_level nob_minimal_log_level // NOTE: Name log is already defined in math.h and historically always was the natural logarithmic function. - // So there should be no reason to strip the `nob_` prefix in this specific case. + // So there should be no reason to not include the `nob_` prefix in this specific case. // #define log nob_log #define shift nob_shift #define shift_args nob_shift_args @@ -1988,12 +2000,13 @@ int closedir(DIR *dirp) #define sv_from_parts nob_sv_from_parts #define sb_to_sv nob_sb_to_sv #define win32_error_message nob_win32_error_message - #endif // NOB_STRIP_PREFIX -#endif // NOB_STRIP_PREFIX_GUARD_ + #endif // NOB_ADD_PREFIX +#endif // NOB_PREFIX_GUARD_ /* Revision history: - + + 1.20.3 (2025-04-29) Reverse direction of prefix guard (opt-in rather than opt-out) (By @cello-ben) 1.20.2 (2025-04-24) Report the program name that failed to start up in nob_cmd_run_async_redirect() (By @rexim) 1.20.1 (2025-04-16) Use vsnprintf() in nob_sb_appendf() instead of vsprintf() (By @LainLayer) 1.20.0 (2025-04-16) Introduce nob_cc(), nob_cc_flags(), nob_cc_inputs(), nob_cc_output() macros (By @rexim) @@ -2064,7 +2077,7 @@ int closedir(DIR *dirp) Naming Conventions: - All the user facing names should be prefixed with `nob_` or `NOB_` depending on the case. - - The prefixes of non-redefinable names should be strippable with NOB_STRIP_PREFIX (unless + - The prefixes of non-redefinable names should be optional, and available via NOB_ADD_PREFIX (unless explicitly stated otherwise like in case of nob_log). - Internal functions should be prefixed with `nob__` (double underscore). */ @@ -2110,3 +2123,4 @@ int closedir(DIR *dirp) WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ + diff --git a/shared.h b/shared.h index 32f8229..dbc5ffc 100644 --- a/shared.h +++ b/shared.h @@ -9,13 +9,14 @@ #define TESTS_FOLDER "tests/" #if defined(_MSC_VER) -# define nob_cc_flags(cmd) cmd_append(cmd, "-I.") +# define nob_cc_flags(cmd) nob_cmd_append(cmd, "-I.") #elif defined(__APPLE__) || defined(__MACH__) // TODO: "-std=c99", "-D_POSIX_SOURCE" didn't work for MacOS, don't know why, don't really care that much at the moment. // Anybody who does feel free to investigate. -# define nob_cc_flags(cmd) cmd_append(cmd, "-Wall", "-Wextra", "-Wswitch-enum", "-I.") +# define nob_cc_flags(cmd) nob_cmd_append(cmd, "-Wall", "-Wextra", "-Wswitch-enum", "-I.") #else -# define nob_cc_flags(cmd) cmd_append(cmd, "-Wall", "-Wextra", "-Wswitch-enum", "-std=c99", "-D_POSIX_SOURCE", "-ggdb", "-I."); +# define nob_cc_flags(cmd) nob_cmd_append(cmd, "-Wall", "-Wextra", "-Wswitch-enum", "-std=c99", "-D_POSIX_SOURCE", "-ggdb", "-I."); #endif #endif // SHARED_H_ + From 3a4eb61e25df604f78df91cf94fbbba0619d1dd9 Mon Sep 17 00:00:00 2001 From: Ben <68027587+cello-ben@users.noreply.github.com> Date: Thu, 15 May 2025 21:56:54 -0400 Subject: [PATCH 2/3] Remove version number --- nob.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nob.h b/nob.h index a3695b1..17011bb 100644 --- a/nob.h +++ b/nob.h @@ -2005,8 +2005,7 @@ int closedir(DIR *dirp) /* Revision history: - - 1.20.3 (2025-04-29) Reverse direction of prefix guard (opt-in rather than opt-out) (By @cello-ben) + 1.20.2 (2025-04-24) Report the program name that failed to start up in nob_cmd_run_async_redirect() (By @rexim) 1.20.1 (2025-04-16) Use vsnprintf() in nob_sb_appendf() instead of vsprintf() (By @LainLayer) 1.20.0 (2025-04-16) Introduce nob_cc(), nob_cc_flags(), nob_cc_inputs(), nob_cc_output() macros (By @rexim) From 0c5501132a9a9e2d638b98e8c40efa8c40a6c79e Mon Sep 17 00:00:00 2001 From: Ben <68027587+cello-ben@users.noreply.github.com> Date: Thu, 15 May 2025 21:58:53 -0400 Subject: [PATCH 3/3] Resolve conflict --- nob.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nob.h b/nob.h index 17011bb..ad86431 100644 --- a/nob.h +++ b/nob.h @@ -160,10 +160,12 @@ } ``` - Not all the names have optional prefixes. All the redefinable names like `NOB_GO_REBUILD_URSELF` - for instance will retain their prefix even if NOB_ADD_PREFIX is not defined. Notable exception is the - nob_log() function. Omitting the prefix results in log() which was historically always referring - to the natural logarithmic function that is already defined in math.h. So there is no reason to omit the prefix for nob_log(). + Not all the names have strippable prefixes. All the redefinable names like `NOB_GO_REBUILD_URSELF` + for instance will retain their prefix even if NOB_STRIP_PREFIX is enabled. Notable exception is the + nob_log() function. Stripping away the prefix results in log() which was historically always referring + to the natural logarithmic function that is already defined in math.h. So there is no reason to strip + off the prefix for nob_log(). Another exception is nob_rename() which collides with the widely known + POSIX function rename(2) if you strip the prefix off. The prefixes are omitted only on the level of preprocessor. The names of the functions in the compiled object file will still retain the `nob_` prefix. Keep that in mind when you FFI with nob.h