Skip to content

Commit 3a95a48

Browse files
committed
feat(core): Add separate crate to enable exporting interfaces to Lua
1 parent cd135d2 commit 3a95a48

9 files changed

Lines changed: 88 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
resolver = "2"
33
members = [
44
".",
5+
"rusile",
56
]
67

78
[workspace.package]
9+
version = "0.15.3"
810
edition = "2021"
11+
rust-version = "1.71.0"
912
authors = [
1013
"Simon Cozens",
1114
"Caleb Maclennan <caleb@alerque.com>",
@@ -18,16 +21,16 @@ license = "MIT"
1821

1922
[package]
2023
name = "sile"
21-
version = "0.15.3"
22-
edition.workspace = true
23-
rust-version = "1.71.0"
2424
description = "Simon’s Improved Layout Engine"
25+
readme = "README.md"
26+
build = "build-aux/build.rs"
27+
version.workspace = true
28+
edition.workspace = true
29+
rust-version.workspace = true
2530
authors.workspace = true
2631
homepage.workspace = true
27-
readme = "README.md"
2832
repository.workspace = true
2933
license.workspace = true
30-
build = "build-aux/build.rs"
3134

3235
[[bin]]
3336
name = "sile"

Makefile.am

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,22 @@ endif
5656
$(MANUAL): $(FIGURES)
5757

5858
BUILT_LUA_SOURCES = core/features.lua core/pathsetup.lua core/version.lua
59+
RUSILE_SOURCES = rusile/Cargo.toml rusile/src/lib.rs
5960

6061
bin_PROGRAMS = sile
6162
bin_SCRIPTS = sile-lua
6263
dist_man_MANS = sile-lua.1
6364
sile_SOURCES = src/bin/sile.rs src/lib.rs src/cli.rs
6465
EXTRA_sile_SOURCES =
65-
if !EMBEDDED_RESOURCES
66+
if EMBEDDED_RESOURCES
67+
noinst_LIBRARIES = librusile.a
68+
librusile_a_SOURCES = $(RUSILE_SOURCES)
69+
else !EMBEDDED_RESOURCES
6670
nobase_dist_pkgdata_DATA = $(SILEDATA) $(LUALIBRARIES)
6771
nobase_nodist_pkgdata_DATA = $(BUILT_LUA_SOURCES) $(LUAMODULES)
68-
endif
72+
pkglib_LIBRARIES = rusile.so
73+
rusile_so_SOURCES = $(RUSILE_SOURCES)
74+
endif !EMBEDDED_RESOURCES
6975
dist_doc_DATA = README.md CHANGELOG.md
7076
dist_pdf_DATA = $(_MANUAL)
7177
dist_license_DATA = LICENSE.md
@@ -122,6 +128,7 @@ $(CARGO_BIN): justenough/.libs/justenoughicu.a
122128
$(CARGO_BIN): justenough/.libs/justenoughlibtexpdf.a
123129
$(CARGO_BIN): justenough/.libs/svg.a
124130
$(CARGO_BIN): libtexpdf/.libs/libtexpdf.a
131+
$(CARGO_BIN): librusile.a rusile.so
125132

126133
src/embed-includes.rs: Makefile-distfiles
127134
{
@@ -146,6 +153,7 @@ else
146153
MLUAVER = lua$(LUA_SHORT_VERSION)
147154
endif
148155
CARGO_FEATURE_ARGS = --features $(MLUAVER)
156+
RUSILE_FEATURE_ARG = --features $(MLUAVER)
149157

150158
if !SYSTEM_LUA_SOURCES
151159
CARGO_FEATURE_ARGS += --features vendored
@@ -159,6 +167,14 @@ if FONT_VARIATIONS
159167
CARGO_FEATURE_ARGS += --features variations
160168
endif
161169

170+
rusile.so:
171+
$(CARGO_ENV) $(CARGO) build $(CARGO_VERBOSE) $(RUSILE_FEATURE_ARG) $(CARGO_RELEASE_ARGS) -p rusile
172+
$(INSTALL) @builddir@/target/@RUST_TARGET_SUBDIR@/lib$@ $@
173+
174+
librusile.a:
175+
$(CARGO_ENV) $(CARGO) build $(CARGO_VERBOSE) $(RUSILE_FEATURE_ARG) $(CARGO_RELEASE_ARGS) -p rusile
176+
$(INSTALL) @builddir@/target/@RUST_TARGET_SUBDIR@/$@ $@
177+
162178
DEPDIR := .deps
163179
LOCALFONTS := FONTCONFIG_FILE=$(PWD)/fontconfig.conf
164180
LOCALPATHS := SILE_PATH="$(PWD);libtexpdf/.libs;justenough/.libs"

build-aux/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ fn main() {
3636
generate_shell_completions();
3737
#[cfg(feature = "static")]
3838
{
39+
println!("cargo:rustc-link-arg=-lrusile");
40+
3941
let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
4042
println!(
4143
"cargo:rustc-link-search=native={}",

build-aux/pkg.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ in stdenv.mkDerivation (finalAttrs: {
115115
# `pdfinfo`, by using `false` we make sure that if it is expected during
116116
# build time we would fail to build since we only provide it at test time.
117117
"PDFINFO=false"
118+
# We're using Cargo to build a shared library skipping some libtool bits
119+
# and Nix mistakenly assumes are relevant and thinks it needs to cleanup.
120+
"RANLIB=:"
118121
#"--with-manual" In Nixpkgs we add this flag, here its not important enough
119122
] ++ lib.optionals (!lua.pkgs.isLuaJIT) [
120123
"--without-luajit"

core/sile.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ SILE = {}
1616
--- Fields
1717
-- @section fields
1818

19+
--- Load the C module that provides any and all SILE features implemented in Rust. The exports in this module are meant
20+
--- to be moved to approprate places in the Lua API as we load other things and assemble the public facing interface.
21+
--- This location is considered internal and accessing them directly from here is not supported.
22+
-- @table SILE.rusile
23+
SILE._rusile = require("rusile")
24+
1925
--- Machine friendly short-form version.
2026
-- Semver, prefixed with "v", possible postfixed with ".r" followed by VCS version information.
2127
-- @string version

rusile/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "rusile"
3+
description = "Rusty components for the SILE typesetter"
4+
readme = "README.md"
5+
version.workspace = true
6+
edition.workspace = true
7+
rust-version.workspace = true
8+
authors.workspace = true
9+
homepage.workspace = true
10+
repository.workspace = true
11+
license.workspace = true
12+
13+
[lib]
14+
crate_type = ["rlib", "cdylib", "staticlib"]
15+
16+
[dependencies.sile]
17+
path = ".."
18+
19+
[features]
20+
lua54 = [ "mlua/lua54" ]
21+
lua53 = [ "mlua/lua53" ]
22+
lua52 = [ "mlua/lua52" ]
23+
lua51 = [ "mlua/lua51" ]
24+
luajit = [ "mlua/luajit" ]
25+
26+
[dependencies.mlua]
27+
version = "0.9"
28+
features = [ "module" ]

rusile/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![crate_type = "cdylib"]
2+
#![crate_type = "rlib"]
3+
#![crate_type = "staticlib"]
4+
5+
use mlua::prelude::*;
6+
7+
#[mlua::lua_module]
8+
fn rusile(lua: &Lua) -> LuaResult<LuaTable> {
9+
let exports = lua.create_table().unwrap();
10+
Ok(exports)
11+
}

src/embed.rs.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct SileModules;
3131
// Link Lua loader functions from C modules that Lua would otherwise be loading externally that
3232
// we've linked into the CLI binary. Linking happens in build-aux/build.rs.
3333
extern "C-unwind" {
34+
fn luaopen_rusile(lua: *mut mlua::lua_State) -> i32;
3435
fn luaopen_fontmetrics(lua: *mut mlua::lua_State) -> i32;
3536
fn luaopen_justenoughfontconfig(lua: *mut mlua::lua_State) -> i32;
3637
fn luaopen_justenoughharfbuzz(lua: *mut mlua::lua_State) -> i32;
@@ -52,6 +53,9 @@ pub fn inject_embedded_loader(lua: &Lua) {
5253
loaders
5354
.push(LuaFunction::wrap(|lua, module: String| unsafe {
5455
match module.as_str() {
56+
"rusile" => lua
57+
.create_c_function(luaopen_rusile)
58+
.map(LuaValue::Function),
5559
"fontmetrics" => lua
5660
.create_c_function(luaopen_fontmetrics)
5761
.map(LuaValue::Function),

0 commit comments

Comments
 (0)