Skip to content

Instantly share code, notes, and snippets.

@m-ou-se
Last active September 18, 2017 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-ou-se/3ec6aa02b66f702929dfae6024ea2ca3 to your computer and use it in GitHub Desktop.
Save m-ou-se/3ec6aa02b66f702929dfae6024ea2ca3 to your computer and use it in GitHub Desktop.
Automatically include git version in software.
ifeq (${VERBOSE},)
MAKEFLAGS += -s
endif
ifeq ($(OS),Windows_NT)
PY = py
else
PY =
endif
# Will be './' or '../'.
ROOT_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
# This makes sure version.h is generated before any source file has the chance
# to include it, but does not make the objects depend on it.
${OBJECTS} ${OBJS}: | pre-build-script
.PHONY: pre-build-script
pre-build-script:
$(PY) $(ROOT_DIR)pre-build.py
$(wildcard $(ROOT_DIR)/src/generated/*): pre-build-script
@
.PHONY: clean-generated
clean-generated:
$(PY) $(ROOT_DIR)pre-build.py clean
clean: clean-generated
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
os.chdir(os.path.dirname(os.path.abspath(__file__)))
git_desc = subprocess.check_output(
['git', 'describe', '--always', '--dirty=-modified']
).decode().strip()
version_h = '// This file is automatically generated.\n'
version_h += '#pragma once\n'
version_h += '#define VERSION "{}"\n'.format(git_desc)
def write_if_changed(file_name, content):
if os.path.exists(file_name):
with open(file_name, 'r') as f:
c = f.read()
if c == content:
return
with open(file_name, 'w') as f:
f.write(content)
print('UPDATE ' + file_name)
os.makedirs('src/generated', exist_ok=True)
write_if_changed('src/generated/version.h', version_h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment