blob: 44db7f57a6daf46b0314772c578c184c1b7cf644 [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 = [
David Benjaminf6584e72017-06-08 16:27:16 -040027 ('ios', 'arm', 'ios32', [], 'S'),
28 ('ios', 'aarch64', 'ios64', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070029 ('linux', 'arm', 'linux32', [], 'S'),
30 ('linux', 'aarch64', 'linux64', [], 'S'),
Adam Langley7c075b92017-05-22 15:31:13 -070031 ('linux', 'ppc64le', 'linux64le', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070032 ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
33 ('linux', 'x86_64', 'elf', [], 'S'),
34 ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
35 ('mac', 'x86_64', 'macosx', [], 'S'),
36 ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
37 ('win', 'x86_64', 'nasm', [], 'asm'),
38]
39
40# NON_PERL_FILES enumerates assembly files that are not processed by the
41# perlasm system.
42NON_PERL_FILES = {
43 ('linux', 'arm'): [
Adam Langley7b8b9c12016-01-04 07:13:00 -080044 'src/crypto/curve25519/asm/x25519-asm-arm.S',
David Benjamin3c4a5cb2016-03-29 17:43:31 -040045 'src/crypto/poly1305/poly1305_arm_asm.S',
Adam Langley7b935932018-11-12 13:53:42 -080046 'src/crypto/hrss/asm/poly_mul_vec_armv7_neon.S',
47 ],
48 ('linux', 'x86_64'): [
49 'src/crypto/hrss/asm/poly_rq_mul.S',
Adam Langley9e1a6602015-05-05 17:47:53 -070050 ],
51}
52
Matt Braithwaite16695892016-06-09 09:34:11 -070053PREFIX = None
Adam Langley990a3232018-05-22 10:02:59 -070054EMBED_TEST_DATA = True
Matt Braithwaite16695892016-06-09 09:34:11 -070055
56
57def PathOf(x):
58 return x if not PREFIX else os.path.join(PREFIX, x)
59
Adam Langley9e1a6602015-05-05 17:47:53 -070060
Adam Langley9e1a6602015-05-05 17:47:53 -070061class Android(object):
62
63 def __init__(self):
64 self.header = \
65"""# Copyright (C) 2015 The Android Open Source Project
66#
67# Licensed under the Apache License, Version 2.0 (the "License");
68# you may not use this file except in compliance with the License.
69# You may obtain a copy of the License at
70#
71# http://www.apache.org/licenses/LICENSE-2.0
72#
73# Unless required by applicable law or agreed to in writing, software
74# distributed under the License is distributed on an "AS IS" BASIS,
75# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
76# See the License for the specific language governing permissions and
77# limitations under the License.
78
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070079# This file is created by generate_build_files.py. Do not edit manually.
80
Adam Langley9e1a6602015-05-05 17:47:53 -070081"""
82
83 def PrintVariableSection(self, out, name, files):
84 out.write('%s := \\\n' % name)
85 for f in sorted(files):
86 out.write(' %s\\\n' % f)
87 out.write('\n')
88
89 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070090 # New Android.bp format
91 with open('sources.bp', 'w+') as blueprint:
92 blueprint.write(self.header.replace('#', '//'))
93
94 blueprint.write('cc_defaults {\n')
95 blueprint.write(' name: "libcrypto_sources",\n')
96 blueprint.write(' srcs: [\n')
David Benjamin8c29e7d2016-09-30 21:34:31 -040097 for f in sorted(files['crypto']):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070098 blueprint.write(' "%s",\n' % f)
99 blueprint.write(' ],\n')
100 blueprint.write(' target: {\n')
101
102 for ((osname, arch), asm_files) in asm_outputs:
Steven Valdez93d242b2016-10-06 13:49:01 -0400103 if osname != 'linux' or arch == 'ppc64le':
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700104 continue
105 if arch == 'aarch64':
106 arch = 'arm64'
107
Dan Willemsen2eb4bc52017-10-16 14:37:00 -0700108 blueprint.write(' linux_%s: {\n' % arch)
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700109 blueprint.write(' srcs: [\n')
110 for f in sorted(asm_files):
111 blueprint.write(' "%s",\n' % f)
112 blueprint.write(' ],\n')
113 blueprint.write(' },\n')
114
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700115 blueprint.write(' },\n')
116 blueprint.write('}\n\n')
117
118 blueprint.write('cc_defaults {\n')
119 blueprint.write(' name: "libssl_sources",\n')
120 blueprint.write(' srcs: [\n')
121 for f in sorted(files['ssl']):
122 blueprint.write(' "%s",\n' % f)
123 blueprint.write(' ],\n')
124 blueprint.write('}\n\n')
125
126 blueprint.write('cc_defaults {\n')
127 blueprint.write(' name: "bssl_sources",\n')
128 blueprint.write(' srcs: [\n')
129 for f in sorted(files['tool']):
130 blueprint.write(' "%s",\n' % f)
131 blueprint.write(' ],\n')
132 blueprint.write('}\n\n')
133
134 blueprint.write('cc_defaults {\n')
135 blueprint.write(' name: "boringssl_test_support_sources",\n')
136 blueprint.write(' srcs: [\n')
137 for f in sorted(files['test_support']):
138 blueprint.write(' "%s",\n' % f)
139 blueprint.write(' ],\n')
140 blueprint.write('}\n\n')
141
142 blueprint.write('cc_defaults {\n')
David Benjamin96628432017-01-19 19:05:47 -0500143 blueprint.write(' name: "boringssl_crypto_test_sources",\n')
144 blueprint.write(' srcs: [\n')
145 for f in sorted(files['crypto_test']):
146 blueprint.write(' "%s",\n' % f)
147 blueprint.write(' ],\n')
148 blueprint.write('}\n\n')
149
150 blueprint.write('cc_defaults {\n')
151 blueprint.write(' name: "boringssl_ssl_test_sources",\n')
152 blueprint.write(' srcs: [\n')
153 for f in sorted(files['ssl_test']):
154 blueprint.write(' "%s",\n' % f)
155 blueprint.write(' ],\n')
156 blueprint.write('}\n\n')
157
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700158 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700159 with open('sources.mk', 'w+') as makefile:
160 makefile.write(self.header)
161
David Benjamin8c29e7d2016-09-30 21:34:31 -0400162 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
Adam Langley9e1a6602015-05-05 17:47:53 -0700163
164 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700165 if osname != 'linux':
166 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700167 self.PrintVariableSection(
168 makefile, '%s_%s_sources' % (osname, arch), asm_files)
169
170
Adam Langley049ef412015-06-09 18:20:57 -0700171class Bazel(object):
172 """Bazel outputs files suitable for including in Bazel files."""
173
174 def __init__(self):
175 self.firstSection = True
176 self.header = \
177"""# This file is created by generate_build_files.py. Do not edit manually.
178
179"""
180
181 def PrintVariableSection(self, out, name, files):
182 if not self.firstSection:
183 out.write('\n')
184 self.firstSection = False
185
186 out.write('%s = [\n' % name)
187 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700188 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700189 out.write(']\n')
190
191 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700192 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700193 out.write(self.header)
194
195 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700196 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700197 self.PrintVariableSection(
198 out, 'ssl_internal_headers', files['ssl_internal_headers'])
199 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
200 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
201 self.PrintVariableSection(
202 out, 'crypto_internal_headers', files['crypto_internal_headers'])
203 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
204 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700205 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700206
207 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700208 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700209 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700210
Chuck Haysc608d6b2015-10-06 17:54:16 -0700211 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700212 out.write(self.header)
213
214 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400215 for filename in sorted(files['test_support'] +
216 files['test_support_headers'] +
217 files['crypto_internal_headers'] +
218 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700219 if os.path.basename(filename) == 'malloc.cc':
220 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700221 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700222
Adam Langley7b6acc52017-07-27 16:33:27 -0700223 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700224
David Benjamin96628432017-01-19 19:05:47 -0500225 self.PrintVariableSection(out, 'crypto_test_sources',
226 files['crypto_test'])
227 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
Adam Langley990a3232018-05-22 10:02:59 -0700228 self.PrintVariableSection(out, 'crypto_test_data',
229 files['crypto_test_data'])
David Benjamin96628432017-01-19 19:05:47 -0500230
Adam Langley049ef412015-06-09 18:20:57 -0700231
Robert Sloane091af42017-10-09 12:47:17 -0700232class Eureka(object):
233
234 def __init__(self):
235 self.header = \
236"""# Copyright (C) 2017 The Android Open Source Project
237#
238# Licensed under the Apache License, Version 2.0 (the "License");
239# you may not use this file except in compliance with the License.
240# You may obtain a copy of the License at
241#
242# http://www.apache.org/licenses/LICENSE-2.0
243#
244# Unless required by applicable law or agreed to in writing, software
245# distributed under the License is distributed on an "AS IS" BASIS,
246# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
247# See the License for the specific language governing permissions and
248# limitations under the License.
249
250# This file is created by generate_build_files.py. Do not edit manually.
251
252"""
253
254 def PrintVariableSection(self, out, name, files):
255 out.write('%s := \\\n' % name)
256 for f in sorted(files):
257 out.write(' %s\\\n' % f)
258 out.write('\n')
259
260 def WriteFiles(self, files, asm_outputs):
261 # Legacy Android.mk format
262 with open('eureka.mk', 'w+') as makefile:
263 makefile.write(self.header)
264
265 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
266 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
267 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
268
269 for ((osname, arch), asm_files) in asm_outputs:
270 if osname != 'linux':
271 continue
272 self.PrintVariableSection(
273 makefile, '%s_%s_sources' % (osname, arch), asm_files)
274
275
David Benjamin38d01c62016-04-21 18:47:57 -0400276class GN(object):
277
278 def __init__(self):
279 self.firstSection = True
280 self.header = \
281"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
282# Use of this source code is governed by a BSD-style license that can be
283# found in the LICENSE file.
284
285# This file is created by generate_build_files.py. Do not edit manually.
286
287"""
288
289 def PrintVariableSection(self, out, name, files):
290 if not self.firstSection:
291 out.write('\n')
292 self.firstSection = False
293
294 out.write('%s = [\n' % name)
295 for f in sorted(files):
296 out.write(' "%s",\n' % f)
297 out.write(']\n')
298
299 def WriteFiles(self, files, asm_outputs):
300 with open('BUILD.generated.gni', 'w+') as out:
301 out.write(self.header)
302
David Benjaminc5aa8412016-07-29 17:41:58 -0400303 self.PrintVariableSection(out, 'crypto_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700304 files['crypto'] +
David Benjaminc5aa8412016-07-29 17:41:58 -0400305 files['crypto_internal_headers'])
James Robinson98dd68f2018-04-11 14:47:34 -0700306 self.PrintVariableSection(out, 'crypto_headers',
307 files['crypto_headers'])
David Benjaminc5aa8412016-07-29 17:41:58 -0400308 self.PrintVariableSection(out, 'ssl_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700309 files['ssl'] + files['ssl_internal_headers'])
310 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400311
312 for ((osname, arch), asm_files) in asm_outputs:
313 self.PrintVariableSection(
314 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
315
316 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
317 for fuzzer in files['fuzz']]
318 self.PrintVariableSection(out, 'fuzzers', fuzzers)
319
320 with open('BUILD.generated_tests.gni', 'w+') as out:
321 self.firstSection = True
322 out.write(self.header)
323
David Benjamin96628432017-01-19 19:05:47 -0500324 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400325 files['test_support'] +
326 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500327 self.PrintVariableSection(out, 'crypto_test_sources',
328 files['crypto_test'])
329 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400330
331
332class GYP(object):
333
334 def __init__(self):
335 self.header = \
336"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
337# Use of this source code is governed by a BSD-style license that can be
338# found in the LICENSE file.
339
340# This file is created by generate_build_files.py. Do not edit manually.
341
342"""
343
344 def PrintVariableSection(self, out, name, files):
345 out.write(' \'%s\': [\n' % name)
346 for f in sorted(files):
347 out.write(' \'%s\',\n' % f)
348 out.write(' ],\n')
349
350 def WriteFiles(self, files, asm_outputs):
351 with open('boringssl.gypi', 'w+') as gypi:
352 gypi.write(self.header + '{\n \'variables\': {\n')
353
David Benjaminc5aa8412016-07-29 17:41:58 -0400354 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
355 files['ssl'] + files['ssl_headers'] +
356 files['ssl_internal_headers'])
357 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
358 files['crypto'] + files['crypto_headers'] +
359 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400360
361 for ((osname, arch), asm_files) in asm_outputs:
362 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
363 (osname, arch), asm_files)
364
365 gypi.write(' }\n}\n')
366
David Benjamin38d01c62016-04-21 18:47:57 -0400367
Adam Langley9e1a6602015-05-05 17:47:53 -0700368def FindCMakeFiles(directory):
369 """Returns list of all CMakeLists.txt files recursively in directory."""
370 cmakefiles = []
371
372 for (path, _, filenames) in os.walk(directory):
373 for filename in filenames:
374 if filename == 'CMakeLists.txt':
375 cmakefiles.append(os.path.join(path, filename))
376
377 return cmakefiles
378
Adam Langleyfd499932017-04-04 14:21:43 -0700379def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700380 return is_dir or (path.startswith(
381 os.path.join('src', 'crypto', 'fipsmodule', '')) and
382 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700383
Adam Langleyfd499932017-04-04 14:21:43 -0700384def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700385 return (NoTests(path, dent, is_dir) and
386 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700387
388def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700389 """Filter function that can be passed to FindCFiles in order to remove test
390 sources."""
391 if is_dir:
392 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400393 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700394
395
Adam Langleyfd499932017-04-04 14:21:43 -0700396def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700397 """Filter function that can be passed to FindCFiles in order to remove
398 non-test sources."""
399 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400400 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400401 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700402
403
Adam Langleyfd499932017-04-04 14:21:43 -0700404def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400405 """Filter function that can be passed to FindCFiles in order to include all
406 sources."""
407 return True
408
409
Adam Langleyfd499932017-04-04 14:21:43 -0700410def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700411 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
412 order to exclude test runner files."""
413 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
414 # are in their own subpackage, from being included in boringssl/BUILD files.
415 return not is_dir or dent != 'runner'
416
417
David Benjamin3ecd0a52017-05-19 15:26:18 -0400418def NotGTestSupport(path, dent, is_dir):
419 return 'gtest' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500420
421
Adam Langleyfd499932017-04-04 14:21:43 -0700422def SSLHeaderFiles(path, dent, is_dir):
Aaron Green0e150022018-10-16 12:05:29 -0700423 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h', 'srtp.h']
Adam Langley049ef412015-06-09 18:20:57 -0700424
425
Adam Langley9e1a6602015-05-05 17:47:53 -0700426def FindCFiles(directory, filter_func):
427 """Recurses through directory and returns a list of paths to all the C source
428 files that pass filter_func."""
429 cfiles = []
430
431 for (path, dirnames, filenames) in os.walk(directory):
432 for filename in filenames:
433 if not filename.endswith('.c') and not filename.endswith('.cc'):
434 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700435 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700436 continue
437 cfiles.append(os.path.join(path, filename))
438
439 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700440 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700441 del dirnames[i]
442
443 return cfiles
444
445
Adam Langley049ef412015-06-09 18:20:57 -0700446def FindHeaderFiles(directory, filter_func):
447 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
448 hfiles = []
449
450 for (path, dirnames, filenames) in os.walk(directory):
451 for filename in filenames:
452 if not filename.endswith('.h'):
453 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700454 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700455 continue
456 hfiles.append(os.path.join(path, filename))
457
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700458 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700459 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700460 del dirnames[i]
461
Adam Langley049ef412015-06-09 18:20:57 -0700462 return hfiles
463
464
Adam Langley9e1a6602015-05-05 17:47:53 -0700465def ExtractPerlAsmFromCMakeFile(cmakefile):
466 """Parses the contents of the CMakeLists.txt file passed as an argument and
467 returns a list of all the perlasm() directives found in the file."""
468 perlasms = []
469 with open(cmakefile) as f:
470 for line in f:
471 line = line.strip()
472 if not line.startswith('perlasm('):
473 continue
474 if not line.endswith(')'):
475 raise ValueError('Bad perlasm line in %s' % cmakefile)
476 # Remove "perlasm(" from start and ")" from end
477 params = line[8:-1].split()
478 if len(params) < 2:
479 raise ValueError('Bad perlasm line in %s' % cmakefile)
480 perlasms.append({
481 'extra_args': params[2:],
482 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
483 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
484 })
485
486 return perlasms
487
488
489def ReadPerlAsmOperations():
490 """Returns a list of all perlasm() directives found in CMake config files in
491 src/."""
492 perlasms = []
493 cmakefiles = FindCMakeFiles('src')
494
495 for cmakefile in cmakefiles:
496 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
497
498 return perlasms
499
500
501def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
502 """Runs the a perlasm script and puts the output into output_filename."""
503 base_dir = os.path.dirname(output_filename)
504 if not os.path.isdir(base_dir):
505 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400506 subprocess.check_call(
507 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700508
509
510def ArchForAsmFilename(filename):
511 """Returns the architectures that a given asm file should be compiled for
512 based on substrings in the filename."""
513
514 if 'x86_64' in filename or 'avx2' in filename:
515 return ['x86_64']
516 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
517 return ['x86']
518 elif 'armx' in filename:
519 return ['arm', 'aarch64']
520 elif 'armv8' in filename:
521 return ['aarch64']
522 elif 'arm' in filename:
523 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400524 elif 'ppc' in filename:
525 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700526 else:
527 raise ValueError('Unknown arch for asm filename: ' + filename)
528
529
530def WriteAsmFiles(perlasms):
531 """Generates asm files from perlasm directives for each supported OS x
532 platform combination."""
533 asmfiles = {}
534
535 for osarch in OS_ARCH_COMBOS:
536 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
537 key = (osname, arch)
538 outDir = '%s-%s' % key
539
540 for perlasm in perlasms:
541 filename = os.path.basename(perlasm['input'])
542 output = perlasm['output']
543 if not output.startswith('src'):
544 raise ValueError('output missing src: %s' % output)
545 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200546 if output.endswith('-armx.${ASM_EXT}'):
547 output = output.replace('-armx',
548 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700549 output = output.replace('${ASM_EXT}', asm_ext)
550
551 if arch in ArchForAsmFilename(filename):
552 PerlAsm(output, perlasm['input'], perlasm_style,
553 perlasm['extra_args'] + extra_args)
554 asmfiles.setdefault(key, []).append(output)
555
556 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
557 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
558
559 return asmfiles
560
561
David Benjamin3ecd0a52017-05-19 15:26:18 -0400562def ExtractVariablesFromCMakeFile(cmakefile):
563 """Parses the contents of the CMakeLists.txt file passed as an argument and
564 returns a dictionary of exported source lists."""
565 variables = {}
566 in_set_command = False
567 set_command = []
568 with open(cmakefile) as f:
569 for line in f:
570 if '#' in line:
571 line = line[:line.index('#')]
572 line = line.strip()
573
574 if not in_set_command:
575 if line.startswith('set('):
576 in_set_command = True
577 set_command = []
578 elif line == ')':
579 in_set_command = False
580 if not set_command:
581 raise ValueError('Empty set command')
582 variables[set_command[0]] = set_command[1:]
583 else:
584 set_command.extend([c for c in line.split(' ') if c])
585
586 if in_set_command:
587 raise ValueError('Unfinished set command')
588 return variables
589
590
Adam Langley049ef412015-06-09 18:20:57 -0700591def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400592 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Andres Erbsen5b280a82017-10-30 15:58:33 +0000593 crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
594 FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments))
Adam Langleyfd499932017-04-04 14:21:43 -0700595 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800596 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400597 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700598 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700599
David Benjamin0c9c1aa2017-12-12 15:19:20 -0500600 # third_party/fiat/p256.c lives in third_party/fiat, but it is a FIPS
601 # fragment, not a normal source file.
602 p256 = os.path.join('src', 'third_party', 'fiat', 'p256.c')
603 fips_fragments.append(p256)
604 crypto_c_files.remove(p256)
605
Adam Langley9e1a6602015-05-05 17:47:53 -0700606 # Generate err_data.c
607 with open('err_data.c', 'w+') as err_data:
608 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
609 cwd=os.path.join('src', 'crypto', 'err'),
610 stdout=err_data)
611 crypto_c_files.append('err_data.c')
612
David Benjamin38d01c62016-04-21 18:47:57 -0400613 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400614 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700615 test_support_h_files = (
616 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700617 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400618
Adam Langley990a3232018-05-22 10:02:59 -0700619 crypto_test_files = []
620 if EMBED_TEST_DATA:
621 # Generate crypto_test_data.cc
622 with open('crypto_test_data.cc', 'w+') as out:
623 subprocess.check_call(
624 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
625 cwd='src',
626 stdout=out)
627 crypto_test_files += ['crypto_test_data.cc']
David Benjamin3ecd0a52017-05-19 15:26:18 -0400628
Adam Langley990a3232018-05-22 10:02:59 -0700629 crypto_test_files += FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
David Benjamin96ee4a82017-07-09 23:46:47 -0400630 crypto_test_files += [
David Benjamin3ecd0a52017-05-19 15:26:18 -0400631 'src/crypto/test/file_test_gtest.cc',
632 'src/crypto/test/gtest_main.cc',
633 ]
David Benjamin1d5a5702017-02-13 22:11:49 -0500634
635 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
636 ssl_test_files.append('src/crypto/test/gtest_main.cc')
Adam Langley9e1a6602015-05-05 17:47:53 -0700637
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
Adam Langleyfd499932017-04-04 14:21:43 -0700645 def NotSSLHeaderFiles(path, filename, is_dir):
646 return not SSLHeaderFiles(path, filename, is_dir)
Adam Langley049ef412015-06-09 18:20:57 -0700647 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)
Andres Erbsen5b280a82017-10-30 15:58:33 +0000653 crypto_internal_h_files = (
654 FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
655 FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests))
Adam Langley049ef412015-06-09 18:20:57 -0700656
Adam Langley9e1a6602015-05-05 17:47:53 -0700657 files = {
658 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700659 'crypto_headers': crypto_h_files,
660 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500661 'crypto_test': sorted(crypto_test_files),
Adam Langley990a3232018-05-22 10:02:59 -0700662 'crypto_test_data': sorted('src/' + x for x in cmake['CRYPTO_TEST_DATA']),
Adam Langleyfd499932017-04-04 14:21:43 -0700663 'fips_fragments': fips_fragments,
David Benjamin38d01c62016-04-21 18:47:57 -0400664 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800665 'ssl': ssl_source_files,
Adam Langley049ef412015-06-09 18:20:57 -0700666 'ssl_headers': ssl_h_files,
667 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500668 'ssl_test': sorted(ssl_test_files),
David Benjamin38d01c62016-04-21 18:47:57 -0400669 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700670 'tool_headers': tool_h_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400671 'test_support': test_support_c_files,
672 'test_support_headers': test_support_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700673 }
674
675 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
676
Adam Langley049ef412015-06-09 18:20:57 -0700677 for platform in platforms:
678 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700679
680 return 0
681
682
Adam Langley9e1a6602015-05-05 17:47:53 -0700683if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700684 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
Robert Sloane091af42017-10-09 12:47:17 -0700685 ' [android|bazel|eureka|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700686 parser.add_option('--prefix', dest='prefix',
687 help='For Bazel, prepend argument to all source files')
Adam Langley990a3232018-05-22 10:02:59 -0700688 parser.add_option(
689 '--embed_test_data', type='choice', dest='embed_test_data',
690 action='store', default="true", choices=["true", "false"],
691 help='For Bazel, don\'t embed data files in crypto_test_data.cc')
Matt Braithwaite16695892016-06-09 09:34:11 -0700692 options, args = parser.parse_args(sys.argv[1:])
693 PREFIX = options.prefix
Adam Langley990a3232018-05-22 10:02:59 -0700694 EMBED_TEST_DATA = (options.embed_test_data == "true")
Matt Braithwaite16695892016-06-09 09:34:11 -0700695
696 if not args:
697 parser.print_help()
698 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700699
Adam Langley049ef412015-06-09 18:20:57 -0700700 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700701 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400702 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700703 platforms.append(Android())
Adam Langley049ef412015-06-09 18:20:57 -0700704 elif s == 'bazel':
705 platforms.append(Bazel())
Robert Sloane091af42017-10-09 12:47:17 -0700706 elif s == 'eureka':
707 platforms.append(Eureka())
David Benjamin38d01c62016-04-21 18:47:57 -0400708 elif s == 'gn':
709 platforms.append(GN())
710 elif s == 'gyp':
711 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700712 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700713 parser.print_help()
714 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700715
Adam Langley049ef412015-06-09 18:20:57 -0700716 sys.exit(main(platforms))