blob: d9f6522413eb9d517aaacee32c64500fd11198f4 [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 = [
57 'crypto/dh/dh_test.cc',
58 'crypto/dsa/dsa_test.cc',
59]
60DECREPIT_TEST_SOURCES = [
61 'decrepit/decrepit_test.cc',
62]
63SSL_TEST_SOURCES = [
64 'ssl/ssl_test.cc',
65]
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'])
228 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
229 self.PrintVariableSection(
230 out, 'crypto_internal_headers', files['crypto_internal_headers'])
231 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
232 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700233 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700234
235 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700236 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700237 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700238
Chuck Haysc608d6b2015-10-06 17:54:16 -0700239 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700240 out.write(self.header)
241
242 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400243 for filename in sorted(files['test_support'] +
244 files['test_support_headers'] +
245 files['crypto_internal_headers'] +
246 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700247 if os.path.basename(filename) == 'malloc.cc':
248 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700249 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700250
Chuck Haysc608d6b2015-10-06 17:54:16 -0700251 out.write(']\n\n')
252
David Benjamin96628432017-01-19 19:05:47 -0500253 self.PrintVariableSection(out, 'crypto_test_sources',
254 files['crypto_test'])
255 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
256
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700257 out.write('def create_tests(copts, crypto, ssl):\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700258 name_counts = {}
259 for test in files['tests']:
260 name = os.path.basename(test[0])
261 name_counts[name] = name_counts.get(name, 0) + 1
262
263 first = True
264 for test in files['tests']:
265 name = os.path.basename(test[0])
266 if name_counts[name] > 1:
267 if '/' in test[1]:
268 name += '_' + os.path.splitext(os.path.basename(test[1]))[0]
269 else:
270 name += '_' + test[1].replace('-', '_')
271
272 if not first:
273 out.write('\n')
274 first = False
275
276 src_prefix = 'src/' + test[0]
277 for src in files['test']:
278 if src.startswith(src_prefix):
279 src = src
280 break
281 else:
282 raise ValueError("Can't find source for %s" % test[0])
283
Chuck Haysc608d6b2015-10-06 17:54:16 -0700284 out.write(' native.cc_test(\n')
285 out.write(' name = "%s",\n' % name)
286 out.write(' size = "small",\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700287 out.write(' srcs = ["%s"] + test_support_sources,\n' %
Matt Braithwaite16695892016-06-09 09:34:11 -0700288 PathOf(src))
Adam Langley9c164b22015-06-10 18:54:47 -0700289
290 data_files = []
291 if len(test) > 1:
292
Chuck Haysc608d6b2015-10-06 17:54:16 -0700293 out.write(' args = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700294 for arg in test[1:]:
295 if '/' in arg:
Matt Braithwaite16695892016-06-09 09:34:11 -0700296 out.write(' "$(location %s)",\n' %
297 PathOf(os.path.join('src', arg)))
Adam Langley9c164b22015-06-10 18:54:47 -0700298 data_files.append('src/%s' % arg)
299 else:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700300 out.write(' "%s",\n' % arg)
301 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700302
Adam Langleyd7b90022016-11-17 09:02:01 -0800303 out.write(' copts = copts + ["-DBORINGSSL_SHARED_LIBRARY"],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700304
305 if len(data_files) > 0:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700306 out.write(' data = [\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700307 for filename in data_files:
Matt Braithwaite16695892016-06-09 09:34:11 -0700308 out.write(' "%s",\n' % PathOf(filename))
Chuck Haysc608d6b2015-10-06 17:54:16 -0700309 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700310
311 if 'ssl/' in test[0]:
Chuck Haysc608d6b2015-10-06 17:54:16 -0700312 out.write(' deps = [\n')
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700313 out.write(' crypto,\n')
314 out.write(' ssl,\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700315 out.write(' ],\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700316 else:
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700317 out.write(' deps = [crypto],\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700318 out.write(' )\n')
Adam Langley9c164b22015-06-10 18:54:47 -0700319
Adam Langley049ef412015-06-09 18:20:57 -0700320
David Benjamin38d01c62016-04-21 18:47:57 -0400321class GN(object):
322
323 def __init__(self):
324 self.firstSection = True
325 self.header = \
326"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
327# Use of this source code is governed by a BSD-style license that can be
328# found in the LICENSE file.
329
330# This file is created by generate_build_files.py. Do not edit manually.
331
332"""
333
334 def PrintVariableSection(self, out, name, files):
335 if not self.firstSection:
336 out.write('\n')
337 self.firstSection = False
338
339 out.write('%s = [\n' % name)
340 for f in sorted(files):
341 out.write(' "%s",\n' % f)
342 out.write(']\n')
343
344 def WriteFiles(self, files, asm_outputs):
345 with open('BUILD.generated.gni', 'w+') as out:
346 out.write(self.header)
347
David Benjaminc5aa8412016-07-29 17:41:58 -0400348 self.PrintVariableSection(out, 'crypto_sources',
349 files['crypto'] + files['crypto_headers'] +
350 files['crypto_internal_headers'])
351 self.PrintVariableSection(out, 'ssl_sources',
352 files['ssl'] + files['ssl_headers'] +
353 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400354
355 for ((osname, arch), asm_files) in asm_outputs:
356 self.PrintVariableSection(
357 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
358
359 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
360 for fuzzer in files['fuzz']]
361 self.PrintVariableSection(out, 'fuzzers', fuzzers)
362
363 with open('BUILD.generated_tests.gni', 'w+') as out:
364 self.firstSection = True
365 out.write(self.header)
366
David Benjamin96628432017-01-19 19:05:47 -0500367 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400368 files['test_support'] +
369 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500370 self.PrintVariableSection(out, 'crypto_test_sources',
371 files['crypto_test'])
372 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400373 out.write('\n')
374
375 out.write('template("create_tests") {\n')
376
377 all_tests = []
378 for test in sorted(files['test']):
379 test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
380 all_tests.append(test_name)
381
382 out.write(' executable("%s") {\n' % test_name)
383 out.write(' sources = [\n')
384 out.write(' "%s",\n' % test)
385 out.write(' ]\n')
David Benjamin96628432017-01-19 19:05:47 -0500386 out.write(' sources += test_support_sources\n')
David Benjaminb3be1cf2016-04-27 19:15:06 -0400387 out.write(' if (defined(invoker.configs_exclude)) {\n')
388 out.write(' configs -= invoker.configs_exclude\n')
389 out.write(' }\n')
David Benjamin38d01c62016-04-21 18:47:57 -0400390 out.write(' configs += invoker.configs\n')
391 out.write(' deps = invoker.deps\n')
392 out.write(' }\n')
393 out.write('\n')
394
395 out.write(' group(target_name) {\n')
396 out.write(' deps = [\n')
397 for test_name in sorted(all_tests):
398 out.write(' ":%s",\n' % test_name)
399 out.write(' ]\n')
400 out.write(' }\n')
401 out.write('}\n')
402
403
404class GYP(object):
405
406 def __init__(self):
407 self.header = \
408"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
409# Use of this source code is governed by a BSD-style license that can be
410# found in the LICENSE file.
411
412# This file is created by generate_build_files.py. Do not edit manually.
413
414"""
415
416 def PrintVariableSection(self, out, name, files):
417 out.write(' \'%s\': [\n' % name)
418 for f in sorted(files):
419 out.write(' \'%s\',\n' % f)
420 out.write(' ],\n')
421
422 def WriteFiles(self, files, asm_outputs):
423 with open('boringssl.gypi', 'w+') as gypi:
424 gypi.write(self.header + '{\n \'variables\': {\n')
425
David Benjaminc5aa8412016-07-29 17:41:58 -0400426 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
427 files['ssl'] + files['ssl_headers'] +
428 files['ssl_internal_headers'])
429 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
430 files['crypto'] + files['crypto_headers'] +
431 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400432
433 for ((osname, arch), asm_files) in asm_outputs:
434 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
435 (osname, arch), asm_files)
436
437 gypi.write(' }\n}\n')
438
David Benjamin38d01c62016-04-21 18:47:57 -0400439
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'
David Benjamin96628432017-01-19 19:05:47 -0500465 # For now, GTest-based tests are specified manually.
466 if dent in [os.path.basename(p) for p in CRYPTO_TEST_SOURCES]:
467 return False
468 if dent in [os.path.basename(p) for p in DECREPIT_TEST_SOURCES]:
469 return False
470 if dent in [os.path.basename(p) for p in SSL_TEST_SOURCES]:
471 return False
Adam Langley9e1a6602015-05-05 17:47:53 -0700472 return '_test.' in dent or dent.startswith('example_')
473
474
David Benjamin26073832015-05-11 20:52:48 -0400475def AllFiles(dent, is_dir):
476 """Filter function that can be passed to FindCFiles in order to include all
477 sources."""
478 return True
479
480
David Benjamin96628432017-01-19 19:05:47 -0500481def NotGTestMain(dent, is_dir):
482 return dent != 'gtest_main.cc'
483
484
Adam Langley049ef412015-06-09 18:20:57 -0700485def SSLHeaderFiles(dent, is_dir):
486 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
487
488
Adam Langley9e1a6602015-05-05 17:47:53 -0700489def FindCFiles(directory, filter_func):
490 """Recurses through directory and returns a list of paths to all the C source
491 files that pass filter_func."""
492 cfiles = []
493
494 for (path, dirnames, filenames) in os.walk(directory):
495 for filename in filenames:
496 if not filename.endswith('.c') and not filename.endswith('.cc'):
497 continue
498 if not filter_func(filename, False):
499 continue
500 cfiles.append(os.path.join(path, filename))
501
502 for (i, dirname) in enumerate(dirnames):
503 if not filter_func(dirname, True):
504 del dirnames[i]
505
506 return cfiles
507
508
Adam Langley049ef412015-06-09 18:20:57 -0700509def FindHeaderFiles(directory, filter_func):
510 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
511 hfiles = []
512
513 for (path, dirnames, filenames) in os.walk(directory):
514 for filename in filenames:
515 if not filename.endswith('.h'):
516 continue
517 if not filter_func(filename, False):
518 continue
519 hfiles.append(os.path.join(path, filename))
520
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700521 for (i, dirname) in enumerate(dirnames):
522 if not filter_func(dirname, True):
523 del dirnames[i]
524
Adam Langley049ef412015-06-09 18:20:57 -0700525 return hfiles
526
527
Adam Langley9e1a6602015-05-05 17:47:53 -0700528def ExtractPerlAsmFromCMakeFile(cmakefile):
529 """Parses the contents of the CMakeLists.txt file passed as an argument and
530 returns a list of all the perlasm() directives found in the file."""
531 perlasms = []
532 with open(cmakefile) as f:
533 for line in f:
534 line = line.strip()
535 if not line.startswith('perlasm('):
536 continue
537 if not line.endswith(')'):
538 raise ValueError('Bad perlasm line in %s' % cmakefile)
539 # Remove "perlasm(" from start and ")" from end
540 params = line[8:-1].split()
541 if len(params) < 2:
542 raise ValueError('Bad perlasm line in %s' % cmakefile)
543 perlasms.append({
544 'extra_args': params[2:],
545 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
546 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
547 })
548
549 return perlasms
550
551
552def ReadPerlAsmOperations():
553 """Returns a list of all perlasm() directives found in CMake config files in
554 src/."""
555 perlasms = []
556 cmakefiles = FindCMakeFiles('src')
557
558 for cmakefile in cmakefiles:
559 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
560
561 return perlasms
562
563
564def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
565 """Runs the a perlasm script and puts the output into output_filename."""
566 base_dir = os.path.dirname(output_filename)
567 if not os.path.isdir(base_dir):
568 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400569 subprocess.check_call(
570 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700571
572
573def ArchForAsmFilename(filename):
574 """Returns the architectures that a given asm file should be compiled for
575 based on substrings in the filename."""
576
577 if 'x86_64' in filename or 'avx2' in filename:
578 return ['x86_64']
579 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
580 return ['x86']
581 elif 'armx' in filename:
582 return ['arm', 'aarch64']
583 elif 'armv8' in filename:
584 return ['aarch64']
585 elif 'arm' in filename:
586 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400587 elif 'ppc' in filename:
588 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700589 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'),
David Benjamin96628432017-01-19 19:05:47 -0500639 NotGTestMain)
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 Benjamin96628432017-01-19 19:05:47 -0500667 # For now, GTest-based tests are specified manually.
668 tests = [test for test in tests if test[0] not in ['crypto/crypto_test',
669 'decrepit/decrepit_test',
670 'ssl/ssl_test']]
Adam Langley9c164b22015-06-10 18:54:47 -0700671 test_binaries = set([test[0] for test in tests])
672 test_sources = set([
673 test.replace('.cc', '').replace('.c', '').replace(
674 'src/',
675 '')
676 for test in test_c_files])
677 if test_binaries != test_sources:
678 print 'Test sources and configured tests do not match'
679 a = test_binaries.difference(test_sources)
680 if len(a) > 0:
681 print 'These tests are configured without sources: ' + str(a)
682 b = test_sources.difference(test_binaries)
683 if len(b) > 0:
684 print 'These test sources are not configured: ' + str(b)
685
Adam Langley9e1a6602015-05-05 17:47:53 -0700686 files = {
687 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700688 'crypto_headers': crypto_h_files,
689 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin96628432017-01-19 19:05:47 -0500690 'crypto_test': sorted(CRYPTO_TEST_SOURCES +
691 ['crypto/test/gtest_main.cc']),
David Benjamin38d01c62016-04-21 18:47:57 -0400692 'fuzz': fuzz_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700693 'ssl': ssl_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700694 'ssl_headers': ssl_h_files,
695 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin96628432017-01-19 19:05:47 -0500696 'ssl_test': sorted(SSL_TEST_SOURCES + ['crypto/test/gtest_main.cc']),
David Benjamin38d01c62016-04-21 18:47:57 -0400697 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700698 'tool_headers': tool_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700699 'test': test_c_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400700 'test_support': test_support_c_files,
701 'test_support_headers': test_support_h_files,
Adam Langley9c164b22015-06-10 18:54:47 -0700702 'tests': tests,
Adam Langley9e1a6602015-05-05 17:47:53 -0700703 }
704
705 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
706
Adam Langley049ef412015-06-09 18:20:57 -0700707 for platform in platforms:
708 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700709
710 return 0
711
712
Adam Langley9e1a6602015-05-05 17:47:53 -0700713if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700714 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
David Benjamin8c29e7d2016-09-30 21:34:31 -0400715 ' [android|bazel|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700716 parser.add_option('--prefix', dest='prefix',
717 help='For Bazel, prepend argument to all source files')
718 options, args = parser.parse_args(sys.argv[1:])
719 PREFIX = options.prefix
720
721 if not args:
722 parser.print_help()
723 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700724
Adam Langley049ef412015-06-09 18:20:57 -0700725 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700726 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400727 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700728 platforms.append(Android())
Adam Langley049ef412015-06-09 18:20:57 -0700729 elif s == 'bazel':
730 platforms.append(Bazel())
David Benjamin38d01c62016-04-21 18:47:57 -0400731 elif s == 'gn':
732 platforms.append(GN())
733 elif s == 'gyp':
734 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700735 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700736 parser.print_help()
737 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700738
Adam Langley049ef412015-06-09 18:20:57 -0700739 sys.exit(main(platforms))