2019-06-02 03:33:40 +08:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2019-02-08 08:16:09 +08:00
|
|
|
# Generates header for which version is taken from (in order of precedence):
|
|
|
|
# 1) .tarball-version file
|
|
|
|
# 2) git
|
|
|
|
#
|
|
|
|
# Version is checked during configuration step and for every target
|
|
|
|
# that has check_version_h target as dependency
|
|
|
|
|
2021-09-09 05:54:20 +08:00
|
|
|
cmake_minimum_required(VERSION 3.13)
|
2019-01-12 03:58:24 +08:00
|
|
|
|
2019-02-08 08:16:09 +08:00
|
|
|
set(VERSION_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR}/version.cmake)
|
|
|
|
|
2021-04-01 01:38:34 +08:00
|
|
|
|
|
|
|
# In an ideal world, every CI engine records the most basic and most
|
|
|
|
# important information:
|
|
|
|
# - current date and time
|
|
|
|
# - git version of the pull request
|
|
|
|
# - git version of the moving branch it's being merged with
|
|
|
|
#
|
|
|
|
# In the real world, some CI results use a random timezone without
|
|
|
|
# telling which one or don't provide any time at all.
|
|
|
|
string(TIMESTAMP build_start_time UTC)
|
2022-03-08 09:50:57 +08:00
|
|
|
message(STATUS "SOF version.cmake starting at ${build_start_time} UTC")
|
2021-04-01 01:38:34 +08:00
|
|
|
|
|
|
|
# Most CI engines test a temporary merge of the pull request with a
|
2022-01-19 04:02:28 +08:00
|
|
|
# moving target: the latest target branch. In that case the HEAD SHA is
|
|
|
|
# very volatile and the --parents are much more relevant and useful.
|
|
|
|
#
|
|
|
|
# --no-abbrev-commit because some git servers like github allow fetching
|
|
|
|
# by full length SHA (others forbid this entirely, see last page of
|
|
|
|
# `git help fetch-pack`)
|
2022-03-08 09:58:37 +08:00
|
|
|
message(STATUS "${SOF_ROOT_SOURCE_DIRECTORY} is at git commit with parent(s):")
|
2021-04-01 01:38:34 +08:00
|
|
|
# Note execute_process() failures are ignored by default (missing git...)
|
|
|
|
execute_process(
|
2022-03-08 09:58:37 +08:00
|
|
|
COMMAND git -C "${SOF_ROOT_SOURCE_DIRECTORY}"
|
2022-01-19 04:02:28 +08:00
|
|
|
log --parents --no-abbrev-commit --decorate -n 1 HEAD
|
2021-04-01 01:38:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-07 02:21:58 +08:00
|
|
|
# Don't confuse this manual _input_ file with the other, output file of
|
|
|
|
# the same name auto-generated in the top _build_ directory by "make
|
|
|
|
# dist", see dist.cmake
|
2019-02-05 19:41:25 +08:00
|
|
|
set(TARBALL_VERSION_FILE_NAME ".tarball-version")
|
2022-01-14 13:34:10 +08:00
|
|
|
|
2019-02-08 08:16:09 +08:00
|
|
|
set(TARBALL_VERSION_SOURCE_PATH "${SOF_ROOT_SOURCE_DIRECTORY}/${TARBALL_VERSION_FILE_NAME}")
|
2019-01-12 03:58:24 +08:00
|
|
|
|
2019-02-05 19:41:25 +08:00
|
|
|
if(EXISTS ${TARBALL_VERSION_SOURCE_PATH})
|
|
|
|
file(STRINGS ${TARBALL_VERSION_SOURCE_PATH} lines ENCODING "UTF-8")
|
|
|
|
list(GET lines 0 GIT_TAG)
|
|
|
|
list(GET lines 1 GIT_LOG_HASH)
|
2022-01-14 13:34:10 +08:00
|
|
|
message(STATUS "Found ${TARBALL_VERSION_SOURCE_PATH}")
|
2019-02-05 19:41:25 +08:00
|
|
|
else()
|
2021-04-01 03:57:25 +08:00
|
|
|
# execute_process() errors are not fatal by default!
|
2021-03-23 08:03:13 +08:00
|
|
|
execute_process(
|
|
|
|
COMMAND git describe --tags --abbrev=12 --match v* --dirty
|
2020-04-15 20:07:05 +08:00
|
|
|
WORKING_DIRECTORY ${SOF_ROOT_SOURCE_DIRECTORY}
|
2019-02-05 19:41:25 +08:00
|
|
|
OUTPUT_VARIABLE GIT_TAG
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
|
|
)
|
|
|
|
|
|
|
|
execute_process(COMMAND git log --pretty=format:%h -1
|
2020-04-15 20:07:05 +08:00
|
|
|
WORKING_DIRECTORY ${SOF_ROOT_SOURCE_DIRECTORY}
|
2019-02-05 19:41:25 +08:00
|
|
|
OUTPUT_VARIABLE GIT_LOG_HASH
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
|
|
)
|
|
|
|
endif()
|
2019-01-12 03:58:24 +08:00
|
|
|
|
|
|
|
if(NOT GIT_TAG MATCHES "^v")
|
2021-04-01 03:57:25 +08:00
|
|
|
message(WARNING
|
|
|
|
"git describe found ${GIT_TAG} / nothing starting with 'v'. Shallow clone?")
|
2019-01-12 03:58:24 +08:00
|
|
|
set(GIT_TAG "v0.0-0-g0000")
|
|
|
|
endif()
|
|
|
|
|
2020-07-07 02:21:58 +08:00
|
|
|
message(STATUS "GIT_TAG / GIT_LOG_HASH : ${GIT_TAG} / ${GIT_LOG_HASH}")
|
|
|
|
|
2019-01-12 03:58:24 +08:00
|
|
|
string(REGEX MATCH "^v([0-9]+)[.]([0-9]+)([.]([0-9]+))?" ignored "${GIT_TAG}")
|
|
|
|
set(SOF_MAJOR ${CMAKE_MATCH_1})
|
|
|
|
set(SOF_MINOR ${CMAKE_MATCH_2})
|
|
|
|
set(SOF_MICRO ${CMAKE_MATCH_4})
|
|
|
|
|
|
|
|
if(NOT SOF_MICRO MATCHES "^[0-9]+$")
|
|
|
|
set(SOF_MICRO 0)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
string(SUBSTRING "${GIT_LOG_HASH}" 0 5 SOF_TAG)
|
|
|
|
if(NOT SOF_TAG)
|
|
|
|
set(SOF_TAG 0)
|
|
|
|
endif()
|
|
|
|
|
2020-07-17 18:35:48 +08:00
|
|
|
# Calculate source hash value, used to check ldc file and firmware compatibility
|
2020-08-27 16:16:54 +08:00
|
|
|
if(EXISTS ${SOF_ROOT_SOURCE_DIRECTORY}/.git/)
|
2020-08-27 17:55:13 +08:00
|
|
|
set(SOURCE_HASH_DIR "${SOF_ROOT_BINARY_DIRECTORY}/source_hash")
|
|
|
|
file(MAKE_DIRECTORY ${SOURCE_HASH_DIR})
|
2022-03-08 10:02:10 +08:00
|
|
|
|
|
|
|
# When building with Zephyr, add a few extra files so the XTOS
|
|
|
|
# and Zephyr .ldc files (which have different content!) do not
|
|
|
|
# end up with the exact same "source checksum". The concept of
|
|
|
|
# source checksum is flawed (see issue #3890) but this is much
|
|
|
|
# better than nothing at all.
|
|
|
|
#
|
|
|
|
# Warning: ZEPHYR_CURRENT_MODULE_DIR is _undefined_ the first
|
|
|
|
# time we're run and _defined empty_ the second time. To
|
|
|
|
# understand why look at the check_version_h target below.
|
|
|
|
if("${ZEPHYR_CURRENT_MODULE_DIR}" STREQUAL "")
|
|
|
|
set(_sof_zephyr_dir "")
|
|
|
|
else()
|
|
|
|
set(_sof_zephyr_dir "zephyr/")
|
|
|
|
endif()
|
|
|
|
|
2020-07-17 18:35:48 +08:00
|
|
|
# list tracked files from src directory
|
2022-03-08 10:02:10 +08:00
|
|
|
execute_process(COMMAND git ls-files src/ scripts/ ${_sof_zephyr_dir}
|
2020-07-17 18:35:48 +08:00
|
|
|
WORKING_DIRECTORY ${SOF_ROOT_SOURCE_DIRECTORY}
|
2020-08-27 17:55:13 +08:00
|
|
|
OUTPUT_FILE "${SOURCE_HASH_DIR}/tracked_file_list"
|
2020-07-17 18:35:48 +08:00
|
|
|
)
|
|
|
|
# calculate hash of each listed files (from file version saved in file system)
|
2022-06-15 06:04:02 +08:00
|
|
|
execute_process(COMMAND git hash-object --no-filters --stdin-paths
|
2020-07-17 18:35:48 +08:00
|
|
|
WORKING_DIRECTORY ${SOF_ROOT_SOURCE_DIRECTORY}
|
2020-08-27 17:55:13 +08:00
|
|
|
INPUT_FILE "${SOURCE_HASH_DIR}/tracked_file_list"
|
|
|
|
OUTPUT_FILE "${SOURCE_HASH_DIR}/tracked_file_hash_list"
|
2020-07-17 18:35:48 +08:00
|
|
|
)
|
|
|
|
# then calculate single hash of previously calculated hash list
|
2022-06-15 06:04:02 +08:00
|
|
|
execute_process(COMMAND git hash-object --no-filters --stdin
|
2020-07-17 18:35:48 +08:00
|
|
|
WORKING_DIRECTORY ${SOF_ROOT_SOURCE_DIRECTORY}
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
2020-08-27 17:55:13 +08:00
|
|
|
INPUT_FILE "${SOURCE_HASH_DIR}/tracked_file_hash_list"
|
2020-07-17 18:35:48 +08:00
|
|
|
OUTPUT_VARIABLE SOF_SRC_HASH_LONG
|
|
|
|
)
|
|
|
|
string(SUBSTRING ${SOF_SRC_HASH_LONG} 0 8 SOF_SRC_HASH)
|
2021-11-02 13:56:49 +08:00
|
|
|
message(STATUS "Source content hash: ${SOF_SRC_HASH}. \
|
|
|
|
Note: by design, source hash is broken by config changes. See #3890.")
|
|
|
|
else() # Zephyr, tarball,...
|
2021-12-24 08:45:26 +08:00
|
|
|
if(NOT "${GIT_LOG_HASH}" STREQUAL "")
|
2020-08-31 16:44:43 +08:00
|
|
|
string(SUBSTRING "${GIT_LOG_HASH}" 0 8 SOF_SRC_HASH)
|
|
|
|
else()
|
2021-12-24 08:45:26 +08:00
|
|
|
set(SOF_SRC_HASH "baadf00d")
|
2020-08-31 16:44:43 +08:00
|
|
|
endif()
|
2021-11-02 13:56:49 +08:00
|
|
|
message(WARNING "${SOF_ROOT_SOURCE_DIRECTORY}/.git not found, \
|
2021-12-24 08:45:26 +08:00
|
|
|
source content hash cannot computed for the .ldc. Using SOF_SRC_HASH=${SOF_SRC_HASH} \
|
|
|
|
from GIT_LOG_HASH instead")
|
2020-07-17 18:35:48 +08:00
|
|
|
endif()
|
|
|
|
|
2019-02-09 05:49:47 +08:00
|
|
|
# for SOF_BUILD
|
|
|
|
include(${CMAKE_CURRENT_LIST_DIR}/version-build-counter.cmake)
|
2019-01-12 03:58:24 +08:00
|
|
|
|
2022-03-08 09:50:57 +08:00
|
|
|
# (Re)-generate "${VERSION_H_PATH}" but overwrite the old one only if
|
|
|
|
# different to avoid a full rebuild. TODO: check how Zephyr solves this
|
|
|
|
# problem, see Zephyr commit 91709778a4878c
|
|
|
|
#
|
|
|
|
# This function is called only below; not supposed to be used elsewhere.
|
|
|
|
# This entire file is run at CMake configure time _and_ invoked
|
|
|
|
# again at build time.
|
2019-02-08 08:16:09 +08:00
|
|
|
function(sof_check_version_h)
|
2019-01-12 03:58:24 +08:00
|
|
|
string(CONCAT header_content
|
|
|
|
"#define SOF_MAJOR ${SOF_MAJOR}\n"
|
|
|
|
"#define SOF_MINOR ${SOF_MINOR}\n"
|
|
|
|
"#define SOF_MICRO ${SOF_MICRO}\n"
|
|
|
|
"#define SOF_TAG \"${SOF_TAG}\"\n"
|
|
|
|
"#define SOF_BUILD ${SOF_BUILD}\n"
|
2020-07-29 15:45:06 +08:00
|
|
|
"#define SOF_GIT_TAG \"${GIT_TAG}\"\n"
|
2020-07-17 18:35:48 +08:00
|
|
|
"#define SOF_SRC_HASH 0x${SOF_SRC_HASH}\n"
|
2019-01-12 03:58:24 +08:00
|
|
|
)
|
|
|
|
|
2019-02-08 08:16:09 +08:00
|
|
|
if(EXISTS "${VERSION_H_PATH}")
|
|
|
|
file(READ "${VERSION_H_PATH}" old_version_content)
|
|
|
|
if("${header_content}" STREQUAL "${old_version_content}")
|
2022-03-08 09:50:57 +08:00
|
|
|
message(STATUS "Unchanged ${VERSION_H_PATH}")
|
2019-01-12 03:58:24 +08:00
|
|
|
return()
|
|
|
|
endif()
|
2022-03-08 09:50:57 +08:00
|
|
|
endif()
|
2019-01-12 03:58:24 +08:00
|
|
|
|
2019-02-08 08:16:09 +08:00
|
|
|
file(WRITE "${VERSION_H_PATH}" "${header_content}")
|
2022-03-08 09:50:57 +08:00
|
|
|
message(STATUS "Generated new ${VERSION_H_PATH}")
|
2019-01-12 03:58:24 +08:00
|
|
|
endfunction()
|
|
|
|
|
2022-03-08 09:50:57 +08:00
|
|
|
# This ${VERSION_CMAKE_PATH} file is run in two (very) different ways:
|
|
|
|
#
|
|
|
|
# 1. explicitly included by some other, top-level CMakeLists.txt file, and
|
|
|
|
# 2. directly and "recursively" invoking itself with 'cmake -P myself' here.
|
|
|
|
#
|
|
|
|
# Add this check_version_h target only in case 1. (no "infinite recursion")
|
2019-02-08 08:16:09 +08:00
|
|
|
if("${CMAKE_SCRIPT_MODE_FILE}" STREQUAL "")
|
|
|
|
add_custom_target(
|
|
|
|
check_version_h
|
|
|
|
BYPRODUCTS ${VERSION_H_PATH}
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
|
|
|
-DVERSION_H_PATH=${VERSION_H_PATH}
|
|
|
|
-DSOF_ROOT_SOURCE_DIRECTORY=${SOF_ROOT_SOURCE_DIRECTORY}
|
2019-02-09 05:49:47 +08:00
|
|
|
-DSOF_ROOT_BINARY_DIRECTORY=${SOF_ROOT_BINARY_DIRECTORY}
|
2022-03-08 10:02:10 +08:00
|
|
|
-DZEPHYR_CURRENT_MODULE_DIR=${ZEPHYR_CURRENT_MODULE_DIR}
|
2019-02-08 08:16:09 +08:00
|
|
|
-P ${VERSION_CMAKE_PATH}
|
2022-03-08 09:50:57 +08:00
|
|
|
COMMENT "cmake -P ${VERSION_CMAKE_PATH}"
|
2019-01-12 03:58:24 +08:00
|
|
|
VERBATIM
|
|
|
|
USES_TERMINAL
|
|
|
|
)
|
2019-02-08 08:16:09 +08:00
|
|
|
endif()
|
2019-01-12 03:58:24 +08:00
|
|
|
|
2019-02-08 08:16:09 +08:00
|
|
|
sof_check_version_h()
|