Skip to content

Commit

Permalink
🏗️ Use LuaNativeObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Dec 7, 2024
1 parent ad01525 commit a4e9a31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions jieba.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <sys/cdefs.h>
__BEGIN_DECLS

struct jieba_path {
typedef struct jieba_path {
const char *dict_path;
const char *model_path;
const char *user_dict_path;
const char *idf_path;
const char *stop_word_path;
};
} jieba_path_t;
void init(struct jieba_path);
char **cut(const char *str, bool hmm);
void deinit();
Expand Down
12 changes: 12 additions & 0 deletions jieba.nobj.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- luacheck: ignore 113
---@diagnostic disable: undefined-global
c_module "jieba" {
use_globals = true,
include "stdlib.h",
include "jieba.h",
c_function "init" {
c_call "void" "init" { "jieba_path_t", "jieba_path" },
c_call "void" "deinit" {},
c_call "char **" "cut" { "char *", "str", "bool", "hmm" },
}
}
35 changes: 31 additions & 4 deletions xmake.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
-- luacheck: ignore 111 113
-- luacheck: ignore 111 113 143
---@diagnostic disable: undefined-global
---@diagnostic disable: undefined-field
add_rules("mode.debug", "mode.release")

rule("lua-native-object")
do
set_extensions(".nobj.lua")
before_buildcmd_file(function(target, batchcmds, sourcefile, opt)
-- get c source file for lua-native-object
local dirname = path.join(target:autogendir(), "rules", "lua-native-object")
local sourcefile_c = path.join(dirname, path.basename(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.nobj.lua %s", sourcefile)
batchcmds:mkdir(path.directory(sourcefile_c))
batchcmds:vrunv("native_objects.lua",
{ "-outpath", dirname, "-gen", "lua", path(sourcefile) })
batchcmds:compile(sourcefile_c, objectfile)

-- add deps
batchcmds:add_depfiles(sourcefile)
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
end)
end

target("jieba")
do
add_rules("luarocks.module")
add_files("*.c", "*.cpp")
add_includedirs("cppjieba/include", "cppjieba/deps/limonp/include")
add_includedirs(".", "cppjieba/include", "cppjieba/deps/limonp/include")
add_rules("luarocks.module", "lua-native-object")
add_files("*.cpp", "*.nobj.lua")
add_links("stdc++")
before_build(
-- luacheck: ignore 212/target
Expand Down

0 comments on commit a4e9a31

Please sign in to comment.