diff mbox series

[v2] tree-wide: refactor versioning passing

Message ID 20241216142259.316122-1-oss@braunwarth.dev
State Accepted
Headers show
Series [v2] tree-wide: refactor versioning passing | expand

Commit Message

Daniel Braunwarth Dec. 16, 2024, 2:22 p.m. UTC
The currently used mechanism is problematic for non-upstream builds.

If custom Git tags are used the version could contain information we
don't want to be actually contained or even worse - no version.

For example if one tags with "debian/2024.05+dfsg-1" the version
available in Lua would always be "0.0".

From now on the main version parts are read from the top Makefile.

Only the EXTRAVERSION is dynamically created based on the current Git
status.

In case the build is started on a tagged commit without any local
changes no EXTRAVERSION is added.

In case the build is started on a commit without any local changes the
short git SHA is added as EXTRAVERSION.

In case the build is started with local changes "-dirty" is appended to
the EXTRAVERSION.

Changes since v1:
- Generate EXTRAVERSION dynamically

Signed-off-by: Daniel Braunwarth <oss@braunwarth.dev>
---
 Makefile                 |  2 --
 Makefile.flags           | 14 ++++++++------
 core/swupdate.c          |  4 ++--
 corelib/lua_interface.c  |  9 +++------
 include/globals.h        |  6 +++++-
 scripts/gen_extraversion | 26 ++++++++++++++++++++++++++
 6 files changed, 44 insertions(+), 17 deletions(-)
 create mode 100755 scripts/gen_extraversion

Comments

Stefano Babic Dec. 17, 2024, 4:03 p.m. UTC | #1
Hi Daniel,

