Skip to content

Commit

Permalink
update makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
autohyl committed Apr 12, 2018
1 parent 593dc64 commit 63b7ff4
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 180 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"git.ignoreLimitWarning": true
{
"git.ignoreLimitWarning": true
}
57 changes: 31 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
142 changes: 71 additions & 71 deletions skynet-src/core/skynet_loadconf.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
#include "skynet_loadconf.h"
#include <assert.h>
#include <string.h>
#include <iostream>

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 <assert.h>
#include <string.h>
#include <iostream>

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;
}
40 changes: 20 additions & 20 deletions skynet-src/core/skynet_loadconf.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

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;
};
123 changes: 62 additions & 61 deletions skynet-src/skynet_main.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
#include <iostream>
#include <assert.h>
#include <string.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;
#include <iostream>
#include <assert.h>
#include <string.h>
#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;
}

0 comments on commit 63b7ff4

Please sign in to comment.