CDC improvements and an a fix in AutoDDoc.

This commit is contained in:
Ferdinand Majerech 2012-01-23 01:16:57 +01:00
parent d46a3ac5c2
commit 06e3a6db3b
2 changed files with 93 additions and 110 deletions

View file

@ -97,7 +97,7 @@ template_header =\
template_footer =\ template_footer =\
("\n<div id=\"copyright\">\n" ("\n<div id=\"copyright\">\n"
"$(COPYRIGHT) |\n" "$(COPYRIGHT) |\n"
"Page generated by Autodoc and $(LINK2 http://www.digitalmars.com/d/2.0/ddoc.html, Ddoc).\n" "Page generated by AutoDDoc and $(LINK2 http://www.digitalmars.com/d/2.0/ddoc.html, Ddoc).\n"
"</div>\n\n") "</div>\n\n")
default_css =\ default_css =\

161
cdc.d
View file

@ -134,10 +134,9 @@ else
void main(string[] args) void main(string[] args)
{ {
string target = "release";
scope(failure){help(); core.stdc.stdlib.exit(-1);} scope(failure){help(); core.stdc.stdlib.exit(-1);}
string[] targets;
string[] extra_args = ["-w", "-wi"]; string[] extra_args = ["-w", "-wi"];
args = args[1 .. $]; args = args[1 .. $];
@ -151,17 +150,22 @@ void main(string[] args)
case "--ldc": compiler = "ldmd"; break; case "--ldc": compiler = "ldmd"; break;
default: extra_args ~= arg; default: extra_args ~= arg;
} }
else
{
targets ~= arg;
}
} }
if(args.length > 0 && args[$ - 1][0] != '-'){target = args[$ - 1];}
string[] dbg = ["-debug", "-gc"]; if(targets.length == 0){targets = ["release"];}
string[] optimize = ["-O", "-inline", "-release", "-noboundscheck"];
string[] profile = ["-O", "-release", "-noboundscheck", "-gc"]; auto dbg = ["-debug", "-gc"];
string[] lib_src = ["dyaml/", "yaml.d"]; auto optimize = ["-O", "-inline", "-release", "-noboundscheck"];
auto profile = ["-O", "-release", "-noboundscheck", "-gc"];
auto lib_src = ["dyaml/", "yaml.d"];
void compile_(string[] args, string[] files) void compile_(string[] args, string[] files)
{ {
compile(files, args ~ extra_args); compile(args ~ extra_args, files);
} }
void build_unittest() void build_unittest()
@ -230,7 +234,7 @@ void main(string[] args)
} }
} }
try{build(target);} try{build(targets);}
catch(CompileException e){writeln("Could not compile: " ~ e.msg);} catch(CompileException e){writeln("Could not compile: " ~ e.msg);}
catch(ProcessException e){writeln("Compilation failed: " ~ e.msg);} catch(ProcessException e){writeln("Compilation failed: " ~ e.msg);}
} }
@ -242,7 +246,7 @@ void help()
"D:YAML build script\n" "D:YAML build script\n"
"Changes Copyright (C) 2011 Ferdinand Majerech\n" "Changes Copyright (C) 2011 Ferdinand Majerech\n"
"Based on CDC script Copyright (C) 2009-2010 Eric Poggel\n" "Based on CDC script Copyright (C) 2009-2010 Eric Poggel\n"
"Usage: cdc [OPTION ...] [EXTRA COMPILER OPTION ...] [TARGET]\n" "Usage: cdc [OPTION ...] [EXTRA COMPILER OPTION ...] [TARGET ...]\n"
"By default, cdc uses the compiler it was built with to compile the project.\n" "By default, cdc uses the compiler it was built with to compile the project.\n"
"\n" "\n"
"Any options starting with '-' not parsed by the script will be\n" "Any options starting with '-' not parsed by the script will be\n"
@ -274,8 +278,8 @@ void help()
/** /**
* Compile D code using the current compiler. * Compile D code using the current compiler.
* *
* Params: paths = Source and library files/directories. Directories are recursively searched. * Params: options = Compiler options.
* options = Compiler options. * paths = Source and library files/directories. Directories are recursively searched.
* *
* Example: * Example:
* -------- * --------
@ -287,7 +291,7 @@ void help()
* *
* TODO Add a dry run option to just return an array of commands to execute. * TODO Add a dry run option to just return an array of commands to execute.
*/ */
static void compile(string[] paths, string[] options = null) void compile(string[] options, string[] paths)
{ {
//Convert src and lib paths to files //Convert src and lib paths to files
string[] sources, libs, ddocs; string[] sources, libs, ddocs;
@ -329,14 +333,14 @@ static void compile(string[] paths, string[] options = null)
//Create modules.ddoc and add it to array of ddocs //Create modules.ddoc and add it to array of ddocs
if(co.generate_doc) if(co.generate_doc)
{ {
string modules = "MODULES = \r\n"; string modules = "MODULES = \n";
sources.sort; sources.sort;
foreach(src; sources) foreach(src; sources)
{ {
//get filename //get filename
src = split(src, "\\.")[0]; src = split(src, "\\.")[0];
src = replace(replace(src, "/", "."), "\\", "."); src = src.replace("/", ".").replace("\\", ".");
modules ~= "\t$(MODULE " ~ src ~ ")\r\n"; modules ~= "\t$(MODULE " ~ src ~ ")\n";
} }
scope(failure){remove("modules.ddoc");} scope(failure){remove("modules.ddoc");}
write("modules.ddoc", modules); write("modules.ddoc", modules);
@ -353,20 +357,14 @@ static void compile(string[] paths, string[] options = null)
if(co.generate_lib || co.generate_doc || co.no_linking) if(co.generate_lib || co.generate_doc || co.no_linking)
{ {
//Remove options we don't want to pass to gdc when building incrementally. //Remove options we don't want to pass to gdc when building incrementally.
string[] incremental_options; auto incremental_options =
foreach(option; options) array(filter!`a != "-lib" && !startsWith(a, "-o")`(options));
{
if(option != "-lib" && !startsWith(option, "-o"))
{
incremental_options ~= option;
}
}
//Compile files individually, outputting full path names //Compile files individually, outputting full path names
string[] obj_files; string[] obj_files;
foreach(source; sources) foreach(source; sources)
{ {
string obj = replace(source, "/", ".")[0 .. $ - 2] ~ ".o"; string obj = source.replace("/", ".")[0 .. $ - 2] ~ ".o";
string ddoc = obj[0 .. $ - 2]; string ddoc = obj[0 .. $ - 2];
if(co.obj_directory !is null) if(co.obj_directory !is null)
{ {
@ -392,20 +390,15 @@ static void compile(string[] paths, string[] options = null)
//Remove obj files if -c or -od not were supplied. //Remove obj files if -c or -od not were supplied.
if(!co.obj_directory && !co.no_linking) if(!co.obj_directory && !co.no_linking)
{ {
foreach (o; obj_files){remove(o);} foreach(o; obj_files){remove(o);}
} }
} }
if (!co.generate_lib && !co.no_linking) if(!co.generate_lib && !co.no_linking)
{ {
//Remove documentation arguments since they were handled above //Remove documentation arguments since they were handled above
string[] nondoc_args; execute_compiler(compiler,
foreach(arg; arguments) array(filter!`!startsWith(a, "-fdoc", "-od")`(arguments)));
{
if(!startsWith(arg, "-fdoc", "-od")){nondoc_args ~= arg;}
}
execute_compiler(compiler, nondoc_args);
} }
} }
//Compilers other than gdc //Compilers other than gdc
@ -414,14 +407,12 @@ static void compile(string[] paths, string[] options = null)
execute_compiler(compiler, arguments); execute_compiler(compiler, arguments);
//Move all html files in doc_path to the doc output folder //Move all html files in doc_path to the doc output folder
//and rename them with the "package.module" naming convention. //and rename them with the "package.module" naming convention.
if(co.generate_doc) if(co.generate_doc) foreach(src; sources)
{
foreach(src; sources)
{ {
if(src.extension != ".d"){continue;} if(src.extension != ".d"){continue;}
string html = src[0 .. $ - 2] ~ ".html"; string html = src[0 .. $ - 2] ~ ".html";
string dest = replace(replace(html, "/", "."), "\\", "."); string dest = html.replace("/", ".").replace("\\", ".");
if(co.doc_directory.length > 0) if(co.doc_directory.length > 0)
{ {
dest = co.doc_directory ~ file_separator ~ dest; dest = co.doc_directory ~ file_separator ~ dest;
@ -435,7 +426,6 @@ static void compile(string[] paths, string[] options = null)
} }
} }
} }
}
//Remove extra files //Remove extra files
string basename = split(co.out_file, "/")[$ - 1]; string basename = split(co.out_file, "/")[$ - 1];
@ -490,20 +480,20 @@ struct CompileOptions
*/ */
this(string[] options, in string[] sources) this(string[] options, in string[] sources)
{ {
foreach (i, option; options) foreach(i, opt; options)
{ {
if(option == "-c"){no_linking = true;} if(opt == "-c") {no_linking = true;}
else if(option == "-D" || option == "-fdoc"){generate_doc = true;} else if(["-D", "-fdoc"].canFind(opt)) {generate_doc = true;}
else if(startsWith(option, "-Dd")){doc_directory = option[3..$];} else if(opt.startsWith("-Dd")) {doc_directory = opt[3..$];}
else if(startsWith(option, "-fdoc-dir=")){doc_directory = option[10..$];} else if(opt.startsWith("-fdoc-dir=")) {doc_directory = opt[10..$];}
else if(startsWith(option, "-Df")){doc_file = option[3..$];} else if(opt.startsWith("-Df")) {doc_file = opt[3..$];}
else if(startsWith(option, "-fdoc-file=")){doc_file = option[11..$];} else if(opt.startsWith("-fdoc-file=")) {doc_file = opt[11..$];}
else if(option == "-lib"){generate_lib = true;} else if(opt == "-lib") {generate_lib = true;}
else if(option == "-o-" || option=="-fsyntax-only"){no_objects = true;} else if(["-o-", "-fsyntax-only"].canFind(opt)){no_objects = true;}
else if(startsWith(option, "-of")){out_file = option[3..$];} else if(opt.startsWith("-of")) {out_file = opt[3..$];}
else if(startsWith(option, "-od")){obj_directory = option[3..$];} else if(opt.startsWith("-od")) {obj_directory = opt[3..$];}
else if(startsWith(option, "-o") && option != "-op"){out_file = option[2..$];} else if(opt.startsWith("-o") && opt != "-op") {out_file = opt[2..$];}
options_ ~= option; options_ ~= opt;
} }
//Set the -o (output filename) flag to the first source file if not already set. //Set the -o (output filename) flag to the first source file if not already set.
@ -511,14 +501,14 @@ struct CompileOptions
string ext = generate_lib ? lib_ext : bin_ext; string ext = generate_lib ? lib_ext : bin_ext;
if(out_file.length == 0 && !no_linking && !no_objects && sources.length > 0) if(out_file.length == 0 && !no_linking && !no_objects && sources.length > 0)
{ {
out_file = split(split(sources[0], "/")[$ - 1], "\\.")[0] ~ ext; out_file = sources[0].split("/").back.split("\\.")[0] ~ ext;
options_ ~= ("-of" ~ out_file); options_ ~= "-of" ~ out_file;
} }
version (Windows) version (Windows)
{ {
//TODO needs testing //TODO needs testing
//{ if (find(this.out_file, ".") <= rfind(this.out_file, "/")) //{ if (find(this.out_file, ".") <= rfind(this.out_file, "/"))
if(find(out_file, '.') <= retro(find(retro(out_file), '/'))) if(out_file.find('.') <= out_file.retro().find('/').retro())
{ {
out_file ~= bin_ext; out_file ~= bin_ext;
} }
@ -540,56 +530,49 @@ struct CompileOptions
if(compiler != "gdc") if(compiler != "gdc")
{ {
version(Windows) version(Windows) foreach(ref option; result)
{ {
foreach(ref option; result) option = option.startsWith("-of") ? option.replace("/", "\\") : option;
{
option = startsWith(option, "-of") ? replace(option, "/", "\\") : option;
}
} }
//ensure ddocs don't overwrite one another. //ensure ddocs don't overwrite one another.
return canFind(result, "-op") ? result : result ~ "-op"; return result.canFind("-op") ? result : result ~ "-op";
} }
//is gdc //is gdc
string[string] translate; auto translate = ["-Dd" : "-fdoc-dir=",
translate["-Dd"] = "-fdoc-dir="; "-Df" : "-fdoc-file=",
translate["-Df"] = "-fdoc-file="; "-debug=" : "-fdebug=",
translate["-debug="] = "-fdebug="; "-debug" : "-fdebug", // will this still get selected?
translate["-debug"] = "-fdebug"; // will this still get selected? "-inline" : "-finline-functions",
translate["-inline"] = "-finline-functions"; "-L" : "-Wl",
translate["-L"] = "-Wl"; "-lib" : "",
translate["-lib"] = ""; "-O" : "-O3",
translate["-O"] = "-O3"; "-o-" : "-fsyntax-only",
translate["-o-"] = "-fsyntax-only"; "-of" : "-o ",
translate["-of"] = "-o "; "-unittest" : "-funittest",
translate["-unittest"] = "-funittest"; "-version" : "-fversion=",
translate["-version"] = "-fversion="; "-version=" : "-fversion=",
translate["-version="] = "-fversion="; "-wi" : "-Wextra",
translate["-wi"] = "-Wextra"; "-w" : "-Wall",
translate["-w"] = "-Wall"; "-gc" : "-g"];
translate["-gc"] = "-g";
//Perform option translation //Perform option translation
foreach(ref option; result) foreach(ref option; result)
{ {
//remove unsupported -od //remove unsupported -od
if(startsWith(option, "-od")){option = "";} if(option.startsWith("-od")){option = "";}
if(option =="-D"){option = "-fdoc";} if(option == "-D"){option = "-fdoc";}
else
{
//Options with a direct translation //Options with a direct translation
foreach(before, after; translate) else foreach(before, after; translate)
{ {
if(startsWith(option, before)) if(option.startsWith(before))
{ {
option = after ~ option[before.length..$]; option = after ~ option[before.length..$];
break; break;
} }
} }
} }
}
return result; return result;
} }
unittest unittest
@ -619,7 +602,7 @@ void execute_compiler(in string compiler, string[] arguments)
{ {
version(Windows) version(Windows)
{ {
write("compile", join(arguments, " ")); write("compile", arguments.join(" "));
scope(exit){remove("compile");} scope(exit){remove("compile");}
execute(compiler ~ " ", ["@compile"]); execute(compiler ~ " ", ["@compile"]);
} }
@ -646,10 +629,10 @@ void execute(string command, string[] args)
{ {
version(Windows) version(Windows)
{ {
if(startsWith(command, "./")){command = command[2 .. $];} if(command.startsWith("./")){command = command[2 .. $];}
} }
string full = command ~ " " ~ join(args, " "); string full = command ~ " " ~ args.join(" ");
writeln("CDC: " ~ full); writeln("CDC: " ~ full);
if(int status = system(full) != 0) if(int status = system(full) != 0)
{ {
@ -673,7 +656,7 @@ string[] scan(in string directory, string extensions ...)
string[] result; string[] result;
foreach(string name; dirEntries(directory, SpanMode.depth)) foreach(string name; dirEntries(directory, SpanMode.depth))
{ {
if(isFile(name) && endsWith(name, extensions)){result ~= name;} if(isFile(name) && name.endsWith(extensions)){result ~= name;}
} }
return result; return result;
} }