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

Conversation

Rexicon226
Copy link
Contributor

  • Passing "-lm" into the prereq isn't the correct way add the flag. This correctly adds it to the command.

  • The "rot13" example incorrectly assumed that the "rot13.h" file would be generated by the time that "main.c" was being compiled, however there is no rule supporting this and it would fail. I've also added "rot13.h" to the clean rule.

- Passing "-lm" into the prereq isn't the correct way add the flag. This correctly adds it to the command.

- The "rot13" example incorrectly assumed that the "rot13.h" file would be generated by the time that "main.c"
  was being compiled, however there is no rule supporting this and it would fail.
  I've also added "rot13.h" to the clean rule.
@sbc100 sbc100 changed the title correct wasm2c examples Correct wasm2c example Makefiles May 21, 2024

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!

Copy link
Member

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

lgtm % comment

@@ -1,702 +0,0 @@
/* Automatically generated by wasm2c */
Copy link
Member

Choose a reason for hiding this comment

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

Can you restore this file? I like to have these files checked in so that when we change the wasm2c generator we can see the effect it has on the output (as part of the diff).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@sbc100 sbc100 enabled auto-merge (squash) June 3, 2024 14:33
@sbc100 sbc100 merged commit 4beb525 into WebAssembly:main Jun 3, 2024
18 checks passed
@Rexicon226 Rexicon226 deleted the correct-examples branch June 3, 2024 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants