| 1 |
From 47ecd95609ec4d25d0b2d5b59cfd31557862d190 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Ewoud Kohl van Wijngaarden <[email protected]> |
| 3 |
Date: Sat, 3 Jan 2026 12:05:04 +0000 |
| 4 |
Subject: [PATCH] Improve build toolchain (#12) |
| 5 |
|
| 6 |
* Use a single sed command to build Version.cpp |
| 7 |
|
| 8 |
This makes it easier to read. It also uses the make variables for target |
| 9 |
and dependency to make sure they don't go out of sync. |
| 10 |
|
| 11 |
* Respect SOURCE_DATE_EPOCH for the build timestamp |
| 12 |
|
| 13 |
This helps making builds reproducible. |
| 14 |
|
| 15 |
Link: https://reproducible-builds.org/docs/source-date-epoch/ |
| 16 |
|
| 17 |
* Set the project name and version in CMake |
| 18 |
|
| 19 |
* Use native configure_file in CMake to replace variables |
| 20 |
|
| 21 |
This is cleaner and allows building in a separate build directory: |
| 22 |
|
| 23 |
cmake -B build |
| 24 |
cmake --build build |
| 25 |
|
| 26 |
* Allow building from a git export |
| 27 |
|
| 28 |
This uses dynamic variable substitution to create files with the right |
| 29 |
content. That means users can download the raw sources from GitHub as |
| 30 |
tarballs and get the correct content. |
| 31 |
|
| 32 |
It also means the release process is easier because no files need to be |
| 33 |
modified anymore. A new release can now be made by tagging any commit. |
| 34 |
If a specific release commit is desired then git commit must be used with |
| 35 |
--allow-empty. |
| 36 |
|
| 37 |
Link: https://saulbaizman.com/article/dynamic-variable-substitution-in-git/ |
| 38 |
|
| 39 |
* Respect specified CXXFLAGS and CFLAGS environment variables |
| 40 |
|
| 41 |
This allows users to set even stricter flags. |
| 42 |
--- |
| 43 |
.gitattributes | 3 +++ |
| 44 |
.gitignore | 1 + |
| 45 |
CMakeLists.txt | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
| 46 |
Makefile | 32 +++++++++++++++++++++----------- |
| 47 |
VERSION | 1 + |
| 48 |
VERSION_GIT_HASH | 1 + |
| 49 |
VERSION_GIT_TIMESTAMP | 1 + |
| 50 |
7 files changed, 82 insertions(+), 28 deletions(-) |
| 51 |
create mode 100644 .gitattributes |
| 52 |
create mode 100644 VERSION |
| 53 |
create mode 100644 VERSION_GIT_HASH |
| 54 |
create mode 100644 VERSION_GIT_TIMESTAMP |
| 55 |
|
| 56 |
Index: railcontrol/.gitattributes |
| 57 |
=================================================================== |
| 58 |
--- /dev/null 1970-01-01 00:00:00.000000000 +0000 |
| 59 |
+++ railcontrol/.gitattributes 2026-01-09 14:13:15.812219371 +0100 |
| 60 |
@@ -0,0 +1,3 @@ |
| 61 |
+VERSION export-subst |
| 62 |
+VERSION_GIT_HASH export-subst |
| 63 |
+VERSION_GIT_TIMESTAMP export-subst |
| 64 |
Index: railcontrol/.gitignore |
| 65 |
=================================================================== |
| 66 |
--- railcontrol.orig/.gitignore 2026-01-09 14:13:15.820340288 +0100 |
| 67 |
+++ railcontrol/.gitignore 2026-01-09 14:13:15.812764854 +0100 |
| 68 |
@@ -20,6 +20,7 @@ |
| 69 |
svg/* |
| 70 |
Version.cpp |
| 71 |
.settings/ |
| 72 |
+build/ |
| 73 |
CMakeCache.txt |
| 74 |
CMakeFiles/* |
| 75 |
cmake_install.cmake |
| 76 |
Index: railcontrol/CMakeLists.txt |
| 77 |
=================================================================== |
| 78 |
--- railcontrol.orig/CMakeLists.txt 2026-01-09 14:13:15.820340288 +0100 |
| 79 |
+++ railcontrol/CMakeLists.txt 2026-01-09 14:13:15.813144234 +0100 |
| 80 |
@@ -1,10 +1,55 @@ |
| 81 |
cmake_minimum_required(VERSION 3.5) |
| 82 |
|
| 83 |
-set(RAILCONTROL_VERSION "24") |
| 84 |
+if(EXISTS "${CMAKE_SOURCE_DIR}/.git") |
| 85 |
+ find_package(Git QUIET) |
| 86 |
+ if(NOT Git_FOUND) |
| 87 |
+ message(WARNING "Git not found") |
| 88 |
+ return() |
| 89 |
+ endif() |
| 90 |
|
| 91 |
-project(CMakeTest) |
| 92 |
+ execute_process( |
| 93 |
+ COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 |
| 94 |
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 95 |
+ OUTPUT_VARIABLE RAILCONTROL_VERSION |
| 96 |
+ OUTPUT_STRIP_TRAILING_WHITESPACE |
| 97 |
+ ) |
| 98 |
+ execute_process( |
| 99 |
+ COMMAND ${GIT_EXECUTABLE} log -1 --format=%H |
| 100 |
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 101 |
+ OUTPUT_VARIABLE GIT_HASH |
| 102 |
+ OUTPUT_STRIP_TRAILING_WHITESPACE |
| 103 |
+ ) |
| 104 |
+ execute_process( |
| 105 |
+ COMMAND ${GIT_EXECUTABLE} log -1 --format=%at |
| 106 |
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 107 |
+ OUTPUT_VARIABLE GIT_TIMESTAMP |
| 108 |
+ OUTPUT_STRIP_TRAILING_WHITESPACE |
| 109 |
+ ) |
| 110 |
+ execute_process( |
| 111 |
+ COMMAND ${GIT_EXECUTABLE} status -s |
| 112 |
+ COMMAND wc -l |
| 113 |
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 114 |
+ OUTPUT_VARIABLE GIT_DIRTY |
| 115 |
+ OUTPUT_STRIP_TRAILING_WHITESPACE |
| 116 |
+ ) |
| 117 |
+else() |
| 118 |
+ file(READ VERSION RAILCONTROL_VERSION) |
| 119 |
+ string(STRIP ${RAILCONTROL_VERSION} RAILCONTROL_VERSION) |
| 120 |
+ file(READ VERSION_GIT_HASH GIT_HASH) |
| 121 |
+ string(STRIP ${GIT_HASH} GIT_HASH) |
| 122 |
+ file(READ VERSION_GIT_TIMESTAMP GIT_TIMESTAMP) |
| 123 |
+ string(STRIP ${GIT_TIMESTAMP} GIT_TIMESTAMP) |
| 124 |
+ set(GIT_DIRTY 0) |
| 125 |
+endif() |
| 126 |
|
| 127 |
-add_executable(railcontrol |
| 128 |
+# Version starts with v |
| 129 |
+string(REGEX REPLACE "^v" "" RAILCONTROL_VERSION "${RAILCONTROL_VERSION}") |
| 130 |
+ |
| 131 |
+project(railcontrol VERSION ${RAILCONTROL_VERSION}) |
| 132 |
+ |
| 133 |
+STRING(TIMESTAMP COMPILE_TIMESTAMP "%s" UTC) |
| 134 |
+ |
| 135 |
+add_executable(${PROJECT_NAME} |
| 136 |
Storage/sqlite/sqlite3.c |
| 137 |
Storage/sqlite/sqlite3.h |
| 138 |
Storage/sqlite/sqlite3ext.h |
| 139 |
@@ -270,9 +315,7 @@ |
| 140 |
Utils/ThreadSafeQueue.h |
| 141 |
Utils/Utils.cpp |
| 142 |
Utils/Utils.h |
| 143 |
-Version.cpp |
| 144 |
-Version.cpp.dummy |
| 145 |
-Version.cpp.in |
| 146 |
+${CMAKE_CURRENT_BINARY_DIR}/Version.cpp |
| 147 |
Version.h |
| 148 |
Hardware/zlib/adler32.c |
| 149 |
Hardware/zlib/compress.c |
| 150 |
@@ -307,19 +350,13 @@ |
| 151 |
set(CMAKE_CXX_STANDARD_REQUIRED True) |
| 152 |
set(CMAKE_CXX_FLAGS_DEBUG "-O0") |
| 153 |
set(CMAKE_CXX_FLAGS_RELEASE "-O2") |
| 154 |
-set(CMAKE_CXX_FLAGS "-g -Wall -Wextra -pedantic -Werror") |
| 155 |
-set(CMAKE_C_FLAGS "-g -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DHAVE_USLEEP -Wno-implicit-function-declaration -Wno-return-local-addr") |
| 156 |
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -pedantic -Werror") |
| 157 |
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DHAVE_USLEEP -Wno-implicit-function-declaration -Wno-return-local-addr") |
| 158 |
|
| 159 |
set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 160 |
find_package(Threads REQUIRED) |
| 161 |
-target_link_libraries(railcontrol PRIVATE Threads::Threads dl) |
| 162 |
- |
| 163 |
-target_include_directories(railcontrol PRIVATE .) |
| 164 |
+target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads dl) |
| 165 |
|
| 166 |
-add_custom_command(OUTPUT Version.cpp Version.cpp.dummy |
| 167 |
- DEPENDS Version.cpp.in |
| 168 |
- COMMAND git status -s| wc -l > ${CMAKE_CURRENT_SOURCE_DIR}/git_dirty.txt |
| 169 |
- COMMAND sed s/@COMPILE_TIMESTAMP@/`date +%s`/ < ${CMAKE_CURRENT_SOURCE_DIR}/Version.cpp.in | sed s/@GIT_HASH@/`git log -1 --format=%H`/ | sed s/@GIT_TIMESTAMP@/`git log -1 --format=%at`/ | sed s,@GIT_DIRTY@,`cat ${CMAKE_CURRENT_SOURCE_DIR}/git_dirty.txt`, | sed s/@RAILCONTROL_VERSION@/${RAILCONTROL_VERSION}/ > ${CMAKE_CURRENT_SOURCE_DIR}/Version.cpp |
| 170 |
- COMMAND rm ${CMAKE_CURRENT_SOURCE_DIR}/git_dirty.txt |
| 171 |
- COMMENT "Creating Version.cpp") |
| 172 |
+target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}) |
| 173 |
|
| 174 |
+configure_file(Version.cpp.in Version.cpp @ONLY) |
| 175 |
Index: railcontrol/Makefile |
| 176 |
=================================================================== |
| 177 |
--- railcontrol.orig/Makefile 2026-01-09 14:13:15.820340288 +0100 |
| 178 |
+++ railcontrol/Makefile 2026-01-09 14:13:15.813543853 +0100 |
| 179 |
@@ -1,5 +1,15 @@ |
| 180 |
- |
| 181 |
-RAILCONTROL_VERSION := 24 |
| 182 |
+ifeq ("$(wildcard .git)", "") |
| 183 |
+RAILCONTROL_VERSION=$(shell cat VERSION | sed s/v//) |
| 184 |
+GIT_HASH=$(shell cat VERSION_GIT_HASH) |
| 185 |
+GIT_TIMESTAMP=$(shell cat VERSION_GIT_TIMESTAMP) |
| 186 |
+GIT_DIRTY=0 |
| 187 |
+else |
| 188 |
+.PHONY: Version.cpp |
| 189 |
+RAILCONTROL_VERSION=$(shell git describe --tags --abbrev=0 | sed s/v//) |
| 190 |
+GIT_HASH=$(shell git log -1 --format=%H) |
| 191 |
+GIT_TIMESTAMP=$(shell git log -1 --format=%at) |
| 192 |
+GIT_DIRTY=$(shell git status -s | wc -l) |
| 193 |
+endif |
| 194 |
|
| 195 |
CFLAGSSQLITE=-g -O2 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE -DHAVE_USLEEP |
| 196 |
CFLAGSZLIB=-g -O2 -Wno-implicit-function-declaration -Wno-\#warnings -Wno-deprecated -Wno-deprecated-non-prototype |
| 197 |
@@ -20,6 +30,8 @@ |
| 198 |
COBJ= $(patsubst %.c,%.o,$(wildcard Hardware/zlib/*.c)) |
| 199 |
OBJ=Storage/sqlite/sqlite3.o $(CXXOBJ) $(COBJ) |
| 200 |
|
| 201 |
+SOURCE_DATE_EPOCH?=$(shell date +%s) |
| 202 |
+ |
| 203 |
all: $(OBJ) |
| 204 |
$(CXX) $(LDFLAGS) $(OBJ) -o railcontrol $(LIBS) |
| 205 |
|
| 206 |
@@ -90,12 +102,10 @@ |
| 207 |
tools: |
| 208 |
make -C tools |
| 209 |
|
| 210 |
-.PHONY: Version.cpp |
| 211 |
-Version.cpp: Version.cpp.in |
| 212 |
- sed s/@COMPILE_TIMESTAMP@/`date +%s`/ < Version.cpp.in \ |
| 213 |
- | sed s/@GIT_HASH@/`git log -1 --format=%H`/ \ |
| 214 |
- | sed s/@GIT_TIMESTAMP@/`git log -1 --format=%at`/ \ |
| 215 |
- | sed "s/@GIT_DIRTY@/`git status -s| wc -l`/" \ |
| 216 |
- | sed s/@RAILCONTROL_VERSION@/$(RAILCONTROL_VERSION)/ \ |
| 217 |
- > Version.cpp |
| 218 |
- |
| 219 |
+Version.cpp: Version.cpp.in VERSION VERSION_GIT_HASH VERSION_GIT_TIMESTAMP |
| 220 |
+ sed -e s/@COMPILE_TIMESTAMP@/$(SOURCE_DATE_EPOCH)/ \ |
| 221 |
+ -e s/@GIT_HASH@/$(GIT_HASH)/ \ |
| 222 |
+ -e s/@GIT_TIMESTAMP@/$(GIT_TIMESTAMP)/ \ |
| 223 |
+ -e s/@GIT_DIRTY@/$(GIT_DIRTY)/ \ |
| 224 |
+ -e s/@RAILCONTROL_VERSION@/$(RAILCONTROL_VERSION)/ \ |
| 225 |
+ < $< > $@ |
| 226 |
Index: railcontrol/VERSION |
| 227 |
=================================================================== |
| 228 |
--- /dev/null 1970-01-01 00:00:00.000000000 +0000 |
| 229 |
+++ railcontrol/VERSION 2026-01-09 14:13:15.813731014 +0100 |
| 230 |
@@ -0,0 +1 @@ |
| 231 |
+$Format:%(describe:tags=true,abbrev=0)$ |
| 232 |
Index: railcontrol/VERSION_GIT_HASH |
| 233 |
=================================================================== |
| 234 |
--- /dev/null 1970-01-01 00:00:00.000000000 +0000 |
| 235 |
+++ railcontrol/VERSION_GIT_HASH 2026-01-09 14:13:15.814119014 +0100 |
| 236 |
@@ -0,0 +1 @@ |
| 237 |
+$Format:%H$ |
| 238 |
Index: railcontrol/VERSION_GIT_TIMESTAMP |
| 239 |
=================================================================== |
| 240 |
--- /dev/null 1970-01-01 00:00:00.000000000 +0000 |
| 241 |
+++ railcontrol/VERSION_GIT_TIMESTAMP 2026-01-09 14:13:15.814699342 +0100 |
| 242 |
@@ -0,0 +1 @@ |
| 243 |
+$Format:%at$ |