blob: 8be7c906cfe87236145e683f1bf6d4f7ebe800bb [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
David Benjamin96628432017-01-19 19:05:47 -050053# For now, GTest-based tests are specified manually. Once everything has updated
54# to support GTest, these will be determined automatically by looking for files
55# ending with _test.cc.
56CRYPTO_TEST_SOURCES = [
David Benjamin358baeb2017-01-23 15:16:14 -050057 'src/crypto/dh/dh_test.cc',
58 'src/crypto/dsa/dsa_test.cc',
David Benjamin96628432017-01-19 19:05:47 -050059]
60DECREPIT_TEST_SOURCES = [
David Benjamin358baeb2017-01-23 15:16:14 -050061 'src/decrepit/decrepit_test.cc',
David Benjamin96628432017-01-19 19:05:47 -050062]
63SSL_TEST_SOURCES = [
David Benjamin358baeb2017-01-23 15:16:14 -050064 'src/ssl/ssl_test.cc',
David Benjamin96628432017-01-19 19:05:47 -050065]
66
Matt Braithwaite16695892016-06-09 09:34:11 -070067PREFIX = None
68
69
70def PathOf(x):
71 return x if not PREFIX else os.path.join(PREFIX, x)
72
Adam Langley9e1a6602015-05-05 17:47:53 -070073
Adam Langley9e1a6602015-05-05 17:47:53 -070074class Android(object):
75
76 def __init__(self):
77 self.header = \
78"""# Copyright (C) 2015 The Android Open Source Project
79#
80# Licensed under the Apache License, Version 2.0 (the "License");
81# you may not use this file except in compliance with the License.
82# You may obtain a copy of the License at
83#
84# http://www.apache.org/licenses/LICENSE-2.0
85#
86# Unless required by applicable law or agreed to in writing, software
87# distributed under the License is distributed on an "AS IS" BASIS,
88# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89# See the License for the specific language governing permissions and
90# limitations under the License.
91
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070092# This file is created by generate_build_files.py. Do not edit manually.
93
Adam Langley9e1a6602015-05-05 17:47:53 -070094"""
95
96 def PrintVariableSection(self, out, name, files):
97 out.write('%s := \\\n' % name)
98 for f in sorted(files):
99 out.write(' %s\\\n' % f)
100 out.write('\n')
101
102 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700103 # New Android.bp format
104 with open('sources.bp', 'w+') as blueprint:
105 blueprint.write(self.header.replace('#', '//'))
106
107 blueprint.write('cc_defaults {\n')
108 blueprint.write(' name: "libcrypto_sources",\n')
109 blueprint.write(' srcs: [\n')
David Benjamin8c29e7d2016-09-30 21:34:31 -0400110 for f in sorted(files['crypto']):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700111 blueprint.write(' "%s",\n' % f)
112 blueprint.write(' ],\n')
113 blueprint.write(' target: {\n')
114
115 for ((osname, arch), asm_files) in asm_outputs:
Steven Valdez93d242b2016-10-06 13:49:01 -0400116 if osname != 'linux' or arch == 'ppc64le':
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700117 continue
118 if arch == 'aarch64':
119 arch = 'arm64'
120
121 blueprint.write(' android_%s: {\n' % arch)
122 blueprint.write(' srcs: [\n')
123 for f in sorted(asm_files):
124 blueprint.write(' "%s",\n' % f)
125 blueprint.write(' ],\n')
126 blueprint.write(' },\n')
127
128 if arch == 'x86' or arch == 'x86_64':
129 blueprint.write(' linux_%s: {\n' % arch)
130 blueprint.write(' srcs: [\n')
131 for f in sorted(asm_files):
132 blueprint.write(' "%s",\n' % f)
133 blueprint.write(' ],\n')
134 blueprint.write(' },\n')
135
136 blueprint.write(' },\n')
137 blueprint.write('}\n\n')
138
139 blueprint.write('cc_defaults {\n')
140 blueprint.write(' name: "libssl_sources",\n')
141 blueprint.write(' srcs: [\n')
142 for f in sorted(files['ssl']):
143 blueprint.write(' "%s",\n' % f)
144 blueprint.write(' ],\n')
145 blueprint.write('}\n\n')
146
147 blueprint.write('cc_defaults {\n')
148 blueprint.write(' name: "bssl_sources",\n')
149 blueprint.write(' srcs: [\n')
150 for f in sorted(files['tool']):
151 blueprint.write(' "%s",\n' % f)
152 blueprint.write(' ],\n')
153 blueprint.write('}\n\n')
154
155 blueprint.write('cc_defaults {\n')
156 blueprint.write(' name: "boringssl_test_support_sources",\n')
157 blueprint.write(' srcs: [\n')
158 for f in sorted(files['test_support']):
159 blueprint.write(' "%s",\n' % f)
160 blueprint.write(' ],\n')
161 blueprint.write('}\n\n')
162
163 blueprint.write('cc_defaults {\n')
David Benjamin96628432017-01-19 19:05:47 -0500164 blueprint.write(' name: "boringssl_crypto_test_sources",\n')
165 blueprint.write(' srcs: [\n')
166 for f in sorted(files['crypto_test']):
167 blueprint.write(' "%s",\n' % f)
168 blueprint.write(' ],\n')
169 blueprint.write('}\n\n')
170
171 blueprint.write('cc_defaults {\n')
172 blueprint.write(' name: "boringssl_ssl_test_sources",\n')
173 blueprint.write(' srcs: [\n')
174 for f in sorted(files['ssl_test']):
175 blueprint.write(' "%s",\n' % f)
176 blueprint.write(' ],\n')
177 blueprint.write('}\n\n')
178
179 blueprint.write('cc_defaults {\n')
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700180 blueprint.write(' name: "boringssl_tests_sources",\n')
181 blueprint.write(' srcs: [\n')
182 for f in sorted(files['test']):
183 blueprint.write(' "%s",\n' % f)
184 blueprint.write(' ],\n')
185 blueprint.write('}\n')
186
187 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700188 with open('sources.mk', 'w+') as makefile:
189 makefile.write(self.header)
190
David Benjamin8c29e7d2016-09-30 21:34:31 -0400191 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
Adam Langley9e1a6602015-05-05 17:47:53 -0700192
193 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700194 if osname != 'linux':
195 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700196 self.PrintVariableSection(
197 makefile, '%s_%s_sources' % (osname, arch), asm_files)
198
199
Adam Langley049ef412015-06-09 18:20:57 -0700200class Bazel(object):
201 """Bazel outputs files suitable for including in Bazel files."""
202
203 def __init__(self):
204 self.firstSection = True
205 self.header = \
206"""# This file is created by generate_build_files.py. Do not edit manually.
207
208"""
209
210 def PrintVariableSection(self, out, name, files):
211 if not self.firstSection:
212 out.write('\n')
213 self.firstSection = False
214
215 out.write('%s = [\n' % name)
216 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700217 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700218 out.write(']\n')
219
220 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700221 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700222 out.write(self.header)
223
224 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
225 self.PrintVariableSection(
226 out, 'ssl_internal_headers', files['ssl_internal_headers'])
227 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
Adam Langleyfeca9e52017-01-23 13:07:50 -0800228 self.PrintVariableSection(out, 'ssl_c_sources', files['ssl_c'])
229 self.PrintVariableSection(out, 'ssl_cc_sources', files['ssl_cc'])
Adam Langley049ef412015-06-09 18:20:57 -0700230 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
231 self.PrintVariableSection(
232 out, 'crypto_internal_headers', files['crypto_internal_headers'])
233 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
234 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700235 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700236
237 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700238 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700239 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700240
Chuck Haysc608d6b2015-10-06 17:54:16 -0700241 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700242 out.write(self.header)
243
244 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400245 for filename in sorted(files['test_support'] +
246 files['test_support_headers'] +
247 files['crypto_internal_headers'] +
248 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700249 if os.path.basename(filename) == 'malloc.cc':
250 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700251 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700252
Chuck Haysc608d6b2015-10-06 17:54:16 -0700253 out.write(']\n\n')
254
David Benjamin96628432017-01-19 19:05:47 -0500255 self.PrintVariableSection(out, 'crypto_test_sources',
256 files['crypto_test'])
257 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
258
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700259 out.write('def create_tests(copts, crypto, ssl):\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700260 name_counts = {}
261 for test in files['tests']:
262 name = os.path.basename(test[0])
263 name_counts[name] = name_counts.get(name, 0) + 1
264
265 first = True
266 for test in files['tests']:
267 name = os.path.basename(test[0])
268 if name_counts[name] > 1:
269 if '/' in test[1]:
270 name += '_' + os.path.splitext(os.path.basename(test[1]))[0]
271 else:
272 name += '_' + test[1].replace('-', '_')
273
274 if not first:
275 out.write('\n')
276 first = False
277
278 src_prefix = 'src/' + test[0]
279 for src in files['test']:
280 if src.startswith(src_prefix):
281 src = src
282 break
283 else:
284 raise ValueError("Can't find source for %s" % test[0])
285
Chuck Haysc608d6b2015-10-06 17:54:16 -0700286 out.write(' native.cc_test(\n')
287 out.write(' name = "%s",\n' % name)
288 out.write(' size = "small",\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700289 out.write(' srcs = ["%s"] + test_support_sources,\n' %
Matt Braithwaite16695892016-06-09 09:34:11 -0700290 PathOf(src))
Adam Langley9c164b22015-06-10 18:54:47 -0700291
292 data_files = []
293 if len(test) > 1:
294
Chuck Haysc608d6b2015-10-06 17:54:16 -0700295 out.write(' args = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700296 for arg in test[1:]:
297 if '/' in arg:
Matt Braithwaite16695892016-06-09 09:34:11 -0700298 out.write(' "$(location %s)",\n' %
299 PathOf(os.path.join('src', arg)))
Adam Langley9c164b22015-06-10 18:54:47 -0700300 data_files.append('src/%s' % arg)
301 else:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700302 out.write(' "%s",\n' % arg)
303 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700304
Adam Langleyd7b90022016-11-17 09:02:01 -0800305 out.write(' copts = copts + ["-DBORINGSSL_SHARED_LIBRARY"],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700306
307 if len(data_files) > 0:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700308 out.write(' data = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700309 for filename in data_files:
Matt Braithwaite16695892016-06-09 09:34:11 -0700310 out.write(' "%s",\n' % PathOf(filename))
Chuck Haysc608d6b2015-10-06 17:54:16 -0700311 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700312
313 if 'ssl/' in test[0]:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700314 out.write(' deps = [\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700315 out.write(' crypto,\n')
316 out.write(' ssl,\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700317 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700318 else:
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700319 out.write(' deps = [crypto],\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700320 out.write(' )\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700321
Adam Langley049ef412015-06-09 18:20:57 -0700322
David Benjamin38d01c62016-04-21 18:47:57 -0400323class GN(object):
324
325 def __init__(self):
326 self.firstSection = True
327 self.header = \
328"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
329# Use of this source code is governed by a BSD-style license that can be
330# found in the LICENSE file.
331
332# This file is created by generate_build_files.py. Do not edit manually.
333
334"""
335
336 def PrintVariableSection(self, out, name, files):
337 if not self.firstSection:
338 out.write('\n')
339 self.firstSection = False
340
341 out.write('%s = [\n' % name)
342 for f in sorted(files):
343 out.write(' "%s",\n' % f)
344 out.write(']\n')
345
346 def WriteFiles(self, files, asm_outputs):
347 with open('BUILD.generated.gni', 'w+') as out:
348 out.write(self.header)
349
David Benjaminc5aa8412016-07-29 17:41:58 -0400350 self.PrintVariableSection(out, 'crypto_sources',
351 files['crypto'] + files['crypto_headers'] +
352 files['crypto_internal_headers'])
353 self.PrintVariableSection(out, 'ssl_sources',
354 files['ssl'] + files['ssl_headers'] +
355 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400356
357 for ((osname, arch), asm_files) in asm_outputs:
358 self.PrintVariableSection(
359 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
360
361 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
362 for fuzzer in files['fuzz']]
363 self.PrintVariableSection(out, 'fuzzers', fuzzers)
364
365 with open('BUILD.generated_tests.gni', 'w+') as out:
366 self.firstSection = True
367 out.write(self.header)
368
David Benjamin96628432017-01-19 19:05:47 -0500369 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400370 files['test_support'] +
371 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500372 self.PrintVariableSection(out, 'crypto_test_sources',
373 files['crypto_test'])
374 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400375 out.write('\n')
376
377 out.write('template("create_tests") {\n')
378
379 all_tests = []
380 for test in sorted(files['test']):
381 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
382 all_tests.append(test_name)
383
384 out.write(' executable("%s") {\n' % test_name)
385 out.write(' sources = [\n')
386 out.write(' "%s",\n' % test)
387 out.write(' ]\n')
David Benjamin96628432017-01-19 19:05:47 -0500388 out.write(' sources += test_support_sources\n')
David Benjaminb3be1cf2016-04-27 19:15:06 -0400389 out.write(' if (defined(invoker.configs_exclude)) {\n')
390 out.write(' configs -= invoker.configs_exclude\n')
391 out.write(' }\n')
David Benjamin38d01c62016-04-21 18:47:57 -0400392 out.write(' configs += invoker.configs\n')
393 out.write(' deps = invoker.deps\n')
394 out.write(' }\n')
395 out.write('\n')
396
397 out.write(' group(target_name) {\n')
398 out.write(' deps = [\n')
399 for test_name in sorted(all_tests):
400 out.write(' ":%s",\n' % test_name)
401 out.write(' ]\n')
402 out.write(' }\n')
403 out.write('}\n')
404
405
406class GYP(object):
407
408 def __init__(self):
409 self.header = \
410"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
411# Use of this source code is governed by a BSD-style license that can be
412# found in the LICENSE file.
413
414# This file is created by generate_build_files.py. Do not edit manually.
415
416"""
417
418 def PrintVariableSection(self, out, name, files):
419 out.write(' \'%s\': [\n' % name)
420 for f in sorted(files):
421 out.write(' \'%s\',\n' % f)
422 out.write(' ],\n')
423
424 def WriteFiles(self, files, asm_outputs):
425 with open('boringssl.gypi', 'w+') as gypi:
426 gypi.write(self.header + '{\n \'variables\': {\n')
427
David Benjaminc5aa8412016-07-29 17:41:58 -0400428 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
429 files['ssl'] + files['ssl_headers'] +
430 files['ssl_internal_headers'])
431 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
432 files['crypto'] + files['crypto_headers'] +
433 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400434
435 for ((osname, arch), asm_files) in asm_outputs:
436 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
437 (osname, arch), asm_files)
438
439 gypi.write(' }\n}\n')
440
David Benjamin38d01c62016-04-21 18:47:57 -0400441
Adam Langley9e1a6602015-05-05 17:47:53 -0700442def FindCMakeFiles(directory):
443 """Returns list of all CMakeLists.txt files recursively in directory."""
444 cmakefiles = []
445
446 for (path, _, filenames) in os.walk(directory):
447 for filename in filenames:
448 if filename == 'CMakeLists.txt':
449 cmakefiles.append(os.path.join(path, filename))
450
451 return cmakefiles
452
453
454def NoTests(dent, is_dir):
455 """Filter function that can be passed to FindCFiles in order to remove test
456 sources."""
457 if is_dir:
458 return dent != 'test'
459 return 'test.' not in dent and not dent.startswith('example_')
460
461
462def OnlyTests(dent, is_dir):
463 """Filter function that can be passed to FindCFiles in order to remove
464 non-test sources."""
465 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400466 return dent != 'test'
David Benjamin96628432017-01-19 19:05:47 -0500467 # For now, GTest-based tests are specified manually.
468 if dent in [os.path.basename(p) for p in CRYPTO_TEST_SOURCES]:
469 return False
470 if dent in [os.path.basename(p) for p in DECREPIT_TEST_SOURCES]:
471 return False
472 if dent in [os.path.basename(p) for p in SSL_TEST_SOURCES]:
473 return False
Adam Langley9e1a6602015-05-05 17:47:53 -0700474 return '_test.' in dent or dent.startswith('example_')
475
476
David Benjamin26073832015-05-11 20:52:48 -0400477def AllFiles(dent, is_dir):
478 """Filter function that can be passed to FindCFiles in order to include all
479 sources."""
480 return True
481
482
David Benjamin96628432017-01-19 19:05:47 -0500483def NotGTestMain(dent, is_dir):
484 return dent != 'gtest_main.cc'
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']
David Benjamin9f16ce12016-09-27 16:30:22 -0400589 elif 'ppc' in filename:
590 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700591 else:
592 raise ValueError('Unknown arch for asm filename: ' + filename)
593
594
595def WriteAsmFiles(perlasms):
596 """Generates asm files from perlasm directives for each supported OS x
597 platform combination."""
598 asmfiles = {}
599
600 for osarch in OS_ARCH_COMBOS:
601 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
602 key = (osname, arch)
603 outDir = '%s-%s' % key
604
605 for perlasm in perlasms:
606 filename = os.path.basename(perlasm['input'])
607 output = perlasm['output']
608 if not output.startswith('src'):
609 raise ValueError('output missing src: %s' % output)
610 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200611 if output.endswith('-armx.${ASM_EXT}'):
612 output = output.replace('-armx',
613 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700614 output = output.replace('${ASM_EXT}', asm_ext)
615
616 if arch in ArchForAsmFilename(filename):
617 PerlAsm(output, perlasm['input'], perlasm_style,
618 perlasm['extra_args'] + extra_args)
619 asmfiles.setdefault(key, []).append(output)
620
621 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
622 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
623
624 return asmfiles
625
626
Adam Langley049ef412015-06-09 18:20:57 -0700627def main(platforms):
Adam Langley9e1a6602015-05-05 17:47:53 -0700628 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800629 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400630 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700631 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700632
633 # Generate err_data.c
634 with open('err_data.c', 'w+') as err_data:
635 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
636 cwd=os.path.join('src', 'crypto', 'err'),
637 stdout=err_data)
638 crypto_c_files.append('err_data.c')
639
David Benjamin38d01c62016-04-21 18:47:57 -0400640 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin96628432017-01-19 19:05:47 -0500641 NotGTestMain)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700642 test_support_h_files = (
643 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
644 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), AllFiles))
David Benjamin26073832015-05-11 20:52:48 -0400645
Adam Langley9e1a6602015-05-05 17:47:53 -0700646 test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
647 test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
648
David Benjamin38d01c62016-04-21 18:47:57 -0400649 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
650
Adam Langley049ef412015-06-09 18:20:57 -0700651 ssl_h_files = (
652 FindHeaderFiles(
653 os.path.join('src', 'include', 'openssl'),
654 SSLHeaderFiles))
655
656 def NotSSLHeaderFiles(filename, is_dir):
657 return not SSLHeaderFiles(filename, is_dir)
658 crypto_h_files = (
659 FindHeaderFiles(
660 os.path.join('src', 'include', 'openssl'),
661 NotSSLHeaderFiles))
662
663 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
664 crypto_internal_h_files = FindHeaderFiles(
665 os.path.join('src', 'crypto'), NoTests)
666
Adam Langley9c164b22015-06-10 18:54:47 -0700667 with open('src/util/all_tests.json', 'r') as f:
668 tests = json.load(f)
David Benjamin96628432017-01-19 19:05:47 -0500669 # For now, GTest-based tests are specified manually.
670 tests = [test for test in tests if test[0] not in ['crypto/crypto_test',
671 'decrepit/decrepit_test',
672 'ssl/ssl_test']]
Adam Langley9c164b22015-06-10 18:54:47 -0700673 test_binaries = set([test[0] for test in tests])
674 test_sources = set([
675 test.replace('.cc', '').replace('.c', '').replace(
676 'src/',
677 '')
678 for test in test_c_files])
679 if test_binaries != test_sources:
680 print 'Test sources and configured tests do not match'
681 a = test_binaries.difference(test_sources)
682 if len(a) > 0:
683 print 'These tests are configured without sources: ' + str(a)
684 b = test_sources.difference(test_binaries)
685 if len(b) > 0:
686 print 'These test sources are not configured: ' + str(b)
687
Adam Langley9e1a6602015-05-05 17:47:53 -0700688 files = {
689 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700690 'crypto_headers': crypto_h_files,
691 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin96628432017-01-19 19:05:47 -0500692 'crypto_test': sorted(CRYPTO_TEST_SOURCES +
David Benjamin358baeb2017-01-23 15:16:14 -0500693 ['src/crypto/test/gtest_main.cc']),
David Benjamin38d01c62016-04-21 18:47:57 -0400694 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800695 'ssl': ssl_source_files,
696 'ssl_c': [s for s in ssl_source_files if s.endswith('.c')],
697 'ssl_cc': [s for s in ssl_source_files if s.endswith('.cc')],
Adam Langley049ef412015-06-09 18:20:57 -0700698 'ssl_headers': ssl_h_files,
699 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin358baeb2017-01-23 15:16:14 -0500700 'ssl_test': sorted(SSL_TEST_SOURCES + ['src/crypto/test/gtest_main.cc']),
David Benjamin38d01c62016-04-21 18:47:57 -0400701 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700702 'tool_headers': tool_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700703 'test': test_c_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400704 'test_support': test_support_c_files,
705 'test_support_headers': test_support_h_files,
Adam Langley9c164b22015-06-10 18:54:47 -0700706 'tests': tests,
Adam Langley9e1a6602015-05-05 17:47:53 -0700707 }
708
709 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
710
Adam Langley049ef412015-06-09 18:20:57 -0700711 for platform in platforms:
712 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700713
714 return 0
715
716
Adam Langley9e1a6602015-05-05 17:47:53 -0700717if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700718 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
David Benjamin8c29e7d2016-09-30 21:34:31 -0400719 ' [android|bazel|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700720 parser.add_option('--prefix', dest='prefix',
721 help='For Bazel, prepend argument to all source files')
722 options, args = parser.parse_args(sys.argv[1:])
723 PREFIX = options.prefix
724
725 if not args:
726 parser.print_help()
727 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700728
Adam Langley049ef412015-06-09 18:20:57 -0700729 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700730 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400731 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700732 platforms.append(Android())
Adam Langley049ef412015-06-09 18:20:57 -0700733 elif s == 'bazel':
734 platforms.append(Bazel())
David Benjamin38d01c62016-04-21 18:47:57 -0400735 elif s == 'gn':
736 platforms.append(GN())
737 elif s == 'gyp':
738 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700739 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700740 parser.print_help()
741 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700742
Adam Langley049ef412015-06-09 18:20:57 -0700743 sys.exit(main(platforms))