Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # This utility enumerates the BoringSSL source in src/ and generates two gypi | |
|
M-A Ruel
2014/07/09 00:24:21
Add chromium copyright
agl
2014/07/09 16:42:01
Done.
| |
| 2 # files: boringssl.gypi and boringssl_tests.gypi | |
| 3 | |
| 4 import os | |
| 5 import os.path | |
|
M-A Ruel
2014/07/09 00:24:21
not necessary
agl
2014/07/09 16:42:01
Done.
| |
| 6 import stat | |
| 7 import subprocess | |
| 8 | |
| 9 | |
| 10 def FindCMakeFiles(directory): | |
| 11 '''FindCMakeFiles recurses through directory and returns a list of the paths | |
|
M-A Ruel
2014/07/09 00:24:21
""" and verb for docstring, e.g.
"""Returns list
agl
2014/07/09 16:42:01
Done.
| |
| 12 to all CMakeLists.txt files.''' | |
| 13 cmakefiles = [] | |
| 14 dents = os.listdir(directory) | |
| 15 | |
| 16 for dent in dents: | |
|
M-A Ruel
2014/07/09 00:24:21
What you want is:
for root, dirs, files in os.walk
agl
2014/07/09 16:42:01
Done.
| |
| 17 path = os.path.join(directory, dent) | |
| 18 if dent == 'CMakeLists.txt': | |
| 19 cmakefiles.append(path) | |
| 20 continue | |
| 21 | |
| 22 statinfo = os.lstat(path) | |
| 23 if not stat.S_ISDIR(statinfo.st_mode): | |
| 24 continue | |
| 25 | |
| 26 cmakefiles.extend(FindCMakeFiles(path)) | |
| 27 | |
| 28 return cmakefiles | |
| 29 | |
| 30 | |
| 31 def NoTests(dent, is_dir): | |
| 32 '''NoTests is a filter function that can be passed to FindCFiles in order to | |
| 33 remove test sources.''' | |
| 34 if is_dir: | |
| 35 return dent != 'test' | |
| 36 return 'test.' not in dent and not dent.startswith('example_') | |
| 37 | |
| 38 | |
| 39 def OnlyTests(dent, is_dir): | |
| 40 '''OnlyTests is a filter function that can be passed to FindCFiles in order to | |
| 41 remove non-test sources.''' | |
| 42 if is_dir: | |
| 43 return True | |
| 44 return '_test.' in dent or dent.startswith('example_') | |
| 45 | |
| 46 | |
| 47 def FindCFiles(directory, filter_func): | |
| 48 '''FindCFiles recurses through directory and returns a list of paths to all | |
| 49 the C source files that pass filter_func.''' | |
| 50 cfiles = [] | |
| 51 dents = os.listdir(directory) | |
| 52 | |
| 53 for dent in dents: | |
|
M-A Ruel
2014/07/09 00:24:21
os.walk
agl
2014/07/09 16:42:01
Done.
| |
| 54 path = os.path.join(directory, dent) | |
| 55 if dent.endswith('.c') and filter_func(dent, False): | |
| 56 cfiles.append(path) | |
| 57 continue | |
| 58 | |
| 59 statinfo = os.lstat(path) | |
| 60 if not stat.S_ISDIR(statinfo.st_mode): | |
| 61 continue | |
| 62 | |
| 63 if not filter_func(dent, True): | |
| 64 continue | |
| 65 | |
| 66 cfiles.extend(FindCFiles(path, filter_func)) | |
| 67 | |
| 68 return cfiles | |
| 69 | |
| 70 | |
| 71 def ExtractPerlAsmFromCMakeFile(cmakefile): | |
| 72 '''ExtractPerlAsmFromCMakeFile parses the contents of the CMakeLists.txt file | |
| 73 passed as an argument and returns a list of all the perlasm() directives | |
| 74 found in the file.''' | |
| 75 perlasms = [] | |
| 76 f = file(cmakefile, 'r') | |
| 77 | |
| 78 for line in f.readlines(): | |
|
M-A Ruel
2014/07/09 00:24:21
with open(cmakefile) as f:
for line in f:
..
agl
2014/07/09 16:42:01
Done.
| |
| 79 line = line.strip() | |
| 80 if not line.startswith('perlasm('): | |
| 81 continue | |
| 82 if not line.endswith(')'): | |
| 83 raise ValueError('Bad perlasm line in %s' % cmakefile) | |
| 84 # Remove "perlasm(" from start and ")" from end | |
|
M-A Ruel
2014/07/09 00:24:21
Align comment with rest of script.
agl
2014/07/09 16:42:01
Done.
| |
| 85 line = line[8:-1] | |
| 86 params = line.split() | |
|
M-A Ruel
2014/07/09 00:24:21
params = line[8:-1].split()
agl
2014/07/09 16:42:00
Done.
| |
| 87 if len(params) < 2: | |
| 88 raise ValueError('Bad perlasm line in %s' % cmakefile) | |
| 89 perlasms.append({ | |
| 90 'output': os.path.join(os.path.dirname(cmakefile), params[0]), | |
|
M-A Ruel
2014/07/09 00:24:21
sort keys
agl
2014/07/09 16:42:00
Done.
| |
| 91 'input': os.path.join(os.path.dirname(cmakefile), params[1]), | |
| 92 'extra_args': params[2:], | |
| 93 }) | |
| 94 | |
| 95 f.close() | |
| 96 return perlasms | |
| 97 | |
| 98 | |
| 99 def ReadPerlAsmOperations(): | |
| 100 '''ReadPerlAsmOperations returns a list of all perlasm() directives found in | |
|
M-A Ruel
2014/07/09 00:24:21
"""Returns ...
agl
2014/07/09 16:42:01
Done.
| |
| 101 CMake config files in src/.''' | |
| 102 perlasms = [] | |
| 103 cmakefiles = FindCMakeFiles('src') | |
| 104 | |
| 105 for cmakefile in cmakefiles: | |
| 106 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile)) | |
| 107 | |
| 108 return perlasms | |
| 109 | |
| 110 # kOSArchCombos maps from OS and platform to the OpenSSL assembly "style" for | |
| 111 # that platform and the extension used by asm files. | |
| 112 kOSArchCombos = [ | |
|
M-A Ruel
2014/07/09 00:24:21
ALL_CAPS const at top of file.
agl
2014/07/09 16:42:01
Done.
| |
| 113 ('linux', 'arm', 'elf', 'S'), | |
| 114 ('linux', 'x86', 'elf', 'S'), | |
| 115 ('linux', 'x86_64', 'elf', 'S'), | |
| 116 ('mac', 'x86', 'macosx', 'S'), | |
| 117 ('mac', 'x86_64', 'macosx', 'S'), | |
| 118 ('win', 'x86_64', 'masm', 'asm'), | |
| 119 ] | |
| 120 | |
| 121 # kNonPerlFiles enumerates assembly files that are not processed by the perlasm | |
| 122 # system. | |
| 123 kNonPerlFiles = { | |
| 124 ('linux', 'arm'): [ | |
| 125 'src/crypto/poly1305/poly1305_arm_asm.S', | |
| 126 'src/crypto/chacha/chacha_vec_arm.S', | |
| 127 ], | |
| 128 } | |
| 129 | |
| 130 | |
| 131 def PerlAsm(output_filename, input_filename, perlasm_style, extra_args): | |
| 132 '''PerlAsm runs the a perlasm script and puts the output into | |
| 133 output_filename.''' | |
| 134 try: | |
| 135 os.makedirs(os.path.dirname(output_filename)) | |
| 136 except OSError: | |
| 137 pass | |
| 138 output = subprocess.check_output( | |
| 139 ['perl', input_filename, perlasm_style] + extra_args) | |
| 140 out_file = file(output_filename, 'w+') | |
|
M-A Ruel
2014/07/09 00:24:21
use with statement
agl
2014/07/09 16:42:00
Done.
| |
| 141 out_file.write(output) | |
| 142 out_file.close() | |
| 143 | |
| 144 | |
| 145 def WriteAsmFiles(perlasms): | |
| 146 '''WriteAsmFiles generates asm files from perlasm directives for each | |
| 147 supported OS x platform combination.''' | |
| 148 asmfiles = {} | |
| 149 | |
| 150 for osarch in kOSArchCombos: | |
| 151 (osname, arch, perlasm_style, asm_ext) = osarch | |
| 152 key = (osname, arch) | |
| 153 outDir = '%s-%s' % key | |
| 154 | |
| 155 for perlasm in perlasms: | |
| 156 filename = os.path.basename(perlasm['input']) | |
| 157 output = perlasm['output'] | |
| 158 if not output.startswith('src'): | |
| 159 raise ValueError('output missing src: %s' % output) | |
| 160 output = os.path.join(outDir, output[4:]) | |
| 161 output = output.replace('${ASM_EXT}', asm_ext) | |
| 162 | |
| 163 found = False | |
| 164 if (arch == 'x86_64' and | |
| 165 ('x86_64' in filename or 'avx2' in filename)): | |
| 166 found = True | |
| 167 elif (arch == 'x86' and | |
| 168 'x86' in filename and | |
| 169 'x86_64' not in filename): | |
| 170 found = True | |
| 171 elif (arch == 'arm' and | |
|
M-A Ruel
2014/07/09 00:24:21
this seems to fit 80 cols
agl
2014/07/09 16:42:01
Done.
| |
| 172 'arm' in filename): | |
| 173 found = True | |
| 174 | |
| 175 if found: | |
| 176 PerlAsm(output, perlasm['input'], perlasm_style, perlasm['extra_args']) | |
| 177 asmfiles[key] = asmfiles.get(key, []) + [output] | |
| 178 | |
| 179 for (key, non_perl_asm_files) in kNonPerlFiles.items(): | |
|
M-A Ruel
2014/07/09 00:24:21
s/items/iteritems/
agl
2014/07/09 16:42:01
Done.
| |
| 180 asmfiles[key] = asmfiles.get(key, []) + non_perl_asm_files | |
| 181 | |
| 182 return asmfiles | |
| 183 | |
| 184 | |
| 185 def PrintVariableSection(out, name, files): | |
| 186 out.write(""" '%s': [\n""" % name) | |
| 187 files.sort() | |
| 188 for f in files: | |
| 189 out.write(""" '%s',\n""" % f) | |
| 190 out.write(''' ],\n''') | |
| 191 | |
| 192 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests) | |
|
Ryan Sleevi
2014/07/09 00:03:35
Chromium Python style is to do all this in a main(
M-A Ruel
2014/07/09 00:24:21
Yes,
def main():
foo ...
bar ...
return 0
agl
2014/07/09 16:42:00
Done.
agl
2014/07/09 16:42:01
Done.
| |
| 193 ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests) | |
| 194 | |
| 195 gypi = file('boringssl.gypi', 'w+') | |
| 196 | |
| 197 header = '''# Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 198 # Use of this source code is governed by a BSD-style license that can be | |
| 199 # found in the LICENSE file. | |
| 200 | |
| 201 # This file is created by update_gypi_and_asm.py. Do not edit manually. | |
| 202 | |
| 203 ''' | |
| 204 | |
| 205 gypi.write(header + '''{ | |
| 206 'variables': {\n''') | |
| 207 | |
| 208 PrintVariableSection( | |
| 209 gypi, 'boringssl_lib_sources', crypto_c_files + ssl_c_files) | |
| 210 | |
| 211 perlasms = ReadPerlAsmOperations() | |
| 212 asmfiles = WriteAsmFiles(perlasms) | |
| 213 | |
| 214 for ((osname, arch), asm_files) in asmfiles.items(): | |
| 215 PrintVariableSection(gypi, 'boringssl_%s_%s_sources' % | |
| 216 (osname, arch), asm_files) | |
| 217 | |
| 218 gypi.write(''' } | |
| 219 }\n''') | |
| 220 | |
| 221 gypi.close() | |
| 222 | |
| 223 | |
| 224 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests) | |
| 225 | |
| 226 test_gypi = file('boringssl_tests.gypi', 'w+') | |
| 227 | |
| 228 test_gypi.write(header + '''{ | |
| 229 'targets': [\n''') | |
| 230 | |
| 231 test_names = [] | |
| 232 for test in test_c_files: | |
| 233 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0] | |
| 234 test_gypi.write(''' { | |
| 235 'target_name': '%s', | |
| 236 'type': 'executable', | |
| 237 'dependencies': [ | |
| 238 'boringssl', | |
| 239 ], | |
| 240 'sources': [ | |
| 241 '%s', | |
| 242 ], | |
| 243 },\n''' % (test_name, test)) | |
| 244 test_names.append(test_name) | |
| 245 | |
| 246 test_names.sort() | |
| 247 | |
| 248 test_gypi.write(''' ], | |
| 249 'variables': { | |
| 250 'boringssl_test_targets': [\n''') | |
| 251 | |
| 252 for test in test_names: | |
| 253 test_gypi.write(""" '%s',\n""" % test) | |
| 254 | |
| 255 test_gypi.write(''' ], | |
| 256 } | |
| 257 }\n''') | |
| 258 | |
| 259 test_gypi.close() | |
| OLD | NEW |