From: Marc Espie Subject: make: -j without params To: tech@openbsd.org Date: Sat, 22 Feb 2025 16:27:38 +0100 I had quite a bit of fun with students this week, and stumbled upon getopt :: extension. Figured out, it would be obvious to do in make. I looked at the others (very few instances) of tools that use :: to discern a pattern: in general, the consensus is that if we get an optarg, we whine if it doesn't match our expectation. So here's a patch to have make -j use hw.ncpuonlines if there's no optarg. Might come in handy for people compiling on several machines who don't want to remember the details. Wrt posix's compatibility, opengroup doesn't say anything. And gmake -j does something (doesn't limit the number of jobs, from the doc) So, handy ? crazy ? not needed ? It's a very straightforward patch with an obvious failure path. Index: main.c =================================================================== RCS file: /build/data/openbsd/cvs/src/usr.bin/make/main.c,v diff -u -p -r1.133 main.c --- main.c 18 Jun 2024 02:11:03 -0000 1.133 +++ main.c 22 Feb 2025 10:06:37 -0000 @@ -37,6 +37,7 @@ #include /* MACHINE MACHINE_ARCH */ #include +#include #include #include #include @@ -192,7 +193,7 @@ MainParseArgs(int argc, char **argv) { int c, optend; -#define OPTFLAGS "BC:D:I:SV:d:ef:ij:km:npqrst" +#define OPTFLAGS "BC:D:I:SV:d:ef:ij::km:npqrst" #define OPTLETTERS "BSiknpqrst" if (pledge("stdio rpath wpath cpath fattr proc exec", NULL) == -1) @@ -314,20 +315,31 @@ MainParseArgs(int argc, char **argv) case 'f': Lst_AtEnd(&makefiles, optarg); break; - case 'j': { - const char *errstr; - + case 'j': forceJobs = true; - optj = strtonum(optarg, 1, INT_MAX, &errstr); - if (errstr != NULL) { - fprintf(stderr, - "make: illegal argument to -j option" - " -- %s -- %s\n", optarg, errstr); - usage(); + if (optarg) { + const char *errstr; + + optj = strtonum(optarg, 1, INT_MAX, &errstr); + if (errstr != NULL) { + fprintf(stderr, + "make: illegal argument to -j option" + " -- %s -- %s\n", optarg, errstr); + usage(); + } + } else { + int mib[2]; + int result; + size_t len; + + mib[0] = CTL_HW; + mib[1] =HW_NCPUONLINE; + len = sizeof(optj); + if (sysctl(mib, 2, &optj, &len, NULL, 0) == -1) + err(1, "sysctl"); } record_option(c, optarg); break; - } case 'm': Dir_AddDir(systemIncludePath, optarg); record_option(c, optarg); Index: make.1 =================================================================== RCS file: /build/data/openbsd/cvs/src/usr.bin/make/make.1,v diff -u -p -r1.141 make.1 --- make.1 10 Aug 2023 10:56:34 -0000 1.141 +++ make.1 22 Feb 2025 10:05:28 -0000 @@ -44,7 +44,7 @@ .Op Fl d Ar flags .Op Fl f Ar mk .Op Fl I Ar directory -.Op Fl j Ar max_jobs +.Op Fl j Op Ar max_jobs .Op Fl m Ar directory .Op Fl V Ar variable .Op Ar NAME Ns = Ns Ar value ... @@ -241,12 +241,17 @@ Multiple directories can be added to for Furthermore, the system include path (see the .Fl m option) will be used after this search path. -.It Fl j Ar max_jobs +.It Fl j Op Ar max_jobs Specify the maximum number of jobs that .Nm may have running at any one time. See the discussion about recursive invocations under .Sx BUGS . +If +.Ar max_jobs +is omitted, uses +.Xr sysctl 8 Ns ' Ns s +.Ar hw.ncpuonline . .It Fl m Ar directory Specify a directory in which to search for system include files: .Pa sys.mk