Procházet zdrojové kódy

antlr4: add package (#4369)

* antlr4: add package

* remove unused file, refractor parser rule

* fix env path

* add more build message

* add 4.13.2 version

* fix env

* disable msys
star9029 před 9 měsíci
rodič
revize
60ad6f5456

+ 19 - 0
packages/a/antlr4/rules/find_antlr4.lua

@@ -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:pkgenvs()
+        local java = assert(find_tool("java", {envs = envs}), "java not found!")
+        local argv = {
+            "-classpath",
+            envs.CLASSPATH,
+            "org.antlr.v4.Tool",
+            "-Dlanguage=Cpp",
+        }
+
+        target:data_set("antlr4.tool", java)
+        target:data_set("antlr4.tool.argv", argv)
+    end)

+ 51 - 0
packages/a/antlr4/rules/lexer.lua

@@ -0,0 +1,51 @@
+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:show_progress(opt.progress, "${color.build.object}compiling.g4 %s", sourcefile_g4)
+        batchcmds:vrunv(java.program, argv)
+
+        local objectfile = target:objectfile(sourcefile_cxx)
+        table.insert(target:objectfiles(), objectfile)
+        batchcmds:show_progress(opt.progress, "${color.build.object}compiling.$(mode) %s", sourcefile_cxx)
+        batchcmds:compile(sourcefile_cxx, objectfile)
+
+        batchcmds:add_depfiles(sourcefile_g4)
+        batchcmds:set_depmtime(os.mtime(objectfile))
+        batchcmds:set_depcache(target:dependfile(objectfile))
+    end)

+ 75 - 0
packages/a/antlr4/rules/parser.lua

@@ -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:show_progress(opt.progress, "${color.build.object}compiling.g4 %s", sourcefile_g4)
+        batchcmds:vrunv(java.program, argv)
+
+        local sourcefiles_cxx = {sourcefile_cxx}
+
+        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 ipairs(sourcefiles_cxx) do
+            local objectfile = target:objectfile(cxx)
+            table.insert(target:objectfiles(), objectfile)
+            batchcmds:show_progress(opt.progress, "${color.build.object}compiling.$(mode) %s", cxx)
+            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)

+ 36 - 0
packages/a/antlr4/xmake.lua

@@ -0,0 +1,36 @@
+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.2", "eae2dfa119a64327444672aff63e9ec35a20180dc5b8090b7a6ab85125df4d76")
+    add_versions("4.13.1", "bc13a9c57a8dd7d5196888211e5ede657cb64a3ce968608697e4f668251a8487")
+
+    if is_plat("linux") then
+        add_extsources("pacman::antlr4", "apt::antlr4")
+    elseif is_plat("macosx") then
+        add_extsources("brew::antlr")
+    end
+
+    set_policy("package.precompiled", false)
+
+    add_deps("openjdk")
+
+    on_load(function (package)
+        package:mark_as_pathenv("CLASSPATH")
+        package:addenv("CLASSPATH", "lib/antlr-complete.jar")
+    end)
+
+    on_install("@windows", "@linux", "@macosx", 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"))
+        os.vmv(package:installdir("lib", source), target)
+    end)
+
+    on_test(function (package)
+        os.vrun("java -classpath $(env CLASSPATH) org.antlr.v4.Tool")
+    end)