blob: 48c08473b451e4f1f8fd7602d4557588e5886fd3 [file] [log] [blame]
Logan Chien48dc01d2011-07-13 18:12:06 +08001#!/usr/bin/env python
Zonr Changfa52e202012-04-12 15:38:42 +08002#
3# Copyright (C) 2011-2012 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Logan Chien48dc01d2011-07-13 18:12:06 +080017
18import datetime
Logan Chien48dc01d2011-07-13 18:12:06 +080019import os
20import re
21import sys
22import subprocess
23
Daniel Sandler1c131472011-07-18 10:59:17 -040024try:
25 import hashlib
26 sha1 = hashlib.sha1
27except ImportError, e:
28 import sha
29 sha1 = sha.sha
30
Logan Chien48dc01d2011-07-13 18:12:06 +080031def get_repo_revision(repo_dir):
32 if not os.path.exists(os.path.join(repo_dir, '.git')):
33 return 'Unknown (not git)'
34
Logan Chien3bfcd862011-07-13 20:11:30 +080035 # Get the HEAD revision
Logan Chien48dc01d2011-07-13 18:12:06 +080036 proc = subprocess.Popen(['git', 'log', '-1', '--format=%H'],
37 stdout=subprocess.PIPE,
38 stderr=subprocess.PIPE,
39 cwd=repo_dir)
40 out, err = proc.communicate()
41 proc.wait()
42
Logan Chien3bfcd862011-07-13 20:11:30 +080043 rev_sha1 = out.strip()
44
45 # Working Directory Modified
46 proc = subprocess.Popen(['git', 'status'],
47 stdout=subprocess.PIPE,
48 stderr=subprocess.PIPE,
49 cwd=repo_dir)
50 out, err = proc.communicate()
51 proc.wait()
52
Logan Chien76bb5882011-07-14 21:28:56 +080053 if out.find('(working directory clean)') == -1:
Daniel Sandler1c131472011-07-18 10:59:17 -040054 mod = ' modified'
55 else:
56 mod = ''
Logan Chien3bfcd862011-07-13 20:11:30 +080057
58 return rev_sha1 + mod + ' (git)'
Logan Chien48dc01d2011-07-13 18:12:06 +080059
60def compute_sha1(path, global_hasher = None):
61 f = open(path, 'rb')
Daniel Sandler1c131472011-07-18 10:59:17 -040062 hasher = sha1()
Logan Chien48dc01d2011-07-13 18:12:06 +080063 while True:
64 buf = f.read(512)
65 hasher.update(buf)
66 if global_hasher:
67 global_hasher.update(buf)
68 if len(buf) < 512:
69 break
70 f.close()
71 return hasher.hexdigest()
72
73def compute_sha1_list(paths):
Daniel Sandler1c131472011-07-18 10:59:17 -040074 hasher = sha1()
Logan Chien48dc01d2011-07-13 18:12:06 +080075 sha1sums = []
76 for path in paths:
77 sha1sums.append(compute_sha1(path, hasher))
78 return (hasher.hexdigest(), sha1sums)
79
80def quote_str(s):
81 result = '"'
82 for c in s:
83 if c == '\\':
84 result += '\\\\'
85 elif c == '\r':
86 result += '\\r'
87 elif c == '\n':
88 result += '\\n'
89 elif c == '\t':
90 result += '\\t'
91 elif c == '\"':
92 result += '\\"'
93 elif c == '\'':
94 result += '\\\''
95 else:
96 result += c
97 result += '"'
98 return result
99
100def main():
101 # Check Argument
102 if len(sys.argv) < 2:
103 print >> sys.stderr, 'USAGE:', sys.argv[0], '[REPO] [LIBs]'
104 sys.exit(1)
105
106 # Record Build Time
107 build_time = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
108
109 # Repository Directory (For build revision)
110 repo_dir = sys.argv[1]
111 build_rev = get_repo_revision(repo_dir)
112
113 # Compute SHA1
114 lib_list = list(set(sys.argv[2:]))
115 lib_list.sort()
116 build_sha1, sha1sum_list = compute_sha1_list(lib_list)
117
118 # Build file list string
119 lib_list_str = ''
120 for i, path in enumerate(lib_list):
121 lib_list_str += ' %s %s\n' % (sha1sum_list[i], path)
122
123 # Print the automatically generated code
124 print """/* Automatically generated file (DON'T MODIFY) */
125
126/* Repository directory: %s */
127
128/* File list:
129%s*/
130
Stephen Hinesaa72a212012-05-03 12:27:04 -0700131#ifdef __cplusplus
132extern "C" {
133#endif
Logan Chien48dc01d2011-07-13 18:12:06 +0800134
Stephen Hinesaa72a212012-05-03 12:27:04 -0700135char const *bccGetBuildTime() {
Logan Chien48dc01d2011-07-13 18:12:06 +0800136 return %s;
137}
138
Stephen Hinesaa72a212012-05-03 12:27:04 -0700139char const *bccGetBuildRev() {
Logan Chien48dc01d2011-07-13 18:12:06 +0800140 return %s;
141}
142
Stephen Hinesaa72a212012-05-03 12:27:04 -0700143char const *bccGetBuildSHA1() {
Logan Chien48dc01d2011-07-13 18:12:06 +0800144 return %s;
145}
146
Stephen Hinesaa72a212012-05-03 12:27:04 -0700147#ifdef __cplusplus
148}
149#endif
150
Logan Chien48dc01d2011-07-13 18:12:06 +0800151""" % (os.path.abspath(repo_dir),
152 lib_list_str,
153 quote_str(build_time),
154 quote_str(build_rev),
155 quote_str(build_sha1))
156
157if __name__ == '__main__':
158 main()