Repository navigation
Expand file tree
/
Copy pathpre_build_script.py
More file actions
executable file
·115 lines (98 loc) · 3.88 KB
/
Copy pathpre_build_script.py
File metadata and controls
executable file
·115 lines (98 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# DAPLink Interface Firmware
# Copyright (c) 2009-2019, ARM Limited, All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import absolute_import
from __future__ import print_function
import sys
import os
import argparse
from subprocess import check_output, CalledProcessError
VERSION_GIT_FILE_TEMPLATE = """
/**
* @file version_git.h
* @brief GIT version info
*
* DAPLink Interface Firmware
* Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef VERSION_GIT_H
#define VERSION_GIT_H
#define GIT_DESCRIPTION \"%s\"
#define GIT_COMMIT_SHA \"%s\"
#define GIT_LOCAL_MODS %d
#endif
"""
def generate_version_file(version_git_dir):
# Get the git description.
print("#> Getting git description")
try:
git_description = check_output("git describe HEAD --always --tags", shell=True)
git_description = git_description.decode().strip()
except:
print("#> ERROR: Failed to get git description, do you have git.exe in your PATH environment variable?")
return 1
output_file = os.path.join(os.path.normpath(version_git_dir),'version_git.h')
print("#> Pre-build script start")
# Get the git SHA.
print("#> Getting git SHA")
try:
git_sha = check_output("git rev-parse --verify HEAD", shell=True)
git_sha = git_sha.decode().strip()
except:
print("#> ERROR: Failed to get git SHA, do you have git.exe in your PATH environment variable?")
return 1
# Check are there any local, uncommitted modifications.
print("#> Checking for local changes")
try:
check_output("git diff --no-ext-diff --quiet --exit-code", shell=True)
except (CalledProcessError, OSError):
git_has_changes = 1
else:
git_has_changes = 0
# Create the version file. Only overwrite an existing file if it changes.
version_text = VERSION_GIT_FILE_TEMPLATE % (git_description, git_sha, git_has_changes)
try:
with open(output_file, 'r') as version_file:
current_version_text = version_file.read()
except IOError:
current_version_text = ''
if version_text != current_version_text:
print("#> Writing git version file")
with open(output_file, 'w+') as version_file:
version_file.write(version_text)
else:
print("#> Keeping git version file since it didn't need to change")
print("#> Pre-build script completed written %s" % output_file )
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='git version generator')
parser.add_argument("--version_git_dir", type=str, default='../../../source/daplink/', help="directory to output version_git.h file")
args = parser.parse_args()
exit(generate_version_file(args.version_git_dir))