generator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. void generateCompilationScript(const Machine &settings, const ReadableString& projectPath) {
  219. ReadableString scriptPath = getFlag(settings, U"ScriptPath", U"");
  220. if (string_length(scriptPath) == 0) {
  221. printText(U"No script path was given, skipping script generation");
  222. return;
  223. }
  224. ScriptLanguage language = identifyLanguage(scriptPath);
  225. scriptPath = file_getTheoreticalAbsolutePath(scriptPath, projectPath);
  226. // The compiler is often a global alias, so the user must supply either an alias or an absolute path.
  227. ReadableString compilerName = getFlag(settings, U"Compiler", U"g++"); // Assume g++ as the compiler if not specified.
  228. // Convert lists of linker and compiler flags into strings.
  229. // TODO: Give a warning if two contradictory flags are used, such as optimization levels and language versions.
  230. // TODO: Make sure that no spaces are inside of the flags, because that can mess up detection of pre-existing and contradictory arguments.
  231. String compilerFlags;
  232. for (int i = 0; i < settings.compilerFlags.length(); i++) {
  233. string_append(compilerFlags, " ", settings.compilerFlags[i]);
  234. }
  235. String linkerFlags;
  236. for (int i = 0; i < settings.linkerFlags.length(); i++) {
  237. string_append(linkerFlags, " ", settings.linkerFlags[i]);
  238. }
  239. // Interpret ProgramPath relative to the project path.
  240. ReadableString binaryPath = getFlag(settings, U"ProgramPath", language == ScriptLanguage::Batch ? U"program.exe" : U"program");
  241. binaryPath = file_getTheoreticalAbsolutePath(binaryPath, projectPath);
  242. String output;
  243. if (language == ScriptLanguage::Batch) {
  244. string_append(output, U"@echo off\n\n");
  245. } else if (language == ScriptLanguage::Bash) {
  246. string_append(output, U"#!/bin/bash\n\n");
  247. } else {
  248. printText(U"The type of script could not be identified for ", scriptPath, U"!\nUse *.bat for Batch or *.sh for Bash.\n");
  249. return;
  250. }
  251. String compiledFiles;
  252. bool needCppCompiler = false;
  253. for (int d = 0; d < dependencies.length(); d++) {
  254. Extension extension = dependencies[d].extension;
  255. if (extension == Extension::Cpp) {
  256. needCppCompiler = true;
  257. }
  258. if (extension == Extension::C || extension == Extension::Cpp) {
  259. // Dependency paths are already absolute from the recursive search.
  260. String sourcePath = dependencies[d].path;
  261. string_append(compiledFiles, U" ", sourcePath);
  262. if (file_getEntryType(sourcePath) != EntryType::File) {
  263. throwError(U"The source file ", sourcePath, U" could not be found!\n");
  264. }
  265. }
  266. }
  267. // TODO: Give a warning if a known C compiler incapable of handling C++ is given C++ source code when needCppCompiler is true.
  268. script_printMessage(output, language, string_combine(U"Compiling with", compilerFlags, linkerFlags));
  269. string_append(output, compilerName, U" -o ", binaryPath, compilerFlags, linkerFlags, " ", compiledFiles, U"\n");
  270. script_printMessage(output, language, U"Done compiling.");
  271. script_printMessage(output, language, string_combine(U"Starting ", binaryPath));
  272. script_executeLocalBinary(output, language, binaryPath);
  273. script_printMessage(output, language, U"The program terminated.");
  274. if (language == ScriptLanguage::Batch) {
  275. // 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.
  276. string_append(output, U"pause\n");
  277. }
  278. if (language == ScriptLanguage::Batch) {
  279. string_save(scriptPath, output);
  280. } else if (language == ScriptLanguage::Bash) {
  281. string_save(scriptPath, output, CharacterEncoding::BOM_UTF8, LineEncoding::Lf);
  282. }
  283. }