changeset 708:fd5665da3a27

Hopefully sensible command switch handling. Changed default ext to .o on Windows.
author Christian Kamm <kamm incasoftware de>
date Thu, 16 Oct 2008 22:07:02 +0200
parents 1556a9328ba1
children b972fb129d37
files dmd/mars.c dmd/mars.h dmd/module.c gen/toobj.cpp
diffstat 4 files changed, 71 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/mars.c	Wed Oct 15 20:22:18 2008 +0200
+++ b/dmd/mars.c	Thu Oct 16 22:07:02 2008 +0200
@@ -55,12 +55,9 @@
     ll_ext  = "ll";
     bc_ext  = "bc";
     s_ext   = "s";
+    obj_ext = "o";
 #if _WIN32
-    obj_ext = "obj";
-#elif POSIX
-    obj_ext = "o";
-#else
-#error "fix this"
+    obj_ext_alt = "obj";
 #endif
 
     copyright = "Copyright (c) 1999-2008 by Digital Mars and Tomas Lindquist Olsen";
@@ -167,14 +164,12 @@
   -op            do not strip paths from source file\n\
   -oq            write object files with fully qualified names\n\
   -of<filename>  name output file to <filename>\n\
-                 extension of <filename> determines output type\n\
-                 no extension means native object or executable\n\
-                 if the extension is ll, bc, s, or o/obj, linking is disabled\n\
+                 if -c and extension of <filename> is known, it determines the output type\n\
 \n\
   -output-ll     write LLVM IR\n\
   -output-bc     write LLVM bitcode\n\
   -output-s      write native assembly\n\
-  -output-o      write native object\n\
+  -output-o      write native object (default if no -output switch passed)\n\
 \n\
   -c             do not link\n\
   -L<linkerflag> pass <linkerflag> to linker\n\
@@ -298,7 +293,7 @@
     global.params.Dversion = 2;
     global.params.quiet = 1;
 
-    global.params.output_o = 1;
+    global.params.output_o = OUTPUTFLAGdefault;
 
     global.params.linkswitches = new Array();
     global.params.libfiles = new Array();
@@ -453,38 +448,6 @@
 			if (!p[3])
 			    goto Lnoarg;
 			global.params.objname = p + 3;
-
-			// remove default output
-			if (global.params.output_ll == 1)
-			    global.params.output_ll = 0;
-			if (global.params.output_bc == 1)
-			    global.params.output_bc = 0;
-			if (global.params.output_s == 1)
-			    global.params.output_s = 0;
-			if (global.params.output_o == 1)
-			    global.params.output_o = 0;
-
-			// determine output based on ext
-			ext = FileName::ext(global.params.objname);
-            if (!ext) {
-                global.params.link = 1;
-                global.params.output_o = 1;
-			} else if (strcmp(ext, global.ll_ext) == 0) {
-			    global.params.output_ll = 1;
-			    global.params.link = 0;
-			} else if (strcmp(ext, global.bc_ext) == 0) {
-			    global.params.output_bc = 1;
-			    global.params.link = 0;
-			} else if (strcmp(ext, global.s_ext) == 0) {
-			    global.params.output_s = 1;
-			    global.params.link = 0;
-			} else if (strcmp(ext, global.obj_ext) == 0) {
-			    global.params.output_o = 1;
-			    global.params.link = 0;
-			} else {
-			    global.params.output_o = 1;
-			}
-
 			break;
 
 		    case 'p':
@@ -502,16 +465,22 @@
 		    case 'u':
 			if (strncmp(p+1, "output-", 7) != 0)
 			    goto Lerror;
+
+			// remove default output
+			if (global.params.output_o == OUTPUTFLAGdefault)
+			    global.params.output_o = OUTPUTFLAGno;
+
 			if (strcmp(p+8, global.ll_ext) == 0)
-			    global.params.output_ll = 2;
+			    global.params.output_ll = OUTPUTFLAGset;
 			else if (strcmp(p+8, global.bc_ext) == 0)
-			    global.params.output_bc = 2;
+			    global.params.output_bc = OUTPUTFLAGset;
 			else if (strcmp(p+8, global.s_ext) == 0)
-			    global.params.output_s = 2;
+			    global.params.output_s = OUTPUTFLAGset;
 			else if (strcmp(p+8, global.obj_ext) == 0)
