Skip to content

Commit

Permalink
🏗️ Use xmake
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Dec 6, 2024
1 parent e881fbe commit 4288a31
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 412 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ jobs:
shell: bash
- runs-on: macos-latest
shell: bash
# gyp only support msbuild/msvc,
# pkg-config --libs --msvc-syntax rime
# give path of rime.lib
# however, msys2 uses rime.dll.a
# force gyp to use make
# gyp tells no toolset found
# - runs-on: windows-latest
# shell: msys2
- runs-on: windows-latest
shell: msys2
runs-on: ${{matrix.runs-on}}
defaults:
run:
Expand All @@ -40,9 +34,14 @@ jobs:
if: runner.os == 'Windows'
- name: Install dependencies
if: runner.os == 'Windows'
# https://github.com/xmake-io/xmake/discussions/5699
# xmake needs git
# xmake search cxx for MinGW
run: |
pacman -Sy --noconfirm mingw-w64-x86_64-pkg-config \
mingw-w64-x86_64-librime mingw-w64-x86_64-nodejs
mingw-w64-x86_64-librime mingw-w64-x86_64-nodejs \
mingw-w64-x86_64-gcc mingw-w64-x86_64-xmake git
ln -s c++ /mingw64/bin/cxx
- name: Enable corepack
run: |
corepack enable
Expand All @@ -56,8 +55,9 @@ jobs:
- name: Install dependencies
if: runner.os == 'Linux'
run: |
sudo add-apt-repository ppa:xmake-io/xmake
sudo apt-get -y update
sudo apt-get -y install librime-dev librime1
sudo apt-get -y install librime-dev librime1 xmake
- uses: cachix/install-nix-action@v24
if: runner.os == 'macOS'
with:
Expand All @@ -68,11 +68,13 @@ jobs:
- name: Install dependencies
if: runner.os == 'macOS'
run: |
nix-env -if'<nixpkgs>' librime
nix-env -if'<nixpkgs>' librime xmake
echo "$HOME/.nix-profile/bin" >> "${GITHUB_PATH}"
- name: Build
run: |
PKG_CONFIG_PATH="$HOME/.nix-profile/lib/pkgconfig" yarn install
PKG_CONFIG_PATH="$HOME/.nix-profile/lib/pkgconfig" yarn pack
ls build/release
env:
NODE_ENV: production
- uses: actions/upload-artifact@v4
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
compile_commands.json
/.xmake/
/prebuilds/
/lib/

Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@ Rime input method integration of coc.nvim
For build, need extra them:

