• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15"""Enumerates the BoringSSL source in src/ and either generates two gypi files
16  (boringssl.gypi and boringssl_tests.gypi) for Chromium, or generates
17  source-list files for Android."""
18
19import os
20import subprocess
21import sys
22
23
24# OS_ARCH_COMBOS maps from OS and platform to the OpenSSL assembly "style" for
25# that platform and the extension used by asm files.
26OS_ARCH_COMBOS = [
27    ('linux', 'arm', 'linux32', [], 'S'),
28    ('linux', 'aarch64', 'linux64', [], 'S'),
29    ('linux', 'x86', 'elf', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
30    ('linux', 'x86_64', 'elf', [], 'S'),
31    ('mac', 'x86', 'macosx', ['-fPIC', '-DOPENSSL_IA32_SSE2'], 'S'),
32    ('mac', 'x86_64', 'macosx', [], 'S'),
33    ('win', 'x86', 'win32n', ['-DOPENSSL_IA32_SSE2'], 'asm'),
34    ('win', 'x86_64', 'nasm', [], 'asm'),
35]
36
37# NON_PERL_FILES enumerates assembly files that are not processed by the
38# perlasm system.
39NON_PERL_FILES = {
40    ('linux', 'arm'): [
41        'src/crypto/poly1305/poly1305_arm_asm.S',
42        'src/crypto/chacha/chacha_vec_arm.S',
43        'src/crypto/cpu-arm-asm.S',
44    ],
45}
46
47
48class Chromium(object):
49
50  def __init__(self):
51    self.header = \
52"""# Copyright (c) 2014 The Chromium Authors. All rights reserved.
53# Use of this source code is governed by a BSD-style license that can be
54# found in the LICENSE file.
55
56# This file is created by generate_build_files.py. Do not edit manually.
57
58"""
59
60  def PrintVariableSection(self, out, name, files):
61    out.write('    \'%s\': [\n' % name)
62    for f in sorted(files):
63      out.write('      \'%s\',\n' % f)
64    out.write('    ],\n')
65
66  def WriteFiles(self, files, asm_outputs):
67    with open('boringssl.gypi', 'w+') as gypi:
68      gypi.write(self.header + '{\n  \'variables\': {\n')
69
70      self.PrintVariableSection(
71          gypi, 'boringssl_lib_sources', files['crypto'] + files['ssl'])
72
73      for ((osname, arch), asm_files) in asm_outputs:
74        self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
75                                  (osname, arch), asm_files)
76
77      gypi.write('  }\n}\n')
78
79    with open('boringssl_tests.gypi', 'w+') as test_gypi:
80      test_gypi.write(self.header + '{\n  \'targets\': [\n')
81
82      test_names = []
83      for test in sorted(files['test']):
84        test_name = 'boringssl_%s' % os.path.splitext(os.path.basename(test))[0]
85        test_gypi.write("""    {
86      'target_name': '%s',
87      'type': 'executable',
88      'dependencies': [
89        'boringssl.gyp:boringssl',
90      ],
91      'sources': [
92        '%s',
93        '<@(boringssl_test_support_sources)',
94      ],
95      # TODO(davidben): Fix size_t truncations in BoringSSL.
96      # https://crbug.com/429039
97      'msvs_disabled_warnings': [ 4267, ],
98    },\n""" % (test_name, test))
99        test_names.append(test_name)
100
101      test_names.sort()
102
103      test_gypi.write('  ],\n  \'variables\': {\n')
104
105      self.PrintVariableSection(
106          test_gypi, 'boringssl_test_support_sources', files['test_support'])
107
108      test_gypi.write('    \'boringssl_test_targets\': [\n')
109
110      for test in test_names:
111        test_gypi.write("""      '%s',\n""" % test)
112
113      test_gypi.write('    ],\n  }\n}\n')
114
115
116class Android(object):
117
118  def __init__(self):
119    self.header = \
120"""# Copyright (C) 2015 The Android Open Source Project
121#
122# Licensed under the Apache License, Version 2.0 (the "License");
123# you may not use this file except in compliance with the License.
124# You may obtain a copy of the License at
125#
126#      http://www.apache.org/licenses/LICENSE-2.0
127#
128# Unless required by applicable law or agreed to in writing, software
129# distributed under the License is distributed on an "AS IS" BASIS,
130# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131# See the License for the specific language governing permissions and
132# limitations under the License.
133
134"""
135
136  def PrintVariableSection(self, out, name, files):
137    out.write('%s := \\\n' % name)
138    for f in sorted(files):
139      out.write('  %s\\\n' % f)
140    out.write('\n')
141
142  def WriteFiles(self, files, asm_outputs):
143    with open('sources.mk', 'w+') as makefile:
144      makefile.write(self.header)
145
146      files['crypto'].append('android_compat_hacks.c')
147      files['crypto'].append('android_compat_keywrap.c')
148      self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
149      self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
150      self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
151
152      for ((osname, arch), asm_files) in asm_outputs:
153        self.PrintVariableSection(
154            makefile, '%s_%s_sources' % (osname, arch), asm_files)
155
156
157def FindCMakeFiles(directory):
158  """Returns list of all CMakeLists.txt files recursively in directory."""
159  cmakefiles = []
160
161  for (path, _, filenames) in os.walk(directory):
162    for filename in filenames:
163      if filename == 'CMakeLists.txt':
164        cmakefiles.append(os.path.join(path, filename))
165
166  return cmakefiles
167
168
169def NoTests(dent, is_dir):
170  """Filter function that can be passed to FindCFiles in order to remove test
171  sources."""
172  if is_dir:
173    return dent != 'test'
174  return 'test.' not in dent and not dent.startswith('example_')
175
176
177def OnlyTests(dent, is_dir):
178  """Filter function that can be passed to FindCFiles in order to remove
179  non-test sources."""
180  if is_dir:
181    return dent != 'test'
182  return '_test.' in dent or dent.startswith('example_')
183
184
185def AllFiles(dent, is_dir):
186  """Filter function that can be passed to FindCFiles in order to include all
187  sources."""
188  return True
189
190
191def FindCFiles(directory, filter_func):
192  """Recurses through directory and returns a list of paths to all the C source
193  files that pass filter_func."""
194  cfiles = []
195
196  for (path, dirnames, filenames) in os.walk(directory):
197    for filename in filenames:
198      if not filename.endswith('.c') and not filename.endswith('.cc'):
199        continue
200      if not filter_func(filename, False):
201        continue
202      cfiles.append(os.path.join(path, filename))
203
204    for (i, dirname) in enumerate(dirnames):
205      if not filter_func(dirname, True):
206        del dirnames[i]
207
208  return cfiles
209
210
211def ExtractPerlAsmFromCMakeFile(cmakefile):
212  """Parses the contents of the CMakeLists.txt file passed as an argument and
213  returns a list of all the perlasm() directives found in the file."""
214  perlasms = []
215  with open(cmakefile) as f:
216    for line in f:
217      line = line.strip()
218      if not line.startswith('perlasm('):
219        continue
220      if not line.endswith(')'):
221        raise ValueError('Bad perlasm line in %s' % cmakefile)
222      # Remove "perlasm(" from start and ")" from end
223      params = line[8:-1].split()
224      if len(params) < 2:
225        raise ValueError('Bad perlasm line in %s' % cmakefile)
226      perlasms.append({
227          'extra_args': params[2:],
228          'input': os.path.join(os.path.dirname(cmakefile), params[1]),
229          'output': os.path.join(os.path.dirname(cmakefile), params[0]),
230      })
231
232  return perlasms
233
234
235def ReadPerlAsmOperations():
236  """Returns a list of all perlasm() directives found in CMake config files in
237  src/."""
238  perlasms = []
239  cmakefiles = FindCMakeFiles('src')
240
241  for cmakefile in cmakefiles:
242    perlasms.extend(ExtractPerlAsmFromCMakeFile(cmakefile))
243
244  return perlasms
245
246
247def PerlAsm(output_filename, input_filename, perlasm_style, extra_args):
248  """Runs the a perlasm script and puts the output into output_filename."""
249  base_dir = os.path.dirname(output_filename)
250  if not os.path.isdir(base_dir):
251    os.makedirs(base_dir)
252  output = subprocess.check_output(
253      ['perl', input_filename, perlasm_style] + extra_args)
254  with open(output_filename, 'w+') as out_file:
255    out_file.write(output)
256
257
258def ArchForAsmFilename(filename):
259  """Returns the architectures that a given asm file should be compiled for
260  based on substrings in the filename."""
261
262  if 'x86_64' in filename or 'avx2' in filename:
263    return ['x86_64']
264  elif ('x86' in filename and 'x86_64' not in filename) or '586' in filename:
265    return ['x86']
266  elif 'armx' in filename:
267    return ['arm', 'aarch64']
268  elif 'armv8' in filename:
269    return ['aarch64']
270  elif 'arm' in filename:
271    return ['arm']
272  else:
273    raise ValueError('Unknown arch for asm filename: ' + filename)
274
275
276def WriteAsmFiles(perlasms):
277  """Generates asm files from perlasm directives for each supported OS x
278  platform combination."""
279  asmfiles = {}
280
281  for osarch in OS_ARCH_COMBOS:
282    (osname, arch, perlasm_style, extra_args, asm_ext) = osarch
283    key = (osname, arch)
284    outDir = '%s-%s' % key
285
286    for perlasm in perlasms:
287      filename = os.path.basename(perlasm['input'])
288      output = perlasm['output']
289      if not output.startswith('src'):
290        raise ValueError('output missing src: %s' % output)
291      output = os.path.join(outDir, output[4:])
292      output = output.replace('${ASM_EXT}', asm_ext)
293
294      if arch in ArchForAsmFilename(filename):
295        PerlAsm(output, perlasm['input'], perlasm_style,
296                perlasm['extra_args'] + extra_args)
297        asmfiles.setdefault(key, []).append(output)
298
299  for (key, non_perl_asm_files) in NON_PERL_FILES.iteritems():
300    asmfiles.setdefault(key, []).extend(non_perl_asm_files)
301
302  return asmfiles
303
304
305def main(platform):
306  crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
307  ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
308  tool_cc_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
309
310  # Generate err_data.c
311  with open('err_data.c', 'w+') as err_data:
312    subprocess.check_call(['go', 'run', 'err_data_generate.go'],
313                          cwd=os.path.join('src', 'crypto', 'err'),
314                          stdout=err_data)
315  crypto_c_files.append('err_data.c')
316
317  test_support_cc_files = FindCFiles(os.path.join('src', 'crypto', 'test'),
318                                     AllFiles)
319
320  test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
321  test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
322
323  files = {
324      'crypto': crypto_c_files,
325      'ssl': ssl_c_files,
326      'tool': tool_cc_files,
327      'test': test_c_files,
328      'test_support': test_support_cc_files,
329  }
330
331  asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
332
333  platform.WriteFiles(files, asm_outputs)
334
335  return 0
336
337
338def Usage():
339  print 'Usage: python %s [chromium|android]' % sys.argv[0]
340  sys.exit(1)
341
342
343if __name__ == '__main__':
344  if len(sys.argv) != 2:
345    Usage()
346
347  platform = None
348  if sys.argv[1] == 'chromium':
349    platform = Chromium()
350  elif sys.argv[1] == 'android':
351    platform = Android()
352  else:
353    Usage()
354
355  sys.exit(main(platform))
356