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