From 9d871387dcafd338096c5030737a45e696a10638 Mon Sep 17 00:00:00 2001 From: lyon Date: Fri, 6 May 2022 18:49:46 +0800 Subject: [PATCH] use Maker to build modules --- port/linux/.vscode/c_cpp_properties.json | 4 +-- port/linux/test/compile-test.cpp | 18 ++++++++++++-- src/PikaCompiler.c | 31 ++++++++++++++++++++++++ src/PikaCompiler.h | 4 +++ src/PikaObj.h | 1 + tools/pikaByteCodeGen/main.c | 9 +++---- tools/pikaCompiler/build.sh | 1 + 7 files changed, 57 insertions(+), 11 deletions(-) diff --git a/port/linux/.vscode/c_cpp_properties.json b/port/linux/.vscode/c_cpp_properties.json index d9bb6966b..91fe93aa3 100644 --- a/port/linux/.vscode/c_cpp_properties.json +++ b/port/linux/.vscode/c_cpp_properties.json @@ -11,9 +11,7 @@ "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c99", - "cppStandard": "c++14", - "intelliSenseMode": "linux-clang-x64", - "configurationProvider": "ms-vscode.cmake-tools" + "cppStandard": "c++14" } ], "version": 4 diff --git a/port/linux/test/compile-test.cpp b/port/linux/test/compile-test.cpp index d34198e1a..64a00338f 100644 --- a/port/linux/test/compile-test.cpp +++ b/port/linux/test/compile-test.cpp @@ -497,6 +497,20 @@ TEST(lib, load_err_file_type) { } TEST(lib, lib_file_to_array) { - Lib_loadLibraryFileToArray("test/python/lib_to_file.py.a", - "test/python"); + Lib_loadLibraryFileToArray("test/python/lib_to_file.py.a", "test/python"); + EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(make, maker) { + PikaMaker* maker = New_PikaMaker(); + obj_deinit(maker); + EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(make, compile) { + PikaMaker* maker = New_PikaMaker(); + pikaMaker_setPWD(maker, "package/pikascript/"); + pikaMaker_compileModule(maker, "main"); + obj_deinit(maker); + EXPECT_EQ(pikaMemNow(), 0); } diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index 838fda384..c36a4d2bd 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -353,3 +353,34 @@ exit: arg_deinit(file_arg); return res; } + +static void __Maker_compileModuleWithInfo(PikaMaker* self, char* module_name) { + Args buffs = {0}; + char* input_file_name = strsAppend(&buffs, module_name, ".py"); + char* input_file_path = + strsAppend(&buffs, obj_getStr(self, "pwd"), input_file_name); + __platform_printf(" compiling %s...\r\n", input_file_name); + char* output_file_name = strsAppend(&buffs, module_name, ".py.o"); + char* output_file_path = NULL; + output_file_path = + strsAppend(&buffs, obj_getStr(self, "pwd"), "pikascript-api/"); + output_file_path = strsAppend(&buffs, output_file_path, output_file_name); + pikaCompileFileWithOutputName(output_file_path, input_file_path); + strsDeinit(&buffs); +} + +PikaMaker* New_PikaMaker(void) { + PikaMaker* self = New_TinyObj(NULL); + obj_setStr(self, "pwd", ""); + return self; +} + +void pikaMaker_setPWD(PikaMaker* self, char* pwd) { + obj_setStr(self, "pwd", pwd); +} + +void pikaMaker_compileModule(PikaMaker* self, char* module_name) { + __Maker_compileModuleWithInfo(self, module_name); + /* update compile info */ + obj_setStr(self, module_name, "compiled"); +} diff --git a/src/PikaCompiler.h b/src/PikaCompiler.h index 9bb4dc799..0db97e97c 100644 --- a/src/PikaCompiler.h +++ b/src/PikaCompiler.h @@ -20,6 +20,10 @@ void LibObj_listModules(LibObj* self); int LibObj_saveLibraryFile(LibObj* self, char* output_file_name); int LibObj_loadLibraryFile(LibObj* self, char* input_file_name); int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root); +PikaMaker* New_PikaMaker(void); +void pikaMaker_setPWD(PikaMaker* self, char* pwd); +void pikaMaker_compileModule(PikaMaker* self, char* module_name); + #define LIB_VERSION_NUMBER 1 #define LIB_INFO_BLOCK_SIZE 32 diff --git a/src/PikaObj.h b/src/PikaObj.h index 9ee6b46b6..d4a3aaa73 100644 --- a/src/PikaObj.h +++ b/src/PikaObj.h @@ -95,6 +95,7 @@ typedef struct MethodInfo_t { } MethodInfo; typedef PikaObj LibObj; +typedef PikaObj PikaMaker; /* operation */ int32_t obj_deinit(PikaObj* self); diff --git a/tools/pikaByteCodeGen/main.c b/tools/pikaByteCodeGen/main.c index 8d06c664d..3ff7f4a15 100644 --- a/tools/pikaByteCodeGen/main.c +++ b/tools/pikaByteCodeGen/main.c @@ -1,22 +1,19 @@ #include #include #include +#include "BaseObj.h" +#include "PikaCompiler.h" #include "PikaObj.h" #include "PikaParser.h" #include "dataStrs.h" -#include "PikaCompiler.h" #include "libpikabinder.h" PikaObj* __pikaMain; -void pikaCompileFileWithInfo(char* filename){ - __platform_printf(" compiling %s...\r\n", filename); - pikaCompileFile(filename); -} + void main() { /* run pika_binder to bind C modules */ pika_binder(); - pikaCompileFileWithInfo("main.py"); return; } diff --git a/tools/pikaCompiler/build.sh b/tools/pikaCompiler/build.sh index ecebd2523..7a8f25a91 100644 --- a/tools/pikaCompiler/build.sh +++ b/tools/pikaCompiler/build.sh @@ -7,6 +7,7 @@ cp target/release/libpikabinder.a libpikabinder cd ../pikaByteCodeGen cp ../pikaCompiler/libpikabinder . -r +rm pikascript/pikascript-core -r cp ../../src pikascript/pikascript-core -r rm build -rf mkdir build