main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "BaseObj.h"
  5. #include "PikaCompiler.h"
  6. #include "PikaObj.h"
  7. #include "PikaParser.h"
  8. #include "dataStrs.h"
  9. #include "libpikabinder.h"
  10. void help(char* argv0) {
  11. Args buffs = {0};
  12. char* exe = argv0;
  13. printf(
  14. "Usage:\r\n"
  15. " %s"
  16. " - [Binding C modules and compile all from main.py]\r\n"
  17. " %s test.py"
  18. " - [Compile all from test.py]\r\n"
  19. " %s test.py -o out.a"
  20. " - [Compile all from test.py and link to out.a]\r\n"
  21. " %s -c test.py"
  22. " - [Only compile test.py to test.py.o]\r\n"
  23. " %s -c test.py -o out.o"
  24. " - [Only compile test.py to out.o]\r\n",
  25. exe, exe, exe, exe, exe);
  26. strsDeinit(&buffs);
  27. }
  28. /* fake implement */
  29. PikaObj* __pikaMain;
  30. void New_PikaStdLib_SysObj(void) {}
  31. void New_PikaStdData_List(void) {}
  32. void New_PikaStdData_Dict(void) {}
  33. void New_PikaStdData_Tuple(void) {}
  34. void New_PikaStdData_String(void) {}
  35. void New_PikaStdData_ByteArray(void) {}
  36. int strGetSizeUtf8(char* str){return 0;}
  37. char* string_slice(Args* outBuffs, char* str, int start, int end) {
  38. return NULL;
  39. }
  40. static int _do_main(int argc, char** argv) {
  41. int parc = argc - 1;
  42. PikaMaker* maker = New_PikaMaker();
  43. /* --add-file xxx --add-file yyy */
  44. // __platform_printf("parc: %d\r\n", parc);
  45. for (int i = 1; i < argc; i++) {
  46. // __platform_printf("%s\r\n", argv[i]);
  47. if (0 == strcmp(argv[i], "--add-file")) {
  48. // __platform_printf("add file: %s\r\n", argv[i + 1]);
  49. if (i + 1 < argc) {
  50. pikaMaker_linkRaw(maker, argv[i + 1]);
  51. }
  52. }
  53. }
  54. /* delete --xxx yyy */
  55. for (int i = 1; i < argc; i++) {
  56. if (0 == strcmp(argv[i], "--add-file")) {
  57. // printf("before delete: %d\r\n", parc);
  58. // for (int j = 0; j < parc; j++) {
  59. // printf("%s\r\n", argv[j + 1]);
  60. // }
  61. parc -= 2;
  62. for (int j = i; j < argc - 2; j++) {
  63. argv[j] = argv[j + 2];
  64. }
  65. // printf("after delete: %d\r\n", parc);
  66. // for (int j = 0; j < parc; j++) {
  67. // printf("%s\r\n", argv[j + 1]);
  68. // }
  69. }
  70. }
  71. if (0 == parc) {
  72. /* no input, default to main.py */
  73. /* run pika_binder to bind C modules */
  74. pika_binder();
  75. pikaMaker_compileModuleWithDepends(maker, "main");
  76. PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
  77. pikaMaker_deinit(maker);
  78. return res;
  79. }
  80. /* example: ./rust-msc-latest-linux -h | --help */
  81. if (1 == parc) {
  82. if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help")) {
  83. help(argv[0]);
  84. return 0;
  85. }
  86. }
  87. /* example: ./rust-msc-latest-linux main.py */
  88. if (1 == parc) {
  89. char* module_entry = argv[1];
  90. /* remove subfix */
  91. char* subfix = strrchr(module_entry, '.');
  92. if (subfix) {
  93. *subfix = '\0';
  94. }
  95. pikaMaker_compileModuleWithDepends(maker, module_entry);
  96. PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
  97. pikaMaker_deinit(maker);
  98. return res;
  99. }
  100. /* example ./rust-msc-latest-linux main.py -o out.a */
  101. if (3 == parc) {
  102. if (0 == strcmp(argv[2], "-o")) {
  103. char* module_entry = argv[1];
  104. /* remove subfix */
  105. char* subfix = strrchr(module_entry, '.');
  106. if (subfix) {
  107. *subfix = '\0';
  108. }
  109. pikaMaker_compileModuleWithDepends(maker, module_entry);
  110. PIKA_RES res = pikaMaker_linkCompiledModules(maker, argv[3]);
  111. pikaMaker_deinit(maker);
  112. return res;
  113. }
  114. }
  115. /* example: ./rust-msc-latest-linux -c main.py */
  116. if (2 == parc) {
  117. if (0 == strcmp(argv[1], "-c")) {
  118. Args buffs = {0};
  119. /* compile only */
  120. char* module_entry = argv[2];
  121. char* module_out = strsCopy(&buffs, module_entry);
  122. char* subfix = strrchr(module_out, '.');
  123. if (subfix) {
  124. *subfix = '\0';
  125. }
  126. module_out = strsAppend(&buffs, module_out, ".py.o");
  127. printf("compiling %s to %s...\r\n", module_entry, module_out);
  128. PIKA_RES res =
  129. pikaCompileFileWithOutputName(module_out, module_entry);
  130. strsDeinit(&buffs);
  131. return res;
  132. }
  133. }
  134. /* example: ./rust-msc-latest-linux -c main.py -o main.py.o */
  135. if (4 == parc) {
  136. if (0 == strcmp(argv[1], "-c") && 0 == strcmp(argv[3], "-o")) {
  137. /* compile only */
  138. char* module_entry = argv[2];
  139. printf("compiling %s to %s...\r\n", module_entry, argv[4]);
  140. PIKA_RES res = pikaCompileFileWithOutputName(argv[4], module_entry);
  141. return res;
  142. }
  143. }
  144. /* no valid input */
  145. printf("Invalid input.\r\n");
  146. help(argv[0]);
  147. return -1;
  148. }
  149. int main(int argc, char** argv) {
  150. char* buf = NULL;
  151. FILE* pika_studio = NULL;
  152. if (argc == 1) {
  153. pika_studio = __platform_fopen("pika.studio", "r");
  154. if (NULL != pika_studio) {
  155. buf = __platform_malloc(1024);
  156. __platform_fread(buf, 1, 1024, pika_studio);
  157. /* find <args> </args> */
  158. char* start = strstr(buf, "<args>");
  159. char* end = strstr(buf, "</args>");
  160. if (start && end) {
  161. start += 6;
  162. *end = '\0';
  163. __platform_printf("args: %s\r\n", start);
  164. /* split args */
  165. char* args[32] = {0};
  166. args[0] = argv[0];
  167. char* p = start;
  168. while (p < end) {
  169. char* q = strchr(p, ' ');
  170. if (q) {
  171. *q = '\0';
  172. args[argc++] = p;
  173. p = q + 1;
  174. } else {
  175. args[argc++] = p;
  176. break;
  177. }
  178. }
  179. argv = args;
  180. // for (int i = 0; i < argc; i++) {
  181. // __platform_printf("argv[%d]: %s\r\n", i, argv[i]);
  182. // }
  183. }
  184. }
  185. }
  186. int ret = _do_main(argc, argv);
  187. if (NULL != buf) {
  188. __platform_free(buf);
  189. }
  190. if (NULL != pika_studio) {
  191. __platform_fclose(pika_studio);
  192. }
  193. return ret;
  194. }