blob: a0d0d49036e44172f23581e79ceb722e958ef596 [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
109 blueprint.write(' android_%s: {\n' % arch)
110 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
116 if arch == 'x86' or arch == 'x86_64':
117 blueprint.write(' linux_%s: {\n' % arch)
118 blueprint.write(' srcs: [\n')
119 for f in sorted(asm_files):
120 blueprint.write(' "%s",\n' % f)
121 blueprint.write(' ],\n')
122 blueprint.write(' },\n')
123
124 blueprint.write(' },\n')
125 blueprint.write('}\n\n')
126
127 blueprint.write('cc_defaults {\n')
128 blueprint.write(' name: "libssl_sources",\n')
129 blueprint.write(' srcs: [\n')
130 for f in sorted(files['ssl']):
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: "bssl_sources",\n')
137 blueprint.write(' srcs: [\n')
138 for f in sorted(files['tool']):
139 blueprint.write(' "%s",\n' % f)
140 blueprint.write(' ],\n')
141 blueprint.write('}\n\n')
142
143 blueprint.write('cc_defaults {\n')
144 blueprint.write(' name: "boringssl_test_support_sources",\n')
145 blueprint.write(' srcs: [\n')
146 for f in sorted(files['test_support']):
147 blueprint.write(' "%s",\n' % f)
148 blueprint.write(' ],\n')
149 blueprint.write('}\n\n')
150
151 blueprint.write('cc_defaults {\n')
David Benjamin96628432017-01-19 19:05:47 -0500152 blueprint.write(' name: "boringssl_crypto_test_sources",\n')
153 blueprint.write(' srcs: [\n')
154 for f in sorted(files['crypto_test']):
155 blueprint.write(' "%s",\n' % f)
156 blueprint.write(' ],\n')
157 blueprint.write('}\n\n')
158
159 blueprint.write('cc_defaults {\n')
160 blueprint.write(' name: "boringssl_ssl_test_sources",\n')
161 blueprint.write(' srcs: [\n')
162 for f in sorted(files['ssl_test']):
163 blueprint.write(' "%s",\n' % f)
164 blueprint.write(' ],\n')
165 blueprint.write('}\n\n')
166
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700167 # Legacy Android.mk format, only used by Trusty in new branches
Adam Langley9e1a6602015-05-05 17:47:53 -0700168 with open('sources.mk', 'w+') as makefile:
169 makefile.write(self.header)
170
David Benjamin8c29e7d2016-09-30 21:34:31 -0400171 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
Adam Langley9e1a6602015-05-05 17:47:53 -0700172
173 for ((osname, arch), asm_files) in asm_outputs:
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700174 if osname != 'linux':
175 continue
Adam Langley9e1a6602015-05-05 17:47:53 -0700176 self.PrintVariableSection(
177 makefile, '%s_%s_sources' % (osname, arch), asm_files)
178
179
Adam Langley049ef412015-06-09 18:20:57 -0700180class Bazel(object):
181 """Bazel outputs files suitable for including in Bazel files."""
182
183 def __init__(self):
184 self.firstSection = True
185 self.header = \
186"""# This file is created by generate_build_files.py. Do not edit manually.
187
188"""
189
190 def PrintVariableSection(self, out, name, files):
191 if not self.firstSection:
192 out.write('\n')
193 self.firstSection = False
194
195 out.write('%s = [\n' % name)
196 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700197 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700198 out.write(']\n')
199
200 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700201 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700202 out.write(self.header)
203
204 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700205 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700206 self.PrintVariableSection(
207 out, 'ssl_internal_headers', files['ssl_internal_headers'])
208 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
209 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
210 self.PrintVariableSection(
211 out, 'crypto_internal_headers', files['crypto_internal_headers'])
212 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
213 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700214 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700215
216 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700217 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700218 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700219
Chuck Haysc608d6b2015-10-06 17:54:16 -0700220 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700221 out.write(self.header)
222
223 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400224 for filename in sorted(files['test_support'] +
225 files['test_support_headers'] +
226 files['crypto_internal_headers'] +
227 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700228 if os.path.basename(filename) == 'malloc.cc':
229 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700230 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700231
Adam Langley7b6acc52017-07-27 16:33:27 -0700232 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700233
David Benjamin96628432017-01-19 19:05:47 -0500234 self.PrintVariableSection(out, 'crypto_test_sources',
235 files['crypto_test'])
236 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
237
Adam Langley049ef412015-06-09 18:20:57 -0700238
Robert Sloane091af42017-10-09 12:47:17 -0700239class Eureka(object):
240
241 def __init__(self):
242 self.header = \
243"""# Copyright (C) 2017 The Android Open Source Project
244#
245# Licensed under the Apache License, Version 2.0 (the "License");
246# you may not use this file except in compliance with the License.
247# You may obtain a copy of the License at
248#
249# http://www.apache.org/licenses/LICENSE-2.0
250#
251# Unless required by applicable law or agreed to in writing, software
252# distributed under the License is distributed on an "AS IS" BASIS,
253# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
254# See the License for the specific language governing permissions and
255# limitations under the License.
256
257# This file is created by generate_build_files.py. Do not edit manually.
258
259"""
260
261 def PrintVariableSection(self, out, name, files):
262 out.write('%s := \\\n' % name)
263 for f in sorted(files):
264 out.write(' %s\\\n' % f)
265 out.write('\n')
266
267 def WriteFiles(self, files, asm_outputs):
268 # Legacy Android.mk format
269 with open('eureka.mk', 'w+') as makefile:
270 makefile.write(self.header)
271
272 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
273 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
274 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
275
276 for ((osname, arch), asm_files) in asm_outputs:
277 if osname != 'linux':
278 continue
279 self.PrintVariableSection(
280 makefile, '%s_%s_sources' % (osname, arch), asm_files)
281
282
David Benjamin38d01c62016-04-21 18:47:57 -0400283class GN(object):
284
285 def __init__(self):
286 self.firstSection = True
287 self.header = \
288"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
289# Use of this source code is governed by a BSD-style license that can be
290# found in the LICENSE file.
291
292# This file is created by generate_build_files.py. Do not edit manually.
293
294"""
295
296 def PrintVariableSection(self, out, name, files):
297 if not self.firstSection:
298 out.write('\n')
299 self.firstSection = False
300
301 out.write('%s = [\n' % name)
302 for f in sorted(files):
303 out.write(' "%s",\n' % f)
304 out.write(']\n')
305
306 def WriteFiles(self, files, asm_outputs):
307 with open('BUILD.generated.gni', 'w+') as out:
308 out.write(self.header)
309
David Benjaminc5aa8412016-07-29 17:41:58 -0400310 self.PrintVariableSection(out, 'crypto_sources',
311 files['crypto'] + files['crypto_headers'] +
312 files['crypto_internal_headers'])
313 self.PrintVariableSection(out, 'ssl_sources',
314 files['ssl'] + files['ssl_headers'] +
315 files['ssl_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400316
317 for ((osname, arch), asm_files) in asm_outputs:
318 self.PrintVariableSection(
319 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
320
321 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
322 for fuzzer in files['fuzz']]
323 self.PrintVariableSection(out, 'fuzzers', fuzzers)
324
325 with open('BUILD.generated_tests.gni', 'w+') as out:
326 self.firstSection = True
327 out.write(self.header)
328
David Benjamin96628432017-01-19 19:05:47 -0500329 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400330 files['test_support'] +
331 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500332 self.PrintVariableSection(out, 'crypto_test_sources',
333 files['crypto_test'])
334 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400335
336
337class GYP(object):
338
339 def __init__(self):
340 self.header = \
341"""# Copyright (c) 2016 The Chromium Authors. All rights reserved.
342# Use of this source code is governed by a BSD-style license that can be
343# found in the LICENSE file.
344
345# This file is created by generate_build_files.py. Do not edit manually.
346
347"""
348
349 def PrintVariableSection(self, out, name, files):
350 out.write(' \'%s\': [\n' % name)
351 for f in sorted(files):
352 out.write(' \'%s\',\n' % f)
353 out.write(' ],\n')
354
355 def WriteFiles(self, files, asm_outputs):
356 with open('boringssl.gypi', 'w+') as gypi:
357 gypi.write(self.header + '{\n \'variables\': {\n')
358
David Benjaminc5aa8412016-07-29 17:41:58 -0400359 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
360 files['ssl'] + files['ssl_headers'] +
361 files['ssl_internal_headers'])
362 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
363 files['crypto'] + files['crypto_headers'] +
364 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400365
366 for ((osname, arch), asm_files) in asm_outputs:
367 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
368 (osname, arch), asm_files)
369
370 gypi.write(' }\n}\n')
371
David Benjamin38d01c62016-04-21 18:47:57 -0400372
Adam Langley9e1a6602015-05-05 17:47:53 -0700373def FindCMakeFiles(directory):
374 """Returns list of all CMakeLists.txt files recursively in directory."""
375 cmakefiles = []
376
377 for (path, _, filenames) in os.walk(directory):
378 for filename in filenames:
379 if filename == 'CMakeLists.txt':
380 cmakefiles.append(os.path.join(path, filename))
381
382 return cmakefiles
383
Adam Langleyfd499932017-04-04 14:21:43 -0700384def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700385 return is_dir or (path.startswith(
386 os.path.join('src', 'crypto', 'fipsmodule', '')) and
387 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700388
Adam Langleyfd499932017-04-04 14:21:43 -0700389def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700390 return (NoTests(path, dent, is_dir) and
391 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700392
393def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700394 """Filter function that can be passed to FindCFiles in order to remove test
395 sources."""
396 if is_dir:
397 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400398 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700399
400
Adam Langleyfd499932017-04-04 14:21:43 -0700401def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700402 """Filter function that can be passed to FindCFiles in order to remove
403 non-test sources."""
404 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400405 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400406 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700407
408
Adam Langleyfd499932017-04-04 14:21:43 -0700409def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400410 """Filter function that can be passed to FindCFiles in order to include all
411 sources."""
412 return True
413
414
Adam Langleyfd499932017-04-04 14:21:43 -0700415def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700416 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
417 order to exclude test runner files."""
418 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
419 # are in their own subpackage, from being included in boringssl/BUILD files.
420 return not is_dir or dent != 'runner'
421
422
David Benjamin3ecd0a52017-05-19 15:26:18 -0400423def NotGTestSupport(path, dent, is_dir):
424 return 'gtest' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500425
426
Adam Langleyfd499932017-04-04 14:21:43 -0700427def SSLHeaderFiles(path, dent, is_dir):
Adam Langley049ef412015-06-09 18:20:57 -0700428 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
429
430
Adam Langley9e1a6602015-05-05 17:47:53 -0700431def FindCFiles(directory, filter_func):
432 """Recurses through directory and returns a list of paths to all the C source
433 files that pass filter_func."""
434 cfiles = []
435
436 for (path, dirnames, filenames) in os.walk(directory):
437 for filename in filenames:
438 if not filename.endswith('.c') and not filename.endswith('.cc'):
439 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700440 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700441 continue
442 cfiles.append(os.path.join(path, filename))
443
444 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700445 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700446 del dirnames[i]
447
448 return cfiles
449
450
Adam Langley049ef412015-06-09 18:20:57 -0700451def FindHeaderFiles(directory, filter_func):
452 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
453 hfiles = []
454
455 for (path, dirnames, filenames) in os.walk(directory):
456 for filename in filenames:
457 if not filename.endswith('.h'):
458 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700459 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700460 continue
461 hfiles.append(os.path.join(path, filename))
462
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700463 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700464 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700465 del dirnames[i]
466
Adam Langley049ef412015-06-09 18:20:57 -0700467 return hfiles
468
469
Adam Langley9e1a6602015-05-05 17:47:53 -0700470def ExtractPerlAsmFromCMakeFile(cmakefile):
471 """Parses the contents of the CMakeLists.txt file passed as an argument and
472 returns a list of all the perlasm() directives found in the file."""
473 perlasms = []
474 with open(cmakefile) as f:
475 for line in f:
476 line = line.strip()
477 if not line.startswith('perlasm('):
478 continue
479 if not line.endswith(')'):
480 raise ValueError('Bad perlasm line in %s' % cmakefile)
481 # Remove "perlasm(" from start and ")" from end
482 params = line[8:-1].split()
483 if len(params) < 2:
484 raise ValueError('Bad perlasm line in %s' % cmakefile)
485 perlasms.append({
486 'extra_args': params[2:],
487 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
488 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
489 })
490
491 return perlasms
492
493
494def ReadPerlAsmOperations():
495 """Returns a list of all perlasm() directives found in CMake config files in
496 src/."""
497 perlasms = []
498 cmakefiles = FindCMakeFiles('src')
499
500 for cmakefile in cmakefiles:
501 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
502
503 return perlasms
504
505
506def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
507 """Runs the a perlasm script and puts the output into output_filename."""
508 base_dir = os.path.dirname(output_filename)
509 if not os.path.isdir(base_dir):
510 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400511 subprocess.check_call(
512 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700513
514
515def ArchForAsmFilename(filename):
516 """Returns the architectures that a given asm file should be compiled for
517 based on substrings in the filename."""
518
519 if 'x86_64' in filename or 'avx2' in filename:
520 return ['x86_64']
521 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
522 return ['x86']
523 elif 'armx' in filename:
524 return ['arm', 'aarch64']
525 elif 'armv8' in filename:
526 return ['aarch64']
527 elif 'arm' in filename:
528 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400529 elif 'ppc' in filename:
530 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700531 else:
532 raise ValueError('Unknown arch for asm filename: ' + filename)
533
534
535def WriteAsmFiles(perlasms):
536 """Generates asm files from perlasm directives for each supported OS x
537 platform combination."""
538 asmfiles = {}
539
540 for osarch in OS_ARCH_COMBOS:
541 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
542 key = (osname, arch)
543 outDir = '%s-%s' % key
544
545 for perlasm in perlasms:
546 filename = os.path.basename(perlasm['input'])
547 output = perlasm['output']
548 if not output.startswith('src'):
549 raise ValueError('output missing src: %s' % output)
550 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200551 if output.endswith('-armx.${ASM_EXT}'):
552 output = output.replace('-armx',
553 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700554 output = output.replace('${ASM_EXT}', asm_ext)
555
556 if arch in ArchForAsmFilename(filename):
557 PerlAsm(output, perlasm['input'], perlasm_style,
558 perlasm['extra_args'] + extra_args)
559 asmfiles.setdefault(key, []).append(output)
560
561 for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
562 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
563
564 return asmfiles
565
566
David Benjamin3ecd0a52017-05-19 15:26:18 -0400567def ExtractVariablesFromCMakeFile(cmakefile):
568 """Parses the contents of the CMakeLists.txt file passed as an argument and
569 returns a dictionary of exported source lists."""
570 variables = {}
571 in_set_command = False
572 set_command = []
573 with open(cmakefile) as f:
574 for line in f:
575 if '#' in line:
576 line = line[:line.index('#')]
577 line = line.strip()
578
579 if not in_set_command:
580 if line.startswith('set('):
581 in_set_command = True
582 set_command = []
583 elif line == ')':
584 in_set_command = False
585 if not set_command:
586 raise ValueError('Empty set command')
587 variables[set_command[0]] = set_command[1:]
588 else:
589 set_command.extend([c for c in line.split(' ') if c])
590
591 if in_set_command:
592 raise ValueError('Unfinished set command')
593 return variables
594
595
Adam Langley049ef412015-06-09 18:20:57 -0700596def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400597 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Adam Langleyfd499932017-04-04 14:21:43 -0700598 crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments)
599 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800600 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400601 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700602 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700603
604 # Generate err_data.c
605 with open('err_data.c', 'w+') as err_data:
606 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
607 cwd=os.path.join('src', 'crypto', 'err'),
608 stdout=err_data)
609 crypto_c_files.append('err_data.c')
610
David Benjamin38d01c62016-04-21 18:47:57 -0400611 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400612 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700613 test_support_h_files = (
614 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700615 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400616
David Benjamin3ecd0a52017-05-19 15:26:18 -0400617 # Generate crypto_test_data.cc
618 with open('crypto_test_data.cc', 'w+') as out:
619 subprocess.check_call(
620 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
621 cwd='src',
622 stdout=out)
623
David Benjamin96ee4a82017-07-09 23:46:47 -0400624 crypto_test_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
625 crypto_test_files += [
David Benjamin3ecd0a52017-05-19 15:26:18 -0400626 'crypto_test_data.cc',
627 'src/crypto/test/file_test_gtest.cc',
628 'src/crypto/test/gtest_main.cc',
629 ]
David Benjamin1d5a5702017-02-13 22:11:49 -0500630
631 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
632 ssl_test_files.append('src/crypto/test/gtest_main.cc')
Adam Langley9e1a6602015-05-05 17:47:53 -0700633
David Benjamin38d01c62016-04-21 18:47:57 -0400634 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
635
Adam Langley049ef412015-06-09 18:20:57 -0700636 ssl_h_files = (
637 FindHeaderFiles(
638 os.path.join('src', 'include', 'openssl'),
639 SSLHeaderFiles))
640
Adam Langleyfd499932017-04-04 14:21:43 -0700641 def NotSSLHeaderFiles(path, filename, is_dir):
642 return not SSLHeaderFiles(path, filename, is_dir)
Adam Langley049ef412015-06-09 18:20:57 -0700643 crypto_h_files = (
644 FindHeaderFiles(
645 os.path.join('src', 'include', 'openssl'),
646 NotSSLHeaderFiles))
647
648 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
649 crypto_internal_h_files = FindHeaderFiles(
650 os.path.join('src', 'crypto'), NoTests)
651
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))