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:
Jussi Pakkanen 2015-02-07 14:33:43 +02:00
parent 704a0b617c
commit 24484412b5
5 changed files with 57 additions and 0 deletions

View File

@ -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;
}

View File

@ -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('.'))

View File

@ -0,0 +1,3 @@
#include"version.h"
const char *version_string = "@VERSION@";

View File

@ -0,0 +1,3 @@
#pragma once
const char *version_string;

View File

@ -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)