generator.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. 
  2. #include "generator.h"
  3. using namespace dsr;
  4. static uint64_t checksum(const ReadableString& text) {
  5. uint64_t a = 0x8C2A03D4;
  6. uint64_t b = 0xF42B1583;
  7. uint64_t c = 0xA6815E74;
  8. uint64_t d = 0;
  9. for (int64_t i = 0; i < string_length(text); i++) {
  10. a = (b * c + ((i * 3756 + 2654) & 58043)) & 0xFFFFFFFF;
  11. b = (231 + text[i] * (a & 154) + c * 867 + 28294061) & 0xFFFFFFFF;
  12. c = (a ^ b ^ (text[i] * 1543217521)) & 0xFFFFFFFF;
  13. d = d ^ (a << 32) ^ b ^ (c << 16);
  14. }
  15. return d;
  16. }
  17. static uint64_t checksum(const Buffer& buffer) {
  18. SafePointer<uint8_t> data = buffer_getSafeData<uint8_t>(buffer, "checksum input buffer");
  19. uint64_t a = 0x8C2A03D4;
  20. uint64_t b = 0xF42B1583;
  21. uint64_t c = 0xA6815E74;
  22. uint64_t d = 0;
  23. for (int64_t i = 0; i < buffer_getSize(buffer); i++) {
  24. a = (b * c + ((i * 3756 + 2654) & 58043)) & 0xFFFFFFFF;
  25. b = (231 + data[i] * (a & 154) + c * 867 + 28294061) & 0xFFFFFFFF;
  26. c = (a ^ b ^ (data[i] * 1543217521)) & 0xFFFFFFFF;
  27. d = d ^ (a << 32) ^ b ^ (c << 16);
  28. }
  29. return d;
  30. }
  31. static int64_t findDependency(ProjectContext &context, const ReadableString& findPath);
  32. static void resolveConnection(Connection &connection);
  33. static void resolveDependency(Dependency &dependency);
  34. static String findSourceFile(const ReadableString& headerPath, bool acceptC, bool acceptCpp);
  35. static void flushToken(List<String> &target, String &currentToken);
  36. static void tokenize(List<String> &target, const ReadableString& line);
  37. static int64_t findDependency(ProjectContext &context, const ReadableString& findPath) {
  38. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  39. if (string_match(context.dependencies[d].path, findPath)) {
  40. return d;
  41. }
  42. }
  43. return -1;
  44. }
  45. static void resolveConnection(ProjectContext &context, Connection &connection) {
  46. connection.dependencyIndex = findDependency(context, connection.path);
  47. }
  48. static void resolveDependency(ProjectContext &context, Dependency &dependency) {
  49. for (int64_t l = 0; l < dependency.links.length(); l++) {
  50. resolveConnection(context, dependency.links[l]);
  51. }
  52. for (int64_t i = 0; i < dependency.includes.length(); i++) {
  53. resolveConnection(context, dependency.includes[i]);
  54. }
  55. }
  56. void resolveDependencies(ProjectContext &context) {
  57. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  58. resolveDependency(context, context.dependencies[d]);
  59. }
  60. }
  61. static String findSourceFile(const ReadableString& headerPath, bool acceptC, bool acceptCpp) {
  62. if (file_hasExtension(headerPath)) {
  63. ReadableString extensionlessPath = file_getExtensionless(headerPath);
  64. String cPath = extensionlessPath + U".c";
  65. String cppPath = extensionlessPath + U".cpp";
  66. if (acceptC && file_getEntryType(cPath) == EntryType::File) {
  67. return cPath;
  68. } else if (acceptCpp && file_getEntryType(cppPath) == EntryType::File) {
  69. return cppPath;
  70. }
  71. }
  72. return U"";
  73. }
  74. static void flushToken(List<String> &target, String &currentToken) {
  75. if (string_length(currentToken) > 0) {
  76. target.push(currentToken);
  77. currentToken = U"";
  78. }
  79. }
  80. static void tokenize(List<String> &target, const ReadableString& line) {
  81. String currentToken;
  82. for (int64_t i = 0; i < string_length(line); i++) {
  83. DsrChar c = line[i];
  84. DsrChar nextC = line[i + 1];
  85. if (c == U'#' && nextC == U'#') {
  86. // Appending tokens using ##
  87. i++;
  88. } else if (c == U'#' || c == U'(' || c == U')' || c == U'[' || c == U']' || c == U'{' || c == U'}') {
  89. // Atomic token of a single character
  90. flushToken(target, currentToken);
  91. string_appendChar(currentToken, c);
  92. flushToken(target, currentToken);
  93. } else if (c == U' ' || c == U'\t') {
  94. // Whitespace
  95. flushToken(target, currentToken);
  96. } else {
  97. string_appendChar(currentToken, c);
  98. }
  99. }
  100. flushToken(target, currentToken);
  101. }
  102. void analyzeFile(Dependency &result, const ReadableString& absolutePath, Extension extension) {
  103. // Get the file's binary content.
  104. Buffer fileBuffer = file_loadBuffer(absolutePath);
  105. // Get the checksum
  106. result.contentChecksum = checksum(fileBuffer);
  107. if (extension == Extension::H || extension == Extension::Hpp) {
  108. // The current file is a header, so look for an implementation with the corresponding name.
  109. String sourcePath = findSourceFile(absolutePath, extension == Extension::H, true);
  110. // If found:
  111. if (string_length(sourcePath) > 0) {
  112. // Remember that anything using the header will have to link with the implementation.
  113. result.links.pushConstruct(sourcePath);
  114. }
  115. }
  116. // Interpret the file's content.
  117. String sourceCode = string_loadFromMemory(fileBuffer);
  118. String parentFolder = file_getRelativeParentFolder(absolutePath);
  119. List<String> tokens;
  120. bool continuingLine = false;
  121. int64_t lineNumber = 0;
  122. string_split_callback(sourceCode, U'\n', true, [&result, &parentFolder, &tokens, &continuingLine, &lineNumber](ReadableString line) {
  123. lineNumber++;
  124. if (line[0] == U'#' || continuingLine) {
  125. tokenize(tokens, line);
  126. // Continuing pre-processing line using \ at the end.
  127. continuingLine = line[string_length(line) - 1] == U'\\';
  128. } else {
  129. continuingLine = false;
  130. }
  131. if (!continuingLine && tokens.length() > 0) {
  132. if (tokens.length() >= 3) {
  133. if (string_match(tokens[1], U"include")) {
  134. if (tokens[2][0] == U'\"') {
  135. String relativePath = string_unmangleQuote(tokens[2]);
  136. String absolutePath = file_getTheoreticalAbsolutePath(relativePath, parentFolder, LOCAL_PATH_SYNTAX);
  137. result.includes.pushConstruct(absolutePath, lineNumber);
  138. }
  139. }
  140. }
  141. tokens.clear();
  142. }
  143. });
  144. }
  145. void analyzeFromFile(ProjectContext &context, const ReadableString& absolutePath) {
  146. if (findDependency(context, absolutePath) != -1) {
  147. // Already analyzed the current entry. Abort to prevent duplicate dependencies.
  148. return;
  149. }
  150. Extension extension = extensionFromString(file_getExtension(absolutePath));
  151. if (extension != Extension::Unknown) {
  152. // Create a new dependency for the file.
  153. int64_t parentIndex = context.dependencies.length();
  154. context.dependencies.push(Dependency(absolutePath, extension));
  155. // Summarize the file's content.
  156. analyzeFile(context.dependencies[parentIndex], absolutePath, extension);
  157. // Continue analyzing recursively into the file's dependencies.
  158. for (int64_t i = 0; i < context.dependencies[parentIndex].includes.length(); i++) {
  159. analyzeFromFile(context, context.dependencies[parentIndex].includes[i].path);
  160. }
  161. for (int64_t l = 0; l < context.dependencies[parentIndex].links.length(); l++) {
  162. analyzeFromFile(context, context.dependencies[parentIndex].links[l].path);
  163. }
  164. }
  165. }
  166. static void debugPrintDependencyList(const List<Connection> &connnections, const ReadableString verb) {
  167. for (int64_t c = 0; c < connnections.length(); c++) {
  168. int64_t lineNumber = connnections[c].lineNumber;
  169. if (lineNumber != -1) {
  170. printText(U" @", lineNumber, U"\t");
  171. } else {
  172. printText(U" \t");
  173. }
  174. printText(U" ", verb, U" ", file_getPathlessName(connnections[c].path), U"\n");
  175. }
  176. }
  177. void printDependencies(ProjectContext &context) {
  178. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  179. printText(U"* ", file_getPathlessName(context.dependencies[d].path), U"\n");
  180. debugPrintDependencyList(context.dependencies[d].includes, U"including");
  181. debugPrintDependencyList(context.dependencies[d].links, U"linking");
  182. }
  183. }
  184. static void script_printMessage(String &output, ScriptLanguage language, const ReadableString message) {
  185. if (language == ScriptLanguage::Batch) {
  186. string_append(output, U"echo ", message, U"\n");
  187. } else if (language == ScriptLanguage::Bash) {
  188. string_append(output, U"echo ", message, U"\n");
  189. }
  190. }
  191. static void traverserHeaderChecksums(ProjectContext &context, uint64_t &target, int64_t dependencyIndex) {
  192. // Use checksums from headers
  193. for (int64_t h = 0; h < context.dependencies[dependencyIndex].includes.length(); h++) {
  194. int64_t includedIndex = context.dependencies[dependencyIndex].includes[h].dependencyIndex;
  195. if (!context.dependencies[includedIndex].visited) {
  196. //printText(U" traverserHeaderChecksums(context, ", includedIndex, U") ", context.dependencies[includedIndex].path, "\n");
  197. // Bitwise exclusive or is both order independent and entropy preserving for non-repeated content.
  198. target = target ^ context.dependencies[includedIndex].contentChecksum;
  199. // Just have to make sure that the same checksum is not used twice.
  200. context.dependencies[includedIndex].visited = true;
  201. // Use checksums from headers recursively
  202. traverserHeaderChecksums(context, target, includedIndex);
  203. }
  204. }
  205. }
  206. static uint64_t getCombinedChecksum(ProjectContext &context, int64_t dependencyIndex) {
  207. //printText(U"getCombinedChecksum(context, ", dependencyIndex, U") ", context.dependencies[dependencyIndex].path, "\n");
  208. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  209. context.dependencies[d].visited = false;
  210. }
  211. context.dependencies[dependencyIndex].visited = true;
  212. uint64_t result = context.dependencies[dependencyIndex].contentChecksum;
  213. traverserHeaderChecksums(context, result, dependencyIndex);
  214. return result;
  215. }
  216. static int64_t findObject(SessionContext &source, uint64_t identityChecksum) {
  217. for (int64_t o = 0; o < source.sourceObjects.length(); o++) {
  218. if (source.sourceObjects[o].identityChecksum == identityChecksum) {
  219. return o;
  220. }
  221. }
  222. return -1;
  223. }
  224. void gatherBuildInstructions(SessionContext &output, ProjectContext &context, Machine &settings, ReadableString programPath) {
  225. // The compiler is often a global alias, so the user must supply either an alias or an absolute path.
  226. ReadableString compilerName = getFlag(settings, U"Compiler", U"g++"); // Assume g++ as the compiler if not specified.
  227. ReadableString compileFrom = getFlag(settings, U"CompileFrom", U"");
  228. // Check if the build system was asked to run the compiler from a specific folder.
  229. bool changePath = (string_length(compileFrom) > 0);
  230. if (changePath) {
  231. printText(U"Using ", compilerName, " as the compiler executed from ", compileFrom, ".\n");
  232. } else {
  233. printText(U"Using ", compilerName, " as the compiler from the current directory.\n");
  234. }
  235. // TODO: Warn if -DNDEBUG, -DDEBUG, or optimization levels are given directly.
  236. // Using the variables instead is both more flexible by accepting input arguments
  237. // and keeping the same format to better reuse compiled objects.
  238. if (getFlagAsInteger(settings, U"Debug")) {
  239. printText(U"Building with debug mode.\n");
  240. settings.compilerFlags.push(U"-DDEBUG");
  241. } else {
  242. printText(U"Building with release mode.\n");
  243. settings.compilerFlags.push(U"-DNDEBUG");
  244. }
  245. if (getFlagAsInteger(settings, U"StaticRuntime")) {
  246. if (getFlagAsInteger(settings, U"Windows")) {
  247. printText(U"Building with static runtime. Your application's binary will be bigger but can run without needing any installer.\n");
  248. settings.compilerFlags.push(U"-static");
  249. settings.compilerFlags.push(U"-static-libgcc");
  250. settings.compilerFlags.push(U"-static-libstdc++");
  251. settings.linkerFlags.push(U"-static");
  252. settings.linkerFlags.push(U"-static-libgcc");
  253. settings.linkerFlags.push(U"-static-libstdc++");
  254. } else {
  255. printText(U"The target platform does not support static linking of runtime. But don't worry about bundling any runtimes, because it comes with most of the Posix compliant operating systems.\n");
  256. }
  257. } else {
  258. printText(U"Building with dynamic runtime. Don't forget to bundle the C and C++ runtimes for systems that don't have it pre-installed.\n");
  259. }
  260. ReadableString optimizationLevel = getFlag(settings, U"Optimization", U"2");
  261. printText(U"Building with optimization level ", optimizationLevel, U".\n");
  262. settings.compilerFlags.push(string_combine(U"-O", optimizationLevel));
  263. // Convert lists of linker and compiler flags into strings.
  264. // TODO: Give a warning if two contradictory flags are used, such as optimization levels and language versions.
  265. // TODO: Make sure that no spaces are inside of the flags, because that can mess up detection of pre-existing and contradictory arguments.
  266. // TODO: Use groups of compiler flags, so that they can be generated in the last step.
  267. // This would allow calling the compiler directly when given a folder path for temporary files instead of a script path.
  268. String generatedCompilerFlags;
  269. for (int64_t i = 0; i < settings.compilerFlags.length(); i++) {
  270. string_append(generatedCompilerFlags, " ", settings.compilerFlags[i]);
  271. }
  272. String linkerFlags;
  273. for (int64_t i = 0; i < settings.linkerFlags.length(); i++) {
  274. string_append(linkerFlags, " -l", settings.linkerFlags[i]);
  275. }
  276. printText(U"Generating build instructions for ", programPath, U" using settings:\n");
  277. printText(U" Compiler flags:", generatedCompilerFlags, U"\n");
  278. printText(U" Linker flags:", linkerFlags, U"\n");
  279. for (int64_t v = 0; v < settings.variables.length(); v++) {
  280. printText(U" * ", settings.variables[v].key, U" = ", settings.variables[v].value);
  281. if (settings.variables[v].inherited) {
  282. printText(U" (inherited input)");
  283. }
  284. printText(U"\n");
  285. }
  286. printText(U"Listing source files to compile in the current session.\n");
  287. // The current project's global indices to objects shared between all projects being built during the session.
  288. List<int64_t> sourceObjectIndices;
  289. bool hasSourceCode = false;
  290. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  291. Extension extension = context.dependencies[d].extension;
  292. if (extension == Extension::C || extension == Extension::Cpp) {
  293. // Dependency paths are already absolute from the recursive search.
  294. String sourcePath = context.dependencies[d].path;
  295. String identity = string_combine(sourcePath, generatedCompilerFlags);
  296. uint64_t identityChecksum = checksum(identity);
  297. int64_t previousIndex = findObject(output, identityChecksum);
  298. if (previousIndex == -1) {
  299. // Content checksums were created while scanning for source code, so now we just combine each source file's content checksum with all its headers to get the combined checksum.
  300. // The combined checksum represents the state after all headers are included recursively and given as input for compilation unit generating an object.
  301. uint64_t combinedChecksum = getCombinedChecksum(context, d);
  302. String objectPath = file_combinePaths(output.tempPath, string_combine(U"dfpsr_", identityChecksum, U"_", combinedChecksum, U".o"));
  303. sourceObjectIndices.push(output.sourceObjects.length());
  304. output.sourceObjects.pushConstruct(identityChecksum, combinedChecksum, sourcePath, objectPath, generatedCompilerFlags, compilerName, compileFrom);
  305. } else {
  306. // Link to this pre-existing source file.
  307. sourceObjectIndices.push(previousIndex);
  308. }
  309. hasSourceCode = true;
  310. }
  311. }
  312. if (hasSourceCode) {
  313. printText(U"Listing target executable ", programPath, " in the current session.\n");
  314. bool executeResult = getFlagAsInteger(settings, U"Supressed") == 0;
  315. output.linkerSteps.pushConstruct(compilerName, compileFrom, programPath, settings.linkerFlags, sourceObjectIndices, executeResult);
  316. } else {
  317. printText(U"Filed to find any source code to compile when building ", programPath, U".\n");
  318. }
  319. }
  320. static ScriptLanguage identifyLanguage(const ReadableString &filename) {
  321. String scriptExtension = string_upperCase(file_getExtension(filename));
  322. if (string_match(scriptExtension, U"BAT")) {
  323. return ScriptLanguage::Batch;
  324. } else if (string_match(scriptExtension, U"SH")) {
  325. return ScriptLanguage::Bash;
  326. } else {
  327. throwError(U"Could not identify the scripting language of ", filename, U". Use *.bat or *.sh.\n");
  328. return ScriptLanguage::Unknown;
  329. }
  330. }
  331. void setCompilationFolder(String &generatedCode, ScriptLanguage language, String &currentPath, const ReadableString &newPath) {
  332. if (!string_match(currentPath, newPath)) {
  333. if (string_length(currentPath) > 0) {
  334. if (language == ScriptLanguage::Batch) {
  335. string_append(generatedCode, "popd\n");
  336. } else if (language == ScriptLanguage::Bash) {
  337. string_append(generatedCode, U")\n");
  338. }
  339. }
  340. if (string_length(newPath) > 0) {
  341. if (language == ScriptLanguage::Batch) {
  342. string_append(generatedCode, "pushd ", newPath, "\n");
  343. } else if (language == ScriptLanguage::Bash) {
  344. string_append(generatedCode, U"(cd ", newPath, ";\n");
  345. }
  346. }
  347. }
  348. }
  349. void generateCompilationScript(SessionContext &input, const ReadableString &scriptPath) {
  350. printText(U"Generating build script\n");
  351. String generatedCode;
  352. ScriptLanguage language = identifyLanguage(scriptPath);
  353. if (language == ScriptLanguage::Batch) {
  354. string_append(generatedCode, U"@echo off\n\n");
  355. } else if (language == ScriptLanguage::Bash) {
  356. string_append(generatedCode, U"#!/bin/bash\n\n");
  357. }
  358. // Keep track of the current path, so that it only changes when needed.
  359. String currentPath;
  360. // Generate code for compiling source code into objects.
  361. printText(U"Generating code for compiling ", input.sourceObjects.length(), U" objects.\n");
  362. for (int64_t o = 0; o < input.sourceObjects.length(); o++) {
  363. SourceObject *sourceObject = &(input.sourceObjects[o]);
  364. printText(U"\t* ", sourceObject->sourcePath, U"\n");
  365. setCompilationFolder(generatedCode, language, currentPath, sourceObject->compileFrom);
  366. if (language == ScriptLanguage::Batch) {
  367. string_append(generatedCode, U"if exist ", sourceObject->objectPath, U" (\n");
  368. } else if (language == ScriptLanguage::Bash) {
  369. string_append(generatedCode, U"if [ -e \"", sourceObject->objectPath, U"\" ]; then\n");
  370. }
  371. script_printMessage(generatedCode, language, string_combine(U"Reusing ", sourceObject->sourcePath, U" ID:", sourceObject->identityChecksum, U"."));
  372. if (language == ScriptLanguage::Batch) {
  373. string_append(generatedCode, U") else (\n");
  374. } else if (language == ScriptLanguage::Bash) {
  375. string_append(generatedCode, U"else\n");
  376. }
  377. String compilerFlags = sourceObject->generatedCompilerFlags;
  378. script_printMessage(generatedCode, language, string_combine(U"Compiling ", sourceObject->sourcePath, U" ID:", sourceObject->identityChecksum, U" with ", compilerFlags, U"."));
  379. string_append(generatedCode, sourceObject->compilerName, compilerFlags, U" -c ", sourceObject->sourcePath, U" -o ", sourceObject->objectPath, U"\n");
  380. if (language == ScriptLanguage::Batch) {
  381. string_append(generatedCode, ")\n");
  382. } else if (language == ScriptLanguage::Bash) {
  383. string_append(generatedCode, U"fi\n");
  384. }
  385. }
  386. // Generate code for linking objects into executables.
  387. printText(U"Generating code for linking ", input.linkerSteps.length(), U" executables:\n");
  388. for (int64_t l = 0; l < input.linkerSteps.length(); l++) {
  389. LinkingStep *linkingStep = &(input.linkerSteps[l]);
  390. String programPath = linkingStep->binaryName;
  391. printText(U"\tGenerating code for linking ", programPath, U" of :\n");
  392. setCompilationFolder(generatedCode, language, currentPath, linkingStep->compileFrom);
  393. String linkerFlags;
  394. for (int64_t lib = 0; lib < linkingStep->linkerFlags.length(); lib++) {
  395. String library = linkingStep->linkerFlags[lib];
  396. string_append(linkerFlags, " -l", library);
  397. printText(U"\t\t* ", library, U" library\n");
  398. }
  399. // Generate a list of object paths from indices.
  400. String allObjects;
  401. for (int64_t i = 0; i < linkingStep->sourceObjectIndices.length(); i++) {
  402. int64_t objectIndex = linkingStep->sourceObjectIndices[i];
  403. SourceObject *sourceObject = &(input.sourceObjects[objectIndex]);
  404. if (objectIndex >= 0 || objectIndex < input.sourceObjects.length()) {
  405. printText(U"\t\t* ", sourceObject->sourcePath, U"\n");
  406. string_append(allObjects, U" ", sourceObject->objectPath);
  407. } else {
  408. throwError(U"Object index ", objectIndex, U" is out of bound ", 0, U"..", (input.sourceObjects.length() - 1), U"\n");
  409. }
  410. }
  411. // Generate the code for building.
  412. if (string_length(linkerFlags) > 0) {
  413. script_printMessage(generatedCode, language, string_combine(U"Linking ", programPath, U" with", linkerFlags, U"."));
  414. } else {
  415. script_printMessage(generatedCode, language, string_combine(U"Linking ", programPath, U"."));
  416. }
  417. string_append(generatedCode, linkingStep->compilerName, allObjects, linkerFlags, U" -o ", programPath, U"\n");
  418. if (linkingStep->executeResult) {
  419. script_printMessage(generatedCode, language, string_combine(U"Starting ", programPath));
  420. string_append(generatedCode, programPath, U"\n");
  421. script_printMessage(generatedCode, language, U"The program terminated.");
  422. }
  423. }
  424. setCompilationFolder(generatedCode, language, currentPath, U"");
  425. script_printMessage(generatedCode, language, U"Done building.");
  426. // Save the script.
  427. printText(U"Saving script to ", scriptPath, "\n");
  428. if (language == ScriptLanguage::Batch) {
  429. string_save(scriptPath, generatedCode);
  430. } else if (language == ScriptLanguage::Bash) {
  431. string_save(scriptPath, generatedCode, CharacterEncoding::BOM_UTF8, LineEncoding::Lf);
  432. }
  433. }