blob: 7066d7995cdee3d5776ec5e75fcd841518602379 [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 = [
David Benjaminf6584e72017-06-08 16:27:16 -040027 ('ios', 'arm', 'ios32', [], 'S'),
28 ('ios', 'aarch64', 'ios64', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070029 ('linux', 'arm', 'linux32', [], 'S'),
30 ('linux', 'aarch64', 'linux64', [], 'S'),
Adam Langley7c075b92017-05-22 15:31:13 -070031 ('linux', 'ppc64le', 'linux64le', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070032 ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
33 ('linux', 'x86_64', 'elf', [], 'S'),
34 ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
35 ('mac', 'x86_64', 'macosx', [], 'S'),
36 ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
37 ('win', 'x86_64', 'nasm', [], 'asm'),
38]
39
40# NON_PERL_FILES enumerates assembly files that are not processed by the
41# perlasm system.
42NON_PERL_FILES = {
43 ('linux', 'arm'): [
Adam Langley7b8b9c12016-01-04 07:13:00 -080044 'src/crypto/curve25519/asm/x25519-asm-arm.S',
David Benjamin3c4a5cb2016-03-29 17:43:31 -040045 'src/crypto/poly1305/poly1305_arm_asm.S',
Adam Langley7b935932018-11-12 13:53:42 -080046 ],
47 ('linux', 'x86_64'): [
48 'src/crypto/hrss/asm/poly_rq_mul.S',
Adam Langley9e1a6602015-05-05 17:47:53 -070049 ],
50}
51
Matt Braithwaite16695892016-06-09 09:34:11 -070052PREFIX = None
Adam Langley990a3232018-05-22 10:02:59 -070053EMBED_TEST_DATA = True
Matt Braithwaite16695892016-06-09 09:34:11 -070054
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
Dan Willemsen2eb4bc52017-10-16 14:37:00 -0700107 blueprint.write(' linux_%s: {\n' % arch)
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700108 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
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700114 blueprint.write(' },\n')
115 blueprint.write('}\n\n')
116
117 blueprint.write('cc_defaults {\n')
118 blueprint.write(' name: "libssl_sources",\n')
119 blueprint.write(' srcs: [\n')
120 for f in sorted(files['ssl']):
121 blueprint.write(' "%s",\n' % f)
122 blueprint.write(' ],\n')
123 blueprint.write('}\n\n')
124
125 blueprint.write('cc_defaults {\n')
126 blueprint.write(' name: "bssl_sources",\n')
127 blueprint.write(' srcs: [\n')
128 for f in sorted(files['tool']):
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: "boringssl_test_support_sources",\n')
135 blueprint.write(' srcs: [\n')
136 for f in sorted(files['test_support']):
137 blueprint.write(' "%s",\n' % f)
138 blueprint.write(' ],\n')
139 blueprint.write('}\n\n')
140
141 blueprint.write('cc_defaults {\n')
David Benjamin96628432017-01-19 19:05:47 -0500142 blueprint.write(' name: "boringssl_crypto_test_sources",\n')
143 blueprint.write(' srcs: [\n')
144 for f in sorted(files['crypto_test']):
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_ssl_test_sources",\n')
151 blueprint.write(' srcs: [\n')
152 for f in sorted(files['ssl_test']):
153 blueprint.write(' "%s",\n' % f)
154 blueprint.write(' ],\n')
155 blueprint.write('}\n\n')
156
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700157 # 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
David Benjamineca48e52019-08-13 11:51:53 -0400170class AndroidCMake(object):
171
172 def __init__(self):
173 self.header = \
174"""# Copyright (C) 2019 The Android Open Source Project
175#
176# Licensed under the Apache License, Version 2.0 (the "License");
177# you may not use this file except in compliance with the License.
178# You may obtain a copy of the License at
179#
180# http://www.apache.org/licenses/LICENSE-2.0
181#
182# Unless required by applicable law or agreed to in writing, software
183# distributed under the License is distributed on an "AS IS" BASIS,
184# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
185# See the License for the specific language governing permissions and
186# limitations under the License.
187
188# This file is created by generate_build_files.py. Do not edit manually.
189# To specify a custom path prefix, set BORINGSSL_ROOT before including this
190# file, or use list(TRANSFORM ... PREPEND) from CMake 3.12.
191
192"""
193
194 def PrintVariableSection(self, out, name, files):
195 out.write('set(%s\n' % name)
196 for f in sorted(files):
197 # Ideally adding the prefix would be the caller's job, but
198 # list(TRANSFORM ... PREPEND) is only available starting CMake 3.12. When
199 # sources.cmake is the source of truth, we can ask Android to either write
200 # a CMake function or update to 3.12.
201 out.write(' ${BORINGSSL_ROOT}%s\n' % f)
202 out.write(')\n')
203
204 def WriteFiles(self, files, asm_outputs):
205 # The Android emulator uses a custom CMake buildsystem.
206 #
207 # TODO(davidben): Move our various source lists into sources.cmake and have
208 # Android consume that directly.
209 with open('android-sources.cmake', 'w+') as out:
210 out.write(self.header)
211
212 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
213 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
214 self.PrintVariableSection(out, 'tool_sources', files['tool'])
215 self.PrintVariableSection(out, 'test_support_sources',
216 files['test_support'])
217 self.PrintVariableSection(out, 'crypto_test_sources',
218 files['crypto_test'])
219 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
220
221 for ((osname, arch), asm_files) in asm_outputs:
222 self.PrintVariableSection(
223 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
224
225
Adam Langley049ef412015-06-09 18:20:57 -0700226class Bazel(object):
227 """Bazel outputs files suitable for including in Bazel files."""
228
229 def __init__(self):
230 self.firstSection = True
231 self.header = \
232"""# This file is created by generate_build_files.py. Do not edit manually.
233
234"""
235
236 def PrintVariableSection(self, out, name, files):
237 if not self.firstSection:
238 out.write('\n')
239 self.firstSection = False
240
241 out.write('%s = [\n' % name)
242 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700243 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700244 out.write(']\n')
245
246 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700247 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700248 out.write(self.header)
249
250 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700251 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700252 self.PrintVariableSection(
253 out, 'ssl_internal_headers', files['ssl_internal_headers'])
254 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
255 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
256 self.PrintVariableSection(
257 out, 'crypto_internal_headers', files['crypto_internal_headers'])
258 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
259 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700260 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700261
262 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700263 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700264 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700265
Chuck Haysc608d6b2015-10-06 17:54:16 -0700266 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700267 out.write(self.header)
268
269 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400270 for filename in sorted(files['test_support'] +
271 files['test_support_headers'] +
272 files['crypto_internal_headers'] +
273 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700274 if os.path.basename(filename) == 'malloc.cc':
275 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700276 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700277
Adam Langley7b6acc52017-07-27 16:33:27 -0700278 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700279
David Benjamin96628432017-01-19 19:05:47 -0500280 self.PrintVariableSection(out, 'crypto_test_sources',
281 files['crypto_test'])
282 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
Adam Langley990a3232018-05-22 10:02:59 -0700283 self.PrintVariableSection(out, 'crypto_test_data',
284 files['crypto_test_data'])
David Benjamin96628432017-01-19 19:05:47 -0500285
Adam Langley049ef412015-06-09 18:20:57 -0700286
Robert Sloane091af42017-10-09 12:47:17 -0700287class Eureka(object):
288
289 def __init__(self):
290 self.header = \
291"""# Copyright (C) 2017 The Android Open Source Project
292#
293# Licensed under the Apache License, Version 2.0 (the "License");
294# you may not use this file except in compliance with the License.
295# You may obtain a copy of the License at
296#
297# http://www.apache.org/licenses/LICENSE-2.0
298#
299# Unless required by applicable law or agreed to in writing, software
300# distributed under the License is distributed on an "AS IS" BASIS,
301# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
302# See the License for the specific language governing permissions and
303# limitations under the License.
304
305# This file is created by generate_build_files.py. Do not edit manually.
306
307"""
308
309 def PrintVariableSection(self, out, name, files):
310 out.write('%s := \\\n' % name)
311 for f in sorted(files):
312 out.write(' %s\\\n' % f)
313 out.write('\n')
314
315 def WriteFiles(self, files, asm_outputs):
316 # Legacy Android.mk format
317 with open('eureka.mk', 'w+') as makefile:
318 makefile.write(self.header)
319
320 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
321 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
322 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
323
324 for ((osname, arch), asm_files) in asm_outputs:
325 if osname != 'linux':
326 continue
327 self.PrintVariableSection(
328 makefile, '%s_%s_sources' % (osname, arch), asm_files)
329
330
David Benjamin38d01c62016-04-21 18:47:57 -0400331class GN(object):
332
333 def __init__(self):
334 self.firstSection = True
335 self.header = \
336"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
337# Use of this source code is governed by a BSD-style license that can be
338# found in the LICENSE file.
339
340# This file is created by generate_build_files.py. Do not edit manually.
341
342"""
343
344 def PrintVariableSection(self, out, name, files):
345 if not self.firstSection:
346 out.write('\n')
347 self.firstSection = False
348
349 out.write('%s = [\n' % name)
350 for f in sorted(files):
351 out.write(' "%s",\n' % f)
352 out.write(']\n')
353
354 def WriteFiles(self, files, asm_outputs):
355 with open('BUILD.generated.gni', 'w+') as out:
356 out.write(self.header)
357
David Benjaminc5aa8412016-07-29 17:41:58 -0400358 self.PrintVariableSection(out, 'crypto_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700359 files['crypto'] +
David Benjaminc5aa8412016-07-29 17:41:58 -0400360 files['crypto_internal_headers'])
James Robinson98dd68f2018-04-11 14:47:34 -0700361 self.PrintVariableSection(out, 'crypto_headers',
362 files['crypto_headers'])
David Benjaminc5aa8412016-07-29 17:41:58 -0400363 self.PrintVariableSection(out, 'ssl_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700364 files['ssl'] + files['ssl_internal_headers'])
365 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400366
367 for ((osname, arch), asm_files) in asm_outputs:
368 self.PrintVariableSection(
369 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
370
371 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
372 for fuzzer in files['fuzz']]
373 self.PrintVariableSection(out, 'fuzzers', fuzzers)
374
375 with open('BUILD.generated_tests.gni', 'w+') as out:
376 self.firstSection = True
377 out.write(self.header)
378
David Benjamin96628432017-01-19 19:05:47 -0500379 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400380 files['test_support'] +
381 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500382 self.PrintVariableSection(out, 'crypto_test_sources',
383 files['crypto_test'])
David Benjaminf014d602019-05-07 18:58:06 -0500384 self.PrintVariableSection(out, 'crypto_test_data',
385 files['crypto_test_data'])
David Benjamin96628432017-01-19 19:05:47 -0500386 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400387
388
389class GYP(object):
390
391 def __init__(self):
392 self.header = \
393"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
394# Use of this source code is governed by a BSD-style license that can be
395# found in the LICENSE file.
396
397# This file is created by generate_build_files.py. Do not edit manually.
398
399"""
400
401 def PrintVariableSection(self, out, name, files):
402 out.write(' \'%s\': [\n' % name)
403 for f in sorted(files):
404 out.write(' \'%s\',\n' % f)
405 out.write(' ],\n')
406
407 def WriteFiles(self, files, asm_outputs):
408 with open('boringssl.gypi', 'w+') as gypi:
409 gypi.write(self.header + '{\n \'variables\': {\n')
410
David Benjaminc5aa8412016-07-29 17:41:58 -0400411 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
412 files['ssl'] + files['ssl_headers'] +
413 files['ssl_internal_headers'])
414 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
415 files['crypto'] + files['crypto_headers'] +
416 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400417
418 for ((osname, arch), asm_files) in asm_outputs:
419 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
420 (osname, arch), asm_files)
421
422 gypi.write(' }\n}\n')
423
David Benjamin38d01c62016-04-21 18:47:57 -0400424
Adam Langley9e1a6602015-05-05 17:47:53 -0700425def FindCMakeFiles(directory):
426 """Returns list of all CMakeLists.txt files recursively in directory."""
427 cmakefiles = []
428
429 for (path, _, filenames) in os.walk(directory):
430 for filename in filenames:
431 if filename == 'CMakeLists.txt':
432 cmakefiles.append(os.path.join(path, filename))
433
434 return cmakefiles
435
Adam Langleyfd499932017-04-04 14:21:43 -0700436def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700437 return is_dir or (path.startswith(
438 os.path.join('src', 'crypto', 'fipsmodule', '')) and
439 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700440
Adam Langleyfd499932017-04-04 14:21:43 -0700441def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700442 return (NoTests(path, dent, is_dir) and
443 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700444
445def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700446 """Filter function that can be passed to FindCFiles in order to remove test
447 sources."""
448 if is_dir:
449 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400450 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700451
452
Adam Langleyfd499932017-04-04 14:21:43 -0700453def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700454 """Filter function that can be passed to FindCFiles in order to remove
455 non-test sources."""
456 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400457 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400458 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700459
460
Adam Langleyfd499932017-04-04 14:21:43 -0700461def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400462 """Filter function that can be passed to FindCFiles in order to include all
463 sources."""
464 return True
465
466
Adam Langleyfd499932017-04-04 14:21:43 -0700467def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700468 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
469 order to exclude test runner files."""
470 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
471 # are in their own subpackage, from being included in boringssl/BUILD files.
472 return not is_dir or dent != 'runner'
473
474
David Benjamin3ecd0a52017-05-19 15:26:18 -0400475def NotGTestSupport(path, dent, is_dir):
David Benjaminc3889632019-03-01 15:03:05 -0500476 return 'gtest' not in dent and 'abi_test' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500477
478
Adam Langleyfd499932017-04-04 14:21:43 -0700479def SSLHeaderFiles(path, dent, is_dir):
Aaron Green0e150022018-10-16 12:05:29 -0700480 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h', 'srtp.h']
Adam Langley049ef412015-06-09 18:20:57 -0700481
482
Adam Langley9e1a6602015-05-05 17:47:53 -0700483def FindCFiles(directory, filter_func):
484 """Recurses through directory and returns a list of paths to all the C source
485 files that pass filter_func."""
486 cfiles = []
487
488 for (path, dirnames, filenames) in os.walk(directory):
489 for filename in filenames:
490 if not filename.endswith('.c') and not filename.endswith('.cc'):
491 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700492 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700493 continue
494 cfiles.append(os.path.join(path, filename))
495
496 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700497 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700498 del dirnames[i]
499
500 return cfiles
501
502
Adam Langley049ef412015-06-09 18:20:57 -0700503def FindHeaderFiles(directory, filter_func):
504 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
505 hfiles = []
506
507 for (path, dirnames, filenames) in os.walk(directory):
508 for filename in filenames:
509 if not filename.endswith('.h'):
510 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700511 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700512 continue
513 hfiles.append(os.path.join(path, filename))
514
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700515 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700516 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700517 del dirnames[i]
518
Adam Langley049ef412015-06-09 18:20:57 -0700519 return hfiles
520
521
Adam Langley9e1a6602015-05-05 17:47:53 -0700522def ExtractPerlAsmFromCMakeFile(cmakefile):
523 """Parses the contents of the CMakeLists.txt file passed as an argument and
524 returns a list of all the perlasm() directives found in the file."""
525 perlasms = []
526 with open(cmakefile) as f:
527 for line in f:
528 line = line.strip()
529 if not line.startswith('perlasm('):
530 continue
531 if not line.endswith(')'):
532 raise ValueError('Bad perlasm line in %s' % cmakefile)
533 # Remove "perlasm(" from start and ")" from end
534 params = line[8:-1].split()
535 if len(params) < 2:
536 raise ValueError('Bad perlasm line in %s' % cmakefile)
537 perlasms.append({
538 'extra_args': params[2:],
539 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
540 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
541 })
542
543 return perlasms
544
545
546def ReadPerlAsmOperations():
547 """Returns a list of all perlasm() directives found in CMake config files in
548 src/."""
549 perlasms = []
550 cmakefiles = FindCMakeFiles('src')
551
552 for cmakefile in cmakefiles:
553 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
554
555 return perlasms
556
557
558def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
559 """Runs the a perlasm script and puts the output into output_filename."""
560 base_dir = os.path.dirname(output_filename)
561 if not os.path.isdir(base_dir):
562 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400563 subprocess.check_call(
564 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700565
566
567def ArchForAsmFilename(filename):
568 """Returns the architectures that a given asm file should be compiled for
569 based on substrings in the filename."""
570
571 if 'x86_64' in filename or 'avx2' in filename:
572 return ['x86_64']
573 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
574 return ['x86']
575 elif 'armx' in filename:
576 return ['arm', 'aarch64']
577 elif 'armv8' in filename:
578 return ['aarch64']
579 elif 'arm' in filename:
580 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400581 elif 'ppc' in filename:
582 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700583 else:
584 raise ValueError('Unknown arch for asm filename: ' + filename)
585
586
587def WriteAsmFiles(perlasms):
588 """Generates asm files from perlasm directives for each supported OS x
589 platform combination."""
590 asmfiles = {}
591
592 for osarch in OS_ARCH_COMBOS:
593 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
594 key = (osname, arch)
595 outDir = '%s-%s' % key
596
597 for perlasm in perlasms:
598 filename = os.path.basename(perlasm['input'])
599 output = perlasm['output']
600 if not output.startswith('src'):
601 raise ValueError('output missing src: %s' % output)
602 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200603 if output.endswith('-armx.${ASM_EXT}'):
604 output = output.replace('-armx',
605 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700606 output = output.replace('${ASM_EXT}', asm_ext)
607
608 if arch in ArchForAsmFilename(filename):
609 PerlAsm(output, perlasm['input'], perlasm_style,
610 perlasm['extra_args'] + extra_args)
611 asmfiles.setdefault(key, []).append(output)
612
613 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
614 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
615
616 return asmfiles
617
618
David Benjamin3ecd0a52017-05-19 15:26:18 -0400619def ExtractVariablesFromCMakeFile(cmakefile):
620 """Parses the contents of the CMakeLists.txt file passed as an argument and
621 returns a dictionary of exported source lists."""
622 variables = {}
623 in_set_command = False
624 set_command = []
625 with open(cmakefile) as f:
626 for line in f:
627 if '#' in line:
628 line = line[:line.index('#')]
629 line = line.strip()
630
631 if not in_set_command:
632 if line.startswith('set('):
633 in_set_command = True
634 set_command = []
635 elif line == ')':
636 in_set_command = False
637 if not set_command:
638 raise ValueError('Empty set command')
639 variables[set_command[0]] = set_command[1:]
640 else:
641 set_command.extend([c for c in line.split(' ') if c])
642
643 if in_set_command:
644 raise ValueError('Unfinished set command')
645 return variables
646
647
Adam Langley049ef412015-06-09 18:20:57 -0700648def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400649 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Andres Erbsen5b280a82017-10-30 15:58:33 +0000650 crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
Adam Langleye0c533a2019-05-20 09:50:07 -0700651 FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments) +
652 FindCFiles(os.path.join('src', 'third_party', 'sike'), NoTestsNorFIPSFragments))
Adam Langleyfd499932017-04-04 14:21:43 -0700653 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800654 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400655 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700656 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700657
David Benjamin0c9c1aa2017-12-12 15:19:20 -0500658 # third_party/fiat/p256.c lives in third_party/fiat, but it is a FIPS
659 # fragment, not a normal source file.
660 p256 = os.path.join('src', 'third_party', 'fiat', 'p256.c')
661 fips_fragments.append(p256)
662 crypto_c_files.remove(p256)
663
Adam Langley9e1a6602015-05-05 17:47:53 -0700664 # Generate err_data.c
665 with open('err_data.c', 'w+') as err_data:
666 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
667 cwd=os.path.join('src', 'crypto', 'err'),
668 stdout=err_data)
669 crypto_c_files.append('err_data.c')
670
David Benjamin38d01c62016-04-21 18:47:57 -0400671 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400672 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700673 test_support_h_files = (
674 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700675 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400676
Adam Langley990a3232018-05-22 10:02:59 -0700677 crypto_test_files = []
678 if EMBED_TEST_DATA:
679 # Generate crypto_test_data.cc
680 with open('crypto_test_data.cc', 'w+') as out:
681 subprocess.check_call(
682 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
683 cwd='src',
684 stdout=out)
685 crypto_test_files += ['crypto_test_data.cc']
David Benjamin3ecd0a52017-05-19 15:26:18 -0400686
Adam Langley990a3232018-05-22 10:02:59 -0700687 crypto_test_files += FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
David Benjamin96ee4a82017-07-09 23:46:47 -0400688 crypto_test_files += [
David Benjaminc3889632019-03-01 15:03:05 -0500689 'src/crypto/test/abi_test.cc',
David Benjamin3ecd0a52017-05-19 15:26:18 -0400690 'src/crypto/test/file_test_gtest.cc',
691 'src/crypto/test/gtest_main.cc',
692 ]
David Benjamin1d5a5702017-02-13 22:11:49 -0500693
694 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
Robert Sloanae1e0872019-03-01 16:01:30 -0800695 ssl_test_files += [
696 'src/crypto/test/abi_test.cc',
697 'src/crypto/test/gtest_main.cc',
698 ]
Adam Langley9e1a6602015-05-05 17:47:53 -0700699
David Benjamin38d01c62016-04-21 18:47:57 -0400700 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
701
Adam Langley049ef412015-06-09 18:20:57 -0700702 ssl_h_files = (
703 FindHeaderFiles(
704 os.path.join('src', 'include', 'openssl'),
705 SSLHeaderFiles))
706
Adam Langleyfd499932017-04-04 14:21:43 -0700707 def NotSSLHeaderFiles(path, filename, is_dir):
708 return not SSLHeaderFiles(path, filename, is_dir)
Adam Langley049ef412015-06-09 18:20:57 -0700709 crypto_h_files = (
710 FindHeaderFiles(
711 os.path.join('src', 'include', 'openssl'),
712 NotSSLHeaderFiles))
713
714 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
Andres Erbsen5b280a82017-10-30 15:58:33 +0000715 crypto_internal_h_files = (
716 FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
Adam Langleye0c533a2019-05-20 09:50:07 -0700717 FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests) +
718 FindHeaderFiles(os.path.join('src', 'third_party', 'sike'), NoTests))
Adam Langley049ef412015-06-09 18:20:57 -0700719
Adam Langley9e1a6602015-05-05 17:47:53 -0700720 files = {
721 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700722 'crypto_headers': crypto_h_files,
723 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500724 'crypto_test': sorted(crypto_test_files),
Adam Langley990a3232018-05-22 10:02:59 -0700725 'crypto_test_data': sorted('src/' + x for x in cmake['CRYPTO_TEST_DATA']),
Adam Langleyfd499932017-04-04 14:21:43 -0700726 'fips_fragments': fips_fragments,
David Benjamin38d01c62016-04-21 18:47:57 -0400727 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800728 'ssl': ssl_source_files,
Adam Langley049ef412015-06-09 18:20:57 -0700729 'ssl_headers': ssl_h_files,
730 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500731 'ssl_test': sorted(ssl_test_files),
David Benjamin38d01c62016-04-21 18:47:57 -0400732 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700733 'tool_headers': tool_h_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400734 'test_support': test_support_c_files,
735 'test_support_headers': test_support_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700736 }
737
738 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
739
Adam Langley049ef412015-06-09 18:20:57 -0700740 for platform in platforms:
741 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700742
743 return 0
744
745
Adam Langley9e1a6602015-05-05 17:47:53 -0700746if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700747 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
David Benjamineca48e52019-08-13 11:51:53 -0400748 ' [android|android-cmake|bazel|eureka|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700749 parser.add_option('--prefix', dest='prefix',
750 help='For Bazel, prepend argument to all source files')
Adam Langley990a3232018-05-22 10:02:59 -0700751 parser.add_option(
752 '--embed_test_data', type='choice', dest='embed_test_data',
753 action='store', default="true", choices=["true", "false"],
David Benjaminf014d602019-05-07 18:58:06 -0500754 help='For Bazel or GN, don\'t embed data files in crypto_test_data.cc')
Matt Braithwaite16695892016-06-09 09:34:11 -0700755 options, args = parser.parse_args(sys.argv[1:])
756 PREFIX = options.prefix
Adam Langley990a3232018-05-22 10:02:59 -0700757 EMBED_TEST_DATA = (options.embed_test_data == "true")
Matt Braithwaite16695892016-06-09 09:34:11 -0700758
759 if not args:
760 parser.print_help()
761 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700762
Adam Langley049ef412015-06-09 18:20:57 -0700763 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700764 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400765 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700766 platforms.append(Android())
David Benjamineca48e52019-08-13 11:51:53 -0400767 elif s == 'android-cmake':
768 platforms.append(AndroidCMake())
Adam Langley049ef412015-06-09 18:20:57 -0700769 elif s == 'bazel':
770 platforms.append(Bazel())
Robert Sloane091af42017-10-09 12:47:17 -0700771 elif s == 'eureka':
772 platforms.append(Eureka())
David Benjamin38d01c62016-04-21 18:47:57 -0400773 elif s == 'gn':
774 platforms.append(GN())
775 elif s == 'gyp':
776 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700777 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700778 parser.print_help()
779 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700780
Adam Langley049ef412015-06-09 18:20:57 -0700781 sys.exit(main(platforms))