Hopefully sensible command switch handling. Changed default ext to .o on Windows.

This commit is contained in:
Christian Kamm
2008-10-16 22:07:02 +02:00
parent 8f14246bef
commit 6c532ac149
4 changed files with 71 additions and 53 deletions

View File

@@ -55,12 +55,9 @@ Global::Global()
ll_ext = "ll";
bc_ext = "bc";
s_ext = "s";
#if _WIN32
obj_ext = "obj";
#elif POSIX
obj_ext = "o";
#else
#error "fix this"
#if _WIN32
obj_ext_alt = "obj";
#endif
copyright = "Copyright (c) 1999-2008 by Digital Mars and Tomas Lindquist Olsen";
@@ -167,14 +164,12 @@ Usage:\n\
-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 @@ int main(int argc, char *argv[], char** envp)
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 @@ int main(int argc, char *argv[], char** envp)
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 @@ int main(int argc, char *argv[], char** envp)
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 @@ int main(int argc, char *argv[], char** envp)
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)
{

View File

@@ -46,6 +46,12 @@ enum ARCH
ARCHppc,
ARCHppc_64
};
enum OUTPUTFLAG
{
OUTPUTFLAGno,
OUTPUTFLAGdefault, // for the .o default
OUTPUTFLAGset // for -output
};
enum OS
{
@@ -161,6 +167,9 @@ struct Global
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;

View File

@@ -166,10 +166,17 @@ File* Module::buildFilePath(char* forcename, char* path, char* ext)
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()

View File

@@ -252,8 +252,6 @@ void Module::genobjfile(int multiobj, char** envp)
// 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);
}