Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake compatibility to c-api #4369

Merged
merged 18 commits into from
Jul 22, 2022
Merged

Add cmake compatibility to c-api #4369

merged 18 commits into from
Jul 22, 2022

Conversation

TheGreatRambler
Copy link
Contributor

This PR adds CMake compatibility to the c-api, similar to that in wasm-tools. The examples are also modified to both demonstrate usage of CMake and are built by CI to verify the c-api is compiling properly.

@github-actions github-actions bot added wasmtime:c-api Issues pertaining to the C API. wasmtime:docs Issues related to Wasmtime's documentation labels Jul 2, 2022
@github-actions
Copy link

github-actions bot commented Jul 2, 2022

Subscribe to Label Action

cc @peterhuene

This issue or pull request has been labeled: "wasmtime:c-api", "wasmtime:docs"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:c-api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@TheGreatRambler
Copy link
Contributor Author

error: failed to open: /home/runner/work/wasmtime/wasmtime/target/release/.cargo-lock This PR couldn't have caused that CI error

@TheGreatRambler
Copy link
Contributor Author

TheGreatRambler commented Jul 3, 2022

error occurred: Failed to find tool. Is aarch64-linux-gnu-gcc installed? how should I go about fixing this?

examples/CMakeLists.txt Outdated Show resolved Hide resolved
-I crates/c-api/include \
-I crates/c-api/wasm-c-api/include \
target/release/libwasmtime.a \
-lpthread -ldl -lm \
Copy link
Contributor

@bjorn3 bjorn3 Jul 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing: I believe you have to use -pthread instead of -lpthread to be portable across UNIXes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a copy and paste from serialize.c, which uses -lpthread, copied so cmake could be mentioned, I could change the rest to be consistent if you'd like

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't think it's worth changing -lpthread to -pthread until somebody reports that it doesn't work for them.

Copy link
Contributor

@bjorn3 bjorn3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not familiar with cmake, so someone else should take a look too.

@alexcrichton
Copy link
Member

Thanks for this! This seems reasonable enough to me. Could this be used to replace and/or modify the existing run-examples crate run on CI? Additionally we ideally wouldn't add any more time to CI since building, especially in release mode, is a bit hefty. Could this ensure that the crates are only built in CI once?

@TheGreatRambler
Copy link
Contributor Author

I've moved compilation to the run-examples crate and verified it worked, the CI seems stuck on my end so not sure how well it will work here

@TheGreatRambler
Copy link
Contributor Author

CI failed due to a lack of disk space. If it becomes a problem in the future I found this script from Apache that could help, it just removes unnecessary packages from the runner.

@TheGreatRambler
Copy link
Contributor Author

I don't even know what to do to fix this CI error. I'm just going to give it a break for now

@TheGreatRambler
Copy link
Contributor Author

Can anyone provide any tips for why the externref example works perfectly fine on my machine but fails in the CI? I clean the build folder to save disk space, but that should only impact the examples/build folder, externref.wat isn't stored there

@TheGreatRambler
Copy link
Contributor Author

Still running out of storage? I... I don't even know, I'm going to revert the tests and test from there

@TheGreatRambler
Copy link
Contributor Author

TheGreatRambler commented Jul 19, 2022

Could someone (if this is possible) manually restart the CI and/or give any tips on how to fix this final issue? It has to do with CI / Test (windows-2019) in particular hanging on linking.

@alexcrichton
Copy link
Member

I don't know CMake on Windows really at all, but that looks like some form of a legitimate loop of some kind.

Copy link
Contributor

@jameysharp jameysharp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this a lot. I have a few fixes to request but I'd like to see this merged.

I spent a few minutes staring at the CI failure on Windows-2019 but not much jumped out at me. The one thing I noticed is that the timestamps are surprising: the output from the example, starting at "Initializing...", shows up all within the same second as the message "error: process didn't exit successfully". But that's hours after the last message from the linker. So I'm wondering if the output is getting buffered somewhere, and the buffer is flushed on ctrl-c.

I've triggered a re-run of the failing job with debug logging enabled, so maybe that will tell us something new.

examples/CMakeLists.txt Outdated Show resolved Hide resolved
crates/misc/run-examples/src/main.rs Show resolved Hide resolved
crates/misc/run-examples/src/main.rs Outdated Show resolved Hide resolved
crates/misc/run-examples/src/main.rs Outdated Show resolved Hide resolved
-I crates/c-api/include \
-I crates/c-api/wasm-c-api/include \
target/release/libwasmtime.a \
-lpthread -ldl -lm \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't think it's worth changing -lpthread to -pthread until somebody reports that it doesn't work for them.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To confirm this isn't building more in CI now than we currently are? This PR's linux run is 10 minutes longer than main last linux run which could be variance but that seems pretty large.

.github/actions/free-disk-space/LICENSE Outdated Show resolved Hide resolved
@@ -1,36 +1,74 @@
use anyhow::Context;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to remove this crate entirely? It seems unfortunate to have both this runner script for CMake plus all the CMake support. I was hoping that this run-examples could be entirely deleted in favor of some CMake scripting. I don't know much about CMake myself though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting aside the C examples for a second, what would that look like for the Rust examples? Some of the examples have nested Rust subprojects that need to be built first to get a .wasm blob, and I couldn't find a way to make cargo run --example trigger that build first. I also don't see a way to ask cargo to run all examples, just a specific one by name.

