blob: a4af6667d9e73a67b3b218850b9ba06009afb0df [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 ],
Matt Braithwaitee021a242016-01-14 13:41:46 -080047 ('linux', 'x86_64'): [
48 'src/crypto/curve25519/asm/x25519-asm-x86_64.S',
49 ],
Piotr Sikora8ca0b412016-06-02 11:59:21 -070050 ('mac', 'x86_64'): [
51 'src/crypto/curve25519/asm/x25519-asm-x86_64.S',
52 ],
Adam Langley9e1a6602015-05-05 17:47:53 -070053}
54
Matt Braithwaite16695892016-06-09 09:34:11 -070055PREFIX = None
56
57
58def PathOf(x):
59 return x if not PREFIX else os.path.join(PREFIX, x)
60
Adam Langley9e1a6602015-05-05 17:47:53 -070061
Adam Langley9e1a6602015-05-05 17:47:53 -070062class Android(object):
63
64 def __init__(self):
65 self.header = \
66"""# Copyright (C) 2015 The Android Open Source Project
67#
68# Licensed under the Apache License, Version 2.0 (the "License");
69# you may not use this file except in compliance with the License.
70# You may obtain a copy of the License at
71#
72# http://www.apache.org/licenses/LICENSE-2.0
73#
74# Unless required by applicable law or agreed to in writing, software
75# distributed under the License is distributed on an "AS IS" BASIS,
76# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77# See the License for the specific language governing permissions and
78# limitations under the License.
79
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070080# This file is created by generate_build_files.py. Do not edit manually.
81
Adam Langley9e1a6602015-05-05 17:47:53 -070082"""
83
84 def PrintVariableSection(self, out, name, files):
85 out.write('%s := \\\n' % name)
86 for f in sorted(files):
87 out.write(' %s\\\n' % f)
88 out.write('\n')
89
90 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070091 # New Android.bp format
92 with open('sources.bp', 'w+') as blueprint:
93 blueprint.write(self.header.replace('#', '//'))
94
95 blueprint.write('cc_defaults {\n')
96 blueprint.write(' name: "libcrypto_sources",\n')
97 blueprint.write(' srcs: [\n')
David Benjamin8c29e7d2016-09-30 21:34:31 -040098 for f in sorted(files['crypto']):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070099 blueprint.write(' "%s",\n' % f)
100 blueprint.write(' ],\n')
101 blueprint.write(' target: {\n')
102
103 for ((osname, arch), asm_files) in asm_outputs:
Steven Valdez93d242b2016-10-06 13:49:01 -0400104 if osname != 'linux' or arch == 'ppc64le':
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700105 continue
106 if arch == 'aarch64':
107 arch = 'arm64'
108
Dan Willemsen2eb4bc52017-10-16 14:37:00 -0700109 blueprint.write(' linux_%s: {\n' % arch)
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700110 blueprint.write(' srcs: [\n')
111 for f in sorted(asm_files):
112 blueprint.write(' "%s",\n' % f)
113 blueprint.write(' ],\n')
114 blueprint.write(' },\n')
115
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700116 blueprint.write(' },\n')
117 blueprint.write('}\n\n')
118
119 blueprint.write('cc_defaults {\n')
120 blueprint.write(' name: "libssl_sources",\n')
121 blueprint.write(' srcs: [\n')
122 for f in sorted(files['ssl']):
123 blueprint.write(' "%s",\n' % f)
124 blueprint.write(' ],\n')
125 blueprint.write('}\n\n')
126
127 blueprint.write('cc_defaults {\n')
128 blueprint.write(' name: "bssl_sources",\n')
129 blueprint.write(' srcs: [\n')
130 for f in sorted(files['tool']):
131 blueprint.write(' "%s",\n' % f)
132 blueprint.write(' ],\n')
133 blueprint.write('}\n\n')
134
135 blueprint.write('cc_defaults {\n')
136 blueprint.write(' name: "boringssl_test_support_sources",\n')
137 blueprint.write(' srcs: [\n')
138 for f in sorted(files['test_support']):
139 blueprint.write(' "%s",\n' % f)
140 blueprint.write(' ],\n')
141 blueprint.write('}\n\n')
142
143 blueprint.write('cc_defaults {\n')
David Benjamin96628432017-01-19 19:05:47 -0500144 blueprint.write(' name: "boringssl_crypto_test_sources",\n')
145 blueprint.write(' srcs: [\n')
146 for f in sorted(files['crypto_test']):
147 blueprint.write(' "%s",\n' % f)
148 blueprint.write(' ],\n')
149 blueprint.write('}\n\n')
150
151 blueprint.write('cc_defaults {\n')
152 blueprint.write(' name: "boringssl_ssl_test_sources",\n')
153 blueprint.write(' srcs: [\n')
154 for f in sorted(files['ssl_test']):
155 blueprint.write(' "%s",\n' % f)
156 blueprint.write(' ],\n')
157 blueprint.write('}\n\n')
158
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700159 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700160 with open('sources.mk', 'w+') as makefile:
161 makefile.write(self.header)
162
David Benjamin8c29e7d2016-09-30 21:34:31 -0400163 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
Adam Langley9e1a6602015-05-05 17:47:53 -0700164
165 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700166 if osname != 'linux':
167 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700168 self.PrintVariableSection(
169 makefile, '%s_%s_sources' % (osname, arch), asm_files)
170
171
Adam Langley049ef412015-06-09 18:20:57 -0700172class Bazel(object):
173 """Bazel outputs files suitable for including in Bazel files."""
174
175 def __init__(self):
176 self.firstSection = True
177 self.header = \
178"""# This file is created by generate_build_files.py. Do not edit manually.
179
180"""
181
182 def PrintVariableSection(self, out, name, files):
183 if not self.firstSection:
184 out.write('\n')
185 self.firstSection = False
186
187 out.write('%s = [\n' % name)
188 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700189 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700190 out.write(']\n')
191
192 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700193 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700194 out.write(self.header)
195
196 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700197 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700198 self.PrintVariableSection(
199 out, 'ssl_internal_headers', files['ssl_internal_headers'])
200 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
201 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
202 self.PrintVariableSection(
203 out, 'crypto_internal_headers', files['crypto_internal_headers'])
204 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
205 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700206 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700207
208 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700209 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700210 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700211
Chuck Haysc608d6b2015-10-06 17:54:16 -0700212 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700213 out.write(self.header)
214
215 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400216 for filename in sorted(files['test_support'] +
217 files['test_support_headers'] +
218 files['crypto_internal_headers'] +
219 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700220 if os.path.basename(filename) == 'malloc.cc':
221 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700222 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700223
Adam Langley7b6acc52017-07-27 16:33:27 -0700224 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700225
David Benjamin96628432017-01-19 19:05:47 -0500226 self.PrintVariableSection(out, 'crypto_test_sources',
227 files['crypto_test'])
228 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
229
Adam Langley049ef412015-06-09 18:20:57 -0700230
Robert Sloane091af42017-10-09 12:47:17 -0700231class Eureka(object):
232
233 def __init__(self):
234 self.header = \
235"""# Copyright (C) 2017 The Android Open Source Project
236#
237# Licensed under the Apache License, Version 2.0 (the "License");
238# you may not use this file except in compliance with the License.
239# You may obtain a copy of the License at
240#
241# http://www.apache.org/licenses/LICENSE-2.0
242#
243# Unless required by applicable law or agreed to in writing, software
244# distributed under the License is distributed on an "AS IS" BASIS,
245# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
246# See the License for the specific language governing permissions and
247# limitations under the License.
248
249# This file is created by generate_build_files.py. Do not edit manually.
250
251"""
252
253 def PrintVariableSection(self, out, name, files):
254 out.write('%s := \\\n' % name)
255 for f in sorted(files):
256 out.write(' %s\\\n' % f)
257 out.write('\n')
258
259 def WriteFiles(self, files, asm_outputs):
260 # Legacy Android.mk format
261 with open('eureka.mk', 'w+') as makefile:
262 makefile.write(self.header)
263
264 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
265 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
266 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
267
268 for ((osname, arch), asm_files) in asm_outputs:
269 if osname != 'linux':
270 continue
271 self.PrintVariableSection(
272 makefile, '%s_%s_sources' % (osname, arch), asm_files)
273
274
David Benjamin38d01c62016-04-21 18:47:57 -0400275class GN(object):
276
277 def __init__(self):
278 self.firstSection = True
279 self.header = \
280"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
281# Use of this source code is governed by a BSD-style license that can be
282# found in the LICENSE file.
283
284# This file is created by generate_build_files.py. Do not edit manually.
285
286"""
287
288 def PrintVariableSection(self, out, name, files):
289 if not self.firstSection:
290 out.write('\n')
291 self.firstSection = False
292
293 out.write('%s = [\n' % name)
294 for f in sorted(files):
295 out.write(' "%s",\n' % f)
296 out.write(']\n')
297
298 def WriteFiles(self, files, asm_outputs):
299 with open('BUILD.generated.gni', 'w+') as out:
300 out.write(self.header)
301
David Benjaminc5aa8412016-07-29 17:41:58 -0400302 self.PrintVariableSection(out, 'crypto_sources',
303 files['crypto'] + files['crypto_headers'] +
304 files['crypto_internal_headers'])
305 self.PrintVariableSection(out, 'ssl_sources',
306 files['ssl'] + files['ssl_headers'] +
307 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400308
309 for ((osname, arch), asm_files) in asm_outputs:
310 self.PrintVariableSection(
311 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
312
313 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
314 for fuzzer in files['fuzz']]
315 self.PrintVariableSection(out, 'fuzzers', fuzzers)
316
317 with open('BUILD.generated_tests.gni', 'w+') as out:
318 self.firstSection = True
319 out.write(self.header)
320
David Benjamin96628432017-01-19 19:05:47 -0500321 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400322 files['test_support'] +
323 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500324 self.PrintVariableSection(out, 'crypto_test_sources',
325 files['crypto_test'])
326 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400327
328
329class GYP(object):
330
331 def __init__(self):
332 self.header = \
333"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
334# Use of this source code is governed by a BSD-style license that can be
335# found in the LICENSE file.
336
337# This file is created by generate_build_files.py. Do not edit manually.
338
339"""
340
341 def PrintVariableSection(self, out, name, files):
342 out.write(' \'%s\': [\n' % name)
343 for f in sorted(files):
344 out.write(' \'%s\',\n' % f)
345 out.write(' ],\n')
346
347 def WriteFiles(self, files, asm_outputs):
348 with open('boringssl.gypi', 'w+') as gypi:
349 gypi.write(self.header + '{\n \'variables\': {\n')
350
David Benjaminc5aa8412016-07-29 17:41:58 -0400351 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
352 files['ssl'] + files['ssl_headers'] +
353 files['ssl_internal_headers'])
354 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
355 files['crypto'] + files['crypto_headers'] +
356 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400357
358 for ((osname, arch), asm_files) in asm_outputs:
359 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
360 (osname, arch), asm_files)
361
362 gypi.write(' }\n}\n')
363
David Benjamin38d01c62016-04-21 18:47:57 -0400364
Adam Langley9e1a6602015-05-05 17:47:53 -0700365def FindCMakeFiles(directory):
366 """Returns list of all CMakeLists.txt files recursively in directory."""
367 cmakefiles = []
368
369 for (path, _, filenames) in os.walk(directory):
370 for filename in filenames:
371 if filename == 'CMakeLists.txt':
372 cmakefiles.append(os.path.join(path, filename))
373
374 return cmakefiles
375
Adam Langleyfd499932017-04-04 14:21:43 -0700376def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700377 return is_dir or (path.startswith(
378 os.path.join('src', 'crypto', 'fipsmodule', '')) and
379 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700380
Adam Langleyfd499932017-04-04 14:21:43 -0700381def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700382 return (NoTests(path, dent, is_dir) and
383 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700384
385def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700386 """Filter function that can be passed to FindCFiles in order to remove test
387 sources."""
388 if is_dir:
389 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400390 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700391
392
Adam Langleyfd499932017-04-04 14:21:43 -0700393def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700394 """Filter function that can be passed to FindCFiles in order to remove
395 non-test sources."""
396 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400397 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400398 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700399
400
Adam Langleyfd499932017-04-04 14:21:43 -0700401def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400402 """Filter function that can be passed to FindCFiles in order to include all
403 sources."""
404 return True
405
406
Adam Langleyfd499932017-04-04 14:21:43 -0700407def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700408 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
409 order to exclude test runner files."""
410 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
411 # are in their own subpackage, from being included in boringssl/BUILD files.
412 return not is_dir or dent != 'runner'
413
414
David Benjamin3ecd0a52017-05-19 15:26:18 -0400415def NotGTestSupport(path, dent, is_dir):
416 return 'gtest' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500417
418
Adam Langleyfd499932017-04-04 14:21:43 -0700419def SSLHeaderFiles(path, dent, is_dir):
Adam Langley049ef412015-06-09 18:20:57 -0700420 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
421
422
Adam Langley9e1a6602015-05-05 17:47:53 -0700423def FindCFiles(directory, filter_func):
424 """Recurses through directory and returns a list of paths to all the C source
425 files that pass filter_func."""
426 cfiles = []
427
428 for (path, dirnames, filenames) in os.walk(directory):
429 for filename in filenames:
430 if not filename.endswith('.c') and not filename.endswith('.cc'):
431 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700432 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700433 continue
434 cfiles.append(os.path.join(path, filename))
435
436 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700437 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700438 del dirnames[i]
439
440 return cfiles
441
442
Adam Langley049ef412015-06-09 18:20:57 -0700443def FindHeaderFiles(directory, filter_func):
444 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
445 hfiles = []
446
447 for (path, dirnames, filenames) in os.walk(directory):
448 for filename in filenames:
449 if not filename.endswith('.h'):
450 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700451 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700452 continue
453 hfiles.append(os.path.join(path, filename))
454
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700455 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700456 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700457 del dirnames[i]
458
Adam Langley049ef412015-06-09 18:20:57 -0700459 return hfiles
460
461
Adam Langley9e1a6602015-05-05 17:47:53 -0700462def ExtractPerlAsmFromCMakeFile(cmakefile):
463 """Parses the contents of the CMakeLists.txt file passed as an argument and
464 returns a list of all the perlasm() directives found in the file."""
465 perlasms = []
466 with open(cmakefile) as f:
467 for line in f:
468 line = line.strip()
469 if not line.startswith('perlasm('):
470 continue
471 if not line.endswith(')'):
472 raise ValueError('Bad perlasm line in %s' % cmakefile)
473 # Remove "perlasm(" from start and ")" from end
474 params = line[8:-1].split()
475 if len(params) < 2:
476 raise ValueError('Bad perlasm line in %s' % cmakefile)
477 perlasms.append({
478 'extra_args': params[2:],
479 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
480 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
481 })
482
483 return perlasms
484
485
486def ReadPerlAsmOperations():
487 """Returns a list of all perlasm() directives found in CMake config files in
488 src/."""
489 perlasms = []
490 cmakefiles = FindCMakeFiles('src')
491
492 for cmakefile in cmakefiles:
493 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
494
495 return perlasms
496
497
498def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
499 """Runs the a perlasm script and puts the output into output_filename."""
500 base_dir = os.path.dirname(output_filename)
501 if not os.path.isdir(base_dir):
502 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400503 subprocess.check_call(
504 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700505
506
507def ArchForAsmFilename(filename):
508 """Returns the architectures that a given asm file should be compiled for
509 based on substrings in the filename."""
510
511 if 'x86_64' in filename or 'avx2' in filename:
512 return ['x86_64']
513 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
514 return ['x86']
515 elif 'armx' in filename:
516 return ['arm', 'aarch64']
517 elif 'armv8' in filename:
518 return ['aarch64']
519 elif 'arm' in filename:
520 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400521 elif 'ppc' in filename:
522 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700523 else:
524 raise ValueError('Unknown arch for asm filename: ' + filename)
525
526
527def WriteAsmFiles(perlasms):
528 """Generates asm files from perlasm directives for each supported OS x
529 platform combination."""
530 asmfiles = {}
531
532 for osarch in OS_ARCH_COMBOS:
533 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
534 key = (osname, arch)
535 outDir = '%s-%s' % key
536
537 for perlasm in perlasms:
538 filename = os.path.basename(perlasm['input'])
539 output = perlasm['output']
540 if not output.startswith('src'):
541 raise ValueError('output missing src: %s' % output)
542 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200543 if output.endswith('-armx.${ASM_EXT}'):
544 output = output.replace('-armx',
545 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700546 output = output.replace('${ASM_EXT}', asm_ext)
547
548 if arch in ArchForAsmFilename(filename):
549 PerlAsm(output, perlasm['input'], perlasm_style,
550 perlasm['extra_args'] + extra_args)
551 asmfiles.setdefault(key, []).append(output)
552
553 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
554 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
555
556 return asmfiles
557
558
David Benjamin3ecd0a52017-05-19 15:26:18 -0400559def ExtractVariablesFromCMakeFile(cmakefile):
560 """Parses the contents of the CMakeLists.txt file passed as an argument and
561 returns a dictionary of exported source lists."""
562 variables = {}
563 in_set_command = False
564 set_command = []
565 with open(cmakefile) as f:
566 for line in f:
567 if '#' in line:
568 line = line[:line.index('#')]
569 line = line.strip()
570
571 if not in_set_command:
572 if line.startswith('set('):
573 in_set_command = True
574 set_command = []
575 elif line == ')':
576 in_set_command = False
577 if not set_command:
578 raise ValueError('Empty set command')
579 variables[set_command[0]] = set_command[1:]
580 else:
581 set_command.extend([c for c in line.split(' ') if c])
582
583 if in_set_command:
584 raise ValueError('Unfinished set command')
585 return variables
586
587
Adam Langley049ef412015-06-09 18:20:57 -0700588def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400589 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Andres Erbsen5b280a82017-10-30 15:58:33 +0000590 crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
591 FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments))
Adam Langleyfd499932017-04-04 14:21:43 -0700592 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800593 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400594 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700595 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700596
David Benjamin0c9c1aa2017-12-12 15:19:20 -0500597 # third_party/fiat/p256.c lives in third_party/fiat, but it is a FIPS
598 # fragment, not a normal source file.
599 p256 = os.path.join('src', 'third_party', 'fiat', 'p256.c')
600 fips_fragments.append(p256)
601 crypto_c_files.remove(p256)
602
Adam Langley9e1a6602015-05-05 17:47:53 -0700603 # Generate err_data.c
604 with open('err_data.c', 'w+') as err_data:
605 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
606 cwd=os.path.join('src', 'crypto', 'err'),
607 stdout=err_data)
608 crypto_c_files.append('err_data.c')
609
David Benjamin38d01c62016-04-21 18:47:57 -0400610 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400611 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700612 test_support_h_files = (
613 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700614 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400615
David Benjamin3ecd0a52017-05-19 15:26:18 -0400616 # Generate crypto_test_data.cc
617 with open('crypto_test_data.cc', 'w+') as out:
618 subprocess.check_call(
619 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
620 cwd='src',
621 stdout=out)
622
David Benjamin96ee4a82017-07-09 23:46:47 -0400623 crypto_test_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
624 crypto_test_files += [
David Benjamin3ecd0a52017-05-19 15:26:18 -0400625 'crypto_test_data.cc',
626 'src/crypto/test/file_test_gtest.cc',
627 'src/crypto/test/gtest_main.cc',
628 ]
David Benjamin1d5a5702017-02-13 22:11:49 -0500629
630 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
631 ssl_test_files.append('src/crypto/test/gtest_main.cc')
Adam Langley9e1a6602015-05-05 17:47:53 -0700632
David Benjamin38d01c62016-04-21 18:47:57 -0400633 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
634
Adam Langley049ef412015-06-09 18:20:57 -0700635 ssl_h_files = (
636 FindHeaderFiles(
637 os.path.join('src', 'include', 'openssl'),
638 SSLHeaderFiles))
639
Adam Langleyfd499932017-04-04 14:21:43 -0700640 def NotSSLHeaderFiles(path, filename, is_dir):
641 return not SSLHeaderFiles(path, filename, is_dir)
Adam Langley049ef412015-06-09 18:20:57 -0700642 crypto_h_files = (
643 FindHeaderFiles(
644 os.path.join('src', 'include', 'openssl'),
645 NotSSLHeaderFiles))
646
647 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
Andres Erbsen5b280a82017-10-30 15:58:33 +0000648 crypto_internal_h_files = (
649 FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
650 FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests))
Adam Langley049ef412015-06-09 18:20:57 -0700651
Adam Langley9e1a6602015-05-05 17:47:53 -0700652 files = {
653 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700654 'crypto_headers': crypto_h_files,
655 'crypto_internal_headers': crypto_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500656 'crypto_test': sorted(crypto_test_files),
Adam Langleyfd499932017-04-04 14:21:43 -0700657 'fips_fragments': fips_fragments,
David Benjamin38d01c62016-04-21 18:47:57 -0400658 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800659 'ssl': ssl_source_files,
Adam Langley049ef412015-06-09 18:20:57 -0700660 'ssl_headers': ssl_h_files,
661 'ssl_internal_headers': ssl_internal_h_files,
David Benjamin1d5a5702017-02-13 22:11:49 -0500662 'ssl_test': sorted(ssl_test_files),
David Benjamin38d01c62016-04-21 18:47:57 -0400663 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700664 'tool_headers': tool_h_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400665 'test_support': test_support_c_files,
666 'test_support_headers': test_support_h_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700667 }
668
669 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
670
Adam Langley049ef412015-06-09 18:20:57 -0700671 for platform in platforms:
672 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700673
674 return 0
675
676
Adam Langley9e1a6602015-05-05 17:47:53 -0700677if __name__ == '__main__':
Matt Braithwaite16695892016-06-09 09:34:11 -0700678 parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
Robert Sloane091af42017-10-09 12:47:17 -0700679 ' [android|bazel|eureka|gn|gyp]')
Matt Braithwaite16695892016-06-09 09:34:11 -0700680 parser.add_option('--prefix', dest='prefix',
681 help='For Bazel, prepend argument to all source files')
682 options, args = parser.parse_args(sys.argv[1:])
683 PREFIX = options.prefix
684
685 if not args:
686 parser.print_help()
687 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700688
Adam Langley049ef412015-06-09 18:20:57 -0700689 platforms = []
Matt Braithwaite16695892016-06-09 09:34:11 -0700690 for s in args:
David Benjamin38d01c62016-04-21 18:47:57 -0400691 if s == 'android':
Adam Langley049ef412015-06-09 18:20:57 -0700692 platforms.append(Android())
Adam Langley049ef412015-06-09 18:20:57 -0700693 elif s == 'bazel':
694 platforms.append(Bazel())
Robert Sloane091af42017-10-09 12:47:17 -0700695 elif s == 'eureka':
696 platforms.append(Eureka())
David Benjamin38d01c62016-04-21 18:47:57 -0400697 elif s == 'gn':
698 platforms.append(GN())
699 elif s == 'gyp':
700 platforms.append(GYP())
Adam Langley049ef412015-06-09 18:20:57 -0700701 else:
Matt Braithwaite16695892016-06-09 09:34:11 -0700702 parser.print_help()
703 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700704
Adam Langley049ef412015-06-09 18:20:57 -0700705 sys.exit(main(platforms))