summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-12-16 23:43:28 +0800
committerruki <[email protected]>2020-12-16 23:43:28 +0800
commit207b4e753a4bed1f75836b78180411442103a9cd (patch)
tree5e4abe6e4f3c5b99ef6f1ebcbbd1a3d7d88795f5
parentf3b3357ddad20367ff94e8b984a525a703d5947f (diff)
add find_platform
-rw-r--r--xmake/modules/private/detect/find_platform.lua146
-rw-r--r--xmake/platforms/android/xmake.lua10
-rw-r--r--xmake/platforms/bsd/xmake.lua10
-rw-r--r--xmake/platforms/cross/xmake.lua48
-rw-r--r--xmake/platforms/cygwin/xmake.lua10
-rw-r--r--xmake/platforms/iphoneos/xmake.lua11
-rw-r--r--xmake/platforms/linux/xmake.lua10
-rw-r--r--xmake/platforms/macosx/xmake.lua10
-rw-r--r--xmake/platforms/mingw/xmake.lua19
-rw-r--r--xmake/platforms/msys/xmake.lua10
-rw-r--r--xmake/platforms/wasm/xmake.lua10
-rw-r--r--xmake/platforms/watchos/xmake.lua10
-rw-r--r--xmake/platforms/windows/xmake.lua10
13 files changed, 146 insertions, 168 deletions
diff --git a/xmake/modules/private/detect/find_platform.lua b/xmake/modules/private/detect/find_platform.lua
new file mode 100644
index 000000000..a18849b82
--- /dev/null
+++ b/xmake/modules/private/detect/find_platform.lua
@@ -0,0 +1,146 @@
+--!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-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_platform.lua
+--
+
+-- imports
+import("core.project.config")
+import("detect.sdks.find_cross_toolchain")
+
+-- find platform
+function _find_plat(plat)
+ plat = plat or config.get("plat")
+ if not plat then
+ plat = os.subhost()
+ if plat == "msys" then
+ local msystem = os.getenv("MSYSTEM")
+ if msystem and msystem:lower():find("mingw", 1, true) then
+ plat = "mingw"
+ end
+ end
+ end
+ return plat
+end
+
+-- find architecture for cross platform
+function _find_arch_from_cross()
+ local cross = config.get("cross")
+ if not cross then
+ local cross_toolchain = find_cross_toolchain(config.get("sdk"), {bindir = config.get("bin")})
+ if cross_toolchain then
+ cross = cross_toolchain.cross
+ end
+ end
+ local arch = "none"
+ if cross then
+ if cross:find("aarch64", 1, true) then
+ arch = "arm64"
+ elseif cross:find("arm", 1, true) then
+ arch = "arm"
+ elseif cross:find("mips64", 1, true) then
+ arch = "mips64"
+ elseif cross:find("mips", 1, true) then
+ arch = "mips"
+ elseif cross:find("riscv64", 1, true) then
+ arch = "riscv64"
+ elseif cross:find("riscv", 1, true) then
+ arch = "riscv"
+ elseif cross:find("s390x", 1, true) then
+ arch = "s390x"
+ elseif cross:find("powerpc64", 1, true) then
+ arch = "ppc64"
+ elseif cross:find("powerpc", 1, true) then
+ arch = "ppc"
+ elseif cross:find("sh4", 1, true) then
+ arch = "sh4"
+ elseif cross:find("x86_64", 1, true) then
+ arch = "x86_64"
+ elseif cross:find("i386", 1, true) or cross:find("i686", 1, true) then
+ arch = "i386"
+ end
+ end
+ return arch
+end
+
+-- find architecture
+function _find_arch(plat, arch)
+ arch = arch or config.get("arch")
+ if not arch then
+ if plat == "android" then
+ arch = "armeabi-v7a"
+ elseif plat == "iphoneos" then
+ arch = "arm64"
+ elseif plat == "watchos" then
+ arch = "armv7k"
+ elseif plat == "wasm" then
+ arch = "wasm32"
+ elseif plat == "mingw" then
+ local mingw_chost = nil
+ if is_subhost("msys") then
+ mingw_chost = os.getenv("MINGW_CHOST")
+ end
+ if mingw_chost == "i686-w64-mingw32" then
+ arch = "i386"
+ else
+ arch = "x86_64"
+ end
+ elseif plat == "cross" then
+ arch = _find_arch_from_cross()
+ else
+ arch = os.subarch()
+ end
+ end
+ return arch
+end
+
+-- find default platform and architecture
+--
+-- @param opt the argument options, e.g. {plat = "", arch = "", global = true}
+--
+-- @return {plat = "", arch = ""}
+--
+-- @code
+--
+-- find the default platform:
+-- local result = find_platform()
+--
+-- find the default architecture from the given platform:
+-- local result = find_platform({plat = "iphoneos"})
+--
+-- @endcode
+--
+function main(opt)
+
+ -- find plat and arch
+ opt = opt or {}
+ local plat = _find_plat(opt.plat)
+ local arch = _find_arch(plat, opt.arch)
+
+ -- save the local configuration if be global
+ if opt.global then
+ if not opt.plat and not config.get("plat") then
+ config.set("plat", plat)
+ cprint("checking for platform ... ${color.success}%s", plat)
+ end
+ if not opt.arch and not config.get("arch") then
+ config.set("arch", arch)
+ cprint("checking for architecture ... ${color.success}%s", plat)
+ end
+ end
+ return {plat = plat, arch = arch}
+end
diff --git a/xmake/platforms/android/xmake.lua b/xmake/platforms/android/xmake.lua
index b68df1072..d36c91ba7 100644
--- a/xmake/platforms/android/xmake.lua
+++ b/xmake/platforms/android/xmake.lua
@@ -42,16 +42,6 @@ platform("android")
set_formats("shared", "lib$(name).so")
set_formats("symbol", "$(name).sym")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", "armeabi-v7a")
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "ndk", "rust")
diff --git a/xmake/platforms/bsd/xmake.lua b/xmake/platforms/bsd/xmake.lua
index 64dacbb3e..f3b7b8044 100644
--- a/xmake/platforms/bsd/xmake.lua
+++ b/xmake/platforms/bsd/xmake.lua
@@ -39,16 +39,6 @@ platform("bsd")
-- set install directory
set_installdir("/usr/local")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", os.arch())
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran", "zig")
diff --git a/xmake/platforms/cross/xmake.lua b/xmake/platforms/cross/xmake.lua
index a7180396a..fb055977a 100644
--- a/xmake/platforms/cross/xmake.lua
+++ b/xmake/platforms/cross/xmake.lua
@@ -33,54 +33,6 @@ platform("cross")
set_formats("shared", "lib$(name).so")
set_formats("symbol", "$(name).sym")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- import("detect.sdks.find_cross_toolchain")
-
- -- detect arch
- local arch = config.get("arch")
- if not arch then
- local cross = config.get("cross")
- if not cross then
- local cross_toolchain = find_cross_toolchain(config.get("sdk"), {bindir = config.get("bin")})
- if cross_toolchain then
- cross = cross_toolchain.cross
- end
- end
- arch = "none"
- if cross then
- if cross:find("aarch64", 1, true) then
- arch = "arm64"
- elseif cross:find("arm", 1, true) then
- arch = "arm"
- elseif cross:find("mips64", 1, true) then
- arch = "mips64"
- elseif cross:find("mips", 1, true) then
- arch = "mips"
- elseif cross:find("riscv64", 1, true) then
- arch = "riscv64"
- elseif cross:find("riscv", 1, true) then
- arch = "riscv"
- elseif cross:find("s390x", 1, true) then
- arch = "s390x"
- elseif cross:find("powerpc64", 1, true) then
- arch = "ppc64"
- elseif cross:find("powerpc", 1, true) then
- arch = "ppc"
- elseif cross:find("sh4", 1, true) then
- arch = "sh4"
- elseif cross:find("x86_64", 1, true) then
- arch = "x86_64"
- elseif cross:find("i386", 1, true) or cross:find("i686", 1, true) then
- arch = "i386"
- end
- end
- config.set("arch", arch)
- cprint("checking for architecture ... ${color.success}%s", arch)
- end
- end)
-
-- set toolchains
set_toolchains("envs", "cross")
diff --git a/xmake/platforms/cygwin/xmake.lua b/xmake/platforms/cygwin/xmake.lua
index 0161ae58c..f2b8722c6 100644
--- a/xmake/platforms/cygwin/xmake.lua
+++ b/xmake/platforms/cygwin/xmake.lua
@@ -40,16 +40,6 @@ platform("cygwin")
-- set install directory
set_installdir("/usr/local")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", os.subarch())
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "cross", "gcc", "clang", "yasm", "gfortran")
diff --git a/xmake/platforms/iphoneos/xmake.lua b/xmake/platforms/iphoneos/xmake.lua
index af6eb8e18..55c4eeec9 100644
--- a/xmake/platforms/iphoneos/xmake.lua
+++ b/xmake/platforms/iphoneos/xmake.lua
@@ -36,17 +36,6 @@ platform("iphoneos")
set_formats("shared", "lib$(name).dylib")
set_formats("symbol", "$(name).dSYM")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", "arm64")
- platform:arch_set("arm64")
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "xcode")
diff --git a/xmake/platforms/linux/xmake.lua b/xmake/platforms/linux/xmake.lua
index 1f70ed35e..40f92e0d6 100644
--- a/xmake/platforms/linux/xmake.lua
+++ b/xmake/platforms/linux/xmake.lua
@@ -39,16 +39,6 @@ platform("linux")
-- set install directory
set_installdir("/usr/local")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", os.arch())
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "cross", "gcc", "clang", "yasm", "nasm", "fasm", "cuda", "dlang", "go", "rust", "gfortran", "zig")
diff --git a/xmake/platforms/macosx/xmake.lua b/xmake/platforms/macosx/xmake.lua
index ebda335c2..e41a3c9da 100644
--- a/xmake/platforms/macosx/xmake.lua
+++ b/xmake/platforms/macosx/xmake.lua
@@ -39,16 +39,6 @@ platform("macosx")
-- set install directory
set_installdir("/usr/local")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", os.arch())
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "xcode", "clang", "gcc", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig")
diff --git a/xmake/platforms/mingw/xmake.lua b/xmake/platforms/mingw/xmake.lua
index 80c6467e7..0d3320807 100644
--- a/xmake/platforms/mingw/xmake.lua
+++ b/xmake/platforms/mingw/xmake.lua
@@ -37,25 +37,6 @@ platform("mingw")
set_formats("binary", "$(name).exe")
set_formats("symbol", "$(name).pdb")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- local mingw_chost = nil
- if is_subhost("msys") then
- mingw_chost = os.getenv("MINGW_CHOST")
- end
- if mingw_chost == "i686-w64-mingw32" then
- arch = "i386"
- else
- arch = "x86_64"
- end
- config.set("arch", arch)
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "mingw", "yasm", "nasm", "fasm", "go")
diff --git a/xmake/platforms/msys/xmake.lua b/xmake/platforms/msys/xmake.lua
index 86901dc08..9cf119bd5 100644
--- a/xmake/platforms/msys/xmake.lua
+++ b/xmake/platforms/msys/xmake.lua
@@ -40,16 +40,6 @@ platform("msys")
-- set install directory
set_installdir("/usr/local")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", os.subarch())
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "cross", "gcc", "clang", "yasm", "go", "gfortran")
diff --git a/xmake/platforms/wasm/xmake.lua b/xmake/platforms/wasm/xmake.lua
index 4b3fb02ac..4676b75fe 100644
--- a/xmake/platforms/wasm/xmake.lua
+++ b/xmake/platforms/wasm/xmake.lua
@@ -37,16 +37,6 @@ platform("wasm")
set_formats("binary", "$(name).html")
set_formats("symbol", "$(name).sym")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", "wasm32")
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("emcc")
diff --git a/xmake/platforms/watchos/xmake.lua b/xmake/platforms/watchos/xmake.lua
index b3652e21b..357b7aa12 100644
--- a/xmake/platforms/watchos/xmake.lua
+++ b/xmake/platforms/watchos/xmake.lua
@@ -36,16 +36,6 @@ platform("watchos")
set_formats("shared", "lib$(name).dylib")
set_formats("symbol", "$(name).dSYM")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", "armv7k")
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("envs", "xcode")
diff --git a/xmake/platforms/windows/xmake.lua b/xmake/platforms/windows/xmake.lua
index 813990f84..141f270ce 100644
--- a/xmake/platforms/windows/xmake.lua
+++ b/xmake/platforms/windows/xmake.lua
@@ -37,16 +37,6 @@ platform("windows")
set_formats("binary", "$(name).exe")
set_formats("symbol", "$(name).pdb")
- -- on check
- on_check(function (platform)
- import("core.project.config")
- local arch = config.get("arch")
- if not arch then
- config.set("arch", os.arch())
- cprint("checking for architecture ... ${color.success}%s", config.get("arch"))
- end
- end)
-
-- set toolchains
set_toolchains("msvc", "clang", "yasm", "nasm", "cuda", "dlang", "rust", "go", "gfortran", "zig", "tinyc")