1# SPDX-License-Identifier: BSD-3-Clause
2
3# Implements build counter and adds it as post-build action for sof
4
5cmake_minimum_required(VERSION 3.10)
6
7set(VERSION_BUILD_COUNTER_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR}/version-build-counter.cmake)
8
9set(BUILD_COUNTER_PATH "${SOF_ROOT_BINARY_DIRECTORY}/.build")
10
11if(NOT EXISTS "${BUILD_COUNTER_PATH}")
12	file(WRITE "${BUILD_COUNTER_PATH}" "1")
13endif()
14
15file(READ "${BUILD_COUNTER_PATH}" SOF_BUILD)
16
17if(NOT SOF_BUILD MATCHES "^[0-9]+$")
18	message(WARNING "Invalid SOF_BUILD - setting to 1")
19	set(SOF_BUILD 1)
20endif()
21
22function(sof_add_build_counter_rule)
23	add_custom_command(
24		TARGET sof
25		POST_BUILD
26		COMMAND ${CMAKE_COMMAND}
27			-DSOF_ROOT_BINARY_DIRECTORY=${SOF_ROOT_BINARY_DIRECTORY}
28			-DBUILD_COUNTER_INCREMENT=ON
29			-P ${VERSION_BUILD_COUNTER_CMAKE_PATH}
30		COMMENT "Incrementing build number in ${BUILD_COUNTER_PATH}"
31		VERBATIM
32		USES_TERMINAL
33	)
34endfunction()
35
36if(BUILD_COUNTER_INCREMENT)
37	MATH(EXPR NEXT_SOF_BUILD "(${SOF_BUILD} + 1) % 65536")
38	file(WRITE "${BUILD_COUNTER_PATH}" ${NEXT_SOF_BUILD})
39endif()
40