changeset 25494:f35bd5cddedd

mkoctfile: disallow -c and -o with multiple source files * mkoctfile.in.cc (main): Error if -c and -o are both specified and there are multiple source files. Rename link to compile_only and invert meaning. When compiling soure files, exit early if compile_only is true since there can be only one source file and there will be nothing left to do.
author John W. Eaton <jwe@octave.org>
date Mon, 25 Jun 2018 13:05:22 -0400
parents 3b5db971e091
children 92a64cb5e475
files src/mkoctfile.in.cc
diffstat 1 files changed, 97 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/src/mkoctfile.in.cc	Mon Jun 25 11:31:41 2018 -0400
+++ b/src/mkoctfile.in.cc	Mon Jun 25 13:05:22 2018 -0400
@@ -484,7 +484,7 @@
   std::string incflags, defs, ldflags, pass_on_options;
   bool strip = false;
   bool no_oct_file_strip_on_this_platform = is_true ("%NO_OCT_FILE_STRIP%");
-  bool link = true;
+  bool compile_only = false;
   bool link_stand_alone = false;
   bool depend = false;
   bool printonly = false;
@@ -614,7 +614,7 @@
         }
       else if (arg == "-c" || arg == "-compile" || arg == "--compile")
         {
-          link = false;
+          compile_only = true;
         }
       else if (arg == "-g")
         {
@@ -674,6 +674,14 @@
       defs += " -DMEX_DEBUG";
     }
 
+  if (compile_only
+      && (cfiles.size () + ccfiles.size () + f77files.size ()) > 1)
+    {
+      std::cerr << "mkoctfile: may not use -c and -o with multiple source files"
+                << std::endl;
+      return 1;
+    }
+
   std::string output_option;
 
   if (link_stand_alone)
@@ -770,21 +778,26 @@
 
   for (const auto& f : f77files)
     {
-      std::string b = basename (f, true);
-
       if (! vars["F77"].empty ())
         {
+          std::string b = basename (f, true);
           std::string o;
-          if (! outputfile.empty ())
+          if (compile_only)
             {
-              if (link)
+              // There will be only one source file, so we are done
+              // after executing the command below and we don't need to
+              // keep track of the object file name.
+
+              if (! outputfile.empty ())
+                o = outputfile;
+              else
                 o = b + ".o";
-              else
-                o = outputfile;
             }
           else
-            o = b + ".o";
-          objfiles += (' ' + o);
+            {
+              o = b + ".o";
+              objfiles += (' ' + o);
+            }
 
           std::string cmd
             = (vars["F77"] + " -c " + vars["FPICFLAG"] + ' '
@@ -793,7 +806,7 @@
 
           int status = run_command (cmd, printonly);
 
-          if (status)
+          if (compile_only || status)
             return status;
         }
       else
@@ -808,17 +821,24 @@
     {
       if (! vars["CC"].empty ())
         {
-          std::string b = basename (f, true), o;
-          if (! outputfile.empty ())
+          std::string b = basename (f, true);
+          std::string o;
+          if (compile_only)
             {
-              if (link)
+              // There will be only one source file, so we are done
+              // after executing the command below and we don't need to
+              // keep track of the object file name.
+
+              if (! outputfile.empty ())
+                o = outputfile;
+              else
                 o = b + ".o";
-              else
-                o = outputfile;
             }
           else
-            o = b + ".o";
-          objfiles += (' ' + o);
+            {
+              o = b + ".o";
+              objfiles += (' ' + o);
+            }
 
           std::string cmd
             = (vars["CC"] + " -c " + vars["CPPFLAGS"] + ' '
@@ -828,7 +848,7 @@
 
           int status = run_command (cmd, printonly);
 
-          if (status)
+          if (compile_only || status)
             return status;
         }
       else
@@ -843,17 +863,24 @@
     {
       if (! vars["CXX"].empty ())
         {
-          std::string b = basename (f, true), o;
-          if (! outputfile.empty ())
+          std::string b = basename (f, true);
+          std::string o;
+          if (compile_only)
             {
-              if (link)
+              // There will be only one source file, so we are done
+              // after executing the command below and we don't need to
+              // keep track of the object file name.
+
+              if (! outputfile.empty ())
+                o = outputfile;
+              else
                 o = b + ".o";
-              else
-                o = outputfile;
             }
           else
-            o = b + ".o";
-          objfiles += (' ' + o);
+            {
+              o = b + ".o";
+              objfiles += (' ' + o);
+            }
 
           std::string cmd
             = (vars["CXX"] + " -c " + vars["CPPFLAGS"] + ' '
@@ -863,7 +890,7 @@
 
           int status = run_command (cmd, printonly);
 
-          if (status)
+          if (compile_only || status)
             return status;
         }
       else
@@ -874,62 +901,65 @@
         }
     }
 
-  if (link && ! objfiles.empty ())
+  if (objfiles.empty ())
     {
-      std::string octave_libs;
+      std::cerr << "mkoctfile: no objects to link" << std::endl;
+      return 1;
+    }
+
+  std::string octave_libs;
 #if defined (OCTAVE_USE_WINDOWS_API) || defined(CROSS)
-      octave_libs = "-loctinterp -loctave";
+  octave_libs = "-loctinterp -loctave";
 #endif
 
-      if (link_stand_alone)
-        {
-          if (! vars["LD_CXX"].empty ())
-            {
-              std::string cmd
-                = (vars["LD_CXX"] + ' ' + vars["CPPFLAGS"] + ' '
-                   + vars["ALL_CXXFLAGS"] + ' ' + vars["RDYNAMIC_FLAG"] + ' '
-                   + vars["ALL_LDFLAGS"] + ' ' + pass_on_options + ' '
-                   + output_option + ' ' + objfiles + ' ' + libfiles + ' '
-                   + ldflags + ' ' + vars["LFLAGS"] + ' ' + octave_libs + ' '
-                   + vars["OCTAVE_LINK_OPTS"] + ' ' + vars["OCTAVE_LINK_DEPS"]);
-
-              int status = run_command (cmd, printonly);
-
-              if (status)
-                return status;
-            }
-          else
-            {
-              std::cerr
-                << "mkoctfile: no way to link stand-alone executable file"
-                << std::endl;
-              return 1;
-            }
-        }
-      else
+  if (link_stand_alone)
+    {
+      if (! vars["LD_CXX"].empty ())
         {
           std::string cmd
-            = (vars["DL_LD"] + ' ' + vars["ALL_CXXFLAGS"] + ' '
-               + vars["DL_LDFLAGS"] + ' ' + pass_on_options
-               + " -o " + octfile + ' ' + objfiles + ' ' + libfiles + ' '
+            = (vars["LD_CXX"] + ' ' + vars["CPPFLAGS"] + ' '
+               + vars["ALL_CXXFLAGS"] + ' ' + vars["RDYNAMIC_FLAG"] + ' '
+               + vars["ALL_LDFLAGS"] + ' ' + pass_on_options + ' '
+               + output_option + ' ' + objfiles + ' ' + libfiles + ' '
                + ldflags + ' ' + vars["LFLAGS"] + ' ' + octave_libs + ' '
-               + vars["OCT_LINK_OPTS"] + ' ' + vars["OCT_LINK_DEPS"]);
+               + vars["OCTAVE_LINK_OPTS"] + ' ' + vars["OCTAVE_LINK_DEPS"]);
 
           int status = run_command (cmd, printonly);
 
           if (status)
             return status;
         }
-
-      if (strip)
+      else
         {
-          std::string cmd = "strip " + octfile;
+          std::cerr
+            << "mkoctfile: no way to link stand-alone executable file"
+            << std::endl;
+          return 1;
+        }
+    }
+  else
+    {
+      std::string cmd
+        = (vars["DL_LD"] + ' ' + vars["ALL_CXXFLAGS"] + ' '
+           + vars["DL_LDFLAGS"] + ' ' + pass_on_options
+           + " -o " + octfile + ' ' + objfiles + ' ' + libfiles + ' '
+           + ldflags + ' ' + vars["LFLAGS"] + ' ' + octave_libs + ' '
+           + vars["OCT_LINK_OPTS"] + ' ' + vars["OCT_LINK_DEPS"]);
 
-          int status = run_command (cmd, printonly);
+      int status = run_command (cmd, printonly);
+
+      if (status)
+        return status;
+    }
 
-          if (status)
-            return status;
-        }
+  if (strip)
+    {
+      std::string cmd = "strip " + octfile;
+
+      int status = run_command (cmd, printonly);
+
+      if (status)
+        return status;
     }
 
   return 0;