| 1 |
#!/usr/bin/python3 |
| 2 |
# This software has been developed by Advanced Driver Information Technology. |
| 3 |
# Copyright(c) 2019 Advanced Driver Information Technology GmbH, |
| 4 |
# Advanced Driver Information Technology Corporation, Robert Bosch GmbH, |
| 5 |
# Robert Bosch Car Multimedia GmbH and DENSO Corporation. |
| 6 |
# |
| 7 |
# This file is part of COVESA Project DLT - Diagnostic Log and Trace. |
| 8 |
# |
| 9 |
# This Source Code Form is subject to the terms of the |
| 10 |
# Mozilla Public License (MPL), v. 2.0. |
| 11 |
# If a copy of the MPL was not distributed with this file, |
| 12 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 13 |
# |
| 14 |
# For further information see http://www.covesa.org/. |
| 15 |
import pathlib |
| 16 |
import subprocess |
| 17 |
import argparse |
| 18 |
import re |
| 19 |
|
| 20 |
|
| 21 |
def get_cmd(cmd, cwd): |
| 22 |
return subprocess.check_output(cmd, cwd=cwd, shell=True, |
| 23 |
stderr=subprocess.DEVNULL, |
| 24 |
).decode().strip() |
| 25 |
|
| 26 |
|
| 27 |
def get_revision(git_dir): |
| 28 |
try: |
| 29 |
return get_cmd('git describe --tags --always --dirty', git_dir) |
| 30 |
except subprocess.CalledProcessError: |
| 31 |
pass |
| 32 |
|
| 33 |
return get_cmd('date +%F', git_dir) |
| 34 |
|
| 35 |
|
| 36 |
def main(cmake_file, header_in_file, header_out_file): |
| 37 |
cmakelists = pathlib.Path(cmake_file) |
| 38 |
header_in = pathlib.Path(header_in_file) |
| 39 |
header_out = pathlib.Path(header_out_file) |
| 40 |
git_dir = str(header_in.parent) |
| 41 |
cmake_vars = {} |
| 42 |
|
| 43 |
for m in re.finditer( |
| 44 |
'project\(\S+ VERSION (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\.(?P<tweak>\d+))?', |
| 45 |
cmakelists.open().read()): |
| 46 |
cmake_vars['PROJECT_VERSION_MAJOR'] = m.group('major') |
| 47 |
cmake_vars['PROJECT_VERSION_MINOR'] = m.group('minor') |
| 48 |
cmake_vars['PROJECT_VERSION_PATCH'] = m.group('patch') |
| 49 |
cmake_vars['PROJECT_VERSION'] = "{}.{}.{}".format(m.group('major'), m.group('minor'), m.group('patch')) |
| 50 |
cmake_vars['PROJECT_VERSION_TWEAK'] = m.group('tweak') |
| 51 |
cmake_vars['DLT_REVISION'] = get_revision(git_dir) |
| 52 |
cmake_vars['DLT_VERSION_STATE'] = 'STABLE' |
| 53 |
|
| 54 |
header_out.parent.mkdir(parents=True, exist_ok=True) |
| 55 |
with header_in.open() as hi, header_out.open('w') as ho: |
| 56 |
for line in hi: |
| 57 |
text, _ = re.subn('@(?P<var_name>\w+)@', lambda x: cmake_vars.get(x.group('var_name'), "NONE"), line) |
| 58 |
ho.write(text) |
| 59 |
|
| 60 |
|
| 61 |
if __name__ == '__main__': |
| 62 |
parser = argparse.ArgumentParser() |
| 63 |
parser.add_argument('cmakelists') |
| 64 |
parser.add_argument('header_in') |
| 65 |
parser.add_argument('header_out') |
| 66 |
args = parser.parse_args() |
| 67 |
main(args.cmakelists, args.header_in, args.header_out) |