Skip to content

Commit 4cbf146

Browse files
authored
Remove bindings to private or c std types (#149)
* Remove bindings to private or c std types * Add va_list to allowlist * Remove functions using va_list * Use var_os in docs.rs check
1 parent 4190919 commit 4cbf146

File tree

4 files changed

+80
-21
lines changed

4 files changed

+80
-21
lines changed

mupdf-sys/build.rs

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,29 @@ fn run() -> Result<()> {
4444
let out_dir =
4545
PathBuf::from(env::var_os("OUT_DIR").ok_or("Missing OUT_DIR environment variable")?);
4646

47-
let build_dir = out_dir.join("build");
48-
let build_dir = build_dir.to_str().ok_or_else(|| {
49-
format!("Build dir path is required to be valid UTF-8, got {build_dir:?}")
50-
})?;
47+
let sysroot = find_clang_sysroot(&target)?;
5148

52-
if let Err(e) = remove_dir_all(build_dir) {
53-
if e.kind() != ErrorKind::NotFound {
54-
println!("cargo:warning=Unable to clear {build_dir:?}. This may lead to flaky builds that might not incorporate configurations changes: {e}");
55-
}
56-
}
49+
let docs = env::var_os("DOCS_RS").is_some();
50+
if !docs {
51+
let build_dir = out_dir.join("build");
52+
let build_dir = build_dir.to_str().ok_or_else(|| {
53+
format!("Build dir path is required to be valid UTF-8, got {build_dir:?}")
54+
})?;
5755

58-
let sysroot = find_clang_sysroot(&target)?;
56+
if let Err(e) = remove_dir_all(build_dir) {
57+
if e.kind() != ErrorKind::NotFound {
58+
println!("cargo:warning=Unable to clear {build_dir:?}. This may lead to flaky builds that might not incorporate configurations changes: {e}");
59+
}
60+
}
5961

60-
copy_recursive(&src_dir, build_dir.as_ref(), &[".git".as_ref()])?;
62+
copy_recursive(&src_dir, build_dir.as_ref(), &[".git".as_ref()])?;
6163

62-
println!("cargo:rerun-if-changed=wrapper.h");
63-
println!("cargo:rerun-if-changed=wrapper.c");
64+
println!("cargo:rerun-if-changed=wrapper.h");
65+
println!("cargo:rerun-if-changed=wrapper.c");
6466

65-
Build::new(&target).run(&target, build_dir)?;
66-
build_wrapper(&target).map_err(|e| format!("Unable to compile mupdf wrapper:\n {e}"))?;
67+
Build::new(&target).run(&target, build_dir)?;
68+
build_wrapper(&target).map_err(|e| format!("Unable to compile mupdf wrapper:\n {e}"))?;
69+
}
6770

6871
generate_bindings(&target, &out_dir.join("bindings.rs"), sysroot)
6972
.map_err(|e| format!("Unable to generate mupdf bindings using bindgen:\n {e}"))?;
@@ -165,15 +168,61 @@ fn generate_bindings(target: &Target, path: &Path, sysroot: Option<String>) -> R
165168
builder = builder
166169
.clang_arg("-Imupdf/include")
167170
.header("wrapper.h")
168-
.header("wrapper.c")
171+
.header("wrapper.c");
172+
173+
builder = builder
174+
.allowlist_recursively(false)
175+
.allowlist_type("wchar_t")
176+
.allowlist_type("FILE")
177+
.opaque_type("FILE");
178+
179+
builder = builder
169180
.allowlist_item("fz_.*")
170181
.allowlist_item("FZ_.*")
171182
.allowlist_item("pdf_.*")
172183
.allowlist_item("PDF_.*")
184+
.allowlist_type("cmap_splay")
173185
.allowlist_item("ucdn_.*")
174186
.allowlist_item("UCDN_.*")
175187
.allowlist_item("Memento_.*")
176-
.allowlist_item("mupdf_.*")
188+
.allowlist_item("mupdf_.*");
189+
190+
// remove va_list functions as for all of these versions using ... exist
191+
builder = builder
192+
.blocklist_function("Memento_vasprintf") // Memento_asprintf
193+
.blocklist_function("fz_vthrow") // fz_throw
194+
.blocklist_function("fz_vwarn") // fz_warn
195+
.blocklist_function("fz_vlog_error_printf") // fz_log_error_printf
196+
.blocklist_function("fz_append_vprintf") // fz_append_printf
197+
.blocklist_function("fz_write_vprintf") // fz_write_printf
198+
.blocklist_function("fz_vsnprintf") // fz_snprintf
199+
.blocklist_function("fz_format_string"); // mupdf_format_string
200+
201+
// build config
202+
builder = builder
203+
.blocklist_var("FZ_VERSION.*")
204+
.blocklist_var("FZ_ENABLE_.*")
205+
.blocklist_var("FZ_PLOTTERS_.*");
206+
207+
// internal implementation details, considered private
208+
builder = builder
209+
.blocklist_item("fz_jmp_buf")
210+
.blocklist_function("fz_var_imp")
211+
.blocklist_function("fz_push_try")
212+
.blocklist_function("fz_do_.*")
213+
.blocklist_var("FZ_JMPBUF_ALIGN")
214+
.blocklist_type("fz_error_stack_slot")
215+
.blocklist_type("fz_error_context")
216+
.blocklist_type("fz_warn_context")
217+
.blocklist_type("fz_aa_context")
218+
.blocklist_type("fz_activity_.*")
219+
.blocklist_function("fz_register_activity_logger")
220+
.opaque_type("fz_context")
221+
.blocklist_type("fz_new_context_imp")
222+
.blocklist_type("fz_lock")
223+
.blocklist_type("fz_unlock");
224+
225+
builder = builder
177226
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
178227
.parse_callbacks(Box::new(DocsCallbacks::default()));
179228

@@ -182,10 +231,7 @@ fn generate_bindings(target: &Target, path: &Path, sysroot: Option<String>) -> R
182231
builder = builder.parse_callbacks(Box::new(ZerocopyDeriveCallbacks));
183232
}
184233

185-
builder
186-
.size_t_is_usize(true)
187-
.generate()?
188-
.write_to_file(path)?;
234+
builder.use_core().generate()?.write_to_file(path)?;
189235

190236
Ok(())
191237
}

mupdf-sys/make.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ impl Make {
201201

202202
self.make_bool("verbose", true);
203203

204+
// harfbuzz sometimes requires this
205+
if target.os == "windows" {
206+
self.build.flag("-Wa,-mbig-obj");
207+
}
208+
204209
self.libs()?;
205210
self.cpus(target);
206211

mupdf-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![no_std]
12
#![allow(non_upper_case_globals)]
23
#![allow(non_camel_case_types)]
34
#![allow(non_snake_case)]

mupdf-sys/wrapper.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,3 +3273,10 @@ int32_t mupdf_search_stext_page_cb(fz_context *ctx, fz_stext_page *page, const c
32733273
}
32743274
return count;
32753275
}
3276+
3277+
void mupdf_format_string(fz_context *ctx, void *user, void (*emit)(fz_context *ctx, void *user, int c), const char *fmt, ...) {
3278+
va_list ap;
3279+
va_start(ap, fmt);
3280+
fz_format_string(ctx, user, emit, fmt, ap);
3281+
va_end(ap);
3282+
}

0 commit comments

Comments
 (0)