blob: 92a15074121f2353d71452e7f31b94728adcda99 [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'),
29 ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
30 ('linux', 'x86_64', 'elf', [], 'S'),
31 ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
32 ('mac', 'x86_64', 'macosx', [], 'S'),
33 ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
34 ('win', 'x86_64', 'nasm', [], 'asm'),
35]
36
37# NON_PERL_FILES enumerates assembly files that are not processed by the
38# perlasm system.
39NON_PERL_FILES = {
40 ('linux', 'arm'): [
Adam Langley7b8b9c12016-01-04 07:13:00 -080041 'src/crypto/curve25519/asm/x25519-asm-arm.S',
David Benjamin3c4a5cb2016-03-29 17:43:31 -040042 'src/crypto/poly1305/poly1305_arm_asm.S',
Adam Langley9e1a6602015-05-05 17:47:53 -070043 ],
Matt Braithwaitee021a242016-01-14 13:41:46 -080044 ('linux', 'x86_64'): [
45 'src/crypto/curve25519/asm/x25519-asm-x86_64.S',
46 ],
Piotr Sikora8ca0b412016-06-02 11:59:21 -070047 ('mac', 'x86_64'): [
48 'src/crypto/curve25519/asm/x25519-asm-x86_64.S',
49 ],
Adam Langley9e1a6602015-05-05 17:47:53 -070050}
51
Matt Braithwaite16695892016-06-09 09:34:11 -070052PREFIX = None
53
54
55def PathOf(x):
56 return x if not PREFIX else os.path.join(PREFIX, x)
57
Adam Langley9e1a6602015-05-05 17:47:53 -070058
Adam Langley9e1a6602015-05-05 17:47:53 -070059class Android(object):
60
61 def __init__(self):
62 self.header = \
63"""# Copyright (C) 2015 The Android Open Source Project
64#
65# Licensed under the Apache License, Version 2.0 (the "License");
66# you may not use this file except in compliance with the License.
67# You may obtain a copy of the License at
68#
69# http://www.apache.org/licenses/LICENSE-2.0
70#
71# Unless required by applicable law or agreed to in writing, software
72# distributed under the License is distributed on an "AS IS" BASIS,
73# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74# See the License for the specific language governing permissions and
75# limitations under the License.
76
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070077# This file is created by generate_build_files.py. Do not edit manually.
78
Adam Langley9e1a6602015-05-05 17:47:53 -070079"""
80
Adam Langley049ef412015-06-09 18:20:57 -070081 def ExtraFiles(self):
David Benjamin28d938d2016-09-21 10:42:54 -040082 return ['android_compat_keywrap.c']
Adam Langley049ef412015-06-09 18:20:57 -070083
Adam Langley9e1a6602015-05-05 17:47:53 -070084 def PrintVariableSection(self, out, name, files):
85 out.write('%s := \\\n' % name)
86 for f in sorted(files):
87 out.write(' %s\\\n' % f)
88 out.write('\n')
89
90 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070091 # New Android.bp format
92 with open('sources.bp', 'w+') as blueprint:
93 blueprint.write(self.header.replace('#', '//'))
94
95 blueprint.write('cc_defaults {\n')
96 blueprint.write(' name: "libcrypto_sources",\n')
97 blueprint.write(' srcs: [\n')
98 for f in sorted(files['crypto'] + self.ExtraFiles()):
99 blueprint.write(' "%s",\n' % f)
100 blueprint.write(' ],\n')
101 blueprint.write(' target: {\n')
102
103 for ((osname, arch), asm_files) in asm_outputs:
104 if osname != 'linux':
105 continue
106 if arch == 'aarch64':
107 arch = 'arm64'
108
109 blueprint.write(' android_%s: {\n' % arch)
110 blueprint.write(' srcs: [\n')
111 for f in sorted(asm_files):
112 blueprint.write(' "%s",\n' % f)
113 blueprint.write(' ],\n')
114 blueprint.write(' },\n')
115
116 if arch == 'x86' or arch == 'x86_64':
117 blueprint.write(' linux_%s: {\n' % arch)
118 blueprint.write(' srcs: [\n')
119 for f in sorted(asm_files):
120 blueprint.write(' "%s",\n' % f)
121 blueprint.write(' ],\n')
122 blueprint.write(' },\n')
123
124 blueprint.write(' },\n')
125 blueprint.write('}\n\n')
126
127 blueprint.write('cc_defaults {\n')
128 blueprint.write(' name: "libssl_sources",\n')
129 blueprint.write(' srcs: [\n')
130 for f in sorted(files['ssl']):
131 blueprint.write(' "%s",\n' % f)
132 blueprint.write(' ],\n')
133 blueprint.write('}\n\n')
134
135 blueprint.write('cc_defaults {\n')
136 blueprint.write(' name: "bssl_sources",\n')
137 blueprint.write(' srcs: [\n')
138 for f in sorted(files['tool']):
139 blueprint.write(' "%s",\n' % f)
140 blueprint.write(' ],\n')
141 blueprint.write('}\n\n')
142
143 blueprint.write('cc_defaults {\n')
144 blueprint.write(' name: "boringssl_test_support_sources",\n')
145 blueprint.write(' srcs: [\n')
146 for f in sorted(files['test_support']):
147 blueprint.write(' "%s",\n' % f)
148 blueprint.write(' ],\n')
149 blueprint.write('}\n\n')
150
151 blueprint.write('cc_defaults {\n')
152 blueprint.write(' name: "boringssl_tests_sources",\n')
153 blueprint.write(' srcs: [\n')
154 for f in sorted(files['test']):
155 blueprint.write(' "%s",\n' % f)
156 blueprint.write(' ],\n')
157 blueprint.write('}\n')
158
159 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700160 with open('sources.mk', 'w+') as makefile:
161 makefile.write(self.header)
162
Piotr Sikora6ae67df2015-11-23 18:46:33 -0800163 crypto_files = files['crypto'] + self.ExtraFiles()
164 self.PrintVariableSection(makefile, 'crypto_sources', crypto_files)
Adam Langley9e1a6602015-05-05 17:47:53 -0700165
166 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700167 if osname != 'linux':
168 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700169 self.PrintVariableSection(
170 makefile, '%s_%s_sources' % (osname, arch), asm_files)
171
172
Adam Langley049ef412015-06-09 18:20:57 -0700173class AndroidStandalone(Android):
174 """AndroidStandalone is for Android builds outside of the Android-system, i.e.
175
176 for applications that wish wish to ship BoringSSL.
177 """
178
179 def ExtraFiles(self):
180 return []
181
182
183class Bazel(object):
184 """Bazel outputs files suitable for including in Bazel files."""
185
186 def __init__(self):
187 self.firstSection = True
188 self.header = \
189"""# This file is created by generate_build_files.py. Do not edit manually.
190
191"""
192
193 def PrintVariableSection(self, out, name, files):
194 if not self.firstSection:
195 out.write('\n')
196 self.firstSection = False
197
198 out.write('%s = [\n' % name)
199 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700200 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700201 out.write(']\n')
202
203 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700204 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700205 out.write(self.header)
206
207 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
208 self.PrintVariableSection(
209 out, 'ssl_internal_headers', files['ssl_internal_headers'])
210 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
211 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
212 self.PrintVariableSection(
213 out, 'crypto_internal_headers', files['crypto_internal_headers'])
214 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
215 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700216 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700217
218 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700219 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700220 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700221
Chuck Haysc608d6b2015-10-06 17:54:16 -0700222 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700223 out.write(self.header)
224
225 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400226 for filename in sorted(files['test_support'] +
227 files['test_support_headers'] +
228 files['crypto_internal_headers'] +
229 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700230 if os.path.basename(filename) == 'malloc.cc':
231 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700232 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700233
Chuck Haysc608d6b2015-10-06 17:54:16 -0700234 out.write(']\n\n')
235
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700236 out.write('def create_tests(copts, crypto, ssl):\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700237 name_counts = {}
238 for test in files['tests']:
239 name = os.path.basename(test[0])
240 name_counts[name] = name_counts.get(name, 0) + 1
241
242 first = True
243 for test in files['tests']:
244 name = os.path.basename(test[0])
245 if name_counts[name] > 1:
246 if '/' in test[1]:
247 name += '_' + os.path.splitext(os.path.basename(test[1]))[0]
248 else:
249 name += '_' + test[1].replace('-', '_')
250
251 if not first:
252 out.write('\n')
253 first = False
254
255 src_prefix = 'src/' + test[0]
256 for src in files['test']:
257 if src.startswith(src_prefix):
258 src = src
259 break
260 else:
261 raise ValueError("Can't find source for %s" % test[0])
262
Chuck Haysc608d6b2015-10-06 17:54:16 -0700263 out.write(' native.cc_test(\n')
264 out.write(' name = "%s",\n' % name)
265 out.write(' size = "small",\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700266 out.write(' srcs = ["%s"] + test_support_sources,\n' %
Matt Braithwaite16695892016-06-09 09:34:11 -0700267 PathOf(src))
Adam Langley9c164b22015-06-10 18:54:47 -0700268
269 data_files = []
270 if len(test) > 1:
271
Chuck Haysc608d6b2015-10-06 17:54:16 -0700272 out.write(' args = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700273 for arg in test[1:]:
274 if '/' in arg:
Matt Braithwaite16695892016-06-09 09:34:11 -0700275 out.write(' "$(location %s)",\n' %
276 PathOf(os.path.join('src', arg)))
Adam Langley9c164b22015-06-10 18:54:47 -0700277 data_files.append('src/%s' % arg)
278 else:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700279 out.write(' "%s",\n' % arg)
280 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700281
Chuck Haysc608d6b2015-10-06 17:54:16 -0700282 out.write(' copts = copts,\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700283
284 if len(data_files) > 0:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700285 out.write(' data = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700286 for filename in data_files:
Matt Braithwaite16695892016-06-09 09:34:11 -0700287 out.write(' "%s",\n' % PathOf(filename))
Chuck Haysc608d6b2015-10-06 17:54:16 -0700288 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700289
290 if 'ssl/' in test[0]:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700291 out.write(' deps = [\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700292 out.write(' crypto,\n')
293 out.write(' ssl,\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700294 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700295 else:
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700296 out.write(' deps = [crypto],\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700297 out.write(' )\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700298
Adam Langley049ef412015-06-09 18:20:57 -0700299
David Benjamin38d01c62016-04-21 18:47:57 -0400300class GN(object):
301
302 def __init__(self):
303 self.firstSection = True
304 self.header = \
305"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
306# Use of this source code is governed by a BSD-style license that can be
307# found in the LICENSE file.
308
309# This file is created by generate_build_files.py. Do not edit manually.
310
311"""
312
313 def PrintVariableSection(self, out, name, files):
314 if not self.firstSection:
315 out.write('\n')
316 self.firstSection = False
317
318 out.write('%s = [\n' % name)
319 for f in sorted(files):
320 out.write(' "%s",\n' % f)
321 out.write(']\n')
322
323 def WriteFiles(self, files, asm_outputs):
324 with open('BUILD.generated.gni', 'w+') as out:
325 out.write(self.header)
326
David Benjaminc5aa8412016-07-29 17:41:58 -0400327 self.PrintVariableSection(out, 'crypto_sources',
328 files['crypto'] + files['crypto_headers'] +
329 files['crypto_internal_headers'])
330 self.PrintVariableSection(out, 'ssl_sources',
331 files['ssl'] + files['ssl_headers'] +
332 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400333
334 for ((osname, arch), asm_files) in asm_outputs:
335 self.PrintVariableSection(
336 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
337
338 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
339 for fuzzer in files['fuzz']]
340 self.PrintVariableSection(out, 'fuzzers', fuzzers)
341
342 with open('BUILD.generated_tests.gni', 'w+') as out:
343 self.firstSection = True
344 out.write(self.header)
345
346 self.PrintVariableSection(out, '_test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400347 files['test_support'] +
348 files['test_support_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400349 out.write('\n')
350
351 out.write('template("create_tests") {\n')
352
353 all_tests = []
354 for test in sorted(files['test']):
355 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
356 all_tests.append(test_name)
357
358 out.write(' executable("%s") {\n' % test_name)
359 out.write(' sources = [\n')
360 out.write(' "%s",\n' % test)
361 out.write(' ]\n')
362 out.write(' sources += _test_support_sources\n')
David Benjaminb3be1cf2016-04-27 19:15:06 -0400363 out.write(' if (defined(invoker.configs_exclude)) {\n')
364 out.write(' configs -= invoker.configs_exclude\n')
365 out.write(' }\n')
David Benjamin38d01c62016-04-21 18:47:57 -0400366 out.write(' configs += invoker.configs\n')
367 out.write(' deps = invoker.deps\n')
368 out.write(' }\n')
369 out.write('\n')
370
371 out.write(' group(target_name) {\n')
372 out.write(' deps = [\n')
373 for test_name in sorted(all_tests):
374 out.write(' ":%s",\n' % test_name)
375 out.write(' ]\n')
376 out.write(' }\n')
377 out.write('}\n')
378
379
380class GYP(object):
381
382 def __init__(self):
383 self.header = \
384"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
385# Use of this source code is governed by a BSD-style license that can be
386# found in the LICENSE file.
387
388# This file is created by generate_build_files.py. Do not edit manually.
389
390"""
391
392 def PrintVariableSection(self, out, name, files):
393 out.write(' \'%s\': [\n' % name)
394 for f in sorted(files):
395 out.write(' \'%s\',\n' % f)
396 out.write(' ],\n')
397
398 def WriteFiles(self, files, asm_outputs):
399 with open('boringssl.gypi', 'w+') as gypi:
400 gypi.write(self.header + '{\n \'variables\': {\n')
401
David Benjaminc5aa8412016-07-29 17:41:58 -0400402 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
403 files['ssl'] + files['ssl_headers'] +
404 files['ssl_internal_headers'])
405 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
406 files['crypto'] + files['crypto_headers'] +
407 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400408
409 for ((osname, arch), asm_files) in asm_outputs:
410 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
411 (osname, arch), asm_files)
412
413 gypi.write(' }\n}\n')
414
415 with open('boringssl_tests.gypi', 'w+') as test_gypi:
416 test_gypi.write(self.header + '{\n \'targets\': [\n')
417
418 test_names = []
419 for test in sorted(files['test']):
420 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
421 test_gypi.write(""" {
422 'target_name': '%s',
423 'type': 'executable',
424 'dependencies': [
425 'boringssl.gyp:boringssl',
426 ],
427 'sources': [
428 '%s',
429 '<@(boringssl_test_support_sources)',
430 ],
431 # TODO(davidben): Fix size_t truncations in BoringSSL.
432 # https://crbug.com/429039
433 'msvs_disabled_warnings': [ 4267, ],
434 },\n""" % (test_name, test))
435 test_names.append(test_name)
436
437 test_names.sort()
438
439 test_gypi.write(' ],\n \'variables\': {\n')
440
David Benjaminc5aa8412016-07-29 17:41:58 -0400441 self.PrintVariableSection(test_gypi, 'boringssl_test_support_sources',
442 files['test_support'] +
443 files['test_support_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400444
445 test_gypi.write(' \'boringssl_test_targets\': [\n')
446
447 for test in sorted(test_names):
448 test_gypi.write(""" '%s',\n""" % test)
449
450 test_gypi.write(' ],\n }\n}\n')
451
452
Adam Langley9e1a6602015-05-05 17:47:53 -0700453def FindCMakeFiles(directory):
454 """Returns list of all CMakeLists.txt files recursively in directory."""
455 cmakefiles = []
456
457 for (path, _, filenames) in os.walk(directory):
458 for filename in filenames:
459 if filename == 'CMakeLists.txt':
460 cmakefiles.append(os.path.join(path, filename))
461
462 return cmakefiles
463
464
465def NoTests(dent, is_dir):
466 """Filter function that can be passed to FindCFiles in order to remove test
467 sources."""
468 if is_dir:
469 return dent != 'test'
470 return 'test.' not in dent and not dent.startswith('example_')
471
472
473def OnlyTests(dent, is_dir):
474 """Filter function that can be passed to FindCFiles in order to remove
475 non-test sources."""
476 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400477 return dent != 'test'
Adam Langley9e1a6602015-05-05 17:47:53 -0700478 return '_test.' in dent or dent.startswith('example_')
479
480
David Benjamin26073832015-05-11 20:52:48 -0400481def AllFiles(dent, is_dir):
482 """Filter function that can be passed to FindCFiles in order to include all
483 sources."""
484 return True
485
486
Adam Langley049ef412015-06-09 18:20:57 -0700487def SSLHeaderFiles(dent, is_dir):
488 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
489
490
Adam Langley9e1a6602015-05-05 17:47:53 -0700491def FindCFiles(directory, filter_func):
492 """Recurses through directory and returns a list of paths to all the C source
493 files that pass filter_func."""
494 cfiles = []
495
496 for (path, dirnames, filenames) in os.walk(directory):
497 for filename in filenames:
498 if not filename.endswith('.c') and not filename.endswith('.cc'):
499 continue
500 if not filter_func(filename, False):
501 continue
502 cfiles.append(os.path.join(path, filename))
503
504 for (i, dirname) in enumerate(dirnames):
505 if not filter_func(dirname, True):
506 del dirnames[i]
507
508 return cfiles
509
510
Adam Langley049ef412015-06-09 18:20:57 -0700511def FindHeaderFiles(directory, filter_func):
512 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
513 hfiles = []
514
515 for (path, dirnames, filenames) in os.walk(directory):
516 for filename in filenames:
517 if not filename.endswith('.h'):
518 continue
519 if not filter_func(filename, False):
520 continue
521 hfiles.append(os.path.join(path, filename))
522
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700523 for (i, dirname) in enumerate(dirnames):
524 if not filter_func(dirname, True):
525 del dirnames[i]
526
Adam Langley049ef412015-06-09 18:20:57 -0700527 return hfiles
528
529
Adam Langley9e1a6602015-05-05 17:47:53 -0700530def ExtractPerlAsmFromCMakeFile(cmakefile):
531 """Parses the contents of the CMakeLists.txt file passed as an argument and
532 returns a list of all the perlasm() directives found in the file."""
533 perlasms = []
534 with open(cmakefile) as f:
535 for line in f:
536 line = line.strip()
537 if not line.startswith('perlasm('):
538 continue
539 if not line.endswith(')'):
540 raise ValueError('Bad perlasm line in %s' % cmakefile)
541 # Remove "perlasm(" from start and ")" from end
542 params = line[8:-1].split()
543 if len(params) < 2:
544 raise ValueError('Bad perlasm line in %s' % cmakefile)
545 perlasms.append({
546 'extra_args': params[2:],
547 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
548 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
549 })
550
551 return perlasms
552
553
554def ReadPerlAsmOperations():
555 """Returns a list of all perlasm() directives found in CMake config files in
556 src/."""
557 perlasms = []
558 cmakefiles = FindCMakeFiles('src')
559
560 for cmakefile in cmakefiles:
561 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
562
563 return perlasms
564
565
566def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
567 """Runs the a perlasm script and puts the output into output_filename."""
568 base_dir = os.path.dirname(output_filename)
569 if not os.path.isdir(base_dir):
570 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400571 subprocess.check_call(
572 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700573
574
575def ArchForAsmFilename(filename):
576 """Returns the architectures that a given asm file should be compiled for
577 based on substrings in the filename."""
578
579 if 'x86_64' in filename or 'avx2' in filename:
580 return ['x86_64']
581 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
582 return ['x86']
583 elif 'armx' in filename:
584 return ['arm', 'aarch64']
585 elif 'armv8' in filename:
586 return ['aarch64']
587 elif 'arm' in filename:
588 return ['arm']
589 else:
590 raise ValueError('Unknown arch for asm filename: ' + filename)
591
592
593def WriteAsmFiles(perlasms):
594 """Generates asm files from perlasm directives for each supported OS x
595 platform combination."""
596 asmfiles = {}
597
598 for osarch in OS_ARCH_COMBOS:
599 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
600 key = (osname, arch)
601 outDir = '%s-%s' % key
602
603 for perlasm in perlasms:
604 filename = os.path.basename(perlasm['input'])
605 output = perlasm['output']
606 if not output.startswith('src'):
607 raise ValueError('output missing src: %s' % output)
608 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200609 if output.endswith('-armx.${ASM_EXT}'):
610 output = output.replace('-armx',
611 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700612 output = output.replace('${ASM_EXT}', asm_ext)
613
614 if arch in ArchForAsmFilename(filename):
615 PerlAsm(output, perlasm['input'], perlasm_style,
616 perlasm['extra_args'] + extra_args)
617 asmfiles.setdefault(key, []).append(output)
618
619 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
620 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
621
622 return asmfiles
623
624
Adam Langley049ef412015-06-09 18:20:57 -0700625def main(platforms):
Adam Langley9e1a6602015-05-05 17:47:53 -0700626 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
627 ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400628 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700629 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700630
631 # Generate err_data.c
632 with open('err_data.c', 'w+') as err_data:
633 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
634 cwd=os.path.join('src', 'crypto', 'err'),
635 stdout=err_data)
636 crypto_c_files.append('err_data.c')
637
David Benjamin38d01c62016-04-21 18:47:57 -0400638 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
639 AllFiles)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700640 test_support_h_files = (
641 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
642 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles))
David Benjamin26073832015-05-11 20:52:48 -0400643
Adam Langley9e1a6602015-05-05 17:47:53 -0700644 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
645 test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
646
David Benjamin38d01c62016-04-21 18:47:57 -0400647 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
648
Adam Langley049ef412015-06-09 18:20:57 -0700649 ssl_h_files = (
650 FindHeaderFiles(
651 os.path.join('src', 'include', 'openssl'),
652 SSLHeaderFiles))
653
654 def NotSSLHeaderFiles(filename, is_dir):
655 return not SSLHeaderFiles(filename, is_dir)
656 crypto_h_files = (
657 FindHeaderFiles(
658 os.path.join('src', 'include', 'openssl'),
659 NotSSLHeaderFiles))
660
661 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
662 crypto_internal_h_files = FindHeaderFiles(
663 os.path.join('src', 'crypto'), NoTests)
664
Adam Langley9c164b22015-06-10 18:54:47 -0700665 with open('src/util/all_tests.json', 'r') as f:
666 tests = json.load(f)
David Benjaminf277add2016-03-09 14:38:24 -0500667 # Skip tests for libdecrepit. Consumers import that manually.
668 tests = [test for test in tests if not test[0].startswith("decrepit/")]
Adam Langley9c164b22015-06-10 18:54:47 -0700669 test_binaries = set([test[0] for test in tests])
670 test_sources = set([
671 test.replace('.cc', '').replace('.c', '').replace(
672 'src/',
673 '')
674 for test in test_c_files])
675 if test_binaries != test_sources:
676 print 'Test sources and configured tests do not match'
677 a = test_binaries.difference(test_sources)
678 if len(a) > 0:
679 print 'These tests are configured without sources: ' + str(a)
680 b = test_sources.difference(test_binaries)
681 if len(b) > 0:
682 print 'These test sources are not configured: ' + str(b)
683
Adam Langley9e1a6602015-05-05 17:47:53 -0700684 files = {
685 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700686 'crypto_headers': crypto_h_files,
687 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400688 'fuzz': fuzz_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700689 'ssl': ssl_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700690 'ssl_headers': ssl_h_files,
691 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400692 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700693 'tool_headers': tool_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700694 'test': test_c_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400695 'test_support': test_support_c_files,
696 'test_support_headers': test_support_h_files,
Adam Langley9c164b22015-06-10 18:54:47 -0700697 'tests': tests,
Adam Langley9e1a6602015-05-05 17:47:53 -0700698 }
699
700 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
701
Adam Langley049ef412015-06-09 18:20:57 -0700702 for platform in platforms:
703 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700704
705 return 0
706
707
Adam Langley9e1a6602015-05-05 17:47:53 -0700708if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700709 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
710 ' [android|android-standalone|bazel|gn|gyp]')
711 parser.add_option('--prefix', dest='prefix',
712 help='For Bazel, prepend argument to all source files')
713 options, args = parser.parse_args(sys.argv[1:])
714 PREFIX = options.prefix
715
716 if not args:
717 parser.print_help()
718 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700719
Adam Langley049ef412015-06-09 18:20:57 -0700720 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700721 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400722 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700723 platforms.append(Android())
724 elif s == 'android-standalone':
725 platforms.append(AndroidStandalone())
726 elif s == 'bazel':
727 platforms.append(Bazel())
David Benjamin38d01c62016-04-21 18:47:57 -0400728 elif s == 'gn':
729 platforms.append(GN())
730 elif s == 'gyp':
731 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700732 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700733 parser.print_help()
734 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700735
Adam Langley049ef412015-06-09 18:20:57 -0700736 sys.exit(main(platforms))