On 16.12.24 15:22, Daniel Braunwarth wrote:
> The currently used mechanism is problematic for non-upstream builds.
>
> If custom Git tags are used the version could contain information we
> don't want to be actually contained or even worse - no version.
>
> For example if one tags with "debian/2024.05+dfsg-1" the version
> available in Lua would always be "0.0".
>
>  From now on the main version parts are read from the top Makefile.
>
> Only the EXTRAVERSION is dynamically created based on the current Git
> status.
>
> In case the build is started on a tagged commit without any local
> changes no EXTRAVERSION is added.
>
> In case the build is started on a commit without any local changes the
> short git SHA is added as EXTRAVERSION.
>
> In case the build is started with local changes "-dirty" is appended to
> the EXTRAVERSION.
>
> Changes since v1:
> - Generate EXTRAVERSION dynamically
>
> Signed-off-by: Daniel Braunwarth <oss@braunwarth.dev>
> ---
>   Makefile                 |  2 --
>   Makefile.flags           | 14 ++++++++------
>   core/swupdate.c          |  4 ++--
>   corelib/lua_interface.c  |  9 +++------
>   include/globals.h        |  6 +++++-
>   scripts/gen_extraversion | 26 ++++++++++++++++++++++++++
>   6 files changed, 44 insertions(+), 17 deletions(-)
>   create mode 100755 scripts/gen_extraversion
>
> diff --git a/Makefile b/Makefile
> index e2aa9ae..58515aa 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -5,8 +5,6 @@
>   VERSION = 2024
>   PATCHLEVEL = 12
>   SUBLEVEL = 0
> -EXTRAVERSION =
> -NAME =
>
>   IPCLIB_VERSION = 0.1
>
> diff --git a/Makefile.flags b/Makefile.flags
> index 4ce1bac..6662d4d 100644
> --- a/Makefile.flags
> +++ b/Makefile.flags
> @@ -5,9 +5,6 @@
>   # ==========================================================================
>   # Build system
>   # ==========================================================================
> -SWU_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
> -SWU_VER = $(shell git -C $(SWU_DIR) describe --tags --always --dirty)
> -export SWU_VER
>   SKIP_STRIP ?= n
>
>   ifneq ($(CC),clang)
> @@ -19,8 +16,13 @@ endif
>   # -std=gnu99 needed for [U]LLONG_MAX on some systems
>   KBUILD_CPPFLAGS += $(call cc-option,-std=gnu99,)
>
> -KBUILD_CPPFLAGS += -D_GNU_SOURCE -DNDEBUG \
> -		   -D"SWU_VER=\"$(SWU_VER)\""
> +KBUILD_CPPFLAGS += -D_GNU_SOURCE -DNDEBUG
> +
> +# Generate version
> +KBUILD_CPPFLAGS += -DVERSION=$(VERSION)
> +KBUILD_CPPFLAGS += -DVERSION_PATCHLEVEL=$(PATCHLEVEL)
> +KBUILD_CPPFLAGS += -DVERSION_SUBLEVEL=$(SUBLEVEL)
> +KBUILD_CPPFLAGS += -DVERSION_EXTRAVERSION=$(shell $(srctree)/scripts/gen_extraversion)
>
>   KBUILD_CFLAGS += $(call cc-option,-Wall,)
>   KBUILD_CFLAGS += $(call cc-option,-Wshadow,)
> @@ -259,7 +261,7 @@ ifneq ($(CONFIG_EMBEDDED_SURICATTA_LUA),)
>   ifneq ($(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE),)
>   LDFLAGS_swupdate += -Wl,--format=binary -Wl,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE) -Wl,--format=default
>   KBUILD_CPPFLAGS += -DEMBEDDED_SURICATTA_LUA_SOURCE_START="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE))))_start"
> -KBUILD_CPPFLAGS += -DEMBEDDED_SURICATTA_LUA_SOURCE_END="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE))))_end"
> +KBUILD_CPPFLAGS += -DEMBEDDED_SURICATTA_LUA_SOURCE_END="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE))))_end"
>   endif
>   endif
>   endif
> diff --git a/core/swupdate.c b/core/swupdate.c
> index e7ec569..0e3c667 100644
> --- a/core/swupdate.c
> +++ b/core/swupdate.c
> @@ -560,7 +560,7 @@ int main(int argc, char **argv)
>   			loglevel = LASTLOGLEVEL;
>   			break;
>   		case '0':
> -			printf("%s", BANNER);
> +			printf("%s\n", BANNER);
>   			exit(EXIT_SUCCESS);
>   		}
>   	}
> @@ -836,7 +836,7 @@ int main(int argc, char **argv)
>   		}
>   	}
>
> -	printf("%s\n", BANNER);
> +	printf("%s\n\n", BANNER);
>   	printf("Licensed under GPLv2. See source distribution for detailed "
>   		"copyright notices.\n\n");
>
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 77e4aef..203a5d1 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -1001,12 +1001,9 @@ static void lua_push_enum(lua_State *L, const char *name, int value)
>
>   int lua_get_swupdate_version(lua_State *L)
>   {
> -	unsigned int version = 0, patchlevel = 0;
> -	/* Deliberately ignore sublevel and extraversion. */
> -	if (sscanf(SWU_VER, "%u.%u.%*s", &version, &patchlevel) != 2) {
> -		version = 0;
> -		patchlevel = 0;
> -	}
> +	const unsigned int version = VERSION;
> +	const unsigned int patchlevel = VERSION_PATCHLEVEL;
> +
>   	lua_newtable (L);
>   	lua_pushnumber(L, 1);
>   	lua_pushnumber(L, version);
> diff --git a/include/globals.h b/include/globals.h
> index a1d6b59..0b3fd88 100644
> --- a/include/globals.h
> +++ b/include/globals.h
> @@ -7,7 +7,11 @@
>
>   #pragma once
>
> -#define BANNER "SWUpdate v" SWU_VER "\n"
> +#define STR_HELPER(x) #x
> +#define STR(x) STR_HELPER(x)
> +
> +#define BANNER "SWUpdate v" \
> +	STR(VERSION)"."STR(VERSION_PATCHLEVEL)"."STR(VERSION_SUBLEVEL)"-"STR(VERSION_EXTRAVERSION)
>
>   #define SWUPDATE_GENERAL_STRING_SIZE	256
>   #define SWUPDATE_UPDATE_DESCRIPTION_STRING_SIZE	512
> diff --git a/scripts/gen_extraversion b/scripts/gen_extraversion
> new file mode 100755
> index 0000000..619b776
> --- /dev/null
> +++ b/scripts/gen_extraversion
> @@ -0,0 +1,26 @@
> +#!/usr/bin/sh
> +#
> +# Copyright (C) 2024, Daniel Braunwarth <oss@braunwarth.dev>
> +#
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +
> +EXTRA_VERSION=""
> +
> +# Check if we are in a git repository
> +if [ -z "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
> +	exit 0
> +fi
> +
> +# If we are on an untagged commit, append the short commit hash
> +if [ -z "$(git describe --tags --exact-match 2>/dev/null)" ]; then
> +	EXTRA_VERSION="${EXTRA_VERSION}-$(git rev-parse --short HEAD)"
> +fi
> +
> +# If the working directory is dirty, append -dirty
> +if [ -n "$(git --no-optional-locks status -uno --porcelain 2>/dev/null)" ]; then
> +	EXTRA_VERSION="${EXTRA_VERSION}-dirty"
> +fi
> +
> +printf "%s" "${EXTRA_VERSION#-}"
> +
> +exit 0

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index e2aa9ae..58515aa 100644
--- a/Makefile
+++ b/Makefile
@@ -5,8 +5,6 @@ 
 VERSION = 2024
 PATCHLEVEL = 12
 SUBLEVEL = 0
-EXTRAVERSION =
-NAME =
 
 IPCLIB_VERSION = 0.1
 
diff --git a/Makefile.flags b/Makefile.flags
index 4ce1bac..6662d4d 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -5,9 +5,6 @@ 
 # ==========================================================================
 # Build system
 # ==========================================================================
-SWU_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
-SWU_VER = $(shell git -C $(SWU_DIR) describe --tags --always --dirty)
-export SWU_VER
 SKIP_STRIP ?= n
 
 ifneq ($(CC),clang)
@@ -19,8 +16,13 @@  endif
 # -std=gnu99 needed for [U]LLONG_MAX on some systems
 KBUILD_CPPFLAGS += $(call cc-option,-std=gnu99,)
 
-KBUILD_CPPFLAGS += -D_GNU_SOURCE -DNDEBUG \
-		   -D"SWU_VER=\"$(SWU_VER)\""
+KBUILD_CPPFLAGS += -D_GNU_SOURCE -DNDEBUG
+
+# Generate version
+KBUILD_CPPFLAGS += -DVERSION=$(VERSION)
+KBUILD_CPPFLAGS += -DVERSION_PATCHLEVEL=$(PATCHLEVEL)
+KBUILD_CPPFLAGS += -DVERSION_SUBLEVEL=$(SUBLEVEL)
+KBUILD_CPPFLAGS += -DVERSION_EXTRAVERSION=$(shell $(srctree)/scripts/gen_extraversion)
 
 KBUILD_CFLAGS += $(call cc-option,-Wall,)
 KBUILD_CFLAGS += $(call cc-option,-Wshadow,)
@@ -259,7 +261,7 @@  ifneq ($(CONFIG_EMBEDDED_SURICATTA_LUA),)
 ifneq ($(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE),)
 LDFLAGS_swupdate += -Wl,--format=binary -Wl,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE) -Wl,--format=default
 KBUILD_CPPFLAGS += -DEMBEDDED_SURICATTA_LUA_SOURCE_START="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE))))_start"
-KBUILD_CPPFLAGS += -DEMBEDDED_SURICATTA_LUA_SOURCE_END="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE))))_end"	
+KBUILD_CPPFLAGS += -DEMBEDDED_SURICATTA_LUA_SOURCE_END="_binary_$(subst ",,$(subst .,_,$(subst /,_,$(CONFIG_EMBEDDED_SURICATTA_LUA_SOURCE))))_end"
 endif
 endif
 endif
diff --git a/core/swupdate.c b/core/swupdate.c
index e7ec569..0e3c667 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -560,7 +560,7 @@  int main(int argc, char **argv)
 			loglevel = LASTLOGLEVEL;
 			break;
 		case '0':
-			printf("%s", BANNER);
+			printf("%s\n", BANNER);
 			exit(EXIT_SUCCESS);
 		}
 	}
@@ -836,7 +836,7 @@  int main(int argc, char **argv)
 		}
 	}
 
-	printf("%s\n", BANNER);
+	printf("%s\n\n", BANNER);
 	printf("Licensed under GPLv2. See source distribution for detailed "
 		"copyright notices.\n\n");
 
diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 77e4aef..203a5d1 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -1001,12 +1001,9 @@  static void lua_push_enum(lua_State *L, const char *name, int value)
 
 int lua_get_swupdate_version(lua_State *L)
 {
-	unsigned int version = 0, patchlevel = 0;
-	/* Deliberately ignore sublevel and extraversion. */
-	if (sscanf(SWU_VER, "%u.%u.%*s", &version, &patchlevel) != 2) {
-		version = 0;
-		patchlevel = 0;
-	}
+	const unsigned int version = VERSION;
+	const unsigned int patchlevel = VERSION_PATCHLEVEL;
+
 	lua_newtable (L);
 	lua_pushnumber(L, 1);
 	lua_pushnumber(L, version);
diff --git a/include/globals.h b/include/globals.h
index a1d6b59..0b3fd88 100644
--- a/include/globals.h
+++ b/include/globals.h
@@ -7,7 +7,11 @@ 
 
 #pragma once
 
-#define BANNER "SWUpdate v" SWU_VER "\n"
+#define STR_HELPER(x) #x
+#define STR(x) STR_HELPER(x)
+
+#define BANNER "SWUpdate v" \
+	STR(VERSION)"."STR(VERSION_PATCHLEVEL)"."STR(VERSION_SUBLEVEL)"-"STR(VERSION_EXTRAVERSION)
 
 #define SWUPDATE_GENERAL_STRING_SIZE	256
 #define SWUPDATE_UPDATE_DESCRIPTION_STRING_SIZE	512
diff --git a/scripts/gen_extraversion b/scripts/gen_extraversion
new file mode 100755
index 0000000..619b776
--- /dev/null
+++ b/scripts/gen_extraversion
@@ -0,0 +1,26 @@ 
+#!/usr/bin/sh
+#
+# Copyright (C) 2024, Daniel Braunwarth <oss@braunwarth.dev>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+EXTRA_VERSION=""
+
+# Check if we are in a git repository
+if [ -z "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+	exit 0
+fi
+
+# If we are on an untagged commit, append the short commit hash
+if [ -z "$(git describe --tags --exact-match 2>/dev/null)" ]; then
+	EXTRA_VERSION="${EXTRA_VERSION}-$(git rev-parse --short HEAD)"
+fi
+
+# If the working directory is dirty, append -dirty
+if [ -n "$(git --no-optional-locks status -uno --porcelain 2>/dev/null)" ]; then
+	EXTRA_VERSION="${EXTRA_VERSION}-dirty"
+fi
+
+printf "%s" "${EXTRA_VERSION#-}"
+
+exit 0