From a34fd6caaaeb17e83e959a9e5a2f38fad499a3c8 Mon Sep 17 00:00:00 2001
From: fef <owo@fef.moe>
Date: Sun, 19 Sep 2021 16:39:05 +0200
Subject: [PATCH] config: include version and git revision

---
 CMakeLists.txt          |  2 ++
 cmake/git.cmake         | 28 ++++++++++++++++++++++++++++
 include/gay/config.h.in | 18 ++++++++++++++++++
 3 files changed, 48 insertions(+)
 create mode 100644 cmake/git.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index facbbc7..393731c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,6 +10,8 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
     set_property(GLOBAL PROPERTY USE_FOLDERS ON)
 endif()
 
+include(cmake/git.cmake)
+
 include(cmake/config.cmake)
 configure_file(
     "${CMAKE_SOURCE_DIR}/include/gay/config.h.in"
diff --git a/cmake/git.cmake b/cmake/git.cmake
new file mode 100644
index 0000000..40f457b
--- /dev/null
+++ b/cmake/git.cmake
@@ -0,0 +1,28 @@
+# See the end of this file for copyright and license terms.
+
+find_package(Git QUIET)
+if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
+	execute_process(
+		COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
+		WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
+		OUTPUT_VARIABLE gaybsd_GIT_REVISION
+		ERROR_QUIET
+		OUTPUT_STRIP_TRAILING_WHITESPACE
+	)
+
+	if (NOT "${gaybsd_GIT_REVISION}" STREQUAL "")
+		set(gaybsd_VERSION_SUFFIX "-${gaybsd_GIT_REVISION}")
+	endif()
+endif()
+
+# This file is part of GayBSD.
+# Copyright (c) 2021 fef <owo@fef.moe>.
+#
+# GayBSD is nonviolent software: you may only use, redistribute, and/or
+# modify it under the terms of the Cooperative Nonviolent Public License
+# (CNPL) as found in the LICENSE file in the source code root directory
+# or at <https://git.pixie.town/thufie/npl-builder>; either version 7
+# of the license, or (at your option) any later version.
+#
+# GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
+# permitted by applicable law.  See the CNPL for details.
diff --git a/include/gay/config.h.in b/include/gay/config.h.in
index 28e5e21..cfe0357 100644
--- a/include/gay/config.h.in
+++ b/include/gay/config.h.in
@@ -1,7 +1,25 @@
 /* See the end of this file for copyright and license terms. */
 
+/**
+ * @file include/gay/config.h
+ * @brief Kernel configuration header.
+ * This header file contains all architecture independent configuration options
+ * for GayBSD, as defined in `cmake/config.cmake`.
+ */
+
 #pragma once
 
+#define GAY_VERSION_MAJOR @gaybsd_VERSION_MAJOR@
+#define GAY_VERSION_MINOR @gaybsd_VERSION_MINOR@
+#define GAY_VERSION_PATCH @gaybsd_VERSION_PATCH@
+#define GAY_VERSION (					\
+	(unsigned long)GAY_VERSION_MAJOR << 24 |	\
+	(unsigned long)GAY_VERSION_MINOR << 16 |	\
+	(unsigned long)GAY_VERSION_PATCH		\
+)
+#define GAY_VERSION_STR \
+	"@gaybsd_VERSION_MAJOR@.@gaybsd_VERSION_MINOR@.@gaybsd_VERSION_PATCH@@gaybsd_VERSION_SUFFIX@"
+
 /** @brief Physical address where the kernel is loaded */
 #define CFG_KERNEL_ORIGIN @KERNEL_ORIGIN@