summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-02-14 23:02:04 +0800
committerruki <[email protected]>2020-02-14 12:12:28 +0800
commitffb92f48bcfbe4c34d027a7e01ff27b18b97c431 (patch)
treed1878aeac54ac4faf1905a964800ff8956cc78f7
parentb44a566e16e9022428d0f4453c8f1df0af1577d5 (diff)
improve host and arch
-rw-r--r--core/src/xmake/machine.c133
-rw-r--r--xmake/core/_xmake_main.lua3
-rw-r--r--xmake/core/base/os.lua23
-rw-r--r--xmake/core/base/path.lua47
-rw-r--r--xmake/core/sandbox/modules/os.lua3
-rw-r--r--xmake/platforms/cygwin/config.lua10
-rw-r--r--xmake/platforms/msys/config.lua10
7 files changed, 146 insertions, 83 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index addfe7d7e..ba2510add 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -562,13 +562,57 @@ static tb_bool_t xm_machine_get_project_directory(xm_machine_t* machine, tb_char
// ok?
return ok;
}
+static tb_void_t xm_machine_init_host(xm_machine_t* machine)
+{
+ // check
+ tb_assert_and_check_return(machine && machine->lua);
+
+ // init system host
+ tb_char_t const* syshost = tb_null;
+#if defined(TB_CONFIG_OS_WINDOWS)
+ syshost = "windows";
+#elif defined(TB_CONFIG_OS_MACOSX)
+ syshost = "macosx";
+#elif defined(TB_CONFIG_OS_LINUX)
+ syshost = "linux";
+#elif defined(TB_CONFIG_OS_IOS)
+ syshost = "ios";
+#elif defined(TB_CONFIG_OS_ANDROID)
+ syshost = "android";
+#endif
+ lua_pushstring(machine->lua, syshost? syshost : "unknown");
+ lua_setglobal(machine->lua, "_SYSHOST");
+
+ // init host
+ tb_char_t const* host = syshost;
+#if defined(TB_CONFIG_OS_WINDOWS)
+# if defined(TB_COMPILER_ON_MSYS)
+ host = "msys";
+# elif defined(TB_COMPILER_ON_CYGWIN)
+ host = "cygwin";
+# else
+ {
+ tb_char_t data[64] = {0};
+ if (tb_environment_first("MSYSTEM", data, sizeof(data)))
+ {
+ // on msys or msys/mingw64 or msys/mingw32?
+ if (!tb_strnicmp(data, "mingw", 5) || !tb_stricmp(data, "msys"))
+ host = "msys";
+ }
+ }
+# endif
+#endif
+ lua_pushstring(machine->lua, host? host : "unknown");
+ lua_setglobal(machine->lua, "_HOST");
+}
static tb_void_t xm_machine_init_arch(xm_machine_t* machine)
{
// check
tb_assert_and_check_return(machine && machine->lua);
+ // init system architecture
+ tb_char_t const* sysarch = tb_null;
#if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX)
-
// the GetNativeSystemInfo function type
typedef void (WINAPI *GetNativeSystemInfo_t)(LPSYSTEM_INFO);
@@ -584,31 +628,76 @@ static tb_void_t xm_machine_init_arch(xm_machine_t* machine)
switch (systeminfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64:
- lua_pushstring(machine->lua, "x64");
+ sysarch = "x64";
break;
case PROCESSOR_ARCHITECTURE_ARM:
- lua_pushstring(machine->lua, "arm");
+ sysarch = "arm";
break;
case PROCESSOR_ARCHITECTURE_INTEL:
- lua_pushstring(machine->lua, "x86");
+ sysarch = "x86";
break;
default:
+ break;
+ }
+
+ // get arch from compiler
+ if (!sysarch)
+ {
# ifdef TB_ARCH_x64
- lua_pushstring(machine->lua, "x64");
+ sysarch = "x64";
# else
- lua_pushstring(machine->lua, "x86");
+ sysarch = "x86";
# endif
- break;
}
#elif defined(TB_ARCH_x64)
- lua_pushstring(machine->lua, "x86_64");
+ sysarch = "x86_64";
#elif defined(TB_ARCH_x86)
- lua_pushstring(machine->lua, "i386");
+ sysarch = "i386";
#else
- lua_pushstring(machine->lua, TB_ARCH_STRING);
+ sysarch = TB_ARCH_STRING;
#endif
+ lua_pushstring(machine->lua, sysarch);
+ lua_setglobal(machine->lua, "_SYSARCH");
+
+ // init architecture
+ tb_char_t const* arch = sysarch;
+#if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX)
+ // get architecture from msys environment
+ tb_char_t data[64] = {0};
+ if (tb_environment_first("MSYSTEM_CARCH", data, sizeof(data)))
+ arch = data;
+#endif
+ lua_pushstring(machine->lua, arch);
lua_setglobal(machine->lua, "_ARCH");
}
+static tb_void_t xm_machine_init_features(xm_machine_t* machine)
+{
+ // check
+ tb_assert_and_check_return(machine && machine->lua);
+
+ // init features
+ lua_newtable(machine->lua);
+
+ // get path seperator
+ lua_pushstring(machine->lua, "path_sep");
+#if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX)
+ lua_pushstring(machine->lua, "\\");
+#else
+ lua_pushstring(machine->lua, "/");
+#endif
+ lua_settable(machine->lua, -3);
+
+ // get environment path seperator
+ lua_pushstring(machine->lua, "path_envsep");
+#if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX)
+ lua_pushstring(machine->lua, ";");
+#else
+ lua_pushstring(machine->lua, ":");
+#endif
+ lua_settable(machine->lua, -3);
+
+ lua_setglobal(machine->lua, "_FEATURES");
+}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
@@ -672,30 +761,14 @@ xm_machine_ref_t xm_machine_init()
#endif
// init host
-#if defined(TB_CONFIG_OS_WINDOWS)
-# if defined(TB_COMPILER_ON_MSYS)
- lua_pushstring(machine->lua, "msys");
-# elif defined(TB_COMPILER_ON_CYGWIN)
- lua_pushstring(machine->lua, "cygwin");
-# else
- lua_pushstring(machine->lua, "windows");
-# endif
-#elif defined(TB_CONFIG_OS_MACOSX)
- lua_pushstring(machine->lua, "macosx");
-#elif defined(TB_CONFIG_OS_LINUX)
- lua_pushstring(machine->lua, "linux");
-#elif defined(TB_CONFIG_OS_IOS)
- lua_pushstring(machine->lua, "ios");
-#elif defined(TB_CONFIG_OS_ANDROID)
- lua_pushstring(machine->lua, "android");
-#else
- lua_pushstring(machine->lua, "unknown");
-#endif
- lua_setglobal(machine->lua, "_HOST");
+ xm_machine_init_host(machine);
// init architecture
xm_machine_init_arch(machine);
+ // init features
+ xm_machine_init_features(machine);
+
// get version
tb_version_t const* version = xm_version();
tb_assert_and_check_break(version);
diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua
index eb50d567d..5a553f507 100644
--- a/xmake/core/_xmake_main.lua
+++ b/xmake/core/_xmake_main.lua
@@ -23,6 +23,8 @@ xmake = xmake or {}
xmake._ARGV = _ARGV
xmake._HOST = _HOST
xmake._ARCH = _ARCH
+xmake._SYSHOST = _SYSHOST
+xmake._SYSARCH = _SYSARCH
xmake._VERSION = _VERSION
xmake._VERSION_SHORT = _VERSION_SHORT
xmake._PROGRAM_DIR = _PROGRAM_DIR
@@ -30,6 +32,7 @@ xmake._PROGRAM_FILE = _PROGRAM_FILE
xmake._PROJECT_DIR = _PROJECT_DIR
xmake._PROJECT_FILE = "xmake.lua"
xmake._WORKING_DIR = os.curdir()
+xmake._FEATURES = _FEATURES
function _loadfile_impl(filepath, mode, opt)
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 5c193a48d..05e12fa3a 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -800,16 +800,31 @@ function os.isexec(filepath)
return os.isfile(filepath)
end
--- get the system host
+-- get host of subsystem, maybe msys/cygwin on windows
function os.host()
return xmake._HOST
end
--- get the system architecture
+-- get host architecture of subsystem
function os.arch()
return xmake._ARCH
end
+-- get system host, msys/cygwin are always windows
+function os.syshost()
+ return xmake._SYSHOST
+end
+
+-- get system host architecture
+function os.sysarch()
+ return xmake._SYSARCH
+end
+
+-- get features
+function os.features()
+ return xmake._FEATURES
+end
+
-- the current host is belong to the given hosts?
function os.is_host(...)
@@ -883,7 +898,7 @@ function os.user_agent()
if os._USER_AGENT == nil then
-- init systems
- local systems = {macosx = "Macintosh", linux = "Linux", windows = "Windows"}
+ local systems = {macosx = "Macintosh", linux = "Linux", windows = "Windows", msys = "MSYS", cygwin = "Cygwin"}
-- os user agent
local os_user_agent = ""
@@ -892,7 +907,7 @@ function os.user_agent()
if ok then
os_user_agent = ("Intel Mac OS X " .. (osver or "")):trim()
end
- elseif os.host() == "linux" then
+ elseif os.host() == "linux" or os.host() == "msys" or os.host() == "cygwin" then
local ok, osarch = os.iorun("uname -m")
if ok then
os_user_agent = (os_user_agent .. " " .. (osarch or "")):trim()
diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua
index 8332925d5..d960509e1 100644
--- a/xmake/core/base/path.lua
+++ b/xmake/core/base/path.lua
@@ -22,14 +22,11 @@
local path = path or {}
-- load modules
-local string = require("base/string")
+local string = require("base/string")
-- get the directory of the path
function path.directory(p)
-
- -- check
assert(p)
-
local i = p:find_last("[/\\]")
if i then
if i > 1 then i = i - 1 end
@@ -41,10 +38,7 @@ end
-- get the filename of the path
function path.filename(p)
-
- -- check
assert(p)
-
local i = p:find_last("[/\\]")
if i then
return p:sub(i + 1)
@@ -55,10 +49,7 @@ end
-- get the basename of the path
function path.basename(p)
-
- -- check
assert(p)
-
local name = path.filename(p)
local i = name:find_last(".", true)
if i then
@@ -70,11 +61,7 @@ end
-- get the file extension of the path: .xxx
function path.extension(p)
-
- -- check
assert(p)
-
- -- get extension
local i = p:find_last(".", true)
if i then
return p:sub(i)
@@ -85,36 +72,37 @@ end
-- join path
function path.join(p, ...)
-
- -- check
assert(p)
-
- -- join them
for _, name in ipairs({...}) do
p = p .. "/" .. name
end
-
- -- translate path
return path.translate(p)
end
-- split path by the separator
function path.split(p)
-
- -- check
assert(p)
-
return p:split("[/\\]")
end
-- get the path seperator
function path.sep()
- return xmake._HOST == "windows" and '\\' or '/'
+ local sep = path._SEP
+ if not sep then
+ sep = xmake._FEATURES.path_sep
+ path._SEP = sep
+ end
+ return sep
end
-- get the path seperator of environment variable
function path.envsep()
- return xmake._HOST == "windows" and ';' or ':'
+ local envsep = path._ENVSEP
+ if not envsep then
+ envsep = xmake._FEATURES.path_envsep
+ path._ENVSEP = envsep
+ end
+ return envsep
end
-- split environment variable with `path.envsep()`,
@@ -125,7 +113,7 @@ function path.splitenv(env_path)
assert(env_path)
local result = {}
- if xmake._HOST == "windows" then
+ if xmake._SYSHOST == "windows" then
while #env_path > 0 do
if env_path:startswith(path.envsep()) then
env_path = env_path:sub(2)
@@ -168,7 +156,7 @@ function path.joinenv(env_table)
end
local envsep = path.envsep()
- if xmake._HOST == "windows" then
+ if xmake._SYSHOST == "windows" then
local tab = {}
for _, v in ipairs(env_table) do
if v ~= "" then
@@ -186,12 +174,9 @@ end
-- the last character is the path seperator?
function path.islastsep(p)
-
- -- check
assert(p)
-
local sep = p:sub(#p, #p)
- return xmake._HOST == "windows" and (sep == '\\' or sep == '/') or (sep == '/')
+ return xmake._SYSHOST == "windows" and (sep == '\\' or sep == '/') or (sep == '/')
end
-- convert path pattern to a lua pattern
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index f6ce8e6b4..782082a7e 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -35,6 +35,8 @@ local sandbox_os = sandbox_os or {}
-- inherit some builtin interfaces
sandbox_os.host = os.host
sandbox_os.arch = os.arch
+sandbox_os.syshost = os.syshost
+sandbox_os.sysarch = os.sysarch
sandbox_os.exit = os.exit
sandbox_os.date = os.date
sandbox_os.time = os.time
@@ -58,6 +60,7 @@ sandbox_os.pbcopy = os.pbcopy
sandbox_os.cpuinfo = os.cpuinfo
sandbox_os.emptydir = os.emptydir
sandbox_os.filesize = os.filesize
+sandbox_os.features = os.features
sandbox_os.workingdir = os.workingdir
sandbox_os.programdir = os.programdir
sandbox_os.programfile = os.programfile
diff --git a/xmake/platforms/cygwin/config.lua b/xmake/platforms/cygwin/config.lua
index 166d12274..96d9e81c4 100644
--- a/xmake/platforms/cygwin/config.lua
+++ b/xmake/platforms/cygwin/config.lua
@@ -34,15 +34,7 @@ function _check_arch()
if not arch then
-- init the default architecture
- local arch = config.get("cross") and "none" or os.arch()
- if is_host("windows") then
- if arch == "x64" then
- arch = "x86_64"
- elseif arch == "x86" then
- arch = "i386"
- end
- end
- config.set("arch", arch)
+ config.set("arch", config.get("cross") and "none" or os.arch())
-- trace
print("checking for the architecture ... %s", config.get("arch"))
diff --git a/xmake/platforms/msys/config.lua b/xmake/platforms/msys/config.lua
index b33fb885f..15abb0e5c 100644
--- a/xmake/platforms/msys/config.lua
+++ b/xmake/platforms/msys/config.lua
@@ -34,15 +34,7 @@ function _check_arch()
if not arch then
-- init the default architecture
- local arch = config.get("cross") and "none" or os.arch()
- if is_host("windows") then
- if arch == "x64" then
- arch = "x86_64"
- elseif arch == "x86" then
- arch = "i386"
- end
- end
- config.set("arch", arch)
+ config.set("arch", config.get("cross") and "none" or os.arch())
-- trace
print("checking for the architecture ... %s", config.get("arch"))