w2c2 icon indicating copy to clipboard operation
w2c2 copied to clipboard

Porting go apps to powerpc-darwin

Open barracuda156 opened this issue 6 months ago • 12 comments

@turbolent Hi, did you try porting Go apps to macOS on powerpc? It seems that Go supports wasm and can produce self-contained executables. Is it then just as easy as to throw .wasm file at the compiler, get a .c translate and then compile that with gcc, normally?

P. S. Porting Go itself would be fun, but perhaps not worth the effort. But having apps like rclone and gh working would be awesome.

barracuda156 avatar Jun 28 '25 04:06 barracuda156

Update. May not work, at least according to Claude, Go does not produce an appropriate wasm for this scenario, and would require Go runtime for WASM which expects JS host. Alas, no magical solution, we are still with the totally broken Go LOL.

barracuda156 avatar Jun 28 '25 08:06 barracuda156

Don't trust LLMs ;-)

It is definitely possible to use w2c2 to compile WebAssembly binaries built using the Go compiler to C. I've opened #119 to add a simple example that demonstrates it. The resulting C program should also run on Darwin/PowerPC.

turbolent avatar Jun 28 '25 19:06 turbolent

Thank you, awesome! I will try then.

barracuda156 avatar Jun 28 '25 19:06 barracuda156

By the way, does it matter if wasm to c is done on target machine or not? I.e. I can only run Go on arm64 or x86, but I can [presumably] run w2c2 on powerpc too.

barracuda156 avatar Jun 28 '25 19:06 barracuda156

No, translation is both host and target independent, the generated C code is portable and should run as-is on the operating systems and architectures listed in the README.

turbolent avatar Jul 04 '25 23:07 turbolent

@turbolent What should be the MODE on macOS? Default gnu-ld fails:

svacchanda@43-8 go-wasi % make
/opt/local/bin/w2c2 -d gnu-ld -f 100 gowasi.wasm gowasi.c
w2c2: skipping custom section 'go:buildid' (size 103)
w2c2: skipping custom section 'producers' (size 61)
cc -I/opt/local/include/w2c2 -O0 -g   -c -o gowasi.o gowasi.c
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f Submake CC="cc" CFLAGS="-I/opt/local/include/w2c2 -O0 -g" LDFLAGS="-lm -L/opt/local/lib -lw2c2wasi" OBJS="datasegments.o"
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f Submake main.o s0000000000.o s0000000001.o s0000000002.o s0000000003.o s0000000004.o s0000000005.o s0000000006.o s0000000007.o s0000000008.o s0000000009.o s0000000010.o s0000000011.o s0000000012.o s0000000013.o s0000000014.o datasegments.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c main.c -o main.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000000.c -o s0000000000.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000001.c -o s0000000001.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000002.c -o s0000000002.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000003.c -o s0000000003.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000004.c -o s0000000004.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000005.c -o s0000000005.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000006.c -o s0000000006.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000007.c -o s0000000007.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000008.c -o s0000000008.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000009.c -o s0000000009.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000010.c -o s0000000010.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000011.c -o s0000000011.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000012.c -o s0000000012.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000013.c -o s0000000013.o
cc -I.. -I/opt/local/include/w2c2 -O0 -g -c s0000000014.c -o s0000000014.o
ld -r -b binary -o datasegments.o datasegments
ld: warning: -b is obsolete
ld: warning: option -b is obsolete and being ignored
ld: file not found: binary
make[2]: *** [datasegments.o] Error 1
make[1]: *** [go-wasi] Error 2
make: *** [go-wasi] Error 2

Update. Apparently sectcreate1 works (and perhaps sectcreate2 when on 10.6?).

barracuda156 avatar Jul 07 '25 18:07 barracuda156

Unfortunately, real-life attempts fail already on compiling to WASM (i.e. unrelated to w2c2):

https://github.com/rclone/rclone/issues/7831

https://github.com/jesseduffield/lazygit/issues/4712

barracuda156 avatar Jul 07 '25 19:07 barracuda156

Don't trust LLMs ;-)

It is definitely possible to use w2c2 to compile WebAssembly binaries built using the Go compiler to C. I've opened #119 to add a simple example that demonstrates it. The resulting C program should also run on Darwin/PowerPC.

Is this issue fixed? Or are you referring to tinygo instead of go? iirc Go uses poll_oneoff

gkgoat1 avatar Jul 08 '25 19:07 gkgoat1

Unfortunately, real-life attempts fail already on compiling to WASM (i.e. unrelated to w2c2):

rclone/rclone#7831

jesseduffield/lazygit#4712

Yeah, there are not many stand-alone programs that can be built to WASM/WASI.

turbolent avatar Jul 08 '25 23:07 turbolent

Is this issue fixed?

No, #109 is not done yet, poll_oneoff is not implemented yet (this is not a bug, but a missing feature).

Or are you referring to tinygo instead of go? iirc Go uses poll_oneoff

I was not referring to tinygo, but the reference Go compiler. Though a WASM binary might have a specific import, like poll_oneoff, that does not mean it is actually used. The Go example in examples/go-wasi shows for example that a hello world does not actually need poll_oneof.

turbolent avatar Jul 08 '25 23:07 turbolent

Possibly once https://github.com/golang/go/issues/64856 issue is fixed, rclone gonna build. See: https://github.com/rclone/rclone/issues/7831#issuecomment-3047843281

barracuda156 avatar Jul 09 '25 06:07 barracuda156

@barracuda156 I wonder if you might have better success compiling your programs to wasm using container2wasm and then with w2c2.

coder0107git avatar Jul 09 '25 21:07 coder0107git