tree_sitter_wasm_build_tool/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(rust_2018_idioms)]
3#![deny(missing_docs)]
4
5use std::{env, error::Error, fs, path::PathBuf};
6
7macro_rules! copy_file {
8    ($sysroot:ident, $name:literal) => {
9        fs::write(
10            $sysroot.join($name),
11            include_bytes!(concat!("../wasm-sysroot/", $name)),
12        )?;
13    };
14}
15
16/// Add the required C header files to the given instance of [`cc::Build`], to allow compilation of
17/// tree-sitter parsers to the `wasm32-unknown-unknown` target.
18pub fn add_wasm_headers(config: &mut cc::Build) -> Result<(), Box<dyn Error>> {
19    let out_dir = PathBuf::from(env::var("OUT_DIR")?);
20
21    let sysroot_dir = out_dir.join("sysroot");
22    if env::var("TARGET")? == "wasm32-unknown-unknown" {
23        fs::create_dir_all(&sysroot_dir)?;
24
25        copy_file!(sysroot_dir, "stdint.h");
26        copy_file!(sysroot_dir, "stdlib.h");
27        copy_file!(sysroot_dir, "stdio.h");
28        copy_file!(sysroot_dir, "stdbool.h");
29        copy_file!(sysroot_dir, "wctype.h");
30
31        config.include(&sysroot_dir);
32
33        copy_file!(out_dir, "wctype.c");
34        config.file(out_dir.join("wctype.c"));
35    }
36
37    Ok(())
38}