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