From a2bdc56efa3fa5c4f62e2cf11a44d1cbfa5c362e Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 12 Sep 2024 15:10:44 -0700 Subject: [PATCH] Add `version.h` defining `WASI_LIBC_VERSION` This change re-imagines [wasi-sdk#487] entirely in `wasi-libc`. Instead of generating a `version.h` header late--in `wasi-sdk`--it is generated immediately when `wasi-libc` is built. This has the disadvantage that we don't know what `wasi-sdk` this will be used in... but almost: when releasing `wasi-sdk`, we _should_ be tagging the `wasi-libc` repository. If we did that more faithfully ([docs]), this commit would generate: ```c // Generated by wasi-lib's `Makefile`. #ifndef VERSION_H #define VERSION_H #define WASI_LIBC_VERSION "wasi-sdk-24" #endif ``` Using the magic of `git-describe`, if someone is using `wasi-libc` directly, they may see a version like: `wasi-sdk-22-19-g5d3c5e9-dirty`. This should be read like: `-<# commits since tag>--`. [wasi-sdk#487]: https://github.com/WebAssembly/wasi-sdk/pull/487 [docs]: https://github.com/WebAssembly/wasi-sdk/blob/754aec3/RELEASING.md?plain=1#L22 --- Makefile | 9 +++++++++ expected/wasm32-wasip1-threads/include-all.c | 1 + expected/wasm32-wasip1-threads/predefined-macros.txt | 1 + expected/wasm32-wasip1/include-all.c | 1 + expected/wasm32-wasip1/predefined-macros.txt | 1 + expected/wasm32-wasip2/include-all.c | 1 + expected/wasm32-wasip2/predefined-macros.txt | 1 + libc-bottom-half/headers/public/wasi/version.h.in | 5 +++++ 8 files changed, 20 insertions(+) create mode 100644 libc-bottom-half/headers/public/wasi/version.h.in diff --git a/Makefile b/Makefile index 174115e2..2345a814 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,8 @@ endif BUILTINS_LIB ?= $(shell ${CC} ${CFLAGS} --print-libgcc-file-name) +LIBC_GIT_REF := $(shell git describe --tags --dirty) + # These variables describe the locations of various files and directories in # the source tree. DLMALLOC_DIR = dlmalloc @@ -755,6 +757,12 @@ include_dirs: mkdir -p "$(SYSROOT_INC)" cp -r "$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)" + # Generate the version.h header (and remove the template). + rm -f "$(SYSROOT_INC)/wasi/version.h.in" + sed 's/{{VERSION}}/$(LIBC_GIT_REF)/' \ + $(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)/wasi/version.h.in \ + > "$(SYSROOT_INC)/wasi/version.h" + # Generate musl's bits/alltypes.h header. mkdir -p "$(SYSROOT_INC)/bits" sed -f $(LIBC_TOP_HALF_MUSL_DIR)/tools/mkalltypes.sed \ @@ -957,6 +965,7 @@ check-symbols: startup_files libc | grep -v '^#define __OPTIMIZE__' \ | grep -v '^#define assert' \ | grep -v '^#define __NO_INLINE__' \ + | grep -v '^#define WASI_LIBC_VERSION' \ > "$(SYSROOT_SHARE)/predefined-macros.txt" # Check that the computed metadata matches the expected metadata. diff --git a/expected/wasm32-wasip1-threads/include-all.c b/expected/wasm32-wasip1-threads/include-all.c index 50733743..5f01f06a 100644 --- a/expected/wasm32-wasip1-threads/include-all.c +++ b/expected/wasm32-wasip1-threads/include-all.c @@ -170,5 +170,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip1-threads/predefined-macros.txt b/expected/wasm32-wasip1-threads/predefined-macros.txt index 644e3697..2b8fec8a 100644 --- a/expected/wasm32-wasip1-threads/predefined-macros.txt +++ b/expected/wasm32-wasip1-threads/predefined-macros.txt @@ -1994,6 +1994,7 @@ #define USHRT_MAX 0xffff #define UTIME_NOW (-1) #define UTIME_OMIT (-2) +#define VERSION_H #define WCHAR_MAX (0x7fffffff+L'\0') #define WCHAR_MIN (-1-0x7fffffff+L'\0') #define WEOF 0xffffffffU diff --git a/expected/wasm32-wasip1/include-all.c b/expected/wasm32-wasip1/include-all.c index 50733743..5f01f06a 100644 --- a/expected/wasm32-wasip1/include-all.c +++ b/expected/wasm32-wasip1/include-all.c @@ -170,5 +170,6 @@ #include #include #include +#include #include #include diff --git a/expected/wasm32-wasip1/predefined-macros.txt b/expected/wasm32-wasip1/predefined-macros.txt index 2fb588ce..48b7ccf2 100644 --- a/expected/wasm32-wasip1/predefined-macros.txt +++ b/expected/wasm32-wasip1/predefined-macros.txt @@ -1989,6 +1989,7 @@ #define USHRT_MAX 0xffff #define UTIME_NOW (-1) #define UTIME_OMIT (-2) +#define VERSION_H #define WCHAR_MAX (0x7fffffff+L'\0') #define WCHAR_MIN (-1-0x7fffffff+L'\0') #define WEOF 0xffffffffU diff --git a/expected/wasm32-wasip2/include-all.c b/expected/wasm32-wasip2/include-all.c index 2efa2ded..5885a978 100644 --- a/expected/wasm32-wasip2/include-all.c +++ b/expected/wasm32-wasip2/include-all.c @@ -171,6 +171,7 @@ #include #include #include +#include #include #include #include diff --git a/expected/wasm32-wasip2/predefined-macros.txt b/expected/wasm32-wasip2/predefined-macros.txt index ea346a9d..b76ce5e0 100644 --- a/expected/wasm32-wasip2/predefined-macros.txt +++ b/expected/wasm32-wasip2/predefined-macros.txt @@ -2139,6 +2139,7 @@ #define USHRT_MAX 0xffff #define UTIME_NOW (-1) #define UTIME_OMIT (-2) +#define VERSION_H #define WCHAR_MAX (0x7fffffff+L'\0') #define WCHAR_MIN (-1-0x7fffffff+L'\0') #define WEOF 0xffffffffU diff --git a/libc-bottom-half/headers/public/wasi/version.h.in b/libc-bottom-half/headers/public/wasi/version.h.in new file mode 100644 index 00000000..2a2f7ad8 --- /dev/null +++ b/libc-bottom-half/headers/public/wasi/version.h.in @@ -0,0 +1,5 @@ +// Generated by wasi-lib's `Makefile`. +#ifndef VERSION_H +#define VERSION_H +#define WASI_LIBC_VERSION "{{VERSION}}" +#endif