blob: fff8904cf04ab92232b0065525ccf9ec1fed6704 [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):
82 return ['android_compat_hacks.c', 'android_compat_keywrap.c']
83
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')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700226 for filename in (files['test_support'] +
227 files['crypto_internal_headers'] +
228 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700229 if os.path.basename(filename) == 'malloc.cc':
230 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700231 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700232
Chuck Haysc608d6b2015-10-06 17:54:16 -0700233 out.write(']\n\n')
234
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700235 out.write('def create_tests(copts, crypto, ssl):\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700236 name_counts = {}
237 for test in files['tests']:
238 name = os.path.basename(test[0])
239 name_counts[name] = name_counts.get(name, 0) + 1
240
241 first = True
242 for test in files['tests']:
243 name = os.path.basename(test[0])
244 if name_counts[name] > 1:
245 if '/' in test[1]:
246 name += '_' + os.path.splitext(os.path.basename(test[1]))[0]
247 else:
248 name += '_' + test[1].replace('-', '_')
249
250 if not first:
251 out.write('\n')
252 first = False
253
254 src_prefix = 'src/' + test[0]
255 for src in files['test']:
256 if src.startswith(src_prefix):
257 src = src
258 break
259 else:
260 raise ValueError("Can't find source for %s" % test[0])
261
Chuck Haysc608d6b2015-10-06 17:54:16 -0700262 out.write(' native.cc_test(\n')
263 out.write(' name = "%s",\n' % name)
264 out.write(' size = "small",\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700265 out.write(' srcs = ["%s"] + test_support_sources,\n' %
Matt Braithwaite16695892016-06-09 09:34:11 -0700266 PathOf(src))
Adam Langley9c164b22015-06-10 18:54:47 -0700267
268 data_files = []
269 if len(test) > 1:
270
Chuck Haysc608d6b2015-10-06 17:54:16 -0700271 out.write(' args = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700272 for arg in test[1:]:
273 if '/' in arg:
Matt Braithwaite16695892016-06-09 09:34:11 -0700274 out.write(' "$(location %s)",\n' %
275 PathOf(os.path.join('src', arg)))
Adam Langley9c164b22015-06-10 18:54:47 -0700276 data_files.append('src/%s' % arg)
277 else:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700278 out.write(' "%s",\n' % arg)
279 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700280
Chuck Haysc608d6b2015-10-06 17:54:16 -0700281 out.write(' copts = copts,\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700282
283 if len(data_files) > 0:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700284 out.write(' data = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700285 for filename in data_files:
Matt Braithwaite16695892016-06-09 09:34:11 -0700286 out.write(' "%s",\n' % PathOf(filename))
Chuck Haysc608d6b2015-10-06 17:54:16 -0700287 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700288
289 if 'ssl/' in test[0]:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700290 out.write(' deps = [\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700291 out.write(' crypto,\n')
292 out.write(' ssl,\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700293 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700294 else:
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700295 out.write(' deps = [crypto],\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700296 out.write(' )\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700297
Adam Langley049ef412015-06-09 18:20:57 -0700298
David Benjamin38d01c62016-04-21 18:47:57 -0400299class GN(object):
300
301 def __init__(self):
302 self.firstSection = True
303 self.header = \
304"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
305# Use of this source code is governed by a BSD-style license that can be
306# found in the LICENSE file.
307
308# This file is created by generate_build_files.py. Do not edit manually.
309
310"""
311
312 def PrintVariableSection(self, out, name, files):
313 if not self.firstSection:
314 out.write('\n')
315 self.firstSection = False
316
317 out.write('%s = [\n' % name)
318 for f in sorted(files):
319 out.write(' "%s",\n' % f)
320 out.write(']\n')
321
322 def WriteFiles(self, files, asm_outputs):
323 with open('BUILD.generated.gni', 'w+') as out:
324 out.write(self.header)
325
326 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
327 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
328
329 for ((osname, arch), asm_files) in asm_outputs:
330 self.PrintVariableSection(
331 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
332
333 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
334 for fuzzer in files['fuzz']]
335 self.PrintVariableSection(out, 'fuzzers', fuzzers)
336
337 with open('BUILD.generated_tests.gni', 'w+') as out:
338 self.firstSection = True
339 out.write(self.header)
340
341 self.PrintVariableSection(out, '_test_support_sources',
342 files['test_support'])
343 out.write('\n')
344
345 out.write('template("create_tests") {\n')
346
347 all_tests = []
348 for test in sorted(files['test']):
349 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
350 all_tests.append(test_name)
351
352 out.write(' executable("%s") {\n' % test_name)
353 out.write(' sources = [\n')
354 out.write(' "%s",\n' % test)
355 out.write(' ]\n')
356 out.write(' sources += _test_support_sources\n')
David Benjaminb3be1cf2016-04-27 19:15:06 -0400357 out.write(' if (defined(invoker.configs_exclude)) {\n')
358 out.write(' configs -= invoker.configs_exclude\n')
359 out.write(' }\n')
David Benjamin38d01c62016-04-21 18:47:57 -0400360 out.write(' configs += invoker.configs\n')
361 out.write(' deps = invoker.deps\n')
362 out.write(' }\n')
363 out.write('\n')
364
365 out.write(' group(target_name) {\n')
366 out.write(' deps = [\n')
367 for test_name in sorted(all_tests):
368 out.write(' ":%s",\n' % test_name)
369 out.write(' ]\n')
370 out.write(' }\n')
371 out.write('}\n')
372
373
374class GYP(object):
375
376 def __init__(self):
377 self.header = \
378"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
379# Use of this source code is governed by a BSD-style license that can be
380# found in the LICENSE file.
381
382# This file is created by generate_build_files.py. Do not edit manually.
383
384"""
385
386 def PrintVariableSection(self, out, name, files):
387 out.write(' \'%s\': [\n' % name)
388 for f in sorted(files):
389 out.write(' \'%s\',\n' % f)
390 out.write(' ],\n')
391
392 def WriteFiles(self, files, asm_outputs):
393 with open('boringssl.gypi', 'w+') as gypi:
394 gypi.write(self.header + '{\n \'variables\': {\n')
395
396 self.PrintVariableSection(
397 gypi, 'boringssl_ssl_sources', files['ssl'])
398 self.PrintVariableSection(
399 gypi, 'boringssl_crypto_sources', files['crypto'])
400
401 for ((osname, arch), asm_files) in asm_outputs:
402 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
403 (osname, arch), asm_files)
404
405 gypi.write(' }\n}\n')
406
407 with open('boringssl_tests.gypi', 'w+') as test_gypi:
408 test_gypi.write(self.header + '{\n \'targets\': [\n')
409
410 test_names = []
411 for test in sorted(files['test']):
412 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
413 test_gypi.write(""" {
414 'target_name': '%s',
415 'type': 'executable',
416 'dependencies': [
417 'boringssl.gyp:boringssl',
418 ],
419 'sources': [
420 '%s',
421 '<@(boringssl_test_support_sources)',
422 ],
423 # TODO(davidben): Fix size_t truncations in BoringSSL.
424 # https://crbug.com/429039
425 'msvs_disabled_warnings': [ 4267, ],
426 },\n""" % (test_name, test))
427 test_names.append(test_name)
428
429 test_names.sort()
430
431 test_gypi.write(' ],\n \'variables\': {\n')
432
433 self.PrintVariableSection(
434 test_gypi, 'boringssl_test_support_sources', files['test_support'])
435
436 test_gypi.write(' \'boringssl_test_targets\': [\n')
437
438 for test in sorted(test_names):
439 test_gypi.write(""" '%s',\n""" % test)
440
441 test_gypi.write(' ],\n }\n}\n')
442
443
Adam Langley9e1a6602015-05-05 17:47:53 -0700444def FindCMakeFiles(directory):
445 """Returns list of all CMakeLists.txt files recursively in directory."""
446 cmakefiles = []
447
448 for (path, _, filenames) in os.walk(directory):
449 for filename in filenames:
450 if filename == 'CMakeLists.txt':
451 cmakefiles.append(os.path.join(path, filename))
452
453 return cmakefiles
454
455
456def NoTests(dent, is_dir):
457 """Filter function that can be passed to FindCFiles in order to remove test
458 sources."""
459 if is_dir:
460 return dent != 'test'
461 return 'test.' not in dent and not dent.startswith('example_')
462
463
464def OnlyTests(dent, is_dir):
465 """Filter function that can be passed to FindCFiles in order to remove
466 non-test sources."""
467 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400468 return dent != 'test'
Adam Langley9e1a6602015-05-05 17:47:53 -0700469 return '_test.' in dent or dent.startswith('example_')
470
471
David Benjamin26073832015-05-11 20:52:48 -0400472def AllFiles(dent, is_dir):
473 """Filter function that can be passed to FindCFiles in order to include all
474 sources."""
475 return True
476
477
Adam Langley049ef412015-06-09 18:20:57 -0700478def SSLHeaderFiles(dent, is_dir):
479 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
480
481
Adam Langley9e1a6602015-05-05 17:47:53 -0700482def FindCFiles(directory, filter_func):
483 """Recurses through directory and returns a list of paths to all the C source
484 files that pass filter_func."""
485 cfiles = []
486
487 for (path, dirnames, filenames) in os.walk(directory):
488 for filename in filenames:
489 if not filename.endswith('.c') and not filename.endswith('.cc'):
490 continue
491 if not filter_func(filename, False):
492 continue
493 cfiles.append(os.path.join(path, filename))
494
495 for (i, dirname) in enumerate(dirnames):
496 if not filter_func(dirname, True):
497 del dirnames[i]
498
499 return cfiles
500
501
Adam Langley049ef412015-06-09 18:20:57 -0700502def FindHeaderFiles(directory, filter_func):
503 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
504 hfiles = []
505
506 for (path, dirnames, filenames) in os.walk(directory):
507 for filename in filenames:
508 if not filename.endswith('.h'):
509 continue
510 if not filter_func(filename, False):
511 continue
512 hfiles.append(os.path.join(path, filename))
513
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700514 for (i, dirname) in enumerate(dirnames):
515 if not filter_func(dirname, True):
516 del dirnames[i]
517
Adam Langley049ef412015-06-09 18:20:57 -0700518 return hfiles
519
520
Adam Langley9e1a6602015-05-05 17:47:53 -0700521def ExtractPerlAsmFromCMakeFile(cmakefile):
522 """Parses the contents of the CMakeLists.txt file passed as an argument and
523 returns a list of all the perlasm() directives found in the file."""
524 perlasms = []
525 with open(cmakefile) as f:
526 for line in f:
527 line = line.strip()
528 if not line.startswith('perlasm('):
529 continue
530 if not line.endswith(')'):
531 raise ValueError('Bad perlasm line in %s' % cmakefile)
532 # Remove "perlasm(" from start and ")" from end
533 params = line[8:-1].split()
534 if len(params) < 2:
535 raise ValueError('Bad perlasm line in %s' % cmakefile)
536 perlasms.append({
537 'extra_args': params[2:],
538 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
539 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
540 })
541
542 return perlasms
543
544
545def ReadPerlAsmOperations():
546 """Returns a list of all perlasm() directives found in CMake config files in
547 src/."""
548 perlasms = []
549 cmakefiles = FindCMakeFiles('src')
550
551 for cmakefile in cmakefiles:
552 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
553
554 return perlasms
555
556
557def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
558 """Runs the a perlasm script and puts the output into output_filename."""
559 base_dir = os.path.dirname(output_filename)
560 if not os.path.isdir(base_dir):
561 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400562 subprocess.check_call(
563 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700564
565
566def ArchForAsmFilename(filename):
567 """Returns the architectures that a given asm file should be compiled for
568 based on substrings in the filename."""
569
570 if 'x86_64' in filename or 'avx2' in filename:
571 return ['x86_64']
572 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
573 return ['x86']
574 elif 'armx' in filename:
575 return ['arm', 'aarch64']
576 elif 'armv8' in filename:
577 return ['aarch64']
578 elif 'arm' in filename:
579 return ['arm']
580 else:
581 raise ValueError('Unknown arch for asm filename: ' + filename)
582
583
584def WriteAsmFiles(perlasms):
585 """Generates asm files from perlasm directives for each supported OS x
586 platform combination."""
587 asmfiles = {}
588
589 for osarch in OS_ARCH_COMBOS:
590 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
591 key = (osname, arch)
592 outDir = '%s-%s' % key
593
594 for perlasm in perlasms:
595 filename = os.path.basename(perlasm['input'])
596 output = perlasm['output']
597 if not output.startswith('src'):
598 raise ValueError('output missing src: %s' % output)
599 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200600 if output.endswith('-armx.${ASM_EXT}'):
601 output = output.replace('-armx',
602 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700603 output = output.replace('${ASM_EXT}', asm_ext)
604
605 if arch in ArchForAsmFilename(filename):
606 PerlAsm(output, perlasm['input'], perlasm_style,
607 perlasm['extra_args'] + extra_args)
608 asmfiles.setdefault(key, []).append(output)
609
610 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
611 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
612
613 return asmfiles
614
615
Adam Langley049ef412015-06-09 18:20:57 -0700616def main(platforms):
Adam Langley9e1a6602015-05-05 17:47:53 -0700617 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
618 ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400619 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700620 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700621
622 # Generate err_data.c
623 with open('err_data.c', 'w+') as err_data:
624 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
625 cwd=os.path.join('src', 'crypto', 'err'),
626 stdout=err_data)
627 crypto_c_files.append('err_data.c')
628
David Benjamin38d01c62016-04-21 18:47:57 -0400629 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
630 AllFiles)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700631 test_support_h_files = (
632 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
633 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles))
David Benjamin26073832015-05-11 20:52:48 -0400634
Adam Langley9e1a6602015-05-05 17:47:53 -0700635 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
636 test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
637
David Benjamin38d01c62016-04-21 18:47:57 -0400638 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
639
Adam Langley049ef412015-06-09 18:20:57 -0700640 ssl_h_files = (
641 FindHeaderFiles(
642 os.path.join('src', 'include', 'openssl'),
643 SSLHeaderFiles))
644
645 def NotSSLHeaderFiles(filename, is_dir):
646 return not SSLHeaderFiles(filename, is_dir)
647 crypto_h_files = (
648 FindHeaderFiles(
649 os.path.join('src', 'include', 'openssl'),
650 NotSSLHeaderFiles))
651
652 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
653 crypto_internal_h_files = FindHeaderFiles(
654 os.path.join('src', 'crypto'), NoTests)
655
Adam Langley9c164b22015-06-10 18:54:47 -0700656 with open('src/util/all_tests.json', 'r') as f:
657 tests = json.load(f)
David Benjaminf277add2016-03-09 14:38:24 -0500658 # Skip tests for libdecrepit. Consumers import that manually.
659 tests = [test for test in tests if not test[0].startswith("decrepit/")]
Adam Langley9c164b22015-06-10 18:54:47 -0700660 test_binaries = set([test[0] for test in tests])
661 test_sources = set([
662 test.replace('.cc', '').replace('.c', '').replace(
663 'src/',
664 '')
665 for test in test_c_files])
666 if test_binaries != test_sources:
667 print 'Test sources and configured tests do not match'
668 a = test_binaries.difference(test_sources)
669 if len(a) > 0:
670 print 'These tests are configured without sources: ' + str(a)
671 b = test_sources.difference(test_binaries)
672 if len(b) > 0:
673 print 'These test sources are not configured: ' + str(b)
674
Adam Langley9e1a6602015-05-05 17:47:53 -0700675 files = {
676 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700677 'crypto_headers': crypto_h_files,
678 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400679 'fuzz': fuzz_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700680 'ssl': ssl_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700681 'ssl_headers': ssl_h_files,
682 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400683 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700684 'tool_headers': tool_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700685 'test': test_c_files,
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700686 'test_support': test_support_h_files + test_support_c_files,
Adam Langley9c164b22015-06-10 18:54:47 -0700687 'tests': tests,
Adam Langley9e1a6602015-05-05 17:47:53 -0700688 }
689
690 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
691
Adam Langley049ef412015-06-09 18:20:57 -0700692 for platform in platforms:
693 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700694
695 return 0
696
697
Adam Langley9e1a6602015-05-05 17:47:53 -0700698if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700699 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
700 ' [android|android-standalone|bazel|gn|gyp]')
701 parser.add_option('--prefix', dest='prefix',
702 help='For Bazel, prepend argument to all source files')
703 options, args = parser.parse_args(sys.argv[1:])
704 PREFIX = options.prefix
705
706 if not args:
707 parser.print_help()
708 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700709
Adam Langley049ef412015-06-09 18:20:57 -0700710 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700711 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400712 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700713 platforms.append(Android())
714 elif s == 'android-standalone':
715 platforms.append(AndroidStandalone())
716 elif s == 'bazel':
717 platforms.append(Bazel())
David Benjamin38d01c62016-04-21 18:47:57 -0400718 elif s == 'gn':
719 platforms.append(GN())
720 elif s == 'gyp':
721 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700722 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700723 parser.print_help()
724 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700725
Adam Langley049ef412015-06-09 18:20:57 -0700726 sys.exit(main(platforms))