My guess is that there's still value in this run-examples crate, so I'd like to merge this PR. If we figure out how to replace what it does with a tiny shell script or something, that'd be great as another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cmake would be able to build, it would look similar to the below

include(ExternalProject)
ExternalProject_Add(
	wasmtime-crate
	DOWNLOAD_COMMAND ""
	CONFIGURE_COMMAND ""
	INSTALL_COMMAND "${WASMTIME_INSTALL_COMMAND}"
	BUILD_COMMAND cargo build ${WASMTIME_BUILD_TYPE_FLAG}
	BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}
	BUILD_ALWAYS ON)
add_library(wasmtime INTERFACE)
add_dependencies(wasmtime wasmtime-crate)

And actually it appears you can test with cmake, I've never done it but it can be done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had glanced at CTest while I was reviewing this PR earlier and at a very brief glance I didn't think it was meant for this kind of use: it seemed like it was more about running a test suite, with the usual facilities for reporting number of passing/failing tests and so on. I assumed that would be a poor fit, but I could be wrong.

I think it'd be unfortunate to require people to use CMake if they just want to try the Rust examples, so I'm skeptical about building the wasm blobs that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just added integration with ctest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, that was simpler than I expected. 😆 Nice!

I see this version fails on Windows because -j4 isn't supported there. It's cool that you can run the examples in parallel but I'd also be happy without that optimization.

@jameysharp
Copy link
Contributor

A little over 2 minutes of that difference was the free-disk-space action, which makes sense to me. But the biggest difference was an extra 4+ minutes in run-tests.sh, which I really think shouldn't have been affected by this PR at all. Other steps were also a little longer, so I think that particular CI job runner was just more heavily loaded.

That said, I would also like to know if removing the free-disk-space action passes CI now. I didn't get a look at the failure mode before you added it so I'm not sure what to expect. But given that the Windows tests seem to have magically fixed themselves (right? you didn't change anything that should explain that?) I'm wondering if this PR just got extraordinarily unlucky with its CI runs.

@TheGreatRambler
Copy link
Contributor Author

free-disk-space was an unneccesary action I pulled in when I kept running out of space during tests. Turns out it was because I was pulling in the full rust toolchain rather than the minimal one, so it should be unneccesary.

@TheGreatRambler
Copy link
Contributor Author

As for the Windows builds passing this time, it could be dumb luck but I actually reduced the warnings msvc reports to /W3 (the default) just in case, it may have helped by reducing stdout.

Copy link
Contributor

@jameysharp jameysharp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, in my opinion if you remove the .github changes and still CI passes, then this should be good to go. In reviewing once more I also noticed one style nit that'd be nice to clean up while you're at it.

examples/README.md Outdated Show resolved Hide resolved
@jameysharp
Copy link
Contributor

The new problem seems to be that the examples which rely on wasm blobs can't find them when run under CTest. The macos build is the one that failed but I think it just got to that point before any of the other job runners did, since I don't see any reason why it should be Mac-specific. Do you need to change how you set the current working directory for each test, perhaps?

@TheGreatRambler
Copy link
Contributor Author

It has to do with the order in which I build the tests, since wasm modules are built for some c tests to run. Now the new problem is msbuild not taking the ctest arguments, and it sort of needs to accept them, without --output-on-failure you can't diagnose issues.

@TheGreatRambler
Copy link
Contributor Author

TheGreatRambler commented Jul 22, 2022

Of COURSE it got stuck again, this PR is taking so much of my time lol (I wish Github let me see the command line output so I can debug better)

@jameysharp
Copy link
Contributor

I've cancelled and restarted the windows-2019 job; 4+ hours in, it clearly wasn't going to work. I wish I had any good guesses about what's going on; I'd assumed there might be something weird in the run-examples crate, but that's gone now and we're still seeing this hang in CI. You have my sympathy for how painful this has been!

I don't know much about cmake but I see there's a TIMEOUT property that you can set on a test. Maybe you can set a timeout on all the tests and at least get it to fail faster? I think 30 seconds ought to be more than enough time when things are working okay.

Ooh, I see you have a new patch on the branch while I was typing this. I'll be curious to see if that fixes things.

@jameysharp
Copy link
Contributor

Hey, windows-2019 got past the point where it had been stuck! Although I see my suggestion of a 30-second timeout would have been too short for epochs-rust and tokio-rust, which took closer to 120 seconds on Windows and 70 seconds on Ubuntu. So I guess 300 seconds might be a better choice, if you should decide to set timeouts.

@TheGreatRambler
Copy link
Contributor Author

It was getting stuck because both memory.c and multimemory.c read 8 bytes into a variable on the stack rather than 4, which I guess the old testing didn't catch. Cmake catches exceptions and return codes, so it just got silently stuck. Ideally everything is fine now

@jameysharp
Copy link
Contributor

Alex and I are dismayed that our CI didn't catch that bug sooner; apparently we should start using -Werror in CI or something. That's not a problem for this PR though, which I'm merging now. Thank you for all your effort getting this through!

@jameysharp jameysharp merged commit 2ba3025 into bytecodealliance:main Jul 22, 2022
@TheGreatRambler TheGreatRambler deleted the cmake-c-api branch July 29, 2022 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:c-api Issues pertaining to the C API. wasmtime:docs Issues related to Wasmtime's documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants