MakefileTarget.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #region BSD License
  2. /*
  3. Copyright (c) 2004 Crestez Leonard ([email protected])
  4. Redistribution and use in source and binary forms, with or without modification, are permitted
  5. provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice, this list of conditions
  7. and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  9. and the following disclaimer in the documentation and/or other materials provided with the
  10. distribution.
  11. * The name of the author may not be used to endorse or promote products derived from this software
  12. without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  14. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  17. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  18. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  19. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. #endregion
  22. using System;
  23. using System.IO;
  24. using System.Text.RegularExpressions;
  25. using Prebuild.Core.Attributes;
  26. using Prebuild.Core.Interfaces;
  27. using Prebuild.Core.Nodes;
  28. using Prebuild.Core.Utilities;
  29. namespace Prebuild.Core.Targets
  30. {
  31. [Target("makefile")]
  32. public class MakefileTarget : ITarget
  33. {
  34. #region Fields
  35. private Kernel m_Kernel = null;
  36. #endregion
  37. #region Private Methods
  38. // This converts a path relative to the path of a project to
  39. // a path relative to the solution path.
  40. private string NicePath(ProjectNode proj, string path)
  41. {
  42. string res;
  43. SolutionNode solution = (SolutionNode)proj.Parent;
  44. res = Path.Combine(Helper.NormalizePath(proj.FullPath, '/'), Helper.NormalizePath(path, '/'));
  45. res = Helper.NormalizePath(res, '/');
  46. res = res.Replace("/./", "/");
  47. while (res.IndexOf("/../") >= 0)
  48. {
  49. int a = res.IndexOf("/../");
  50. int b = res.LastIndexOf("/", a - 1);
  51. res = res.Remove(b, a - b + 3);
  52. }
  53. res = Helper.MakePathRelativeTo(solution.FullPath, res);
  54. if (res.StartsWith("./"))
  55. res = res.Substring(2, res.Length - 2);
  56. res = Helper.NormalizePath(res, '/');
  57. return res;
  58. }
  59. private void WriteProjectFiles(StreamWriter f, SolutionNode solution, ProjectNode project)
  60. {
  61. // Write list of source code files
  62. f.WriteLine("SOURCES_{0} = \\", project.Name);
  63. foreach (string file in project.Files)
  64. if (project.Files.GetBuildAction(file) == BuildAction.Compile)
  65. f.WriteLine("\t{0} \\", NicePath(project, file));
  66. f.WriteLine();
  67. // Write list of resource files
  68. f.WriteLine("RESOURCES_{0} = \\", project.Name);
  69. foreach (string file in project.Files)
  70. if (project.Files.GetBuildAction(file) == BuildAction.EmbeddedResource)
  71. {
  72. string path = NicePath(project, file);
  73. f.WriteLine("\t-resource:{0},{1} \\", path, Path.GetFileName(path));
  74. }
  75. f.WriteLine();
  76. // There's also Content and None in BuildAction.
  77. // What am I supposed to do with that?
  78. }
  79. private string FindFileReference(string refName, ProjectNode project)
  80. {
  81. foreach (ReferencePathNode refPath in project.ReferencePaths)
  82. {
  83. string fullPath = NicePath(project, Helper.MakeFilePath(refPath.Path, refName, "dll"));
  84. if (File.Exists(fullPath))
  85. return fullPath;
  86. }
  87. return null;
  88. }
  89. private void WriteProjectReferences(StreamWriter f, SolutionNode solution, ProjectNode project)
  90. {
  91. f.WriteLine("REFERENCES_{0} = \\", project.Name);
  92. foreach (ReferenceNode refr in project.References)
  93. {
  94. string path;
  95. // Project references change with configurations.
  96. if (solution.ProjectsTable.ContainsKey(refr.Name))
  97. continue;
  98. path = FindFileReference(refr.Name, project);
  99. if (path != null)
  100. f.WriteLine("\t-r:{0} \\", path);
  101. else
  102. f.WriteLine("\t-r:{0} \\", refr.Name);
  103. }
  104. f.WriteLine();
  105. }
  106. private void WriteProjectDependencies(StreamWriter f, SolutionNode solution, ProjectNode project)
  107. {
  108. f.WriteLine("DEPENDENCIES_{0} = \\", project.Name);
  109. f.WriteLine("\t$(SOURCES_{0}) \\", project.Name);
  110. foreach (string file in project.Files)
  111. if (project.Files.GetBuildAction(file) == BuildAction.EmbeddedResource)
  112. f.WriteLine("\t{0} \\", NicePath(project, file));
  113. f.WriteLine();
  114. }
  115. private string ProjectTypeToExtension(ProjectType t)
  116. {
  117. if (t == ProjectType.Exe || t == ProjectType.WinExe)
  118. {
  119. return "exe";
  120. }
  121. else if (t == ProjectType.Library)
  122. {
  123. return "dll";
  124. }
  125. else
  126. {
  127. throw new FatalException("Bad ProjectType: {0}", t);
  128. }
  129. }
  130. private string ProjectTypeToTarget(ProjectType t)
  131. {
  132. if (t == ProjectType.Exe)
  133. {
  134. return "exe";
  135. }
  136. else if (t == ProjectType.WinExe)
  137. {
  138. return "winexe";
  139. }
  140. else if (t == ProjectType.Library)
  141. {
  142. return "library";
  143. }
  144. else
  145. {
  146. throw new FatalException("Bad ProjectType: {0}", t);
  147. }
  148. }
  149. private string ProjectOutput(ProjectNode project, ConfigurationNode config)
  150. {
  151. string filepath;
  152. filepath = Helper.MakeFilePath((string)config.Options["OutputPath"],
  153. project.AssemblyName, ProjectTypeToExtension(project.Type));
  154. return NicePath(project, filepath);
  155. }
  156. // Returns true if two configs in one project have the same output.
  157. private bool ProjectClashes(ProjectNode project)
  158. {
  159. foreach (ConfigurationNode conf1 in project.Configurations)
  160. foreach (ConfigurationNode conf2 in project.Configurations)
  161. if (ProjectOutput(project, conf1) == ProjectOutput(project, conf2) && conf1 != conf2)
  162. {
  163. m_Kernel.Log.Write("Warning: Configurations {0} and {1} for project {2} output the same file",
  164. conf1.Name, conf2.Name, project.Name);
  165. m_Kernel.Log.Write("Warning: I'm going to use some timestamps(extra empty files).");
  166. return true;
  167. }
  168. return false;
  169. }
  170. private void WriteProject(StreamWriter f, SolutionNode solution, ProjectNode project)
  171. {
  172. f.WriteLine("# This is for project {0}", project.Name);
  173. f.WriteLine();
  174. WriteProjectFiles(f, solution, project);
  175. WriteProjectReferences(f, solution, project);
  176. WriteProjectDependencies(f, solution, project);
  177. bool clash = ProjectClashes(project);
  178. foreach (ConfigurationNode conf in project.Configurations)
  179. {
  180. string outpath = ProjectOutput(project, conf);
  181. string filesToClean = outpath;
  182. if (clash)
  183. {
  184. f.WriteLine("{0}-{1}: .{0}-{1}-timestamp", project.Name, conf.Name);
  185. f.WriteLine();
  186. f.Write(".{0}-{1}-timestamp: $(DEPENDENCIES_{0})", project.Name, conf.Name);
  187. }
  188. else
  189. {
  190. f.WriteLine("{0}-{1}: {2}", project.Name, conf.Name, outpath);
  191. f.WriteLine();
  192. f.Write("{2}: $(DEPENDENCIES_{0})", project.Name, conf.Name, outpath);
  193. }
  194. // Dependencies on other projects.
  195. foreach (ReferenceNode refr in project.References)
  196. if (solution.ProjectsTable.ContainsKey(refr.Name))
  197. {
  198. ProjectNode refProj = (ProjectNode)solution.ProjectsTable[refr.Name];
  199. if (ProjectClashes(refProj))
  200. f.Write(" .{0}-{1}-timestamp", refProj.Name, conf.Name);
  201. else
  202. f.Write(" {0}", ProjectOutput(refProj, conf));
  203. }
  204. f.WriteLine();
  205. // make directory for output.
  206. if (Path.GetDirectoryName(outpath) != "")
  207. {
  208. f.WriteLine("\tmkdir -p {0}", Path.GetDirectoryName(outpath));
  209. }
  210. // mcs command line.
  211. f.Write("\tgmcs", project.Name);
  212. f.Write(" -warn:{0}", conf.Options["WarningLevel"]);
  213. if ((bool)conf.Options["DebugInformation"])
  214. f.Write(" -debug");
  215. if ((bool)conf.Options["AllowUnsafe"])
  216. f.Write(" -unsafe");
  217. if ((bool)conf.Options["CheckUnderflowOverflow"])
  218. f.Write(" -checked");
  219. if (project.StartupObject != "")
  220. f.Write(" -main:{0}", project.StartupObject);
  221. if ((string)conf.Options["CompilerDefines"] != "")
  222. {
  223. f.Write(" -define:\"{0}\"", conf.Options["CompilerDefines"]);
  224. }
  225. f.Write(" -target:{0} -out:{1}", ProjectTypeToTarget(project.Type), outpath);
  226. // Build references to other projects. Now that sux.
  227. // We have to reference the other project in the same conf.
  228. foreach (ReferenceNode refr in project.References)
  229. if (solution.ProjectsTable.ContainsKey(refr.Name))
  230. {
  231. ProjectNode refProj;
  232. refProj = (ProjectNode)solution.ProjectsTable[refr.Name];
  233. f.Write(" -r:{0}", ProjectOutput(refProj, conf));
  234. }
  235. f.Write(" $(REFERENCES_{0})", project.Name);
  236. f.Write(" $(RESOURCES_{0})", project.Name);
  237. f.Write(" $(SOURCES_{0})", project.Name);
  238. f.WriteLine();
  239. // Copy references with localcopy.
  240. foreach (ReferenceNode refr in project.References)
  241. if (refr.LocalCopy)
  242. {
  243. string outPath, srcPath, destPath;
  244. outPath = Helper.NormalizePath((string)conf.Options["OutputPath"]);
  245. if (solution.ProjectsTable.ContainsKey(refr.Name))
  246. {
  247. ProjectNode refProj;
  248. refProj = (ProjectNode)solution.ProjectsTable[refr.Name];
  249. srcPath = ProjectOutput(refProj, conf);
  250. destPath = Path.Combine(outPath, Path.GetFileName(srcPath));
  251. destPath = NicePath(project, destPath);
  252. if (srcPath != destPath)
  253. {
  254. f.WriteLine("\tcp -f {0} {1}", srcPath, destPath);
  255. filesToClean += " " + destPath;
  256. }
  257. continue;
  258. }
  259. srcPath = FindFileReference(refr.Name, project);
  260. if (srcPath != null)
  261. {
  262. destPath = Path.Combine(outPath, Path.GetFileName(srcPath));
  263. destPath = NicePath(project, destPath);
  264. f.WriteLine("\tcp -f {0} {1}", srcPath, destPath);
  265. filesToClean += " " + destPath;
  266. }
  267. }
  268. if (clash)
  269. {
  270. filesToClean += String.Format(" .{0}-{1}-timestamp", project.Name, conf.Name);
  271. f.WriteLine("\ttouch .{0}-{1}-timestamp", project.Name, conf.Name);
  272. f.Write("\trm -rf");
  273. foreach (ConfigurationNode otherConf in project.Configurations)
  274. if (otherConf != conf)
  275. f.WriteLine(" .{0}-{1}-timestamp", project.Name, otherConf.Name);
  276. f.WriteLine();
  277. }
  278. f.WriteLine();
  279. f.WriteLine("{0}-{1}-clean:", project.Name, conf.Name);
  280. f.WriteLine("\trm -rf {0}", filesToClean);
  281. f.WriteLine();
  282. }
  283. }
  284. private void WriteIntro(StreamWriter f, SolutionNode solution)
  285. {
  286. f.WriteLine("# Makefile for {0} generated by Prebuild ( http://dnpb.sf.net )", solution.Name);
  287. f.WriteLine("# Do not edit.");
  288. f.WriteLine("#");
  289. f.Write("# Configurations:");
  290. foreach (ConfigurationNode conf in solution.Configurations)
  291. f.Write(" {0}", conf.Name);
  292. f.WriteLine();
  293. f.WriteLine("# Projects:");
  294. foreach (ProjectNode proj in solution.Projects)
  295. f.WriteLine("#\t{0}", proj.Name);
  296. f.WriteLine("#");
  297. f.WriteLine("# Building:");
  298. f.WriteLine("#\t\"make\" to build everything under the default(first) configuration");
  299. f.WriteLine("#\t\"make CONF\" to build every project under configuration CONF");
  300. f.WriteLine("#\t\"make PROJ\" to build project PROJ under the default(first) configuration");
  301. f.WriteLine("#\t\"make PROJ-CONF\" to build project PROJ under configuration CONF");
  302. f.WriteLine("#");
  303. f.WriteLine("# Cleaning (removing results of build):");
  304. f.WriteLine("#\t\"make clean\" to clean everything, that's what you probably want");
  305. f.WriteLine("#\t\"make CONF\" to clean everything for a configuration");
  306. f.WriteLine("#\t\"make PROJ\" to clean everything for a project");
  307. f.WriteLine("#\t\"make PROJ-CONF\" to clea project PROJ under configuration CONF");
  308. f.WriteLine();
  309. }
  310. private void WritePhony(StreamWriter f, SolutionNode solution)
  311. {
  312. string defconf = "";
  313. foreach (ConfigurationNode conf in solution.Configurations)
  314. {
  315. defconf = conf.Name;
  316. break;
  317. }
  318. f.Write(".PHONY: all");
  319. foreach (ProjectNode proj in solution.Projects)
  320. f.Write(" {0} {0}-clean", proj.Name);
  321. foreach (ConfigurationNode conf in solution.Configurations)
  322. f.Write(" {0} {0}-clean", conf.Name);
  323. foreach (ProjectNode proj in solution.Projects)
  324. foreach (ConfigurationNode conf in solution.Configurations)
  325. f.Write(" {0}-{1} {0}-{1}-clean", proj.Name, conf.Name);
  326. f.WriteLine();
  327. f.WriteLine();
  328. f.WriteLine("all: {0}", defconf);
  329. f.WriteLine();
  330. f.Write("clean:");
  331. foreach (ConfigurationNode conf in solution.Configurations)
  332. f.Write(" {0}-clean", conf.Name);
  333. f.WriteLine();
  334. f.WriteLine();
  335. foreach (ConfigurationNode conf in solution.Configurations)
  336. {
  337. f.Write("{0}: ", conf.Name);
  338. foreach (ProjectNode proj in solution.Projects)
  339. f.Write(" {0}-{1}", proj.Name, conf.Name);
  340. f.WriteLine();
  341. f.WriteLine();
  342. f.Write("{0}-clean: ", conf.Name);
  343. foreach (ProjectNode proj in solution.Projects)
  344. f.Write(" {0}-{1}-clean", proj.Name, conf.Name);
  345. f.WriteLine();
  346. f.WriteLine();
  347. }
  348. foreach (ProjectNode proj in solution.Projects)
  349. {
  350. f.WriteLine("{0}: {0}-{1}", proj.Name, defconf);
  351. f.WriteLine();
  352. f.Write("{0}-clean:", proj.Name);
  353. foreach (ConfigurationNode conf in proj.Configurations)
  354. f.Write(" {0}-{1}-clean", proj.Name, conf.Name);
  355. f.WriteLine();
  356. f.WriteLine();
  357. }
  358. }
  359. private void WriteSolution(SolutionNode solution)
  360. {
  361. m_Kernel.Log.Write("Creating makefile for {0}", solution.Name);
  362. m_Kernel.CurrentWorkingDirectory.Push();
  363. string file = "Makefile";// Helper.MakeFilePath(solution.FullPath, solution.Name, "make");
  364. StreamWriter f = new StreamWriter(file);
  365. Helper.SetCurrentDir(Path.GetDirectoryName(file));
  366. using (f)
  367. {
  368. WriteIntro(f, solution);
  369. WritePhony(f, solution);
  370. foreach (ProjectNode project in solution.Projects)
  371. {
  372. m_Kernel.Log.Write("...Creating Project: {0}", project.Name);
  373. WriteProject(f, solution, project);
  374. }
  375. }
  376. m_Kernel.Log.Write("");
  377. m_Kernel.CurrentWorkingDirectory.Pop();
  378. }
  379. private void CleanSolution(SolutionNode solution)
  380. {
  381. m_Kernel.Log.Write("Cleaning makefile for {0}", solution.Name);
  382. string file = Helper.MakeFilePath(solution.FullPath, solution.Name, "make");
  383. Helper.DeleteIfExists(file);
  384. m_Kernel.Log.Write("");
  385. }
  386. #endregion
  387. #region ITarget Members
  388. public void Write(Kernel kern)
  389. {
  390. m_Kernel = kern;
  391. foreach (SolutionNode solution in kern.Solutions)
  392. WriteSolution(solution);
  393. m_Kernel = null;
  394. }
  395. public virtual void Clean(Kernel kern)
  396. {
  397. m_Kernel = kern;
  398. foreach (SolutionNode sol in kern.Solutions)
  399. CleanSolution(sol);
  400. m_Kernel = null;
  401. }
  402. public string Name
  403. {
  404. get
  405. {
  406. return "makefile";
  407. }
  408. }
  409. #endregion
  410. }
  411. }