1 module dco.buildExes;
2 
3 import core.time;
4 
5 import std.file;
6 import std..string;
7 import std.process;
8 import std.stdio;
9 import std.datetime;
10 import std.datetime.stopwatch;
11 import std.exception;
12 import dco.readConfigs;
13 import dco.globalInfo;
14 import dco.checkInfo;
15 import dco.operateFile;
16 
17 void buildExe(string[] args)
18 {
19 	string c;
20 	size_t p;
21 
22 	if(bGetLocal) goto build;
23 
24 	foreach(int i,arg;args)
25 	{
26 		if(i ==0) continue;
27 		c = toLower(arg);
28 		p = c.indexOf('-');
29 		if(p != -1)
30 		{
31 			c = c[p+1 .. c.length];
32 
33 			switch(c)
34 			{
35 				case "gui":
36 					strTargetLflags = strWindows;
37 					bUseSpecialLib = true;
38 					strAddArgs = strAddArgsdfl;
39 					break;
40 				case "use":
41 					bUseSpecialLib = true;
42 					strAddArgs = strAddArgsdfl;
43 					break;
44 				case "win","windows","winexe":
45 					strTargetLflags = strWindows;
46 					strLibs =strWinLibs;
47 					break;
48 				case "debug":
49 					bDebug = true;
50 					break;
51 				case "release":
52 					bDebug = false;
53 					buildMode = " -" ~ c.idup;
54 					break;
55 
56 				case "console","con","exe":
57 					strTargetLflags = strConsole;
58 					break;
59 				case "all":
60 					bUseSpecialLib = false;
61 					strAddLib = strLibs;
62 					strAddArgs = strAddArgsdfl;
63 					strImport = strImportDefault;
64 					strTargetLflags = strConsole;
65 					break;
66 				case "addlib":
67 					strAddLib = strLibs~" ";
68 					//strImport = strImportDefault;
69 					//strTargetLflags = strConsole;
70 					break;
71 				case "arg":
72 					strAddArgs = strAddArgsdfl;
73 					break;
74 				case "lib":
75 					strTargetTypeSwitch = " -" ~ targetTypeDefault;
76 					break;
77 				case "dfl","dfllib":
78 					SpecialLib = "dfl";
79 					bBuildSpecialLib = true;
80 					strTargetTypeSwitch = " -" ~ targetTypeDefault;
81 					break;
82 				case "dgui","dguilib":
83 					SpecialLib = "dgui";
84 					bBuildSpecialLib = true;
85 					strTargetTypeSwitch = " -" ~ targetTypeDefault;
86 					break;
87 				case "copy":
88 					bCopy = true;
89 					break;
90 				case "force":
91 					bForce = true;
92 					break;
93 				case "shared":
94 					strTargetTypeSwitch = " -" ~ targetTypeShared;
95 					strTargetType = "dll";
96 					break;
97 				default:
98 					if(c == "m64" || c == "m32mscoff")
99 					{ 
100 						compileType =c[1..$];
101 					}
102 					strOtherArgs ~= " ";
103 					strOtherArgs ~= arg;
104 					break;
105     		}
106     	}
107 	}
108 
109 	strTargetLib = bDebug ? SpecialLib ~ "_debug" ~ compileType ~ ".lib" : SpecialLib ~ compileType ~ ".lib" ;
110 
111 	if(bBuildSpecialLib)
112 	{
113 		strOtherArgs ~= " -of" ~ strTargetPath ~ separator ~ strTargetLib;
114 		strAddLib = strLibs;
115 		strTargetFileName = getcwd() ~ separator ~ strTargetLib;
116 	}
117 	else
118 	{
119 		strTargetFileName = getcwd() ~ separator ~ strTargetName;
120 	}
121 
122 
123     if(strTargetLflags == "" && strLflags !="")
124 	{
125 		strTargetLflags = strLflags;
126 	}
127 
128 	if(strTargetLflags == "" && strLflags =="")
129 	{
130 		if(bUseSpecialLib) 
131 			strTargetLflags = strWindows;
132 		else
133 			strTargetLflags = strConsole;
134 	}
135 
136 build: 
137 
138 	if(buildMode.toLower().indexOf("debug") != -1)
139 	{
140 		strOtherArgs ~= " -g";
141 	}
142 
143 	if(strDflags != "")
144     {
145     	strOtherArgs ~= " ";
146     	strOtherArgs ~= strDflags;
147     }
148 
149 	if(strObjPath != "")
150 	{
151 		strOtherArgs ~= " -od";
152     	strOtherArgs ~= strObjPath;
153 	}
154 
155 	if(bUseSpecialLib)
156 	{
157 		writeln("dco.ini's SpecialLib is ",SpecialLib);
158 		if(SpecialLib == "dfl" || SpecialLib == "dgui")
159 		{
160 			strTargetLib = bDebug ? SpecialLib ~ "_debug" ~ compileType ~ ".lib" : SpecialLib ~ compileType ~ ".lib" ;
161 			strLibs =strWinLibs;
162 		}
163 
164 		if(SpecialLib !="")
165 		{
166 			strAddLib = strLibs ~" " ~ strTargetLib ;
167 		}
168 	}
169 	else
170 	{
171 		strAddLib = strLibs;
172 	}
173 
174 	if(compileType == "64")
175 	{
176 		if(strip(strTargetLflags) !=strip(strConsole))
177 		{
178 			if(strAddLib.indexOf(strip(strWinLibs64)) == -1)
179 			{
180 				strAddLib ~= strWinLibs64;
181 			}
182 
183 		    strTargetLflags = strWindows64;
184 		}
185 		else //console x64 not set
186 		{
187 			strTargetLflags= "";
188 		}
189 	} 
190 
191 	buildExe();
192 }
193 
194 void buildExe()
195 {
196 	if(bForce)
197 	{
198 		removeExe(strTargetFileName);
199 	}
200 	strDC ~= " ";
201 	strDC ~= strTargetTypeSwitch;
202 	string strCommon = strOtherArgs ~" " ~ strImportDefault ~ strImport ~ " " ~ strAddLib ~ strTargetLflags ~ strDFile ~ buildMode;
203     string buildstr = strDC ~ strAddArgsdfl ~ strCommon ~ "\r\n";
204 	buildstr = bUseSpecialLib ? buildstr : strDC ~ strCommon;
205 
206 	std.datetime.stopwatch.StopWatch sw;
207 	sw.start();
208 	auto pid =  enforce(spawnShell(buildstr.dup()),"build function is error! ");
209 
210 	if (wait(pid) != 0)
211 	{
212 		writeln("\n"~ buildstr);
213 		writeln("\nCompilation failed:\n", pid);
214 
215 		auto err = File("ErrBuild.txt","w"); 
216 		scope(failure) err.close();
217 		err.writeln(buildstr);
218 		err.close();
219 	}
220 	else
221 	{
222 		sw.stop();
223 
224 		writeln(buildstr);
225 		writeln("\nCompile time :" , sw.peek().total!"seconds","secs");
226 
227 		if(bCopy)
228 		{
229 			copyFile();
230 		}
231 	}
232 
233 	writeln("End.");
234 }