From ab36dca23f1cd80720e9d0eb2c3ce72d5895ba3e Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 May 2015 08:39:20 +0800 Subject: load xmake.xconf before loading project --- xmake/scripts/action/_config.lua | 48 ++++++++++ xmake/scripts/action/action.lua | 2 +- xmake/scripts/action/config.lua | 188 --------------------------------------- xmake/scripts/base/config.lua | 169 +++++++++++++++++++++++++++++++++++ xmake/scripts/base/main.lua | 26 ++---- xmake/scripts/base/project.lua | 38 ++++++++ 6 files changed, 263 insertions(+), 208 deletions(-) create mode 100644 xmake/scripts/action/_config.lua delete mode 100644 xmake/scripts/action/config.lua create mode 100644 xmake/scripts/base/config.lua diff --git a/xmake/scripts/action/_config.lua b/xmake/scripts/action/_config.lua new file mode 100644 index 000000000..a627c61d9 --- /dev/null +++ b/xmake/scripts/action/_config.lua @@ -0,0 +1,48 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see http://www.gnu.org/licenses/ +-- +-- Copyright (C) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file _config.lua +-- + +-- define module: _config +local _config = _config or {} + +-- load modules +local io = require("base/io") +local utils = require("base/utils") +local config = require("base/config") + +-- done the given config +function _config.done() + + -- dump configs + config.dump() + + -- save configs + if not config.savexconf() then + return false + end + + -- ok + print("configure ok!") + return true +end + +-- return module: _config +return _config diff --git a/xmake/scripts/action/action.lua b/xmake/scripts/action/action.lua index dbb00a5b2..eab768364 100644 --- a/xmake/scripts/action/action.lua +++ b/xmake/scripts/action/action.lua @@ -27,7 +27,7 @@ local action = action or {} function action.done(name) -- load the given action - local a = require("action/" .. name) + local a = require("action/_" .. name) if not a then return false end -- done the given action diff --git a/xmake/scripts/action/config.lua b/xmake/scripts/action/config.lua deleted file mode 100644 index 13b5ffac5..000000000 --- a/xmake/scripts/action/config.lua +++ /dev/null @@ -1,188 +0,0 @@ ---!The Automatic Cross-platform Build Tool --- --- XMake is free software; you can redistribute it and/or modify --- it under the terms of the GNU Lesser General Public License as published by --- the Free Software Foundation; either version 2.1 of the License, or --- (at your option) any later version. --- --- XMake is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU Lesser General Public License for more details. --- --- You should have received a copy of the GNU Lesser General Public License --- along with XMake; --- If not, see http://www.gnu.org/licenses/ --- --- Copyright (C) 2009 - 2015, ruki All rights reserved. --- --- @author ruki --- @file config.lua --- - --- define module: config -local config = config or {} - --- load modules -local io = require("base/io") -local utils = require("base/utils") - --- save configs -function config._save() - - -- the options - local options = xmake._OPTIONS - assert(options) - - -- open the configure file - local path = options.project .. "/xmake.xconf" - local file = io.open(path, "w") - if not file then - -- error - utils.error("open %s failed!", path) - return false - end - - -- save configs to file - if not io.save(file, xmake._CONFIGS, "return") then - -- error - utils.error("save %s failed!", path) - file:close() - return false - end - - -- close file - file:close() - - -- ok - return true -end - --- load configs -function config._load() - - -- the options - local options = xmake._OPTIONS - assert(options) - - -- the target - local target = options.target or options._DEFAULTS.target - assert(target) - - -- open the configure file - local path = options.project .. "/xmake.xconf" - local file = loadfile(path) - if file then - -- execute it - local ok, cfg = pcall(file) - if not ok then - -- error - utils.error("load %s failed!", path) - utils.error(cfg) - return - end - - -- check - assert(cfg and type(cfg) == "table") - - -- clear configs if the host environment has been changed - if cfg[target] and cfg[target].host ~= xmake._HOST then - cfg = {} - end - if cfg.all and cfg.all.host ~= xmake._HOST then - cfg = {} - end - - -- merges configs to xmake._CONFIGS - xmake._CONFIGS = cfg - end - - -- the configs - xmake._CONFIGS = xmake._CONFIGS or {} - local configs = xmake._CONFIGS - - -- init the configs for the target - configs[target] = configs[target] or {} - - -- merge xmake._OPTIONS to xmake._CONFIGS[target] - for k, v in pairs(options) do - - -- check - assert(type(k) == "string") - - -- skip some options - if not k:startswith("_") and k ~= "project" and k ~= "file" and k ~= "verbose" and k ~= "target" then - - -- save the option to the target - configs[target][k] = v - end - end - - -- merge xmake._OPTIONS._DEFAULTS to xmake._CONFIGS[target] - for k, v in pairs(options._DEFAULTS) do - - -- check - assert(type(k) == "string") - - -- skip some options - if k ~= "project" and k ~= "file" and k ~= "verbose" and k ~= "target" then - - -- save the default option to the target - if not configs[target][k] then - if v == "auto" then - configs[target][k] = config._auto(k) - else - configs[target][k] = v - end - end - end - end -end - --- auto configs -function config._auto(name) - - -- get platform or host - if name == "plat" or name == "host" then - return xmake._HOST - -- get architecture - elseif name == "arch" then - return xmake._ARCH - end - - -- unknown - utils.error("unknown config: %s", name) - return "unknown" -end - --- dump configs -function config._dump() - - -- dump - if xmake._OPTIONS.verbose then - utils.dump(xmake._CONFIGS, "configs = ") - end - -end - --- done the given config -function config.done() - - -- load configs - config._load() - - -- dump configs - config._dump() - - -- save configs - if not config._save() then - return false - end - - -- ok - print("configure ok!") - return true -end - --- return module: config -return config diff --git a/xmake/scripts/base/config.lua b/xmake/scripts/base/config.lua new file mode 100644 index 000000000..ae73b61d8 --- /dev/null +++ b/xmake/scripts/base/config.lua @@ -0,0 +1,169 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see http://www.gnu.org/licenses/ +-- +-- Copyright (C) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file config.lua +-- + +-- define module: config +local config = config or {} + +-- load modules +local io = require("base/io") +local utils = require("base/utils") + +-- auto configs +function config._auto(name) + + -- get platform or host + if name == "plat" or name == "host" then + return xmake._HOST + -- get architecture + elseif name == "arch" then + return xmake._ARCH + end + + -- unknown + utils.error("unknown config: %s", name) + return "unknown" +end + +-- save xmake.xconf +function config.savexconf() + + -- the options + local options = xmake._OPTIONS + assert(options) + + -- open the configure file + local path = options.project .. "/xmake.xconf" + local file = io.open(path, "w") + if not file then + -- error + utils.error("open %s failed!", path) + return false + end + + -- save configs to file + if not io.save(file, xmake._CONFIGS, "return") then + -- error + utils.error("save %s failed!", path) + file:close() + return false + end + + -- close file + file:close() + + -- ok + return true +end + +-- load xmake.xconf +function config.loadxconf() + + -- the options + local options = xmake._OPTIONS + assert(options) + + -- the target + local target = options.target or options._DEFAULTS.target + assert(target) + + -- open the configure file + local path = options.project .. "/xmake.xconf" + local file = loadfile(path) + if file then + -- execute it + local ok, cfg = pcall(file) + if not ok then + -- error + utils.error("load %s failed!", path) + utils.error(cfg) + return + end + + -- check + assert(cfg and type(cfg) == "table") + + -- clear configs if the host environment has been changed + if cfg[target] and cfg[target].host ~= xmake._HOST then + cfg = {} + end + if cfg.all and cfg.all.host ~= xmake._HOST then + cfg = {} + end + + -- merges configs to xmake._CONFIGS + xmake._CONFIGS = cfg + end + + -- the configs + xmake._CONFIGS = xmake._CONFIGS or {} + local configs = xmake._CONFIGS + + -- init the configs for the target + configs[target] = configs[target] or {} + + -- merge xmake._OPTIONS to xmake._CONFIGS[target] + for k, v in pairs(options) do + + -- check + assert(type(k) == "string") + + -- skip some options + if not k:startswith("_") and k ~= "project" and k ~= "file" and k ~= "verbose" and k ~= "target" then + + -- save the option to the target + configs[target][k] = v + end + end + + -- merge xmake._OPTIONS._DEFAULTS to xmake._CONFIGS[target] + for k, v in pairs(options._DEFAULTS) do + + -- check + assert(type(k) == "string") + + -- skip some options + if k ~= "project" and k ~= "file" and k ~= "verbose" and k ~= "target" then + + -- save the default option to the target + if not configs[target][k] then + if v == "auto" then + configs[target][k] = config._auto(k) + else + configs[target][k] = v + end + end + end + end +end + +-- dump configs +function config.dump() + + -- dump + if xmake._OPTIONS.verbose then + utils.dump(xmake._CONFIGS, "configs = ") + end + +end + +-- return module: config +return config diff --git a/xmake/scripts/base/main.lua b/xmake/scripts/base/main.lua index 04d3d9e52..3b9f3d57a 100644 --- a/xmake/scripts/base/main.lua +++ b/xmake/scripts/base/main.lua @@ -27,6 +27,7 @@ local main = main or {} local path = require("base/path") local utils = require("base/utils") local option = require("base/option") +local config = require("base/config") local project = require("base/project") local preprocessor = require("base/preprocessor") local action = require("action/action") @@ -276,24 +277,11 @@ function main._done_option() end assert(options.file) - -- load and execute the xmake.xproj - local errors = nil - local script = preprocessor.load_xproj(options.file) - if script then + -- load xmake.xconf file + config.loadxconf() - -- init the project envirnoment - setfenv(script, project) - - -- execute it - local ok, err = pcall(script) - if not ok then - -- error - errors = err - end - else - -- error - errors = string.format("load %s failed!", options.file) - end + -- load xmake.xproj file + local _, errors = project.loadxproj(options.file) -- done help if options.help then @@ -328,8 +316,8 @@ function main._done_option() -- save project xmake._PROJECT = project - -- dump project configs --- utils.dump(xmake._PROJECT._CONFIGS, "", "_PARENT") + -- dump project + project.dump() -- done action return action.done(options._ACTION or "build") diff --git a/xmake/scripts/base/project.lua b/xmake/scripts/base/project.lua index 5ed4229f0..f2cf1a9f0 100644 --- a/xmake/scripts/base/project.lua +++ b/xmake/scripts/base/project.lua @@ -155,6 +155,44 @@ function _register(names) end end +-- load xproj +function loadxproj(file) + + -- check + assert(file) + + -- load and execute the xmake.xproj + local script = preprocessor.load_xproj(file) + if script then + + -- init the project envirnoment + setfenv(script, _PROJECT) + + -- execute it + local ok, err = pcall(script) + if not ok then + -- error + return false, err + end + else + -- error + return false, string.format("load %s failed!", file) + end + + -- ok + return true +end + +-- dump all configures +function dump() + + -- dump + if xmake._OPTIONS.verbose then + utils.dump(xmake._PROJECT._CONFIGS, "", "_PARENT") + end + +end + -- register all configures _register { "kind" , "deps" -- cgit v1.3.1