avdl_main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <time.h>
  7. #include <errno.h>
  8. #include "avdl_struct_table.h"
  9. #include "avdl_commands.h"
  10. #include "avdl_file_op.h"
  11. #include "avdl_lexer.h"
  12. #include "avdl_semantic_analyser.h"
  13. #include "avdl_ast_node.h"
  14. #include "avdl_parser.h"
  15. #include "avdl_platform.h"
  16. extern float parsing_float;
  17. char included_files[10][100];
  18. int included_files_num = 0;
  19. // buffer for general use
  20. #define DD_BUFFER_SIZE 1000
  21. char buffer[DD_BUFFER_SIZE];
  22. // game node, parent of all nodes
  23. struct ast_node *game_node;
  24. #define MAX_INPUT_FILES 10
  25. const char *dependencies[] = {
  26. "GLEW",
  27. "m",
  28. "glut",
  29. "SDL2-2.0",
  30. "SDL2_mixer-2.0",
  31. };
  32. unsigned int dependencies_count = sizeof(dependencies) /sizeof(char *);
  33. char *includePath = 0;
  34. char *installLocation = "";
  35. // init data, parse, exit
  36. int main(int argc, char *argv[])
  37. {
  38. avdl_platform_initialise();
  39. /* tweakable data
  40. */
  41. int show_ast = 0;
  42. int show_struct_table = 0;
  43. char filename[MAX_INPUT_FILES][100];
  44. int input_file_total = 0;
  45. char *outname = 0;
  46. int getDependencies = 0;
  47. /*
  48. * phases
  49. *
  50. * translate: translate `.dd` files to `.c`
  51. * compile: compile `.c` files to `.o`
  52. * link: link all `.o` files to an executable
  53. *
  54. */
  55. int translate = 1;
  56. int compile = 1;
  57. int link = 1;
  58. // parse arguments
  59. for (int i = 1; i < argc; i++) {
  60. // dash argument
  61. if (strlen(argv[i]) > 0 && argv[i][0] == '-') {
  62. // double dash argument
  63. if (strlen(argv[i]) > 1 && argv[i][1] == '-') {
  64. // temp
  65. if (strcmp(argv[i], "--dependencies") == 0) {
  66. getDependencies = 1;
  67. }
  68. else
  69. // print abstract syntax tree
  70. if (strcmp(argv[i], "--print-ast") == 0) {
  71. show_ast = 1;
  72. }
  73. else
  74. // print struct table
  75. if (strcmp(argv[i], "--print-struct-table") == 0) {
  76. show_struct_table = 1;
  77. }
  78. else
  79. // compiling for windows
  80. if (strcmp(argv[i], "--windows") == 0) {
  81. avdl_platform_set(AVDL_PLATFORM_WINDOWS);
  82. }
  83. else
  84. // compiling for linux
  85. if (strcmp(argv[i], "--linux") == 0) {
  86. avdl_platform_set(AVDL_PLATFORM_LINUX);
  87. }
  88. else
  89. // compiling for android
  90. if (strcmp(argv[i], "--android") == 0) {
  91. avdl_platform_set(AVDL_PLATFORM_ANDROID);
  92. link = 0;
  93. }
  94. else
  95. // show version number
  96. if (strcmp(argv[i], "--version") == 0) {
  97. printf(PKG_NAME " v%s\n", PKG_VERSION);
  98. return -1;
  99. }
  100. else
  101. // custom install location
  102. if (strcmp(argv[i], "--install-loc") == 0) {
  103. if (argc > i+1) {
  104. installLocation = argv[i+1];
  105. i++;
  106. }
  107. else {
  108. printf("avdl error: --install-loc expects a path\n");
  109. return -1;
  110. }
  111. }
  112. // unknown double dash argument
  113. else {
  114. printf("avdl error: cannot understand double dash argument '%s'\n", argv[i]);
  115. return -1;
  116. }
  117. }
  118. else
  119. /* phase arguments
  120. * -t: translate only
  121. * -c: skip linking
  122. */
  123. if (strcmp(argv[i], "-t") == 0) {
  124. compile = 0;
  125. link = 0;
  126. }
  127. else
  128. if (strcmp(argv[i], "-c") == 0) {
  129. link = 0;
  130. }
  131. else
  132. // output filename
  133. if (strcmp(argv[i], "-o") == 0) {
  134. if (argc > i+1) {
  135. outname = argv[i+1];
  136. i++;
  137. }
  138. else {
  139. printf("avdl error: name is expected after `-o`\n");
  140. return -1;
  141. }
  142. }
  143. else
  144. // add include path
  145. if (strcmp(argv[i], "-I") == 0) {
  146. if (argc > i+1) {
  147. includePath = argv[i+1];
  148. i++;
  149. }
  150. else {
  151. printf("avdl error: include path is expected after `-I`\n");
  152. return -1;
  153. }
  154. }
  155. // unknown single dash argument
  156. else {
  157. printf("avdl error: cannot understand dash argument '%s'\n", argv[i]);
  158. return -1;
  159. }
  160. }
  161. // non-dash argument - input file?
  162. else {
  163. // input file
  164. if (input_file_total < MAX_INPUT_FILES) {
  165. strncpy(filename[input_file_total], argv[i], 100);
  166. filename[input_file_total][99] = '\0';
  167. input_file_total++;
  168. }
  169. // error argument
  170. else {
  171. printf("avdl error: '%s': Only %d input files can be provided at a time\n", argv[i], MAX_INPUT_FILES);
  172. return -1;
  173. }
  174. }
  175. }
  176. // make sure the minimum to parse exists
  177. if (input_file_total <= 0) {
  178. printf("avdl error: No filename given\n");
  179. return -1;
  180. }
  181. if (getDependencies) {
  182. for (unsigned int i = 0; i < dependencies_count; i++) {
  183. strcpy(buffer, "ldd ");
  184. strcat(buffer, filename[0]);
  185. strcat(buffer, " | grep 'lib");
  186. strcat(buffer, dependencies[i]);
  187. strcat(buffer, "\\.' | sed 's/^.*=> \\(.*\\) .*$/\\1/'");
  188. //printf("command: %s\n", buffer);
  189. system(buffer);
  190. }
  191. return 0;
  192. }
  193. if (outname && !link && input_file_total > 1) {
  194. printf("avdl error: cannot supply `-o` with `-c` and multiple input files\n");
  195. return -1;
  196. }
  197. if (translate) {
  198. //printf("~~~ translation phase ~~~\n");
  199. }
  200. // translate all given input files
  201. for (int i = 0; i < input_file_total && translate; i++) {
  202. // check if file is meant to be compiled, or is already compiled
  203. if (strcmp(filename[i] +strlen(filename[i]) -3, ".dd") != 0) {
  204. continue;
  205. }
  206. included_files_num = 0;
  207. // initialise the parent node
  208. game_node = ast_create(AST_GAME, 0);
  209. semanticAnalyser_convertToAst(game_node, filename[i]);
  210. /*
  211. * if only transpiling, check output file
  212. */
  213. //printf("transpiling: %s", filename[i]);
  214. // parse resulting ast tree to a file
  215. if (!compile && !link && outname) {
  216. strcpy(buffer, outname);
  217. }
  218. else {
  219. // given an outname, compile the .c file in the same directory
  220. if (outname) {
  221. strcpy(buffer, outname);
  222. buffer[strlen(buffer) -1] = 'c';
  223. strcpy(filename[i], buffer);
  224. }
  225. // by default, put .c file in the same directory as source one
  226. else {
  227. filename[i][strlen(filename[i]) -2] = 'c';
  228. filename[i][strlen(filename[i]) -1] = '\0';
  229. strcpy(buffer, filename[i]);
  230. }
  231. }
  232. //printf(" to %s\n", buffer);
  233. if (transpile_cglut(buffer, game_node) != 0) {
  234. printf("avdl: transpilation failed to file: %s\n", buffer);
  235. return -1;
  236. }
  237. // print debug data
  238. if (show_ast) {
  239. ast_print(game_node);
  240. }
  241. if (show_struct_table) {
  242. struct_table_print();
  243. }
  244. }
  245. if (compile) {
  246. //printf("~~~ compilation phase ~~~\n");
  247. }
  248. // compile all given input files
  249. for (int i = 0; i < input_file_total && compile; i++) {
  250. // check if file is meant to be compiled, or is already compiled
  251. if (strcmp(filename[i] +strlen(filename[i]) -2, ".c") != 0) {
  252. continue;
  253. }
  254. //printf("compiling: %s\n", filename[i]);
  255. // compile
  256. strcpy(buffer, "gcc -DDD_PLATFORM_NATIVE -c -w ");
  257. strcat(buffer, filename[i]);
  258. strcat(buffer, " -o ");
  259. if (outname && !link) {
  260. strcat(buffer, outname);
  261. }
  262. else {
  263. filename[i][strlen(filename[i]) -1] = 'o';
  264. strcat(buffer, filename[i]);
  265. }
  266. strcat(buffer, " -O3 -lGL -lGLU -lGLEW -lglut -lavdl-cengine -lm -w -lSDL2 -lSDL2_mixer");
  267. if (includePath) {
  268. strcat(buffer, " -I ");
  269. strcat(buffer, includePath);
  270. }
  271. //printf("command: %s\n", buffer);
  272. if (system(buffer)) {
  273. printf("avdl: error compiling file: %s\n", filename[i]);
  274. exit(-1);
  275. }
  276. }
  277. if (1) {
  278. //printf("~~~ asset phase ~~~\n");
  279. }
  280. // compile all given input files
  281. for (int i = 0; i < input_file_total && compile; i++) {
  282. // check if file is asset
  283. if (strcmp(filename[i] +strlen(filename[i]) -4, ".ply") != 0
  284. && strcmp(filename[i] +strlen(filename[i]) -4, ".bmp") != 0
  285. && strcmp(filename[i] +strlen(filename[i]) -4, ".wav") != 0
  286. && strcmp(filename[i] +strlen(filename[i]) -4, ".ogg") != 0) {
  287. continue;
  288. }
  289. if (outname) {
  290. strcpy(buffer, outname);
  291. }
  292. else {
  293. strcpy(buffer, filename[i]);
  294. buffer[strlen(buffer) -4] = '\0';
  295. strcat(buffer, ".asset");
  296. }
  297. //printf("compiling asset: %s to %s\n", filename[i], buffer);
  298. file_copy(filename[i], buffer, 0);
  299. }
  300. // compile the final executable
  301. if (link) {
  302. //printf("~~~ link phase ~~~\n");
  303. buffer[0] = '\0';
  304. sprintf(buffer, "gcc -DDD_PLATFORM_NATIVE ");
  305. for (int i = 0; i < input_file_total; i++) {
  306. strcat(buffer, filename[i]);
  307. strcat(buffer, " ");
  308. }
  309. strcat(buffer, "-o ");
  310. if (outname) {
  311. strcat(buffer, outname);
  312. }
  313. else {
  314. strcat(buffer, "game");
  315. }
  316. strcat(buffer, " -lGLU -O3 -lavdl-cengine -lm -w -lSDL2 -lSDL2_mixer -lpthread -lglut -lGL -lGLEW");
  317. //printf("command: %s\n", buffer);
  318. if (system(buffer)) {
  319. printf("avdl: error linking files\n");
  320. exit(-1);
  321. }
  322. }
  323. // success!
  324. return 0;
  325. }