analyzer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. 
  2. #include "analyzer.h"
  3. #include "generator.h"
  4. #include "Machine.h"
  5. using namespace dsr;
  6. static Extension extensionFromString(ReadableString extensionName) {
  7. String upperName = string_upperCase(string_removeOuterWhiteSpace(extensionName));
  8. Extension result = Extension::Unknown;
  9. if (string_match(upperName, U"H")) {
  10. result = Extension::H;
  11. } else if (string_match(upperName, U"HPP")) {
  12. result = Extension::Hpp;
  13. } else if (string_match(upperName, U"C")) {
  14. result = Extension::C;
  15. } else if (string_match(upperName, U"CPP")) {
  16. result = Extension::Cpp;
  17. }
  18. return result;
  19. }
  20. struct HashGenerator {
  21. uint64_t a = 0x8C2A03D4;
  22. uint64_t b = 0xF42B1583;
  23. uint64_t c = 0xA6815E74;
  24. uint64_t d = 0x634B20F6;
  25. uint64_t e = 0x12C49B72;
  26. uint64_t f = 0x06E1F489;
  27. uint64_t g = 0xA8D24954;
  28. uint64_t h = 0x19CF53AA;
  29. HashGenerator() {}
  30. void feedByte(uint64_t input) {
  31. // Write input
  32. a = a ^ (input << ((e >> 12u) % 56u));
  33. b = b ^ (input << ((f >> 18u) % 56u));
  34. c = c ^ (input << ((g >> 15u) % 56u));
  35. d = d ^ (input << ((h >> 5u) % 56u));
  36. // Select bits
  37. uint64_t e = (a & c) | (b & ~c);
  38. uint64_t f = (c & b) | (d & ~b);
  39. // Multiply
  40. uint64_t g = (e >> 32) * (f & 0xFFFFFFFF);
  41. uint64_t h = (f >> 32) * (e & 0xFFFFFFFF);
  42. // Add
  43. a = a ^ (b >> ((input) % 3u)) + (c >> ((h >> 25u) % 4u));
  44. b = b ^ (c >> ((g >> 36u) % 6u)) + (d >> ((input ^ 0b10101101) % 5u));
  45. c = c ^ g;
  46. d = d ^ h;
  47. }
  48. uint64_t getHash64() {
  49. return a ^ (b << 7) ^ (c << 19) ^ (d << 24);
  50. }
  51. };
  52. static uint64_t checksum(ReadableString text) {
  53. HashGenerator generator;
  54. for (int64_t i = 0; i < string_length(text); i++) {
  55. DsrChar c = text[i];
  56. generator.feedByte((c >> 24) & 0xFF);
  57. generator.feedByte((c >> 16) & 0xFF);
  58. generator.feedByte((c >> 8) & 0xFF);
  59. generator.feedByte(c & 0xFF);
  60. }
  61. return generator.getHash64();
  62. }
  63. static uint64_t checksum(const Buffer& buffer) {
  64. HashGenerator generator;
  65. SafePointer<uint8_t> data = buffer_getSafeData<uint8_t>(buffer, "checksum input buffer");
  66. for (int64_t i = 0; i < buffer_getSize(buffer); i++) {
  67. generator.feedByte(data[i]);
  68. }
  69. return generator.getHash64();
  70. }
  71. static int64_t findDependency(ProjectContext &context, ReadableString findPath) {
  72. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  73. if (string_match(context.dependencies[d].path, findPath)) {
  74. return d;
  75. }
  76. }
  77. return -1;
  78. }
  79. static void resolveConnection(ProjectContext &context, Connection &connection) {
  80. connection.dependencyIndex = findDependency(context, connection.path);
  81. }
  82. static void resolveDependency(ProjectContext &context, Dependency &dependency) {
  83. for (int64_t l = 0; l < dependency.links.length(); l++) {
  84. resolveConnection(context, dependency.links[l]);
  85. }
  86. for (int64_t i = 0; i < dependency.includes.length(); i++) {
  87. resolveConnection(context, dependency.includes[i]);
  88. }
  89. }
  90. void resolveDependencies(ProjectContext &context) {
  91. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  92. resolveDependency(context, context.dependencies[d]);
  93. }
  94. }
  95. static String findSourceFile(ReadableString headerPath, bool acceptC, bool acceptCpp) {
  96. if (file_hasExtension(headerPath)) {
  97. ReadableString extensionlessPath = file_getExtensionless(headerPath);
  98. String cPath = extensionlessPath + U".c";
  99. String cppPath = extensionlessPath + U".cpp";
  100. if (acceptC && file_getEntryType(cPath) == EntryType::File) {
  101. return cPath;
  102. } else if (acceptCpp && file_getEntryType(cppPath) == EntryType::File) {
  103. return cppPath;
  104. }
  105. }
  106. return U"";
  107. }
  108. static void flushToken(List<String> &target, String &currentToken) {
  109. if (string_length(currentToken) > 0) {
  110. target.push(currentToken);
  111. currentToken = U"";
  112. }
  113. }
  114. static void tokenize(List<String> &target, ReadableString line) {
  115. String currentToken;
  116. for (int64_t i = 0; i < string_length(line); i++) {
  117. DsrChar c = line[i];
  118. DsrChar nextC = line[i + 1];
  119. if (c == U'#' && nextC == U'#') {
  120. // Appending tokens using ##
  121. i++;
  122. } else if (c == U'#' || c == U'(' || c == U')' || c == U'[' || c == U']' || c == U'{' || c == U'}') {
  123. // Atomic token of a single character
  124. flushToken(target, currentToken);
  125. string_appendChar(currentToken, c);
  126. flushToken(target, currentToken);
  127. } else if (c == U' ' || c == U'\t') {
  128. // Whitespace
  129. flushToken(target, currentToken);
  130. } else {
  131. string_appendChar(currentToken, c);
  132. }
  133. }
  134. flushToken(target, currentToken);
  135. }
  136. // When CACHED_ANALYSIS is enabled, files will only be analyzed once per session, by remembering them from previous projects.
  137. // If features that require a different type of analysis per project are implemented, this can easily be turned off.
  138. #define CACHED_ANALYSIS
  139. #ifdef CACHED_ANALYSIS
  140. // Remembering previous results from analyzing the same files.
  141. List<Dependency> analysisCache;
  142. #endif
  143. void analyzeFile(Dependency &result, ReadableString absolutePath, Extension extension) {
  144. #ifdef CACHED_ANALYSIS
  145. // Check if the file has already been analyzed.
  146. for (int c = 0; c < analysisCache.length(); c++) {
  147. if (string_match(analysisCache[c].path, absolutePath)) {
  148. // Clone all the results to keep projects separate in memory for safety.
  149. result = analysisCache[c];
  150. return;
  151. }
  152. }
  153. #endif
  154. // Get the file's binary content.
  155. Buffer fileBuffer = file_loadBuffer(absolutePath);
  156. // Get the checksum
  157. result.contentChecksum = checksum(fileBuffer);
  158. if (extension == Extension::H || extension == Extension::Hpp) {
  159. // The current file is a header, so look for an implementation with the corresponding name.
  160. String sourcePath = findSourceFile(absolutePath, extension == Extension::H, true);
  161. // If found:
  162. if (string_length(sourcePath) > 0) {
  163. // Remember that anything using the header will have to link with the implementation.
  164. result.links.pushConstruct(sourcePath);
  165. }
  166. }
  167. // Interpret the file's content.
  168. String sourceCode = string_loadFromMemory(fileBuffer);
  169. String parentFolder = file_getRelativeParentFolder(absolutePath);
  170. List<String> tokens;
  171. bool continuingLine = false;
  172. int64_t lineNumber = 0;
  173. string_split_callback(sourceCode, U'\n', true, [&result, &parentFolder, &tokens, &continuingLine, &absolutePath, &lineNumber](ReadableString line) {
  174. lineNumber++;
  175. if (line[0] == U'#' || continuingLine) {
  176. tokenize(tokens, line);
  177. // Continuing pre-processing line using \ at the end.
  178. continuingLine = line[string_length(line) - 1] == U'\\';
  179. } else {
  180. continuingLine = false;
  181. }
  182. if (!continuingLine && tokens.length() > 0) {
  183. if (tokens.length() >= 3) {
  184. if (string_match(tokens[1], U"include")) {
  185. if (tokens[2][0] == U'\"') {
  186. String relativePath = string_unmangleQuote(tokens[2]);
  187. String absoluteHeaderPath = file_getTheoreticalAbsolutePath(relativePath, parentFolder, LOCAL_PATH_SYNTAX);
  188. if (file_getEntryType(absoluteHeaderPath) != EntryType::File) {
  189. throwError(U"Failed to find ", absoluteHeaderPath, U" from line ", lineNumber, U" in ", absolutePath, U"\n");
  190. } else {
  191. result.includes.pushConstruct(absoluteHeaderPath, lineNumber);
  192. }
  193. }
  194. }
  195. }
  196. tokens.clear();
  197. }
  198. });
  199. }
  200. void analyzeFromFile(ProjectContext &context, ReadableString absolutePath) {
  201. if (findDependency(context, absolutePath) != -1) {
  202. // Already analyzed the current entry. Abort to prevent duplicate dependencies.
  203. return;
  204. }
  205. Extension extension = extensionFromString(file_getExtension(absolutePath));
  206. if (extension != Extension::Unknown) {
  207. // Create a new dependency for the file.
  208. int64_t parentIndex = context.dependencies.length();
  209. context.dependencies.push(Dependency(absolutePath, extension));
  210. // Summarize the file's content.
  211. analyzeFile(context.dependencies[parentIndex], absolutePath, extension);
  212. // Continue analyzing recursively into the file's dependencies.
  213. for (int64_t i = 0; i < context.dependencies[parentIndex].includes.length(); i++) {
  214. analyzeFromFile(context, context.dependencies[parentIndex].includes[i].path);
  215. }
  216. for (int64_t l = 0; l < context.dependencies[parentIndex].links.length(); l++) {
  217. analyzeFromFile(context, context.dependencies[parentIndex].links[l].path);
  218. }
  219. }
  220. }
  221. static void debugPrintDependencyList(const List<Connection> &connnections, const ReadableString verb) {
  222. for (int64_t c = 0; c < connnections.length(); c++) {
  223. int64_t lineNumber = connnections[c].lineNumber;
  224. if (lineNumber != -1) {
  225. printText(U" @", lineNumber, U"\t");
  226. } else {
  227. printText(U" \t");
  228. }
  229. printText(U" ", verb, U" ", file_getPathlessName(connnections[c].path), U"\n");
  230. }
  231. }
  232. void printDependencies(ProjectContext &context) {
  233. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  234. printText(U"* ", file_getPathlessName(context.dependencies[d].path), U"\n");
  235. debugPrintDependencyList(context.dependencies[d].includes, U"including");
  236. debugPrintDependencyList(context.dependencies[d].links, U"linking");
  237. }
  238. }
  239. static void traverserHeaderChecksums(ProjectContext &context, uint64_t &target, int64_t dependencyIndex) {
  240. // Use checksums from headers
  241. for (int64_t h = 0; h < context.dependencies[dependencyIndex].includes.length(); h++) {
  242. int64_t includedIndex = context.dependencies[dependencyIndex].includes[h].dependencyIndex;
  243. if (!context.dependencies[includedIndex].visited) {
  244. // Bitwise exclusive or is both order independent and entropy preserving for non-repeated content.
  245. target = target ^ context.dependencies[includedIndex].contentChecksum;
  246. // Just have to make sure that the same checksum is not used twice.
  247. context.dependencies[includedIndex].visited = true;
  248. // Use checksums from headers recursively
  249. traverserHeaderChecksums(context, target, includedIndex);
  250. }
  251. }
  252. }
  253. static uint64_t getCombinedChecksum(ProjectContext &context, int64_t dependencyIndex) {
  254. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  255. context.dependencies[d].visited = false;
  256. }
  257. context.dependencies[dependencyIndex].visited = true;
  258. uint64_t result = context.dependencies[dependencyIndex].contentChecksum;
  259. traverserHeaderChecksums(context, result, dependencyIndex);
  260. return result;
  261. }
  262. static int64_t findObject(SessionContext &source, uint64_t identityChecksum) {
  263. for (int64_t o = 0; o < source.sourceObjects.length(); o++) {
  264. if (source.sourceObjects[o].identityChecksum == identityChecksum) {
  265. return o;
  266. }
  267. }
  268. return -1;
  269. }
  270. void gatherBuildInstructions(SessionContext &output, ProjectContext &context, Machine &settings, ReadableString programPath) {
  271. validateSettings(settings, string_combine(U"in settings at the beginning of gatherBuildInstructions, for ", programPath, U"\n"));
  272. // The compiler is often a global alias, so the user must supply either an alias or an absolute path.
  273. ReadableString compilerName = getFlag(settings, U"Compiler", U"g++"); // Assume g++ as the compiler if not specified.
  274. ReadableString compileFrom = getFlag(settings, U"CompileFrom", U"");
  275. // Check if the build system was asked to run the compiler from a specific folder.
  276. bool changePath = (string_length(compileFrom) > 0);
  277. if (changePath) {
  278. printText(U"Using ", compilerName, U" as the compiler executed from ", compileFrom, U".\n");
  279. } else {
  280. printText(U"Using ", compilerName, U" as the compiler from the current directory.\n");
  281. }
  282. // TODO: Warn if -DNDEBUG, -DDEBUG, or optimization levels are given directly.
  283. // Using the variables instead is both more flexible by accepting input arguments
  284. // and keeping the same format to better reuse compiled objects.
  285. if (getFlagAsInteger(settings, U"Debug")) {
  286. printText(U"Building with debug mode.\n");
  287. settings.compilerFlags.push(U"-DDEBUG");
  288. } else {
  289. printText(U"Building with release mode.\n");
  290. settings.compilerFlags.push(U"-DNDEBUG");
  291. }
  292. if (getFlagAsInteger(settings, U"StaticRuntime")) {
  293. if (getFlagAsInteger(settings, U"Windows")) {
  294. printText(U"Building with static runtime. Your application's binary will be bigger but can run without needing any installer.\n");
  295. settings.compilerFlags.push(U"-static");
  296. settings.compilerFlags.push(U"-static-libgcc");
  297. settings.compilerFlags.push(U"-static-libstdc++");
  298. settings.linkerFlags.push(U"-static");
  299. settings.linkerFlags.push(U"-static-libgcc");
  300. settings.linkerFlags.push(U"-static-libstdc++");
  301. } else {
  302. 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");
  303. }
  304. } else {
  305. 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");
  306. }
  307. ReadableString optimizationLevel = getFlag(settings, U"Optimization", U"2");
  308. printText(U"Building with optimization level ", optimizationLevel, U".\n");
  309. settings.compilerFlags.push(string_combine(U"-O", optimizationLevel));
  310. validateSettings(settings, string_combine(U"in settings after adding flags from settings in gatherBuildInstructions, for ", programPath, U"\n"));
  311. // Convert lists of linker and compiler flags into strings.
  312. // TODO: Give a warning if two contradictory flags are used, such as optimization levels and language versions.
  313. // TODO: Make sure that no spaces are inside of the flags, because that can mess up detection of pre-existing and contradictory arguments.
  314. // TODO: Use groups of compiler flags, so that they can be generated in the last step.
  315. // This would allow calling the compiler directly when given a folder path for temporary files instead of a script path.
  316. String generatedCompilerFlags;
  317. for (int64_t i = 0; i < settings.compilerFlags.length(); i++) {
  318. printText(U"Build script gave compiler flag:", settings.compilerFlags[i], U"\n");
  319. string_append(generatedCompilerFlags, U" ", settings.compilerFlags[i]);
  320. }
  321. String linkerFlags;
  322. for (int64_t i = 0; i < settings.linkerFlags.length(); i++) {
  323. printText(U"Build script gave linker flag:", settings.linkerFlags[i], U"\n");
  324. string_append(linkerFlags, settings.linkerFlags[i]);
  325. }
  326. printText(U"Generating build instructions for ", programPath, U" using settings:\n");
  327. printText(U" Compiler flags:", generatedCompilerFlags, U"\n");
  328. printText(U" Linker flags:", linkerFlags, U"\n");
  329. for (int64_t v = 0; v < settings.variables.length(); v++) {
  330. printText(U" * ", settings.variables[v].key, U" = ", settings.variables[v].value);
  331. if (settings.variables[v].inherited) {
  332. printText(U" (inherited input)");
  333. }
  334. printText(U"\n");
  335. }
  336. printText(U"Listing source files to compile in the current session.\n");
  337. // The current project's global indices to objects shared between all projects being built during the session.
  338. List<int64_t> sourceObjectIndices;
  339. bool hasSourceCode = false;
  340. for (int64_t d = 0; d < context.dependencies.length(); d++) {
  341. Extension extension = context.dependencies[d].extension;
  342. if (extension == Extension::C || extension == Extension::Cpp) {
  343. // Dependency paths are already absolute from the recursive search.
  344. String sourcePath = context.dependencies[d].path;
  345. String identity = string_combine(sourcePath, generatedCompilerFlags);
  346. uint64_t identityChecksum = checksum(identity);
  347. int64_t previousIndex = findObject(output, identityChecksum);
  348. if (previousIndex == -1) {
  349. // 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.
  350. // The combined checksum represents the state after all headers are included recursively and given as input for compilation unit generating an object.
  351. uint64_t combinedChecksum = getCombinedChecksum(context, d);
  352. String objectPath = file_combinePaths(output.tempPath, string_combine(U"dfpsr_", identityChecksum, U"_", combinedChecksum, U".o"));
  353. sourceObjectIndices.push(output.sourceObjects.length());
  354. output.sourceObjects.pushConstruct(identityChecksum, combinedChecksum, sourcePath, objectPath, settings.compilerFlags, compilerName, compileFrom);
  355. } else {
  356. // Link to this pre-existing source file.
  357. sourceObjectIndices.push(previousIndex);
  358. }
  359. hasSourceCode = true;
  360. }
  361. }
  362. if (hasSourceCode) {
  363. printText(U"Listing target executable ", programPath, U" in the current session.\n");
  364. bool executeResult = getFlagAsInteger(settings, U"Supressed") == 0;
  365. output.linkerSteps.pushConstruct(compilerName, compileFrom, programPath, settings.linkerFlags, sourceObjectIndices, executeResult);
  366. } else {
  367. printText(U"Failed to find any source code to compile when building ", programPath, U".\n");
  368. }
  369. validateSettings(settings, string_combine(U"in settings at the end of gatherBuildInstructions, for ", programPath, U"\n"));
  370. }
  371. static void crawlSource(ProjectContext &context, ReadableString absolutePath) {
  372. EntryType pathType = file_getEntryType(absolutePath);
  373. if (pathType == EntryType::File) {
  374. printText(U"Crawling for source from ", absolutePath, U".\n");
  375. analyzeFromFile(context, absolutePath);
  376. } else if (pathType == EntryType::Folder) {
  377. printText(U"Crawling was given the folder ", absolutePath, U" but a source file was expected!\n");
  378. } else if (pathType == EntryType::SymbolicLink) {
  379. // Symbolic links can point to both files and folder, so we need to follow it and find out what it really is.
  380. crawlSource(context, file_followSymbolicLink(absolutePath));
  381. }
  382. }
  383. static List<String> initializedProjects;
  384. static void buildProjectFromSettings(SessionContext &output, const ReadableString &path, Machine &settings) {
  385. printText(U"Building project at ", path, U"\n");
  386. // Check if this project has begun building previously during this session.
  387. String absolutePath = file_getAbsolutePath(path);
  388. for (int64_t p = 0; p < initializedProjects.length(); p++) {
  389. if (string_caseInsensitiveMatch(absolutePath, initializedProjects[p])) {
  390. throwError(U"Found duplicate requests to build from the same initial script ", absolutePath, U" which could cause non-determinism if different arguments are given to each!\n");
  391. return;
  392. }
  393. }
  394. // Remember that building of this project has started.
  395. initializedProjects.push(absolutePath);
  396. ProjectContext context;
  397. // Find out where things are located.
  398. String projectPath = file_getAbsoluteParentFolder(path);
  399. // Get the project's name.
  400. String projectName = file_getPathlessName(file_getExtensionless(path));
  401. // If no application path is given, the new executable will be named after the project and placed in the same folder.
  402. String fullProgramPath = getFlag(settings, U"ProgramPath", projectName);
  403. if (string_length(output.executableExtension) > 0) {
  404. string_append(fullProgramPath, output.executableExtension);
  405. }
  406. // Interpret ProgramPath relative to the project path.
  407. fullProgramPath = file_getTheoreticalAbsolutePath(fullProgramPath, projectPath);
  408. // Build projects from files. (used for running many tests)
  409. for (int64_t b = 0; b < settings.projectFromSourceFilenames.length(); b++) {
  410. buildFromFile(output, settings.projectFromSourceFilenames[b], settings.projectFromSourceSettings[b]);
  411. }
  412. // Build other projects. (used for compiling programs that the main program should call)
  413. for (int64_t b = 0; b < settings.otherProjectPaths.length(); b++) {
  414. buildFromFolder(output, settings.otherProjectPaths[b], settings.otherProjectSettings[b]);
  415. }
  416. validateSettings(settings, string_combine(U"in settings after building other projects in buildProject, for ", path, U"\n"));
  417. // If the SkipIfBinaryExists flag is given, we will abort as soon as we have handled its external BuildProjects requests and confirmed that the application exists.
  418. if (getFlagAsInteger(settings, U"SkipIfBinaryExists") && file_getEntryType(fullProgramPath) == EntryType::File) {
  419. // SkipIfBinaryExists was active and the binary exists, so abort here to avoid redundant work.
  420. printText(U"Skipping build of ", path, U" because the SkipIfBinaryExists flag was given and ", fullProgramPath, U" was found.\n");
  421. return;
  422. }
  423. // Once we know where the binary is and that it should be built, we can start searching for source code.
  424. for (int64_t o = 0; o < settings.crawlOrigins.length(); o++) {
  425. crawlSource(context, settings.crawlOrigins[o]);
  426. }
  427. validateSettings(settings, string_combine(U"in settings after crawling source in buildProject, for ", path, U"\n"));
  428. // Once we are done finding all source files, we can resolve the dependencies to create a graph connected by indices.
  429. resolveDependencies(context);
  430. if (getFlagAsInteger(settings, U"ListDependencies")) {
  431. printDependencies(context);
  432. }
  433. gatherBuildInstructions(output, context, settings, fullProgramPath);
  434. validateSettings(settings, string_combine(U"in settings after gathering build instructions in buildProject, for ", path, U"\n"));
  435. }
  436. // Using a project file path and input arguments.
  437. void buildProject(SessionContext &output, ReadableString projectFilePath, Machine &sharedSettings) {
  438. // Inherit external settings.
  439. Machine settings(file_getPathlessName(projectFilePath));
  440. inheritMachine(settings, sharedSettings);
  441. validateSettings(settings, string_combine(U"in settings after inheriting settings from caller, for ", projectFilePath, U"\n"));
  442. // Evaluate the project's script.
  443. printText(U"Executing project file from ", projectFilePath, U".\n");
  444. evaluateScript(settings, projectFilePath);
  445. validateSettings(settings, string_combine(U"in settings after evaluateScript in buildProject, for ", projectFilePath, U"\n"));
  446. // Complete the project.
  447. buildProjectFromSettings(output, projectFilePath, settings);
  448. }
  449. // Using a folder path and input arguments for all projects.
  450. void buildProjects(SessionContext &output, ReadableString projectFolderPath, Machine &sharedSettings) {
  451. printText(U"Building all projects in ", projectFolderPath, U"\n");
  452. file_getFolderContent(projectFolderPath, [&sharedSettings, &output](const ReadableString& entryPath, const ReadableString& entryName, EntryType entryType) {
  453. if (entryType == EntryType::Folder) {
  454. buildProjects(output, entryPath, sharedSettings);
  455. } else if (entryType == EntryType::File) {
  456. ReadableString extension = file_getExtension(entryName);
  457. if (string_caseInsensitiveMatch(extension, U"DSRPROJ")) {
  458. buildProject(output, entryPath, sharedSettings);
  459. }
  460. }
  461. });
  462. }
  463. void buildFromFolder(SessionContext &output, ReadableString projectPath, Machine &sharedSettings) {
  464. EntryType entryType = file_getEntryType(projectPath);
  465. printText(U"Building anything at ", projectPath, U" which is ", entryType, U"\n");
  466. if (entryType == EntryType::File) {
  467. String extension = string_upperCase(file_getExtension(projectPath));
  468. if (!string_match(extension, U"DSRPROJ")) {
  469. printText(U"Can't use the Build keyword with a file that is not a project!\n");
  470. } else {
  471. // Build the given project
  472. buildProject(output, projectPath, sharedSettings);
  473. }
  474. } else if (entryType == EntryType::Folder) {
  475. buildProjects(output, projectPath, sharedSettings);
  476. }
  477. }
  478. void buildFromFile(SessionContext &output, ReadableString mainPath, Machine &sharedSettings) {
  479. // Inherit settings, flags and dependencies from the parent, because they do not exist in single source files.
  480. Machine settings(file_getPathlessName(mainPath));
  481. cloneMachine(settings, sharedSettings);
  482. ReadableString extension = file_getExtension(mainPath);
  483. if (!(string_caseInsensitiveMatch(extension, U"c") || string_caseInsensitiveMatch(extension, U"cpp"))) {
  484. throwError(U"Creating projects from source files is currently only supported for *.c and *.cpp, but the extension was '", extension, U"'.");
  485. }
  486. // Crawl from the selected file to discover direct dependencies.
  487. settings.crawlOrigins.push(mainPath);
  488. // Check that settings are okay.
  489. validateSettings(settings, string_combine(U"in settings after inheriting settings from caller, for ", mainPath, U"\n"));
  490. // Create the project to save as a script or build using direct calls to the compiler.
  491. buildProjectFromSettings(output, mainPath, settings);
  492. }