diff options
| author | OpportunityLiu <[email protected]> | 2019-07-23 20:45:14 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-23 20:45:14 +0800 |
| commit | 89773632186291f5f4d5d48c029c959c0fe774fc (patch) | |
| tree | 230c904ec5aa4a60acfe26063d6a1442b40e4b1f | |
| parent | e8851355af6b24ad1ddf5545a5056eed7f7b9786 (diff) | |
revert
| -rw-r--r-- | core/src/xmake/global/loadfile.c | 196 | ||||
| -rw-r--r-- | core/src/xmake/global/prefix.h | 32 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 51 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | xmake/core/_xmake_main.lua | 36 |
5 files changed, 35 insertions, 281 deletions
diff --git a/core/src/xmake/global/loadfile.c b/core/src/xmake/global/loadfile.c deleted file mode 100644 index b1b4a728d..000000000 --- a/core/src/xmake/global/loadfile.c +++ /dev/null @@ -1,196 +0,0 @@ -/*!A cross-platform build utility based on Lua - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2015 - 2019, TBOOX Open Source Group. - * - * @author OpportunityLiu - * @file loadfile.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "loadfile" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * private implementation - */ - -typedef struct _xm_global_filereader -{ - tb_char_t buffer[8192]; - tb_file_ref_t file; -} xm_global_filereader; - -static tb_char_t const* xm_global_readxmakefile(lua_State* lua, xm_global_filereader* data, size_t* size) -{ - tb_long_t readsize = tb_file_read(data->file, (tb_byte_t*)data->buffer, tb_arrayn(data->buffer)); - if (readsize <= 0) return tb_null; - *size = (size_t)readsize; - return data->buffer; -} - -static tb_int_t xm_global_loadxmakefile(lua_State* lua, tb_char_t const* filepath, tb_char_t const* disppath, tb_char_t const* mode) -{ - tb_file_ref_t file = tb_file_init(filepath, TB_FILE_MODE_RO); - if(!file) - { - lua_pushnil(lua); - lua_pushfstring(lua, "failed to open %s", disppath + 1); - return 2; - } - xm_global_filereader* reader = tb_malloc0_type(xm_global_filereader); - tb_assert_and_check_return_val(reader, 0); - reader->file = file; - tb_int_t loadresult = lua_loadx(lua, (lua_Reader)xm_global_readxmakefile, (tb_void_t*)reader, disppath, mode); - - tb_bool_t status = tb_file_exit(file); - tb_assert_and_check_return_val(status, 0); - tb_free(reader); - file = tb_null; - reader = tb_null; - - if (loadresult == LUA_OK) - { - return 1; - } - else - { - lua_pushnil(lua); - lua_pushvalue(lua, 1); - return 2; - } -} - -static tb_int_t xm_global_loaduserfile(lua_State* lua, tb_char_t const* filepath, tb_char_t const* disppath, tb_char_t const* mode) -{ - lua_getglobal(lua, "io"); - lua_getfield(lua, 1, "file"); - lua_getfield(lua, 1, "open"); - // io, file, open, filepath - lua_pushstring(lua, filepath); - // io, file, ffile, ferr - lua_call(lua, 1, 2); - if (lua_type(lua, -2) == LUA_TNIL) return 2; - // io, file, ffile - lua_pop(lua, 1); - // io, file, ffile, read - lua_getfield(lua, 2, "read"); - lua_pushvalue(lua, 3); - // io, file, ffile, read, ffile, "a" - lua_pushliteral(lua, "a"); - // io, file, ffile, rdata, rerr - lua_call(lua, 2, 2); - lua_getfield(lua, 2, "close"); - // io, file, ffile, rdata, rerr, close, ffile - lua_pushvalue(lua, 3); - // io, file, ffile, rdata, rerr - lua_call(lua, 1, 0); - if (lua_type(lua, -2) == LUA_TNIL) return 2; - // io, file, ffile, rdata - lua_pop(lua, 1); - - lua_getglobal(lua, "load"); - lua_pushvalue(lua, -2); - lua_pushstring(lua, disppath); - lua_pushstring(lua, mode); - lua_call(lua, 3, 2); - - return 2; -} - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ -tb_int_t xm_global_loadfile(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, 0); - - tb_char_t const* filepath = luaL_checkstring(lua, 1); - tb_char_t const* mode = luaL_optstring(lua, 2, "bt"); - - tb_char_t disppath[TB_PATH_MAXN] = {0}; - tb_char_t const* prefix = "@"; - tb_size_t prefixlen = tb_arrayn("@") - 1; - tb_char_t const* root = tb_null; - - tb_bool_t isxmakefile = tb_false; - - do - { - size_t path_len; - tb_char_t const* path; - - lua_settop(lua, 0); - lua_getglobal(lua, "xmake"); - lua_getfield(lua, 1, "_WORKING_DIR"); - path = luaL_checklstring(lua, 2, &path_len); - if (!tb_strncmp(filepath, path, (tb_size_t)path_len)) - { - prefix = "@./"; - prefixlen = tb_arrayn("@./") - 1; - root = path; - break; - } - - lua_settop(lua, 1); - lua_getfield(lua, 1, "_PROGRAM_DIR"); - path = luaL_checklstring(lua, 2, &path_len); - if (!tb_strncmp(filepath, path, (tb_size_t)path_len)) - { - prefix = "@$(programdir)/"; - prefixlen = tb_arrayn("@$(programdir)/") - 1; - root = path; - isxmakefile = tb_true; - break; - } - - lua_settop(lua, 1); - lua_getfield(lua, 1, "_PROJECT_DIR"); - path = luaL_checklstring(lua, 2, &path_len); - if (!tb_strncmp(filepath, path, (tb_size_t)path_len)) - { - prefix = "@$(projectdir)/"; - prefixlen = tb_arrayn("@$(projectdir)/") - 1; - root = path; - break; - } - } while (0); - lua_settop(lua, 0); - - if(root) - { - tb_strncpy(disppath, prefix, TB_PATH_MAXN); - tb_path_relative_to(root, filepath, disppath + prefixlen, TB_PATH_MAXN - prefixlen); - } - else - { - disppath[0] = '@'; - tb_strncpy(disppath + 1, filepath, TB_PATH_MAXN - 1); - } - tb_path_translate(disppath + 1, tb_strlen(disppath + 1), TB_PATH_MAXN - 1); - - if (isxmakefile) - return xm_global_loadxmakefile(lua, filepath, disppath, mode); - else - return xm_global_loaduserfile(lua, filepath, disppath, mode); -} diff --git a/core/src/xmake/global/prefix.h b/core/src/xmake/global/prefix.h deleted file mode 100644 index d48a81b1c..000000000 --- a/core/src/xmake/global/prefix.h +++ /dev/null @@ -1,32 +0,0 @@ -/*!A cross-platform build utility based on Lua - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2015 - 2019, TBOOX Open Source Group. - * - * @author ruki - * @file prefix.h - * - */ -#ifndef XM_GLOBAL_PREFIX_H -#define XM_GLOBAL_PREFIX_H - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "../prefix.h" - - -#endif - - diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index de5e3738b..ff0758882 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -46,7 +46,7 @@ // the machine type typedef struct __xm_machine_t { - // the lua + // the lua lua_State* lua; }xm_machine_t; @@ -56,7 +56,6 @@ typedef struct __xm_machine_t */ // the global functions -tb_int_t xm_global_loadfile(lua_State* lua); // the os functions tb_int_t xm_os_argv(lua_State* lua); @@ -168,13 +167,6 @@ tb_int_t xm_curses_register(lua_State* lua); * globals */ -// the global functions -static luaL_Reg const g_global_functions[] = -{ - { "loadfile", xm_global_loadfile } -, { tb_null, tb_null } -}; - // the os functions static luaL_Reg const g_os_functions[] = { @@ -499,35 +491,6 @@ static tb_bool_t xm_machine_get_project_directory(xm_machine_t* machine, tb_char // save the directory to the global variable: _PROJECT_DIR lua_pushstring(machine->lua, path); lua_setglobal(machine->lua, "_PROJECT_DIR"); - // ok - ok = tb_true; - - } while (0); - - // failed? - if (!ok) tb_printf("error: not found the project directory!\n"); - - // ok? - return ok; -} -static tb_bool_t xm_machine_get_working_directory(xm_machine_t* machine, tb_char_t* path, tb_size_t maxn) -{ - // check - tb_assert_and_check_return_val(machine && path && maxn, tb_false); - - // done - tb_bool_t ok = tb_false; - do - { - // get it from the current directory - if (!tb_directory_current(path, maxn)) break; - - // trace - tb_trace_d("working: %s", path); - - // save the directory to the global variable: _WORKING_DIR - lua_pushstring(machine->lua, path); - lua_setglobal(machine->lua, "_WORKING_DIR"); // ok ok = tb_true; @@ -535,7 +498,7 @@ static tb_bool_t xm_machine_get_working_directory(xm_machine_t* machine, tb_char } while (0); // failed? - if (!ok) tb_printf("error: can't get the working directory!\n"); + if (!ok) tb_printf("error: not found the project directory!\n"); // ok? return ok; @@ -609,13 +572,6 @@ xm_machine_ref_t xm_machine_init() // open lua libraries luaL_openlibs(machine->lua); - // bind global functions - for (tb_size_t i = 0; g_global_functions[i].func; i++) - { - lua_pushcfunction(machine->lua, g_global_functions[i].func); - lua_setglobal(machine->lua, g_global_functions[i].name); - } - // bind os functions luaL_register(machine->lua, "os", g_os_functions); @@ -776,9 +732,6 @@ tb_int_t xm_machine_main(xm_machine_ref_t self, tb_int_t argc, tb_char_t** argv) tb_char_t path[TB_PATH_MAXN] = {0}; if (!xm_machine_get_project_directory(machine, path, sizeof(path))) return -1; - // get the working directory - if (!xm_machine_get_working_directory(machine, path, sizeof(path))) return -1; - // get the program file if (!xm_machine_get_program_file(machine, path, sizeof(path))) return -1; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 9e3c069dc..7232e0fd9 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -14,7 +14,6 @@ xmake_CONFIG = y xmake_C_FILES += \ xmake \ machine \ - global/loadfile \ os/argv \ os/find \ os/link \ diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua index ef3dadd12..962eee224 100644 --- a/xmake/core/_xmake_main.lua +++ b/xmake/core/_xmake_main.lua @@ -29,7 +29,37 @@ xmake._PROGRAM_DIR = _PROGRAM_DIR xmake._PROGRAM_FILE = _PROGRAM_FILE xmake._PROJECT_DIR = _PROJECT_DIR xmake._PROJECT_FILE = "xmake.lua" -xmake._WORKING_DIR = _WORKING_DIR +xmake._WORKING_DIR = os.curdir() + +local function loadfileimpl(filepath, mode) + + -- init displaypath + local binary = false + local displaypath = filepath + if filepath:startswith(xmake._WORKING_DIR) then + displaypath = path.translate("@./" .. path.relative(filepath, xmake._WORKING_DIR)) + elseif filepath:startswith(xmake._PROGRAM_DIR) then + binary = true -- read file by binary mode, will be faster + displaypath = path.translate("@$(programdir)/" .. path.relative(filepath, xmake._PROGRAM_DIR)) + elseif filepath:startswith(xmake._PROJECT_DIR) then + displaypath = path.translate("@$(projectdir)/" .. path.relative(filepath, xmake._PROJECT_DIR)) + end + + -- load script data from file + local file, ferrors = io.open(filepath, binary and "rb" or "r") + if not file then + return nil, ferrors + end + + local data, rerrors = file:read("a") + if not data then + return nil, rerrors + end + file:close() + + -- load script from string + return load(data, displaypath, mode) +end -- init loadfile local _loadfile = _loadfile or loadfile @@ -49,7 +79,7 @@ function loadfile(filepath, mode) end -- load file - local script, errors = _loadfile(filepath, mode) + local script, errors = loadfileimpl(filepath, mode) if script then _loadcache[filepath] = {script = script, mtime = mtime or os.mtime(filepath)} end @@ -59,7 +89,7 @@ end -- init package path table.insert(package.loaders, 2, function(v) local filepath = xmake._PROGRAM_DIR .. "/core/" .. v .. ".lua" - local script, serr = _loadfile(filepath) + local script, serr = loadfileimpl(filepath) if not script then return "\n\tfailed to load " .. filepath .. " : " .. serr end |
