xref: /aosp_15_r20/external/swiftshader/src/commit_id.py (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker#!/usr/bin/env python
2*03ce13f7SAndroid Build Coastguard Worker# Copyright 2019 The SwiftShader Authors. All Rights Reserved.
3*03ce13f7SAndroid Build Coastguard Worker#
4*03ce13f7SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*03ce13f7SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*03ce13f7SAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*03ce13f7SAndroid Build Coastguard Worker#
8*03ce13f7SAndroid Build Coastguard Worker#    http://www.apache.org/licenses/LICENSE-2.0
9*03ce13f7SAndroid Build Coastguard Worker#
10*03ce13f7SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*03ce13f7SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*03ce13f7SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*03ce13f7SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*03ce13f7SAndroid Build Coastguard Worker# limitations under the License.
15*03ce13f7SAndroid Build Coastguard Worker#
16*03ce13f7SAndroid Build Coastguard Worker# Generate commit.h with git commit hash.
17*03ce13f7SAndroid Build Coastguard Worker#
18*03ce13f7SAndroid Build Coastguard Worker
19*03ce13f7SAndroid Build Coastguard Workerimport subprocess as sp
20*03ce13f7SAndroid Build Coastguard Workerimport sys
21*03ce13f7SAndroid Build Coastguard Workerimport os
22*03ce13f7SAndroid Build Coastguard Worker
23*03ce13f7SAndroid Build Coastguard Workerusage = """\
24*03ce13f7SAndroid Build Coastguard WorkerUsage: commit_id.py check                 - check if git is present
25*03ce13f7SAndroid Build Coastguard Worker       commit_id.py gen <file_to_write>   - generate commit.h"""
26*03ce13f7SAndroid Build Coastguard Worker
27*03ce13f7SAndroid Build Coastguard Worker
28*03ce13f7SAndroid Build Coastguard Workerdef grab_output(command, cwd):
29*03ce13f7SAndroid Build Coastguard Worker    return sp.Popen(command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip()
30*03ce13f7SAndroid Build Coastguard Worker
31*03ce13f7SAndroid Build Coastguard Worker
32*03ce13f7SAndroid Build Coastguard Workerif len(sys.argv) < 2:
33*03ce13f7SAndroid Build Coastguard Worker    sys.exit(usage)
34*03ce13f7SAndroid Build Coastguard Worker
35*03ce13f7SAndroid Build Coastguard Workeroperation = sys.argv[1]
36*03ce13f7SAndroid Build Coastguard Workercwd = sys.path[0]
37*03ce13f7SAndroid Build Coastguard Worker
38*03ce13f7SAndroid Build Coastguard Workerif operation == 'check':
39*03ce13f7SAndroid Build Coastguard Worker    index_path = os.path.join(cwd, '.git', 'index')
40*03ce13f7SAndroid Build Coastguard Worker    if os.path.exists(index_path):
41*03ce13f7SAndroid Build Coastguard Worker        print("1")
42*03ce13f7SAndroid Build Coastguard Worker    else:
43*03ce13f7SAndroid Build Coastguard Worker        print("0")
44*03ce13f7SAndroid Build Coastguard Worker    sys.exit(0)
45*03ce13f7SAndroid Build Coastguard Worker
46*03ce13f7SAndroid Build Coastguard Workerif len(sys.argv) < 3 or operation != 'gen':
47*03ce13f7SAndroid Build Coastguard Worker    sys.exit(usage)
48*03ce13f7SAndroid Build Coastguard Worker
49*03ce13f7SAndroid Build Coastguard Workeroutput_file = sys.argv[2]
50*03ce13f7SAndroid Build Coastguard Workercommit_id_size = 12
51*03ce13f7SAndroid Build Coastguard Worker
52*03ce13f7SAndroid Build Coastguard Workercommit_id = 'invalid-hash'
53*03ce13f7SAndroid Build Coastguard Workercommit_date = 'invalid-date'
54*03ce13f7SAndroid Build Coastguard Worker
55*03ce13f7SAndroid Build Coastguard Workertry:
56*03ce13f7SAndroid Build Coastguard Worker    commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd)
57*03ce13f7SAndroid Build Coastguard Worker    commit_date = grab_output('git show -s --format=%ci HEAD', cwd)
58*03ce13f7SAndroid Build Coastguard Workerexcept:
59*03ce13f7SAndroid Build Coastguard Worker    pass
60*03ce13f7SAndroid Build Coastguard Worker
61*03ce13f7SAndroid Build Coastguard Workerhfile = open(output_file, 'w')
62*03ce13f7SAndroid Build Coastguard Worker
63*03ce13f7SAndroid Build Coastguard Workerhfile.write('#define SWIFTSHADER_COMMIT_HASH "%s"\n' % commit_id)
64*03ce13f7SAndroid Build Coastguard Workerhfile.write('#define SWIFTSHADER_COMMIT_HASH_SIZE %d\n' % commit_id_size)
65*03ce13f7SAndroid Build Coastguard Workerhfile.write('#define SWIFTSHADER_COMMIT_DATE "%s"\n' % commit_date)
66*03ce13f7SAndroid Build Coastguard Workerhfile.write('#define SWIFTSHADER_VERSION_STRING    \\\n'
67*03ce13f7SAndroid Build Coastguard Worker            'MACRO_STRINGIFY(MAJOR_VERSION) \".\"  \\\n'
68*03ce13f7SAndroid Build Coastguard Worker            'MACRO_STRINGIFY(MINOR_VERSION) \".\"  \\\n'
69*03ce13f7SAndroid Build Coastguard Worker            'MACRO_STRINGIFY(PATCH_VERSION) \".\"  \\\n'
70*03ce13f7SAndroid Build Coastguard Worker            'SWIFTSHADER_COMMIT_HASH\n')
71*03ce13f7SAndroid Build Coastguard Worker
72*03ce13f7SAndroid Build Coastguard Workerhfile.close()
73