From 63b7ff490e4a0c991b82b71117e2260e55ac9c98 Mon Sep 17 00:00:00 2001 From: yulin <1374868636@qq.com> Date: Thu, 12 Apr 2018 18:24:32 +0800 Subject: [PATCH] update makefile --- .vscode/settings.json | 4 +- Makefile | 57 ++++++----- skynet-src/core/skynet_loadconf.cpp | 142 ++++++++++++++-------------- skynet-src/core/skynet_loadconf.h | 40 ++++---- skynet-src/skynet_main.cpp | 123 ++++++++++++------------ 5 files changed, 186 insertions(+), 180 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3b66410..ca6fe06 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ -{ - "git.ignoreLimitWarning": true +{ + "git.ignoreLimitWarning": true } \ No newline at end of file diff --git a/Makefile b/Makefile index 5e5cb30..239c1ea 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,31 @@ -LUA_STATICLIB := 3rd/lua/liblua.a -LUA_LIB ?= $(LUA_STATICLIB) -LUA_INC ?= 3rd/lua/ - -INC = -I./ -I$(LUA_INC) -LIB += $(LUA_STATICLIB) -ldl -CFLAGS = -g - -TARGET = skynet - -SKYNET_SRC = skynet-src -SKYNET_OBJ = $(SKYNET_SRC)/skynet_main.o - -$(TARGET):$(SKYNET_OBJ) - $(CXX) $(CFLAGS) $(INC) -o $@ $^ $(LIB) - -%.o:%.cpp - $(CXX) $(CFLAGS) $(INC) -c -o $@ $< - - -$(LUA_STATICLIB): - cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' linux - -cleanall: - cd 3rd/lua && $(MAKE) clean - rm -f $(LUA_STATICLIB) +LUA_STATICLIB := 3rd/lua/liblua.a +LUA_LIB ?= $(LUA_STATICLIB) +LUA_INC ?= 3rd/lua/ + +SKYNET_SRC = skynet-src +INC = -I./ -I$(LUA_INC) -I$(SKYNET_SRC)/core/ +LIB += $(LUA_STATICLIB) -ldl +CFLAGS = -g + +TARGET = skynet + +SKYNET_OBJ = $(SKYNET_SRC)/skynet_main.o\ + $(SKYNET_SRC)/core/skynet_loadconf.o + +$(TARGET):$(SKYNET_OBJ) + $(CXX) $(CFLAGS) $(INC) -o $@ $^ $(LIB) + +%.o:%.cpp + $(CXX) $(CFLAGS) $(INC) -c -o $@ $< + +$(LUA_STATICLIB): + cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' linux + +clean: + rm -f $(TARGET) + rm -f $(SKYNET_SRC)/core/*.o + rm -f $(SKYNET_SRC)/*.o + +cleanall: clean + cd 3rd/lua && $(MAKE) clean + rm -f $(LUA_STATICLIB) diff --git a/skynet-src/core/skynet_loadconf.cpp b/skynet-src/core/skynet_loadconf.cpp index dbb7306..bacf54e 100644 --- a/skynet-src/core/skynet_loadconf.cpp +++ b/skynet-src/core/skynet_loadconf.cpp @@ -1,72 +1,72 @@ -#include "skynet_loadconf.h" -#include -#include -#include - -const char *LoadConf::load_config = "\ - local result = {}\n\ - local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ - local sep = package.config:sub(1,1)\n\ - local current_path = [[.]]..sep\n\ - local function include(filename)\n\ - local last_path = current_path\n\ - local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ - if path then\n\ - if path:sub(1,1) == sep then -- root\n\ - current_path = path\n\ - else\n\ - current_path = current_path .. path\n\ - end\n\ - else\n\ - name = filename\n\ - end\n\ - local f = assert(io.open(current_path .. name))\n\ - local code = assert(f:read [[*a]])\n\ - code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ - f:close()\n\ - assert(load(code,[[@]]..filename,[[t]],result))()\n\ - current_path = last_path\n\ - end\n\ - setmetatable(result, { __index = { include = include } })\n\ - local config_name = ...\n\ - include(config_name)\n\ - setmetatable(result, nil)\n\ - return result\n\ -"; - -bool LoadConf::load_config(const char* config_file) { - int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); - assert(err == LUA_OK); - lua_pushstring(L, config_file); - - err = lua_pcall(L, 1, 1, 0); - if (err) { - std::cerr << lua_tostring(L, -1) << std::endl; - return false; - } - - lua_pushnil(L); - while (lua_next(L, -2)) { - int keyt = lua_type(L, -2); - if (keyt != LUA_TSTRING) { - std::cerr << "Invalid config table" << std::endl; - return false; - } - - const char* key = lua_tostring(L, -2); - if (lua_type(L, -1) == LUA_TBOOLEAN) { - int b = lua_toboolean(L, -1); - - } - else { - const char* value = lua_tostring(L, -1); - if (value == NULL) { - std::cerr << "Invalid config table key = " << key << std::endl; - - } - } - lua_pop(L, 1); - } - lua_pop(L, -1); - return true; +#include "skynet_loadconf.h" +#include +#include +#include + +const char *LoadConf::load_config = "\ + local result = {}\n\ + local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ + local sep = package.config:sub(1,1)\n\ + local current_path = [[.]]..sep\n\ + local function include(filename)\n\ + local last_path = current_path\n\ + local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ + if path then\n\ + if path:sub(1,1) == sep then -- root\n\ + current_path = path\n\ + else\n\ + current_path = current_path .. path\n\ + end\n\ + else\n\ + name = filename\n\ + end\n\ + local f = assert(io.open(current_path .. name))\n\ + local code = assert(f:read [[*a]])\n\ + code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ + f:close()\n\ + assert(load(code,[[@]]..filename,[[t]],result))()\n\ + current_path = last_path\n\ + end\n\ + setmetatable(result, { __index = { include = include } })\n\ + local config_name = ...\n\ + include(config_name)\n\ + setmetatable(result, nil)\n\ + return result\n\ +"; + +bool LoadConf::load_config_file(const char* config_file) { + int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); + assert(err == LUA_OK); + lua_pushstring(L, config_file); + + err = lua_pcall(L, 1, 1, 0); + if (err) { + std::cerr << lua_tostring(L, -1) << std::endl; + return false; + } + + lua_pushnil(L); + while (lua_next(L, -2)) { + int keyt = lua_type(L, -2); + if (keyt != LUA_TSTRING) { + std::cerr << "Invalid config table" << std::endl; + return false; + } + + const char* key = lua_tostring(L, -2); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + int b = lua_toboolean(L, -1); + + } + else { + const char* value = lua_tostring(L, -1); + if (value == NULL) { + std::cerr << "Invalid config table key = " << key << std::endl; + + } + } + lua_pop(L, 1); + } + lua_pop(L, -1); + return true; } \ No newline at end of file diff --git a/skynet-src/core/skynet_loadconf.h b/skynet-src/core/skynet_loadconf.h index 5562202..ae214c2 100644 --- a/skynet-src/core/skynet_loadconf.h +++ b/skynet-src/core/skynet_loadconf.h @@ -1,21 +1,21 @@ -extern "C" { -#include -#include -#include -} - -class LoadConf { -public: - LoadConf(struct lua_State *l = new luaL_newstate()) : L(l) { - luaL_openlibs(L); // link lua libs - } - ~LoadConf() { - lua_close(L); - } - - bool load_config(const char* config_file); - -private: - struct lua_State *L; - static const char *load_config; +extern "C" { +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +} + +class LoadConf { +public: + LoadConf(struct lua_State *l) : L(l) { + luaL_openlibs(L); // link lua libs + } + ~LoadConf() { + lua_close(L); + } + + bool load_config_file(const char* config_file); + +private: + struct lua_State *L; + static const char *load_config; }; \ No newline at end of file diff --git a/skynet-src/skynet_main.cpp b/skynet-src/skynet_main.cpp index da511ee..6ad7f71 100644 --- a/skynet-src/skynet_main.cpp +++ b/skynet-src/skynet_main.cpp @@ -1,62 +1,63 @@ -#include -#include -#include - -using namespace std; - -static const char * load_config = "\ - local result = {}\n\ - local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ - local sep = package.config:sub(1,1)\n\ - local current_path = [[.]]..sep\n\ - local function include(filename)\n\ - local last_path = current_path\n\ - local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ - if path then\n\ - if path:sub(1,1) == sep then -- root\n\ - current_path = path\n\ - else\n\ - current_path = current_path .. path\n\ - end\n\ - else\n\ - name = filename\n\ - end\n\ - local f = assert(io.open(current_path .. name))\n\ - local code = assert(f:read [[*a]])\n\ - code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ - f:close()\n\ - assert(load(code,[[@]]..filename,[[t]],result))()\n\ - current_path = last_path\n\ - end\n\ - setmetatable(result, { __index = { include = include } })\n\ - local config_name = ...\n\ - include(config_name)\n\ - setmetatable(result, nil)\n\ - return result\n\ -"; - -int main(int argc, char ** argv) { - const char * config_file = NULL; - if (argc > 1) { - config_file = argv[1]; - } else { - cerr << "Need a config file." << endl; - return 1; - } - - struct lua_State *L = luaL_newstate(); - luaL_openlibs(L); // link lua lib - - int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); - assert(err == LUA_OK); - lua_pushstring(L, config_file); - - err = lua_pcall(L, 1, 1, 0); - if (err) { - cerr << lua_tostring(L, -1) << endl; - lua_close(L); - return 1; - } - - return 0; +#include +#include +#include +#include "core/skynet_loadconf.h" + +using namespace std; + +static const char * load_config = "\ + local result = {}\n\ + local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\ + local sep = package.config:sub(1,1)\n\ + local current_path = [[.]]..sep\n\ + local function include(filename)\n\ + local last_path = current_path\n\ + local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\ + if path then\n\ + if path:sub(1,1) == sep then -- root\n\ + current_path = path\n\ + else\n\ + current_path = current_path .. path\n\ + end\n\ + else\n\ + name = filename\n\ + end\n\ + local f = assert(io.open(current_path .. name))\n\ + local code = assert(f:read [[*a]])\n\ + code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\ + f:close()\n\ + assert(load(code,[[@]]..filename,[[t]],result))()\n\ + current_path = last_path\n\ + end\n\ + setmetatable(result, { __index = { include = include } })\n\ + local config_name = ...\n\ + include(config_name)\n\ + setmetatable(result, nil)\n\ + return result\n\ +"; + +int main(int argc, char ** argv) { + const char * config_file = NULL; + if (argc > 1) { + config_file = argv[1]; + } else { + cerr << "Need a config file." << endl; + return 1; + } + + struct lua_State *L = luaL_newstate(); + luaL_openlibs(L); // link lua lib + + int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t"); + assert(err == LUA_OK); + lua_pushstring(L, config_file); + + err = lua_pcall(L, 1, 1, 0); + if (err) { + cerr << lua_tostring(L, -1) << endl; + lua_close(L); + return 1; + } + + return 0; } \ No newline at end of file