Index: trunk/src/kmk/read.c
===================================================================
--- trunk/src/kmk/read.c	(revision 1020)
+++ trunk/src/kmk/read.c	(revision 1021)
@@ -2044,6 +2044,6 @@
 	    {
 	      max_targets += 5;
-	      targets = xrealloc (targets, max_targets * sizeof (char *));
-	      target_percents = xrealloc (target_percents,
+	      targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
+	      target_percents = xrealloc ((void *)target_percents,
                                           max_targets * sizeof (char *));
 	    }
@@ -2056,5 +2056,5 @@
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
       /* Check for the explicit multitarget mode operators. For this to be
-         identified as an excplicit multiple target rule, the first + or +|
+         identified as an explicit multiple target rule, the first + or +|
          operator *must* appear between the first two files. If not found as
          the 2nd file or if found as the 1st file, the rule will be rejected
@@ -2069,7 +2069,6 @@
                 cmp timestamp maybe.h || cp -f timestamp maybe.h
 
-         This is handled below by adding replacing the prereqs of the
-         maybe-updated by an order only dependency on the primary target
-         (see below). This saves messing up remake.c. */
+        This is implemented in remake.c where we don't consider the mtime of 
+        the maybe-updated targets. */
       if (multi_mode != m_no && name[0] == '+'
         && (name[1] == '\0' || (name[1] == '|' && name[2] == '\0')))
@@ -2080,5 +2079,14 @@
             {
               if (multi_mode == m_unsettled)
-                prev_file->multi_head = prev_file;
+                {
+                  prev_file->multi_head = prev_file;
+
+                  /* Only the primary file needs the dependencies. */
+                  if (deps)
+                    {
+                      free_dep_chain (deps);
+                      deps = NULL;
+                    }
+                }
               multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
               continue;
@@ -2141,38 +2149,4 @@
 	  if (cmds != 0)
 	    f->cmds = cmds;
-
-#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-          /* If this is an explicit multi target rule, add it to the
-             target chain and set the multi_maybe flag according to
-             the current mode. This is also where we do the dependency
-             tricks for the 'maybe' targets (see above). */
-
-          if (multi_mode >= m_yes)
-            {
-              f->multi_maybe = multi_mode == m_yes_maybe;
-              if (f->multi_maybe)
-                {
-                  /* expand_deps needs the "| " bit.
-                     XXX leaking memory here? */
-                  size_t len = strlen (prev_file->multi_head->name) + 1;
-                  char *maybe_dep = xmalloc (len + 2);
-                  memcpy (maybe_dep, "| ", 2);
-                  memcpy (maybe_dep + 2, prev_file->multi_head->name, len);
-
-                  free_dep_chain (this);
-                  this = alloc_dep ();
-                  this->name = maybe_dep;
-                  this->ignore_mtime = 1;
-                }
-              prev_file->multi_next = f;
-              assert (prev_file->multi_head != 0);
-              f->multi_head = prev_file->multi_head;
-
-              if (f == suffix_file)
-                error (flocp,
-                       _(".SUFFIXES encountered in an explicit multi target rule"));
-            }
-          prev_file = f;
-#endif
 
 	  /* Defining .SUFFIXES with no dependencies clears out the list of
Index: trunk/src/kmk/remake.c
===================================================================
--- trunk/src/kmk/remake.c	(revision 1020)
+++ trunk/src/kmk/remake.c	(revision 1021)
@@ -380,13 +380,13 @@
   int running = 0;
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-  struct file *dep_file;
+  struct file *f2, *f3;
 
   /* Always work on the primary multi target file. */
+
   if (file->multi_head != NULL && file->multi_head != file)
     {
-      DBS (DB_VERBOSE, (_("Considering target file `%s' -> switching to multi head `%s'.\n"), 
+      DBS (DB_VERBOSE, (_("Considering target file `%s' -> multi head `%s'.\n"), 
                           file->name, file->multi_head->name));
       file = file->multi_head;
-      /* XXX: optimize dependencies. */
     }
   else
@@ -442,6 +442,29 @@
      not need an implicit rule.  If this were not done, the file
      might get implicit commands that apply to its initial name, only
-     to have that name replaced with another found by VPATH search.  */
-
+     to have that name replaced with another found by VPATH search.
+
+     For multi target files check the other files and use the time
+     of the oldest / non-existing file. */
+
+#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
+  this_mtime = file_mtime (file);
+  f3 = file;
+  for (f2 = file->multi_next; 
+       f2 != NULL && this_mtime != NONEXISTENT_MTIME; 
+       f2 = f2->multi_next)
+    if (!f2->multi_maybe)
+      {
+        FILE_TIMESTAMP second_mtime = file_mtime (f2);
+        if (second_mtime < this_mtime)
+          {
+            this_mtime = second_mtime;
+            f3 = f2;
+          }
+      }
+  check_renamed (file);
+  noexist = this_mtime == NONEXISTENT_MTIME;
+  if (noexist)
+    DBS (DB_BASIC, (_("File `%s' does not exist.\n"), f3->name));
+#else /* !CONFIG_WITH_EXPLICIT_MULTITARGET */
   this_mtime = file_mtime (file);
   check_renamed (file);
@@ -449,4 +472,5 @@
   if (noexist)
     DBF (DB_BASIC, _("File `%s' does not exist.\n"));
+#endif /* !CONFIG_WITH_EXPLICIT_MULTITARGET */
   else if (ORDINARY_MTIME_MIN <= this_mtime && this_mtime <= ORDINARY_MTIME_MAX
 	   && file->low_resolution_time)
@@ -483,12 +507,11 @@
      and see whether any of them is more recent than this file.  
      For explicit multitarget rules we must iterate all the output
-     files to get the correct picture (this means re-evaluating 
-     shared dependencies - bad). */
+     files to get the correct picture. */
 
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-  for (dep_file = file; dep_file; dep_file = dep_file->multi_next)
+  for (f2 = file; f2; f2 = f2->multi_next)
     {
       lastd = 0;
-      d = dep_file->deps;
+      d = f2->deps;
 #else
       lastd = 0;
@@ -510,5 +533,5 @@
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
               /* silently ignore the order-only dep hack. */
-              if (dep_file->multi_maybe && d->file == file)
+              if (f2->multi_maybe && d->file == file)
                 {
                   lastd = d;
@@ -520,5 +543,5 @@
               error (NILF, _("Circular %s <- %s dependency dropped."),
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-                     dep_file->name, d->file->name);
+                     f2->name, d->file->name);
 #else
                      file->name, d->file->name);
@@ -529,5 +552,5 @@
               if (lastd == 0)
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-                dep_file->deps = d->next;
+                f2->deps = d->next;
 #else
                 file->deps = d->next;
@@ -540,5 +563,5 @@
     
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-          d->file->parent = dep_file;
+          d->file->parent = f2;
 #else
           d->file->parent = file;
@@ -602,6 +625,6 @@
     {
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-      for (dep_file = file; dep_file; dep_file = dep_file->multi_next)
-        for (d = dep_file->deps; d != 0; d = d->next)
+      for (f2 = file; f2; f2 = f2->multi_next)
+        for (d = f2->deps; d != 0; d = d->next)
 #else
         for (d = file->deps; d != 0; d = d->next)
@@ -614,5 +637,5 @@
               check_renamed (d->file);
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-              d->file->parent = dep_file;
+              d->file->parent = f2;
 #else
               d->file->parent = file;
@@ -653,5 +676,5 @@
               if (!running)
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-                d->changed = ((dep_file->phony && dep_file->cmds != 0)
+                d->changed = ((f2->phony && f2->cmds != 0)
 #else
                 d->changed = ((file->phony && file->cmds != 0)
@@ -708,5 +731,5 @@
   deps_changed = 0;
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-  for (dep_file = file; dep_file; dep_file = dep_file->multi_next)
+  for (f2 = file; f2; f2 = f2->multi_next)
 #endif
     for (d = file->deps; d != 0; d = d->next)
@@ -714,5 +737,5 @@
         FILE_TIMESTAMP d_mtime = file_mtime (d->file);
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-        if (d->file == file && dep_file->multi_maybe)
+        if (d->file == file && f2->multi_maybe)
           continue;
 #endif 
@@ -819,10 +842,5 @@
     }
 
-#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-  if (ISDB(DB_BASIC) && file->multi_head && file->multi_head != file)
-    DBS (DB_BASIC, (_("Must remake target `%s' - primary target `%s'.\n"), file->name, file->multi_head->name));
-  else
-#endif
-    DBF (DB_BASIC, _("Must remake target `%s'.\n"));
+  DBF (DB_BASIC, _("Must remake target `%s'.\n"));
 
   /* It needs to be remade.  If it's VPATH and not reset via GPATH, toss the
@@ -1212,7 +1230,5 @@
 {
 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
-  /* Always operate on the primary file. */
-  if (file->multi_head && file->multi_head != file)
-    file = file->multi_head;
+  assert(file->multi_head == NULL || file->multi_head == file);
 #endif
 
