diff options
| author | TitanSnow <[email protected]> | 2017-05-25 08:07:12 +0800 |
|---|---|---|
| committer | TitanSnow <[email protected]> | 2017-05-25 08:12:46 +0800 |
| commit | b91fc2f5a31c9a7aefbc5585a60b14d6cfa614fa (patch) | |
| tree | e4b206979a8e86f35e4c05b213acde2db949cf4e | |
| parent | 4fafe79dd79efe67c3ed92362152a8aa883788c7 (diff) | |
| parent | ea123cda41560f549861eca8222a4153cb72bbd6 (diff) | |
Merge branch 'dev' into installtest
24 files changed, 473 insertions, 212 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index f6818d3be..5bea2d01e 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -206,7 +206,7 @@ static luaL_Reg const g_readline_functions[] = /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_bool_t xm_machine_main_save_arguments(xm_machine_impl_t* impl, tb_int_t argc, tb_char_t** argv) +static tb_bool_t xm_machine_save_arguments(xm_machine_impl_t* impl, tb_int_t argc, tb_char_t** argv) { // check tb_assert_and_check_return_val(impl && impl->lua && argc >= 1 && argv, tb_false); @@ -229,7 +229,7 @@ static tb_bool_t xm_machine_main_save_arguments(xm_machine_impl_t* impl, tb_int_ // ok return tb_true; } -static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn) +static tb_size_t xm_machine_get_program_file(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(impl && path && maxn, tb_false); @@ -255,6 +255,9 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, // end data[size] = '\0'; + // trace + tb_trace_d("programfile: %s", data); + // set _EXECUTABLE_PATH lua_pushstring(impl -> lua, data); lua_setglobal(impl -> lua, "_EXECUTABLE_PATH"); @@ -291,6 +294,9 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, tb_uint32_t size = (tb_uint32_t)maxn; if (!_NSGetExecutablePath(data, &size)) { + // trace + tb_trace_d("programfile: %s", data); + // set _EXECUTABLE_PATH lua_pushstring(impl -> lua, data); lua_setglobal(impl -> lua, "_EXECUTABLE_PATH"); @@ -325,6 +331,9 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, // end data[size] = '\0'; + // trace + tb_trace_d("programfile: %s", data); + // set _EXECUTABLE_PATH lua_pushstring(impl -> lua, data); lua_setglobal(impl -> lua, "_EXECUTABLE_PATH"); @@ -356,7 +365,7 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, if (ok) { // trace - tb_trace_d("program: %s", path); + tb_trace_d("programdir: %s", path); // save the directory to the global variable: _PROGRAM_DIR lua_pushstring(impl->lua, path); @@ -366,7 +375,7 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, // ok? return ok; } -static tb_bool_t xm_machine_main_get_project_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn) +static tb_bool_t xm_machine_get_project_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn) { // check tb_assert_and_check_return_val(impl && path && maxn, tb_false); @@ -534,14 +543,14 @@ tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** ar tb_assert_and_check_return_val(impl && impl->lua, -1); // save main arguments to the global variable: _ARGV - if (!xm_machine_main_save_arguments(impl, argc, argv)) return -1; + if (!xm_machine_save_arguments(impl, argc, argv)) return -1; // get the project directory tb_char_t path[TB_PATH_MAXN] = {0}; - if (!xm_machine_main_get_project_directory(impl, path, sizeof(path))) return -1; + if (!xm_machine_get_project_directory(impl, path, sizeof(path))) return -1; - // get the program directory - if (!xm_machine_main_get_program_directory(impl, path, sizeof(path))) return -1; + // get the program file & directory + if (!xm_machine_get_program_file(impl, path, sizeof(path))) return -1; // append the main script path tb_strcat(path, "/core/_xmake_main.lua"); diff --git a/core/xmake.lua b/core/xmake.lua index b8fdb4da6..581f91224 100644 --- a/core/xmake.lua +++ b/core/xmake.lua @@ -15,7 +15,6 @@ set_languages("c99", "cxx11") -- disable some compiler errors add_cxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing") -add_mxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing") -- add defines add_defines("_GNU_SOURCE=1", "_FILE_OFFSET_BITS=64", "_LARGEFILE_SOURCE") @@ -28,7 +27,6 @@ set_strip("all") -- fomit the frame pointer add_cxflags("-fomit-frame-pointer") -add_mxflags("-fomit-frame-pointer") -- for the windows platform (msvc) if is_plat("windows") then diff --git a/tests/build.lua b/tests/build.lua index 6a2ccb339..667b29e99 100644 --- a/tests/build.lua +++ b/tests/build.lua @@ -1,3 +1,6 @@ +-- imports +import("privilege.sudo") + -- main entry function main(argv) @@ -14,7 +17,6 @@ function main(argv) os.exec("xmake f -m release") os.exec("xmake -r -a -v --backtrace") os.exec("xmake f --mode=debug --verbose --backtrace") - os.exec("xmake --rebuild --all --verbose --backtrace") os.exec("xmake p --verbose --backtrace") os.exec("xmake c --verbose --backtrace") os.exec("xmake m -e buildtest") @@ -22,14 +24,14 @@ function main(argv) os.exec("xmake f --cc=gcc --cxx=g++") os.exec("xmake m buildtest") if os.host() ~= "windows" then - os.exec("sudo xmake install") - os.exec("sudo xmake uninstall") + sudo.exec("xmake install") + sudo.exec("xmake uninstall") end os.exec("xmake f --cc=clang --cxx=clang++ --ld=clang++ --verbose --backtrace") os.exec("xmake m buildtest") if os.host() ~= "windows" then - os.exec("sudo xmake install --all -v --backtrace") - os.exec("sudo xmake uninstall -v --backtrace") + sudo.exec("xmake install --all -v --backtrace") + sudo.exec("xmake uninstall -v --backtrace") end os.exec("xmake m -d buildtest") diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua index c1e1d9c3d..b9bd6ca79 100644 --- a/xmake/actions/install/install.lua +++ b/xmake/actions/install/install.lua @@ -74,7 +74,7 @@ function _install_target_and_deps(target) end -- install targets -function install(targetname) +function main(targetname) -- init finished states _g.finished = {} diff --git a/xmake/actions/install/install_admin.lua b/xmake/actions/install/install_admin.lua index 386911027..f641628ea 100644 --- a/xmake/actions/install/install_admin.lua +++ b/xmake/actions/install/install_admin.lua @@ -53,7 +53,7 @@ function main(targetname, installdir) end -- install target - install.install(targetname) + install(targetname) -- restore the previous option context option.restore() diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua index ae98f3eea..976478a76 100644 --- a/xmake/actions/install/main.lua +++ b/xmake/actions/install/main.lua @@ -27,6 +27,7 @@ import("core.base.option") import("core.project.task") import("core.platform.platform") import("core.base.privilege") +import("privilege.sudo") import("install") -- main @@ -47,7 +48,7 @@ function main() function () -- install target - install.install(targetname or ifelse(option.get("all"), "__all", "__def")) + install(targetname or ifelse(option.get("all"), "__all", "__def")) -- trace cprint("${bright}install ok!${clear}${ok_hand}") @@ -65,7 +66,7 @@ function main() function () -- install target - install.install(targetname or ifelse(option.get("all"), "__all", "__def")) + install(targetname or ifelse(option.get("all"), "__all", "__def")) -- trace cprint("${bright}install ok!${clear}${ok_hand}") @@ -86,7 +87,7 @@ function main() cprint("${bright red}error: ${default red}installation failed, may permission denied!") -- continue to install with administrator permission? - if os.feature("sudo") then + if sudo.has() then -- show tips cprint("${bright yellow}note: ${default yellow}try continue to install with administrator permission again?") @@ -98,7 +99,7 @@ function main() if answer == 'y' or answer == '' then -- install target with administrator permission - os.sudol(os.runv, path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), option.get("installdir")}) + sudo.runl(path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), option.get("installdir")}) -- trace cprint("${bright}install ok!${clear}${ok_hand}") diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua index a0460dbe2..36b113181 100644 --- a/xmake/actions/uninstall/main.lua +++ b/xmake/actions/uninstall/main.lua @@ -27,6 +27,7 @@ import("core.base.option") import("core.project.task") import("core.platform.platform") import("core.base.privilege") +import("privilege.sudo") import("uninstall") -- main @@ -47,7 +48,7 @@ function main() function () -- uninstall target - uninstall.uninstall(targetname) + uninstall(targetname) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") @@ -65,7 +66,7 @@ function main() function () -- uninstall target - uninstall.uninstall(targetname) + uninstall(targetname) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") @@ -86,7 +87,7 @@ function main() cprint("${bright red}error: ${default red}failed to uninstall, may permission denied!") -- continue to uninstall with administrator permission? - if os.feature("sudo") then + if sudo.has() then -- show tips cprint("${bright yellow}note: ${default yellow}try continue to uninstall with administrator permission again?") @@ -98,7 +99,7 @@ function main() if answer == 'y' or answer == '' then -- uninstall target with administrator permission - os.sudol(os.runv, path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", option.get("installdir")}) + sudo.runl(path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", option.get("installdir")}) -- trace cprint("${bright}uninstall ok!${clear}${ok_hand}") diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua index 6ab3d6cc1..a3a8b869f 100644 --- a/xmake/actions/uninstall/uninstall.lua +++ b/xmake/actions/uninstall/uninstall.lua @@ -74,7 +74,7 @@ function _uninstall_target_and_deps(target) end -- uninstall -function uninstall(targetname) +function main(targetname) -- init finished states _g.finished = {} diff --git a/xmake/actions/uninstall/uninstall_admin.lua b/xmake/actions/uninstall/uninstall_admin.lua index 8dded1cce..891b4a857 100644 --- a/xmake/actions/uninstall/uninstall_admin.lua +++ b/xmake/actions/uninstall/uninstall_admin.lua @@ -53,7 +53,7 @@ function main(targetname, installdir) end -- uninstall target - uninstall.uninstall(ifelse(targetname ~= "__all", targetname, nil)) + uninstall(ifelse(targetname ~= "__all", targetname, nil)) -- restore the previous option context option.restore() diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua index c454406c0..9ce5fdb42 100644 --- a/xmake/core/_xmake_main.lua +++ b/xmake/core/_xmake_main.lua @@ -31,6 +31,7 @@ xmake._NULDEV = _NULDEV xmake._VERSION = _VERSION xmake._VERSION_SHORT = _VERSION_SHORT xmake._PROGRAM_DIR = _PROGRAM_DIR +xmake._PROGRAM_FILE = _EXECUTABLE_PATH xmake._PROJECT_DIR = _PROJECT_DIR xmake._CORE_DIR = _PROGRAM_DIR .. "/core" xmake._TOOLS_DIR = _PROGRAM_DIR .. "/tools" diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 95ebfa79f..5c7ca78e2 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -680,6 +680,20 @@ function os.addenv(name, ...) end +-- get the program directory +function os.programdir() + + -- get it + return xmake._PROGRAM_DIR +end + +-- get the program file +function os.programfile() + + -- get it + return xmake._PROGRAM_FILE +end + -- get version info function os.versioninfo() diff --git a/xmake/core/base/privilege.lua b/xmake/core/base/privilege.lua index 96426cbf3..c6b7bb0c2 100644 --- a/xmake/core/base/privilege.lua +++ b/xmake/core/base/privilege.lua @@ -36,16 +36,26 @@ function privilege.store() return false end - -- find projectdir's owner - local projectdir = xmake._PROJECT_DIR - assert(projectdir) - local owner = os.getown(projectdir) - if not owner then - - -- fallback to current dir - owner = os.getown(os.curdir()) + local owner = {} + -- sudo will set SUDO_UID & SUDO_GID + -- so that can easily get original uid & gid + local sudo_uid = os.getenv("SUDO_UID") + local sudo_gid = os.getenv("SUDO_GID") + if sudo_uid and sudo_gid then + owner.uid = sudo_uid + owner.gid = sudo_gid + else + -- find projectdir's owner + local projectdir = xmake._PROJECT_DIR + assert(projectdir) + owner = os.getown(projectdir) if not owner then - return false + + -- fallback to current dir + owner = os.getown(os.curdir()) + if not owner then + return false + end end end diff --git a/xmake/core/main.lua b/xmake/core/main.lua index cda338294..62800d2be 100644 --- a/xmake/core/main.lua +++ b/xmake/core/main.lua @@ -126,6 +126,14 @@ function main._init() break end end + + -- add the directory of the program file (xmake) to $PATH environment + local programfile = os.programfile() + if programfile and os.isfile(programfile) then + os.addenv("PATH", path.directory(programfile)) + else + os.addenv("PATH", os.programdir()) + end end -- the main function diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index e68f399ca..191e53871 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -319,8 +319,7 @@ function project._interpreter() , configdir = config.directory() , projectdir = project.directory() , packagedir = package.directory() - , programdir = xmake._PROGRAM_DIR - , xmake = xmake._EXECUTABLE_PATH or path.join(xmake._PROGRAM_DIR, "xmake") + , programdir = os.programdir() } -- map it diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua index 732a33b34..a162d84e4 100644 --- a/xmake/core/project/task.lua +++ b/xmake/core/project/task.lua @@ -203,8 +203,7 @@ function task._interpreter() , configdir = config.directory() , projectdir = xmake._PROJECT_DIR , packagedir = package.directory() - , programdir = xmake._PROGRAM_DIR - , xmake = xmake._EXECUTABLE_PATH or path.join(xmake._PROGRAM_DIR, "xmake") + , programdir = os.programdir() } -- map it diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 083181c00..4b31128d2 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -34,18 +34,20 @@ local vformat = require("sandbox/modules/vformat") local sandbox_os = sandbox_os or {} -- inherit some builtin interfaces -sandbox_os.host = os.host -sandbox_os.arch = os.arch -sandbox_os.exit = os.exit -sandbox_os.date = os.date -sandbox_os.time = os.time -sandbox_os.argv = os.argv -sandbox_os.argw = os.argw -sandbox_os.mtime = os.mtime -sandbox_os.sleep = os.sleep -sandbox_os.mclock = os.mclock -sandbox_os.nuldev = os.nuldev -sandbox_os.emptydir = os.emptydir +sandbox_os.host = os.host +sandbox_os.arch = os.arch +sandbox_os.exit = os.exit +sandbox_os.date = os.date +sandbox_os.time = os.time +sandbox_os.argv = os.argv +sandbox_os.argw = os.argw +sandbox_os.mtime = os.mtime +sandbox_os.sleep = os.sleep +sandbox_os.mclock = os.mclock +sandbox_os.nuldev = os.nuldev +sandbox_os.emptydir = os.emptydir +sandbox_os.programdir = os.programdir +sandbox_os.programfile = os.programfile -- copy file or directory function sandbox_os.cp(...) @@ -202,13 +204,6 @@ function sandbox_os.toolsdir() return xmake._TOOLS_DIR end --- get the program directory -function sandbox_os.programdir() - - -- get it - return xmake._PROGRAM_DIR -end - -- get the script directory function sandbox_os.scriptdir() @@ -464,79 +459,6 @@ function sandbox_os.uuid(name) return uuid end --- get the feature of os -function sandbox_os.feature(name) - - -- has 'sudo' feature? - if name == "sudo" then - return sandbox_os.host() ~= "windows" - end -end - --- sudo run shell with administrator permission --- --- .e.g --- os.sudo(os.run, "echo", "hello xmake!") --- -function sandbox_os.sudo(runner, cmd, ...) - - -- check - assert(sandbox_os.feature("sudo"), "no sudo!") - - -- run it with administrator permission - if sandbox_os.host() == "windows" then - - -- TODO wrap sudo.bat - runner(cmd, ...) - else - -- run it with administrator permission and preserve parent environment - runner("sudo -E " .. cmd, ...) - end -end - --- sudo run shell with administrator permission and arguments list --- --- .e.g --- os.sudov(os.runv, {"echo", "hello xmake!"}) --- -function sandbox_os.sudov(runner, shellname, argv) - - -- check - assert(sandbox_os.feature("sudo"), "no sudo!") - - -- run it with administrator permission - if sandbox_os.host() == "windows" then - - -- TODO wrap sudo.bat - runner(shellname, argv) - else - -- run it with administrator permission and preserve parent environment - runner("sudo", table.join("-E", shellname, argv)) - end -end - --- sudo run lua script with administrator permission and arguments list --- --- .e.g --- os.sudol(os.runv, "xxx.lua", {"arg1", "arg2"}) --- -function sandbox_os.sudol(runner, luafile, luaargv) - - -- init argv - local argv = {"lua", "--root"} - for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do - local value = option.get(name) - if type(value) == "string" then - table.insert(argv, "--" .. name .. "=" .. value) - elseif value then - table.insert(argv, "--" .. name) - end - end - - -- run it with administrator permission - sandbox_os.sudov(runner, path.join(sandbox_os.programdir(), "xmake"), table.join(argv, luafile, luaargv)) -end - -- get versioninfo function sandbox_os.versioninfo() return os.versioninfo() diff --git a/xmake/modules/detect/sdk/find_ndk_sdkvers.lua b/xmake/modules/detect/sdk/find_ndk_sdkvers.lua new file mode 100644 index 000000000..16ad5eca7 --- /dev/null +++ b/xmake/modules/detect/sdk/find_ndk_sdkvers.lua @@ -0,0 +1,62 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_ndk_sdkvers.lua +-- + +-- find ndk sdk versions +-- +-- @param ndk_dir the ndk directory +-- +-- @return the ndk sdk version array +-- +-- @code +-- +-- local ndk_sdkvers = find_ndk_sdkvers("/xxx/android-ndk-r10e") +-- +-- @endcode +-- +function main(ndk_dir) + + -- get ndk directory + if not ndk_dir or not os.isdir(ndk_dir) then + return {} + end + + -- find all sdk directories + local ndk_sdkvers = {} + for _, sdkdir in ipairs(os.dirs(path.join(ndk_dir, "platforms/android-*"))) do + + -- get version + local filename = path.filename(sdkdir) + local version, count = filename:gsub("android%-", "") + if count > 0 then + + -- get the max version + if tonumber(version) > 0 then + table.insert(ndk_sdkvers, version) + end + end + end + + -- ok? + return ndk_sdkvers +end diff --git a/xmake/modules/detect/sdk/find_ndk_toolchains.lua b/xmake/modules/detect/sdk/find_ndk_toolchains.lua new file mode 100644 index 000000000..f7d5b7430 --- /dev/null +++ b/xmake/modules/detect/sdk/find_ndk_toolchains.lua @@ -0,0 +1,73 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_ndk_toolchains.lua +-- + +-- imports +import("core.project.config") + +-- find ndk toolchains +-- +-- @param ndk_dir the ndk directory +-- @param opt the argument options +-- .e.g {arch = "[armv5te|armv6|armv7-a|armv8-a|arm64-v8a]"} +-- +-- @return the toolchains array. .e.g {{bin = .., cross = ..}, .. } +-- +-- @code +-- +-- local ndk_toolchains = find_ndk_toolchains("/xxx/android-ndk-r10e") +-- local ndk_toolchains = find_ndk_toolchains("/xxx/android-ndk-r10e", {arch = "arm64-v8a"}) +-- +-- @endcode +-- +function main(ndk_dir, opt) + + -- init arguments + opt = opt or {} + + -- get ndk directory + if not ndk_dir or not os.isdir(ndk_dir) then + return {} + end + + -- get arch + local arch = opt.arch or config.get("arch") or "armv7-a" + + -- is arm64? + local arm64 = arch and arch:startswith("arm64") + + -- the cross + local cross = ifelse(arm64, "aarch64-linux-android-", "arm-linux-androideabi-") + + -- save the toolchains directory + local ndk_toolchains = {} + for _, bindir in ipairs(os.dirs(path.join(ndk_dir, "toolchains", cross .. "**", "prebuilt/*/bin"))) do + local binfiles = os.files(path.join(bindir, cross .. "*")) + if binfiles and #binfiles > 0 then + table.insert(ndk_toolchains, {bin = bindir, cross = cross}) + end + end + + -- ok? + return ndk_toolchains +end diff --git a/xmake/modules/detect/sdk/find_xcode_sdkver.lua b/xmake/modules/detect/sdk/find_xcode_sdkvers.lua index 075379eaf..fb479208c 100644 --- a/xmake/modules/detect/sdk/find_xcode_sdkver.lua +++ b/xmake/modules/detect/sdk/find_xcode_sdkvers.lua @@ -19,7 +19,7 @@ -- Copyright (C) 2015 - 2017, TBOOX Open Source Group. -- -- @author ruki --- @file find_xcode_sdkver.lua +-- @file find_xcode_sdkvers.lua -- -- imports @@ -35,9 +35,9 @@ import("detect.sdk.find_xcode_dir") -- -- @code -- --- local xcode_sdkvers = find_xcode_sdkver() --- local xcode_sdkvers = find_xcode_sdkver({xcode_dir = ""}) --- local xcode_sdkvers = find_xcode_sdkver({xcode_dir = "", plat = "iphoneos", arch = "arm64"}) +-- local xcode_sdkvers = find_xcode_sdkvers() +-- local xcode_sdkvers = find_xcode_sdkvers({xcode_dir = ""}) +-- local xcode_sdkvers = find_xcode_sdkvers({xcode_dir = "", plat = "iphoneos", arch = "arm64"}) -- -- @endcode -- diff --git a/xmake/modules/detect/tool/find_sudo.lua b/xmake/modules/detect/tool/find_sudo.lua new file mode 100644 index 000000000..968746020 --- /dev/null +++ b/xmake/modules/detect/tool/find_sudo.lua @@ -0,0 +1,55 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_sudo.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find sudo +-- +-- @param opt the argument options, .e.g {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local sudo = find_sudo() +-- local sudo, version = find_sudo({version = true}) +-- +-- @endcode +-- +function main(opt) + + -- find program + local program = find_program("sudo", { "/usr/bin", "/usr/local/bin"}, "-h") + + -- find program version + local version = nil + if program and opt and opt.version then + version = find_programver(program, "-V") + end + + -- ok? + return program, version +end diff --git a/xmake/modules/privilege/sudo.lua b/xmake/modules/privilege/sudo.lua new file mode 100644 index 000000000..ae35ee23a --- /dev/null +++ b/xmake/modules/privilege/sudo.lua @@ -0,0 +1,144 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you 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 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file sudo.lua +-- + +-- imports +import("core.base.option") +import("detect.tool.find_sudo") + +-- sudo run shell with administrator permission +-- +-- .e.g +-- _sudo(os.run, "echo", "hello xmake!") +-- +function _sudo(runner, cmd, ...) + + -- find sudo + local program = find_sudo() + assert(program, "sudo not found!") + + -- run it with administrator permission and preserve parent environment + runner(program .. " PATH=" .. os.getenv("PATH") .. " " .. cmd, ...) +end + +-- sudo run shell with administrator permission and arguments list +-- +-- .e.g +-- _sudov(os.runv, {"echo", "hello xmake!"}) +-- +function _sudov(runner, shellname, argv) + + -- find sudo + local program = find_sudo() + assert(program, "sudo not found!") + + -- run it with administrator permission and preserve parent environment + runner(program, table.join("PATH=" .. os.getenv("PATH"), shellname, argv)) +end + +-- sudo run lua script with administrator permission and arguments list +-- +-- .e.g +-- _lua(os.runv, "xxx.lua", {"arg1", "arg2"}) +-- +function _lua(runner, luafile, luaargv) + + -- init argv + local argv = {"lua", "--root"} + for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do + local value = option.get(name) + if type(value) == "string" then + table.insert(argv, "--" .. name .. "=" .. value) + elseif value then + table.insert(argv, "--" .. name) + end + end + + -- run it with administrator permission + _sudov(runner, "xmake", table.join(argv, luafile, luaargv)) +end + +-- has sudo? +function has() + return find_sudo() ~= nil +end + +-- sudo run shell +function run(cmd, ...) + return _sudo(os.run, cmd, ...) +end + +-- sudo run shell with arguments list +function runv(shellname, argv) + return _sudo(os.run, shellname, argv) +end + +-- sudo quietly run shell and echo verbose info if [-v|--verbose] option is enabled +function vrun(cmd, ...) + return _sudo(os.vrun, cmd, ...) +end + +-- sudo quietly run shell with arguments list and echo verbose info if [-v|--verbose] option is enabled +function vrunv(shellname, argv) + return _sudo(os.vrunv, shellname, argv) +end + +-- sudo run shell and return output and error data +function iorun(cmd, ...) + return _sudo(os.iorun, cmd, ...) +end + +-- sudo run shell and return output and error data +function iorunv(shellname, argv) + return _sudo(os.iorunv, shellname, argv) +end + +-- sudo execute shell +function exec(cmd, ...) + return _sudo(os.exec, cmd, ...) +end + +-- sudo execute shell with arguments list +function execv(shellname, argv) + return _sudo(os.execv, shellname, argv) +end + +-- sudo run lua script +function runl(luafile, luaargv) + return _lua(os.runv, luafile, luaargv) +end + +-- sudo quietly run lua script and echo verbose info if [-v|--verbose] option is enabled +function vrunl(luafile, luaargv) + return _lua(os.vrunv, luafile, luaargv) +end + +-- sudo run lua script and return output and error data +function iorunl(luafile, luaargv) + return _lua(os.iorunv, luafile, luaargv) +end + +-- sudo execute lua script +function execl(luafile, luaargv) + return _lua(os.execv, luafile, luaargv) +end diff --git a/xmake/platforms/android/check.lua b/xmake/platforms/android/check.lua index 51bac4dcd..cb8b15e21 100644 --- a/xmake/platforms/android/check.lua +++ b/xmake/platforms/android/check.lua @@ -23,8 +23,9 @@ -- -- imports -import("core.tool.tool") -import("platforms.checker", {rootdir = os.programdir()}) +import(".checker") +import("detect.sdk.find_ndk_sdkvers") +import("detect.sdk.find_ndk_toolchains") -- check the sdk version for ndk function _check_ndk_sdkver(config) @@ -33,31 +34,20 @@ function _check_ndk_sdkver(config) local ndk_sdkver = config.get("ndk_sdkver") if not ndk_sdkver then - -- get the ndk - local ndk = config.get("ndk") - if ndk then + -- find the max version + local sdkver_max = 0 + for _, sdkver in ipairs(find_ndk_sdkvers(config.get("ndk"))) do - -- match all sdk directories - local version_maxn = 0 - for _, sdkdir in ipairs(os.match(ndk .. "/platforms/android-*", true)) do - - -- get version - local filename = path.filename(sdkdir) - local version, count = filename:gsub("android%-", "") - if count > 0 then - - -- get the max version - version = tonumber(version) - if version > version_maxn then - version_maxn = version - end - end + -- get the max version + sdkver = tonumber(sdkver) + if sdkver > sdkver_max then + sdkver_max = sdkver end - - -- save the version - if version_maxn > 0 then ndk_sdkver = version_maxn end end + -- save the version + if sdkver_max > 0 then ndk_sdkver = sdkver_max end + -- probe ok? update it if ndk_sdkver ~= nil and ndk_sdkver > 0 then @@ -74,58 +64,36 @@ function _check_ndk_sdkver(config) end end --- check toolchains directory -function _check_toolchains_dir(config) +-- check toolchains +function _check_toolchains(config) -- get toolchains directory - local toolchains_dir = config.get("toolchains") - if not toolchains_dir then - - -- get architecture - local arch = config.get("arch") + local toolchains = config.get("toolchains") + if not toolchains then - -- get ndk - local ndk = config.get("ndk") - if ndk then - - -- match all toolchains - if arch and arch:startswith("arm64") then - toolchains_dir = os.match("%s/toolchains/aarch64-linux-android-**/prebuilt/*/bin/aarch64-linux-android-*", false, ndk) - else - toolchains_dir = os.match("%s/toolchains/arm-linux-androideabi-**/prebuilt/*/bin/arm-linux-androideabi-*", false, ndk) - end - - -- save the toolchains directory - for _, filepath in ipairs(toolchains_dir) do - config.set("toolchains", path.directory(filepath)) - break - end + -- find first toolchains + for _, toolchains in ipairs(find_ndk_toolchains(config.get("ndk"), config.get("arch"))) do + config.set("toolchains", toolchains.bin) + config.set("cross", toolchains.cross) + break end end -end - --- check toolchains version -function _check_toolchains_ver(config) + toolchains = config.get("toolchains") -- get toolchains version local toolchains_ver = config.get("toolchains_ver") - if not toolchains_ver then + if not toolchains_ver and toolchains then + local toolchains_ver = toolchains:match("%-(%d*%.%d*)[/\\]") + if toolchains_ver then - -- get toolchains directory - local toolchains_dir = config.get("toolchains") - if toolchains_dir then - local pos, _, toolchains_ver = toolchains_dir:find("%-(%d*%.%d*)[/\\]") - if pos and toolchains_ver then - - -- save the toolchains version - config.set("toolchains_ver", toolchains_ver) - - -- trace - cprint("checking for the version of toolchains ... ${green}%s", toolchains_ver) - else - -- trace - cprint("checking for the version of toolchains ... ${red}no") - end + -- save the toolchains version + config.set("toolchains_ver", toolchains_ver) + + -- trace + cprint("checking for the version of toolchains ... ${green}%s", toolchains_ver) + else + -- trace + cprint("checking for the version of toolchains ... ${red}no") end end end @@ -142,10 +110,7 @@ function _toolchains(config) local arch = config.get("arch") -- get cross - local cross = "arm-linux-androideabi-" - if arch and arch:startswith("arm64") then - cross = "aarch64-linux-android-" - end + local cross = config.get("cross") -- init toolchains local toolchains = {} @@ -187,8 +152,7 @@ function main(kind, toolkind) { { checker.check_arch, "armv7-a" } , _check_ndk_sdkver - , _check_toolchains_dir - , _check_toolchains_ver + , _check_toolchains , { checker.toolchain_check, "sh", _toolchains } , { checker.toolchain_check, "ld", _toolchains } } diff --git a/xmake/platforms/checker.lua b/xmake/platforms/checker.lua index 20465276e..870b51053 100644 --- a/xmake/platforms/checker.lua +++ b/xmake/platforms/checker.lua @@ -26,7 +26,7 @@ import("core.tool.tool") import("core.base.option") import("detect.sdk.find_xcode_dir") -import("detect.sdk.find_xcode_sdkver") +import("detect.sdk.find_xcode_sdkvers") -- find the given tool function _toolchain_check(config, toolkind, toolinfo) @@ -187,7 +187,7 @@ function check_xcode_sdkver(config) if not xcode_sdkver then -- check ok? update it - xcode_sdkver = find_xcode_sdkver({xcode_dir = config.get("xcode_dir"), plat = config.get("plat"), arch = config.get("arch")})[1] + xcode_sdkver = find_xcode_sdkvers({xcode_dir = config.get("xcode_dir"), plat = config.get("plat"), arch = config.get("arch")})[1] if xcode_sdkver then -- save it diff --git a/xmake/platforms/macosx/check.lua b/xmake/platforms/macosx/check.lua index 6aec3fa2d..e09776969 100644 --- a/xmake/platforms/macosx/check.lua +++ b/xmake/platforms/macosx/check.lua @@ -23,8 +23,7 @@ -- -- imports -import("core.tool.tool") -import("platforms.checker", {rootdir = os.programdir()}) +import(".checker") -- get toolchains function _toolchains(config) |
