Skip to content

Commit

Permalink
Publish header with toolchain versions
Browse files Browse the repository at this point in the history
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/<target>/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]: WebAssembly/wasi-libc#490
  • Loading branch information
abrown committed Sep 5, 2024
1 parent 754aec3 commit aa33732
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions cmake/wasi-sdk-sysroot.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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).')
Expand Down

0 comments on commit aa33732

Please sign in to comment.