Skip to content

Wasm examples require wasi-libc to compile from dev builds #923

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

Closed
justinclift opened this issue Feb 26, 2020 · 18 comments
Closed

Wasm examples require wasi-libc to compile from dev builds #923

justinclift opened this issue Feb 26, 2020 · 18 comments
Labels
docs Documentation needed

Comments

@justinclift
Copy link
Member

Discovered a problem (and solution) earlier on today, when attempting to compile the TinyGo wasm examples in under the src/examples/wasm directory:

$ cd src/examples/wasm
$ make main
rm -rf ./html
mkdir ./html
cp ../../../targets/wasm_exec.js ./html/
tinygo build -o ./html/wasm.wasm -target wasm -no-debug ./main/main.go
tinygo:wasm-ld: error: cannot open /home/jc/go/src/github.com/tinygo-org/tinygo/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a: No such file or directory
error: failed to link /tmp/tinygo685543697/main: failed to link using built-in wasm-ld
make: *** [Makefile:23: main] Error 1

The problem turned out to be that the wasi-libc submodule needs to be (manually) compiled first, before the compiling the wasm examples:

$ cd lib/wasi-libc
$ make WASM_CC=~/go/src/github.com/tinygo-org/tinygo/llvm-build/bin/clang \
  WASM_AR=~/go/src/github.com/tinygo-org/tinygo/llvm-build/bin/llvm-ar \
  WASM_NM=~/go/src/github.com/tinygo-org/tinygo/llvm-build/bin/llvm-nm

This compile step creates the libc.a file being complained about in the initial error.

The proper solution to this might be something like including this in the top level Makefile, so it's done automatically during the normal TinyGo compilation.

@aykevl
Copy link
Member

aykevl commented Feb 27, 2020

This is only a problem with local builds of TinyGo. Release builds have everything set up correctly.

You can run make wasi-libc in the TinyGo root directory, after possibly setting WASM_CC etc. #928 should make this easier.

@aykevl
Copy link
Member

aykevl commented Feb 28, 2020

Maybe this should be included in the documentation? For example, just after https://tinygo.org/getting-started/linux/#additional-requirements-for-microcontrollers.

Another option could be to build it when needed, similar to how compiler-rt is currently built when needed. That depends on #871 however.

@justinclift
Copy link
Member Author

Adding it to the docs is probably a good starting point. 😄

@deadprogram
Copy link
Member

Agreed that this needs to be in the docs.

@deadprogram deadprogram changed the title Wasm examples no longer compile Wasm examples require wasi-libc to compile from dev builds Mar 16, 2020
@deadprogram deadprogram added the docs Documentation needed label Mar 16, 2020
@deadprogram
Copy link
Member

I think this has now been addressed, so closing. Please reopen if needed, thanks!

@tve
Copy link

tve commented Jul 28, 2020

How has this been addressed? I'm running arch linux (manjaro) and followed the getting started > linux > arch directions, which boil down to installing the tinygo-bin AUR package. The result is:

> tinygo build -o hello-world.wasm -target=wasm hello-world.go
tinygo:wasm-ld: error: /usr/lib/tinygo/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a: archive has no
index; run ranlib to add one
failed to run tool: wasm-ld

Not sure what to do next...

@justinclift
Copy link
Member Author

@tve In theory, running make wasi-libc should set up the WASI piece for you. At least, it did when this issue was filed. 😄

@tve
Copy link

tve commented Jul 28, 2020

After something like pamac tinygo-bin where would I type make wasi-libc?
FYI, there's a tinygo package https://www.archlinux.org/packages/?sort=&q=tinygo&maintainer=&flagged= that works correctly.

@justinclift
Copy link
Member Author

justinclift commented Jul 28, 2020

Good question. 😁

My first thought was the tinygo package might need updating to include the WASI step in it's build process.

However, the file list for it already includes compiled wasi files:

https://www.archlinux.org/packages/community/x86_64/tinygo/files/

(eg search for wasi-libc/sysroot/lib/wasm32-wasi to see them)

Now I'm kind of wondering if there's a different step the package still needs to do, so the wasi pieces were found.

Note that I'm not that familiar with Arch personally. 🤷

@justinclift
Copy link
Member Author

Reading over your error message again:

tinygo:wasm-ld: error: /usr/lib/tinygo/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a: archive has no
index; run ranlib to add one
failed to run tool: wasm-ld

Maybe the /usr/lib/tinygo/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a file included in the package wasn't created 100% correctly, and running the ranlib command (as per error message) on it might help?

@justinclift
Copy link
Member Author

Doing a general search online for that error message is throwing up lots of possibilities.

I'd tend to explore those next, unless someone with good idea(s) chimes in here. 😄

@justinclift
Copy link
Member Author

This looks relevant: emscripten-core/emscripten#9329 (comment)

@aykevl
Copy link
Member

aykevl commented Jul 29, 2020

This looks like a problem with the package. This should work out of the box, no modification needed.

@johanbrandhorst do you have an idea what's going on?

@johanbrandhorst
Copy link
Member

The tinygo-bin package just downloads the latest release binary, it doesn't attempt to build anything. It's possible it's missing some runtime dependencies? It currently declares gcc-libs and go dependencies, should we be requiring anything else to run the rebuilt binaries?

@aykevl
Copy link
Member

aykevl commented Jul 30, 2020

Strange that it fails just on Arch. @tve to be absolutely sure the packaging didn't mess something up, can you try whether the prebuilt binaries from the release page don't have this error?
https://github.com/tinygo-org/tinygo/releases/tag/v0.13.1

@tcastelly
Copy link

Thank you @aykevl
I'm Archlinux user with the same error. Build my app from the downloaded release works for me.

Strange that it fails just on Arch. @tve to be absolutely sure the packaging didn't mess something up, can you try whether the prebuilt binaries from the release page don't have this error?
https://github.com/tinygo-org/tinygo/releases/tag/v0.13.1

@aykevl
Copy link
Member

aykevl commented May 5, 2021

Okay, then I would consider this a bug in the tinygo Arch Linux package, not in TinyGo itself.

@johanbrandhorst
Copy link
Member

@tcastelly could you figure out the error and report it on the relevant package? I'd be happy to take contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation needed
Projects
None yet
Development

No branches or pull requests

6 participants