generator.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. 
  2. #include "generator.h"
  3. using namespace dsr;
  4. struct Connection {
  5. String path;
  6. int64_t lineNumber = -1;
  7. int64_t dependencyIndex = -1;
  8. Connection(const ReadableString& path)
  9. : path(path) {}
  10. Connection(const ReadableString& path, int64_t lineNumber)
  11. : path(path), lineNumber(lineNumber) {}
  12. };
  13. enum class Extension {
  14. Unknown, H, Hpp, C, Cpp
  15. };
  16. static Extension extensionFromString(const ReadableString& extensionName) {
  17. String upperName = string_upperCase(string_removeOuterWhiteSpace(extensionName));
  18. Extension result = Extension::Unknown;
  19. if (string_match(upperName, U"H")) {
  20. result = Extension::H;
  21. } else if (string_match(upperName, U"HPP")) {
  22. result = Extension::Hpp;
  23. } else if (string_match(upperName, U"C")) {
  24. result = Extension::C;
  25. } else if (string_match(upperName, U"CPP")) {
  26. result = Extension::Cpp;
  27. }
  28. return result;
  29. }
  30. struct Dependency {
  31. String path;
  32. Extension extension;
  33. List<Connection> links; // Depends on having these linked after compiling.
  34. List<Connection> includes; // Depends on having these included in pre-processing.
  35. Dependency(const ReadableString& path, Extension extension)
  36. : path(path), extension(extension) {}
  37. };
  38. List<Dependency> dependencies;
  39. static int64_t findDependency(const ReadableString& findPath);
  40. static void resolveConnection(Connection &connection);
  41. static void resolveDependency(Dependency &dependency);
  42. static String findSourceFile(const ReadableString& headerPath, bool acceptC, bool acceptCpp);
  43. static void flushToken(List<String> &target, String &currentToken);
  44. static void tokenize(List<String> &target, const ReadableString& line);
  45. static void interpretPreprocessing(int64_t parentIndex, const List<String> &tokens, const ReadableString &parentFolder, int64_t lineNumber);
  46. static void interpretPreprocessing(int64_t parentIndex, const List<String> &tokens, const ReadableString &parentFolder, int64_t lineNumber);
  47. static void analyzeCode(int64_t parentIndex, String content, const ReadableString &parentFolder);
  48. static int64_t findDependency(const ReadableString& findPath) {
  49. for (int d = 0; d < dependencies.length(); d++) {
  50. if (string_match(dependencies[d].path, findPath)) {
  51. return d;
  52. }
  53. }
  54. return -1;
  55. }
  56. static void resolveConnection(Connection &connection) {
  57. connection.dependencyIndex = findDependency(connection.path);
  58. }
  59. static void resolveDependency(Dependency &dependency) {
  60. for (int l = 0; l < dependency.links.length(); l++) {
  61. resolveConnection(dependency.links[l]);
  62. }
  63. for (int i = 0; i < dependency.includes.length(); i++) {
  64. resolveConnection(dependency.includes[i]);
  65. }
  66. }
  67. void resolveDependencies() {
  68. for (int d = 0; d < dependencies.length(); d++) {
  69. resolveDependency(dependencies[d]);
  70. }
  71. }
  72. static String findSourceFile(const ReadableString& headerPath, bool acceptC, bool acceptCpp) {
  73. int lastDotIndex = string_findLast(headerPath, U'.');
  74. if (lastDotIndex != -1) {
  75. ReadableString extensionlessPath = string_removeOuterWhiteSpace(string_before(headerPath, lastDotIndex));
  76. String cPath = extensionlessPath + U".c";
  77. String cppPath = extensionlessPath + U".cpp";
  78. if (acceptC && file_getEntryType(cPath) == EntryType::File) {
  79. return cPath;
  80. } else if (acceptCpp && file_getEntryType(cppPath) == EntryType::File) {
  81. return cppPath;
  82. }
  83. }
  84. return U"";
  85. }
  86. static void flushToken(List<String> &target, String &currentToken) {
  87. if (string_length(currentToken) > 0) {
  88. target.push(currentToken);
  89. currentToken = U"";
  90. }
  91. }
  92. static void tokenize(List<String> &target, const ReadableString& line) {
  93. String currentToken;
  94. for (int i = 0; i < string_length(line); i++) {
  95. DsrChar c = line[i];
  96. DsrChar nextC = line[i + 1];
  97. if (c == U'#' && nextC == U'#') {
  98. // Appending tokens using ##
  99. i++;
  100. } else if (c == U'#' || c == U'(' || c == U')' || c == U'[' || c == U']' || c == U'{' || c == U'}') {
  101. // Atomic token of a single character
  102. flushToken(target, currentToken);
  103. string_appendChar(currentToken, c);
  104. flushToken(target, currentToken);
  105. } else if (c == U' ' || c == U'\t') {
  106. // Whitespace
  107. flushToken(target, currentToken);
  108. } else {
  109. string_appendChar(currentToken, c);
  110. }
  111. }
  112. flushToken(target, currentToken);
  113. }
  114. static void interpretPreprocessing(int64_t parentIndex, const List<String> &tokens, const ReadableString &parentFolder, int64_t lineNumber) {
  115. if (tokens.length() >= 3) {
  116. if (string_match(tokens[1], U"include")) {
  117. if (tokens[2][0] == U'\"') {
  118. String relativePath = string_unmangleQuote(tokens[2]);
  119. String absolutePath = file_getTheoreticalAbsolutePath(relativePath, parentFolder, LOCAL_PATH_SYNTAX);
  120. dependencies[parentIndex].includes.pushConstruct(absolutePath, lineNumber);
  121. analyzeFromFile(absolutePath);
  122. }
  123. }
  124. }
  125. }
  126. static void analyzeCode(int64_t parentIndex, String content, const ReadableString &parentFolder) {
  127. List<String> tokens;
  128. bool continuingLine = false;
  129. int64_t lineNumber = 0;
  130. string_split_callback(content, U'\n', true, [&parentIndex, &parentFolder, &tokens, &continuingLine, &lineNumber](ReadableString line) {
  131. lineNumber++;
  132. if (line[0] == U'#' || continuingLine) {
  133. tokenize(tokens, line);
  134. // Continuing pre-processing line using \ at the end.
  135. continuingLine = line[string_length(line) - 1] == U'\\';
  136. } else {
  137. continuingLine = false;
  138. }
  139. if (!continuingLine && tokens.length() > 0) {
  140. interpretPreprocessing(parentIndex, tokens, parentFolder, lineNumber);
  141. tokens.clear();
  142. }
  143. });
  144. }
  145. void analyzeFromFile(const ReadableString& absolutePath) {
  146. if (findDependency(absolutePath) != -1) {
  147. // Already analyzed the current entry. Abort to prevent duplicate dependencies.
  148. return;
  149. }
  150. int lastDotIndex = string_findLast(absolutePath, U'.');
  151. if (lastDotIndex != -1) {
  152. Extension extension = extensionFromString(string_after(absolutePath, lastDotIndex));
  153. if (extension != Extension::Unknown) {
  154. int64_t parentIndex = dependencies.length();
  155. dependencies.pushConstruct(absolutePath, extension);
  156. if (extension == Extension::H || extension == Extension::Hpp) {
  157. // The current file is a header, so look for an implementation with the corresponding name.
  158. String sourcePath = findSourceFile(absolutePath, extension == Extension::H, true);
  159. // If found:
  160. if (string_length(sourcePath) > 0) {
  161. // Remember that anything using the header will have to link with the implementation.
  162. dependencies[parentIndex].links.pushConstruct(sourcePath);
  163. // Look for included headers in the implementation file.
  164. analyzeFromFile(sourcePath);
  165. }
  166. }
  167. // Get the file's binary content for checksums.
  168. Buffer fileBuffer = file_loadBuffer(absolutePath);
  169. // TODO: Get a checksum of fileBuffer and compare with the previous state. Files that changed should recompile all object files that depend on it.
  170. // Interpret the file's content.
  171. analyzeCode(parentIndex, string_loadFromMemory(fileBuffer), file_getRelativeParentFolder(absolutePath));
  172. }
  173. }
  174. }
  175. static void debugPrintDependencyList(const List<Connection> &connnections, const ReadableString verb) {
  176. for (int c = 0; c < connnections.length(); c++) {
  177. int64_t lineNumber = connnections[c].lineNumber;
  178. if (lineNumber != -1) {
  179. printText(U" @", lineNumber, U"\t");
  180. } else {
  181. printText(U" \t");
  182. }
  183. printText(U" ", verb, U" ", file_getPathlessName(connnections[c].path), U"\n");
  184. }
  185. }
  186. void printDependencies() {
  187. for (int d = 0; d < dependencies.length(); d++) {
  188. printText(U"* ", file_getPathlessName(dependencies[d].path), U"\n");
  189. debugPrintDependencyList(dependencies[d].includes, U"including");
  190. debugPrintDependencyList(dependencies[d].links, U"linking");
  191. }
  192. }
  193. static ScriptLanguage identifyLanguage(const ReadableString filename) {
  194. String scriptExtension = string_upperCase(file_getExtension(filename));
  195. if (string_match(scriptExtension, U"BAT")) {
  196. return ScriptLanguage::Batch;
  197. } else if (string_match(scriptExtension, U"SH")) {
  198. return ScriptLanguage::Bash;
  199. } else {
  200. throwError(U"Could not identify the scripting language of ", filename, U". Use *.bat or *.sh.\n");
  201. return ScriptLanguage::Unknown;
  202. }
  203. }
  204. static void script_printMessage(String &output, ScriptLanguage language, const ReadableString message) {
  205. if (language == ScriptLanguage::Batch) {
  206. string_append(output, U"echo ", message, U"\n");
  207. } else if (language == ScriptLanguage::Bash) {
  208. string_append(output, U"echo ", message, U"\n");
  209. }
  210. }
  211. static void script_executeLocalBinary(String &output, ScriptLanguage language, const ReadableString code) {
  212. if (language == ScriptLanguage::Batch) {
  213. string_append(output, code, ".exe\n");
  214. } else if (language == ScriptLanguage::Bash) {
  215. string_append(output, file_combinePaths(U".", code), U";\n");
  216. }
  217. }
  218. // TODO: Make a checksum for binary buffers too, so that changes can be detected in a dependency graph for lazy compilation.
  219. static uint64_t checksum(const ReadableString& text) {
  220. uint64_t a = 0x8C2A03D4;
  221. uint64_t b = 0xF42B1583;
  222. uint64_t c = 0xA6815E74;
  223. uint64_t d = 0;
  224. for (int i = 0; i < string_length(text); i++) {
  225. a = (b * c + ((i * 3756 + 2654) & 58043)) & 0xFFFFFFFF;
  226. b = (231 + text[i] * (a & 154) + c * 867 + 28294061) & 0xFFFFFFFF;
  227. c = (a ^ b ^ (text[i] * 1543217521)) & 0xFFFFFFFF;
  228. d = d ^ (a << 32) ^ b ^ (c << 16);
  229. }
  230. return d;
  231. }
  232. struct SourceObject {
  233. // TODO: Assert that there are no name collisions between identity checksums.
  234. uint64_t identityChecksum = 0; // Identification number for the object's name.
  235. // TODO: Content checksum, dependency checksum.
  236. String sourcePath, objectPath;
  237. SourceObject(const ReadableString& sourcePath, const ReadableString& tempFolder, const ReadableString& identity)
  238. : identityChecksum(checksum(identity)), sourcePath(sourcePath) {
  239. // TODO: Include compiler flags in the checksum.
  240. this->objectPath = file_combinePaths(tempFolder, string_combine(U"dfpsr_builder_", identityChecksum, U".o"));
  241. }
  242. };
  243. void generateCompilationScript(const Machine &settings, const ReadableString& projectPath) {
  244. ReadableString scriptPath = getFlag(settings, U"ScriptPath", U"");
  245. ReadableString tempFolder = file_getAbsoluteParentFolder(scriptPath);
  246. if (string_length(scriptPath) == 0) {
  247. printText(U"No script path was given, skipping script generation\n");
  248. return;
  249. }
  250. ScriptLanguage language = identifyLanguage(scriptPath);
  251. scriptPath = file_getTheoreticalAbsolutePath(scriptPath, projectPath);
  252. // The compiler is often a global alias, so the user must supply either an alias or an absolute path.
  253. ReadableString compilerName = getFlag(settings, U"Compiler", U"g++"); // Assume g++ as the compiler if not specified.
  254. ReadableString compileFrom = getFlag(settings, U"CompileFrom", U"");
  255. // Check if the build system was asked to run the compiler from a specific folder.
  256. bool changePath = (string_length(compileFrom) > 0);
  257. if (changePath) {
  258. printText(U"Using ", compilerName, " as the compiler executed from ", compileFrom, ".\n");
  259. } else {
  260. printText(U"Using ", compilerName, " as the compiler from the current directory.\n");
  261. }
  262. // Convert lists of linker and compiler flags into strings.
  263. // TODO: Give a warning if two contradictory flags are used, such as optimization levels and language versions.
  264. // TODO: Make sure that no spaces are inside of the flags, because that can mess up detection of pre-existing and contradictory arguments.
  265. String compilerFlags;
  266. for (int i = 0; i < settings.compilerFlags.length(); i++) {
  267. string_append(compilerFlags, " ", settings.compilerFlags[i]);
  268. }
  269. String linkerFlags;
  270. for (int i = 0; i < settings.linkerFlags.length(); i++) {
  271. string_append(linkerFlags, " -l", settings.linkerFlags[i]);
  272. }
  273. // Interpret ProgramPath relative to the project path.
  274. ReadableString binaryPath = getFlag(settings, U"ProgramPath", language == ScriptLanguage::Batch ? U"program.exe" : U"program");
  275. binaryPath = file_getTheoreticalAbsolutePath(binaryPath, projectPath);
  276. String output;
  277. if (language == ScriptLanguage::Batch) {
  278. string_append(output, U"@echo off\n\n");
  279. } else if (language == ScriptLanguage::Bash) {
  280. string_append(output, U"#!/bin/bash\n\n");
  281. } else {
  282. printText(U"The type of script could not be identified for ", scriptPath, U"!\nUse *.bat for Batch or *.sh for Bash.\n");
  283. return;
  284. }
  285. List<SourceObject> sourceObjects;
  286. bool hasSourceCode = false;
  287. bool needCppCompiler = false;
  288. for (int d = 0; d < dependencies.length(); d++) {
  289. Extension extension = dependencies[d].extension;
  290. if (extension == Extension::Cpp) {
  291. needCppCompiler = true;
  292. }
  293. if (extension == Extension::C || extension == Extension::Cpp) {
  294. // Dependency paths are already absolute from the recursive search.
  295. String sourcePath = dependencies[d].path;
  296. sourceObjects.pushConstruct(sourcePath, tempFolder, string_combine(sourcePath, compilerFlags, projectPath));
  297. if (file_getEntryType(sourcePath) != EntryType::File) {
  298. throwError(U"The source file ", sourcePath, U" could not be found!\n");
  299. } else {
  300. hasSourceCode = true;
  301. }
  302. }
  303. }
  304. if (hasSourceCode) {
  305. // TODO: Give a warning if a known C compiler incapable of handling C++ is given C++ source code when needCppCompiler is true.
  306. if (changePath) {
  307. // Go into the requested folder.
  308. if (language == ScriptLanguage::Batch) {
  309. string_append(output, "pushd ", compileFrom, "\n");
  310. } else if (language == ScriptLanguage::Bash) {
  311. string_append(output, U"(cd ", compileFrom, ";\n");
  312. }
  313. }
  314. String allObjects;
  315. for (int i = 0; i < sourceObjects.length(); i++) {
  316. script_printMessage(output, language, string_combine(U"Compiling ", sourceObjects[i].sourcePath, U" ID:", sourceObjects[i].identityChecksum, U" with ", compilerFlags, U"."));
  317. string_append(output, compilerName, compilerFlags, U" -c ", sourceObjects[i].sourcePath, U" -o ", sourceObjects[i].objectPath, U"\n");
  318. string_append(allObjects, U" ", sourceObjects[i].objectPath);
  319. }
  320. script_printMessage(output, language, string_combine(U"Linking with ", linkerFlags, U"."));
  321. string_append(output, compilerName, allObjects, linkerFlags, U" -o ", binaryPath, U"\n");
  322. if (changePath) {
  323. // Get back to the previous folder.
  324. if (language == ScriptLanguage::Batch) {
  325. string_append(output, "popd\n");
  326. } else if (language == ScriptLanguage::Bash) {
  327. string_append(output, U")\n");
  328. }
  329. }
  330. script_printMessage(output, language, U"Done compiling.");
  331. script_printMessage(output, language, string_combine(U"Starting ", binaryPath));
  332. script_executeLocalBinary(output, language, binaryPath);
  333. script_printMessage(output, language, U"The program terminated.");
  334. if (language == ScriptLanguage::Batch) {
  335. // Windows might close the window before you have time to read the results or error messages of a CLI application, so pause at the end.
  336. string_append(output, U"pause\n");
  337. }
  338. if (language == ScriptLanguage::Batch) {
  339. string_save(scriptPath, output);
  340. } else if (language == ScriptLanguage::Bash) {
  341. string_save(scriptPath, output, CharacterEncoding::BOM_UTF8, LineEncoding::Lf);
  342. }
  343. } else {
  344. printText("Filed to find any source code to compile.\n");
  345. }
  346. }