binji / wasm-clang

Running Clang/LLD in WebAssembly Demo
https://binji.github.io/wasm-clang
Apache License 2.0
259 stars 27 forks source link

How to add a custom header file? #19

Closed kkoomen closed 6 months ago

kkoomen commented 6 months ago

Hi, I'm trying to bundle cs50.h from the cs50 library into this project. I thought, perhaps I can just do tar -xvf syroot.tar, execute cp /usr/local/include/cs50.h include/ to copy the cs50.h inside the include and do tar -cvf sysroot.tar include lib share. Unfortunately, after creating a new tar, it can't find anything anymore.

Output:

clang -cc1 -emit-obj -disable-free -isysroot / -internal-isystem /include/c++/v1 -internal-isystem /include -internal-isystem /lib/clang/8.0.1/include -ferror-limit 19 -fmessage-length 80 -fcolor-diagnostics -O2 -o test.o -x c++ test.cc
test.cc:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
Error: process exited with code 1.

Now, even when I just do tar -xvf sysroot.tar && rm sysroot.tar && tar -cvf sysroot.tar include lib share to just extract and immediately create a new tar, I immediately run into this error. My main OS is macOS, I thought the issue might be on the system itself so I created a new tar on a fresh Ubuntu VPS and then copy that tar over to my mac, but then I get the same error.

What's the tar command that was used to create the tar?

kkoomen commented 6 months ago

Okay rather than creating a new tar, I can make it work by appending to the tar:

Assuming this file strucure:

.
├── include
│   └── cs50.h
└── sysroot.tar

Then, run:

tar --append --file=sysroot.tar include/cs50.h
kkoomen commented 6 months ago

I found the problem.

https://github.com/binji/wasm-clang/blob/648c4a89997a351eef75cdaec3ef5b89d4937dec/shared.js#L617

The check here is incorrect. When I simply extract the tar and recreate it without any modifications, the this.readStr(8) returns "ustar" for me, not "ustar ". A more relaxed constraint I personally use is this:

const format = this.readStr(8);
if (!/ustar/.test(format)) {
  return null;
}