main.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 
  2. // Because it would be slow to check if the build system needs to be recompiled every time something uses it,
  3. // you must manually delete the build system's binary and try to build a project using it after making changes to the builder's source code.
  4. // Otherwise buildProject.sh will just see that an old version exists and use it.
  5. // TODO:
  6. // * Give a warning when the given compiler path is not actually a path to a file and script generation is disabled.
  7. // Also make the compiler's path absolute from the current directory when called, or the specified folder to call from.
  8. // * Run multiple instances of the compiler at the same time on different CPU cores.
  9. // * Implement more features for the machine, such as:
  10. // * else and elseif cases.
  11. // * Temporarily letting the theoretical path go into another folder within a scope, similar to if statements but only affecting the path.
  12. // Like writing (cd path; stmt;) in Bash but with fast parsed Basic-like syntax.
  13. // The same stack used to store theoretical paths might be useful for else if cases to remember when the scope has already passed a case when not jumping with gotos.
  14. // * Create portable scripted events for pre-build and post-build, translated into both Batch and Bash.
  15. // Pre-build can be used to generate and transpile code before compiling.
  16. // Post-build should be used to execute the resulting program.
  17. // Optionally with variables from the build script as input arguments.
  18. /*
  19. Project files:
  20. Syntax:
  21. * Assign "10" to variable x:
  22. x = 10
  23. * Assign "1" to variable myVariable:
  24. myVariable
  25. * Assign b plus c to a:
  26. a = b + c
  27. * Assign b minus c to a:
  28. a = b - c
  29. * Assign b times c to a:
  30. a = b * c
  31. * Assign b divided by c to a:
  32. a = b / c
  33. * Concatenate "hello" and " world" into "hello world" in message:
  34. message = "hello" & " world"
  35. * If a is less than b or c equals 3 then assign y to z:
  36. if (a < b) or (c == 3)
  37. z = y
  38. end if
  39. * x is assigned a boolean value telling if the content of a matches "abc". (case sensitive comparison)
  40. x = a matches "abc"
  41. Commands:
  42. Finding source code for the current project:
  43. * Add file.cpp and other implementations found through includes into the list of source code to compile and link.
  44. Crawl "folder/file.cpp"
  45. Settings for compiling:
  46. * Add a compiler flag as is
  47. CompilerFlag -DMACRO
  48. Settings for linking:
  49. * Add a linker flag as is for direct control
  50. LinkerFlag -lLibrary
  51. * Add a linker flag with automatic prefix for future proofing
  52. Link Library
  53. Building other projects at the same time:
  54. * Build all projects in myFolder with the SkipIfBinaryExists flag in arbitrary order before continuing with compilation
  55. Build "../myFolder" SkipIfBinaryExists
  56. Building a project crawling from each file matching a pattern:
  57. All variables are inherited, so no variables are given to the command.
  58. * Build a project for each file ending with 'Test.cpp' in a folder named 'tests'.
  59. Projects from "*Test.cpp" in "tests"
  60. * Build a project for each file starting with 'main_' and ending with '.cpp' in a folder named "code/projects".
  61. Projects from "main_*.cpp" in "code/projects"
  62. * Build a project for each file named 'main.cpp' in a folder named "examples".
  63. Projects from "main_*.cpp" in "examples"
  64. * You can also swap argument order like this, because it is designed to be easily extended with more keywords if needed.
  65. Projects in "tests" from "*Test.cpp"
  66. Systems:
  67. * Linux
  68. Set to non-zero on Linux or similar operating systems.
  69. * Windows
  70. Set to non-zero on MS-Windows.
  71. * MacOS
  72. Set to non-zero on MacOS.
  73. Variables:
  74. * SkipIfBinaryExists, skips building if the binary already exists.
  75. * Supressed, prevents a compiled program from running after building, which is usually given as an extra argument to Build to avoid launching all programs in a row.
  76. * ProgramPath, a path to the application to create.
  77. * Compiler, a path or global alias to the compiler.
  78. * CompileFrom, from which path should the compiler be executed? Leave empty to use the current directory.
  79. * Debug, 0 for release, anything else (usually 1) for debug.
  80. * StaticRuntime, 0 for dynamic runtime linking, anything else (usually 1) for static runtime.
  81. * Optimization, a natural integer specifying the amount of optimization to apply.
  82. */
  83. #include "../../../DFPSR/api/fileAPI.h"
  84. #include "Machine.h"
  85. #include "expression.h"
  86. #include "analyzer.h"
  87. #include "generator.h"
  88. using namespace dsr;
  89. ScriptLanguage identifyLanguage(const ReadableString &filename) {
  90. String scriptExtension = string_upperCase(file_getExtension(filename));
  91. if (string_match(scriptExtension, U"BAT")) {
  92. return ScriptLanguage::Batch;
  93. } else if (string_match(scriptExtension, U"SH")) {
  94. return ScriptLanguage::Bash;
  95. } else {
  96. return ScriptLanguage::Unknown;
  97. }
  98. }
  99. // Approximate syntax:
  100. // outputPath <- tempFolder | tempFolder/scriptName.sh | tempFolder\scriptName.bat
  101. // key <- SkipIfBinaryExists | Supressed | ProgramPath | Compiler | CompileFrom | Debug | StaticRuntime | Optimization | (a..z|A..Z)(0..9|a..z|A..Z)*
  102. // flag <- key | key=value
  103. // buildCall <- builderPath outputPath projectPath flag*
  104. // Example uses:
  105. // Build Wizard.DsrProj for Linux using the g++ compiler by generating dfpsr_compile.sh and *.o objects in the /tmp folder.
  106. // ../builder/builder /tmp/dfpsr_compile.sh ./Wizard.DsrProj Compiler=g++ Linux
  107. // One can also just give the temporary folder to have the compiler called directly.
  108. // ../builder/builder /tmp ./Wizard.DsrProj Compiler=g++ Linux
  109. DSR_MAIN_CALLER(dsrMain)
  110. void dsrMain(List<String> args) {
  111. if (args.length() <= 1) {
  112. printText(U"No arguments given to Builder. Starting regression test.\n");
  113. expression_runRegressionTests();
  114. } else if (args.length() == 2) {
  115. printText(U"To use the DFPSR build system, pass a path to a script to generate, a project file or folder containing multiple projects, and the flags you want assigned before building.\n");
  116. printText(U"To run regression tests, don't pass any argument to the program.\n");
  117. } else {
  118. // Print the full command to show the caller if the arguments got messed up.
  119. printText(U"Build command:");
  120. for (int i = 0; i < args.length(); i++) {
  121. printText(U" ", args[i]);
  122. }
  123. printText(U"\n");
  124. // Get the script's destination path, or the temporary folder for all projects built during the session as the first argument.
  125. String outputPath = args[1];
  126. String scriptPath, tempFolder;
  127. ScriptLanguage language = ScriptLanguage::Unknown;
  128. if (file_getEntryType(outputPath) == EntryType::Folder) {
  129. printText(U"The output path is a folder.\n");
  130. // Not creating a script is useful if the operating system does not support any of the generated script languages.
  131. tempFolder = outputPath;
  132. } else {
  133. // Creating a script is useful for understanding what went wrong when building fails.
  134. language = identifyLanguage(outputPath);
  135. if (language == ScriptLanguage::Unknown) {
  136. printText(U"Could not identify the scripting language of \"", outputPath, U"\". Use *.bat, *.sh or just a temporary folder path to call the compiler directly.\n");
  137. return;
  138. }
  139. printText(U"The output path is a script file.\n");
  140. scriptPath = outputPath;
  141. tempFolder = file_getAbsoluteParentFolder(outputPath);
  142. }
  143. printText(U"Using ", tempFolder, U" as the temporary folder for compiled objects.\n");
  144. if (string_length(scriptPath) > 0) {
  145. printText(U"Using ", scriptPath, U" as the temporary script for calling the compiler.\n");
  146. } else {
  147. printText(U"No script path was given. The compiler will be called directly instead.\n");
  148. }
  149. // Get the project file's path, or a folder path containing all projects to build.
  150. String projectPath = args[2];
  151. String projectExtension = string_upperCase(file_getExtension(projectPath));
  152. if (string_match(projectExtension, U"DSRHEAD")) {
  153. printText(U"The path ", projectPath, U" does not refer to a project file. *.DsrHead is imported into projects to automate build configurations for users of a specific library.\n");
  154. return;
  155. } else if (!string_match(projectExtension, U"DSRPROJ")) {
  156. printText(U"The path ", projectPath, U" does not refer to a project file, because it does not have the *.DsrProj extension.\n");
  157. return;
  158. }
  159. // Read the reas after the project's path, as named integers assigned to ones.
  160. // Calling builder with the extra arguments will interpret them as variables and mark them as inherited, so that they are passed on to any other projects build from the project file.
  161. // Other values can be assigned using an equality sign.
  162. // Avoid spaces around the equality sign, because quotes are already used for string arguments in assignments.
  163. Machine settings(file_getPathlessName(projectPath));
  164. argumentsToSettings(settings, args, 3, args.length() - 1);
  165. validateSettings(settings, U"in settings after getting application arguments (in main)");
  166. // Generate build instructions.
  167. String executableExtension;
  168. if (getFlagAsInteger(settings, U"Windows")) {
  169. executableExtension = U".exe";
  170. }
  171. SessionContext buildContext = SessionContext(tempFolder, executableExtension);
  172. buildFromFolder(buildContext, projectPath, settings);
  173. validateSettings(settings, U"in settings after executing the root build script (in main)");
  174. if (language == ScriptLanguage::Unknown) {
  175. // Call the compiler directly.
  176. executeBuildInstructions(buildContext);
  177. } else {
  178. // Generate a script to execute.
  179. generateCompilationScript(buildContext, scriptPath, language);
  180. }
  181. }
  182. }