浏览代码

Introduce `nob config` subcommand

rexim 6 月之前
父节点
当前提交
0e747e14cc
共有 4 个文件被更改,包括 43 次插入14 次删除
  1. 2 1
      .github/workflows/nob.yaml
  2. 27 0
      nob.c
  3. 13 13
      src_build/configurer.c
  4. 1 0
      src_build/nob_stage2.c

+ 2 - 1
.github/workflows/nob.yaml

@@ -52,11 +52,12 @@ jobs:
         # https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#create-your-own-command-prompt-shortcut
         run: |
           call "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat"
-          cl.exe ${{ matrix.hotreload == true && '/DMUSIALIZER_HOTRELOAD' || '' }} /Fenob nob.c
+          cl.exe /Fenob nob.c
       - name: Run nob
         shell: cmd
         run: |
           call "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat"
+          nob.exe config ${{ matrix.hotreload == true && 'hotreload || '' }}
           nob.exe
       - name: Upload build folder
         uses: actions/upload-artifact@v4

+ 27 - 0
nob.c

@@ -25,6 +25,33 @@ int main(int argc, char **argv)
 
     if (!nob_mkdir_if_not_exists("build")) return 1;
 
+    if (argc > 0) {
+        const char *command_name = shift(argv, argc);
+        if (strcmp(command_name, "config") == 0) {
+            // TODO: an ability to set target through the `config` command
+            while (argc > 0) {
+                const char *flag_name = shift(argv, argc);
+                bool found = false;
+                for (size_t i = 0; !found && i < ARRAY_LEN(feature_flags); ++i) {
+                    // TODO: an ability to disable flags that enabled by default
+                    if (strcmp(feature_flags[i].name, flag_name) == 0) {
+                        feature_flags[i].enabled_by_default = true;
+                        found = true;
+                    }
+                }
+                if (!found) {
+                    nob_log(ERROR, "Unknown command `%s`", flag_name);
+                    return 1;
+                }
+            }
+            if (!generate_default_config(CONFIG_PATH)) return 1;
+            return 0;
+        } else {
+            nob_log(ERROR, "Unknown command `%s`", command_name);
+            return 1;
+        }
+    }
+
     int config_exists = nob_file_exists(CONFIG_PATH);
     if (config_exists < 0) return 1;
     if (config_exists == 0) {

+ 13 - 13
src_build/configurer.c

@@ -51,25 +51,26 @@ static Target_Flag target_flags[] = {
 };
 
 typedef struct {
-    const char *display;
+    const char *name;
     const char *macro;
     const char *description;
+    bool enabled_by_default;
 } Feature_Flag;
 
 static Feature_Flag feature_flags[] = {
     {
-        .display = "Hotreload",
         .macro = "MUSIALIZER_HOTRELOAD",
+        .name = "hotreload",
         .description = "Moves everything in src/plug.c to a separate \"DLL\" so it can be hotreloaded.",
     },
     {
-        .display = "Unbundle",
         .macro = "MUSIALIZER_UNBUNDLE",
+        .name = "unbundle",
         .description = "Don't bundle resources/ folder with the executable and load the resources directly from the folder.",
     },
     {
-        .display = "Microphone",
         .macro = "MUSIALIZER_MICROPHONE",
+        .name = "microphone",
         .description = "Unfinished feature that enables capturing sound from the mic."
     },
 };
@@ -94,6 +95,7 @@ bool generate_default_config(const char *file_path)
         return false;
     }
 
+    // TODO: generate_default_config() should also log what platform it picked
     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) {
@@ -107,17 +109,15 @@ bool generate_default_config(const char *file_path)
 
     for (size_t i = 0; i < NOB_ARRAY_LEN(feature_flags); ++i) {
         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
-                fprintf(f, "#define %s\n", feature_flags[i].macro);
-            #else
-                fprintf(f, "// #define %s\n", feature_flags[i].macro);
-            #endif
+        if (feature_flags[i].enabled_by_default) {
+            nob_log(INFO, "%s: ENABLED", feature_flags[i].name);
+            fprintf(f, "#define %s\n", feature_flags[i].macro);
         } else {
+            nob_log(INFO, "%s: DISABLED", feature_flags[i].name);
             fprintf(f, "// #define %s\n", feature_flags[i].macro);
         }
         fprintf(f, "\n");
+
     }
 
     fclose(f);
@@ -138,9 +138,9 @@ bool generate_config_logger(const char *config_logger_path)
     genf(f, "    nob_log(level, \"Target: %%s\", MUSIALIZER_TARGET_NAME);");
     for (size_t i = 0; i < NOB_ARRAY_LEN(feature_flags); ++i) {
         genf(f, "    #ifdef %s", feature_flags[i].macro);
-        genf(f, "        nob_log(level, \"%s: ENABLED\");", feature_flags[i].display);
+        genf(f, "        nob_log(level, \"%s: ENABLED\");", feature_flags[i].name);
         genf(f, "    #else");
-        genf(f, "        nob_log(level, \"%s: DISABLED\");", feature_flags[i].display);
+        genf(f, "        nob_log(level, \"%s: DISABLED\");", feature_flags[i].name);
         genf(f, "    #endif");
     }
     genf(f, "}");

+ 1 - 0
src_build/nob_stage2.c

@@ -1,6 +1,7 @@
 #include <stdbool.h>
 
 #define NOB_IMPLEMENTATION
+#define NOB_STRIP_PREFIX
 #include "../thirdparty/nob.h"
 #include "../build/config.h"
 #include "./configurer.c"