Skip to content
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

Go Redefine Urself™ - user configs #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sqaxomonophonen
Copy link

@sqaxomonophonen sqaxomonophonen commented Jan 8, 2025

Hack that exploits Go Rebuild Urself™ Technology to support user configs. Demo:

#define NOB_IMPLEMENTATION
#include "nob.h"

#ifndef FOO
#define FOO "default foo"
#endif

#ifndef BAR
#define BAR "default bar"
#endif

int main(int argc, char **argv)
{
   NOB_GO_DEFINE(FOO); 
   NOB_GO_DEFINE(BAR);
   NOB_GO_REBUILD_URSELF(argc, argv);
   for (int i=1; i<argc; ++i) {
      char* arg = argv[i];
      if (strlen(argv[i]) >= 4 && 0 == memcmp(arg, "FOO=", 4)) NOB_GO_REDEFINE(FOO, arg+4);
      if (strlen(argv[i]) >= 4 && 0 == memcmp(arg, "BAR=", 4)) NOB_GO_REDEFINE(BAR, arg+4);
   }
   printf("FOO=[%s] BAR=[%s]\n", FOO, BAR);
}

Then ./nob FOO=69 rebuilds with -DFOO="69" (or /DFOO="69" if cl.exe — not tested) unless already defined as "69". It also works with ./nob FOO=69 BAR=96 but recompiles twice if both values are new. ./nob FOO=69 FOO=96 enters an infinte recompile loop and deletes itself (Go Delete Urself™).

allows redefines inside macros:

   #define CFG(x) if (strlen(argv[i]) >= (1+strlen(#x)) && 0 == memcmp(arg, #x "=", (1+strlen(#x)))) NOB_GO_REDEFINE_STR(#x, arg+(1+strlen(#x)));
   CFG(FOO)
   CFG(BAR)
   #undef CFG

the other macro (NOB_GO_REDEFINE) doesn't do The Right Thing (tm) whether you
use `#x` or `x`
allows stuff like

  #ifndef FOO
  #define FOO "FOO default"
  #endif

  #ifndef BAR
  #define BAR "BAR default"
  #endif

  #define EMIT_CFGS \
    X(FOO) \
    X(BAR)

  int main(int argc, char **argv)
  {
    #define X(x) nob_go_define(#x, x);
    EMIT_CFGS
    #undef X
    NOB_GO_REBUILD_URSELF(argc, argv);

    #define X(x) printf("%s=\"%s\"\n", #x, x);
    EMIT_CFGS
    #undef X

    ...

    char* arg = argv[i];
    char* val = argv[i+1];
    #define X(x) if (strcmp(arg,#x)==0) { match=1; NOB_GO_REDEFINE_STR(#x, val); }
    EMIT_CFGS
    #undef X

would be nice to also allow
  #define EMIT_CFGS \
    X(FOO, "FOO default") \
    X(BAR, "BAR default")
but I don't think it's possible? (you can't have #ifndef/#define/#endif inside
a #define)
@sqaxomonophonen
Copy link
Author

I made it X-macro friendlier so it's possible to avoid a lot of config key duplication:

#define NOB_IMPLEMENTATION
#include "nob.h"

#ifndef FOO
#define FOO "default FOO"
#endif

#ifndef BAR
#define BAR "default BAR"
#endif

#define EMIT_CONFIGS \
   X(FOO) \
   X(BAR)

int main(int argc, char **argv)
{
   #define X(x) nob_go_define(#x, x);
   EMIT_CONFIGS
   #undef X
   NOB_GO_REBUILD_URSELF(argc, argv);
   for (int i=1; i<argc; ++i) {
      char* arg = argv[i];
      #define X(x) if (strlen(argv[i]) >= (1+strlen(#x)) && 0 == memcmp(arg, #x "=", (1+strlen(#x)))) NOB_GO_REDEFINE_STR(#x, arg+1+strlen(#x));
      EMIT_CONFIGS
      #undef X
   }
   #define X(x) printf("%s=\"%s\"\n", #x, x);
   EMIT_CONFIGS
   #undef X
   return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant