blob: f778a28b19e6959036a08579c8a0eff41d1cd01e [file] [log] [blame]
Adam Langleycfd80a92019-11-08 14:40:08 -08001# coding=utf8
2
Adam Langley9e1a6602015-05-05 17:47:53 -07003# Copyright (c) 2015, Google Inc.
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
Matt Braithwaite16695892016-06-09 09:34:11 -070017"""Enumerates source files for consumption by various build systems."""
Adam Langley9e1a6602015-05-05 17:47:53 -070018
Matt Braithwaite16695892016-06-09 09:34:11 -070019import optparse
Adam Langley9e1a6602015-05-05 17:47:53 -070020import os
21import subprocess
22import sys
Adam Langley9c164b22015-06-10 18:54:47 -070023import json
Adam Langley9e1a6602015-05-05 17:47:53 -070024
25
26# OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for
27# that platform and the extension used by asm files.
28OS_ARCH_COMBOS = [
David Benjamin351b2f82022-02-06 12:57:17 -050029 ('apple', 'arm', 'ios32', [], 'S'),
30 ('apple', 'aarch64', 'ios64', [], 'S'),
31 ('apple', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
32 ('apple', 'x86_64', 'macosx', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070033 ('linux', 'arm', 'linux32', [], 'S'),
34 ('linux', 'aarch64', 'linux64', [], 'S'),
Adam Langley7c075b92017-05-22 15:31:13 -070035 ('linux', 'ppc64le', 'linux64le', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070036 ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
37 ('linux', 'x86_64', 'elf', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070038 ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
39 ('win', 'x86_64', 'nasm', [], 'asm'),
Anthony Robertsafd5dba2020-10-19 12:27:51 +010040 ('win', 'aarch64', 'win64', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070041]
42
43# NON_PERL_FILES enumerates assembly files that are not processed by the
44# perlasm system.
45NON_PERL_FILES = {
46 ('linux', 'arm'): [
Adam Langley7b8b9c12016-01-04 07:13:00 -080047 'src/crypto/curve25519/asm/x25519-asm-arm.S',
David Benjamin3c4a5cb2016-03-29 17:43:31 -040048 'src/crypto/poly1305/poly1305_arm_asm.S',
Adam Langley7b935932018-11-12 13:53:42 -080049 ],
50 ('linux', 'x86_64'): [
51 'src/crypto/hrss/asm/poly_rq_mul.S',
Adam Langley9e1a6602015-05-05 17:47:53 -070052 ],
53}
54
Matt Braithwaite16695892016-06-09 09:34:11 -070055PREFIX = None
Adam Langley990a3232018-05-22 10:02:59 -070056EMBED_TEST_DATA = True
Matt Braithwaite16695892016-06-09 09:34:11 -070057
58
59def PathOf(x):
60 return x if not PREFIX else os.path.join(PREFIX, x)
61
Adam Langley9e1a6602015-05-05 17:47:53 -070062
David Benjamin70690f72023-01-25 09:56:43 -050063LICENSE_TEMPLATE = """Copyright (c) 2015, Google Inc.
64
65Permission to use, copy, modify, and/or distribute this software for any
66purpose with or without fee is hereby granted, provided that the above
67copyright notice and this permission notice appear in all copies.
68
69THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
70WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
71MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
72SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
73WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
74OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
75CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.""".split("\n")
76
77def LicenseHeader(comment):
78 lines = []
79 for line in LICENSE_TEMPLATE:
80 if not line:
81 lines.append(comment)
82 else:
83 lines.append("%s %s" % (comment, line))
84 lines.append("")
85 return "\n".join(lines)
86
87
Adam Langley9e1a6602015-05-05 17:47:53 -070088class Android(object):
89
90 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -050091 self.header = LicenseHeader("#") + """
Dan Willemsenb57e4fc2016-07-21 11:08:44 -070092# This file is created by generate_build_files.py. Do not edit manually.
Adam Langley9e1a6602015-05-05 17:47:53 -070093"""
94
95 def PrintVariableSection(self, out, name, files):
96 out.write('%s := \\\n' % name)
97 for f in sorted(files):
98 out.write(' %s\\\n' % f)
99 out.write('\n')
100
101 def WriteFiles(self, files, asm_outputs):
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700102 # New Android.bp format
103 with open('sources.bp', 'w+') as blueprint:
104 blueprint.write(self.header.replace('#', '//'))
105
Pete Bentley44544d92019-08-15 15:01:26 +0100106 # Separate out BCM files to allow different compilation rules (specific to Android FIPS)
107 bcm_c_files = files['bcm_crypto']
108 non_bcm_c_files = [file for file in files['crypto'] if file not in bcm_c_files]
109 non_bcm_asm = self.FilterBcmAsm(asm_outputs, False)
110 bcm_asm = self.FilterBcmAsm(asm_outputs, True)
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700111
Pete Bentley44544d92019-08-15 15:01:26 +0100112 self.PrintDefaults(blueprint, 'libcrypto_sources', non_bcm_c_files, non_bcm_asm)
113 self.PrintDefaults(blueprint, 'libcrypto_bcm_sources', bcm_c_files, bcm_asm)
114 self.PrintDefaults(blueprint, 'libssl_sources', files['ssl'])
115 self.PrintDefaults(blueprint, 'bssl_sources', files['tool'])
116 self.PrintDefaults(blueprint, 'boringssl_test_support_sources', files['test_support'])
117 self.PrintDefaults(blueprint, 'boringssl_crypto_test_sources', files['crypto_test'])
118 self.PrintDefaults(blueprint, 'boringssl_ssl_test_sources', files['ssl_test'])
119
120 # Legacy Android.mk format, only used by Trusty in new branches
121 with open('sources.mk', 'w+') as makefile:
122 makefile.write(self.header)
123 makefile.write('\n')
124 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
125
126 for ((osname, arch), asm_files) in asm_outputs:
127 if osname != 'linux':
128 continue
129 self.PrintVariableSection(
130 makefile, '%s_%s_sources' % (osname, arch), asm_files)
131
132 def PrintDefaults(self, blueprint, name, files, asm_outputs={}):
133 """Print a cc_defaults section from a list of C files and optionally assembly outputs"""
134 blueprint.write('\n')
135 blueprint.write('cc_defaults {\n')
136 blueprint.write(' name: "%s",\n' % name)
137 blueprint.write(' srcs: [\n')
138 for f in sorted(files):
139 blueprint.write(' "%s",\n' % f)
140 blueprint.write(' ],\n')
141
142 if asm_outputs:
143 blueprint.write(' target: {\n')
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700144 for ((osname, arch), asm_files) in asm_outputs:
Steven Valdez93d242b2016-10-06 13:49:01 -0400145 if osname != 'linux' or arch == 'ppc64le':
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700146 continue
147 if arch == 'aarch64':
148 arch = 'arm64'
149
Dan Willemsen2eb4bc52017-10-16 14:37:00 -0700150 blueprint.write(' linux_%s: {\n' % arch)
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700151 blueprint.write(' srcs: [\n')
152 for f in sorted(asm_files):
153 blueprint.write(' "%s",\n' % f)
154 blueprint.write(' ],\n')
155 blueprint.write(' },\n')
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700156 blueprint.write(' },\n')
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700157
Pete Bentley44544d92019-08-15 15:01:26 +0100158 blueprint.write('}\n')
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700159
Pete Bentley44544d92019-08-15 15:01:26 +0100160 def FilterBcmAsm(self, asm, want_bcm):
161 """Filter a list of assembly outputs based on whether they belong in BCM
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700162
Pete Bentley44544d92019-08-15 15:01:26 +0100163 Args:
164 asm: Assembly file lists to filter
165 want_bcm: If true then include BCM files, otherwise do not
Dan Willemsenb57e4fc2016-07-21 11:08:44 -0700166
Pete Bentley44544d92019-08-15 15:01:26 +0100167 Returns:
168 A copy of |asm| with files filtered according to |want_bcm|
169 """
170 return [(archinfo, filter(lambda p: ("/crypto/fipsmodule/" in p) == want_bcm, files))
171 for (archinfo, files) in asm]
Adam Langley9e1a6602015-05-05 17:47:53 -0700172
173
David Benjamineca48e52019-08-13 11:51:53 -0400174class AndroidCMake(object):
175
176 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500177 self.header = LicenseHeader("#") + """
David Benjamineca48e52019-08-13 11:51:53 -0400178# This file is created by generate_build_files.py. Do not edit manually.
179# To specify a custom path prefix, set BORINGSSL_ROOT before including this
180# file, or use list(TRANSFORM ... PREPEND) from CMake 3.12.
181
182"""
183
184 def PrintVariableSection(self, out, name, files):
185 out.write('set(%s\n' % name)
186 for f in sorted(files):
187 # Ideally adding the prefix would be the caller's job, but
188 # list(TRANSFORM ... PREPEND) is only available starting CMake 3.12. When
189 # sources.cmake is the source of truth, we can ask Android to either write
190 # a CMake function or update to 3.12.
191 out.write(' ${BORINGSSL_ROOT}%s\n' % f)
192 out.write(')\n')
193
194 def WriteFiles(self, files, asm_outputs):
195 # The Android emulator uses a custom CMake buildsystem.
196 #
197 # TODO(davidben): Move our various source lists into sources.cmake and have
198 # Android consume that directly.
199 with open('android-sources.cmake', 'w+') as out:
200 out.write(self.header)
201
202 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
203 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
204 self.PrintVariableSection(out, 'tool_sources', files['tool'])
205 self.PrintVariableSection(out, 'test_support_sources',
206 files['test_support'])
207 self.PrintVariableSection(out, 'crypto_test_sources',
208 files['crypto_test'])
209 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
210
211 for ((osname, arch), asm_files) in asm_outputs:
212 self.PrintVariableSection(
213 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
214
215
Adam Langley049ef412015-06-09 18:20:57 -0700216class Bazel(object):
217 """Bazel outputs files suitable for including in Bazel files."""
218
219 def __init__(self):
220 self.firstSection = True
221 self.header = \
222"""# This file is created by generate_build_files.py. Do not edit manually.
223
224"""
225
226 def PrintVariableSection(self, out, name, files):
227 if not self.firstSection:
228 out.write('\n')
229 self.firstSection = False
230
231 out.write('%s = [\n' % name)
232 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700233 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700234 out.write(']\n')
235
236 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700237 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700238 out.write(self.header)
239
240 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700241 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700242 self.PrintVariableSection(
243 out, 'ssl_internal_headers', files['ssl_internal_headers'])
244 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
245 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
246 self.PrintVariableSection(
247 out, 'crypto_internal_headers', files['crypto_internal_headers'])
248 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
249 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700250 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700251
252 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700253 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700254 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700255
Chuck Haysc608d6b2015-10-06 17:54:16 -0700256 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700257 out.write(self.header)
258
259 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400260 for filename in sorted(files['test_support'] +
261 files['test_support_headers'] +
262 files['crypto_internal_headers'] +
263 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700264 if os.path.basename(filename) == 'malloc.cc':
265 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700266 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700267
Adam Langley7b6acc52017-07-27 16:33:27 -0700268 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700269
David Benjamin96628432017-01-19 19:05:47 -0500270 self.PrintVariableSection(out, 'crypto_test_sources',
271 files['crypto_test'])
272 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
Adam Langley990a3232018-05-22 10:02:59 -0700273 self.PrintVariableSection(out, 'crypto_test_data',
274 files['crypto_test_data'])
Adam Langley3e502c82019-10-16 09:56:38 -0700275 self.PrintVariableSection(out, 'urandom_test_sources',
276 files['urandom_test'])
David Benjamin96628432017-01-19 19:05:47 -0500277
Adam Langley049ef412015-06-09 18:20:57 -0700278
Robert Sloane091af42017-10-09 12:47:17 -0700279class Eureka(object):
280
281 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500282 self.header = LicenseHeader("#") + """
Robert Sloane091af42017-10-09 12:47:17 -0700283# This file is created by generate_build_files.py. Do not edit manually.
284
285"""
286
287 def PrintVariableSection(self, out, name, files):
288 out.write('%s := \\\n' % name)
289 for f in sorted(files):
290 out.write(' %s\\\n' % f)
291 out.write('\n')
292
293 def WriteFiles(self, files, asm_outputs):
294 # Legacy Android.mk format
295 with open('eureka.mk', 'w+') as makefile:
296 makefile.write(self.header)
297
298 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
299 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
300 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
301
302 for ((osname, arch), asm_files) in asm_outputs:
303 if osname != 'linux':
304 continue
305 self.PrintVariableSection(
306 makefile, '%s_%s_sources' % (osname, arch), asm_files)
307
308
David Benjamin38d01c62016-04-21 18:47:57 -0400309class GN(object):
310
311 def __init__(self):
312 self.firstSection = True
David Benjamin70690f72023-01-25 09:56:43 -0500313 self.header = LicenseHeader("#") + """
David Benjamin38d01c62016-04-21 18:47:57 -0400314# This file is created by generate_build_files.py. Do not edit manually.
315
316"""
317
318 def PrintVariableSection(self, out, name, files):
319 if not self.firstSection:
320 out.write('\n')
321 self.firstSection = False
322
323 out.write('%s = [\n' % name)
324 for f in sorted(files):
325 out.write(' "%s",\n' % f)
326 out.write(']\n')
327
328 def WriteFiles(self, files, asm_outputs):
329 with open('BUILD.generated.gni', 'w+') as out:
330 out.write(self.header)
331
David Benjaminc5aa8412016-07-29 17:41:58 -0400332 self.PrintVariableSection(out, 'crypto_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700333 files['crypto'] +
David Benjaminc5aa8412016-07-29 17:41:58 -0400334 files['crypto_internal_headers'])
James Robinson98dd68f2018-04-11 14:47:34 -0700335 self.PrintVariableSection(out, 'crypto_headers',
336 files['crypto_headers'])
David Benjaminc5aa8412016-07-29 17:41:58 -0400337 self.PrintVariableSection(out, 'ssl_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700338 files['ssl'] + files['ssl_internal_headers'])
339 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
David Benjaminbb0cb952020-12-17 16:35:39 -0500340 self.PrintVariableSection(out, 'tool_sources',
341 files['tool'] + files['tool_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400342
343 for ((osname, arch), asm_files) in asm_outputs:
344 self.PrintVariableSection(
345 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
346
347 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
348 for fuzzer in files['fuzz']]
349 self.PrintVariableSection(out, 'fuzzers', fuzzers)
350
351 with open('BUILD.generated_tests.gni', 'w+') as out:
352 self.firstSection = True
353 out.write(self.header)
354
David Benjamin96628432017-01-19 19:05:47 -0500355 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400356 files['test_support'] +
357 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500358 self.PrintVariableSection(out, 'crypto_test_sources',
359 files['crypto_test'])
David Benjaminf014d602019-05-07 18:58:06 -0500360 self.PrintVariableSection(out, 'crypto_test_data',
361 files['crypto_test_data'])
David Benjamin96628432017-01-19 19:05:47 -0500362 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400363
364
365class GYP(object):
366
367 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500368 self.header = LicenseHeader("#") + """
David Benjamin38d01c62016-04-21 18:47:57 -0400369# This file is created by generate_build_files.py. Do not edit manually.
370
371"""
372
373 def PrintVariableSection(self, out, name, files):
374 out.write(' \'%s\': [\n' % name)
375 for f in sorted(files):
376 out.write(' \'%s\',\n' % f)
377 out.write(' ],\n')
378
379 def WriteFiles(self, files, asm_outputs):
380 with open('boringssl.gypi', 'w+') as gypi:
381 gypi.write(self.header + '{\n \'variables\': {\n')
382
David Benjaminc5aa8412016-07-29 17:41:58 -0400383 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
384 files['ssl'] + files['ssl_headers'] +
385 files['ssl_internal_headers'])
386 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
387 files['crypto'] + files['crypto_headers'] +
388 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400389
390 for ((osname, arch), asm_files) in asm_outputs:
391 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
392 (osname, arch), asm_files)
393
394 gypi.write(' }\n}\n')
395
Adam Langleycfd80a92019-11-08 14:40:08 -0800396class CMake(object):
397
398 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500399 self.header = LicenseHeader("#") + R'''
Adam Langleycfd80a92019-11-08 14:40:08 -0800400# This file is created by generate_build_files.py. Do not edit manually.
401
David Benjamind0b66c72021-03-22 17:30:02 -0400402cmake_minimum_required(VERSION 3.5)
Adam Langleycfd80a92019-11-08 14:40:08 -0800403
404project(BoringSSL LANGUAGES C CXX)
405
406if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
407 set(CLANG 1)
408endif()
409
410if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
David Benjamin493d5cb2022-04-18 17:20:27 -0400411 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fvisibility=hidden -fno-common -fno-exceptions -fno-rtti")
David Benjamin49f03292021-03-22 17:35:06 -0400412 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fno-common -std=c11")
Adam Langleycfd80a92019-11-08 14:40:08 -0800413endif()
414
415# pthread_rwlock_t requires a feature flag.
416if(NOT WIN32)
417 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
418endif()
419
420if(WIN32)
421 add_definitions(-D_HAS_EXCEPTIONS=0)
422 add_definitions(-DWIN32_LEAN_AND_MEAN)
423 add_definitions(-DNOMINMAX)
424 # Allow use of fopen.
425 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
426 # VS 2017 and higher supports STL-only warning suppressions.
427 # A bug in CMake < 3.13.0 may cause the space in this value to
428 # cause issues when building with NASM. In that case, update CMake.
429 add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
430endif()
431
432add_definitions(-DBORINGSSL_IMPLEMENTATION)
433
Adam Langley89730072020-01-17 08:18:07 -0800434# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
435# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
436# alone, and expects all architecture-specific logic to be conditioned within
437# the source files rather than the build. This does not work for our assembly
438# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
439# builds.
440if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
441 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
David Benjamin7e265972021-08-04 14:28:55 -0400442 if(NOT NUM_ARCHES EQUAL 1)
Adam Langley89730072020-01-17 08:18:07 -0800443 message(FATAL_ERROR "Universal binaries not supported.")
444 endif()
445 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
446endif()
447
Adam Langleycfd80a92019-11-08 14:40:08 -0800448if(OPENSSL_NO_ASM)
449 add_definitions(-DOPENSSL_NO_ASM)
450 set(ARCH "generic")
David Benjamin7e265972021-08-04 14:28:55 -0400451elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800452 set(ARCH "x86_64")
David Benjamin7e265972021-08-04 14:28:55 -0400453elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800454 set(ARCH "x86_64")
David Benjamin7e265972021-08-04 14:28:55 -0400455elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800456 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
David Benjamin884614c2020-06-16 10:59:58 -0400457 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
Adam Langleycfd80a92019-11-08 14:40:08 -0800458 set(ARCH "x86_64")
459 else()
460 set(ARCH "x86")
461 endif()
David Benjamin7e265972021-08-04 14:28:55 -0400462elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
Adam Langleycfd80a92019-11-08 14:40:08 -0800463 set(ARCH "x86")
David Benjamin7e265972021-08-04 14:28:55 -0400464elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i386")
Adam Langleycfd80a92019-11-08 14:40:08 -0800465 set(ARCH "x86")
David Benjamin7e265972021-08-04 14:28:55 -0400466elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686")
Adam Langleycfd80a92019-11-08 14:40:08 -0800467 set(ARCH "x86")
David Benjamin7e265972021-08-04 14:28:55 -0400468elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800469 set(ARCH "aarch64")
David Benjamin7e265972021-08-04 14:28:55 -0400470elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800471 set(ARCH "aarch64")
472# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
David Benjamin7e265972021-08-04 14:28:55 -0400473elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64e")
Adam Langleycfd80a92019-11-08 14:40:08 -0800474 set(ARCH "aarch64")
David Benjamin7e265972021-08-04 14:28:55 -0400475elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm*")
Adam Langleycfd80a92019-11-08 14:40:08 -0800476 set(ARCH "arm")
David Benjamin7e265972021-08-04 14:28:55 -0400477elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "mips")
Adam Langleycfd80a92019-11-08 14:40:08 -0800478 # Just to avoid the “unknown processor” error.
479 set(ARCH "generic")
David Benjamin7e265972021-08-04 14:28:55 -0400480elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc64le")
Adam Langleycfd80a92019-11-08 14:40:08 -0800481 set(ARCH "ppc64le")
482else()
483 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
484endif()
485
486if(NOT OPENSSL_NO_ASM)
487 if(UNIX)
488 enable_language(ASM)
489
490 # Clang's integerated assembler does not support debug symbols.
491 if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
492 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
493 endif()
494
495 # CMake does not add -isysroot and -arch flags to assembly.
496 if(APPLE)
497 if(CMAKE_OSX_SYSROOT)
498 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
499 endif()
500 foreach(arch ${CMAKE_OSX_ARCHITECTURES})
501 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
502 endforeach()
503 endif()
504 else()
505 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
506 enable_language(ASM_NASM)
507 endif()
508endif()
509
Adam Langleya0cdbf92020-01-21 09:07:46 -0800510if(BUILD_SHARED_LIBS)
511 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
512 # Enable position-independent code globally. This is needed because
513 # some library targets are OBJECT libraries.
514 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
515endif()
516
Adam Langleycfd80a92019-11-08 14:40:08 -0800517include_directories(src/include)
518
519'''
520
521 def PrintLibrary(self, out, name, files):
522 out.write('add_library(\n')
523 out.write(' %s\n\n' % name)
524
525 for f in sorted(files):
526 out.write(' %s\n' % PathOf(f))
527
528 out.write(')\n\n')
529
530 def PrintExe(self, out, name, files, libs):
531 out.write('add_executable(\n')
532 out.write(' %s\n\n' % name)
533
534 for f in sorted(files):
535 out.write(' %s\n' % PathOf(f))
536
537 out.write(')\n\n')
538 out.write('target_link_libraries(%s %s)\n\n' % (name, ' '.join(libs)))
539
540 def PrintSection(self, out, name, files):
541 out.write('set(\n')
542 out.write(' %s\n\n' % name)
543 for f in sorted(files):
544 out.write(' %s\n' % PathOf(f))
545 out.write(')\n\n')
546
547 def WriteFiles(self, files, asm_outputs):
548 with open('CMakeLists.txt', 'w+') as cmake:
549 cmake.write(self.header)
550
551 for ((osname, arch), asm_files) in asm_outputs:
552 self.PrintSection(cmake, 'CRYPTO_%s_%s_SOURCES' % (osname, arch),
553 asm_files)
554
555 cmake.write(
David Benjamin68addd22022-02-08 00:25:34 -0500556R'''if(APPLE)
David Benjamin351b2f82022-02-06 12:57:17 -0500557 set(CRYPTO_ARCH_SOURCES ${CRYPTO_apple_${ARCH}_SOURCES})
Adam Langleycfd80a92019-11-08 14:40:08 -0800558elseif(UNIX)
559 set(CRYPTO_ARCH_SOURCES ${CRYPTO_linux_${ARCH}_SOURCES})
560elseif(WIN32)
561 set(CRYPTO_ARCH_SOURCES ${CRYPTO_win_${ARCH}_SOURCES})
562endif()
563
564''')
565
566 self.PrintLibrary(cmake, 'crypto',
567 files['crypto'] + ['${CRYPTO_ARCH_SOURCES}'])
568 self.PrintLibrary(cmake, 'ssl', files['ssl'])
Adam Langleyff631132020-01-13 15:24:22 -0800569 self.PrintExe(cmake, 'bssl', files['tool'], ['ssl', 'crypto'])
570
571 cmake.write(
David Benjamin8f88b272020-07-09 13:35:01 -0400572R'''if(NOT WIN32 AND NOT ANDROID)
Adam Langleyff631132020-01-13 15:24:22 -0800573 target_link_libraries(crypto pthread)
574endif()
575
David Benjamin8f88b272020-07-09 13:35:01 -0400576if(WIN32)
577 target_link_libraries(bssl ws2_32)
578endif()
579
Adam Langleyff631132020-01-13 15:24:22 -0800580''')
David Benjamin38d01c62016-04-21 18:47:57 -0400581
David Benjamin8c0a6eb2020-07-16 14:45:51 -0400582class JSON(object):
583 def WriteFiles(self, files, asm_outputs):
584 sources = dict(files)
585 for ((osname, arch), asm_files) in asm_outputs:
586 sources['crypto_%s_%s' % (osname, arch)] = asm_files
587 with open('sources.json', 'w+') as f:
588 json.dump(sources, f, sort_keys=True, indent=2)
589
Adam Langley9e1a6602015-05-05 17:47:53 -0700590def FindCMakeFiles(directory):
591 """Returns list of all CMakeLists.txt files recursively in directory."""
592 cmakefiles = []
593
594 for (path, _, filenames) in os.walk(directory):
595 for filename in filenames:
596 if filename == 'CMakeLists.txt':
597 cmakefiles.append(os.path.join(path, filename))
598
599 return cmakefiles
600
Adam Langleyfd499932017-04-04 14:21:43 -0700601def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700602 return is_dir or (path.startswith(
603 os.path.join('src', 'crypto', 'fipsmodule', '')) and
604 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700605
Adam Langleyfd499932017-04-04 14:21:43 -0700606def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700607 return (NoTests(path, dent, is_dir) and
608 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700609
610def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700611 """Filter function that can be passed to FindCFiles in order to remove test
612 sources."""
613 if is_dir:
614 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400615 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700616
617
Adam Langleyfd499932017-04-04 14:21:43 -0700618def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700619 """Filter function that can be passed to FindCFiles in order to remove
620 non-test sources."""
621 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400622 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400623 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700624
625
Adam Langleyfd499932017-04-04 14:21:43 -0700626def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400627 """Filter function that can be passed to FindCFiles in order to include all
628 sources."""
629 return True
630
631
Adam Langleyfd499932017-04-04 14:21:43 -0700632def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700633 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
634 order to exclude test runner files."""
635 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
636 # are in their own subpackage, from being included in boringssl/BUILD files.
637 return not is_dir or dent != 'runner'
638
639
David Benjamin3ecd0a52017-05-19 15:26:18 -0400640def NotGTestSupport(path, dent, is_dir):
David Benjaminc3889632019-03-01 15:03:05 -0500641 return 'gtest' not in dent and 'abi_test' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500642
643
Adam Langleyfd499932017-04-04 14:21:43 -0700644def SSLHeaderFiles(path, dent, is_dir):
Aaron Green0e150022018-10-16 12:05:29 -0700645 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h', 'srtp.h']
Adam Langley049ef412015-06-09 18:20:57 -0700646
647
Adam Langley9e1a6602015-05-05 17:47:53 -0700648def FindCFiles(directory, filter_func):
649 """Recurses through directory and returns a list of paths to all the C source
650 files that pass filter_func."""
651 cfiles = []
652
653 for (path, dirnames, filenames) in os.walk(directory):
654 for filename in filenames:
655 if not filename.endswith('.c') and not filename.endswith('.cc'):
656 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700657 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700658 continue
659 cfiles.append(os.path.join(path, filename))
660
661 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700662 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700663 del dirnames[i]
664
David Benjaminedd4c5f2020-08-19 14:46:17 -0400665 cfiles.sort()
Adam Langley9e1a6602015-05-05 17:47:53 -0700666 return cfiles
667
668
Adam Langley049ef412015-06-09 18:20:57 -0700669def FindHeaderFiles(directory, filter_func):
670 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
671 hfiles = []
672
673 for (path, dirnames, filenames) in os.walk(directory):
674 for filename in filenames:
675 if not filename.endswith('.h'):
676 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700677 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700678 continue
679 hfiles.append(os.path.join(path, filename))
680
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700681 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700682 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700683 del dirnames[i]
684
David Benjaminedd4c5f2020-08-19 14:46:17 -0400685 hfiles.sort()
Adam Langley049ef412015-06-09 18:20:57 -0700686 return hfiles
687
688
Adam Langley9e1a6602015-05-05 17:47:53 -0700689def ExtractPerlAsmFromCMakeFile(cmakefile):
690 """Parses the contents of the CMakeLists.txt file passed as an argument and
691 returns a list of all the perlasm() directives found in the file."""
692 perlasms = []
693 with open(cmakefile) as f:
694 for line in f:
695 line = line.strip()
696 if not line.startswith('perlasm('):
697 continue
698 if not line.endswith(')'):
699 raise ValueError('Bad perlasm line in %s' % cmakefile)
700 # Remove "perlasm(" from start and ")" from end
701 params = line[8:-1].split()
702 if len(params) < 2:
703 raise ValueError('Bad perlasm line in %s' % cmakefile)
704 perlasms.append({
705 'extra_args': params[2:],
706 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
707 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
708 })
709
710 return perlasms
711
712
713def ReadPerlAsmOperations():
714 """Returns a list of all perlasm() directives found in CMake config files in
715 src/."""
716 perlasms = []
717 cmakefiles = FindCMakeFiles('src')
718
719 for cmakefile in cmakefiles:
720 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
721
722 return perlasms
723
724
725def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
726 """Runs the a perlasm script and puts the output into output_filename."""
727 base_dir = os.path.dirname(output_filename)
728 if not os.path.isdir(base_dir):
729 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400730 subprocess.check_call(
731 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700732
733
734def ArchForAsmFilename(filename):
735 """Returns the architectures that a given asm file should be compiled for
736 based on substrings in the filename."""
737
738 if 'x86_64' in filename or 'avx2' in filename:
739 return ['x86_64']
740 elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
741 return ['x86']
742 elif 'armx' in filename:
743 return ['arm', 'aarch64']
744 elif 'armv8' in filename:
745 return ['aarch64']
746 elif 'arm' in filename:
747 return ['arm']
David Benjamin9f16ce12016-09-27 16:30:22 -0400748 elif 'ppc' in filename:
749 return ['ppc64le']
Adam Langley9e1a6602015-05-05 17:47:53 -0700750 else:
751 raise ValueError('Unknown arch for asm filename: ' + filename)
752
753
754def WriteAsmFiles(perlasms):
755 """Generates asm files from perlasm directives for each supported OS x
756 platform combination."""
757 asmfiles = {}
758
759 for osarch in OS_ARCH_COMBOS:
760 (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
761 key = (osname, arch)
762 outDir = '%s-%s' % key
763
764 for perlasm in perlasms:
765 filename = os.path.basename(perlasm['input'])
766 output = perlasm['output']
767 if not output.startswith('src'):
768 raise ValueError('output missing src: %s' % output)
769 output = os.path.join(outDir, output[4:])
William Hessec618c402015-06-22 16:34:02 +0200770 if output.endswith('-armx.${ASM_EXT}'):
771 output = output.replace('-armx',
772 '-armx64' if arch == 'aarch64' else '-armx32')
Adam Langley9e1a6602015-05-05 17:47:53 -0700773 output = output.replace('${ASM_EXT}', asm_ext)
774
775 if arch in ArchForAsmFilename(filename):
776 PerlAsm(output, perlasm['input'], perlasm_style,
777 perlasm['extra_args'] + extra_args)
778 asmfiles.setdefault(key, []).append(output)
779
Yoshisato Yanagisawa56508162021-03-23 05:34:47 +0000780 for (key, non_perl_asm_files) in NON_PERL_FILES.items():
Adam Langley9e1a6602015-05-05 17:47:53 -0700781 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
782
Yoshisato Yanagisawa56508162021-03-23 05:34:47 +0000783 for files in asmfiles.values():
David Benjaminedd4c5f2020-08-19 14:46:17 -0400784 files.sort()
785
Adam Langley9e1a6602015-05-05 17:47:53 -0700786 return asmfiles
787
788
David Benjamin3ecd0a52017-05-19 15:26:18 -0400789def ExtractVariablesFromCMakeFile(cmakefile):
790 """Parses the contents of the CMakeLists.txt file passed as an argument and
791 returns a dictionary of exported source lists."""
792 variables = {}
793 in_set_command = False
794 set_command = []
795 with open(cmakefile) as f:
796 for line in f:
797 if '#' in line:
798 line = line[:line.index('#')]
799 line = line.strip()
800
801 if not in_set_command:
802 if line.startswith('set('):
803 in_set_command = True
804 set_command = []
805 elif line == ')':
806 in_set_command = False
807 if not set_command:
808 raise ValueError('Empty set command')
809 variables[set_command[0]] = set_command[1:]
810 else:
811 set_command.extend([c for c in line.split(' ') if c])
812
813 if in_set_command:
814 raise ValueError('Unfinished set command')
815 return variables
816
817
Adam Langley049ef412015-06-09 18:20:57 -0700818def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400819 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Andres Erbsen5b280a82017-10-30 15:58:33 +0000820 crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
Adam Langley7f028812019-10-18 14:48:11 -0700821 FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments))
Adam Langleyfd499932017-04-04 14:21:43 -0700822 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800823 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400824 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700825 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700826
Pete Bentley44544d92019-08-15 15:01:26 +0100827 # BCM shared library C files
828 bcm_crypto_c_files = [
829 os.path.join('src', 'crypto', 'fipsmodule', 'bcm.c')
830 ]
831
Adam Langley9e1a6602015-05-05 17:47:53 -0700832 # Generate err_data.c
833 with open('err_data.c', 'w+') as err_data:
834 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
835 cwd=os.path.join('src', 'crypto', 'err'),
836 stdout=err_data)
837 crypto_c_files.append('err_data.c')
David Benjaminedd4c5f2020-08-19 14:46:17 -0400838 crypto_c_files.sort()
Adam Langley9e1a6602015-05-05 17:47:53 -0700839
David Benjamin38d01c62016-04-21 18:47:57 -0400840 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400841 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700842 test_support_h_files = (
843 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700844 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400845
Adam Langley990a3232018-05-22 10:02:59 -0700846 crypto_test_files = []
847 if EMBED_TEST_DATA:
848 # Generate crypto_test_data.cc
849 with open('crypto_test_data.cc', 'w+') as out:
850 subprocess.check_call(
851 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
852 cwd='src',
853 stdout=out)
854 crypto_test_files += ['crypto_test_data.cc']
David Benjamin3ecd0a52017-05-19 15:26:18 -0400855
Adam Langley990a3232018-05-22 10:02:59 -0700856 crypto_test_files += FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
David Benjamin96ee4a82017-07-09 23:46:47 -0400857 crypto_test_files += [
David Benjaminc3889632019-03-01 15:03:05 -0500858 'src/crypto/test/abi_test.cc',
David Benjamin3ecd0a52017-05-19 15:26:18 -0400859 'src/crypto/test/file_test_gtest.cc',
860 'src/crypto/test/gtest_main.cc',
861 ]
Adam Langley3e502c82019-10-16 09:56:38 -0700862 # urandom_test.cc is in a separate binary so that it can be test PRNG
863 # initialisation.
864 crypto_test_files = [
865 file for file in crypto_test_files
866 if not file.endswith('/urandom_test.cc')
867 ]
David Benjaminedd4c5f2020-08-19 14:46:17 -0400868 crypto_test_files.sort()
David Benjamin1d5a5702017-02-13 22:11:49 -0500869
870 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
Robert Sloanae1e0872019-03-01 16:01:30 -0800871 ssl_test_files += [
872 'src/crypto/test/abi_test.cc',
873 'src/crypto/test/gtest_main.cc',
874 ]
David Benjaminedd4c5f2020-08-19 14:46:17 -0400875 ssl_test_files.sort()
Adam Langley9e1a6602015-05-05 17:47:53 -0700876
Adam Langley3e502c82019-10-16 09:56:38 -0700877 urandom_test_files = [
878 'src/crypto/fipsmodule/rand/urandom_test.cc',
879 ]
880
David Benjamin38d01c62016-04-21 18:47:57 -0400881 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
882
David Benjaminedd4c5f2020-08-19 14:46:17 -0400883 ssl_h_files = FindHeaderFiles(os.path.join('src', 'include', 'openssl'),
884 SSLHeaderFiles)
Adam Langley049ef412015-06-09 18:20:57 -0700885
Adam Langleyfd499932017-04-04 14:21:43 -0700886 def NotSSLHeaderFiles(path, filename, is_dir):
887 return not SSLHeaderFiles(path, filename, is_dir)
David Benjaminedd4c5f2020-08-19 14:46:17 -0400888 crypto_h_files = FindHeaderFiles(os.path.join('src', 'include', 'openssl'),
889 NotSSLHeaderFiles)
Adam Langley049ef412015-06-09 18:20:57 -0700890
891 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
Andres Erbsen5b280a82017-10-30 15:58:33 +0000892 crypto_internal_h_files = (
893 FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
Adam Langley7f028812019-10-18 14:48:11 -0700894 FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests))
Adam Langley049ef412015-06-09 18:20:57 -0700895
Adam Langley9e1a6602015-05-05 17:47:53 -0700896 files = {
Pete Bentley44544d92019-08-15 15:01:26 +0100897 'bcm_crypto': bcm_crypto_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700898 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700899 'crypto_headers': crypto_h_files,
900 'crypto_internal_headers': crypto_internal_h_files,
David Benjaminedd4c5f2020-08-19 14:46:17 -0400901 'crypto_test': crypto_test_files,
Adam Langley990a3232018-05-22 10:02:59 -0700902 'crypto_test_data': sorted('src/' + x for x in cmake['CRYPTO_TEST_DATA']),
Adam Langleyfd499932017-04-04 14:21:43 -0700903 'fips_fragments': fips_fragments,
David Benjamin38d01c62016-04-21 18:47:57 -0400904 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800905 'ssl': ssl_source_files,
Adam Langley049ef412015-06-09 18:20:57 -0700906 'ssl_headers': ssl_h_files,
907 'ssl_internal_headers': ssl_internal_h_files,
David Benjaminedd4c5f2020-08-19 14:46:17 -0400908 'ssl_test': ssl_test_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400909 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700910 'tool_headers': tool_h_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400911 'test_support': test_support_c_files,
912 'test_support_headers': test_support_h_files,
David Benjaminedd4c5f2020-08-19 14:46:17 -0400913 'urandom_test': urandom_test_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700914 }
915
Yoshisato Yanagisawa56508162021-03-23 05:34:47 +0000916 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).items())
Adam Langley9e1a6602015-05-05 17:47:53 -0700917
Adam Langley049ef412015-06-09 18:20:57 -0700918 for platform in platforms:
919 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700920
921 return 0
922
David Benjamin8c0a6eb2020-07-16 14:45:51 -0400923ALL_PLATFORMS = {
924 'android': Android,
925 'android-cmake': AndroidCMake,
926 'bazel': Bazel,
927 'cmake': CMake,
928 'eureka': Eureka,
929 'gn': GN,
930 'gyp': GYP,
931 'json': JSON,
932}
Adam Langley9e1a6602015-05-05 17:47:53 -0700933
Adam Langley9e1a6602015-05-05 17:47:53 -0700934if __name__ == '__main__':
David Benjamin4acc7dd2022-11-19 10:13:49 -0500935 parser = optparse.OptionParser(
936 usage='Usage: %%prog [--prefix=<path>] [all|%s]' %
937 '|'.join(sorted(ALL_PLATFORMS.keys())))
Matt Braithwaite16695892016-06-09 09:34:11 -0700938 parser.add_option('--prefix', dest='prefix',
939 help='For Bazel, prepend argument to all source files')
Adam Langley990a3232018-05-22 10:02:59 -0700940 parser.add_option(
941 '--embed_test_data', type='choice', dest='embed_test_data',
942 action='store', default="true", choices=["true", "false"],
David Benjaminf014d602019-05-07 18:58:06 -0500943 help='For Bazel or GN, don\'t embed data files in crypto_test_data.cc')
Matt Braithwaite16695892016-06-09 09:34:11 -0700944 options, args = parser.parse_args(sys.argv[1:])
945 PREFIX = options.prefix
Adam Langley990a3232018-05-22 10:02:59 -0700946 EMBED_TEST_DATA = (options.embed_test_data == "true")
Matt Braithwaite16695892016-06-09 09:34:11 -0700947
948 if not args:
949 parser.print_help()
950 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700951
David Benjamin4acc7dd2022-11-19 10:13:49 -0500952 if 'all' in args:
953 platforms = [platform() for platform in ALL_PLATFORMS.values()]
954 else:
955 platforms = []
956 for s in args:
957 platform = ALL_PLATFORMS.get(s)
958 if platform is None:
959 parser.print_help()
960 sys.exit(1)
961 platforms.append(platform())
Adam Langley9e1a6602015-05-05 17:47:53 -0700962
Adam Langley049ef412015-06-09 18:20:57 -0700963 sys.exit(main(platforms))