Skip to content

Switch prefix stripping to opt-in #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions nob.c
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -20,45 +20,45 @@ 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;
}

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) {
Expand All @@ -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 <names...> to run individual tests", program_name);
nob_log(NOB_INFO, "Use %s test <names...> 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;
}

45 changes: 30 additions & 15 deletions nob.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -146,14 +143,31 @@
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().
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 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).

Expand Down Expand Up @@ -1881,15 +1895,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
Expand All @@ -1902,7 +1916,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
Expand Down Expand Up @@ -1988,8 +2002,8 @@ 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:
Expand Down Expand Up @@ -2064,7 +2078,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).
*/
Expand Down Expand Up @@ -2110,3 +2124,4 @@ int closedir(DIR *dirp)
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/

7 changes: 4 additions & 3 deletions shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_