1 module build;
2 
3 version = showBuildString;
4 
5 import std.file : dirEntries, SpanMode;
6 import std.process : executeShell;
7 import std.stdio : writeln;
8 
9 import std.algorithm : findSplitBefore;
10 import std.range : retro, chain;
11 import std.array : array;
12 import std.conv : to;
13 
14 enum 
15 {
16 	executable,
17 	staticLib,
18 	sharedLib,
19 }
20 
21 alias pack = packageSettings;
22 struct packageSettings
23 {
24 	string name;
25 	string sourcePath;
26 	string[] importPaths;
27 	string[] libFiles;
28 	string outputName;
29 	uint targetType;
30 	string linkerFlags;
31 }
32 
33 version(Windows)
34 {
35 	enum exeSuffix = ".exe";
36 	enum exePrefix = "";
37 	enum staticLibSuffix = ".lib";
38 	enum staticLibPrefix = "";
39 	enum sharedLibSuffix = ".dll";
40 	enum sharedLibPrefix = "";
41 }
42 version(linux)
43 {
44 	enum exeSuffix = "";
45 	enum exePrefix = "";
46 	enum staticLibSuffix = ".a";
47 	enum staticLibPrefix = "lib";
48 	enum sharedLibSuffix = ".so";
49 	enum sharedLibPrefix = "lib";
50 }
51 
52 string withSuffixPrefix(string filePath, string prefix, string suffix)
53 {
54 	auto splitted = filePath.retro.findSplitBefore("/");
55 
56     return chain(splitted[1].retro,
57 		prefix,
58 		splitted[0].array.retro,
59 		suffix).array.to!string;
60 }
61 
62 void buildPackage(ref packageSettings settings, string flags)
63 {
64 	string buildString = "dmd"~exeSuffix~" "~flags~" ";
65 	if (settings.targetType == staticLib) buildString ~= "-lib ";
66 	
67 	foreach(string filename; dirEntries(settings.sourcePath, "*.d", SpanMode.depth))
68 	{
69 		buildString ~= '"'~filename~"\" ";
70 	}
71 
72 	foreach(path; settings.importPaths)
73 	{
74 		buildString ~= "-I\""~path~"\" ";
75 	}
76 	
77 	foreach(lib; settings.libFiles)
78 	{
79 		buildString ~= "\""~withSuffixPrefix(lib, staticLibPrefix, staticLibSuffix)~"\" ";
80 	}
81 
82 	buildString ~= settings.linkerFlags;
83 
84 	buildString ~= " -of\"";
85 
86 	switch(settings.targetType)
87 	{
88 		case executable: buildString ~= withSuffixPrefix(settings.outputName, exePrefix, exeSuffix) ~ "\""; break;
89 		case staticLib: buildString ~= withSuffixPrefix(settings.outputName, staticLibPrefix, staticLibSuffix) ~ "\""; break;
90 		case sharedLib: buildString ~= withSuffixPrefix(settings.outputName, sharedLibPrefix, sharedLibSuffix) ~ "\""; break;
91 		default: assert(false);
92 	}
93 	
94 	version(showBuildString) writeln(buildString);
95 	
96 	auto result = executeShell(buildString);
97 	if (result.status != 0)
98 	{
99 		writeln("Compilation failed:\n"~result.output);
100 	}
101 }
102 
103 void main()
104 {
105 	auto imports = ["import", "deps/dlib", "deps/derelict-fi-master/source", "deps/derelict-sdl2-master/source", "deps/derelict-ft-master/source", "deps/derelict-gl3-master/source", "deps/derelict-glfw3-master/source", "deps/derelict-util-1.0.0/source", "deps/sdlang-d-0.8.4/src"];
106 	auto packages = [
107 	pack("core", "import/anchovy/core", imports, [], "lib/debug/core", staticLib), 
108 	pack("graphics", "import/anchovy/graphics", imports,[], "lib/debug/graphics", staticLib), 
109 	pack("gui", "import/anchovy/gui", imports,[], "lib/debug/gui", staticLib), 
110 	pack("utils", "import/anchovy/utils", imports,[], "lib/debug/utils", staticLib), 
111 	pack("examples", "examples", imports,
112 		["deps/derelict-util-1.0.0/lib/DerelictUtil","deps/derelict-glfw3-master/lib/DerelictGLFW3", "deps/derelict-gl3-master/lib/DerelictGL3", "deps/derelict-ft-master/lib/DerelictFT", "deps/derelict-fi-master/lib/DerelictFI", "deps/dlib/dlib","lib/debug/utils", "lib/debug/core", "lib/debug/graphics", "deps/sdlang-d-0.8.4/sdlang-d", "lib/debug/gui"].retro.array, "bin/guidemo", executable)];
113 	
114 	foreach(ref pack; packages)
115 		buildPackage(pack, "-debug -gc -m32");
116 }