Last active
September 18, 2017 08:20
-
-
Save m-ou-se/3ec6aa02b66f702929dfae6024ea2ca3 to your computer and use it in GitHub Desktop.
Automatically include git version in software.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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