From aa3373270da4f8be90d8fb48e1e3e48e3083f40b Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 5 Sep 2024 10:57:55 -0700 Subject: [PATCH] Publish header with toolchain versions This change is an attempt to resolve [wasi-libc#490] but at the wasi-sdk level. It adds a `version.h` header file to `share/wasi-sysroot/include//wasi` so that users have programmatic access to some extra information to output better error messages, e.g. This `version.h` looks something like: ```c // Generated by wasi-sdk's `version.py` script. #ifndef VERSION_H #define VERSION_H #define WASI_SDK_VERSION "24.6g754aec3d6f58+m" #define WASI_LIBC_VERSION "b9ef79d7dbd4" #endif ``` It _is_ a bit strange that we're adding to wasi-libc's include files from wasi-sdk (it would be cleaner if it happened in wasi-libc directly) but wasi-sdk actually has the information available. [wasi-libc#490]: https://github.com/WebAssembly/wasi-libc/issues/490 --- cmake/wasi-sdk-sysroot.cmake | 13 +++++++++++++ version.py | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cmake/wasi-sdk-sysroot.cmake b/cmake/wasi-sdk-sysroot.cmake index 127ca62d3..debe8acf5 100644 --- a/cmake/wasi-sdk-sysroot.cmake +++ b/cmake/wasi-sdk-sysroot.cmake @@ -336,6 +336,19 @@ file(GENERATE OUTPUT ${version_file_tmp} CONTENT ${version_dump}) add_custom_target(version-file DEPENDS ${version_file_tmp}) add_dependencies(build version-file) +# Install a `version.h` script in each sysroot's include directory. +execute_process( + COMMAND ${PYTHON} ${version_script} header + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE version_header) +foreach(target IN LISTS WASI_SDK_TARGETS) + message(STATUS "Creating version.h in ${version_dir}") + set(version_header_path ${wasi_sysroot}/include/${target}/wasi/version.h) + file(GENERATE OUTPUT ${version_header_path} CONTENT ${version_header}) + add_custom_target(version-header-${target} DEPENDS ${version_header_path}) + add_dependencies(build version-header-${target}) +endforeach() + if(WASI_SDK_INCLUDE_TESTS) add_subdirectory(tests) endif() diff --git a/version.py b/version.py index 62f0ff9ef..396c65eba 100755 --- a/version.py +++ b/version.py @@ -62,7 +62,7 @@ def parse_git_version(version): def git_version(): version = exec(['git', 'describe', '--long', '--candidates=999', '--match=wasi-sdk-*', '--dirty=+m', f'--abbrev={GIT_REF_LEN}'], - os.path.dirname(sys.argv[0])) + os.path.dirname(sys.argv[0])) major, minor, git, dirty = parse_git_version(version) version = f'{major}.{minor}' if git: @@ -109,13 +109,23 @@ def main(action, llvm_dir): major, minor, path = llvm_cmake_version(llvm_dir) print(f'llvm-version: {major}.{minor}.{path}') print(f'config: {git_commit("src/config")}') + elif action == 'header': + print('// Generated by wasi-sdk\'s `version.py` script.') + print('#ifndef VERSION_H') + print('#define VERSION_H') + print() + print(f'#define WASI_SDK_VERSION "{git_version()}"') + print(f'#define WASI_LIBC_VERSION "{git_commit("src/wasi-libc")}"') + print() + print('#endif') if __name__ == '__main__': parser = argparse.ArgumentParser( description='Print the various kinds of versions in wasi-sdk') parser.add_argument('action', - choices=['wasi-sdk', 'llvm', 'llvm-major', 'dump'], + choices=['wasi-sdk', 'llvm', + 'llvm-major', 'dump', 'header'], nargs='?', default='wasi-sdk', help='Which kind of version to print (default: wasi-sdk).')