diff options
| author | ruki <[email protected]> | 2021-04-04 23:53:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-04-04 23:53:33 +0800 |
| commit | 74eab67a10e81e22c713f44582f39a1008393453 (patch) | |
| tree | 77130be1f66ed112ebb4f10a49b045535e8057b8 | |
| parent | afbbe0900bd48341084c11bcf4e0120782dff6e5 (diff) | |
add cpu.info
| -rw-r--r-- | xmake/core/base/cpu.lua | 81 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/cpu.lua | 24 |
2 files changed, 105 insertions, 0 deletions
diff --git a/xmake/core/base/cpu.lua b/xmake/core/base/cpu.lua new file mode 100644 index 000000000..e523d0eaa --- /dev/null +++ b/xmake/core/base/cpu.lua @@ -0,0 +1,81 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file cpu.lua +-- + +-- define module +local cpu = cpu or {} + +-- load modules +local os = require("base/os") +local winos = require("base/winos") +local io = require("base/io") + +-- get cpu info +function cpu.info() + local cpuinfo = cpu._CPUINFO + if cpuinfo == nil then + cpuinfo = {} + if os.host() == "macosx" then + local ok, sysctl_result = os.iorun("/usr/sbin/sysctl -n machdep.cpu.vendor machdep.cpu.model machdep.cpu.family machdep.cpu.features") + if ok and sysctl_result then + sysctl_result = sysctl_result:trim():split('\n', {plain = true}) + cpuinfo.vender_id = sysctl_result[1] + cpuinfo.cpu_model = sysctl_result[2] + cpuinfo.cpu_family = sysctl_result[3] + cpuinfo.cpu_flags = sysctl_result[4] + if cpuinfo.cpu_flags then + cpuinfo.cpu_flags = cpuinfo.cpu_flags:lower():gsub("%.", "_") + end + end + elseif os.host() == "linux" then + -- FIXME + -- local proc_cpuinfo = io.readfile("/proc/cpuinfo") + local ok, proc_cpuinfo = os.iorun("cat /proc/cpuinfo") + if ok and proc_cpuinfo then + for _, line in ipairs(proc_cpuinfo:split('\n', {plain = true})) do + if not cpuinfo.vender_id and line:startswith("vendor_id") then + cpuinfo.vendor_id = line:match("vendor_id%s+:%s+(.*)") + end + if not cpuinfo.cpu_model and line:startswith("model") then + cpuinfo.cpu_model = line:match("model%s+:%s+(.*)") + end + if not cpuinfo.cpu_family and line:startswith("cpu family") then + cpuinfo.cpu_family = line:match("cpu family%s+:%s+(.*)") + end + if not cpuinfo.cpu_flags and line:startswith("flags") then + cpuinfo.cpu_flags = line:match("flags%s+:%s+(.*)") + end + end + end + elseif os.host() == "windows" then + cpuinfo.vender_id = winos.registry_query("HKEY_LOCAL_MACHINE\\Hardware\\Description\\System\\CentralProcessor\\0;VendorIdentifier") + local cpu_id = winos.registry_query("HKEY_LOCAL_MACHINE\\Hardware\\Description\\System\\CentralProcessor\\0;Identifier") + if cpu_id then + local cpu_family, cpu_model = cpu_id:match("Family (%d+) Model (%d+)") + cpuinfo.cpu_family = cpu_family + cpuinfo.cpu_model = cpu_model + end + end + cpu._CPUINFO = cpuinfo + end + return cpuinfo +end + +-- return module +return cpu diff --git a/xmake/core/sandbox/modules/import/core/base/cpu.lua b/xmake/core/sandbox/modules/import/core/base/cpu.lua new file mode 100644 index 000000000..22940c97e --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/cpu.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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file cpu.lua +-- + +-- load modules +return require("base/cpu") + + |
