160 lines
4.9 KiB
C
Raw Normal View History

2021-11-28 21:25:25 +08:00
#include <stdarg.h>
2021-11-28 20:32:09 +08:00
#include <stdio.h>
2021-11-28 21:25:25 +08:00
#include <stdlib.h>
2022-05-06 18:49:46 +08:00
#include "BaseObj.h"
#include "PikaCompiler.h"
2021-11-28 20:32:09 +08:00
#include "PikaObj.h"
2021-11-28 21:25:25 +08:00
#include "PikaParser.h"
#include "dataStrs.h"
2022-05-05 16:42:24 +08:00
#include "libpikabinder.h"
2021-11-28 21:25:25 +08:00
2022-10-24 10:14:12 +08:00
void help(char* argv0) {
Args buffs = {0};
char* exe = argv0;
printf(
"Usage:\r\n"
" %s"
" - [Binding C modules and compile all from main.py]\r\n"
" %s test.py"
" - [Compile all from test.py]\r\n"
" %s test.py -o out.a"
" - [Compile all from test.py and link to out.a]\r\n"
" %s -c test.py"
" - [Only compile test.py to test.py.o]\r\n"
" %s -c test.py -o out.o"
" - [Only compile test.py to out.o]\r\n",
exe, exe, exe, exe, exe);
strsDeinit(&buffs);
}
2022-06-24 16:23:44 +08:00
/* fake implement */
PikaObj* __pikaMain;
2022-10-24 00:10:28 +08:00
void New_PikaStdLib_SysObj(void) {}
2022-06-24 16:23:44 +08:00
void New_PikaStdData_List(void) {}
2022-06-30 13:11:01 +08:00
void New_PikaStdData_Dict(void) {}
void New_PikaStdData_Tuple(void) {}
void New_PikaStdData_String(void) {}
void New_PikaStdData_ByteArray(void) {}
2022-10-24 00:10:28 +08:00
char* string_slice(Args* outBuffs, char* str, int start, int end) {
2022-09-30 15:25:24 +08:00
return NULL;
}
2022-06-24 16:23:44 +08:00
2022-10-24 00:10:28 +08:00
int main(int argc, char** argv) {
int parc = argc - 1;
2022-12-01 13:07:24 +08:00
PikaMaker* maker = New_PikaMaker();
/* --add-file xxx --add-file yyy */
for (int i = 1; i < argc; i++) {
// __platform_printf("%s\r\n", argv[i]);
2022-12-01 17:50:27 +08:00
if (0 == strcmp(argv[i], "--add-file")) {
2022-12-01 13:07:24 +08:00
// __platform_printf("add file: %s\r\n", argv[i + 1]);
if (i + 1 < argc) {
pikaMaker_linkRaw(maker, argv[i + 1]);
}
}
}
/* delete --xxx yyy */
for (int i = 1; i < argc; i++) {
if (0 == strcmp(argv[i], "--add-file")) {
// printf("before delete: %d\r\n", parc);
// for (int j = 0; j < parc; j++) {
// printf("%s\r\n", argv[j + 1]);
// }
parc -= 2;
for (int j = i; j < argc - 2; j++) {
argv[j] = argv[j + 2];
}
// printf("after delete: %d\r\n", parc);
// for (int j = 0; j < parc; j++) {
// printf("%s\r\n", argv[j + 1]);
// }
}
}
2022-10-24 00:10:28 +08:00
if (0 == parc) {
/* no input, default to main.py */
/* run pika_binder to bind C modules */
pika_binder();
pikaMaker_compileModuleWithDepends(maker, "main");
2022-10-24 10:14:12 +08:00
PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
2022-12-01 13:07:24 +08:00
pikaMaker_deinit(maker);
2022-10-24 10:14:12 +08:00
return res;
2022-10-24 00:10:28 +08:00
}
2022-05-15 00:10:09 +08:00
2022-10-24 00:10:28 +08:00
/* example: ./rust-msc-latest-linux -h | --help */
if (1 == parc) {
if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help")) {
help(argv[0]);
return 0;
}
}
2022-05-14 23:44:44 +08:00
2022-10-24 00:10:28 +08:00
/* example: ./rust-msc-latest-linux main.py */
if (1 == parc) {
char* module_entry = argv[1];
/* remove subfix */
char* subfix = strrchr(module_entry, '.');
if (subfix) {
*subfix = '\0';
}
pikaMaker_compileModuleWithDepends(maker, module_entry);
2022-10-24 10:14:12 +08:00
PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
2022-12-01 13:07:24 +08:00
pikaMaker_deinit(maker);
2022-10-24 10:14:12 +08:00
return res;
2022-10-24 00:10:28 +08:00
}
/* example ./rust-msc-latest-linux main.py -o out.a */
if (3 == parc) {
2022-10-24 00:27:11 +08:00
if (0 == strcmp(argv[2], "-o")) {
char* module_entry = argv[1];
/* remove subfix */
char* subfix = strrchr(module_entry, '.');
if (subfix) {
*subfix = '\0';
}
pikaMaker_compileModuleWithDepends(maker, module_entry);
2022-10-24 10:14:12 +08:00
PIKA_RES res = pikaMaker_linkCompiledModules(maker, argv[3]);
2022-12-01 13:07:24 +08:00
pikaMaker_deinit(maker);
2022-10-24 10:14:12 +08:00
return res;
2022-10-24 00:10:28 +08:00
}
}
/* example: ./rust-msc-latest-linux -c main.py */
if (2 == parc) {
if (0 == strcmp(argv[1], "-c")) {
Args buffs = {0};
/* compile only */
char* module_entry = argv[2];
char* module_out = strsCopy(&buffs, module_entry);
char* subfix = strrchr(module_out, '.');
if (subfix) {
*subfix = '\0';
}
module_out = strsAppend(&buffs, module_out, ".py.o");
printf("compiling %s to %s...\r\n", module_entry, module_out);
2022-10-24 10:14:12 +08:00
PIKA_RES res =
pikaCompileFileWithOutputName(module_out, module_entry);
2022-10-24 00:10:28 +08:00
strsDeinit(&buffs);
2022-10-24 10:14:12 +08:00
return res;
2022-10-24 00:10:28 +08:00
}
}
/* example: ./rust-msc-latest-linux -c main.py -o main.py.o */
if (4 == parc) {
if (0 == strcmp(argv[1], "-c") && 0 == strcmp(argv[3], "-o")) {
/* compile only */
char* module_entry = argv[2];
printf("compiling %s to %s...\r\n", module_entry, argv[4]);
2022-10-24 10:14:12 +08:00
PIKA_RES res = pikaCompileFileWithOutputName(argv[4], module_entry);
return res;
2022-10-24 00:10:28 +08:00
}
}
/* no valid input */
2022-10-24 10:14:12 +08:00
printf("Invalid input.\r\n");
2022-10-24 00:10:28 +08:00
help(argv[0]);
2022-10-24 10:14:12 +08:00
return -1;
2021-11-28 22:29:25 +08:00
}