diff options
| author | ruki <[email protected]> | 2016-03-17 11:17:11 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-03-17 11:17:11 +0800 |
| commit | 2b233ade0aba7cb061d7b576357a462910c4b62f (patch) | |
| tree | e81f198ef7b2e84ecb736dbaa3fe9057308b0187 | |
| parent | 713413a6f962704fdb5078a38b32c8637eca3d7a (diff) | |
make config.h
| -rwxr-xr-x | xmake/actions/config/config_h.lua | 158 | ||||
| -rwxr-xr-x | xmake/actions/config/main.lua | 6 | ||||
| -rw-r--r-- | xmake/core/base/io.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 21 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 177 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/project.lua | 11 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/io.lua | 19 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 4 |
8 files changed, 232 insertions, 166 deletions
diff --git a/xmake/actions/config/config_h.lua b/xmake/actions/config/config_h.lua new file mode 100755 index 000000000..1cf3d8c38 --- /dev/null +++ b/xmake/actions/config/config_h.lua @@ -0,0 +1,158 @@ +--!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 <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file config_h.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") + +-- make configure for the given target name +function _make_for_target(files, targetname, target) + + -- get the target configure file + local config_h = target.config_h + if not config_h then return end + + -- the prefix + local prefix = target.config_h_prefix or (targetname:upper() .. "_CONFIG") + + -- open the file + local file = files[config_h] or io.open(config_h, "w") + + -- make the head + if files[config_h] then file:write("\n") end + file:write(format("#ifndef %s_H\n", prefix)) + file:write(format("#define %s_H\n", prefix)) + file:write("\n") + + -- make version + if target.version then + file:write("// version\n") + file:write(format("#define %s_VERSION \"%s\"\n", prefix, target.version)) + local i = 1 + local m = {"MAJOR", "MINOR", "ALTER"} + for v in target.version:gmatch("%d+") do + file:write(format("#define %s_VERSION_%s %s\n", prefix, m[i], v)) + i = i + 1 + if i > 3 then break end + end + file:write(format("#define %s_VERSION_BUILD %s\n", prefix, os.date("%Y%m%d%H%M", os.time()))) + file:write("\n") + end + + -- make the defines + local defines = table.copy(target.defines_h) + + -- make the undefines + local undefines = table.copy(target.undefines_h) + + -- make the options + for _, name in ipairs(utils.wrap(target.options)) do + + -- get option if be enabled + local opt = nil + if config.get(name) then opt = config.get("__" .. name) end + if nil ~= opt then + + -- get the option defines + table.join2(defines, opt.defines_h_if_ok) + + -- get the option undefines + table.join2(undefines, opt.undefines_h_if_ok) + + end + end + + -- make the defines + if #defines ~= 0 then + file:write("// defines\n") + for _, define in ipairs(defines) do + file:write(format("#define %s 1\n", define:gsub("=", " "):gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end))) + end + file:write("\n") + end + + -- make the undefines + if #undefines ~= 0 then + file:write("// undefines\n") + for _, undefine in ipairs(undefines) do + file:write(format("#undef %s\n", undefine:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end))) + end + file:write("\n") + end + + -- make the tail + file:write("#endif\n") + + -- cache the file + files[config_h] = file + +end + +-- make the configure file for the given target and dependents +function _make_for_target_with_deps(files, targetname) + + -- the target + local target = project.target(targetname) + + -- make configure for the target + _make_for_target(files, targetname, target) + + -- make configure for the dependent targets? + for _, dep in ipairs(utils.wrap(target.deps)) do + _make_for_target_with_deps(files, dep) + end + +end + +-- make the config.h +function make() + + -- the target name + local targetname = option.get("target") + + -- enter project directory + os.cd(project.directory()) + + -- init files + local files = {} + + -- make configure for the given target name + if targetname and targetname ~= "all" then + _make_for_target_with_deps(files, targetname) + else + + -- make configure for all targets + for name, target in pairs(project.targets()) do + _make_for_target(files, name, target) + end + end + + -- exit files + for _, file in pairs(files) do + file:close() + end + + -- leave project directory + os.cd("-") + +end diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua index b09720be1..610e39293 100755 --- a/xmake/actions/config/main.lua +++ b/xmake/actions/config/main.lua @@ -25,6 +25,7 @@ import("core.base.option") import("core.project.config") import("core.project.global") import("core.project.project") +import("config_h") -- filter option function _option_filter(name) @@ -87,13 +88,16 @@ function main() project.load() -- check target - if nil == project.target(targetname) then + if targetname and targetname ~= "all" and nil == project.target(targetname) then raise("unknown target: %s", targetname) end -- save the project configure config.save() + -- make the config.h + config_h.make() + -- TODO -- dump it diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua index 71c6cc5b8..26c9163e2 100644 --- a/xmake/core/base/io.lua +++ b/xmake/core/base/io.lua @@ -159,7 +159,7 @@ function io.open(filepath, mode) end -- open it - return io._open(filepath, mode) + return io._open(path.translate(filepath), mode) end -- save object the the given filepath diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index f26678c09..363f6efe0 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -80,10 +80,29 @@ function table.clear(self) end -- copy the table to self +function table.copy(copied) + + -- init it + copied = copied or {} + + -- copy it + local result = {} + for k, v in pairs(copied) do + result[k] = v + end + + -- ok + return result +end + +-- copy the table to self function table.copy2(self, copied) -- check - assert(self and copied) + assert(self) + + -- init it + copied = copied or {} -- clear self first table.clear(self) diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index c8f0a0fa2..231bdee3d 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -476,136 +476,6 @@ function project._interpreter() return interp end --- make configure for the given target_name -function project._makeconf_for_target(target_name, target) - - -- check - assert(target_name and target) - - -- get the target configure file - local config_h = target.config_h - if not config_h then - return true - end - - -- translate file path - if not path.is_absolute(config_h) then - config_h = path.absolute(config_h, xmake._PROJECT_DIR) - else - config_h = path.translate(config_h) - end - - -- the prefix - local prefix = target.config_h_prefix or (target_name:upper() .. "_CONFIG") - - -- open the file - local file = project._CONFILES[config_h] or io.open(config_h, "w") - assert(file) - - -- make the head - if project._CONFILES[config_h] then file:write("\n") end - file:write(string.format("#ifndef %s_H\n", prefix)) - file:write(string.format("#define %s_H\n", prefix)) - file:write("\n") - - -- make version - if target.version then - file:write("// version\n") - file:write(string.format("#define %s_VERSION \"%s\"\n", prefix, target.version)) - local i = 1 - local m = {"MAJOR", "MINOR", "ALTER"} - for v in target.version:gmatch("%d+") do - file:write(string.format("#define %s_VERSION_%s %s\n", prefix, m[i], v)) - i = i + 1 - if i > 3 then break end - end - file:write(string.format("#define %s_VERSION_BUILD %s\n", prefix, os.date("%Y%m%d%H%M", os.time()))) - file:write("\n") - end - - -- make the defines - local defines = {} - if target.defines_h then table.join2(defines, target.defines_h) end - - -- make the undefines - local undefines = {} - if target.undefines_h then table.join2(undefines, target.undefines_h) end - - -- the options - if target.options then - for _, name in ipairs(utils.wrap(target.options)) do - - -- get option if be enabled - local opt = nil - if config.get(name) then opt = config.get("__" .. name) end - if nil ~= opt then - - -- get the option defines - if opt.defines_h_if_ok then table.join2(defines, opt.defines_h_if_ok) end - - -- get the option undefines - if opt.undefines_h_if_ok then table.join2(undefines, opt.undefines_h_if_ok) end - - end - end - end - - -- make the defines - if #defines ~= 0 then - file:write("// defines\n") - for _, define in ipairs(defines) do - file:write(string.format("#define %s 1\n", define:gsub("=", " "):gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end))) - end - file:write("\n") - end - - -- make the undefines - if #undefines ~= 0 then - file:write("// undefines\n") - for _, undefine in ipairs(undefines) do - file:write(string.format("#undef %s\n", undefine:gsub("%$%((.-)%)", function (w) if w == "prefix" then return prefix end end))) - end - file:write("\n") - end - - -- make the tail - file:write("#endif\n") - - -- cache the file - project._CONFILES[config_h] = file - - -- ok - return true -end - --- make the configure file for the given target and dependents -function project._makeconf_for_target_and_deps(target_name) - - -- the targets - local targets = project.targets() - assert(targets) - - -- the target - local target = targets[target_name] - assert(target) - - -- make configure for the target - if not project._makeconf_for_target(target_name, target) then - return false - end - - -- exists the dependent targets? - if target.deps then - local deps = utils.wrap(target.deps) - for _, dep in ipairs(deps) do - if not project._makeconf_for_target_and_deps(dep) then return false end - end - end - - -- ok - return true -end - -- make option for checking links function project._make_option_for_checking_links(opt, links, cfile, objectfile, targetfile) @@ -896,7 +766,7 @@ function project.probe() -- enter the project directory local ok, errors = os.cd(xmake._PROJECT_DIR) if not ok then - return failse, errors + return false, errors end -- load the options from the the project file @@ -908,6 +778,12 @@ function project.probe() -- make the options from the the project file project._make_options(options) + -- leave the project directory + ok, errors = os.cd("-") + if not ok then + return false, errors + end + -- ok return true end @@ -922,7 +798,7 @@ function project.load() -- enter the project directory local ok, errors = os.cd(xmake._PROJECT_DIR) if not ok then - return failse, errors + return false, errors end -- load targets @@ -931,6 +807,12 @@ function project.load() return false, errors end + -- leave the project directory + ok, errors = os.cd("-") + if not ok then + return false, errors + end + -- save targets project._TARGETS = targets @@ -948,37 +830,6 @@ function project.dump() end --- make the configure file for the given target -function project.makeconf(target_name) - - -- init files - project._CONFILES = {} - - -- the target name - if target_name and target_name ~= "all" then - -- make configure for the target and dependents - if not project._makeconf_for_target_and_deps(target_name) then return false end - else - - -- the targets - local targets = project.targets() - assert(targets) - - -- make configure for the targets - for target_name, target in pairs(targets) do - if not project._makeconf_for_target(target_name, target) then return false end - end - end - - -- exit files - for _, file in pairs(project._CONFILES) do - file:close() - end - - -- ok - return true -end - -- get the project menu function project.menu() diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index 69c1d0216..0e527df38 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -55,6 +55,17 @@ function sandbox_core_project_project.target(targetname) return project.target(targetname) end +-- get the all targets +function sandbox_core_project_project.targets() + + -- get targets + local targets = project.targets() + assert(targets) + + -- ok + return targets +end + -- get the project directory function sandbox_core_project_project.directory() diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua index 5c7d0fc27..ad10d8a95 100644 --- a/xmake/core/sandbox/modules/io.lua +++ b/xmake/core/sandbox/modules/io.lua @@ -48,6 +48,25 @@ function sandbox_io.gsub(filepath, pattern, replace) return data, count end +-- open file +function sandbox_io.open(filepath, mode) + + -- check + assert(filepath) + + -- format it first + filepath = vformat(filepath) + + -- open it + local file, errors = io.open(filepath, mode) + if not file then + raise(errors) + end + + -- ok? + return file +end + -- load object from the given file function sandbox_io.load(filepath) diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index c4903b2c0..1fedef80e 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -29,6 +29,10 @@ local vformat = require("sandbox/modules/vformat") -- define module local sandbox_os = sandbox_os or {} +-- inherit some builtin interfaces +sandbox_os.date = os.date +sandbox_os.time = os.time + -- copy file or directory function sandbox_os.cp(src, dst) |