- [pkg-config](http://pkg-config.freedesktop.org/)
- [python](https://github.com/python/cpython): gyp is written in it
- make: gyp generates Makefile
- [xmake](https://github.com/xmake-io/xmake)

```sh
# Ubuntu
sudo apt-get -y install pkg-config librime-dev librime1 ninja
sudo apt-mark auto librime-dev pkg-config ninja
sudo apt-get -y install pkg-config librime-dev librime1 xmake
sudo apt-mark auto librime-dev pkg-config xmake
# ArchLinux
sudo pacman -S pkg-config librime ninja
sudo pacman -S pkg-config librime xmake
# Android Termux
apt-get -y install pkg-config librime ninja
apt-get -y install pkg-config librime xmake
# Nix
# without any extra operation
# homebrew
brew tap tonyfettes/homebrew-rime
brew install pkg-config librime ninja
brew install pkg-config librime xmake
# Windows msys2
pacboy -S --noconfirm pkg-config librime gcc ninja
pacboy -S --noconfirm pkg-config librime gcc xmake
```

## Install
Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"coc": "^0.0.80"
},
"scripts": {
"install": "node-gyp-build && node-gyp configure -- -fcompile_commands_json && ln -s Release/compile_commands.json build",
"install": "xmake && xmake install",
"lint": "eslint src --ext ts",
"clean": "rimraf lib",
"watch": "node esbuild.js --watch",
"prepack": "prebuildify --napi --strip && node esbuild.js",
"prepack": "pkg-prebuilds-copy --baseDir build/release --source rime.node --name=rime --strip --napi_version=7 && node esbuild.js",
"prepare": "npm-run-all clean prepack"
},
"prettier": {
Expand All @@ -29,21 +29,18 @@
"@types/mkdirp": "^0.5.2",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"async-mutex": "^0.2.6",
"coc.nvim": "^0.0.80",
"esbuild": "^0.20.2",
"eslint": "^7.14.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.1.4",
"mkdirp": "^0.5.1",
"node-addon-api": "^8.1.0",
"node-gyp": "^10.1.0",
"node-gyp-build": "^4.8.0",
"node-api-headers": "^1.4.0",
"npm-run-all": "^4.1.5",
"prebuildify": "^6.0.0",
"pkg-prebuilds": "^1.0.0",
"prettier": "^2.2.0",
"rimraf": "^5.0.5",
"ts-loader": "^8.0.2",
"typescript": "^4.1.2",
"vscode-languageserver-protocol": "^3.14.1",
"vscode-languageserver-textdocument": "^1.0.1",
Expand Down
6 changes: 5 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ mkShell {
librime
nodejs
pkg-config
python3
xmake
stdenv.cc
];
# https://github.com/NixOS/nixpkgs/issues/314313#issuecomment-2134252094
shellHook = ''
LD="$CC"
'';
}
7 changes: 4 additions & 3 deletions src/binding.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { existsSync } from 'fs';
import { resolve } from 'path';
import { default as build } from 'node-gyp-build';
import { default as build, Options } from 'pkg-prebuilds';
import { execSync } from 'child_process';

let options: Options = { name: 'rime', napi_versions: [7] };
let binding;
const root = resolve(__dirname, '..');
try {
binding = build(root);
binding = build(root, options);
} catch (e) {
let cmd = 'npm rebuild';
if (existsSync('/run/current-system/nixos-version')) {
cmd = `nix-shell --pure --run "${cmd}"`;
}
execSync(cmd, { cwd: resolve(__dirname, '..') });
binding = build(root);
binding = build(root, options);
}
export default binding;

Expand Down
89 changes: 89 additions & 0 deletions xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
-- luacheck: ignore 113 143
---@diagnostic disable: undefined-global
---@diagnostic disable: undefined-field
add_rules("mode.debug", "mode.release")

add_requires("rime")

-- https://github.com/xmake-io/xmake/issues/5938
rule("nodejs.module")
do
on_load(function(target)
-- imports
import("core.cache.detectcache")
import("core.project.target", { alias = "project_target" })

-- set kind
if target:is_plat("macosx") then
target:set("kind", "binary")
target:add("ldflags", "-bundle", "-undefined dynamic_lookup", { force = true })
else
target:set("kind", "shared")
end

-- set library name
local modulename = target:name():split('.', { plain = true })
modulename = modulename[#modulename]
target:set("filename", modulename .. ".node")

-- export symbols
if target:is_plat("windows") then
local exported_name = target:name():gsub("%.", "_")
exported_name = exported_name:match('^[^%-]+%-(.+)$') or exported_name
target:add("shflags", "/export:napi_register_module_v1", { force = true })
else
target:set("symbols", "none")
end

-- https://github.com/nodejs/node-addon-api/issues/1021
if is_plat("mingw") then
import("core.project.config")
local outputdir = config.get("buildir") .. "/node-api-stub"
if not os.isdir(outputdir) then
import("devel.git")
git.clone("https://github.com/napi-bindings/node-api-stub", { depth = 1, outputdir = outputdir })
end
target:add("files", outputdir .. "/node_api.c")
end

-- add node library
local has_node = false
local includedirs = get_config("includedirs") -- pass node library from nodejs/xmake.lua
if includedirs and includedirs:find("node-api-headers", 1, true) then
has_node = true
end
if not has_node then
-- user use `add_requires/add_packages` to add node package
for _, pkg in ipairs(target:get("packages")) do
if pkg == "node-api-headers" then
has_node = true
break
end
end
end
if not has_node then
target:add(find_package("node-api-headers"))
end
end)
on_install(function(target)
if target:is_plat("macosx") then
target:set("kind", "shared")
end
local moduledir = path.directory((target:name():gsub('%.', '/')))
import("target.action.install")(target, {
installdir = "build/" .. get_config("mode"),
libdir = moduledir,
bindir = path.join("lib", moduledir),
includedir = path.join("include", moduledir)
})
end)
end

target("rime")
do
set_languages("cxx17")
add_rules("nodejs.module")
add_packages("rime")
add_includedirs("node_modules/node-addon-api", "node_modules/node-api-headers/include")
add_files("*.cc")
end
Loading

0 comments on commit 4288a31

Please sign in to comment.