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

Correct wasm2c example Makefiles #2426

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion wasm2c/examples/callback/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ all: callback
clean:
rm -rf callback callback.wasm callback.c callback.h *.o

callback: main.o callback.o ../../wasm-rt-impl.o ../../wasm-rt-mem-impl.o -lm
callback: main.o callback.o ../../wasm-rt-impl.o ../../wasm-rt-mem-impl.o
$(CC) $(LDFLAGS) -o $@ $^ -lm

callback.wasm: callback.wat ../../../bin/wat2wasm
../../../bin/wat2wasm --debug-names $< -o $@
Expand Down
3 changes: 2 additions & 1 deletion wasm2c/examples/fac/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ all: fac
clean:
rm -rf fac fac.wasm fac.c *.o

fac: main.o fac.o ../../wasm-rt-impl.o -lm
fac: main.o fac.o ../../wasm-rt-impl.o
$(CC) $(LDFLAGS) -o $@ $^ -lm

fac.wasm: fac.wat ../../../bin/wat2wasm
../../../bin/wat2wasm $< -o $@
Expand Down
11 changes: 7 additions & 4 deletions wasm2c/examples/rot13/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ CFLAGS=-I../..
all: rot13

clean:
rm -rf rot13 rot13.wasm rot13.c *.o
rm -rf rot13 rot13.wasm rot13.c rot13.h *.o

rot13: main.o rot13.o ../../wasm-rt-impl.o ../../wasm-rt-mem-impl.o -lm
rot13: main.o rot13.o ../../wasm-rt-impl.o ../../wasm-rt-mem-impl.o
$(CC) $(LDFLAGS) -o $@ $^ -lm
Copy link
Member

Choose a reason for hiding this comment

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

Would it be enough to set LDFLAGS=-lm above? (i.e. can we keep the implicit rule if we do it this 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.

Like this? Afaik you can't have implicit LDFLAGS without adding it to the command.

Copy link
Member

Choose a reason for hiding this comment

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

LDFLAGS seems like it it gets added to the implicit link command:

$ touch Makefile
$ touch test.c
$ make LDFLAGS=-lxxx test
cc   -lxxx  test.c   -o test
/usr/bin/ld: cannot find -lxxx: No such file or directory

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. When I test it, I get different results:

# Use implicit rules for compiling C files.
CFLAGS=-I../..
LDFLAGS=-lm

...

rot13: main.o rot13.o ../../wasm-rt-impl.o ../../wasm-rt-mem-impl.o
	$(CC) -o $@ $^
$ make                                                                                                                                      (base)
../../../bin/wat2wasm rot13.wat -o rot13.wasm
../../../bin/wasm2c rot13.wasm -o rot13.c --disable-simd
cc -I../..   -c -o main.o main.c
cc -I../..   -c -o rot13.o rot13.c
cc -o rot13 main.o rot13.o ../../wasm-rt-impl.o ../../wasm-rt-mem-impl.o
/usr/bin/ld: rot13.o: in function `wasm_floor':
rot13.c:(.text+0x16e): undefined reference to `floor'
/usr/bin/ld: rot13.o: in function `wasm_floorf':
rot13.c:(.text+0x1ab): undefined reference to `floorf'
/usr/bin/ld: rot13.o: in function `wasm_ceil':
rot13.c:(.text+0x1ed): undefined reference to `ceil'
/usr/bin/ld: rot13.o: in function `wasm_ceilf':
rot13.c:(.text+0x22a): undefined reference to `ceilf'
/usr/bin/ld: rot13.o: in function `wasm_trunc':
rot13.c:(.text+0x26c): undefined reference to `trunc'
/usr/bin/ld: rot13.o: in function `wasm_truncf':
rot13.c:(.text+0x2a9): undefined reference to `truncf'
/usr/bin/ld: rot13.o: in function `wasm_nearbyintf':
rot13.c:(.text+0x2e6): undefined reference to `nearbyintf'
/usr/bin/ld: rot13.o: in function `wasm_nearbyint':
rot13.c:(.text+0x328): undefined reference to `nearbyint'
/usr/bin/ld: rot13.o: in function `wasm_sqrt':
rot13.c:(.text+0x41c): undefined reference to `sqrt'
/usr/bin/ld: rot13.o: in function `wasm_sqrtf':
rot13.c:(.text+0x459): undefined reference to `sqrtf'
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: rot13] Error 1

The flag doesn't get passed into the cc call. Maybe I'm missing something 😄

Copy link
Member

Choose a reason for hiding this comment

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

You would also need to remove the action itself. If you include the explicit link command you also need to include the explicit LDFLAGS. Hopefully you can remove it and let the command be implict

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sbc100 Sorry for disappearing for a couple of weeks, I had exams.

I looked back into this and I'm running into an issue I can't get past.
Using:

# Use implicit rules for compiling C files.
CFLAGS=-I../..
LDFLAGS=-lm

all: fac

clean:
	rm -rf fac fac.wasm fac.c *.o

fac: main.o fac.o ../../wasm-rt-impl.o

fac.wasm: fac.wat ../../../bin/wat2wasm
	../../../bin/wat2wasm $< -o $@

fac.c: fac.wasm ../../../bin/wasm2c
	../../../bin/wasm2c $< -o $@ --disable-simd

.PHONY: all clean

without the action, I get the almost correct:

cc -lm  fac.o main.o ../../wasm-rt-impl.o   -o fac

however this still leads to undefined references because -lm is passed before the file arguments.

Any idea on how to solve this?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, that works!


main.o: rot13.h

rot13.wasm: rot13.wat ../../../bin/wat2wasm
../../../bin/wat2wasm $< -o $@

rot13.c: rot13.wasm ../../../bin/wasm2c
../../../bin/wasm2c $< -o $@ --disable-simd
rot13.c rot13.h: rot13.wasm ../../../bin/wasm2c
../../../bin/wasm2c $< -o rot13.c --disable-simd

.PHONY: all clean
5 changes: 3 additions & 2 deletions wasm2c/examples/threads/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ LDFLAGS=${SANITIZERS} -pthread

all: threads

threads: threads.o sample.o ../../wasm-rt-impl.o ../../wasm-rt-exceptions-impl.o -lm

clean:
rm -rf threads sample.wasm sample.c sample.h *.o ../../*.o

threads: threads.o sample.o ../../wasm-rt-impl.o ../../wasm-rt-exceptions-impl.o
$(CC) $(LDFLAGS) -o $@ $^ -lm

sample.wasm: sample.wat ../../../bin/wat2wasm
../../../bin/wat2wasm --debug-names $< -o $@

Expand Down
Loading