summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-09 22:22:52 +0800
committerruki <[email protected]>2019-10-09 08:17:14 +0800
commit06e6a3b6aaf5465421f93e7a7158101fbea4cee9 (patch)
tree925582755ea768a4ceaff4155534c2e3bce47a75
parentb7d21719c405866e1ebc6597cba561c577850ae6 (diff)
add os.cpuinfo and improve the default build jobs number
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/cpuinfo.c58
-rw-r--r--xmake/actions/build/xmake.lua3
-rw-r--r--xmake/core/sandbox/modules/interpreter/math.lua24
-rw-r--r--xmake/core/sandbox/modules/interpreter/os.lua1
-rw-r--r--xmake/core/sandbox/modules/os.lua1
7 files changed, 89 insertions, 1 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index d9dc111c3..b98e60236 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -78,6 +78,7 @@ tb_int_t xm_os_exists(lua_State* lua);
tb_int_t xm_os_setenv(lua_State* lua);
tb_int_t xm_os_getenv(lua_State* lua);
tb_int_t xm_os_getenvs(lua_State* lua);
+tb_int_t xm_os_cpuinfo(lua_State* lua);
tb_int_t xm_os_readlink(lua_State* lua);
tb_int_t xm_os_filesize(lua_State* lua);
tb_int_t xm_os_emptydir(lua_State* lua);
@@ -193,6 +194,7 @@ static luaL_Reg const g_os_functions[] =
, { "setenv", xm_os_setenv }
, { "getenv", xm_os_getenv }
, { "getenvs", xm_os_getenvs }
+, { "cpuinfo", xm_os_cpuinfo }
, { "readlink", xm_os_readlink }
, { "emptydir", xm_os_emptydir }
, { "strerror", xm_os_strerror }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index ae9ebbbe4..996123191 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -36,6 +36,7 @@ xmake_C_FILES += \
os/setenv \
os/getenv \
os/getenvs \
+ os/cpuinfo \
os/readlink \
os/emptydir \
os/strerror \
diff --git a/core/src/xmake/os/cpuinfo.c b/core/src/xmake/os/cpuinfo.c
new file mode 100644
index 000000000..7d9dce5f0
--- /dev/null
+++ b/core/src/xmake/os/cpuinfo.c
@@ -0,0 +1,58 @@
+/*!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 - 2019, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file cpuinfo.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "cpuinfo"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* local cpuinfo = os.cpuinfo()
+ * {
+ * ncpu = 4,
+ * ...
+ * }
+ */
+tb_int_t xm_os_cpuinfo(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // init table
+ lua_newtable(lua);
+
+ // get cpu number
+ tb_int_t ncpu = (tb_int_t)tb_cpu_count();
+ lua_pushstring(lua, "ncpu");
+ lua_pushinteger(lua, ncpu > 0? ncpu : 1);
+ lua_settable(lua, -3);
+
+ return 1;
+}
diff --git a/xmake/actions/build/xmake.lua b/xmake/actions/build/xmake.lua
index 510cf3c71..cc057c5a0 100644
--- a/xmake/actions/build/xmake.lua
+++ b/xmake/actions/build/xmake.lua
@@ -43,7 +43,8 @@ task("build")
, {'a', "all", "k", nil, "Build all targets." }
, {}
- , {'j', "jobs", "kv", "4", "Specifies the number of jobs to build simultaneously." }
+ , {'j', "jobs", "kv", tostring(math.ceil(os.cpuinfo().ncpu * 3 / 2)),
+ "Specifies the number of jobs to build simultaneously." }
, {'w', "warning", "k", false, "Enable the warnings output." }
, {'t', "try", "k", false, "Try building project using third-party buildsystem." }
, {nil, "files", "kv", nil, "Build the given source files.",
diff --git a/xmake/core/sandbox/modules/interpreter/math.lua b/xmake/core/sandbox/modules/interpreter/math.lua
new file mode 100644
index 000000000..a048f9f34
--- /dev/null
+++ b/xmake/core/sandbox/modules/interpreter/math.lua
@@ -0,0 +1,24 @@
+--!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 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file math.lua
+--
+
+-- load module
+return require("sandbox/modules/math")
+
+
diff --git a/xmake/core/sandbox/modules/interpreter/os.lua b/xmake/core/sandbox/modules/interpreter/os.lua
index 7dbaf0eee..62879e566 100644
--- a/xmake/core/sandbox/modules/interpreter/os.lua
+++ b/xmake/core/sandbox/modules/interpreter/os.lua
@@ -39,6 +39,7 @@ sandbox_os.isfile = os.isfile
sandbox_os.exists = os.exists
sandbox_os.curdir = os.curdir
sandbox_os.tmpdir = os.tmpdir
+sandbox_os.cpuinfo = os.cpuinfo
sandbox_os.filesize = os.filesize
sandbox_os.programdir = os.programdir
sandbox_os.programfile = os.programfile
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 348649177..90c5b3e04 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -55,6 +55,7 @@ sandbox_os.addenvp = os.addenvp
sandbox_os.getenvs = os.getenvs
sandbox_os.pbpaste = os.pbpaste
sandbox_os.pbcopy = os.pbcopy
+sandbox_os.cpuinfo = os.cpuinfo
sandbox_os.emptydir = os.emptydir
sandbox_os.filesize = os.filesize
sandbox_os.workingdir = os.workingdir