Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

antlr4: add package #4369

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/a/antlr4/rules/find_antlr4.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- https://github.com/antlr/antlr4/blob/master/runtime/Cpp/cmake/antlr4-generator.cmake.in
rule("find_antlr4")
on_config(function(target)
import("lib.detect.find_tool")

assert(target:pkg("antlr4"), "Please configure add_packages(antlr4) for target(" .. target:name() .. ")")
local envs = target:pkg("antlr4"):get("envs")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接用 target:pkgenvs() 就行了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但这里拿到的 CLASSPATH 还是相对路径


local java = assert(find_tool("java", {paths = envs["PATH"]}), "java not found!")
local argv = {
"-classpath",
path.joinenv(envs["CLASSPATH"]),
"org.antlr.v4.Tool",
"-Dlanguage=Cpp",
}

target:data_set("antlr4.tool", java)
target:data_set("antlr4.tool.argv", argv)
end)
50 changes: 50 additions & 0 deletions packages/a/antlr4/rules/lexer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
rule("lexer")
set_extensions(".g4")

add_deps("@find_antlr4")

on_config(function (target)
-- remove parser g4
for _, sourcebatch in pairs(target:sourcebatches()) do
if sourcebatch.rulename == "@antlr4/lexer" then
local sourcefiles = {}
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
if not sourcefile:lower():find("parser") then
table.insert(sourcefiles, sourcefile)
end
end
sourcebatch.sourcefiles = sourcefiles
break
end
end
end)

before_buildcmd_file(function (target, batchcmds, sourcefile_g4, opt)
local java = target:data("antlr4.tool")
local argv = target:data("antlr4.tool.argv")
table.join2(argv, target:values("antlr4.lexer.flags"))

local autogendir = path.join(target:autogendir(), "rules", "antlr4", "lexer")
local sourcefile_cxx = path.join(autogendir, path.directory(sourcefile_g4), path.basename(sourcefile_g4) .. ".cpp")
local sourcefile_dir = path.directory(sourcefile_cxx)

batchcmds:mkdir(sourcefile_dir)
table.insert(argv, "-o")
table.insert(argv, autogendir)
table.insert(argv, "-lib")
table.insert(argv, sourcefile_dir)

target:add("includedirs", sourcefile_dir, {public = true})

table.insert(argv, sourcefile_g4)
batchcmds:vrunv(java.program, argv)
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.g4 %s", sourcefile_g4)

local objectfile = target:objectfile(sourcefile_cxx)
table.insert(target:objectfiles(), objectfile)
batchcmds:compile(sourcefile_cxx, objectfile)

batchcmds:add_depfiles(sourcefile_g4)
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
end)
75 changes: 75 additions & 0 deletions packages/a/antlr4/rules/parser.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
rule("parser")
set_extensions(".g4")

add_deps("@lexer", {order = true})

on_config(function (target)
-- remove lexer g4
for _, sourcebatch in pairs(target:sourcebatches()) do
if sourcebatch.rulename == "@antlr4/parser" then
local sourcefiles = {}
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
if not sourcefile:lower():find("lexer") then
table.insert(sourcefiles, sourcefile)
end
end
sourcebatch.sourcefiles = sourcefiles
break
end
end
end)

before_buildcmd_file(function (target, batchcmds, sourcefile_g4, opt)
local java = target:data("antlr4.tool")
local argv = target:data("antlr4.tool.argv")

local visitor = target:extraconf("rules", "@antlr4/parser", "visitor")
local listener = target:extraconf("rules", "@antlr4/parser", "listener")

table.insert(argv, (visitor and "-visitor" or "-no-visitor"))
table.insert(argv, (listener and "-listener" or "-no-listener"))

table.join2(argv, target:values("antlr4.parser.flags"))

local autogendir = path.join(target:autogendir(), "rules", "antlr4", "parser")
local sourcefile_cxx = path.join(autogendir, path.directory(sourcefile_g4), path.basename(sourcefile_g4) .. ".cpp")
local sourcefile_dir = path.directory(sourcefile_cxx)

batchcmds:mkdir(sourcefile_dir)
table.insert(argv, "-o")
table.insert(argv, autogendir)
table.insert(argv, "-lib")
table.insert(argv, sourcefile_dir)

target:add("includedirs", sourcefile_dir, {public = true})

table.insert(argv, sourcefile_g4)
batchcmds:vrunv(java.program, argv)
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.g4 %s", sourcefile_g4)

local sourcefiles_cxx = {sourcefile_cxx}

local autogendir = path.join(target:autogendir(), "rules", "antlr4", "parser")
local sourcefile_file_dir = path.join(autogendir, path.directory(sourcefile_g4))
if visitor then
table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "Visitor.cpp"))
table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "BaseVisitor.cpp"))
end
if listener then
table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "Listener.cpp"))
table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "BaseListener.cpp"))
end

for _, cxx in pairs(sourcefiles_cxx) do
local objectfile = target:objectfile(cxx)
table.insert(target:objectfiles(), objectfile)
batchcmds:compile(cxx, objectfile)

if cxx == sourcefile_cxx then
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
end
end

batchcmds:add_depfiles(sourcefile_g4)
end)
25 changes: 25 additions & 0 deletions packages/a/antlr4/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package("antlr4")
set_kind("binary")
set_homepage("https://www.antlr.org")
set_description("powerful parser generator for reading, processing, executing, or translating structured text or binary files.")
set_license("BSD-3-Clause")

add_urls("https://www.antlr.org/download/antlr-$(version)-complete.jar")
add_versions("4.13.1", "bc13a9c57a8dd7d5196888211e5ede657cb64a3ce968608697e4f668251a8487")

add_deps("openjdk")

on_install("windows|x64", "linux|x86_64", "macosx|x86_64", "macosx|arm64", "mingw|x86_64", function (package)
local source = "antlr-" .. package:version() .. "-complete.jar"
local target = path.join(package:installdir("lib"), "antlr-complete.jar")
os.vcp("../" .. source, package:installdir("lib"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥解压出来会调到 source 目录外面去?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接下的 jar,不会解压

os.vmv(package:installdir("lib", source), target)
package:addenv("CLASSPATH", "lib/antlr-complete.jar")
package:mark_as_pathenv("CLASSPATH")
end)

on_test(function (package)
if not package:is_cross() then
os.vrun("java -classpath $(env CLASSPATH) org.antlr.v4.Tool")
end
end)
Loading