obelisk_component_builder/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use cargo_metadata::camino::Utf8Path;
use std::{
    path::{Path, PathBuf},
    process::Command,
};

const WASI_P2: &str = "wasm32-wasip2";
const WASM_CORE_MODULE: &str = "wasm32-unknown-unknown";

/// Build the parent activity WASM component and place it into the `target` directory.
///
/// This function must be called from `build.rs`. It reads the current package
/// name and strips the `-builder` suffix to determine the target package name.
/// Then, it runs `cargo build` with the appropriate target triple and sets
/// the `--target` directory to the output of [`get_target_dir`].
pub fn build_activity() {
    build_internal(WASI_P2, ComponentType::ActivityWasm, &get_target_dir());
}

/// Build the parent webhook endpoint WASM component and place it into the `target` directory.
///
/// This function must be called from `build.rs`. It reads the current package
/// name and strips the `-builder` suffix to determine the target package name.
/// Then, it runs `cargo build` with the appropriate target triple and sets
/// the `--target` directory to the output of [`get_target_dir`].
pub fn build_webhook_endpoint() {
    build_internal(WASI_P2, ComponentType::WebhookEndpoint, &get_target_dir());
}

/// Build the parent workflow WASM component and place it into the `target` directory.
///
/// This function must be called from `build.rs`. It reads the current package
/// name and strips the `-builder` suffix to determine the target package name.
/// Then, it runs `cargo build` with the appropriate target triple and sets
/// the `--target` directory to the output of [`get_target_dir`].
pub fn build_workflow() {
    build_internal(WASM_CORE_MODULE, ComponentType::Workflow, &get_target_dir());
}

enum ComponentType {
    ActivityWasm,
    WebhookEndpoint,
    Workflow,
}

fn to_snake_case(input: &str) -> String {
    input.replace(['-', '.'], "_")
}

fn is_transformation_to_wasm_component_needed(target_tripple: &str) -> bool {
    target_tripple == WASM_CORE_MODULE
}

/// Get the path to the target directory.
///
/// Attempts to use the `CARGO_WORKSPACE_DIR` environment variable first.
/// If not set, falls back to deriving it from the `OUT_DIR` environment variable.
/// To set the environment variable automatically, modify `.cargo/config.toml`:
/// ```toml
/// [env]
/// # remove once stable https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
/// CARGO_WORKSPACE_DIR = { value = "", relative = true }
/// ```
fn get_target_dir() -> PathBuf {
    // Try to get `CARGO_WORKSPACE_DIR` from the environment
    if let Ok(workspace_dir) = std::env::var("CARGO_WORKSPACE_DIR") {
        return Path::new(&workspace_dir).join("target");
    }
    let out_path = get_out_dir();
    // Navigate up to the `target` directory
    out_path
        .ancestors()
        .nth(4) // `out` = 0th, `<crate_name>-<hash>` = 1st, `build` = 2nd, `debug` = 3rd, `target` = 4th
        .expect("Unable to determine target directory")
        .to_path_buf()
}
/// Get the `OUT_DIR` as a `PathBuf`.
///
/// The folder structure typically looks like this: `target/debug/build/<crate_name>-<hash>/out`.
fn get_out_dir() -> PathBuf {
    PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set"))
}

fn build_internal(target_tripple: &str, component_type: ComponentType, dst_target_dir: &Path) {
    let pkg_name = std::env::var("CARGO_PKG_NAME").unwrap();
    let pkg_name = pkg_name.strip_suffix("-builder").unwrap();
    let wasm_path = run_cargo_build(dst_target_dir, pkg_name, target_tripple);
    if std::env::var("RUST_LOG").is_ok() {
        println!("cargo:warning=Built `{pkg_name}` - {wasm_path:?}");
    }

    generate_code(&wasm_path, pkg_name, component_type);

    let meta = cargo_metadata::MetadataCommand::new().exec().unwrap();
    let package = meta
        .packages
        .iter()
        .find(|p| p.name == pkg_name)
        .unwrap_or_else(|| panic!("package `{pkg_name}` must exist"));

    add_dependency(&package.manifest_path); // Cargo.toml
    for src_path in package
        .targets
        .iter()
        .map(|target| target.src_path.parent().unwrap())
    {
        add_dependency(src_path);
    }
    let wit_path = &package.manifest_path.parent().unwrap().join("wit");
    if wit_path.exists() && wit_path.is_dir() {
        add_dependency(wit_path);
    }
}

#[cfg(not(feature = "genrs"))]
fn generate_code(_wasm_path: &Path, _pkg_name: &str, _component_type: ComponentType) {}

#[cfg(feature = "genrs")]
impl From<ComponentType> for utils::wasm_tools::ComponentExportsType {
    fn from(value: ComponentType) -> Self {
        match value {
            ComponentType::ActivityWasm | ComponentType::Workflow => Self::Enrichable,
            ComponentType::WebhookEndpoint => Self::Plain,
        }
    }
}

