make/meson: Generate a `version.h'

Using a header file allows us to  keep the version string out of the
compiler command line. This should heavily increase direct hits with
ccache. Specifying the compiler argument for a single source file is
rather clumsy with Meson, hence the more pompous header file.

Change-Id: If6c4e33e4944d2b264252dbcd2fd69ecf7bd8905
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/355
Reviewed-by: Felix Singer <felixsinger@posteo.net>
diff --git a/.gitignore b/.gitignore
index 63ad6b9..f7eb943 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,7 @@
 /flashprog-*
 /flashprog.exe
 /flashprog.8
+/include/version.h
 /libflashprog.a
 /libflashprog-doc/
 /versioninfo.inc
diff --git a/Makefile b/Makefile
index 12adf61..2d94afe 100644
--- a/Makefile
+++ b/Makefile
@@ -409,8 +409,6 @@
 VERSION := $(shell ./util/getversion.sh --version)
 MAN_DATE := $(shell ./util/getversion.sh --man-date)
 
-SCMDEF := -D'FLASHPROG_VERSION="$(VERSION)"'
-
 # Inform user of the version string
 ifeq ($(filter branch tag,$(MAKECMDGOALS)), )
 $(info Replacing all version templates with $(VERSION).)
@@ -1003,8 +1001,13 @@
 		exit 1;								\
 	fi
 
+include/version.h: %.h: %.h.in
+	sed 's/@VERSION@/$(VERSION)/' $< >$@
+
+flashprog.o: include/version.h
+
 %.o: %.c | config
-	$(CC) -MMD $(CFLAGS) $(CPPFLAGS) $(FLASHPROG_CFLAGS) $(FEATURE_FLAGS) $(SCMDEF) -o $@ -c $<
+	$(CC) -MMD $(CFLAGS) $(CPPFLAGS) $(FLASHPROG_CFLAGS) $(FEATURE_FLAGS) -o $@ -c $<
 
 $(PROGRAM)$(EXEC_SUFFIX): $(CLI_OBJS) libflashprog.a
 	$(CC) -o $@ $^ $(LDFLAGS)
@@ -1028,7 +1031,7 @@
 # We don't use EXEC_SUFFIX here because we want to clean everything.
 clean:
 	rm -f $(PROGRAM) $(PROGRAM).exe libflashprog.a $(filter-out Makefile.d, $(wildcard *.d *.o platform/*.d platform/*.o)) \
-		$(MANS) $(MANS:.8=.8.html) $(BUILD_DETAILS_FILE)
+		include/version.h $(MANS) $(MANS:.8=.8.html) $(BUILD_DETAILS_FILE)
 	@+$(MAKE) -C util/ich_descriptors_tool/ clean
 
 install: $(PROGRAM)$(EXEC_SUFFIX) $(MANS)
diff --git a/flashprog.c b/flashprog.c
index 80e53b9..07025d9 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -33,6 +33,7 @@
 #include "programmer.h"
 #include "hwaccess_physmap.h"
 #include "chipdrivers.h"
+#include "version.h"
 
 const char flashprog_version[] = FLASHPROG_VERSION;
 const char *chip_to_probe = NULL;
diff --git a/include/version.h.in b/include/version.h.in
new file mode 100644
index 0000000..1bb7fd4
--- /dev/null
+++ b/include/version.h.in
@@ -0,0 +1,20 @@
+/*
+ * This file is part of the flashprog project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef FLASHPROG_VERSION_H
+#define FLASHPROG_VERSION_H
+
+#define FLASHPROG_VERSION "@VERSION@"
+
+#endif
diff --git a/meson.build b/meson.build
index 1cfe517..39ae0c4 100644
--- a/meson.build
+++ b/meson.build
@@ -37,7 +37,6 @@
 add_project_arguments('-D__XSI_VISIBLE', language : 'c') # required for gettimeofday() on FreeBSD
 add_project_arguments('-D_NETBSD_SOURCE', language : 'c') # required for indirect include of strings.h on NetBSD
 add_project_arguments('-D_DARWIN_C_SOURCE', language : 'c') # required for indirect include of strings.h on MacOS
-add_project_arguments('-DFLASHPROG_VERSION="' + meson.project_version() + '"', language : 'c')
 
 # get defaults from configure
 config_print_wiki = get_option('classic_cli_print_wiki')
@@ -590,14 +589,21 @@
   description : 'library to interact with flashprog',
 )
 
-config_manfile = configuration_data()
-config_manfile.set('VERSION', meson.project_version())
-config_manfile.set('MAN_DATE', run_command('util/getversion.sh', '--man-date', check : true).stdout().strip())
+config_data = configuration_data()
+config_data.set('VERSION', meson.project_version())
+config_data.set('MAN_DATE', run_command('util/getversion.sh', '--man-date', check : true).stdout().strip())
+
+configure_file(
+  input : 'include/version.h.in',
+  output : 'version.h',
+  configuration : config_data,
+)
+
 foreach man : [ 'flashprog.8', 'flashprog-config.8', 'flashprog-write-protect.8' ]
   configure_file(
     input : man + '.tmpl',
     output : man,
-    configuration : config_manfile,
+    configuration : config_data,
     install: true,
     install_dir: join_paths(get_option('mandir'), 'man8'),
   )