build: move `generate-wasm-exports-lists to xtask

This commit is contained in:
Amaan Qureshi 2024-10-26 18:46:57 -04:00
parent c8cf75fd30
commit c7d6fd7fa5
3 changed files with 57 additions and 9 deletions

View file

@ -1,8 +0,0 @@
#!/usr/bin/env bash
set -e
while read -r wasm_file; do
wasm-objdump --details "$wasm_file" --section Import | \
sed -n 's/.*<env\.\([A-Za-z0-9_]*\)>.*/\1/p'
done < <(find target -maxdepth 2 -name 'tree-sitter-*.wasm') | sort -u

View file

@ -1,4 +1,4 @@
use std::{ffi::OsStr, fs, process::Command};
use std::{collections::BTreeSet, ffi::OsStr, fs, path::Path, process::Command};
use anyhow::{Context, Result};
@ -99,6 +99,59 @@ pub fn run_bindings() -> Result<()> {
.with_context(|| "Failed to write bindings")
}
pub fn run_wasm_exports() -> Result<()> {
let mut imports = BTreeSet::new();
let mut callback = |path: &str| -> Result<()> {
let output = Command::new("wasm-objdump")
.args(["--details", path, "--section", "Import"])
.output()?;
bail_on_err(&output, "Failed to run wasm-objdump")?;
let output = String::from_utf8_lossy(&output.stdout);
for line in output.lines() {
if let Some(imp) = line.split("<env.").nth(1).and_then(|s| s.split('>').next()) {
imports.insert(imp.to_string());
}
}
Ok(())
};
for entry in fs::read_dir(Path::new("target"))? {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
if path.is_dir() {
for entry in fs::read_dir(&path)? {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
if path.is_file()
&& path.extension() == Some(OsStr::new("wasm"))
&& path
.file_name()
.unwrap()
.to_str()
.unwrap()
.starts_with("tree-sitter-")
{
callback(path.to_str().unwrap())?;
}
}
}
}
for imp in imports {
println!("{imp}");
}
Ok(())
}
fn find_grammar_files(
dir: &str,
) -> impl Iterator<Item = Result<std::path::PathBuf, std::io::Error>> {

View file

@ -34,6 +34,8 @@ enum Commands {
GenerateBindings,
/// Generates the fixtures for testing tree-sitter.
GenerateFixtures(GenerateFixtures),
/// Generate the list of exports from Tree-sitter WASM files.
GenerateWasmExports,
/// Run the test suite
Test(Test),
/// Run the WASM test suite
@ -197,6 +199,7 @@ fn run() -> Result<()> {
Commands::GenerateFixtures(generate_fixtures_options) => {
generate::run_fixtures(&generate_fixtures_options)?;
}
Commands::GenerateWasmExports => generate::run_wasm_exports()?,
Commands::Test(test_options) => test::run(&test_options)?,
Commands::TestWasm => test::run_wasm()?,
Commands::UpgradeWasmtime(upgrade_wasmtime_options) => {