From 3f53e1ea0f6c9deff5295e689df141feba9cce4c Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 May 2015 12:45:41 +0800 Subject: make target and dependence for makefile --- xmake/scripts/base/makefile.lua | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'xmake/scripts/base/makefile.lua') diff --git a/xmake/scripts/base/makefile.lua b/xmake/scripts/base/makefile.lua index 5f71538c2..754131fa4 100644 --- a/xmake/scripts/base/makefile.lua +++ b/xmake/scripts/base/makefile.lua @@ -23,5 +23,117 @@ -- define module: makefile local makefile = makefile or {} +-- load modules +local io = require("base/io") +local os = require("base/os") +local utils = require("base/utils") +local config = require("base/config") +local project = require("base/project") + +-- make the given target to the makefile +function makefile._make_target(file, name, target) + + -- check + assert(file and name and target) + + -- make head + file:write(string.format("%s:", name)) + + -- make dependence for target + if target.deps then + local deps = utils.wrap(target.deps) + for _, dep in ipairs(deps) do + file:write(" " .. dep) + end + end + + -- make dependence end + file:write("\n") + + -- make body + file:write(string.format("\techo \"%s\"\n", name)) + + -- make end + file:write("\n") + + -- ok + return true +end + +-- make all targets to the makefile +function makefile._make_targets(file) + + -- check + assert(file and project and project._CONFIGS) + + -- get all project targets + local targets = project._CONFIGS._TARGET + if not targets then + -- error + utils.error("not found target in this project!") + return false + end + + -- make all first + local all = "" + for name, _ in pairs(targets) do + -- append the target name to all + all = all .. " " .. name + end + file:write(string.format("all: %s\n\n", all)) + + -- make it for all targets + for name, target in pairs(targets) do + -- make target + if not makefile._make_target(file, name, target) then + -- error + utils.error("failed to make target %s to makefile!", name) + return false + end + + -- append the target name to all + all = all .. " " .. name + end + + -- ok + return true +end + +-- make makefile in the build directory +function makefile.make() + + -- the configs + local configs = config._CONFIGS + assert(configs and configs.buildir) + + -- make the build directory + if not os.isdir(configs.buildir) then + assert(os.mkdir(configs.buildir)) + end + + -- open the makefile + local path = configs.buildir .. "/makefile" + local file = io.open(path, "w") + if not file then + -- error + utils.error("open %s failed!", path) + return false + end + + -- make all targets to the makefile + if not makefile._make_targets(file) then + -- error + utils.error("save %s failed!", path) + file:close() + return false + end + + -- close the makefile + file:close() + + -- ok + return true +end + -- return module: makefile return makefile -- cgit v1.3.1