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