Repository: bytecodealliance/wasmtime
Pull request #8960 (2024-07-15 21:02:12)

Update the docs for building the C API
Suggest `cmake` since that is what we test in CI and keep working.

<!--
Please make sure you include the following information:

- If this work has been discussed elsewhere, please include a link to that
  conversation. If it was discussed in an issue, just mention "issue #...".

- Explain why this change is needed. If the details are in an issue already,
  this can be brief.

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->



Patch:
diff --git a/crates/c-api/README.md b/crates/c-api/README.md
index 370be40e6..8f093855a 100644
--- a/crates/c-api/README.md
+++ b/crates/c-api/README.md
@@ -1,17 +1,42 @@
 # Wasmtime's C API
 
-For more information you can find the documentation for this library
-[online](https://bytecodealliance.github.io/wasmtime/c-api/).
+## API Documentation
+
+[The API documentation for the Wasmtime C library is hosted
+here.](https://bytecodealliance.github.io/wasmtime/c-api/).
 
 ## Using in a C Project
 
-To use Wasmtime from a C or C++ project, you can use Cargo to build the Wasmtime C bindings. From the root of the Wasmtime repository, run the following command:
+### Using a Pre-Built Static or Dynamic Library
+
+Each release on Wasmtime's [GitHub Releases
+page](https://github.com/bytecodealliance/wasmtime/releases) has pre-built
+binaries for both static and dynamic libraries for a variety of architectures
+and operating systems attached, as well as header files you can include.
+
+### Building Wasmtime's C API from Source
+
+To use Wasmtime from a C or C++ project, you must have
+[CMake](https://cmake.org/) and [a Rust
+toolchain](https://www.rust-lang.org/tools/install) installed.
+
+From the root of the Wasmtime repository, run the following commands:
 
 ```
-cargo build --release -p wasmtime-c-api
+$ cmake -S crates/c-api -B target/c-api --install-prefix "$(pwd)/artifacts"
+$ cmake --build target/c-api
+$ cmake --install target/c-api
 ```
 
-This will create static and dynamic libraries called `libwasmtime` in the `target/release` directory.
+These commands will produce the following files:
+
+* `artifacts/lib/libwasmtime.{a,lib}`: Static Wasmtime library. Exact extension
+  depends on your operating system.
+
+* `artifacts/lib/libwasmtime.{so,dylib,dll}`: Dynamic Wasmtime library. Exact
+  extension depends on your operating system.
+
+* `artifacts/include/**.h`: Header files for working with Wasmtime.
 
 ## Using in a Rust Project
 
diff --git a/docs/contributing-building.md b/docs/contributing-building.md
index 11c6593a6..9387d2b56 100644
--- a/docs/contributing-building.md
+++ b/docs/contributing-building.md
@@ -54,16 +54,9 @@ with `cargo run`.
 
 ## Building the Wasmtime C API
 
-To build the C API of Wasmtime you can run:
-
-```shell
-cargo build --release -p wasmtime-c-api
-```
-
-This will place the shared library inside of `target/release`. On Linux it will
-be called `libwasmtime.{a,so}`, on macOS it will be called
-`libwasmtime.{a,dylib}`, and on Windows it will be called
-`wasmtime.{lib,dll,dll.lib}`.
+See
+[`crates/c-api/README.md`](https://github.com/bytecodealliance/wasmtime/blob/main/crates/c-api/README.md)
+for details.
 
 ## Building Other Wasmtime Crates
 
diff --git a/src/commands/run.rs b/src/commands/run.rs
index e5b3b816a..3aa9a4b4f 100644
--- a/src/commands/run.rs
+++ b/src/commands/run.rs
@@ -54,6 +54,15 @@ pub struct RunCommand {
     )]
     pub preloads: Vec<(String, PathBuf)>,
 
+    /// Override the value of `argv[0]`, typically the name of the executable of
+    /// the application being run.
+    ///
+    /// This can be useful to pass in situations where a CLI tool is being
+    /// executed that dispatches its functionality on the value of `argv[0]`
+    /// without needing to rename the original wasm binary.
+    #[arg(long)]
+    pub argv0: Option<String>,
+
     /// The WebAssembly module to run and arguments to pass to it.
     ///
     /// Arguments passed to the wasm module will be configured as WASI CLI
@@ -233,7 +242,10 @@ impl RunCommand {
             // For argv[0], which is the program name. Only include the base
             // name of the main wasm module, to avoid leaking path information.
             let arg = if i == 0 {
-                Path::new(arg).components().next_back().unwrap().as_os_str()
+                match &self.argv0 {
+                    Some(s) => s.as_ref(),
+                    None => Path::new(arg).components().next_back().unwrap().as_os_str(),
+                }
             } else {
                 arg.as_ref()
             };


Test patch:
diff --git a/crates/test-programs/src/bin/cli_argv0.rs b/crates/test-programs/src/bin/cli_argv0.rs
new file mode 100644
index 000000000..d64562fb8
--- /dev/null
+++ b/crates/test-programs/src/bin/cli_argv0.rs
@@ -0,0 +1,4 @@
+fn main() {
+    let mut args = std::env::args();
+    assert_eq!(args.next(), args.next());
+}
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index bba532d9a..41754789c 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -1845,6 +1845,14 @@ stderr [1] :: after empty
 
         Ok(())
     }
+
+    #[test]
+    fn cli_argv0() -> Result<()> {
+        run_wasmtime(&["run", "--argv0=a", CLI_ARGV0, "a"])?;
+        run_wasmtime(&["run", "--argv0=b", CLI_ARGV0_COMPONENT, "b"])?;
+        run_wasmtime(&["run", "--argv0=foo.wasm", CLI_ARGV0, "foo.wasm"])?;
+        Ok(())
+    }
 }
 
 #[test]