blob: 4a93a7fc887d071d02a284bc1234a577a4495cbe [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.
David Benjamin19676212023-01-25 10:03:53 -050028#
29# TODO(https://crbug.com/boringssl/524): This probably should be a map, but some
30# downstream scripts import this to find what folders to add/remove from git.
Adam Langley9e1a6602015-05-05 17:47:53 -070031OS_ARCH_COMBOS = [
David Benjamin351b2f82022-02-06 12:57:17 -050032 ('apple', 'arm', 'ios32', [], 'S'),
33 ('apple', 'aarch64', 'ios64', [], 'S'),
34 ('apple', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
35 ('apple', 'x86_64', 'macosx', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070036 ('linux', 'arm', 'linux32', [], 'S'),
37 ('linux', 'aarch64', 'linux64', [], 'S'),
Adam Langley7c075b92017-05-22 15:31:13 -070038 ('linux', 'ppc64le', 'linux64le', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070039 ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
40 ('linux', 'x86_64', 'elf', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070041 ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
42 ('win', 'x86_64', 'nasm', [], 'asm'),
Anthony Robertsafd5dba2020-10-19 12:27:51 +010043 ('win', 'aarch64', 'win64', [], 'S'),
Adam Langley9e1a6602015-05-05 17:47:53 -070044]
45
46# NON_PERL_FILES enumerates assembly files that are not processed by the
47# perlasm system.
48NON_PERL_FILES = {
49 ('linux', 'arm'): [
Adam Langley7b8b9c12016-01-04 07:13:00 -080050 'src/crypto/curve25519/asm/x25519-asm-arm.S',
David Benjamin3c4a5cb2016-03-29 17:43:31 -040051 'src/crypto/poly1305/poly1305_arm_asm.S',
Adam Langley7b935932018-11-12 13:53:42 -080052 ],
Adam Langley9e1a6602015-05-05 17:47:53 -070053}
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 """
David Benjamin19676212023-01-25 10:03:53 -0500170 # TODO(https://crbug.com/boringssl/542): Rather than filtering by filename,
171 # use the variable listed in the CMake perlasm line, available in
172 # ExtractPerlAsmFromCMakeFile.
Pete Bentley44544d92019-08-15 15:01:26 +0100173 return [(archinfo, filter(lambda p: ("/crypto/fipsmodule/" in p) == want_bcm, files))
174 for (archinfo, files) in asm]
Adam Langley9e1a6602015-05-05 17:47:53 -0700175
176
David Benjamineca48e52019-08-13 11:51:53 -0400177class AndroidCMake(object):
178
179 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500180 self.header = LicenseHeader("#") + """
David Benjamineca48e52019-08-13 11:51:53 -0400181# This file is created by generate_build_files.py. Do not edit manually.
182# To specify a custom path prefix, set BORINGSSL_ROOT before including this
183# file, or use list(TRANSFORM ... PREPEND) from CMake 3.12.
184
185"""
186
187 def PrintVariableSection(self, out, name, files):
188 out.write('set(%s\n' % name)
189 for f in sorted(files):
190 # Ideally adding the prefix would be the caller's job, but
191 # list(TRANSFORM ... PREPEND) is only available starting CMake 3.12. When
192 # sources.cmake is the source of truth, we can ask Android to either write
193 # a CMake function or update to 3.12.
194 out.write(' ${BORINGSSL_ROOT}%s\n' % f)
195 out.write(')\n')
196
197 def WriteFiles(self, files, asm_outputs):
198 # The Android emulator uses a custom CMake buildsystem.
199 #
200 # TODO(davidben): Move our various source lists into sources.cmake and have
201 # Android consume that directly.
202 with open('android-sources.cmake', 'w+') as out:
203 out.write(self.header)
204
205 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
206 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
207 self.PrintVariableSection(out, 'tool_sources', files['tool'])
208 self.PrintVariableSection(out, 'test_support_sources',
209 files['test_support'])
210 self.PrintVariableSection(out, 'crypto_test_sources',
211 files['crypto_test'])
212 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
213
214 for ((osname, arch), asm_files) in asm_outputs:
215 self.PrintVariableSection(
216 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
217
218
Adam Langley049ef412015-06-09 18:20:57 -0700219class Bazel(object):
220 """Bazel outputs files suitable for including in Bazel files."""
221
222 def __init__(self):
223 self.firstSection = True
224 self.header = \
225"""# This file is created by generate_build_files.py. Do not edit manually.
226
227"""
228
229 def PrintVariableSection(self, out, name, files):
230 if not self.firstSection:
231 out.write('\n')
232 self.firstSection = False
233
234 out.write('%s = [\n' % name)
235 for f in sorted(files):
Matt Braithwaite16695892016-06-09 09:34:11 -0700236 out.write(' "%s",\n' % PathOf(f))
Adam Langley049ef412015-06-09 18:20:57 -0700237 out.write(']\n')
238
239 def WriteFiles(self, files, asm_outputs):
Chuck Haysc608d6b2015-10-06 17:54:16 -0700240 with open('BUILD.generated.bzl', 'w+') as out:
Adam Langley049ef412015-06-09 18:20:57 -0700241 out.write(self.header)
242
243 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
Adam Langleyfd499932017-04-04 14:21:43 -0700244 self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
Adam Langley049ef412015-06-09 18:20:57 -0700245 self.PrintVariableSection(
246 out, 'ssl_internal_headers', files['ssl_internal_headers'])
247 self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
248 self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
249 self.PrintVariableSection(
250 out, 'crypto_internal_headers', files['crypto_internal_headers'])
251 self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
252 self.PrintVariableSection(out, 'tool_sources', files['tool'])
Adam Langleyf11f2332016-06-30 11:56:19 -0700253 self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
Adam Langley049ef412015-06-09 18:20:57 -0700254
255 for ((osname, arch), asm_files) in asm_outputs:
Adam Langley049ef412015-06-09 18:20:57 -0700256 self.PrintVariableSection(
Piotr Sikora3f5fe602015-10-28 12:24:35 -0700257 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
Adam Langley049ef412015-06-09 18:20:57 -0700258
Chuck Haysc608d6b2015-10-06 17:54:16 -0700259 with open('BUILD.generated_tests.bzl', 'w+') as out:
Adam Langley9c164b22015-06-10 18:54:47 -0700260 out.write(self.header)
261
262 out.write('test_support_sources = [\n')
David Benjaminc5aa8412016-07-29 17:41:58 -0400263 for filename in sorted(files['test_support'] +
264 files['test_support_headers'] +
265 files['crypto_internal_headers'] +
266 files['ssl_internal_headers']):
Adam Langley9c164b22015-06-10 18:54:47 -0700267 if os.path.basename(filename) == 'malloc.cc':
268 continue
Matt Braithwaite16695892016-06-09 09:34:11 -0700269 out.write(' "%s",\n' % PathOf(filename))
Adam Langley9c164b22015-06-10 18:54:47 -0700270
Adam Langley7b6acc52017-07-27 16:33:27 -0700271 out.write(']\n')
Chuck Haysc608d6b2015-10-06 17:54:16 -0700272
David Benjamin96628432017-01-19 19:05:47 -0500273 self.PrintVariableSection(out, 'crypto_test_sources',
274 files['crypto_test'])
275 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
Adam Langley990a3232018-05-22 10:02:59 -0700276 self.PrintVariableSection(out, 'crypto_test_data',
277 files['crypto_test_data'])
Adam Langley3e502c82019-10-16 09:56:38 -0700278 self.PrintVariableSection(out, 'urandom_test_sources',
279 files['urandom_test'])
David Benjamin96628432017-01-19 19:05:47 -0500280
Adam Langley049ef412015-06-09 18:20:57 -0700281
Robert Sloane091af42017-10-09 12:47:17 -0700282class Eureka(object):
283
284 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500285 self.header = LicenseHeader("#") + """
Robert Sloane091af42017-10-09 12:47:17 -0700286# This file is created by generate_build_files.py. Do not edit manually.
287
288"""
289
290 def PrintVariableSection(self, out, name, files):
291 out.write('%s := \\\n' % name)
292 for f in sorted(files):
293 out.write(' %s\\\n' % f)
294 out.write('\n')
295
296 def WriteFiles(self, files, asm_outputs):
297 # Legacy Android.mk format
298 with open('eureka.mk', 'w+') as makefile:
299 makefile.write(self.header)
300
301 self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
302 self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
303 self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
304
305 for ((osname, arch), asm_files) in asm_outputs:
306 if osname != 'linux':
307 continue
308 self.PrintVariableSection(
309 makefile, '%s_%s_sources' % (osname, arch), asm_files)
310
311
David Benjamin38d01c62016-04-21 18:47:57 -0400312class GN(object):
313
314 def __init__(self):
315 self.firstSection = True
David Benjamin70690f72023-01-25 09:56:43 -0500316 self.header = LicenseHeader("#") + """
David Benjamin38d01c62016-04-21 18:47:57 -0400317# This file is created by generate_build_files.py. Do not edit manually.
318
319"""
320
321 def PrintVariableSection(self, out, name, files):
322 if not self.firstSection:
323 out.write('\n')
324 self.firstSection = False
325
326 out.write('%s = [\n' % name)
327 for f in sorted(files):
328 out.write(' "%s",\n' % f)
329 out.write(']\n')
330
331 def WriteFiles(self, files, asm_outputs):
332 with open('BUILD.generated.gni', 'w+') as out:
333 out.write(self.header)
334
David Benjaminc5aa8412016-07-29 17:41:58 -0400335 self.PrintVariableSection(out, 'crypto_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700336 files['crypto'] +
David Benjaminc5aa8412016-07-29 17:41:58 -0400337 files['crypto_internal_headers'])
James Robinson98dd68f2018-04-11 14:47:34 -0700338 self.PrintVariableSection(out, 'crypto_headers',
339 files['crypto_headers'])
David Benjaminc5aa8412016-07-29 17:41:58 -0400340 self.PrintVariableSection(out, 'ssl_sources',
James Robinson98dd68f2018-04-11 14:47:34 -0700341 files['ssl'] + files['ssl_internal_headers'])
342 self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
David Benjaminbb0cb952020-12-17 16:35:39 -0500343 self.PrintVariableSection(out, 'tool_sources',
344 files['tool'] + files['tool_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400345
346 for ((osname, arch), asm_files) in asm_outputs:
347 self.PrintVariableSection(
348 out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
349
350 fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
351 for fuzzer in files['fuzz']]
352 self.PrintVariableSection(out, 'fuzzers', fuzzers)
353
354 with open('BUILD.generated_tests.gni', 'w+') as out:
355 self.firstSection = True
356 out.write(self.header)
357
David Benjamin96628432017-01-19 19:05:47 -0500358 self.PrintVariableSection(out, 'test_support_sources',
David Benjaminc5aa8412016-07-29 17:41:58 -0400359 files['test_support'] +
360 files['test_support_headers'])
David Benjamin96628432017-01-19 19:05:47 -0500361 self.PrintVariableSection(out, 'crypto_test_sources',
362 files['crypto_test'])
David Benjaminf014d602019-05-07 18:58:06 -0500363 self.PrintVariableSection(out, 'crypto_test_data',
364 files['crypto_test_data'])
David Benjamin96628432017-01-19 19:05:47 -0500365 self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
David Benjamin38d01c62016-04-21 18:47:57 -0400366
367
368class GYP(object):
369
370 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500371 self.header = LicenseHeader("#") + """
David Benjamin38d01c62016-04-21 18:47:57 -0400372# This file is created by generate_build_files.py. Do not edit manually.
373
374"""
375
376 def PrintVariableSection(self, out, name, files):
377 out.write(' \'%s\': [\n' % name)
378 for f in sorted(files):
379 out.write(' \'%s\',\n' % f)
380 out.write(' ],\n')
381
382 def WriteFiles(self, files, asm_outputs):
383 with open('boringssl.gypi', 'w+') as gypi:
384 gypi.write(self.header + '{\n \'variables\': {\n')
385
David Benjaminc5aa8412016-07-29 17:41:58 -0400386 self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
387 files['ssl'] + files['ssl_headers'] +
388 files['ssl_internal_headers'])
389 self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
390 files['crypto'] + files['crypto_headers'] +
391 files['crypto_internal_headers'])
David Benjamin38d01c62016-04-21 18:47:57 -0400392
393 for ((osname, arch), asm_files) in asm_outputs:
394 self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
395 (osname, arch), asm_files)
396
397 gypi.write(' }\n}\n')
398
Adam Langleycfd80a92019-11-08 14:40:08 -0800399class CMake(object):
400
401 def __init__(self):
David Benjamin70690f72023-01-25 09:56:43 -0500402 self.header = LicenseHeader("#") + R'''
Adam Langleycfd80a92019-11-08 14:40:08 -0800403# This file is created by generate_build_files.py. Do not edit manually.
404
David Benjamind0b66c72021-03-22 17:30:02 -0400405cmake_minimum_required(VERSION 3.5)
Adam Langleycfd80a92019-11-08 14:40:08 -0800406
407project(BoringSSL LANGUAGES C CXX)
408
409if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
410 set(CLANG 1)
411endif()
412
413if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
David Benjamin493d5cb2022-04-18 17:20:27 -0400414 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 -0400415 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fno-common -std=c11")
Adam Langleycfd80a92019-11-08 14:40:08 -0800416endif()
417
418# pthread_rwlock_t requires a feature flag.
419if(NOT WIN32)
420 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
421endif()
422
423if(WIN32)
424 add_definitions(-D_HAS_EXCEPTIONS=0)
425 add_definitions(-DWIN32_LEAN_AND_MEAN)
426 add_definitions(-DNOMINMAX)
427 # Allow use of fopen.
428 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
429 # VS 2017 and higher supports STL-only warning suppressions.
430 # A bug in CMake < 3.13.0 may cause the space in this value to
431 # cause issues when building with NASM. In that case, update CMake.
432 add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
433endif()
434
435add_definitions(-DBORINGSSL_IMPLEMENTATION)
436
Adam Langley89730072020-01-17 08:18:07 -0800437# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
438# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
439# alone, and expects all architecture-specific logic to be conditioned within
440# the source files rather than the build. This does not work for our assembly
441# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
442# builds.
443if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
444 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
David Benjamin7e265972021-08-04 14:28:55 -0400445 if(NOT NUM_ARCHES EQUAL 1)
Adam Langley89730072020-01-17 08:18:07 -0800446 message(FATAL_ERROR "Universal binaries not supported.")
447 endif()
448 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
449endif()
450
Adam Langleycfd80a92019-11-08 14:40:08 -0800451if(OPENSSL_NO_ASM)
452 add_definitions(-DOPENSSL_NO_ASM)
453 set(ARCH "generic")
David Benjamin7e265972021-08-04 14:28:55 -0400454elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800455 set(ARCH "x86_64")
David Benjamin7e265972021-08-04 14:28:55 -0400456elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800457 set(ARCH "x86_64")
David Benjamin7e265972021-08-04 14:28:55 -0400458elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800459 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
David Benjamin884614c2020-06-16 10:59:58 -0400460 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
Adam Langleycfd80a92019-11-08 14:40:08 -0800461 set(ARCH "x86_64")
462 else()
463 set(ARCH "x86")
464 endif()
David Benjamin7e265972021-08-04 14:28:55 -0400465elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
Adam Langleycfd80a92019-11-08 14:40:08 -0800466 set(ARCH "x86")
David Benjamin7e265972021-08-04 14:28:55 -0400467elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i386")
Adam Langleycfd80a92019-11-08 14:40:08 -0800468 set(ARCH "x86")
David Benjamin7e265972021-08-04 14:28:55 -0400469elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686")
Adam Langleycfd80a92019-11-08 14:40:08 -0800470 set(ARCH "x86")
David Benjamin7e265972021-08-04 14:28:55 -0400471elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800472 set(ARCH "aarch64")
David Benjamin7e265972021-08-04 14:28:55 -0400473elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
Adam Langleycfd80a92019-11-08 14:40:08 -0800474 set(ARCH "aarch64")
475# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
David Benjamin7e265972021-08-04 14:28:55 -0400476elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64e")
Adam Langleycfd80a92019-11-08 14:40:08 -0800477 set(ARCH "aarch64")
David Benjamin7e265972021-08-04 14:28:55 -0400478elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm*")
Adam Langleycfd80a92019-11-08 14:40:08 -0800479 set(ARCH "arm")
David Benjamin7e265972021-08-04 14:28:55 -0400480elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "mips")
Adam Langleycfd80a92019-11-08 14:40:08 -0800481 # Just to avoid the “unknown processor” error.
482 set(ARCH "generic")
David Benjamin7e265972021-08-04 14:28:55 -0400483elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc64le")
Adam Langleycfd80a92019-11-08 14:40:08 -0800484 set(ARCH "ppc64le")
485else()
486 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
487endif()
488
489if(NOT OPENSSL_NO_ASM)
490 if(UNIX)
491 enable_language(ASM)
492
493 # Clang's integerated assembler does not support debug symbols.
494 if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
495 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
496 endif()
497
498 # CMake does not add -isysroot and -arch flags to assembly.
499 if(APPLE)
500 if(CMAKE_OSX_SYSROOT)
501 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
502 endif()
503 foreach(arch ${CMAKE_OSX_ARCHITECTURES})
504 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
505 endforeach()
506 endif()
507 else()
508 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
509 enable_language(ASM_NASM)
510 endif()
511endif()
512
Adam Langleya0cdbf92020-01-21 09:07:46 -0800513if(BUILD_SHARED_LIBS)
514 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
515 # Enable position-independent code globally. This is needed because
516 # some library targets are OBJECT libraries.
517 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
518endif()
519
Adam Langleycfd80a92019-11-08 14:40:08 -0800520include_directories(src/include)
521
522'''
523
524 def PrintLibrary(self, out, name, files):
525 out.write('add_library(\n')
526 out.write(' %s\n\n' % name)
527
528 for f in sorted(files):
529 out.write(' %s\n' % PathOf(f))
530
531 out.write(')\n\n')
532
533 def PrintExe(self, out, name, files, libs):
534 out.write('add_executable(\n')
535 out.write(' %s\n\n' % name)
536
537 for f in sorted(files):
538 out.write(' %s\n' % PathOf(f))
539
540 out.write(')\n\n')
541 out.write('target_link_libraries(%s %s)\n\n' % (name, ' '.join(libs)))
542
543 def PrintSection(self, out, name, files):
544 out.write('set(\n')
545 out.write(' %s\n\n' % name)
546 for f in sorted(files):
547 out.write(' %s\n' % PathOf(f))
548 out.write(')\n\n')
549
550 def WriteFiles(self, files, asm_outputs):
551 with open('CMakeLists.txt', 'w+') as cmake:
552 cmake.write(self.header)
553
554 for ((osname, arch), asm_files) in asm_outputs:
555 self.PrintSection(cmake, 'CRYPTO_%s_%s_SOURCES' % (osname, arch),
556 asm_files)
557
558 cmake.write(
David Benjamin68addd22022-02-08 00:25:34 -0500559R'''if(APPLE)
David Benjamin351b2f82022-02-06 12:57:17 -0500560 set(CRYPTO_ARCH_SOURCES ${CRYPTO_apple_${ARCH}_SOURCES})
Adam Langleycfd80a92019-11-08 14:40:08 -0800561elseif(UNIX)
562 set(CRYPTO_ARCH_SOURCES ${CRYPTO_linux_${ARCH}_SOURCES})
563elseif(WIN32)
564 set(CRYPTO_ARCH_SOURCES ${CRYPTO_win_${ARCH}_SOURCES})
565endif()
566
567''')
568
569 self.PrintLibrary(cmake, 'crypto',
570 files['crypto'] + ['${CRYPTO_ARCH_SOURCES}'])
571 self.PrintLibrary(cmake, 'ssl', files['ssl'])
Adam Langleyff631132020-01-13 15:24:22 -0800572 self.PrintExe(cmake, 'bssl', files['tool'], ['ssl', 'crypto'])
573
574 cmake.write(
David Benjamin8f88b272020-07-09 13:35:01 -0400575R'''if(NOT WIN32 AND NOT ANDROID)
Adam Langleyff631132020-01-13 15:24:22 -0800576 target_link_libraries(crypto pthread)
577endif()
578
David Benjamin8f88b272020-07-09 13:35:01 -0400579if(WIN32)
580 target_link_libraries(bssl ws2_32)
581endif()
582
Adam Langleyff631132020-01-13 15:24:22 -0800583''')
David Benjamin38d01c62016-04-21 18:47:57 -0400584
David Benjamin8c0a6eb2020-07-16 14:45:51 -0400585class JSON(object):
586 def WriteFiles(self, files, asm_outputs):
587 sources = dict(files)
588 for ((osname, arch), asm_files) in asm_outputs:
589 sources['crypto_%s_%s' % (osname, arch)] = asm_files
590 with open('sources.json', 'w+') as f:
591 json.dump(sources, f, sort_keys=True, indent=2)
592
Adam Langley9e1a6602015-05-05 17:47:53 -0700593def FindCMakeFiles(directory):
594 """Returns list of all CMakeLists.txt files recursively in directory."""
595 cmakefiles = []
596
597 for (path, _, filenames) in os.walk(directory):
598 for filename in filenames:
599 if filename == 'CMakeLists.txt':
600 cmakefiles.append(os.path.join(path, filename))
601
602 return cmakefiles
603
Adam Langleyfd499932017-04-04 14:21:43 -0700604def OnlyFIPSFragments(path, dent, is_dir):
Matthew Braithwaite95511e92017-05-08 16:38:03 -0700605 return is_dir or (path.startswith(
606 os.path.join('src', 'crypto', 'fipsmodule', '')) and
607 NoTests(path, dent, is_dir))
Adam Langley9e1a6602015-05-05 17:47:53 -0700608
Adam Langleyfd499932017-04-04 14:21:43 -0700609def NoTestsNorFIPSFragments(path, dent, is_dir):
Adam Langley323f1eb2017-04-06 17:29:10 -0700610 return (NoTests(path, dent, is_dir) and
611 (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
Adam Langleyfd499932017-04-04 14:21:43 -0700612
613def NoTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700614 """Filter function that can be passed to FindCFiles in order to remove test
615 sources."""
616 if is_dir:
617 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400618 return 'test.' not in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700619
620
Adam Langleyfd499932017-04-04 14:21:43 -0700621def OnlyTests(path, dent, is_dir):
Adam Langley9e1a6602015-05-05 17:47:53 -0700622 """Filter function that can be passed to FindCFiles in order to remove
623 non-test sources."""
624 if is_dir:
David Benjamin26073832015-05-11 20:52:48 -0400625 return dent != 'test'
David Benjamin96ee4a82017-07-09 23:46:47 -0400626 return '_test.' in dent
Adam Langley9e1a6602015-05-05 17:47:53 -0700627
628
Adam Langleyfd499932017-04-04 14:21:43 -0700629def AllFiles(path, dent, is_dir):
David Benjamin26073832015-05-11 20:52:48 -0400630 """Filter function that can be passed to FindCFiles in order to include all
631 sources."""
632 return True
633
634
Adam Langleyfd499932017-04-04 14:21:43 -0700635def NoTestRunnerFiles(path, dent, is_dir):
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700636 """Filter function that can be passed to FindCFiles or FindHeaderFiles in
637 order to exclude test runner files."""
638 # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
639 # are in their own subpackage, from being included in boringssl/BUILD files.
640 return not is_dir or dent != 'runner'
641
642
David Benjamin3ecd0a52017-05-19 15:26:18 -0400643def NotGTestSupport(path, dent, is_dir):
David Benjaminc3889632019-03-01 15:03:05 -0500644 return 'gtest' not in dent and 'abi_test' not in dent
David Benjamin96628432017-01-19 19:05:47 -0500645
646
Adam Langleyfd499932017-04-04 14:21:43 -0700647def SSLHeaderFiles(path, dent, is_dir):
Aaron Green0e150022018-10-16 12:05:29 -0700648 return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h', 'srtp.h']
Adam Langley049ef412015-06-09 18:20:57 -0700649
650
Adam Langley9e1a6602015-05-05 17:47:53 -0700651def FindCFiles(directory, filter_func):
652 """Recurses through directory and returns a list of paths to all the C source
653 files that pass filter_func."""
654 cfiles = []
655
656 for (path, dirnames, filenames) in os.walk(directory):
657 for filename in filenames:
658 if not filename.endswith('.c') and not filename.endswith('.cc'):
659 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700660 if not filter_func(path, filename, False):
Adam Langley9e1a6602015-05-05 17:47:53 -0700661 continue
662 cfiles.append(os.path.join(path, filename))
663
664 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700665 if not filter_func(path, dirname, True):
Adam Langley9e1a6602015-05-05 17:47:53 -0700666 del dirnames[i]
667
David Benjaminedd4c5f2020-08-19 14:46:17 -0400668 cfiles.sort()
Adam Langley9e1a6602015-05-05 17:47:53 -0700669 return cfiles
670
671
Adam Langley049ef412015-06-09 18:20:57 -0700672def FindHeaderFiles(directory, filter_func):
673 """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
674 hfiles = []
675
676 for (path, dirnames, filenames) in os.walk(directory):
677 for filename in filenames:
678 if not filename.endswith('.h'):
679 continue
Adam Langleyfd499932017-04-04 14:21:43 -0700680 if not filter_func(path, filename, False):
Adam Langley049ef412015-06-09 18:20:57 -0700681 continue
682 hfiles.append(os.path.join(path, filename))
683
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700684 for (i, dirname) in enumerate(dirnames):
Adam Langleyfd499932017-04-04 14:21:43 -0700685 if not filter_func(path, dirname, True):
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700686 del dirnames[i]
687
David Benjaminedd4c5f2020-08-19 14:46:17 -0400688 hfiles.sort()
Adam Langley049ef412015-06-09 18:20:57 -0700689 return hfiles
690
691
Adam Langley9e1a6602015-05-05 17:47:53 -0700692def ExtractPerlAsmFromCMakeFile(cmakefile):
693 """Parses the contents of the CMakeLists.txt file passed as an argument and
694 returns a list of all the perlasm() directives found in the file."""
695 perlasms = []
696 with open(cmakefile) as f:
697 for line in f:
698 line = line.strip()
699 if not line.startswith('perlasm('):
700 continue
701 if not line.endswith(')'):
702 raise ValueError('Bad perlasm line in %s' % cmakefile)
703 # Remove "perlasm(" from start and ")" from end
704 params = line[8:-1].split()
David Benjamin19676212023-01-25 10:03:53 -0500705 if len(params) != 4:
Adam Langley9e1a6602015-05-05 17:47:53 -0700706 raise ValueError('Bad perlasm line in %s' % cmakefile)
707 perlasms.append({
David Benjamin19676212023-01-25 10:03:53 -0500708 'arch': params[1],
709 'output': os.path.join(os.path.dirname(cmakefile), params[2]),
710 'input': os.path.join(os.path.dirname(cmakefile), params[3]),
Adam Langley9e1a6602015-05-05 17:47:53 -0700711 })
712
713 return perlasms
714
715
716def ReadPerlAsmOperations():
717 """Returns a list of all perlasm() directives found in CMake config files in
718 src/."""
719 perlasms = []
720 cmakefiles = FindCMakeFiles('src')
721
722 for cmakefile in cmakefiles:
723 perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
724
725 return perlasms
726
727
728def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
729 """Runs the a perlasm script and puts the output into output_filename."""
730 base_dir = os.path.dirname(output_filename)
731 if not os.path.isdir(base_dir):
732 os.makedirs(base_dir)
David Benjaminfdd8e9c2016-06-26 13:18:50 -0400733 subprocess.check_call(
734 ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
Adam Langley9e1a6602015-05-05 17:47:53 -0700735
736
Adam Langley9e1a6602015-05-05 17:47:53 -0700737def WriteAsmFiles(perlasms):
738 """Generates asm files from perlasm directives for each supported OS x
739 platform combination."""
740 asmfiles = {}
741
David Benjamin19676212023-01-25 10:03:53 -0500742 for perlasm in perlasms:
743 for (osname, arch, perlasm_style, extra_args, asm_ext) in OS_ARCH_COMBOS:
744 if arch != perlasm['arch']:
745 continue
746 # TODO(https://crbug.com/boringssl/524): Now that we incorporate osname in
747 # the output filename, the asm files can just go in a single directory.
748 # For now, we keep them in target-specific directories to avoid breaking
749 # downstream scripts.
750 key = (osname, arch)
751 outDir = '%s-%s' % key
Adam Langley9e1a6602015-05-05 17:47:53 -0700752 output = perlasm['output']
753 if not output.startswith('src'):
754 raise ValueError('output missing src: %s' % output)
755 output = os.path.join(outDir, output[4:])
David Benjamin19676212023-01-25 10:03:53 -0500756 output = '%s-%s.%s' % (output, osname, asm_ext)
757 PerlAsm(output, perlasm['input'], perlasm_style, extra_args)
758 asmfiles.setdefault(key, []).append(output)
Adam Langley9e1a6602015-05-05 17:47:53 -0700759
Yoshisato Yanagisawa56508162021-03-23 05:34:47 +0000760 for (key, non_perl_asm_files) in NON_PERL_FILES.items():
Adam Langley9e1a6602015-05-05 17:47:53 -0700761 asmfiles.setdefault(key, []).extend(non_perl_asm_files)
762
Yoshisato Yanagisawa56508162021-03-23 05:34:47 +0000763 for files in asmfiles.values():
David Benjaminedd4c5f2020-08-19 14:46:17 -0400764 files.sort()
765
Adam Langley9e1a6602015-05-05 17:47:53 -0700766 return asmfiles
767
768
David Benjamin3ecd0a52017-05-19 15:26:18 -0400769def ExtractVariablesFromCMakeFile(cmakefile):
770 """Parses the contents of the CMakeLists.txt file passed as an argument and
771 returns a dictionary of exported source lists."""
772 variables = {}
773 in_set_command = False
774 set_command = []
775 with open(cmakefile) as f:
776 for line in f:
777 if '#' in line:
778 line = line[:line.index('#')]
779 line = line.strip()
780
781 if not in_set_command:
782 if line.startswith('set('):
783 in_set_command = True
784 set_command = []
785 elif line == ')':
786 in_set_command = False
787 if not set_command:
788 raise ValueError('Empty set command')
789 variables[set_command[0]] = set_command[1:]
790 else:
791 set_command.extend([c for c in line.split(' ') if c])
792
793 if in_set_command:
794 raise ValueError('Unfinished set command')
795 return variables
796
797
Adam Langley049ef412015-06-09 18:20:57 -0700798def main(platforms):
David Benjamin3ecd0a52017-05-19 15:26:18 -0400799 cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
Andres Erbsen5b280a82017-10-30 15:58:33 +0000800 crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
Adam Langley7f028812019-10-18 14:48:11 -0700801 FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments))
Adam Langleyfd499932017-04-04 14:21:43 -0700802 fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
Adam Langleyfeca9e52017-01-23 13:07:50 -0800803 ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
David Benjamin38d01c62016-04-21 18:47:57 -0400804 tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
Adam Langleyf11f2332016-06-30 11:56:19 -0700805 tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
Adam Langley9e1a6602015-05-05 17:47:53 -0700806
Pete Bentley44544d92019-08-15 15:01:26 +0100807 # BCM shared library C files
808 bcm_crypto_c_files = [
809 os.path.join('src', 'crypto', 'fipsmodule', 'bcm.c')
810 ]
811
Adam Langley9e1a6602015-05-05 17:47:53 -0700812 # Generate err_data.c
813 with open('err_data.c', 'w+') as err_data:
814 subprocess.check_call(['go', 'run', 'err_data_generate.go'],
815 cwd=os.path.join('src', 'crypto', 'err'),
816 stdout=err_data)
817 crypto_c_files.append('err_data.c')
David Benjaminedd4c5f2020-08-19 14:46:17 -0400818 crypto_c_files.sort()
Adam Langley9e1a6602015-05-05 17:47:53 -0700819
David Benjamin38d01c62016-04-21 18:47:57 -0400820 test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
David Benjamin3ecd0a52017-05-19 15:26:18 -0400821 NotGTestSupport)
Matt Braithwaitedfdd49c2016-06-13 17:06:48 -0700822 test_support_h_files = (
823 FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
Martin Kreichgauer8b487b72017-04-03 16:07:27 -0700824 FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
David Benjamin26073832015-05-11 20:52:48 -0400825
Adam Langley990a3232018-05-22 10:02:59 -0700826 crypto_test_files = []
827 if EMBED_TEST_DATA:
828 # Generate crypto_test_data.cc
829 with open('crypto_test_data.cc', 'w+') as out:
830 subprocess.check_call(
831 ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
832 cwd='src',
833 stdout=out)
834 crypto_test_files += ['crypto_test_data.cc']
David Benjamin3ecd0a52017-05-19 15:26:18 -0400835
Adam Langley990a3232018-05-22 10:02:59 -0700836 crypto_test_files += FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
David Benjamin96ee4a82017-07-09 23:46:47 -0400837 crypto_test_files += [
David Benjaminc3889632019-03-01 15:03:05 -0500838 'src/crypto/test/abi_test.cc',
David Benjamin3ecd0a52017-05-19 15:26:18 -0400839 'src/crypto/test/file_test_gtest.cc',
840 'src/crypto/test/gtest_main.cc',
841 ]
Adam Langley3e502c82019-10-16 09:56:38 -0700842 # urandom_test.cc is in a separate binary so that it can be test PRNG
843 # initialisation.
844 crypto_test_files = [
845 file for file in crypto_test_files
846 if not file.endswith('/urandom_test.cc')
847 ]
David Benjaminedd4c5f2020-08-19 14:46:17 -0400848 crypto_test_files.sort()
David Benjamin1d5a5702017-02-13 22:11:49 -0500849
850 ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
Robert Sloanae1e0872019-03-01 16:01:30 -0800851 ssl_test_files += [
852 'src/crypto/test/abi_test.cc',
853 'src/crypto/test/gtest_main.cc',
854 ]
David Benjaminedd4c5f2020-08-19 14:46:17 -0400855 ssl_test_files.sort()
Adam Langley9e1a6602015-05-05 17:47:53 -0700856
Adam Langley3e502c82019-10-16 09:56:38 -0700857 urandom_test_files = [
858 'src/crypto/fipsmodule/rand/urandom_test.cc',
859 ]
860
David Benjamin38d01c62016-04-21 18:47:57 -0400861 fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
862
David Benjaminedd4c5f2020-08-19 14:46:17 -0400863 ssl_h_files = FindHeaderFiles(os.path.join('src', 'include', 'openssl'),
864 SSLHeaderFiles)
Adam Langley049ef412015-06-09 18:20:57 -0700865
Adam Langleyfd499932017-04-04 14:21:43 -0700866 def NotSSLHeaderFiles(path, filename, is_dir):
867 return not SSLHeaderFiles(path, filename, is_dir)
David Benjaminedd4c5f2020-08-19 14:46:17 -0400868 crypto_h_files = FindHeaderFiles(os.path.join('src', 'include', 'openssl'),
869 NotSSLHeaderFiles)
Adam Langley049ef412015-06-09 18:20:57 -0700870
871 ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
Andres Erbsen5b280a82017-10-30 15:58:33 +0000872 crypto_internal_h_files = (
873 FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
Adam Langley7f028812019-10-18 14:48:11 -0700874 FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests))
Adam Langley049ef412015-06-09 18:20:57 -0700875
Adam Langley9e1a6602015-05-05 17:47:53 -0700876 files = {
Pete Bentley44544d92019-08-15 15:01:26 +0100877 'bcm_crypto': bcm_crypto_c_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700878 'crypto': crypto_c_files,
Adam Langley049ef412015-06-09 18:20:57 -0700879 'crypto_headers': crypto_h_files,
880 'crypto_internal_headers': crypto_internal_h_files,
David Benjaminedd4c5f2020-08-19 14:46:17 -0400881 'crypto_test': crypto_test_files,
Adam Langley990a3232018-05-22 10:02:59 -0700882 'crypto_test_data': sorted('src/' + x for x in cmake['CRYPTO_TEST_DATA']),
Adam Langleyfd499932017-04-04 14:21:43 -0700883 'fips_fragments': fips_fragments,
David Benjamin38d01c62016-04-21 18:47:57 -0400884 'fuzz': fuzz_c_files,
Adam Langleyfeca9e52017-01-23 13:07:50 -0800885 'ssl': ssl_source_files,
Adam Langley049ef412015-06-09 18:20:57 -0700886 'ssl_headers': ssl_h_files,
887 'ssl_internal_headers': ssl_internal_h_files,
David Benjaminedd4c5f2020-08-19 14:46:17 -0400888 'ssl_test': ssl_test_files,
David Benjamin38d01c62016-04-21 18:47:57 -0400889 'tool': tool_c_files,
Adam Langleyf11f2332016-06-30 11:56:19 -0700890 'tool_headers': tool_h_files,
David Benjaminc5aa8412016-07-29 17:41:58 -0400891 'test_support': test_support_c_files,
892 'test_support_headers': test_support_h_files,
David Benjaminedd4c5f2020-08-19 14:46:17 -0400893 'urandom_test': urandom_test_files,
Adam Langley9e1a6602015-05-05 17:47:53 -0700894 }
895
Yoshisato Yanagisawa56508162021-03-23 05:34:47 +0000896 asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).items())
Adam Langley9e1a6602015-05-05 17:47:53 -0700897
Adam Langley049ef412015-06-09 18:20:57 -0700898 for platform in platforms:
899 platform.WriteFiles(files, asm_outputs)
Adam Langley9e1a6602015-05-05 17:47:53 -0700900
901 return 0
902
David Benjamin8c0a6eb2020-07-16 14:45:51 -0400903ALL_PLATFORMS = {
904 'android': Android,
905 'android-cmake': AndroidCMake,
906 'bazel': Bazel,
907 'cmake': CMake,
908 'eureka': Eureka,
909 'gn': GN,
910 'gyp': GYP,
911 'json': JSON,
912}
Adam Langley9e1a6602015-05-05 17:47:53 -0700913
Adam Langley9e1a6602015-05-05 17:47:53 -0700914if __name__ == '__main__':
David Benjamin4acc7dd2022-11-19 10:13:49 -0500915 parser = optparse.OptionParser(
916 usage='Usage: %%prog [--prefix=<path>] [all|%s]' %
917 '|'.join(sorted(ALL_PLATFORMS.keys())))
Matt Braithwaite16695892016-06-09 09:34:11 -0700918 parser.add_option('--prefix', dest='prefix',
919 help='For Bazel, prepend argument to all source files')
Adam Langley990a3232018-05-22 10:02:59 -0700920 parser.add_option(
921 '--embed_test_data', type='choice', dest='embed_test_data',
922 action='store', default="true", choices=["true", "false"],
David Benjaminf014d602019-05-07 18:58:06 -0500923 help='For Bazel or GN, don\'t embed data files in crypto_test_data.cc')
Matt Braithwaite16695892016-06-09 09:34:11 -0700924 options, args = parser.parse_args(sys.argv[1:])
925 PREFIX = options.prefix
Adam Langley990a3232018-05-22 10:02:59 -0700926 EMBED_TEST_DATA = (options.embed_test_data == "true")
Matt Braithwaite16695892016-06-09 09:34:11 -0700927
928 if not args:
929 parser.print_help()
930 sys.exit(1)
Adam Langley9e1a6602015-05-05 17:47:53 -0700931
David Benjamin4acc7dd2022-11-19 10:13:49 -0500932 if 'all' in args:
933 platforms = [platform() for platform in ALL_PLATFORMS.values()]
934 else:
935 platforms = []
936 for s in args:
937 platform = ALL_PLATFORMS.get(s)
938 if platform is None:
939 parser.print_help()
940 sys.exit(1)
941 platforms.append(platform())
Adam Langley9e1a6602015-05-05 17:47:53 -0700942
Adam Langley049ef412015-06-09 18:20:57 -0700943 sys.exit(main(platforms))