blob: e8b484e9a6cdf25e56c6376dc15d245659bb8b60 [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
Adam Langley049ef412015-06-09 18:20:57 -070082 def ExtraFiles(self):
David Benjamin28d938d2016-09-21 10:42:54 -040083 return ['android_compat_keywrap.c']
Adam Langley049ef412015-06-09 18:20:57 -070084
Adam Langley9e1a6602015-05-05 17:47:53 -070085 def PrintVariableSection(self, out, name, files):
86 out.write('%s := \\\n' % name)
87 for f in sorted(files):
88 out.write(' %s\\\n' % f)
89 out.write('\n')
90
91 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070092 # New Android.bp format
93 with open('sources.bp', 'w+') as blueprint:
94 blueprint.write(self.header.replace('#', '//'))
95
96 blueprint.write('cc_defaults {\n')
97 blueprint.write(' name: "libcrypto_sources",\n')
98 blueprint.write(' srcs: [\n')
99 for f in sorted(files['crypto'] + self.ExtraFiles()):
100 blueprint.write(' "%s",\n' % f)
101 blueprint.write(' ],\n')
102 blueprint.write(' target: {\n')
103
104 for ((osname, arch), asm_files) in asm_outputs:
105 if osname != 'linux':
106 continue
107 if arch == 'aarch64':
108 arch = 'arm64'
109
110 blueprint.write(' android_%s: {\n' % arch)
111 blueprint.write(' srcs: [\n')
112 for f in sorted(asm_files):
113 blueprint.write(' "%s",\n' % f)
114 blueprint.write(' ],\n')
115 blueprint.write(' },\n')
116
117 if arch == 'x86' or arch == 'x86_64':
118 blueprint.write(' linux_%s: {\n' % arch)
119 blueprint.write(' srcs: [\n')
120 for f in sorted(asm_files):
121 blueprint.write(' "%s",\n' % f)
122 blueprint.write(' ],\n')
123 blueprint.write(' },\n')
124
125 blueprint.write(' },\n')
126 blueprint.write('}\n\n')
127
128 blueprint.write('cc_defaults {\n')
129 blueprint.write(' name: "libssl_sources",\n')
130 blueprint.write(' srcs: [\n')
131 for f in sorted(files['ssl']):
132 blueprint.write(' "%s",\n' % f)
133 blueprint.write(' ],\n')
134 blueprint.write('}\n\n')
135
136 blueprint.write('cc_defaults {\n')
137 blueprint.write(' name: "bssl_sources",\n')
138 blueprint.write(' srcs: [\n')
139 for f in sorted(files['tool']):
140 blueprint.write(' "%s",\n' % f)
141 blueprint.write(' ],\n')
142 blueprint.write('}\n\n')
143
144 blueprint.write('cc_defaults {\n')
145 blueprint.write(' name: "boringssl_test_support_sources",\n')
146 blueprint.write(' srcs: [\n')
147 for f in sorted(files['test_support']):
148 blueprint.write(' "%s",\n' % f)
149 blueprint.write(' ],\n')
150 blueprint.write('}\n\n')
151
152 blueprint.write('cc_defaults {\n')
153 blueprint.write(' name: "boringssl_tests_sources",\n')
154 blueprint.write(' srcs: [\n')
155 for f in sorted(files['test']):
156 blueprint.write(' "%s",\n' % f)
157 blueprint.write(' ],\n')
158 blueprint.write('}\n')
159
160 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700161 with open('sources.mk', 'w+') as makefile:
162 makefile.write(self.header)
163
Piotr Sikora6ae67df2015-11-23 18:46:33 -0800164 crypto_files = files['crypto'] + self.ExtraFiles()
165 self.PrintVariableSection(makefile, 'crypto_sources', crypto_files)
Adam Langley9e1a6602015-05-05 17:47:53 -0700166
167 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700168 if osname != 'linux':
169 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700170 self.PrintVariableSection(
171 makefile, '%s_%s_sources' % (osname, arch), asm_files)
172
173
Adam Langley049ef412015-06-09 18:20:57 -0700174class AndroidStandalone(Android):
175 """AndroidStandalone is for Android builds outside of the Android-system, i.e.
176
177 for applications that wish wish to ship BoringSSL.
178 """
179
180 def ExtraFiles(self):
181 return []
182
183
184class Bazel(object):
185 """Bazel outputs files suitable for including in Bazel files."""
186
187 def __init__(self):
188 self.firstSection = True
189 self.header = \
190"""# This file is created by generate_build_files.py. Do not edit manually.
191
192"""
193
194 def PrintVariableSection(self, out, name, files):
195 if not self.firstSection:
196 out.write('\n')
197 self.firstSection = False
198
199 out.write('%s = [\n' % name)
200 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700201 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700202 out.write(']\n')
203
204 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700205 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700206 out.write(self.header)
207
208 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
209 self.PrintVariableSection(
210 out, 'ssl_internal_headers', files['ssl_internal_headers'])
211 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
212 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
213 self.PrintVariableSection(
214 out, 'crypto_internal_headers', files['crypto_internal_headers'])
215 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
216 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700217 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700218
219 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700220 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700221 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700222
Chuck Haysc608d6b2015-10-06 17:54:16 -0700223 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700224 out.write(self.header)
225
226 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400227 for filename in sorted(files['test_support'] +
228 files['test_support_headers'] +
229 files['crypto_internal_headers'] +
230 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700231 if os.path.basename(filename) == 'malloc.cc':
232 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700233 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700234
Chuck Haysc608d6b2015-10-06 17:54:16 -0700235 out.write(']\n\n')
236
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700237 out.write('def create_tests(copts, crypto, ssl):\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700238 name_counts = {}
239 for test in files['tests']:
240 name = os.path.basename(test[0])
241 name_counts[name] = name_counts.get(name, 0) + 1
242
243 first = True
244 for test in files['tests']:
245 name = os.path.basename(test[0])
246 if name_counts[name] > 1:
247 if '/' in test[1]:
248 name += '_' + os.path.splitext(os.path.basename(test[1]))[0]
249 else:
250 name += '_' + test[1].replace('-', '_')
251
252 if not first:
253 out.write('\n')
254 first = False
255
256 src_prefix = 'src/' + test[0]
257 for src in files['test']:
258 if src.startswith(src_prefix):
259 src = src
260 break
261 else:
262 raise ValueError("Can't find source for %s" % test[0])
263
Chuck Haysc608d6b2015-10-06 17:54:16 -0700264 out.write(' native.cc_test(\n')
265 out.write(' name = "%s",\n' % name)
266 out.write(' size = "small",\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700267 out.write(' srcs = ["%s"] + test_support_sources,\n' %
Matt Braithwaite16695892016-06-09 09:34:11 -0700268 PathOf(src))
Adam Langley9c164b22015-06-10 18:54:47 -0700269
270 data_files = []
271 if len(test) > 1:
272
Chuck Haysc608d6b2015-10-06 17:54:16 -0700273 out.write(' args = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700274 for arg in test[1:]:
275 if '/' in arg:
Matt Braithwaite16695892016-06-09 09:34:11 -0700276 out.write(' "$(location %s)",\n' %
277 PathOf(os.path.join('src', arg)))
Adam Langley9c164b22015-06-10 18:54:47 -0700278 data_files.append('src/%s' % arg)
279 else:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700280 out.write(' "%s",\n' % arg)
281 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700282
Chuck Haysc608d6b2015-10-06 17:54:16 -0700283 out.write(' copts = copts,\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700284
285 if len(data_files) > 0:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700286 out.write(' data = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700287 for filename in data_files:
Matt Braithwaite16695892016-06-09 09:34:11 -0700288 out.write(' "%s",\n' % PathOf(filename))
Chuck Haysc608d6b2015-10-06 17:54:16 -0700289 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700290
291 if 'ssl/' in test[0]:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700292 out.write(' deps = [\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700293 out.write(' crypto,\n')
294 out.write(' ssl,\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700295 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700296 else:
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700297 out.write(' deps = [crypto],\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700298 out.write(' )\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700299
Adam Langley049ef412015-06-09 18:20:57 -0700300
David Benjamin38d01c62016-04-21 18:47:57 -0400301class GN(object):
302
303 def __init__(self):
304 self.firstSection = True
305 self.header = \
306"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
307# Use of this source code is governed by a BSD-style license that can be
308# found in the LICENSE file.
309
310# This file is created by generate_build_files.py. Do not edit manually.
311
312"""
313
314 def PrintVariableSection(self, out, name, files):
315 if not self.firstSection:
316 out.write('\n')
317 self.firstSection = False
318
319 out.write('%s = [\n' % name)
320 for f in sorted(files):
321 out.write(' "%s",\n' % f)
322 out.write(']\n')
323
324 def WriteFiles(self, files, asm_outputs):
325 with open('BUILD.generated.gni', 'w+') as out:
326 out.write(self.header)
327
David Benjaminc5aa8412016-07-29 17:41:58 -0400328 self.PrintVariableSection(out, 'crypto_sources',
329 files['crypto'] + files['crypto_headers'] +
330 files['crypto_internal_headers'])
331 self.PrintVariableSection(out, 'ssl_sources',
332 files['ssl'] + files['ssl_headers'] +
333 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400334
335 for ((osname, arch), asm_files) in asm_outputs:
336 self.PrintVariableSection(
337 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
338
339 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
340 for fuzzer in files['fuzz']]
341 self.PrintVariableSection(out, 'fuzzers', fuzzers)
342
343 with open('BUILD.generated_tests.gni', 'w+') as out:
344 self.firstSection = True
345 out.write(self.header)
346
347 self.PrintVariableSection(out, '_test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400348 files['test_support'] +
349 files['test_support_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400350 out.write('\n')
351
352 out.write('template("create_tests") {\n')
353
354 all_tests = []
355 for test in sorted(files['test']):
356 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
357 all_tests.append(test_name)
358
359 out.write(' executable("%s") {\n' % test_name)
360 out.write(' sources = [\n')
361 out.write(' "%s",\n' % test)
362 out.write(' ]\n')
363 out.write(' sources += _test_support_sources\n')
David Benjaminb3be1cf2016-04-27 19:15:06 -0400364 out.write(' if (defined(invoker.configs_exclude)) {\n')
365 out.write(' configs -= invoker.configs_exclude\n')
366 out.write(' }\n')
David Benjamin38d01c62016-04-21 18:47:57 -0400367 out.write(' configs += invoker.configs\n')
368 out.write(' deps = invoker.deps\n')
369 out.write(' }\n')
370 out.write('\n')
371
372 out.write(' group(target_name) {\n')
373 out.write(' deps = [\n')
374 for test_name in sorted(all_tests):
375 out.write(' ":%s",\n' % test_name)
376 out.write(' ]\n')
377 out.write(' }\n')
378 out.write('}\n')
379
380
381class GYP(object):
382
383 def __init__(self):
384 self.header = \
385"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
386# Use of this source code is governed by a BSD-style license that can be
387# found in the LICENSE file.
388
389# This file is created by generate_build_files.py. Do not edit manually.
390
391"""
392
393 def PrintVariableSection(self, out, name, files):
394 out.write(' \'%s\': [\n' % name)
395 for f in sorted(files):
396 out.write(' \'%s\',\n' % f)
397 out.write(' ],\n')
398
399 def WriteFiles(self, files, asm_outputs):
400 with open('boringssl.gypi', 'w+') as gypi:
401 gypi.write(self.header + '{\n \'variables\': {\n')
402
David Benjaminc5aa8412016-07-29 17:41:58 -0400403 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
404 files['ssl'] + files['ssl_headers'] +
405 files['ssl_internal_headers'])
406 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
407 files['crypto'] + files['crypto_headers'] +
408 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400409
410 for ((osname, arch), asm_files) in asm_outputs:
411 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
412 (osname, arch), asm_files)
413
414 gypi.write(' }\n}\n')
415
416 with open('boringssl_tests.gypi', 'w+') as test_gypi:
417 test_gypi.write(self.header + '{\n \'targets\': [\n')
418
419 test_names = []
420 for test in sorted(files['test']):
421 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
422 test_gypi.write(""" {
423 'target_name': '%s',
424 'type': 'executable',
425 'dependencies': [
426 'boringssl.gyp:boringssl',
427 ],
428 'sources': [
429 '%s',
430 '<@(boringssl_test_support_sources)',
431 ],
432 # TODO(davidben): Fix size_t truncations in BoringSSL.
433 # https://crbug.com/429039
434 'msvs_disabled_warnings': [ 4267, ],
435 },\n""" % (test_name, test))
436 test_names.append(test_name)
437
438 test_names.sort()
439
440 test_gypi.write(' ],\n \'variables\': {\n')
441
David Benjaminc5aa8412016-07-29 17:41:58 -0400442 self.PrintVariableSection(test_gypi, 'boringssl_test_support_sources',
443 files['test_support'] +
444 files['test_support_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400445
446 test_gypi.write(' \'boringssl_test_targets\': [\n')
447
448 for test in sorted(test_names):
449 test_gypi.write(""" '%s',\n""" % test)
450
451 test_gypi.write(' ],\n }\n}\n')
452
453
Adam Langley9e1a6602015-05-05 17:47:53 -0700454def FindCMakeFiles(directory):
455 """Returns list of all CMakeLists.txt files recursively in directory."""
456 cmakefiles = []
457
458 for (path, _, filenames) in os.walk(directory):
459 for filename in filenames:
460 if filename == 'CMakeLists.txt':
461 cmakefiles.append(os.path.join(path, filename))
462
463 return cmakefiles
464
465
466def NoTests(dent, is_dir):
467 """Filter function that can be passed to FindCFiles in order to remove test
468 sources."""
469 if is_dir:
470 return dent != 'test'
471 return 'test.' not in dent and not dent.startswith('example_')
472
473
474def OnlyTests(dent, is_dir):
475 """Filter function that can be passed to FindCFiles in order to remove
476 non-test sources."""
477 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400478 return dent != 'test'
Adam Langley9e1a6602015-05-05 17:47:53 -0700479 return '_test.' in dent or dent.startswith('example_')
480
481
David Benjamin26073832015-05-11 20:52:48 -0400482def AllFiles(dent, is_dir):
483 """Filter function that can be passed to FindCFiles in order to include all
484 sources."""
485 return True
486
487
Adam Langley049ef412015-06-09 18:20:57 -0700488def SSLHeaderFiles(dent, is_dir):
489 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
490
491
Adam Langley9e1a6602015-05-05 17:47:53 -0700492def FindCFiles(directory, filter_func):
493 """Recurses through directory and returns a list of paths to all the C source
494 files that pass filter_func."""
495 cfiles = []
496
497 for (path, dirnames, filenames) in os.walk(directory):
498 for filename in filenames:
499 if not filename.endswith('.c') and not filename.endswith('.cc'):
500 continue
501 if not filter_func(filename, False):
502 continue
503 cfiles.append(os.path.join(path, filename))
504
505 for (i, dirname) in enumerate(dirnames):
506 if not filter_func(dirname, True):
507 del dirnames[i]
508
509 return cfiles
510
511
Adam Langley049ef412015-06-09 18:20:57 -0700512def FindHeaderFiles(directory, filter_func):
513 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
514 hfiles = []
515
516 for (path, dirnames, filenames) in os.walk(directory):
517 for filename in filenames:
518 if not filename.endswith('.h'):
519 continue
520 if not filter_func(filename, False):
521 continue
522 hfiles.append(os.path.join(path, filename))
523
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700524 for (i, dirname) in enumerate(dirnames):
525 if not filter_func(dirname, True):
526 del dirnames[i]
527
Adam Langley049ef412015-06-09 18:20:57 -0700528 return hfiles
529
530
Adam Langley9e1a6602015-05-05 17:47:53 -0700531def ExtractPerlAsmFromCMakeFile(cmakefile):
532 """Parses the contents of the CMakeLists.txt file passed as an argument and
533 returns a list of all the perlasm() directives found in the file."""
534 perlasms = []
535 with open(cmakefile) as f:
536 for line in f:
537 line = line.strip()
538 if not line.startswith('perlasm('):
539 continue
540 if not line.endswith(')'):
541 raise ValueError('Bad perlasm line in %s' % cmakefile)
542 # Remove "perlasm(" from start and ")" from end
543 params = line[8:-1].split()
544 if len(params) < 2:
545 raise ValueError('Bad perlasm line in %s' % cmakefile)
546 perlasms.append({
547 'extra_args': params[2:],
548 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
549 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
550 })
551
552 return perlasms
553
554
555def ReadPerlAsmOperations():
556 """Returns a list of all perlasm() directives found in CMake config files in
557 src/."""
558 perlasms = []
559 cmakefiles = FindCMakeFiles('src')
560
561 for cmakefile in cmakefiles:
562 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
563
564 return perlasms
565
566
567def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
568 """Runs the a perlasm script and puts the output into output_filename."""
569 base_dir = os.path.dirname(output_filename)
570 if not os.path.isdir(base_dir):
571 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400572 subprocess.check_call(
573 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700574
575
576def ArchForAsmFilename(filename):
577 """Returns the architectures that a given asm file should be compiled for
578 based on substrings in the filename."""
579
580 if 'x86_64' in filename or 'avx2' in filename:
581 return ['x86_64']
582 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
583 return ['x86']
584 elif 'armx' in filename:
585 return ['arm', 'aarch64']
586 elif 'armv8' in filename:
587 return ['aarch64']
588 elif 'arm' in filename:
589 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400590 elif 'ppc' in filename:
591 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700592 else:
593 raise ValueError('Unknown arch for asm filename: ' + filename)
594
595
596def WriteAsmFiles(perlasms):
597 """Generates asm files from perlasm directives for each supported OS x
598 platform combination."""
599 asmfiles = {}
600
601 for osarch in OS_ARCH_COMBOS:
602 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
603 key = (osname, arch)
604 outDir = '%s-%s' % key
605
606 for perlasm in perlasms:
607 filename = os.path.basename(perlasm['input'])
608 output = perlasm['output']
609 if not output.startswith('src'):
610 raise ValueError('output missing src: %s' % output)
611 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200612 if output.endswith('-armx.${ASM_EXT}'):
613 output = output.replace('-armx',
614 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700615 output = output.replace('${ASM_EXT}', asm_ext)
616
617 if arch in ArchForAsmFilename(filename):
618 PerlAsm(output, perlasm['input'], perlasm_style,
619 perlasm['extra_args'] + extra_args)
620 asmfiles.setdefault(key, []).append(output)
621
622 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
623 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
624
625 return asmfiles
626
627
Adam Langley049ef412015-06-09 18:20:57 -0700628def main(platforms):
Adam Langley9e1a6602015-05-05 17:47:53 -0700629 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
630 ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400631 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700632 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700633
634 # Generate err_data.c
635 with open('err_data.c', 'w+') as err_data:
636 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
637 cwd=os.path.join('src', 'crypto', 'err'),
638 stdout=err_data)
639 crypto_c_files.append('err_data.c')
640
David Benjamin38d01c62016-04-21 18:47:57 -0400641 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
642 AllFiles)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700643 test_support_h_files = (
644 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
645 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles))
David Benjamin26073832015-05-11 20:52:48 -0400646
Adam Langley9e1a6602015-05-05 17:47:53 -0700647 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
648 test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
649
David Benjamin38d01c62016-04-21 18:47:57 -0400650 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
651
Adam Langley049ef412015-06-09 18:20:57 -0700652 ssl_h_files = (
653 FindHeaderFiles(
654 os.path.join('src', 'include', 'openssl'),
655 SSLHeaderFiles))
656
657 def NotSSLHeaderFiles(filename, is_dir):
658 return not SSLHeaderFiles(filename, is_dir)
659 crypto_h_files = (
660 FindHeaderFiles(
661 os.path.join('src', 'include', 'openssl'),
662 NotSSLHeaderFiles))
663
664 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
665 crypto_internal_h_files = FindHeaderFiles(
666 os.path.join('src', 'crypto'), NoTests)
667
Adam Langley9c164b22015-06-10 18:54:47 -0700668 with open('src/util/all_tests.json', 'r') as f:
669 tests = json.load(f)
David Benjaminf277add2016-03-09 14:38:24 -0500670 # Skip tests for libdecrepit. Consumers import that manually.
671 tests = [test for test in tests if not test[0].startswith("decrepit/")]
Adam Langley9c164b22015-06-10 18:54:47 -0700672 test_binaries = set([test[0] for test in tests])
673 test_sources = set([
674 test.replace('.cc', '').replace('.c', '').replace(
675 'src/',
676 '')
677 for test in test_c_files])
678 if test_binaries != test_sources:
679 print 'Test sources and configured tests do not match'
680 a = test_binaries.difference(test_sources)
681 if len(a) > 0:
682 print 'These tests are configured without sources: ' + str(a)
683 b = test_sources.difference(test_binaries)
684 if len(b) > 0:
685 print 'These test sources are not configured: ' + str(b)
686
Adam Langley9e1a6602015-05-05 17:47:53 -0700687 files = {
688 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700689 'crypto_headers': crypto_h_files,
690 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400691 'fuzz': fuzz_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700692 'ssl': ssl_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700693 'ssl_headers': ssl_h_files,
694 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400695 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700696 'tool_headers': tool_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700697 'test': test_c_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400698 'test_support': test_support_c_files,
699 'test_support_headers': test_support_h_files,
Adam Langley9c164b22015-06-10 18:54:47 -0700700 'tests': tests,
Adam Langley9e1a6602015-05-05 17:47:53 -0700701 }
702
703 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
704
Adam Langley049ef412015-06-09 18:20:57 -0700705 for platform in platforms:
706 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700707
708 return 0
709
710
Adam Langley9e1a6602015-05-05 17:47:53 -0700711if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700712 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
713 ' [android|android-standalone|bazel|gn|gyp]')
714 parser.add_option('--prefix', dest='prefix',
715 help='For Bazel, prepend argument to all source files')
716 options, args = parser.parse_args(sys.argv[1:])
717 PREFIX = options.prefix
718
719 if not args:
720 parser.print_help()
721 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700722
Adam Langley049ef412015-06-09 18:20:57 -0700723 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700724 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400725 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700726 platforms.append(Android())
727 elif s == 'android-standalone':
728 platforms.append(AndroidStandalone())
729 elif s == 'bazel':
730 platforms.append(Bazel())
David Benjamin38d01c62016-04-21 18:47:57 -0400731 elif s == 'gn':
732 platforms.append(GN())
733 elif s == 'gyp':
734 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700735 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700736 parser.print_help()
737 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700738
Adam Langley049ef412015-06-09 18:20:57 -0700739 sys.exit(main(platforms))