generate_build_files.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. # 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. """Enumerates source files for consumption by various build systems."""
  15. import optparse
  16. import os
  17. import subprocess
  18. import sys
  19. import json
  20. # OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for
  21. # that platform and the extension used by asm files.
  22. OS_ARCH_COMBOS = [
  23. ('ios', 'arm', 'ios32', [], 'S'),
  24. ('ios', 'aarch64', 'ios64', [], 'S'),
  25. ('linux', 'arm', 'linux32', [], 'S'),
  26. ('linux', 'aarch64', 'linux64', [], 'S'),
  27. ('linux', 'ppc64le', 'linux64le', [], 'S'),
  28. ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
  29. ('linux', 'x86_64', 'elf', [], 'S'),
  30. ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
  31. ('mac', 'x86_64', 'macosx', [], 'S'),
  32. ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
  33. ('win', 'x86_64', 'nasm', [], 'asm'),
  34. ]
  35. # NON_PERL_FILES enumerates assembly files that are not processed by the
  36. # perlasm system.
  37. NON_PERL_FILES = {
  38. ('linux', 'arm'): [
  39. 'src/crypto/curve25519/asm/x25519-asm-arm.S',
  40. 'src/crypto/poly1305/poly1305_arm_asm.S',
  41. ],
  42. ('linux', 'x86_64'): [
  43. 'src/crypto/hrss/asm/poly_rq_mul.S',
  44. ],
  45. }
  46. PREFIX = None
  47. EMBED_TEST_DATA = True
  48. def PathOf(x):
  49. return x if not PREFIX else os.path.join(PREFIX, x)
  50. class Android(object):
  51. def __init__(self):
  52. self.header = \
  53. """# Copyright (C) 2015 The Android Open Source Project
  54. #
  55. # Licensed under the Apache License, Version 2.0 (the "License");
  56. # you may not use this file except in compliance with the License.
  57. # You may obtain a copy of the License at
  58. #
  59. # http://www.apache.org/licenses/LICENSE-2.0
  60. #
  61. # Unless required by applicable law or agreed to in writing, software
  62. # distributed under the License is distributed on an "AS IS" BASIS,
  63. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  64. # See the License for the specific language governing permissions and
  65. # limitations under the License.
  66. # This file is created by generate_build_files.py. Do not edit manually.
  67. """
  68. def PrintVariableSection(self, out, name, files):
  69. out.write('%s := \\\n' % name)
  70. for f in sorted(files):
  71. out.write(' %s\\\n' % f)
  72. out.write('\n')
  73. def WriteFiles(self, files, asm_outputs):
  74. # New Android.bp format
  75. with open('sources.bp', 'w+') as blueprint:
  76. blueprint.write(self.header.replace('#', '//'))
  77. # Separate out BCM files to allow different compilation rules (specific to Android FIPS)
  78. bcm_c_files = files['bcm_crypto']
  79. non_bcm_c_files = [file for file in files['crypto'] if file not in bcm_c_files]
  80. non_bcm_asm = self.FilterBcmAsm(asm_outputs, False)
  81. bcm_asm = self.FilterBcmAsm(asm_outputs, True)
  82. self.PrintDefaults(blueprint, 'libcrypto_sources', non_bcm_c_files, non_bcm_asm)
  83. self.PrintDefaults(blueprint, 'libcrypto_bcm_sources', bcm_c_files, bcm_asm)
  84. self.PrintDefaults(blueprint, 'libssl_sources', files['ssl'])
  85. self.PrintDefaults(blueprint, 'bssl_sources', files['tool'])
  86. self.PrintDefaults(blueprint, 'boringssl_test_support_sources', files['test_support'])
  87. self.PrintDefaults(blueprint, 'boringssl_crypto_test_sources', files['crypto_test'])
  88. self.PrintDefaults(blueprint, 'boringssl_ssl_test_sources', files['ssl_test'])
  89. # Legacy Android.mk format, only used by Trusty in new branches
  90. with open('sources.mk', 'w+') as makefile:
  91. makefile.write(self.header)
  92. makefile.write('\n')
  93. self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
  94. for ((osname, arch), asm_files) in asm_outputs:
  95. if osname != 'linux':
  96. continue
  97. self.PrintVariableSection(
  98. makefile, '%s_%s_sources' % (osname, arch), asm_files)
  99. def PrintDefaults(self, blueprint, name, files, asm_outputs={}):
  100. """Print a cc_defaults section from a list of C files and optionally assembly outputs"""
  101. blueprint.write('\n')
  102. blueprint.write('cc_defaults {\n')
  103. blueprint.write(' name: "%s",\n' % name)
  104. blueprint.write(' srcs: [\n')
  105. for f in sorted(files):
  106. blueprint.write(' "%s",\n' % f)
  107. blueprint.write(' ],\n')
  108. if asm_outputs:
  109. blueprint.write(' target: {\n')
  110. for ((osname, arch), asm_files) in asm_outputs:
  111. if osname != 'linux' or arch == 'ppc64le':
  112. continue
  113. if arch == 'aarch64':
  114. arch = 'arm64'
  115. blueprint.write(' linux_%s: {\n' % arch)
  116. blueprint.write(' srcs: [\n')
  117. for f in sorted(asm_files):
  118. blueprint.write(' "%s",\n' % f)
  119. blueprint.write(' ],\n')
  120. blueprint.write(' },\n')
  121. blueprint.write(' },\n')
  122. blueprint.write('}\n')
  123. def FilterBcmAsm(self, asm, want_bcm):
  124. """Filter a list of assembly outputs based on whether they belong in BCM
  125. Args:
  126. asm: Assembly file lists to filter
  127. want_bcm: If true then include BCM files, otherwise do not
  128. Returns:
  129. A copy of |asm| with files filtered according to |want_bcm|
  130. """
  131. return [(archinfo, filter(lambda p: ("/crypto/fipsmodule/" in p) == want_bcm, files))
  132. for (archinfo, files) in asm]
  133. class AndroidCMake(object):
  134. def __init__(self):
  135. self.header = \
  136. """# Copyright (C) 2019 The Android Open Source Project
  137. #
  138. # Licensed under the Apache License, Version 2.0 (the "License");
  139. # you may not use this file except in compliance with the License.
  140. # You may obtain a copy of the License at
  141. #
  142. # http://www.apache.org/licenses/LICENSE-2.0
  143. #
  144. # Unless required by applicable law or agreed to in writing, software
  145. # distributed under the License is distributed on an "AS IS" BASIS,
  146. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  147. # See the License for the specific language governing permissions and
  148. # limitations under the License.
  149. # This file is created by generate_build_files.py. Do not edit manually.
  150. # To specify a custom path prefix, set BORINGSSL_ROOT before including this
  151. # file, or use list(TRANSFORM ... PREPEND) from CMake 3.12.
  152. """
  153. def PrintVariableSection(self, out, name, files):
  154. out.write('set(%s\n' % name)
  155. for f in sorted(files):
  156. # Ideally adding the prefix would be the caller's job, but
  157. # list(TRANSFORM ... PREPEND) is only available starting CMake 3.12. When
  158. # sources.cmake is the source of truth, we can ask Android to either write
  159. # a CMake function or update to 3.12.
  160. out.write(' ${BORINGSSL_ROOT}%s\n' % f)
  161. out.write(')\n')
  162. def WriteFiles(self, files, asm_outputs):
  163. # The Android emulator uses a custom CMake buildsystem.
  164. #
  165. # TODO(davidben): Move our various source lists into sources.cmake and have
  166. # Android consume that directly.
  167. with open('android-sources.cmake', 'w+') as out:
  168. out.write(self.header)
  169. self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
  170. self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
  171. self.PrintVariableSection(out, 'tool_sources', files['tool'])
  172. self.PrintVariableSection(out, 'test_support_sources',
  173. files['test_support'])
  174. self.PrintVariableSection(out, 'crypto_test_sources',
  175. files['crypto_test'])
  176. self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
  177. for ((osname, arch), asm_files) in asm_outputs:
  178. self.PrintVariableSection(
  179. out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
  180. class Bazel(object):
  181. """Bazel outputs files suitable for including in Bazel files."""
  182. def __init__(self):
  183. self.firstSection = True
  184. self.header = \
  185. """# This file is created by generate_build_files.py. Do not edit manually.
  186. """
  187. def PrintVariableSection(self, out, name, files):
  188. if not self.firstSection:
  189. out.write('\n')
  190. self.firstSection = False
  191. out.write('%s = [\n' % name)
  192. for f in sorted(files):
  193. out.write(' "%s",\n' % PathOf(f))
  194. out.write(']\n')
  195. def WriteFiles(self, files, asm_outputs):
  196. with open('BUILD.generated.bzl', 'w+') as out:
  197. out.write(self.header)
  198. self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
  199. self.PrintVariableSection(out, 'fips_fragments', files['fips_fragments'])
  200. self.PrintVariableSection(
  201. out, 'ssl_internal_headers', files['ssl_internal_headers'])
  202. self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
  203. self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
  204. self.PrintVariableSection(
  205. out, 'crypto_internal_headers', files['crypto_internal_headers'])
  206. self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
  207. self.PrintVariableSection(out, 'tool_sources', files['tool'])
  208. self.PrintVariableSection(out, 'tool_headers', files['tool_headers'])
  209. for ((osname, arch), asm_files) in asm_outputs:
  210. self.PrintVariableSection(
  211. out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
  212. with open('BUILD.generated_tests.bzl', 'w+') as out:
  213. out.write(self.header)
  214. out.write('test_support_sources = [\n')
  215. for filename in sorted(files['test_support'] +
  216. files['test_support_headers'] +
  217. files['crypto_internal_headers'] +
  218. files['ssl_internal_headers']):
  219. if os.path.basename(filename) == 'malloc.cc':
  220. continue
  221. out.write(' "%s",\n' % PathOf(filename))
  222. out.write(']\n')
  223. self.PrintVariableSection(out, 'crypto_test_sources',
  224. files['crypto_test'])
  225. self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
  226. self.PrintVariableSection(out, 'crypto_test_data',
  227. files['crypto_test_data'])
  228. class Eureka(object):
  229. def __init__(self):
  230. self.header = \
  231. """# Copyright (C) 2017 The Android Open Source Project
  232. #
  233. # Licensed under the Apache License, Version 2.0 (the "License");
  234. # you may not use this file except in compliance with the License.
  235. # You may obtain a copy of the License at
  236. #
  237. # http://www.apache.org/licenses/LICENSE-2.0
  238. #
  239. # Unless required by applicable law or agreed to in writing, software
  240. # distributed under the License is distributed on an "AS IS" BASIS,
  241. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  242. # See the License for the specific language governing permissions and
  243. # limitations under the License.
  244. # This file is created by generate_build_files.py. Do not edit manually.
  245. """
  246. def PrintVariableSection(self, out, name, files):
  247. out.write('%s := \\\n' % name)
  248. for f in sorted(files):
  249. out.write(' %s\\\n' % f)
  250. out.write('\n')
  251. def WriteFiles(self, files, asm_outputs):
  252. # Legacy Android.mk format
  253. with open('eureka.mk', 'w+') as makefile:
  254. makefile.write(self.header)
  255. self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
  256. self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
  257. self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
  258. for ((osname, arch), asm_files) in asm_outputs:
  259. if osname != 'linux':
  260. continue
  261. self.PrintVariableSection(
  262. makefile, '%s_%s_sources' % (osname, arch), asm_files)
  263. class GN(object):
  264. def __init__(self):
  265. self.firstSection = True
  266. self.header = \
  267. """# Copyright (c) 2016 The Chromium Authors. All rights reserved.
  268. # Use of this source code is governed by a BSD-style license that can be
  269. # found in the LICENSE file.
  270. # This file is created by generate_build_files.py. Do not edit manually.
  271. """
  272. def PrintVariableSection(self, out, name, files):
  273. if not self.firstSection:
  274. out.write('\n')
  275. self.firstSection = False
  276. out.write('%s = [\n' % name)
  277. for f in sorted(files):
  278. out.write(' "%s",\n' % f)
  279. out.write(']\n')
  280. def WriteFiles(self, files, asm_outputs):
  281. with open('BUILD.generated.gni', 'w+') as out:
  282. out.write(self.header)
  283. self.PrintVariableSection(out, 'crypto_sources',
  284. files['crypto'] +
  285. files['crypto_internal_headers'])
  286. self.PrintVariableSection(out, 'crypto_headers',
  287. files['crypto_headers'])
  288. self.PrintVariableSection(out, 'ssl_sources',
  289. files['ssl'] + files['ssl_internal_headers'])
  290. self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
  291. for ((osname, arch), asm_files) in asm_outputs:
  292. self.PrintVariableSection(
  293. out, 'crypto_sources_%s_%s' % (osname, arch), asm_files)
  294. fuzzers = [os.path.splitext(os.path.basename(fuzzer))[0]
  295. for fuzzer in files['fuzz']]
  296. self.PrintVariableSection(out, 'fuzzers', fuzzers)
  297. with open('BUILD.generated_tests.gni', 'w+') as out:
  298. self.firstSection = True
  299. out.write(self.header)
  300. self.PrintVariableSection(out, 'test_support_sources',
  301. files['test_support'] +
  302. files['test_support_headers'])
  303. self.PrintVariableSection(out, 'crypto_test_sources',
  304. files['crypto_test'])
  305. self.PrintVariableSection(out, 'crypto_test_data',
  306. files['crypto_test_data'])
  307. self.PrintVariableSection(out, 'ssl_test_sources', files['ssl_test'])
  308. class GYP(object):
  309. def __init__(self):
  310. self.header = \
  311. """# Copyright (c) 2016 The Chromium Authors. All rights reserved.
  312. # Use of this source code is governed by a BSD-style license that can be
  313. # found in the LICENSE file.
  314. # This file is created by generate_build_files.py. Do not edit manually.
  315. """
  316. def PrintVariableSection(self, out, name, files):
  317. out.write(' \'%s\': [\n' % name)
  318. for f in sorted(files):
  319. out.write(' \'%s\',\n' % f)
  320. out.write(' ],\n')
  321. def WriteFiles(self, files, asm_outputs):
  322. with open('boringssl.gypi', 'w+') as gypi:
  323. gypi.write(self.header + '{\n \'variables\': {\n')
  324. self.PrintVariableSection(gypi, 'boringssl_ssl_sources',
  325. files['ssl'] + files['ssl_headers'] +
  326. files['ssl_internal_headers'])
  327. self.PrintVariableSection(gypi, 'boringssl_crypto_sources',
  328. files['crypto'] + files['crypto_headers'] +
  329. files['crypto_internal_headers'])
  330. for ((osname, arch), asm_files) in asm_outputs:
  331. self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
  332. (osname, arch), asm_files)
  333. gypi.write(' }\n}\n')
  334. def FindCMakeFiles(directory):
  335. """Returns list of all CMakeLists.txt files recursively in directory."""
  336. cmakefiles = []
  337. for (path, _, filenames) in os.walk(directory):
  338. for filename in filenames:
  339. if filename == 'CMakeLists.txt':
  340. cmakefiles.append(os.path.join(path, filename))
  341. return cmakefiles
  342. def OnlyFIPSFragments(path, dent, is_dir):
  343. return is_dir or (path.startswith(
  344. os.path.join('src', 'crypto', 'fipsmodule', '')) and
  345. NoTests(path, dent, is_dir))
  346. def NoTestsNorFIPSFragments(path, dent, is_dir):
  347. return (NoTests(path, dent, is_dir) and
  348. (is_dir or not OnlyFIPSFragments(path, dent, is_dir)))
  349. def NoTests(path, dent, is_dir):
  350. """Filter function that can be passed to FindCFiles in order to remove test
  351. sources."""
  352. if is_dir:
  353. return dent != 'test'
  354. return 'test.' not in dent
  355. def OnlyTests(path, dent, is_dir):
  356. """Filter function that can be passed to FindCFiles in order to remove
  357. non-test sources."""
  358. if is_dir:
  359. return dent != 'test'
  360. return '_test.' in dent
  361. def AllFiles(path, dent, is_dir):
  362. """Filter function that can be passed to FindCFiles in order to include all
  363. sources."""
  364. return True
  365. def NoTestRunnerFiles(path, dent, is_dir):
  366. """Filter function that can be passed to FindCFiles or FindHeaderFiles in
  367. order to exclude test runner files."""
  368. # NOTE(martinkr): This prevents .h/.cc files in src/ssl/test/runner, which
  369. # are in their own subpackage, from being included in boringssl/BUILD files.
  370. return not is_dir or dent != 'runner'
  371. def NotGTestSupport(path, dent, is_dir):
  372. return 'gtest' not in dent and 'abi_test' not in dent
  373. def SSLHeaderFiles(path, dent, is_dir):
  374. return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h', 'srtp.h']
  375. def FindCFiles(directory, filter_func):
  376. """Recurses through directory and returns a list of paths to all the C source
  377. files that pass filter_func."""
  378. cfiles = []
  379. for (path, dirnames, filenames) in os.walk(directory):
  380. for filename in filenames:
  381. if not filename.endswith('.c') and not filename.endswith('.cc'):
  382. continue
  383. if not filter_func(path, filename, False):
  384. continue
  385. cfiles.append(os.path.join(path, filename))
  386. for (i, dirname) in enumerate(dirnames):
  387. if not filter_func(path, dirname, True):
  388. del dirnames[i]
  389. return cfiles
  390. def FindHeaderFiles(directory, filter_func):
  391. """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
  392. hfiles = []
  393. for (path, dirnames, filenames) in os.walk(directory):
  394. for filename in filenames:
  395. if not filename.endswith('.h'):
  396. continue
  397. if not filter_func(path, filename, False):
  398. continue
  399. hfiles.append(os.path.join(path, filename))
  400. for (i, dirname) in enumerate(dirnames):
  401. if not filter_func(path, dirname, True):
  402. del dirnames[i]
  403. return hfiles
  404. def ExtractPerlAsmFromCMakeFile(cmakefile):
  405. """Parses the contents of the CMakeLists.txt file passed as an argument and
  406. returns a list of all the perlasm() directives found in the file."""
  407. perlasms = []
  408. with open(cmakefile) as f:
  409. for line in f:
  410. line = line.strip()
  411. if not line.startswith('perlasm('):
  412. continue
  413. if not line.endswith(')'):
  414. raise ValueError('Bad perlasm line in %s' % cmakefile)
  415. # Remove "perlasm(" from start and ")" from end
  416. params = line[8:-1].split()
  417. if len(params) < 2:
  418. raise ValueError('Bad perlasm line in %s' % cmakefile)
  419. perlasms.append({
  420. 'extra_args': params[2:],
  421. 'input': os.path.join(os.path.dirname(cmakefile), params[1]),
  422. 'output': os.path.join(os.path.dirname(cmakefile), params[0]),
  423. })
  424. return perlasms
  425. def ReadPerlAsmOperations():
  426. """Returns a list of all perlasm() directives found in CMake config files in
  427. src/."""
  428. perlasms = []
  429. cmakefiles = FindCMakeFiles('src')
  430. for cmakefile in cmakefiles:
  431. perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
  432. return perlasms
  433. def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
  434. """Runs the a perlasm script and puts the output into output_filename."""
  435. base_dir = os.path.dirname(output_filename)
  436. if not os.path.isdir(base_dir):
  437. os.makedirs(base_dir)
  438. subprocess.check_call(
  439. ['perl', input_filename, perlasm_style] + extra_args + [output_filename])
  440. def ArchForAsmFilename(filename):
  441. """Returns the architectures that a given asm file should be compiled for
  442. based on substrings in the filename."""
  443. if 'x86_64' in filename or 'avx2' in filename:
  444. return ['x86_64']
  445. elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
  446. return ['x86']
  447. elif 'armx' in filename:
  448. return ['arm', 'aarch64']
  449. elif 'armv8' in filename:
  450. return ['aarch64']
  451. elif 'arm' in filename:
  452. return ['arm']
  453. elif 'ppc' in filename:
  454. return ['ppc64le']
  455. else:
  456. raise ValueError('Unknown arch for asm filename: ' + filename)
  457. def WriteAsmFiles(perlasms):
  458. """Generates asm files from perlasm directives for each supported OS x
  459. platform combination."""
  460. asmfiles = {}
  461. for osarch in OS_ARCH_COMBOS:
  462. (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
  463. key = (osname, arch)
  464. outDir = '%s-%s' % key
  465. for perlasm in perlasms:
  466. filename = os.path.basename(perlasm['input'])
  467. output = perlasm['output']
  468. if not output.startswith('src'):
  469. raise ValueError('output missing src: %s' % output)
  470. output = os.path.join(outDir, output[4:])
  471. if output.endswith('-armx.${ASM_EXT}'):
  472. output = output.replace('-armx',
  473. '-armx64' if arch == 'aarch64' else '-armx32')
  474. output = output.replace('${ASM_EXT}', asm_ext)
  475. if arch in ArchForAsmFilename(filename):
  476. PerlAsm(output, perlasm['input'], perlasm_style,
  477. perlasm['extra_args'] + extra_args)
  478. asmfiles.setdefault(key, []).append(output)
  479. for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
  480. asmfiles.setdefault(key, []).extend(non_perl_asm_files)
  481. return asmfiles
  482. def ExtractVariablesFromCMakeFile(cmakefile):
  483. """Parses the contents of the CMakeLists.txt file passed as an argument and
  484. returns a dictionary of exported source lists."""
  485. variables = {}
  486. in_set_command = False
  487. set_command = []
  488. with open(cmakefile) as f:
  489. for line in f:
  490. if '#' in line:
  491. line = line[:line.index('#')]
  492. line = line.strip()
  493. if not in_set_command:
  494. if line.startswith('set('):
  495. in_set_command = True
  496. set_command = []
  497. elif line == ')':
  498. in_set_command = False
  499. if not set_command:
  500. raise ValueError('Empty set command')
  501. variables[set_command[0]] = set_command[1:]
  502. else:
  503. set_command.extend([c for c in line.split(' ') if c])
  504. if in_set_command:
  505. raise ValueError('Unfinished set command')
  506. return variables
  507. def main(platforms):
  508. cmake = ExtractVariablesFromCMakeFile(os.path.join('src', 'sources.cmake'))
  509. crypto_c_files = (FindCFiles(os.path.join('src', 'crypto'), NoTestsNorFIPSFragments) +
  510. FindCFiles(os.path.join('src', 'third_party', 'fiat'), NoTestsNorFIPSFragments) +
  511. FindCFiles(os.path.join('src', 'third_party', 'sike'), NoTestsNorFIPSFragments))
  512. fips_fragments = FindCFiles(os.path.join('src', 'crypto', 'fipsmodule'), OnlyFIPSFragments)
  513. ssl_source_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
  514. tool_c_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
  515. tool_h_files = FindHeaderFiles(os.path.join('src', 'tool'), AllFiles)
  516. # third_party/fiat/p256.c lives in third_party/fiat, but it is a FIPS
  517. # fragment, not a normal source file.
  518. p256 = os.path.join('src', 'third_party', 'fiat', 'p256.c')
  519. fips_fragments.append(p256)
  520. crypto_c_files.remove(p256)
  521. # BCM shared library C files
  522. bcm_crypto_c_files = [
  523. os.path.join('src', 'crypto', 'fipsmodule', 'bcm.c')
  524. ]
  525. # Generate err_data.c
  526. with open('err_data.c', 'w+') as err_data:
  527. subprocess.check_call(['go', 'run', 'err_data_generate.go'],
  528. cwd=os.path.join('src', 'crypto', 'err'),
  529. stdout=err_data)
  530. crypto_c_files.append('err_data.c')
  531. test_support_c_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
  532. NotGTestSupport)
  533. test_support_h_files = (
  534. FindHeaderFiles(os.path.join('src', 'crypto', 'test'), AllFiles) +
  535. FindHeaderFiles(os.path.join('src', 'ssl', 'test'), NoTestRunnerFiles))
  536. crypto_test_files = []
  537. if EMBED_TEST_DATA:
  538. # Generate crypto_test_data.cc
  539. with open('crypto_test_data.cc', 'w+') as out:
  540. subprocess.check_call(
  541. ['go', 'run', 'util/embed_test_data.go'] + cmake['CRYPTO_TEST_DATA'],
  542. cwd='src',
  543. stdout=out)
  544. crypto_test_files += ['crypto_test_data.cc']
  545. crypto_test_files += FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
  546. crypto_test_files += [
  547. 'src/crypto/test/abi_test.cc',
  548. 'src/crypto/test/file_test_gtest.cc',
  549. 'src/crypto/test/gtest_main.cc',
  550. ]
  551. ssl_test_files = FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
  552. ssl_test_files += [
  553. 'src/crypto/test/abi_test.cc',
  554. 'src/crypto/test/gtest_main.cc',
  555. ]
  556. fuzz_c_files = FindCFiles(os.path.join('src', 'fuzz'), NoTests)
  557. ssl_h_files = (
  558. FindHeaderFiles(
  559. os.path.join('src', 'include', 'openssl'),
  560. SSLHeaderFiles))
  561. def NotSSLHeaderFiles(path, filename, is_dir):
  562. return not SSLHeaderFiles(path, filename, is_dir)
  563. crypto_h_files = (
  564. FindHeaderFiles(
  565. os.path.join('src', 'include', 'openssl'),
  566. NotSSLHeaderFiles))
  567. ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
  568. crypto_internal_h_files = (
  569. FindHeaderFiles(os.path.join('src', 'crypto'), NoTests) +
  570. FindHeaderFiles(os.path.join('src', 'third_party', 'fiat'), NoTests) +
  571. FindHeaderFiles(os.path.join('src', 'third_party', 'sike'), NoTests))
  572. files = {
  573. 'bcm_crypto': bcm_crypto_c_files,
  574. 'crypto': crypto_c_files,
  575. 'crypto_headers': crypto_h_files,
  576. 'crypto_internal_headers': crypto_internal_h_files,
  577. 'crypto_test': sorted(crypto_test_files),
  578. 'crypto_test_data': sorted('src/' + x for x in cmake['CRYPTO_TEST_DATA']),
  579. 'fips_fragments': fips_fragments,
  580. 'fuzz': fuzz_c_files,
  581. 'ssl': ssl_source_files,
  582. 'ssl_headers': ssl_h_files,
  583. 'ssl_internal_headers': ssl_internal_h_files,
  584. 'ssl_test': sorted(ssl_test_files),
  585. 'tool': tool_c_files,
  586. 'tool_headers': tool_h_files,
  587. 'test_support': test_support_c_files,
  588. 'test_support_headers': test_support_h_files,
  589. }
  590. asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
  591. for platform in platforms:
  592. platform.WriteFiles(files, asm_outputs)
  593. return 0
  594. if __name__ == '__main__':
  595. parser = optparse.OptionParser(usage='Usage: %prog [--prefix=<path>]'
  596. ' [android|android-cmake|bazel|eureka|gn|gyp]')
  597. parser.add_option('--prefix', dest='prefix',
  598. help='For Bazel, prepend argument to all source files')
  599. parser.add_option(
  600. '--embed_test_data', type='choice', dest='embed_test_data',
  601. action='store', default="true", choices=["true", "false"],
  602. help='For Bazel or GN, don\'t embed data files in crypto_test_data.cc')
  603. options, args = parser.parse_args(sys.argv[1:])
  604. PREFIX = options.prefix
  605. EMBED_TEST_DATA = (options.embed_test_data == "true")
  606. if not args:
  607. parser.print_help()
  608. sys.exit(1)
  609. platforms = []
  610. for s in args:
  611. if s == 'android':
  612. platforms.append(Android())
  613. elif s == 'android-cmake':
  614. platforms.append(AndroidCMake())
  615. elif s == 'bazel':
  616. platforms.append(Bazel())
  617. elif s == 'eureka':
  618. platforms.append(Eureka())
  619. elif s == 'gn':
  620. platforms.append(GN())
  621. elif s == 'gyp':
  622. platforms.append(GYP())
  623. else:
  624. parser.print_help()
  625. sys.exit(1)
  626. sys.exit(main(platforms))