blob: caed812bbc328f9f932054e9d5dcab6339903e1f [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 Langley9e1a6602015-05-05 17:47:53 -070046 ],
47}
48
Matt Braithwaite16695892016-06-09 09:34:11 -070049PREFIX = None
50
51
52def PathOf(x):
53 return x if not PREFIX else os.path.join(PREFIX, x)
54
Adam Langley9e1a6602015-05-05 17:47:53 -070055
Adam Langley9e1a6602015-05-05 17:47:53 -070056class Android(object):
57
58 def __init__(self):
59 self.header = \
60"""# Copyright (C) 2015 The Android Open Source Project
61#
62# Licensed under the Apache License, Version 2.0 (the "License");
63# you may not use this file except in compliance with the License.
64# You may obtain a copy of the License at
65#
66# http://www.apache.org/licenses/LICENSE-2.0
67#
68# Unless required by applicable law or agreed to in writing, software
69# distributed under the License is distributed on an "AS IS" BASIS,
70# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
71# See the License for the specific language governing permissions and
72# limitations under the License.
73
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070074# This file is created by generate_build_files.py. Do not edit manually.
75
Adam Langley9e1a6602015-05-05 17:47:53 -070076"""
77
78 def PrintVariableSection(self, out, name, files):
79 out.write('%s := \\\n' % name)
80 for f in sorted(files):
81 out.write(' %s\\\n' % f)
82 out.write('\n')
83
84 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070085 # New Android.bp format
86 with open('sources.bp', 'w+') as blueprint:
87 blueprint.write(self.header.replace('#', '//'))
88
89 blueprint.write('cc_defaults {\n')
90 blueprint.write(' name: "libcrypto_sources",\n')
91 blueprint.write(' srcs: [\n')
David Benjamin8c29e7d2016-09-30 21:34:31 -040092 for f in sorted(files['crypto']):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070093 blueprint.write(' "%s",\n' % f)
94 blueprint.write(' ],\n')
95 blueprint.write(' target: {\n')
96
97 for ((osname, arch), asm_files) in asm_outputs:
Steven Valdez93d242b2016-10-06 13:49:01 -040098 if osname != 'linux' or arch == 'ppc64le':
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070099 continue
100 if arch == 'aarch64':
101 arch = 'arm64'
102
Dan Willemsen2eb4bc52017-10-16 14:37:00 -0700103 blueprint.write(' linux_%s: {\n' % arch)
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700104 blueprint.write(' srcs: [\n')
105 for f in sorted(asm_files):
106 blueprint.write(' "%s",\n' % f)
107 blueprint.write(' ],\n')
108 blueprint.write(' },\n')
109
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700110 blueprint.write(' },\n')
111 blueprint.write('}\n\n')
112
113 blueprint.write('cc_defaults {\n')
114 blueprint.write(' name: "libssl_sources",\n')
115 blueprint.write(' srcs: [\n')
116 for f in sorted(files['ssl']):
117 blueprint.write(' "%s",\n' % f)
118 blueprint.write(' ],\n')
119 blueprint.write('}\n\n')
120
121 blueprint.write('cc_defaults {\n')
122 blueprint.write(' name: "bssl_sources",\n')
123 blueprint.write(' srcs: [\n')
124 for f in sorted(files['tool']):
125 blueprint.write(' "%s",\n' % f)
126 blueprint.write(' ],\n')
127 blueprint.write('}\n\n')
128
129 blueprint.write('cc_defaults {\n')
130 blueprint.write(' name: "boringssl_test_support_sources",\n')
131 blueprint.write(' srcs: [\n')
132 for f in sorted(files['test_support']):
133 blueprint.write(' "%s",\n' % f)
134 blueprint.write(' ],\n')
135 blueprint.write('}\n\n')
136
137 blueprint.write('cc_defaults {\n')
David Benjamin96628432017-01-19 19:05:47 -0500138 blueprint.write(' name: "boringssl_crypto_test_sources",\n')
139 blueprint.write(' srcs: [\n')
140 for f in sorted(files['crypto_test']):
141 blueprint.write(' "%s",\n' % f)
142 blueprint.write(' ],\n')
143 blueprint.write('}\n\n')
144
145 blueprint.write('cc_defaults {\n')
146 blueprint.write(' name: "boringssl_ssl_test_sources",\n')
147 blueprint.write(' srcs: [\n')
148 for f in sorted(files['ssl_test']):
149 blueprint.write(' "%s",\n' % f)
150 blueprint.write(' ],\n')
151 blueprint.write('}\n\n')
152
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700153 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700154 with open('sources.mk', 'w+') as makefile:
155 makefile.write(self.header)
156
David Benjamin8c29e7d2016-09-30 21:34:31 -0400157 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
Adam Langley9e1a6602015-05-05 17:47:53 -0700158
159 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700160 if osname != 'linux':
161 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700162 self.PrintVariableSection(
163 makefile, '%s_%s_sources' % (osname, arch), asm_files)
164
165
Adam Langley049ef412015-06-09 18:20:57 -0700166class Bazel(object):
167 """Bazel outputs files suitable for including in Bazel files."""
168
169 def __init__(self):
170 self.firstSection = True
171 self.header = \
172"""# This file is created by generate_build_files.py. Do not edit manually.
173
174"""
175
176 def PrintVariableSection(self, out, name, files):
177 if not self.firstSection:
178 out.write('\n')
179 self.firstSection = False
180
181 out.write('%s = [\n' % name)
182 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700183 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700184 out.write(']\n')
185
186 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700187 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700188 out.write(self.header)
189
190 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700191 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700192 self.PrintVariableSection(
193 out, 'ssl_internal_headers', files['ssl_internal_headers'])
194 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
195 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
196 self.PrintVariableSection(
197 out, 'crypto_internal_headers', files['crypto_internal_headers'])
198 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
199 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700200 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700201
202 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700203 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700204 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700205
Chuck Haysc608d6b2015-10-06 17:54:16 -0700206 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700207 out.write(self.header)
208
209 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400210 for filename in sorted(files['test_support'] +
211 files['test_support_headers'] +
212 files['crypto_internal_headers'] +
213 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700214 if os.path.basename(filename) == 'malloc.cc':
215 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700216 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700217
Adam Langley7b6acc52017-07-27 16:33:27 -0700218 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700219
David Benjamin96628432017-01-19 19:05:47 -0500220 self.PrintVariableSection(out, 'crypto_test_sources',
221 files['crypto_test'])
222 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
223
Adam Langley049ef412015-06-09 18:20:57 -0700224
Robert Sloane091af42017-10-09 12:47:17 -0700225class Eureka(object):
226
227 def __init__(self):
228 self.header = \
229"""# Copyright (C) 2017 The Android Open Source Project
230#
231# Licensed under the Apache License, Version 2.0 (the "License");
232# you may not use this file except in compliance with the License.
233# You may obtain a copy of the License at
234#
235# http://www.apache.org/licenses/LICENSE-2.0
236#
237# Unless required by applicable law or agreed to in writing, software
238# distributed under the License is distributed on an "AS IS" BASIS,
239# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
240# See the License for the specific language governing permissions and
241# limitations under the License.
242
243# This file is created by generate_build_files.py. Do not edit manually.
244
245"""
246
247 def PrintVariableSection(self, out, name, files):
248 out.write('%s := \\\n' % name)
249 for f in sorted(files):
250 out.write(' %s\\\n' % f)
251 out.write('\n')
252
253 def WriteFiles(self, files, asm_outputs):
254 # Legacy Android.mk format
255 with open('eureka.mk', 'w+') as makefile:
256 makefile.write(self.header)
257
258 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
259 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
260 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
261
262 for ((osname, arch), asm_files) in asm_outputs:
263 if osname != 'linux':
264 continue
265 self.PrintVariableSection(
266 makefile, '%s_%s_sources' % (osname, arch), asm_files)
267
268
David Benjamin38d01c62016-04-21 18:47:57 -0400269class GN(object):
270
271 def __init__(self):
272 self.firstSection = True
273 self.header = \
274"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
275# Use of this source code is governed by a BSD-style license that can be
276# found in the LICENSE file.
277
278# This file is created by generate_build_files.py. Do not edit manually.
279
280"""
281
282 def PrintVariableSection(self, out, name, files):
283 if not self.firstSection:
284 out.write('\n')
285 self.firstSection = False
286
287 out.write('%s = [\n' % name)
288 for f in sorted(files):
289 out.write(' "%s",\n' % f)
290 out.write(']\n')
291
292 def WriteFiles(self, files, asm_outputs):
293 with open('BUILD.generated.gni', 'w+') as out:
294 out.write(self.header)
295
David Benjaminc5aa8412016-07-29 17:41:58 -0400296 self.PrintVariableSection(out, 'crypto_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700297 files['crypto'] +
David Benjaminc5aa8412016-07-29 17:41:58 -0400298 files['crypto_internal_headers'])
James Robinson98dd68f2018-04-11 14:47:34 -0700299 self.PrintVariableSection(out, 'crypto_headers',
300 files['crypto_headers'])
David Benjaminc5aa8412016-07-29 17:41:58 -0400301 self.PrintVariableSection(out, 'ssl_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700302 files['ssl'] + files['ssl_internal_headers'])
303 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400304
305 for ((osname, arch), asm_files) in asm_outputs:
306 self.PrintVariableSection(
307 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
308
309 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
310 for fuzzer in files['fuzz']]
311 self.PrintVariableSection(out, 'fuzzers', fuzzers)
312
313 with open('BUILD.generated_tests.gni', 'w+') as out:
314 self.firstSection = True
315 out.write(self.header)
316
David Benjamin96628432017-01-19 19:05:47 -0500317 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400318 files['test_support'] +
319 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500320 self.PrintVariableSection(out, 'crypto_test_sources',
321 files['crypto_test'])
322 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400323
324
325class GYP(object):
326
327 def __init__(self):
328 self.header = \
329"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
330# Use of this source code is governed by a BSD-style license that can be
331# found in the LICENSE file.
332
333# This file is created by generate_build_files.py. Do not edit manually.
334
335"""
336
337 def PrintVariableSection(self, out, name, files):
338 out.write(' \'%s\': [\n' % name)
339 for f in sorted(files):
340 out.write(' \'%s\',\n' % f)
341 out.write(' ],\n')
342
343 def WriteFiles(self, files, asm_outputs):
344 with open('boringssl.gypi', 'w+') as gypi:
345 gypi.write(self.header + '{\n \'variables\': {\n')
346
David Benjaminc5aa8412016-07-29 17:41:58 -0400347 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
348 files['ssl'] + files['ssl_headers'] +
349 files['ssl_internal_headers'])
350 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
351 files['crypto'] + files['crypto_headers'] +
352 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400353
354 for ((osname, arch), asm_files) in asm_outputs:
355 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
356 (osname, arch), asm_files)
357
358 gypi.write(' }\n}\n')
359
David Benjamin38d01c62016-04-21 18:47:57 -0400360
Adam Langley9e1a6602015-05-05 17:47:53 -0700361def FindCMakeFiles(directory):
362 """Returns list of all CMakeLists.txt files recursively in directory."""
363 cmakefiles = []
364
365 for (path, _, filenames) in os.walk(directory):
366 for filename in filenames:
367 if filename == 'CMakeLists.txt':
368 cmakefiles.append(os.path.join(path, filename))
369
370 return cmakefiles
371
Adam Langleyfd499932017-04-04 14:21:43 -0700372def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700373 return is_dir or (path.startswith(
374 os.path.join('src', 'crypto', 'fipsmodule', '')) and
375 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700376
Adam Langleyfd499932017-04-04 14:21:43 -0700377def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700378 return (NoTests(path, dent, is_dir) and
379 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700380
381def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700382 """Filter function that can be passed to FindCFiles in order to remove test
383 sources."""
384 if is_dir:
385 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400386 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700387
388
Adam Langleyfd499932017-04-04 14:21:43 -0700389def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700390 """Filter function that can be passed to FindCFiles in order to remove
391 non-test sources."""
392 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400393 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400394 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700395
396
Adam Langleyfd499932017-04-04 14:21:43 -0700397def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400398 """Filter function that can be passed to FindCFiles in order to include all
399 sources."""
400 return True
401
402
Adam Langleyfd499932017-04-04 14:21:43 -0700403def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700404 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
405 order to exclude test runner files."""
406 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
407 # are in their own subpackage, from being included in boringssl/BUILD files.
408 return not is_dir or dent != 'runner'
409
410
David Benjamin3ecd0a52017-05-19 15:26:18 -0400411def NotGTestSupport(path, dent, is_dir):
412 return 'gtest' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500413
414
Adam Langleyfd499932017-04-04 14:21:43 -0700415def SSLHeaderFiles(path, dent, is_dir):
Adam Langley049ef412015-06-09 18:20:57 -0700416 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
417
418
Adam Langley9e1a6602015-05-05 17:47:53 -0700419def FindCFiles(directory, filter_func):
420 """Recurses through directory and returns a list of paths to all the C source
421 files that pass filter_func."""
422 cfiles = []
423
424 for (path, dirnames, filenames) in os.walk(directory):
425 for filename in filenames:
426 if not filename.endswith('.c') and not filename.endswith('.cc'):
427 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700428 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700429 continue
430 cfiles.append(os.path.join(path, filename))
431
432 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700433 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700434 del dirnames[i]
435
436 return cfiles
437
438
Adam Langley049ef412015-06-09 18:20:57 -0700439def FindHeaderFiles(directory, filter_func):
440 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
441 hfiles = []
442
443 for (path, dirnames, filenames) in os.walk(directory):
444 for filename in filenames:
445 if not filename.endswith('.h'):
446 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700447 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700448 continue
449 hfiles.append(os.path.join(path, filename))
450
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700451 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700452 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700453 del dirnames[i]
454
Adam Langley049ef412015-06-09 18:20:57 -0700455 return hfiles
456
457
Adam Langley9e1a6602015-05-05 17:47:53 -0700458def ExtractPerlAsmFromCMakeFile(cmakefile):
459 """Parses the contents of the CMakeLists.txt file passed as an argument and
460 returns a list of all the perlasm() directives found in the file."""
461 perlasms = []
462 with open(cmakefile) as f:
463 for line in f:
464 line = line.strip()
465 if not line.startswith('perlasm('):
466 continue
467 if not line.endswith(')'):
468 raise ValueError('Bad perlasm line in %s' % cmakefile)
469 # Remove "perlasm(" from start and ")" from end
470 params = line[8:-1].split()
471 if len(params) < 2:
472 raise ValueError('Bad perlasm line in %s' % cmakefile)
473 perlasms.append({
474 'extra_args': params[2:],
475 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
476 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
477 })
478
479 return perlasms
480
481
482def ReadPerlAsmOperations():
483 """Returns a list of all perlasm() directives found in CMake config files in
484 src/."""
485 perlasms = []
486 cmakefiles = FindCMakeFiles('src')
487
488 for cmakefile in cmakefiles:
489 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
490
491 return perlasms
492
493
494def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
495 """Runs the a perlasm script and puts the output into output_filename."""
496 base_dir = os.path.dirname(output_filename)
497 if not os.path.isdir(base_dir):
498 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400499 subprocess.check_call(
500 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700501
502
503def ArchForAsmFilename(filename):
504 """Returns the architectures that a given asm file should be compiled for
505 based on substrings in the filename."""
506
507 if 'x86_64' in filename or 'avx2' in filename:
508 return ['x86_64']
509 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
510 return ['x86']
511 elif 'armx' in filename:
512 return ['arm', 'aarch64']
513 elif 'armv8' in filename:
514 return ['aarch64']
515 elif 'arm' in filename:
516 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400517 elif 'ppc' in filename:
518 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700519 else:
520 raise ValueError('Unknown arch for asm filename: ' + filename)
521
522
523def WriteAsmFiles(perlasms):
524 """Generates asm files from perlasm directives for each supported OS x
525 platform combination."""
526 asmfiles = {}
527
528 for osarch in OS_ARCH_COMBOS:
529 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
530 key = (osname, arch)
531 outDir = '%s-%s' % key
532
533 for perlasm in perlasms:
534 filename = os.path.basename(perlasm['input'])
535 output = perlasm['output']
536 if not output.startswith('src'):
537 raise ValueError('output missing src: %s' % output)
538 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200539 if output.endswith('-armx.${ASM_EXT}'):
540 output = output.replace('-armx',
541 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700542 output = output.replace('${ASM_EXT}', asm_ext)
543
544 if arch in ArchForAsmFilename(filename):
545 PerlAsm(output, perlasm['input'], perlasm_style,
546 perlasm['extra_args'] + extra_args)
547 asmfiles.setdefault(key, []).append(output)
548
549 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
550 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
551
552 return asmfiles
553
554
David Benjamin3ecd0a52017-05-19 15:26:18 -0400555def ExtractVariablesFromCMakeFile(cmakefile):
556 """Parses the contents of the CMakeLists.txt file passed as an argument and
557 returns a dictionary of exported source lists."""
558 variables = {}
559 in_set_command = False
560 set_command = []
561 with open(cmakefile) as f:
562 for line in f:
563 if '#' in line:
564 line = line[:line.index('#')]
565 line = line.strip()
566
567 if not in_set_command:
568 if line.startswith('set('):
569 in_set_command = True
570 set_command = []
571 elif line == ')':
572 in_set_command = False
573 if not set_command:
574 raise ValueError('Empty set command')
575 variables[set_command[0]] = set_command[1:]
576 else:
577 set_command.extend([c for c in line.split(' ') if c])
578
579 if in_set_command:
580 raise ValueError('Unfinished set command')
581 return variables
582
583
Adam Langley049ef412015-06-09 18:20:57 -0700584def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400585 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Andres Erbsen5b280a82017-10-30 15:58:33 +0000586 crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
587 FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments))
Adam Langleyfd499932017-04-04 14:21:43 -0700588 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800589 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400590 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700591 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700592
David Benjamin0c9c1aa2017-12-12 15:19:20 -0500593 # third_party/fiat/p256.c lives in third_party/fiat, but it is a FIPS
594 # fragment, not a normal source file.
595 p256 = os.path.join('src', 'third_party', 'fiat', 'p256.c')
596 fips_fragments.append(p256)
597 crypto_c_files.remove(p256)
598
Adam Langley9e1a6602015-05-05 17:47:53 -0700599 # Generate err_data.c
600 with open('err_data.c', 'w+') as err_data:
601 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
602 cwd=os.path.join('src', 'crypto', 'err'),
603 stdout=err_data)
604 crypto_c_files.append('err_data.c')
605
David Benjamin38d01c62016-04-21 18:47:57 -0400606 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400607 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700608 test_support_h_files = (
609 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700610 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400611
David Benjamin3ecd0a52017-05-19 15:26:18 -0400612 # Generate crypto_test_data.cc
613 with open('crypto_test_data.cc', 'w+') as out:
614 subprocess.check_call(
615 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
616 cwd='src',
617 stdout=out)
618
David Benjamin96ee4a82017-07-09 23:46:47 -0400619 crypto_test_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
620 crypto_test_files += [
David Benjamin3ecd0a52017-05-19 15:26:18 -0400621 'crypto_test_data.cc',
622 'src/crypto/test/file_test_gtest.cc',
623 'src/crypto/test/gtest_main.cc',
624 ]
David Benjamin1d5a5702017-02-13 22:11:49 -0500625
626 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
627 ssl_test_files.append('src/crypto/test/gtest_main.cc')
Adam Langley9e1a6602015-05-05 17:47:53 -0700628
David Benjamin38d01c62016-04-21 18:47:57 -0400629 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
630
Adam Langley049ef412015-06-09 18:20:57 -0700631 ssl_h_files = (
632 FindHeaderFiles(
633 os.path.join('src', 'include', 'openssl'),
634 SSLHeaderFiles))
635
Adam Langleyfd499932017-04-04 14:21:43 -0700636 def NotSSLHeaderFiles(path, filename, is_dir):
637 return not SSLHeaderFiles(path, filename, is_dir)
Adam Langley049ef412015-06-09 18:20:57 -0700638 crypto_h_files = (
639 FindHeaderFiles(
640 os.path.join('src', 'include', 'openssl'),
641 NotSSLHeaderFiles))
642
643 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
Andres Erbsen5b280a82017-10-30 15:58:33 +0000644 crypto_internal_h_files = (
645 FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
646 FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests))
Adam Langley049ef412015-06-09 18:20:57 -0700647
Adam Langley9e1a6602015-05-05 17:47:53 -0700648 files = {
649 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700650 'crypto_headers': crypto_h_files,
651 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500652 'crypto_test': sorted(crypto_test_files),
Adam Langleyfd499932017-04-04 14:21:43 -0700653 'fips_fragments': fips_fragments,
David Benjamin38d01c62016-04-21 18:47:57 -0400654 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800655 'ssl': ssl_source_files,
Adam Langley049ef412015-06-09 18:20:57 -0700656 'ssl_headers': ssl_h_files,
657 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500658 'ssl_test': sorted(ssl_test_files),
David Benjamin38d01c62016-04-21 18:47:57 -0400659 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700660 'tool_headers': tool_h_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400661 'test_support': test_support_c_files,
662 'test_support_headers': test_support_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700663 }
664
665 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
666
Adam Langley049ef412015-06-09 18:20:57 -0700667 for platform in platforms:
668 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700669
670 return 0
671
672
Adam Langley9e1a6602015-05-05 17:47:53 -0700673if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700674 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
Robert Sloane091af42017-10-09 12:47:17 -0700675 ' [android|bazel|eureka|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700676 parser.add_option('--prefix', dest='prefix',
677 help='For Bazel, prepend argument to all source files')
678 options, args = parser.parse_args(sys.argv[1:])
679 PREFIX = options.prefix
680
681 if not args:
682 parser.print_help()
683 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700684
Adam Langley049ef412015-06-09 18:20:57 -0700685 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700686 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400687 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700688 platforms.append(Android())
Adam Langley049ef412015-06-09 18:20:57 -0700689 elif s == 'bazel':
690 platforms.append(Bazel())
Robert Sloane091af42017-10-09 12:47:17 -0700691 elif s == 'eureka':
692 platforms.append(Eureka())
David Benjamin38d01c62016-04-21 18:47:57 -0400693 elif s == 'gn':
694 platforms.append(GN())
695 elif s == 'gyp':
696 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700697 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700698 parser.print_help()
699 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700700
Adam Langley049ef412015-06-09 18:20:57 -0700701 sys.exit(main(platforms))