main.c 6.4 KB

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