blob: 67e5e379904d26da4b7363c2ff85c59ff9299bea [file] [log] [blame]
Adam Langley9e1a6602015-05-05 17:47:53 -07001# Copyright (c) 2015, Google Inc.
2#
3# Permission to use, copy, modify, and/or distribute this software for any
4# purpose with or without fee is hereby granted, provided that the above
5# copyright notice and this permission notice appear in all copies.
6#
7# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
Matt Braithwaite16695892016-06-09 09:34:11 -070015"""Enumerates source files for consumption by various build systems."""
Adam Langley9e1a6602015-05-05 17:47:53 -070016
Matt Braithwaite16695892016-06-09 09:34:11 -070017import optparse
Adam Langley9e1a6602015-05-05 17:47:53 -070018import os
19import subprocess
20import sys
Adam Langley9c164b22015-06-10 18:54:47 -070021import json
Adam Langley9e1a6602015-05-05 17:47:53 -070022
23
24# OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for
25# that platform and the extension used by asm files.
26OS_ARCH_COMBOS = [
27 ('linux', 'arm', 'linux32', [], 'S'),
28 ('linux', 'aarch64', 'linux64', [], 'S'),
David Benjamin9f16ce12016-09-27 16:30:22 -040029 ('linux', 'ppc64le', 'ppc64le', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070030 ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
31 ('linux', 'x86_64', 'elf', [], 'S'),
32 ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
33 ('mac', 'x86_64', 'macosx', [], 'S'),
34 ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
35 ('win', 'x86_64', 'nasm', [], 'asm'),
36]
37
38# NON_PERL_FILES enumerates assembly files that are not processed by the
39# perlasm system.
40NON_PERL_FILES = {
41 ('linux', 'arm'): [
Adam Langley7b8b9c12016-01-04 07:13:00 -080042 'src/crypto/curve25519/asm/x25519-asm-arm.S',
David Benjamin3c4a5cb2016-03-29 17:43:31 -040043 'src/crypto/poly1305/poly1305_arm_asm.S',
Adam Langley9e1a6602015-05-05 17:47:53 -070044 ],
Matt Braithwaitee021a242016-01-14 13:41:46 -080045 ('linux', 'x86_64'): [
46 'src/crypto/curve25519/asm/x25519-asm-x86_64.S',
47 ],
Piotr Sikora8ca0b412016-06-02 11:59:21 -070048 ('mac', 'x86_64'): [
49 'src/crypto/curve25519/asm/x25519-asm-x86_64.S',
50 ],
Adam Langley9e1a6602015-05-05 17:47:53 -070051}
52
Matt Braithwaite16695892016-06-09 09:34:11 -070053PREFIX = None
54
55
56def PathOf(x):
57 return x if not PREFIX else os.path.join(PREFIX, x)
58
Adam Langley9e1a6602015-05-05 17:47:53 -070059
Adam Langley9e1a6602015-05-05 17:47:53 -070060class Android(object):
61
62 def __init__(self):
63 self.header = \
64"""# Copyright (C) 2015 The Android Open Source Project
65#
66# Licensed under the Apache License, Version 2.0 (the "License");
67# you may not use this file except in compliance with the License.
68# You may obtain a copy of the License at
69#
70# http://www.apache.org/licenses/LICENSE-2.0
71#
72# Unless required by applicable law or agreed to in writing, software
73# distributed under the License is distributed on an "AS IS" BASIS,
74# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
75# See the License for the specific language governing permissions and
76# limitations under the License.
77
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070078# This file is created by generate_build_files.py. Do not edit manually.
79
Adam Langley9e1a6602015-05-05 17:47:53 -070080"""
81
82 def PrintVariableSection(self, out, name, files):
83 out.write('%s := \\\n' % name)
84 for f in sorted(files):
85 out.write(' %s\\\n' % f)
86 out.write('\n')
87
88 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070089 # New Android.bp format
90 with open('sources.bp', 'w+') as blueprint:
91 blueprint.write(self.header.replace('#', '//'))
92
93 blueprint.write('cc_defaults {\n')
94 blueprint.write(' name: "libcrypto_sources",\n')
95 blueprint.write(' srcs: [\n')
David Benjamin8c29e7d2016-09-30 21:34:31 -040096 for f in sorted(files['crypto']):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070097 blueprint.write(' "%s",\n' % f)
98 blueprint.write(' ],\n')
99 blueprint.write(' target: {\n')
100
101 for ((osname, arch), asm_files) in asm_outputs:
Steven Valdez93d242b2016-10-06 13:49:01 -0400102 if osname != 'linux' or arch == 'ppc64le':
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700103 continue
104 if arch == 'aarch64':
105 arch = 'arm64'
106
107 blueprint.write(' android_%s: {\n' % arch)
108 blueprint.write(' srcs: [\n')
109 for f in sorted(asm_files):
110 blueprint.write(' "%s",\n' % f)
111 blueprint.write(' ],\n')
112 blueprint.write(' },\n')
113
114 if arch == 'x86' or arch == 'x86_64':
115 blueprint.write(' linux_%s: {\n' % arch)
116 blueprint.write(' srcs: [\n')
117 for f in sorted(asm_files):
118 blueprint.write(' "%s",\n' % f)
119 blueprint.write(' ],\n')
120 blueprint.write(' },\n')
121
122 blueprint.write(' },\n')
123 blueprint.write('}\n\n')
124
125 blueprint.write('cc_defaults {\n')
126 blueprint.write(' name: "libssl_sources",\n')
127 blueprint.write(' srcs: [\n')
128 for f in sorted(files['ssl']):
129 blueprint.write(' "%s",\n' % f)
130 blueprint.write(' ],\n')
131 blueprint.write('}\n\n')
132
133 blueprint.write('cc_defaults {\n')
134 blueprint.write(' name: "bssl_sources",\n')
135 blueprint.write(' srcs: [\n')
136 for f in sorted(files['tool']):
137 blueprint.write(' "%s",\n' % f)
138 blueprint.write(' ],\n')
139 blueprint.write('}\n\n')
140
141 blueprint.write('cc_defaults {\n')
142 blueprint.write(' name: "boringssl_test_support_sources",\n')
143 blueprint.write(' srcs: [\n')
144 for f in sorted(files['test_support']):
145 blueprint.write(' "%s",\n' % f)
146 blueprint.write(' ],\n')
147 blueprint.write('}\n\n')
148
149 blueprint.write('cc_defaults {\n')
150 blueprint.write(' name: "boringssl_tests_sources",\n')
151 blueprint.write(' srcs: [\n')
152 for f in sorted(files['test']):
153 blueprint.write(' "%s",\n' % f)
154 blueprint.write(' ],\n')
155 blueprint.write('}\n')
156
157 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700158 with open('sources.mk', 'w+') as makefile:
159 makefile.write(self.header)
160
David Benjamin8c29e7d2016-09-30 21:34:31 -0400161 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
Adam Langley9e1a6602015-05-05 17:47:53 -0700162
163 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700164 if osname != 'linux':
165 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700166 self.PrintVariableSection(
167 makefile, '%s_%s_sources' % (osname, arch), asm_files)
168
169
Adam Langley049ef412015-06-09 18:20:57 -0700170class Bazel(object):
171 """Bazel outputs files suitable for including in Bazel files."""
172
173 def __init__(self):
174 self.firstSection = True
175 self.header = \
176"""# This file is created by generate_build_files.py. Do not edit manually.
177
178"""
179
180 def PrintVariableSection(self, out, name, files):
181 if not self.firstSection:
182 out.write('\n')
183 self.firstSection = False
184
185 out.write('%s = [\n' % name)
186 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700187 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700188 out.write(']\n')
189
190 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700191 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700192 out.write(self.header)
193
194 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
195 self.PrintVariableSection(
196 out, 'ssl_internal_headers', files['ssl_internal_headers'])
197 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
198 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
199 self.PrintVariableSection(
200 out, 'crypto_internal_headers', files['crypto_internal_headers'])
201 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
202 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700203 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700204
205 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700206 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700207 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700208
Chuck Haysc608d6b2015-10-06 17:54:16 -0700209 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700210 out.write(self.header)
211
212 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400213 for filename in sorted(files['test_support'] +
214 files['test_support_headers'] +
215 files['crypto_internal_headers'] +
216 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700217 if os.path.basename(filename) == 'malloc.cc':
218 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700219 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700220
Chuck Haysc608d6b2015-10-06 17:54:16 -0700221 out.write(']\n\n')
222
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700223 out.write('def create_tests(copts, crypto, ssl):\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700224 name_counts = {}
225 for test in files['tests']:
226 name = os.path.basename(test[0])
227 name_counts[name] = name_counts.get(name, 0) + 1
228
229 first = True
230 for test in files['tests']:
231 name = os.path.basename(test[0])
232 if name_counts[name] > 1:
233 if '/' in test[1]:
234 name += '_' + os.path.splitext(os.path.basename(test[1]))[0]
235 else:
236 name += '_' + test[1].replace('-', '_')
237
238 if not first:
239 out.write('\n')
240 first = False
241
242 src_prefix = 'src/' + test[0]
243 for src in files['test']:
244 if src.startswith(src_prefix):
245 src = src
246 break
247 else:
248 raise ValueError("Can't find source for %s" % test[0])
249
Chuck Haysc608d6b2015-10-06 17:54:16 -0700250 out.write(' native.cc_test(\n')
251 out.write(' name = "%s",\n' % name)
252 out.write(' size = "small",\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700253 out.write(' srcs = ["%s"] + test_support_sources,\n' %
Matt Braithwaite16695892016-06-09 09:34:11 -0700254 PathOf(src))
Adam Langley9c164b22015-06-10 18:54:47 -0700255
256 data_files = []
257 if len(test) > 1:
258
Chuck Haysc608d6b2015-10-06 17:54:16 -0700259 out.write(' args = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700260 for arg in test[1:]:
261 if '/' in arg:
Matt Braithwaite16695892016-06-09 09:34:11 -0700262 out.write(' "$(location %s)",\n' %
263 PathOf(os.path.join('src', arg)))
Adam Langley9c164b22015-06-10 18:54:47 -0700264 data_files.append('src/%s' % arg)
265 else:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700266 out.write(' "%s",\n' % arg)
267 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700268
Adam Langleyd7b90022016-11-17 09:02:01 -0800269 out.write(' copts = copts + ["-DBORINGSSL_SHARED_LIBRARY"],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700270
271 if len(data_files) > 0:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700272 out.write(' data = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700273 for filename in data_files:
Matt Braithwaite16695892016-06-09 09:34:11 -0700274 out.write(' "%s",\n' % PathOf(filename))
Chuck Haysc608d6b2015-10-06 17:54:16 -0700275 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700276
277 if 'ssl/' in test[0]:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700278 out.write(' deps = [\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700279 out.write(' crypto,\n')
280 out.write(' ssl,\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700281 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700282 else:
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700283 out.write(' deps = [crypto],\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700284 out.write(' )\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700285
Adam Langley049ef412015-06-09 18:20:57 -0700286
David Benjamin38d01c62016-04-21 18:47:57 -0400287class GN(object):
288
289 def __init__(self):
290 self.firstSection = True
291 self.header = \
292"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
293# Use of this source code is governed by a BSD-style license that can be
294# found in the LICENSE file.
295
296# This file is created by generate_build_files.py. Do not edit manually.
297
298"""
299
300 def PrintVariableSection(self, out, name, files):
301 if not self.firstSection:
302 out.write('\n')
303 self.firstSection = False
304
305 out.write('%s = [\n' % name)
306 for f in sorted(files):
307 out.write(' "%s",\n' % f)
308 out.write(']\n')
309
310 def WriteFiles(self, files, asm_outputs):
311 with open('BUILD.generated.gni', 'w+') as out:
312 out.write(self.header)
313
David Benjaminc5aa8412016-07-29 17:41:58 -0400314 self.PrintVariableSection(out, 'crypto_sources',
315 files['crypto'] + files['crypto_headers'] +
316 files['crypto_internal_headers'])
317 self.PrintVariableSection(out, 'ssl_sources',
318 files['ssl'] + files['ssl_headers'] +
319 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400320
321 for ((osname, arch), asm_files) in asm_outputs:
322 self.PrintVariableSection(
323 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
324
325 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
326 for fuzzer in files['fuzz']]
327 self.PrintVariableSection(out, 'fuzzers', fuzzers)
328
329 with open('BUILD.generated_tests.gni', 'w+') as out:
330 self.firstSection = True
331 out.write(self.header)
332
333 self.PrintVariableSection(out, '_test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400334 files['test_support'] +
335 files['test_support_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400336 out.write('\n')
337
338 out.write('template("create_tests") {\n')
339
340 all_tests = []
341 for test in sorted(files['test']):
342 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
343 all_tests.append(test_name)
344
345 out.write(' executable("%s") {\n' % test_name)
346 out.write(' sources = [\n')
347 out.write(' "%s",\n' % test)
348 out.write(' ]\n')
349 out.write(' sources += _test_support_sources\n')
David Benjaminb3be1cf2016-04-27 19:15:06 -0400350 out.write(' if (defined(invoker.configs_exclude)) {\n')
351 out.write(' configs -= invoker.configs_exclude\n')
352 out.write(' }\n')
David Benjamin38d01c62016-04-21 18:47:57 -0400353 out.write(' configs += invoker.configs\n')
354 out.write(' deps = invoker.deps\n')
355 out.write(' }\n')
356 out.write('\n')
357
358 out.write(' group(target_name) {\n')
359 out.write(' deps = [\n')
360 for test_name in sorted(all_tests):
361 out.write(' ":%s",\n' % test_name)
362 out.write(' ]\n')
363 out.write(' }\n')
364 out.write('}\n')
365
366
367class GYP(object):
368
369 def __init__(self):
370 self.header = \
371"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
372# Use of this source code is governed by a BSD-style license that can be
373# found in the LICENSE file.
374
375# This file is created by generate_build_files.py. Do not edit manually.
376
377"""
378
379 def PrintVariableSection(self, out, name, files):
380 out.write(' \'%s\': [\n' % name)
381 for f in sorted(files):
382 out.write(' \'%s\',\n' % f)
383 out.write(' ],\n')
384
385 def WriteFiles(self, files, asm_outputs):
386 with open('boringssl.gypi', 'w+') as gypi:
387 gypi.write(self.header + '{\n \'variables\': {\n')
388
David Benjaminc5aa8412016-07-29 17:41:58 -0400389 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
390 files['ssl'] + files['ssl_headers'] +
391 files['ssl_internal_headers'])
392 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
393 files['crypto'] + files['crypto_headers'] +
394 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400395
396 for ((osname, arch), asm_files) in asm_outputs:
397 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
398 (osname, arch), asm_files)
399
400 gypi.write(' }\n}\n')
401
David Benjamin38d01c62016-04-21 18:47:57 -0400402
Adam Langley9e1a6602015-05-05 17:47:53 -0700403def FindCMakeFiles(directory):
404 """Returns list of all CMakeLists.txt files recursively in directory."""
405 cmakefiles = []
406
407 for (path, _, filenames) in os.walk(directory):
408 for filename in filenames:
409 if filename == 'CMakeLists.txt':
410 cmakefiles.append(os.path.join(path, filename))
411
412 return cmakefiles
413
414
415def NoTests(dent, is_dir):
416 """Filter function that can be passed to FindCFiles in order to remove test
417 sources."""
418 if is_dir:
419 return dent != 'test'
420 return 'test.' not in dent and not dent.startswith('example_')
421
422
423def OnlyTests(dent, is_dir):
424 """Filter function that can be passed to FindCFiles in order to remove
425 non-test sources."""
426 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400427 return dent != 'test'
Adam Langley9e1a6602015-05-05 17:47:53 -0700428 return '_test.' in dent or dent.startswith('example_')
429
430
David Benjamin26073832015-05-11 20:52:48 -0400431def AllFiles(dent, is_dir):
432 """Filter function that can be passed to FindCFiles in order to include all
433 sources."""
434 return True
435
436
Adam Langley049ef412015-06-09 18:20:57 -0700437def SSLHeaderFiles(dent, is_dir):
438 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
439
440
Adam Langley9e1a6602015-05-05 17:47:53 -0700441def FindCFiles(directory, filter_func):
442 """Recurses through directory and returns a list of paths to all the C source
443 files that pass filter_func."""
444 cfiles = []
445
446 for (path, dirnames, filenames) in os.walk(directory):
447 for filename in filenames:
448 if not filename.endswith('.c') and not filename.endswith('.cc'):
449 continue
450 if not filter_func(filename, False):
451 continue
452 cfiles.append(os.path.join(path, filename))
453
454 for (i, dirname) in enumerate(dirnames):
455 if not filter_func(dirname, True):
456 del dirnames[i]
457
458 return cfiles
459
460
Adam Langley049ef412015-06-09 18:20:57 -0700461def FindHeaderFiles(directory, filter_func):
462 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
463 hfiles = []
464
465 for (path, dirnames, filenames) in os.walk(directory):
466 for filename in filenames:
467 if not filename.endswith('.h'):
468 continue
469 if not filter_func(filename, False):
470 continue
471 hfiles.append(os.path.join(path, filename))
472
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700473 for (i, dirname) in enumerate(dirnames):
474 if not filter_func(dirname, True):
475 del dirnames[i]
476
Adam Langley049ef412015-06-09 18:20:57 -0700477 return hfiles
478
479
Adam Langley9e1a6602015-05-05 17:47:53 -0700480def ExtractPerlAsmFromCMakeFile(cmakefile):
481 """Parses the contents of the CMakeLists.txt file passed as an argument and
482 returns a list of all the perlasm() directives found in the file."""
483 perlasms = []
484 with open(cmakefile) as f:
485 for line in f:
486 line = line.strip()
487 if not line.startswith('perlasm('):
488 continue
489 if not line.endswith(')'):
490 raise ValueError('Bad perlasm line in %s' % cmakefile)
491 # Remove "perlasm(" from start and ")" from end
492 params = line[8:-1].split()
493 if len(params) < 2:
494 raise ValueError('Bad perlasm line in %s' % cmakefile)
495 perlasms.append({
496 'extra_args': params[2:],
497 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
498 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
499 })
500
501 return perlasms
502
503
504def ReadPerlAsmOperations():
505 """Returns a list of all perlasm() directives found in CMake config files in
506 src/."""
507 perlasms = []
508 cmakefiles = FindCMakeFiles('src')
509
510 for cmakefile in cmakefiles:
511 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
512
513 return perlasms
514
515
516def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
517 """Runs the a perlasm script and puts the output into output_filename."""
518 base_dir = os.path.dirname(output_filename)
519 if not os.path.isdir(base_dir):
520 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400521 subprocess.check_call(
522 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700523
524
525def ArchForAsmFilename(filename):
526 """Returns the architectures that a given asm file should be compiled for
527 based on substrings in the filename."""
528
529 if 'x86_64' in filename or 'avx2' in filename:
530 return ['x86_64']
531 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
532 return ['x86']
533 elif 'armx' in filename:
534 return ['arm', 'aarch64']
535 elif 'armv8' in filename:
536 return ['aarch64']
537 elif 'arm' in filename:
538 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400539 elif 'ppc' in filename:
540 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700541 else:
542 raise ValueError('Unknown arch for asm filename: ' + filename)
543
544
545def WriteAsmFiles(perlasms):
546 """Generates asm files from perlasm directives for each supported OS x
547 platform combination."""
548 asmfiles = {}
549
550 for osarch in OS_ARCH_COMBOS:
551 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
552 key = (osname, arch)
553 outDir = '%s-%s' % key
554
555 for perlasm in perlasms:
556 filename = os.path.basename(perlasm['input'])
557 output = perlasm['output']
558 if not output.startswith('src'):
559 raise ValueError('output missing src: %s' % output)
560 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200561 if output.endswith('-armx.${ASM_EXT}'):
562 output = output.replace('-armx',
563 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700564 output = output.replace('${ASM_EXT}', asm_ext)
565
566 if arch in ArchForAsmFilename(filename):
567 PerlAsm(output, perlasm['input'], perlasm_style,
568 perlasm['extra_args'] + extra_args)
569 asmfiles.setdefault(key, []).append(output)
570
571 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
572 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
573
574 return asmfiles
575
576
Adam Langley049ef412015-06-09 18:20:57 -0700577def main(platforms):
Adam Langley9e1a6602015-05-05 17:47:53 -0700578 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
579 ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400580 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700581 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700582
583 # Generate err_data.c
584 with open('err_data.c', 'w+') as err_data:
585 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
586 cwd=os.path.join('src', 'crypto', 'err'),
587 stdout=err_data)
588 crypto_c_files.append('err_data.c')
589
David Benjamin38d01c62016-04-21 18:47:57 -0400590 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
591 AllFiles)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700592 test_support_h_files = (
593 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
594 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles))
David Benjamin26073832015-05-11 20:52:48 -0400595
Adam Langley9e1a6602015-05-05 17:47:53 -0700596 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
597 test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
598
David Benjamin38d01c62016-04-21 18:47:57 -0400599 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
600
Adam Langley049ef412015-06-09 18:20:57 -0700601 ssl_h_files = (
602 FindHeaderFiles(
603 os.path.join('src', 'include', 'openssl'),
604 SSLHeaderFiles))
605
606 def NotSSLHeaderFiles(filename, is_dir):
607 return not SSLHeaderFiles(filename, is_dir)
608 crypto_h_files = (
609 FindHeaderFiles(
610 os.path.join('src', 'include', 'openssl'),
611 NotSSLHeaderFiles))
612
613 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
614 crypto_internal_h_files = FindHeaderFiles(
615 os.path.join('src', 'crypto'), NoTests)
616
Adam Langley9c164b22015-06-10 18:54:47 -0700617 with open('src/util/all_tests.json', 'r') as f:
618 tests = json.load(f)
David Benjaminf277add2016-03-09 14:38:24 -0500619 # Skip tests for libdecrepit. Consumers import that manually.
620 tests = [test for test in tests if not test[0].startswith("decrepit/")]
Adam Langley9c164b22015-06-10 18:54:47 -0700621 test_binaries = set([test[0] for test in tests])
622 test_sources = set([
623 test.replace('.cc', '').replace('.c', '').replace(
624 'src/',
625 '')
626 for test in test_c_files])
627 if test_binaries != test_sources:
628 print 'Test sources and configured tests do not match'
629 a = test_binaries.difference(test_sources)
630 if len(a) > 0:
631 print 'These tests are configured without sources: ' + str(a)
632 b = test_sources.difference(test_binaries)
633 if len(b) > 0:
634 print 'These test sources are not configured: ' + str(b)
635
Adam Langley9e1a6602015-05-05 17:47:53 -0700636 files = {
637 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700638 'crypto_headers': crypto_h_files,
639 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400640 'fuzz': fuzz_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700641 'ssl': ssl_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700642 'ssl_headers': ssl_h_files,
643 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400644 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700645 'tool_headers': tool_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700646 'test': test_c_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400647 'test_support': test_support_c_files,
648 'test_support_headers': test_support_h_files,
Adam Langley9c164b22015-06-10 18:54:47 -0700649 'tests': tests,
Adam Langley9e1a6602015-05-05 17:47:53 -0700650 }
651
652 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
653
Adam Langley049ef412015-06-09 18:20:57 -0700654 for platform in platforms:
655 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700656
657 return 0
658
659
Adam Langley9e1a6602015-05-05 17:47:53 -0700660if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700661 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
David Benjamin8c29e7d2016-09-30 21:34:31 -0400662 ' [android|bazel|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700663 parser.add_option('--prefix', dest='prefix',
664 help='For Bazel, prepend argument to all source files')
665 options, args = parser.parse_args(sys.argv[1:])
666 PREFIX = options.prefix
667
668 if not args:
669 parser.print_help()
670 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700671
Adam Langley049ef412015-06-09 18:20:57 -0700672 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700673 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400674 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700675 platforms.append(Android())
Adam Langley049ef412015-06-09 18:20:57 -0700676 elif s == 'bazel':
677 platforms.append(Bazel())
David Benjamin38d01c62016-04-21 18:47:57 -0400678 elif s == 'gn':
679 platforms.append(GN())
680 elif s == 'gyp':
681 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700682 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700683 parser.print_help()
684 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700685
Adam Langley049ef412015-06-09 18:20:57 -0700686 sys.exit(main(platforms))