-			    global.params.output_o = 2;
+			    global.params.output_o = OUTPUTFLAGset;
 			else
 			    goto Lerror;
+
 			break;
 
 		    case 0:
@@ -788,13 +757,48 @@
     if (global.params.useUnitTests)
 	global.params.useAssert = 1;
 
-    if (!global.params.obj)
+    // LDC output determination
+
+    // if we don't link, autodetect target from extension
+    if(!global.params.link && global.params.objname) {
+	ext = FileName::ext(global.params.objname);
+	bool autofound = false;
+	if (!ext) {
+	    // keep things as they are
+	} else if (strcmp(ext, global.ll_ext) == 0) {
+	    global.params.output_ll = OUTPUTFLAGset;
+	    autofound = true;
+	} else if (strcmp(ext, global.bc_ext) == 0) {
+	    global.params.output_bc = OUTPUTFLAGset;
+	    autofound = true;
+	} else if (strcmp(ext, global.s_ext) == 0) {
+	    global.params.output_s = OUTPUTFLAGset;
+	    autofound = true;
+	} else if (strcmp(ext, global.obj_ext) == 0) {
+	    global.params.output_o = OUTPUTFLAGset;
+	    autofound = true;
+	} else {
+	    // append dot, so forceExt won't change existing name even if it contains dots
+	    size_t len = strlen(global.params.objname);
+	    size_t extlen = strlen(".");
+	    char* s = (char *)alloca(len + 1 + extlen + 1);
+	    memcpy(s, global.params.objname, len);
+	    s[len] = '.';
+	    s[len+1+extlen] = 0;
+	    global.params.objname = s;
+
+	}
+	if(autofound && global.params.output_o == OUTPUTFLAGdefault)
+	    global.params.output_o = OUTPUTFLAGno;
+    }
+
+    // only link if possible
+    if (!global.params.obj || !global.params.output_o)
 	global.params.link = 0;
 
     if (global.params.link)
     {
 	global.params.exefile = global.params.objname;
-	global.params.objname = NULL;
     }
     else if (global.params.run)
     {
--- a/dmd/mars.h	Wed Oct 15 20:22:18 2008 +0200
+++ b/dmd/mars.h	Thu Oct 16 22:07:02 2008 +0200
@@ -46,6 +46,12 @@
     ARCHppc,
     ARCHppc_64
 };
+enum OUTPUTFLAG
+{
+    OUTPUTFLAGno,
+    OUTPUTFLAGdefault, // for the .o default
+    OUTPUTFLAGset // for -output
+};
 
 enum OS
 {
@@ -161,6 +167,9 @@
     char *mars_ext;
     char *sym_ext;
     char *obj_ext;
+#if _WIN32
+    char *obj_ext_alt;
+#endif
     char *ll_ext;
     char *bc_ext;
     char *s_ext;
--- a/dmd/module.c	Wed Oct 15 20:22:18 2008 +0200
+++ b/dmd/module.c	Thu Oct 16 22:07:02 2008 +0200
@@ -166,10 +166,17 @@
 
     FileName::ensurePathExists(FileName::path(argobj));
 
-    if (forcename)
+// always append the extension! otherwise hard to make output switches consistent
+//    if (forcename)
+//	return new File(argobj);
+//    else
+    // allow for .o and .obj on windows
+#if _WIN32
+    if (ext == global.params.objdir && FileName::ext(argobj) 
+	    && stricmp(FileName::ext(argobj), global.obj_ext_alt) == 0)
 	return new File(argobj);
-    else
-	return new File(FileName::forceExt(argobj, ext));
+#endif
+    return new File(FileName::forceExt(argobj, ext));
 }
 
 void Module::buildTargetFiles()
--- a/gen/toobj.cpp	Wed Oct 15 20:22:18 2008 +0200
+++ b/gen/toobj.cpp	Thu Oct 16 22:07:02 2008 +0200
@@ -252,8 +252,6 @@
         // call gcc to convert assembly to object file
         if (global.params.output_o) {
             LLPath objpath = LLPath(objfile->name->toChars());
-            objpath.eraseSuffix();
-            objpath.appendSuffix(std::string(global.obj_ext));
             assemble(spath, objpath, envp);
         }