Repository navigation
[cmake] Regenerate git version in config.h at build time
#9299
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,24 +86,62 @@ option(EMSCRIPTEN_ENABLE_SINGLE_FILE "Enable SINGLE_FILE mode in emscripten buil | |
|
|
||
| option(ENABLE_WERROR "Enable -Werror" ON) | ||
|
|
||
| # For git users, attempt to generate a more useful version string | ||
| if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git) | ||
| find_package(Git QUIET REQUIRED) | ||
| execute_process(COMMAND | ||
| "${GIT_EXECUTABLE}" --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git describe --tags --match version_* | ||
| RESULT_VARIABLE | ||
| GIT_VERSION_RESULT | ||
| OUTPUT_VARIABLE | ||
| GIT_VERSION | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| if(${GIT_VERSION_RESULT}) | ||
| message(WARNING "Error running git describe to determine version") | ||
| else() | ||
| set(PROJECT_VERSION "${PROJECT_VERSION} (${GIT_VERSION})") | ||
| # Generate config.h with PROJECT_VERSION (including git describe output when | ||
| # building from a git checkout). This uses a build-time custom command | ||
| # depending on .git/logs/HEAD so switching commits updates the version string | ||
| # without needing to re-run CMake. | ||
| set(VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/config.h") | ||
| set(GEN_VERSION_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen-version.cmake") | ||
| set(GEN_VERSION_DEPS "${GEN_VERSION_SCRIPT}" "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in") | ||
| set(GENERATED_VERSION_FILES "${VERSION_HEADER}") | ||
|
|
||
| if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git") | ||
| find_package(Git QUIET) | ||
| if(GIT_FOUND) | ||
| execute_process( | ||
| COMMAND "${GIT_EXECUTABLE}" rev-parse --git-dir | ||
| WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | ||
| RESULT_VARIABLE GIT_RESULT | ||
| OUTPUT_VARIABLE GIT_DIR | ||
| ERROR_QUIET | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| if(GIT_RESULT EQUAL 0) | ||
| get_filename_component(GIT_DIR "${GIT_DIR}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") | ||
| if(EXISTS "${GIT_DIR}/logs/HEAD") | ||
| list(APPEND GEN_VERSION_DEPS "${GIT_DIR}/logs/HEAD") | ||
| else() | ||
| # Some checkouts (e.g. 'repo' or reflog-disabled) may not have | ||
| # .git/logs/HEAD; use a non-existent output to force checking git at | ||
| # build time. | ||
| list(APPEND GENERATED_VERSION_FILES "${CMAKE_CURRENT_BINARY_DIR}/__fake_config.h") | ||
| endif() | ||
|
sbc100 marked this conversation as resolved.
Comment on lines
+112
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent IDEs (like Visual Studio or Xcode) from displaying warnings about the missing |
||
| endif() | ||
| endif() | ||
| endif() | ||
|
|
||
| configure_file(config.h.in config.h) | ||
| set(GEN_VERSION_CMD | ||
| "${CMAKE_COMMAND}" | ||
| "-DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}" | ||
| "-DOUTPUT_FILE=${VERSION_HEADER}" | ||
| "-DPROJECT_VERSION=${PROJECT_VERSION}" | ||
| "-DGIT_EXECUTABLE=${GIT_EXECUTABLE}" | ||
| -P "${GEN_VERSION_SCRIPT}") | ||
|
|
||
| # Also generate config.h at configure time so tools like clang-tidy that run | ||
| # before a build can find it. | ||
| execute_process(COMMAND ${GEN_VERSION_CMD}) | ||
|
|
||
| add_custom_command( | ||
| OUTPUT ${GENERATED_VERSION_FILES} | ||
|
sbc100 marked this conversation as resolved.
|
||
| DEPENDS ${GEN_VERSION_DEPS} | ||
| COMMAND ${GEN_VERSION_CMD} | ||
| COMMENT "Generating config.h") | ||
|
|
||
| set_source_files_properties("${VERSION_HEADER}" | ||
| PROPERTIES GENERATED TRUE | ||
| HEADER_FILE_ONLY TRUE) | ||
|
|
||
| add_custom_target(binaryen_version ALL DEPENDS ${GENERATED_VERSION_FILES}) | ||
|
|
||
| # Configure threads | ||
|
|
||
|
|
@@ -444,6 +482,7 @@ endif() | |
| # Declare libbinaryen | ||
| # This will be either be STATIC or SHARED depending on BUILD_SHARED_LIBS | ||
| add_library(binaryen) | ||
| add_dependencies(binaryen binaryen_version) | ||
|
|
||
| if(BUILD_SHARED_LIBS) | ||
| add_definitions(-DBUILD_SHARED_LIBS) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Copyright 2026 WebAssembly Community Group participants | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| if(GIT_EXECUTABLE AND EXISTS "${SOURCE_DIR}/.git") | ||
| execute_process( | ||
| COMMAND "${GIT_EXECUTABLE}" describe --tags --match "version_*" | ||
| WORKING_DIRECTORY "${SOURCE_DIR}" | ||
| RESULT_VARIABLE GIT_VERSION_RESULT | ||
| OUTPUT_VARIABLE GIT_VERSION | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| if(GIT_VERSION_RESULT) | ||
| message(WARNING "Error running git describe to determine version") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| else() | ||
| set(PROJECT_VERSION "${PROJECT_VERSION} (${GIT_VERSION})") | ||
| endif() | ||
|
sbc100 marked this conversation as resolved.
|
||
| endif() | ||
|
|
||
| configure_file("${SOURCE_DIR}/config.h.in" "${OUTPUT_FILE}") | ||
|
sbc100 marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.