Print this page
make: be serial if 'make', parallel if 'dmake', and parallel if '-j' is specified either way

@@ -38,10 +38,11 @@
 
 #include <bsd/bsd.h>            /* bsd_signal() */
 
 
 #include <locale.h>             /* setlocale() */
+#include <libgen.h>
 #include <mk/defs.h>
 #include <mksdmsi18n/mksdmsi18n.h>      /* libmksdmsi18n_init() */
 #include <mksh/macro.h>         /* getvar() */
 #include <mksh/misc.h>          /* getmem(), setup_char_semantics() */
 

@@ -80,12 +81,17 @@
 #define MAKE_PREFIX             NOCATGETS("/usr")
 #define LD_SUPPORT_ENV_VAR      NOCATGETS("SGS_SUPPORT_32")
 #define LD_SUPPORT_ENV_VAR_32   NOCATGETS("SGS_SUPPORT_32")
 #define LD_SUPPORT_ENV_VAR_64   NOCATGETS("SGS_SUPPORT_64")
 #define LD_SUPPORT_MAKE_LIB     NOCATGETS("libmakestate.so.1")
-#define LD_SUPPORT_MAKE_LIB_DIR NOCATGETS("/lib")
-#define LD_SUPPORT_MAKE_LIB_DIR_64      NOCATGETS("/64")
+#ifdef __i386
+#define LD_SUPPORT_MAKE_ARCH    NOCATGETS("i386")
+#elif __sparc
+#define LD_SUPPORT_MAKE_ARCH    NOCATGETS("sparc")
+#else
+#error "Unsupported architecture"
+#endif
 
 /*
  * typedefs & structs
  */
 

@@ -411,17 +417,31 @@
         /*
          * Find the dmake_mode: parallel, or serial.
          */
     if ((!pmake_cap_r_specified) &&
         (!pmake_machinesfile_specified)) {
+        char *s = strdup(argv[0]);    
+            
         MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MODE"));
         dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
         prop2 = get_prop(dmake_name2->prop, macro_prop);
-        if (prop2 == NULL) {
-                /* DMAKE_MODE not defined, default to parallel mode */
+        // If we're invoked as 'make' run serially, regardless of DMAKE_MODE
+        // If we're invoked as 'make' but passed -j, run parallel
+        // If we're invoked as 'dmake', without DMAKE_MODE, default parallel
+        // If we're invoked as 'dmake' and DMAKE_MODE is set, honour it.
+        if ((strcmp(basename(s), NOCATGETS("make")) == 0) &&
+            !dmake_max_jobs_specified) {
+                dmake_mode_type = serial_mode;
+                no_parallel = true;
+        } else if (prop2 == NULL) {
+                /* DMAKE_MODE not defined, default based on our name */
+                char *s = strdup(argv[0]);
+
+                if (strcmp(basename(s), NOCATGETS("dmake")) == 0) {
                 dmake_mode_type = parallel_mode;
                 no_parallel = false;
+                }
         } else {
                 dmake_value2 = prop2->body.macro.value;
                 if (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("parallel"))) {
                         dmake_mode_type = parallel_mode;
                         no_parallel = false;

@@ -430,11 +450,11 @@
                         no_parallel = true;
                 } else {
                         fatal(catgets(catd, 1, 307, "Unknown dmake mode argument `%s' after -m flag"), dmake_value2->string_mb);
                 }
         }
-
+        free(s);
     }
 
         parallel_flag = true;
         putenv(strdup(NOCATGETS("DMAKE_CHILD=TRUE")));
 

@@ -1361,10 +1381,12 @@
                 return 0;
         case 'j':                        /* Use alternative DMake max jobs */
                 if (invert_this) {
                         dmake_max_jobs_specified = false;
                 } else {
+                        dmake_mode_type = parallel_mode;
+                        no_parallel = false;
                         dmake_max_jobs_specified = true;
                 }
                 return 8;
         case 'K':                        /* Read alternative .make.state */
                 return 256;

@@ -1597,10 +1619,54 @@
                 }
            }
         }
 }
 
