2018-01-08 01:39:45 -08:00
# This file adds commands to manage the FISH-BUILD-VERSION-FILE (hereafter
# FBVF). This file exists in the build directory and is used to populate the
# documentation and also the version string in fish_version.o (printed with
# `echo $version` and also fish --version). The essential idea is that we are
# going to invoke git_version_gen.sh, which will update the
# FISH-BUILD-VERSION-FILE only if it needs to change; this is what makes
# incremental rebuilds fast.
#
# This code is delicate, with the chief subtlety revolving around Ninja. A
# natural and naive approach would tell the generated build system that FBVF is
# a dependency of fish_version.o, and that git_version_gen.sh updates it. Make
# will then invoke the script, check the timestamp on fish_version.o and FBVF,
# see that FBVF is earlier, and then not rebuild fish_version.o. Ninja,
# however, decides what to build up-front and will unconditionally rebuild
# fish_version.o.
#
# To avoid this with Ninja, we want to hook into its 'restat' option which we
# can do through the BYPRODUCTS feature of CMake. See
# https://cmake.org/cmake/help/latest/policy/CMP0058.html
#
# Unfortunately BYPRODUCTS behaves strangely with the Makefile generator: it
# marks FBVF as generated and then CMake itself will `touch` it on every build,
# meaning that using BYPRODUCTS will cause fish_version.o to be rebuilt
# unconditionally with the Makefile generator. Thus we want to use the
# natural-and-naive approach for Makefiles.
# **IMPORTANT** If you touch these build rules, please test both Ninja and
# Makefile generators with both a clean and dirty git tree. Verify that both
# generated build systems rebuild fish when the git tree goes from dirty to
# clean (and vice versa), and verify they do NOT rebuild it when the git tree
# stays the same (incremental builds must be fast).
# Just a handy abbreviation.
2020-03-14 16:11:35 -07:00
set ( FBVF FISH-BUILD-VERSION-FILE )
2018-01-08 01:39:45 -08:00
# TODO: find a cleaner way to do this.
IF ( ${ CMAKE_GENERATOR } STREQUAL Ninja )
2020-03-14 16:11:35 -07:00
set ( FBVF-OUTPUT fish-build-version-witness.txt )
set ( CFBVF-BYPRODUCTS ${ FBVF } )
else ( ${ CMAKE_GENERATOR } STREQUAL Ninja )
set ( FBVF-OUTPUT ${ FBVF } )
set ( CFBVF-BYPRODUCTS )
endif ( ${ CMAKE_GENERATOR } STREQUAL Ninja )
2018-01-08 01:39:45 -08:00
# Set up the version targets
2020-03-14 16:11:35 -07:00
add_custom_target ( CHECK-FISH-BUILD-VERSION-FILE
2018-08-28 23:10:10 +08:00
C O M M A N D $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / b u i l d _ t o o l s / g i t _ v e r s i o n _ g e n . s h $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R }
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R }
2018-01-08 01:39:45 -08:00
B Y P R O D U C T S $ { C F B V F - B Y P R O D U C T S } )
2020-03-14 16:11:35 -07:00
add_custom_command ( OUTPUT ${ FBVF-OUTPUT }
2018-01-08 01:39:45 -08:00
D E P E N D S C H E C K - F I S H - B U I L D - V E R S I O N - F I L E )
2018-03-04 21:23:36 -08:00
# Abbreviation for the target.
2020-03-14 16:11:35 -07:00
set ( CFBVF CHECK-FISH-BUILD-VERSION-FILE )