summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-07-29 22:43:45 +0800
committerruki <[email protected]>2021-07-29 22:43:45 +0800
commite2067d1b58aeec0024ac2f52e7599a4893b15f14 (patch)
tree783e31fb7410f9e2eb17a47fbb6b7a6b1c09a74b
parent6acd768b2f692c49ddc89b5fffdfed0bf75744dc (diff)
impl vala rule
-rw-r--r--tests/projects/vala/lua/src/main.vala21
-rw-r--r--tests/projects/vala/lua/src/sub/stub.vala21
-rw-r--r--tests/projects/vala/lua/xmake.lua10
-rw-r--r--tests/projects/vala/sdl/src/main.vala91
-rw-r--r--tests/projects/vala/sdl/xmake.lua5
-rw-r--r--xmake/languages/vala/check_main.lua40
-rw-r--r--xmake/languages/vala/load.lua72
-rw-r--r--xmake/languages/vala/xmake.lua85
-rw-r--r--xmake/rules/lex_yacc/lex/xmake.lua4
-rw-r--r--xmake/rules/lex_yacc/yacc/xmake.lua4
-rw-r--r--xmake/rules/vala/xmake.lua33
11 files changed, 85 insertions, 301 deletions
diff --git a/tests/projects/vala/lua/src/main.vala b/tests/projects/vala/lua/src/main.vala
new file mode 100644
index 000000000..8e8a32da0
--- /dev/null
+++ b/tests/projects/vala/lua/src/main.vala
@@ -0,0 +1,21 @@
+using Lua;
+
+static int my_func (LuaVM vm) {
+ stdout.printf ("Vala Code From Lua Code! (%f)\n", vm.to_number (1));
+ return 1;
+}
+
+static int main (string[] args) {
+
+ string code = """
+ print "Lua Code From Vala Code!"
+ my_func(33)
+ """;
+
+ var vm = new LuaVM ();
+ vm.open_libs ();
+ vm.register ("my_func", my_func);
+ vm.do_string (code);
+
+ return 0;
+}
diff --git a/tests/projects/vala/lua/src/sub/stub.vala b/tests/projects/vala/lua/src/sub/stub.vala
new file mode 100644
index 000000000..7b3a2d4d8
--- /dev/null
+++ b/tests/projects/vala/lua/src/sub/stub.vala
@@ -0,0 +1,21 @@
+using Lua;
+
+static int my_func2 (LuaVM vm) {
+ stdout.printf ("Vala Code From Lua Code! (%f)\n", vm.to_number (1));
+ return 1;
+}
+
+static int main2 (string[] args) {
+
+ string code = """
+ print "Lua Code From Vala Code!"
+ my_func(33)
+ """;
+
+ var vm = new LuaVM ();
+ vm.open_libs ();
+ vm.register ("my_func", my_func2);
+ vm.do_string (code);
+
+ return 0;
+}
diff --git a/tests/projects/vala/lua/xmake.lua b/tests/projects/vala/lua/xmake.lua
new file mode 100644
index 000000000..e149fb6d3
--- /dev/null
+++ b/tests/projects/vala/lua/xmake.lua
@@ -0,0 +1,10 @@
+add_rules("mode.release", "mode.debug")
+
+add_requires("lua", "glib")
+
+target("test")
+ set_kind("binary")
+ add_rules("vala")
+ add_files("src/**.vala")
+ add_packages("lua", "glib")
+ add_values("vala.packages", "lua")
diff --git a/tests/projects/vala/sdl/src/main.vala b/tests/projects/vala/sdl/src/main.vala
deleted file mode 100644
index c8aa2588b..000000000
--- a/tests/projects/vala/sdl/src/main.vala
+++ /dev/null
@@ -1,91 +0,0 @@
-using SDL;
-using SDLGraphics;
-
-public class SDLSample : Object {
-
- private const int SCREEN_WIDTH = 640;
- private const int SCREEN_HEIGHT = 480;
- private const int SCREEN_BPP = 32;
- private const int DELAY = 10;
-
- private unowned SDL.Screen screen;
- private GLib.Rand rand;
- private bool done;
-
- public SDLSample () {
- this.rand = new GLib.Rand ();
- }
-
- public void run () {
- init_video ();
-
- while (!done) {
- draw ();
- process_events ();
- SDL.Timer.delay (DELAY);
- }
- }
-
- private void init_video () {
- uint32 video_flags = SurfaceFlag.DOUBLEBUF
- | SurfaceFlag.HWACCEL
- | SurfaceFlag.HWSURFACE;
-
- this.screen = Screen.set_video_mode (SCREEN_WIDTH, SCREEN_HEIGHT,
- SCREEN_BPP, video_flags);
- if (this.screen == null) {
- stderr.printf ("Could not set video mode.\n");
- }
-
- SDL.WindowManager.set_caption ("Vala SDL Demo", "");
- }
-
- private void draw () {
- int16 x = (int16) rand.int_range (0, screen.w);
- int16 y = (int16) rand.int_range (0, screen.h);
- int16 radius = (int16) rand.int_range (0, 100);
- uint32 color = rand.next_int ();
-
- Circle.fill_color (this.screen, x, y, radius, color);
- Circle.outline_color_aa (this.screen, x, y, radius, color);
-
- this.screen.flip ();
- }
-
- private void process_events () {
- Event event;
- while (Event.poll (out event) == 1) {
- switch (event.type) {
- case EventType.QUIT:
- this.done = true;
- break;
- case EventType.KEYDOWN:
- this.on_keyboard_event (event.key);
- break;
- }
- }
- }
-
- private void on_keyboard_event (KeyboardEvent event) {
- if (is_alt_enter (event.keysym)) {
- WindowManager.toggle_fullscreen (screen);
- }
- }
-
- private static bool is_alt_enter (Key key) {
- return ((key.mod & KeyModifier.LALT)!=0)
- && (key.sym == KeySymbol.RETURN
- || key.sym == KeySymbol.KP_ENTER);
- }
-
- public static int main (string[] args) {
- SDL.init (InitFlag.VIDEO);
-
- var sample = new SDLSample ();
- sample.run ();
-
- SDL.quit ();
-
- return 0;
- }
-}
diff --git a/tests/projects/vala/sdl/xmake.lua b/tests/projects/vala/sdl/xmake.lua
deleted file mode 100644
index 7f7266724..000000000
--- a/tests/projects/vala/sdl/xmake.lua
+++ /dev/null
@@ -1,5 +0,0 @@
-add_rules("mode.release", "mode.debug")
-
-target("test")
- set_kind("binary")
- add_files("src/*.vala")
diff --git a/xmake/languages/vala/check_main.lua b/xmake/languages/vala/check_main.lua
deleted file mode 100644
index b2251f190..000000000
--- a/xmake/languages/vala/check_main.lua
+++ /dev/null
@@ -1,40 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file check_main.lua
---
-
--- check it
-function main(sourcefile)
-
- -- load source code
- local sourcecode = io.readfile(sourcefile)
-
- -- remove comment first
- sourcecode = sourcecode:gsub("/%*.-%*/", "")
- sourcecode = sourcecode:gsub("//.-\n", "\n")
-
- -- find "public static int main (string[] args) {"
- if sourcecode:find("public%s+static%s+int%s+main%s*%(.-%)") then
- return true
- end
-
- -- no main function
- return false
-end
-
-
diff --git a/xmake/languages/vala/load.lua b/xmake/languages/vala/load.lua
deleted file mode 100644
index 32ab9cae2..000000000
--- a/xmake/languages/vala/load.lua
+++ /dev/null
@@ -1,72 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file load.lua
---
-
-function _get_apis()
- local apis = {}
- apis.values = {
- -- target.add_xxx
- "target.add_links"
- , "target.add_syslinks"
- , "target.add_vaflags"
- , "target.add_ldflags"
- , "target.add_arflags"
- , "target.add_shflags"
- , "target.add_rpathdirs" -- @note do not translate path, it's usually an absolute path or contains $ORIGIN/@loader_path
- -- option.add_xxx
- , "option.add_links"
- , "option.add_syslinks"
- , "option.add_vaflags"
- , "option.add_ldflags"
- , "option.add_arflags"
- , "option.add_shflags"
- , "option.add_rpathdirs"
- -- package.add_xxx
- , "package.add_links"
- , "package.add_syslinks"
- , "package.add_vaflags"
- , "package.add_ldflags"
- , "package.add_arflags"
- , "package.add_shflags"
- , "package.add_rpathdirs"
- , "package.add_linkdirs"
- -- toolchain.add_xxx
- , "toolchain.add_links"
- , "toolchain.add_syslinks"
- , "toolchain.add_vaflags"
- , "toolchain.add_ldflags"
- , "toolchain.add_arflags"
- , "toolchain.add_shflags"
- , "toolchain.add_rpathdirs"
- , "toolchain.add_linkdirs"
- }
- apis.paths = {
- -- target.add_xxx
- "target.add_linkdirs"
- -- option.add_xxx
- , "option.add_linkdirs"
- }
- return apis
-end
-
-function main()
- return {apis = _get_apis()}
-end
-
-
diff --git a/xmake/languages/vala/xmake.lua b/xmake/languages/vala/xmake.lua
deleted file mode 100644
index 16cc226f3..000000000
--- a/xmake/languages/vala/xmake.lua
+++ /dev/null
@@ -1,85 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file xmake.lua
---
-
-language("vala")
- add_rules("vala")
- set_sourcekinds {va = ".vala"}
- set_sourceflags {va = "vaflags"}
- set_targetkinds {binary = "ld", static = "ar", shared = "sh"}
- set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"}
- set_langkinds {vala = "va"}
- set_mixingkinds("va", "cc", "cxx", "as")
-
- on_load("load")
- on_check_main("check_main")
-
- set_nameflags {
- object = {
- "target.symbols"
- , "target.warnings"
- , "target.optimize:check"
- , "target.vectorexts:check"
- }
- , binary = {
- "config.linkdirs"
- , "target.linkdirs"
- , "target.rpathdirs"
- , "target.strip"
- , "target.symbols"
- , "toolchain.linkdirs"
- , "toolchain.rpathdirs"
- , "config.links"
- , "target.links"
- , "toolchain.links"
- , "config.syslinks"
- , "target.syslinks"
- , "toolchain.syslinks"
- }
- , shared = {
- "config.linkdirs"
- , "target.linkdirs"
- , "target.strip"
- , "target.symbols"
- , "toolchain.linkdirs"
- , "config.links"
- , "target.links"
- , "toolchain.links"
- , "config.syslinks"
- , "target.syslinks"
- , "toolchain.syslinks"
- }
- , static = {
- "target.strip"
- , "target.symbols"
- }
- }
-
- set_menu {
- config = {
- {category = "Cross Complation Configuration/Compiler Configuration" }
- , {nil, "va", "kv", nil, "The Vala Compiler" }
-
- , {category = "Cross Complation Configuration/Builtin Flags Configuration" }
- , {nil, "links", "kv", nil, "The Link Libraries" }
- , {nil, "syslinks", "kv", nil, "The System Link Libraries" }
- , {nil, "linkdirs", "kv", nil, "The Link Search Directories" }
- }
- }
-
diff --git a/xmake/rules/lex_yacc/lex/xmake.lua b/xmake/rules/lex_yacc/lex/xmake.lua
index cbdb766e4..adc3c49ec 100644
--- a/xmake/rules/lex_yacc/lex/xmake.lua
+++ b/xmake/rules/lex_yacc/lex/xmake.lua
@@ -23,10 +23,8 @@ rule("lex")
set_extensions(".l", ".ll")
on_buildcmd_file(function (target, batchcmds, sourcefile_lex, opt)
- -- imports
- import("lib.detect.find_tool")
-
-- get lex
+ import("lib.detect.find_tool")
local lex = assert(find_tool("flex") or find_tool("lex"), "lex not found!")
-- get c/c++ source file for lex
diff --git a/xmake/rules/lex_yacc/yacc/xmake.lua b/xmake/rules/lex_yacc/yacc/xmake.lua
index f93db2ccb..70514cadb 100644
--- a/xmake/rules/lex_yacc/yacc/xmake.lua
+++ b/xmake/rules/lex_yacc/yacc/xmake.lua
@@ -23,10 +23,8 @@ rule("yacc")
set_extensions(".y", ".yy")
before_buildcmd_file(function (target, batchcmds, sourcefile_yacc, opt)
- -- imports
- import("lib.detect.find_tool")
-
-- get yacc
+ import("lib.detect.find_tool")
local yacc = assert(find_tool("bison") or find_tool("yacc"), "yacc/bison not found!")
-- get c/c++ source file for yacc
diff --git a/xmake/rules/vala/xmake.lua b/xmake/rules/vala/xmake.lua
index 11b002a02..6de49665c 100644
--- a/xmake/rules/vala/xmake.lua
+++ b/xmake/rules/vala/xmake.lua
@@ -20,9 +20,38 @@
rule("vala")
set_extensions(".vala")
- before_buildcmd_files(function (target, batchcmds, sourcebatch, opt)
+ before_buildcmd_file(function (target, batchcmds, sourcefile_vala, opt)
+
+ -- get valac
import("lib.detect.find_tool")
local valac = assert(find_tool("valac"), "valac not found!")
- print(sourcebatch)
+
+ -- get c source file for vala
+ local sourcefile_c = path.join(target:autogendir(), "rules", "vala", path.basename(sourcefile_vala) .. ".c")
+ local basedir = path.directory(sourcefile_c)
+
+ -- add objectfile
+ local objectfile = target:objectfile(sourcefile_c)
+ table.insert(target:objectfiles(), objectfile)
+
+ -- add commands
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.vala %s", sourcefile_vala)
+ batchcmds:mkdir(basedir)
+ local argv = {"-C", "-b", basedir}
+ local packages = target:values("vala.packages")
+ if packages then
+ for _, package in ipairs(packages) do
+ table.insert(argv, "--pkg")
+ table.insert(argv, package)
+ end
+ end
+ table.insert(argv, sourcefile_vala)
+ batchcmds:vrunv(valac.program, argv)
+ batchcmds:compile(sourcefile_c, objectfile)
+
+ -- add deps
+ batchcmds:add_depfiles(sourcefile_vala)
+ batchcmds:set_depmtime(os.mtime(objectfile))
+ batchcmds:set_depcache(target:dependfile(objectfile))
end)