From 37c2d10e0c62306e92911b660156b4bd7062f1d9 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Sat, 4 Dec 2021 02:50:07 +0900 Subject: [PATCH 1/2] fix build error in a shallow-cloned source tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cloning the repository with the --depth option is useful when you do not need the full git history, but it fails to build core/version.c $ git clone --depth 1 git://git.ipxe.org/ipxe.git $ cd ipxe/src $ make [snip] [VERSION] bin/version.ipxe.dsk.o In file included from include/ipxe/features.h:6, from core/version.c:33: : error: unsigned conversion from ‘int’ to ‘unsigned char’ changes value from ‘9062’ to ‘102’ [-Werror=overflow] include/ipxe/dhcp.h:529:58: note: in definition of macro ‘DHCP_OPTION’ 529 | #define DHCP_OPTION( ... ) VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__ | ^~~~~~~~~~~ ... A shallow-cloned repository may not have any tag at all, then it returns only a commit hash. $ git describe --tags --always --long --abbrev=1 --match "v*" 9062 In such a case, VERSION_MINOR becomes empty, causing the build error. If 'git describe' does not find any tag, let the version macros fall back to the default: VERSION_MAJOR = 1 VERSION_MINOR = 0 VERSION_PATCH = 0 EXTRAVERSION = + Setting sha1 to GITVERSION is still useful. Signed-off-by: Masahiro Yamada --- src/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Makefile b/src/Makefile index 69139dc11..6a6011544 100644 --- a/src/Makefile +++ b/src/Makefile @@ -208,8 +208,9 @@ install : # # Version number calculations # -ifneq ($(wildcard ../.git),) -VERSIONS := $(shell git describe --tags --always --long --abbrev=1 --match "v*") +VERSIONS := $(if $(wildcard ../.git),$(shell git describe --tags --always --long --abbrev=1 --match "v*")) + +ifneq ($(filter v%, $(VERSIONS)),) VERSION_TUPLE := $(subst ., ,$(subst -, ,$(patsubst v%,%,$(VERSIONS)))) VERSION_MAJOR := $(word 1,$(VERSION_TUPLE)) VERSION_MINOR := $(word 2,$(VERSION_TUPLE)) @@ -225,6 +226,7 @@ VERSION_MAJOR = 1 VERSION_MINOR = 0 VERSION_PATCH = 0 EXTRAVERSION = + +GITVERSION = $(VERSIONS) endif MM_VERSION = $(VERSION_MAJOR).$(VERSION_MINOR) VERSION = $(MM_VERSION).$(VERSION_PATCH)$(EXTRAVERSION) From 5f810751d7f795f1bb9c54ab9b254a7ad359de1a Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Sat, 4 Dec 2021 03:51:37 +0900 Subject: [PATCH 2/2] take exactly 7 hex characters for GITVERSION git-describe(3) says: --abbrev= Instead of using the default 7 hexadecimal digits as the abbreviated object name, use digits, or as many digits as needed to form a unique object name. An of 0 will suppress long format, only showing the closest tag. The number of "digits needed to form a unique object name" depends on your git repository, e.g. 1) how you cloned it, 2) how many local branches you have, etc. In the full cloned repository, --abbrev=1 for v1.21.1 gave me 5-digits as of writing (but it may increase in the future): $ git checkout v1.21.1 $ git describe --tags --always --long --abbrev=1 --match 'v*' v1.21.1-0-g988d2 In the repository cloned with --depth 1000, it is 4-digits. $ git clone --depth 1000 git://git.ipxe.org/ipxe.git $ ipxe $ git checkout v1.21.1 $ git describe --tags --always --long --abbrev=1 --match 'v*' v1.21.1-0-g988d To make the version string deterministic, use --abbrev=32 and then take the first 7 characters, which is reasonable for this project. Signed-off-by: Masahiro Yamada --- src/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Makefile b/src/Makefile index 6a6011544..88cedb449 100644 --- a/src/Makefile +++ b/src/Makefile @@ -208,7 +208,7 @@ install : # # Version number calculations # -VERSIONS := $(if $(wildcard ../.git),$(shell git describe --tags --always --long --abbrev=1 --match "v*")) +VERSIONS := $(if $(wildcard ../.git),$(shell git describe --tags --always --long --abbrev=32 --match "v*")) ifneq ($(filter v%, $(VERSIONS)),) VERSION_TUPLE := $(subst ., ,$(subst -, ,$(patsubst v%,%,$(VERSIONS)))) @@ -231,7 +231,7 @@ endif MM_VERSION = $(VERSION_MAJOR).$(VERSION_MINOR) VERSION = $(MM_VERSION).$(VERSION_PATCH)$(EXTRAVERSION) ifneq ($(GITVERSION),) -VERSION += ($(GITVERSION)) +VERSION += ($(shell echo $(GITVERSION) | cut -c1-8)) endif version : @$(ECHO) "$(VERSION)"