Start proper approach to determining how build_always targets should work by writing a proper test case illustrating how it should behave.
This commit is contained in:
parent
704a0b617c
commit
24484412b5
|
@ -0,0 +1,7 @@
|
||||||
|
#include<stdio.h>
|
||||||
|
#include"version.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
printf("Version is %s.\n", version_string);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
project('run always', 'c')
|
||||||
|
|
||||||
|
version = '1.0.0'
|
||||||
|
|
||||||
|
vgen = find_program('version_gen.py')
|
||||||
|
|
||||||
|
version_src = custom_target('Version string',
|
||||||
|
input : 'version.c.in',
|
||||||
|
output : 'version.c',
|
||||||
|
command : [vgen, '@INPUT@', '@OUTPUT@', version],
|
||||||
|
build_always : true,
|
||||||
|
)
|
||||||
|
|
||||||
|
executable('versionprinter', 'main.c', version_src,
|
||||||
|
include_directories : include_directories('.'))
|
|
@ -0,0 +1,3 @@
|
||||||
|
#include"version.h"
|
||||||
|
|
||||||
|
const char *version_string = "@VERSION@";
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
const char *version_string;
|
|
@ -0,0 +1,29 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import sys, os, subprocess
|
||||||
|
|
||||||
|
def generate(infile, outfile, fallback):
|
||||||
|
workdir = os.path.split(infile)[0]
|
||||||
|
if workdir == '':
|
||||||
|
workdir = '.'
|
||||||
|
p = subprocess.Popen(['git', 'describe'], cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
(stdo, _) = p.communicate()
|
||||||
|
# If we are working off an extracted tarball, git version number is not available.
|
||||||
|
if p.returncode == 0:
|
||||||
|
version = stdo.decode().strip()
|
||||||
|
else:
|
||||||
|
version = fallback
|
||||||
|
newdata = open(infile).read().replace('@VERSION@', version)
|
||||||
|
try:
|
||||||
|
olddata = open(outfile).read()
|
||||||
|
if olddata == newdata:
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
open(outfile, 'w').write(newdata)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
infile = sys.argv[1]
|
||||||
|
outfile = sys.argv[2]
|
||||||
|
fallback = sys.argv[3]
|
||||||
|
generate(infile, outfile, fallback)
|
Loading…
Reference in New Issue