#[cfg(feature = "genrs")]
fn generate_code(wasm_path: &Path, pkg_name: &str, component_type: ComponentType) {
    use concepts::FunctionMetadata;
    use indexmap::IndexMap;

    enum Value {
        Map(IndexMap<String, Value>),
        Leaf(Vec<String>),
    }

    fn ser_map(map: &IndexMap<String, Value>, output: &mut String) {
        for (k, v) in map {
            match v {
                Value::Leaf(vec) => {
                    for line in vec {
                        *output += line;
                        *output += "\n";
                    }
                }
                Value::Map(map) => {
                    *output += &format!("#[allow(clippy::all)]\npub mod r#{k} {{\n");
                    ser_map(map, output);
                    *output += "}\n";
                }
            }
        }
    }

    let engine = {
        let mut wasmtime_config = wasmtime::Config::new();
        wasmtime_config.wasm_component_model(true);
        wasmtime_config.async_support(true);
        wasmtime::Engine::new(&wasmtime_config).unwrap()
    };
    let mut generated_code = String::new();
    generated_code += &format!(
        "pub const {name_upper}: &str = {wasm_path:?};\n",
        name_upper = to_snake_case(pkg_name).to_uppercase()
    );

    let component =
        utils::wasm_tools::WasmComponent::new(wasm_path, &engine, Some(component_type.into()))
            .expect("cannot decode wasm component");
    generated_code += "pub mod exports {\n";
    let mut outer_map: IndexMap<String, Value> = IndexMap::new();
    for export in component.exim.get_exports_hierarchy_ext() {
        let ifc_fqn_split = export
            .ifc_fqn
            .split_terminator([':', '/', '@'])
            .map(to_snake_case);
        let mut map = &mut outer_map;
        for mut split in ifc_fqn_split {
            if split.starts_with(|c: char| c.is_numeric()) {
                split = format!("_{split}");
            }
            if let Value::Map(m) = map
                .entry(split)
                .or_insert_with(|| Value::Map(IndexMap::new()))
            {
                map = m;
            } else {
                unreachable!()
            }
        }
        let vec = export
                .fns
                .iter()
                .filter(| (_, FunctionMetadata { submittable,.. }) | *submittable )
                .map(|(function_name, FunctionMetadata{parameter_types, return_type, ..})| {
                    format!(
                        "/// {fn}: func{parameter_types}{arrow_ret_type};\npub const r#{name_upper}: (&str, &str) = (\"{ifc}\", \"{fn}\");\n",
                        name_upper = to_snake_case(function_name).to_uppercase(),
                        ifc = export.ifc_fqn,
                        fn = function_name,
                        arrow_ret_type = if let Some(ret_type) = return_type { format!(" -> {ret_type}") } else { String::new() }
                    )
                })
                .collect();
        let old_val = map.insert(String::new(), Value::Leaf(vec));
        assert!(old_val.is_none(), "same interface cannot appear twice");
    }

    ser_map(&outer_map, &mut generated_code);
    generated_code += "}\n";
    std::fs::write(get_out_dir().join("gen.rs"), generated_code).unwrap();
}

fn add_dependency(file: &Utf8Path) {
    println!("cargo:rerun-if-changed={file}");
}

fn run_cargo_build(dst_target_dir: &Path, name: &str, tripple: &str) -> PathBuf {
    let mut cmd = Command::new("cargo");
    cmd.arg("build")
        .arg("--release")
        .arg(format!("--target={tripple}"))
        .arg(format!("--package={name}"))
        .env("CARGO_TARGET_DIR", dst_target_dir)
        .env("CARGO_PROFILE_RELEASE_DEBUG", "limited") // keep debuginfo for backtraces
        .env_remove("CARGO_ENCODED_RUSTFLAGS")
        .env_remove("CLIPPY_ARGS"); // do not pass clippy parameters
    let status = cmd.status().unwrap();
    assert!(status.success());
    let name_snake_case = to_snake_case(name);
    let target = dst_target_dir
        .join(tripple)
        .join("release")
        .join(format!("{name_snake_case}.wasm",));
    assert!(target.exists(), "Target path must exist: {target:?}");
    if is_transformation_to_wasm_component_needed(tripple) {
        let target_transformed = dst_target_dir
            .join(tripple)
            .join("release")
            .join(format!("{name_snake_case}_component.wasm",));
        let mut cmd = Command::new("wasm-tools");
        cmd.arg("component")
            .arg("new")
            .arg(
                target
                    .to_str()
                    .expect("only utf-8 encoded paths are supported"),
            )
            .arg("--output")
            .arg(
                target_transformed
                    .to_str()
                    .expect("only utf-8 encoded paths are supported"),
            );
        let status = cmd.status().unwrap();
        assert!(status.success());
        assert!(
            target_transformed.exists(),
            "Transformed target path must exist: {target_transformed:?}"
        );
        // mv target_transformed -> target
        std::fs::remove_file(&target).expect("deletion must succeed");
        std::fs::rename(target_transformed, &target).expect("rename must succeed");
    }
    target
}