+char *
+make_install_prefix(void)
+{
+        int ret;
+        char origin[PATH_MAX];
+        char *dir;
+
+        if ((ret = readlink("/proc/self/path/a.out", origin,
+            PATH_MAX - 1)) < 0)
+                fatal("failed to read origin from /proc\n");
+
+        
+        origin[ret] = '\0';
+        return strdup(dirname(origin));
+}
+
+static char *
+add_to_env(const char *var, const char *value, const char *fallback)
+{
+        const char *oldpath;
+        char *newpath;
+
+        oldpath = getenv(var);
+        if (oldpath == NULL) {
+                if (value != NULL) {
+                        asprintf(&newpath, "%s=%s",
+                            var, value);
+                } else {
+                        asprintf(&newpath, "%s=%s",
+                            var, fallback);
+                }
+        } else {
+                if (value != NULL) {
+                        asprintf(&newpath, "%s=%s:%s",
+                            var, oldpath, value);
+                } else {
+                        asprintf(&newpath, "%s=%s:%s",
+                            var, oldpath, fallback);                    
+                }
+        }
+
+        return (newpath);
+}
+
 /*
  *      set_sgs_support()
  *
  *      Add the libmakestate.so.1 lib to the env var SGS_SUPPORT
  *        if it's not already in there.

@@ -1616,60 +1682,47 @@
 static void
 set_sgs_support()
 {
         int             len;
         char            *newpath, *newpath64;
-        char            *oldpath, *oldpath64;
+        char            *lib32, *lib64;
         static char     *prev_path, *prev_path64;
+        char            *origin = make_install_prefix();
+        struct stat st;
 
-        oldpath = getenv(LD_SUPPORT_ENV_VAR_32);
-        if (oldpath == NULL) {
-                len = snprintf(NULL, 0, "%s=%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_32,
-                    MAKE_PREFIX,
-                    LD_SUPPORT_MAKE_LIB_DIR,
-                    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB) + 1;
-                newpath = (char *) malloc(len);
-                sprintf(newpath, "%s=%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_32,
-                    MAKE_PREFIX,
-                    LD_SUPPORT_MAKE_LIB_DIR,
-                    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB);
-        } else {
-                len = snprintf(NULL, 0, "%s=%s:%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_32, oldpath, MAKE_PREFIX,
-                    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB,
-                    LD_SUPPORT_MAKE_LIB) + 1;
-                newpath = (char *) malloc(len);
-                sprintf(newpath, "%s=%s:%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_32, oldpath, MAKE_PREFIX,
-                    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB,
+        asprintf(&lib32, "%s/%s/%s", origin, "../lib",
                     LD_SUPPORT_MAKE_LIB);
+
+        if (stat(lib32, &st) != 0) {
+                free(lib32);
+                // Try the tools path
+                asprintf(&lib32, "%s/%s/%s/%s", origin, "../../lib/",
+                    LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
+
+                if (stat(lib32, &st) != 0) {
+                        free(lib32);
+                        lib32 = NULL;
+                }
         }
 
-        oldpath64 = getenv(LD_SUPPORT_ENV_VAR_64);
-        if (oldpath64 == NULL) {
-                len = snprintf(NULL, 0, "%s=%s/%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_64, MAKE_PREFIX, LD_SUPPORT_MAKE_LIB_DIR,
-                    LD_SUPPORT_MAKE_LIB_DIR_64, LD_SUPPORT_MAKE_LIB,
-                    LD_SUPPORT_MAKE_LIB) + 1;
-                newpath64 = (char *) malloc(len);
-                sprintf(newpath64, "%s=%s/%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_64, MAKE_PREFIX, LD_SUPPORT_MAKE_LIB_DIR,
-                    LD_SUPPORT_MAKE_LIB_DIR_64, LD_SUPPORT_MAKE_LIB,
+        asprintf(&lib64, "%s/%s/64/%s", origin, "../lib",
                     LD_SUPPORT_MAKE_LIB);
-        } else {
-                len = snprintf(NULL, 0, "%s=%s:%s/%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_64, oldpath64, MAKE_PREFIX,
-                    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB_DIR_64,
-                    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB) + 1;
-                newpath64 = (char *) malloc(len);
-                sprintf(newpath64, "%s=%s:%s/%s/%s/%s:%s",
-                    LD_SUPPORT_ENV_VAR_64, oldpath64, MAKE_PREFIX,
-                    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB_DIR_64,
-                    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB);
+
+        if (stat(lib64, &st) != 0) {
+                free(lib64);
+                // Try the tools path
+                asprintf(&lib64, "%s/%s/%s/64/%s", origin, "../../lib/",
+                    LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
+
+                if (stat(lib64, &st) != 0) {
+                        free(lib64);
+                        lib64 = NULL;
         }
+        }
+
+        newpath = add_to_env(LD_SUPPORT_ENV_VAR_32, lib32, LD_SUPPORT_MAKE_LIB);
+        newpath64 = add_to_env(LD_SUPPORT_ENV_VAR_64, lib64, LD_SUPPORT_MAKE_LIB);
 
         putenv(newpath);
         if (prev_path) {
                 free(prev_path);
         }

@@ -1678,10 +1731,13 @@
         putenv(newpath64);
         if (prev_path64) {
                 free(prev_path64);
         }
         prev_path64 = newpath64;
+        free(lib32);
+        free(lib64);
+        free(origin);
 }
 
 /*
  *      read_files_and_state(argc, argv)
  *