summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-20 23:23:53 +0800
committerruki <[email protected]>2021-02-20 23:23:53 +0800
commitb038b2b86af516d3d49d7cc3fd6226ad23944564 (patch)
tree56069e73787194656518d10ae3a7c042d274f454
parent84ae35cd1b165ea51d0b5997a1ca23edb17f58b2 (diff)
improve lex and yacc rules
-rw-r--r--xmake/rules/lex_yacc/lex/xmake.lua89
-rw-r--r--xmake/rules/lex_yacc/yacc/xmake.lua92
-rw-r--r--xmake/rules/protobuf/proto.lua6
-rw-r--r--xmake/rules/protobuf/xmake.lua9
4 files changed, 30 insertions, 166 deletions
diff --git a/xmake/rules/lex_yacc/lex/xmake.lua b/xmake/rules/lex_yacc/lex/xmake.lua
index 0e28a2ad7..cbdb766e4 100644
--- a/xmake/rules/lex_yacc/lex/xmake.lua
+++ b/xmake/rules/lex_yacc/lex/xmake.lua
@@ -20,93 +20,32 @@
-- define rule: lex
rule("lex")
-
- -- set extension
set_extensions(".l", ".ll")
-
- -- load lex/flex
- before_load(function (target)
- import("core.project.config")
- import("lib.detect.find_tool")
- local lex = config.get("__lex")
- if not lex then
- lex = find_tool("flex") or find_tool("lex")
- if lex and lex.program then
- config.set("__lex", lex.program)
- cprint("checking for Lex ... ${color.success}%s", lex.program)
- else
- cprint("checking for Lex ... ${color.nothing}${text.nothing}")
- raise("lex/flex not found!")
- end
- end
- end)
-
- -- build lex file
- on_build_file(function (target, sourcefile_lex, opt)
+ on_buildcmd_file(function (target, batchcmds, sourcefile_lex, opt)
-- imports
- import("core.base.option")
- import("core.theme.theme")
- import("core.project.config")
- import("core.project.depend")
- import("core.tool.compiler")
- import("private.utils.progress")
+ import("lib.detect.find_tool")
-- get lex
- local lex = assert(config.get("__lex"), "lex not found!")
-
- -- get extension: .l/.ll
- local extension = path.extension(sourcefile_lex)
+ local lex = assert(find_tool("flex") or find_tool("lex"), "lex not found!")
-- get c/c++ source file for lex
+ local extension = path.extension(sourcefile_lex)
local sourcefile_cx = path.join(target:autogendir(), "rules", "lex_yacc", path.basename(sourcefile_lex) .. (extension == ".ll" and ".cpp" or ".c"))
- local sourcefile_dir = path.directory(sourcefile_cx)
-
- -- get object file
- local objectfile = target:objectfile(sourcefile_cx)
-
- -- load compiler
- local compinst = compiler.load((extension == ".ll" and "cxx" or "cc"), {target = target})
-
- -- get compile flags
- local compflags = compinst:compflags({target = target, sourcefile = sourcefile_cx})
-- add objectfile
+ local objectfile = target:objectfile(sourcefile_cx)
table.insert(target:objectfiles(), objectfile)
- -- load dependent info
- local dependfile = target:dependfile(objectfile)
- local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {})
-
- -- need build this object?
- local depvalues = {compinst:program(), compflags}
- if not depend.is_changed(dependinfo, {lastmtime = os.mtime(objectfile), values = depvalues}) then
- return
- end
-
- -- trace progress info
- progress.show(opt.progress, "${color.build.object}compiling.lex %s", sourcefile_lex)
-
- -- ensure the source file directory
- if not os.isdir(sourcefile_dir) then
- os.mkdir(sourcefile_dir)
- end
-
- -- compile lex
- os.vrunv(lex, {"-o", sourcefile_cx, sourcefile_lex})
-
- -- trace
- if option.get("verbose") then
- print(compinst:compcmd(sourcefile_cx, objectfile, {compflags = compflags}))
- end
-
- -- compile c/c++ source file for lex
- dependinfo.files = {}
- assert(compinst:compile(sourcefile_cx, objectfile, {dependinfo = dependinfo, compflags = compflags}))
+ -- add commands
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.lex %s", sourcefile_lex)
+ batchcmds:mkdir(path.directory(sourcefile_cx))
+ batchcmds:vrunv(lex.program, {"-o", sourcefile_cx, sourcefile_lex})
+ batchcmds:compile(sourcefile_cx, objectfile)
- -- update files and values to the dependent file
- dependinfo.values = depvalues
- table.insert(dependinfo.files, sourcefile_lex)
- depend.save(dependinfo, dependfile)
+ -- add deps
+ batchcmds:add_depfiles(sourcefile_lex)
+ batchcmds:set_depmtime(os.mtime(objectfile))
+ batchcmds:set_depcache(target:dependfile(objectfile))
end)
diff --git a/xmake/rules/lex_yacc/yacc/xmake.lua b/xmake/rules/lex_yacc/yacc/xmake.lua
index a93f458c0..f93db2ccb 100644
--- a/xmake/rules/lex_yacc/yacc/xmake.lua
+++ b/xmake/rules/lex_yacc/yacc/xmake.lua
@@ -20,96 +20,36 @@
-- define rule: yacc
rule("yacc")
-
- -- set extension
set_extensions(".y", ".yy")
-
- -- load yacc/bison
- before_load(function (target)
- import("core.project.config")
- import("lib.detect.find_tool")
- local yacc = config.get("__yacc")
- if not yacc then
- yacc = find_tool("bison") or find_tool("yacc")
- if yacc and yacc.program then
- config.set("__yacc", yacc.program)
- cprint("checking for Yacc ... ${color.success}%s", yacc.program)
- else
- cprint("checking for Yacc ... ${color.nothing}${text.nothing}")
- raise("yacc/bison not found!")
- end
- end
- end)
-
- -- build yacc file
- before_build_file(function (target, sourcefile_yacc, opt)
+ before_buildcmd_file(function (target, batchcmds, sourcefile_yacc, opt)
-- imports
- import("core.base.option")
- import("core.theme.theme")
- import("core.project.config")
- import("core.project.depend")
- import("core.tool.compiler")
- import("private.utils.progress")
+ import("lib.detect.find_tool")
-- get yacc
- local yacc = assert(config.get("__yacc"), "yacc not found!")
-
- -- get extension: .l/.ll
- local extension = path.extension(sourcefile_yacc)
+ local yacc = assert(find_tool("bison") or find_tool("yacc"), "yacc/bison not found!")
-- get c/c++ source file for yacc
- local sourcefile_cx = path.join(target:autogendir(), "rules", "lex_yacc", path.basename(sourcefile_yacc) .. ".tab" .. (extension == ".yy" and ".cpp" or ".c"))
- local sourcefile_dir = path.directory(sourcefile_cx)
-
- -- get object file
- local objectfile = target:objectfile(sourcefile_cx)
-
- -- load compiler
- local compinst = compiler.load((extension == ".yy" and "cxx" or "cc"), {target = target})
-
- -- get compile flags
- local compflags = compinst:compflags({target = target, sourcefile = sourcefile_cx})
+ local extension = path.extension(sourcefile_yacc)
+ local sourcefile_cx = path.join(target:autogendir(), "rules", "yacc_yacc", path.basename(sourcefile_yacc) .. ".tab" .. (extension == ".yy" and ".cpp" or ".c"))
-- add objectfile
+ local objectfile = target:objectfile(sourcefile_cx)
table.insert(target:objectfiles(), objectfile)
-- add includedirs
+ local sourcefile_dir = path.directory(sourcefile_cx)
target:add("includedirs", sourcefile_dir)
- -- load dependent info
- local dependfile = target:dependfile(objectfile)
- local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {})
-
- -- need build this object?
- local depvalues = {compinst:program(), compflags}
- if not depend.is_changed(dependinfo, {lastmtime = os.mtime(objectfile), values = depvalues}) then
- return
- end
-
- -- trace progress info
- progress.show(opt.progress, "${color.build.object}compiling.yacc %s", sourcefile_yacc)
-
- -- ensure the source file directory
- if not os.isdir(sourcefile_dir) then
- os.mkdir(sourcefile_dir)
- end
-
- -- compile yacc
- os.vrunv(yacc, {"-d", "-o", sourcefile_cx, sourcefile_yacc})
-
- -- trace
- if option.get("verbose") then
- print(compinst:compcmd(sourcefile_cx, objectfile, {compflags = compflags}))
- end
-
- -- compile c/c++ source file for yacc
- dependinfo.files = {}
- assert(compinst:compile(sourcefile_cx, objectfile, {dependinfo = dependinfo, compflags = compflags}))
+ -- add commands
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.yacc %s", sourcefile_yacc)
+ batchcmds:mkdir(sourcefile_dir)
+ batchcmds:vrunv(yacc.program, {"-d", "-o", sourcefile_cx, sourcefile_yacc})
+ batchcmds:compile(sourcefile_cx, objectfile)
- -- update files and values to the dependent file
- dependinfo.values = depvalues
- table.insert(dependinfo.files, sourcefile_yacc)
- depend.save(dependinfo, dependfile)
+ -- add deps
+ batchcmds:add_depfiles(sourcefile_yacc)
+ batchcmds:set_depmtime(os.mtime(objectfile))
+ batchcmds:set_depcache(target:dependfile(objectfile))
end)
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index 83706adab..7a29d8b4a 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -19,13 +19,7 @@
--
-- imports
-import("core.base.option")
-import("core.theme.theme")
-import("core.project.config")
-import("core.project.depend")
-import("core.tool.compiler")
import("lib.detect.find_tool")
-import("private.utils.progress")
-- get protoc
function _get_protoc(target, sourcekind)
diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua
index 76f114aea..e28a9cbb6 100644
--- a/xmake/rules/protobuf/xmake.lua
+++ b/xmake/rules/protobuf/xmake.lua
@@ -20,23 +20,14 @@
-- define rule: protobuf.cpp
rule("protobuf.cpp")
-
- -- set extension
set_extensions(".proto")
-
- -- generate build commands
before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt)
return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx")
end)
-
-- define rule: protobuf.c
rule("protobuf.c")
-
- -- set extension
set_extensions(".proto")
-
- -- generate build commands
before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt)
return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc")
end)