Updates to the build-file generation script.

This change adds ‘android-standalone’ and ‘bazel’ targets to the
build-file generation script. It also allows multiple build outputs to
be written in one invocation, which saves the cost of Perl to build all
the assembly files multiple times.

Note that this will require changes to the Chromium GYP files when its
rolled out because “boringssl_lib_sources” has been broken into
“boringssl_crypto_sources” and “boringssl_ssl_sources”.

Change-Id: Icfe9678d08fba74fd215cf92521831e9cdc265da
Reviewed-on: https://boringssl-review.googlesource.com/5070
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/util/generate_build_files.py b/util/generate_build_files.py
index e534a56..bee46ca 100644
--- a/util/generate_build_files.py
+++ b/util/generate_build_files.py
@@ -68,7 +68,9 @@
       gypi.write(self.header + '{\n  \'variables\': {\n')
 
       self.PrintVariableSection(
-          gypi, 'boringssl_lib_sources', files['crypto'] + files['ssl'])
+          gypi, 'boringssl_ssl_sources', files['ssl'])
+      self.PrintVariableSection(
+          gypi, 'boringssl_crypto_sources', files['crypto'])
 
       for ((osname, arch), asm_files) in asm_outputs:
         self.PrintVariableSection(gypi, 'boringssl_%s_%s_sources' %
@@ -133,6 +135,9 @@
 
 """
 
+  def ExtraFiles(self):
+    return ['android_compat_hacks.c', 'android_compat_keywrap.c']
+
   def PrintVariableSection(self, out, name, files):
     out.write('%s := \\\n' % name)
     for f in sorted(files):
@@ -143,8 +148,7 @@
     with open('sources.mk', 'w+') as makefile:
       makefile.write(self.header)
 
-      files['crypto'].append('android_compat_hacks.c')
-      files['crypto'].append('android_compat_keywrap.c')
+      files['crypto'].extend(self.ExtraFiles())
       self.PrintVariableSection(makefile, 'crypto_sources', files['crypto'])
       self.PrintVariableSection(makefile, 'ssl_sources', files['ssl'])
       self.PrintVariableSection(makefile, 'tool_sources', files['tool'])
@@ -154,6 +158,57 @@
             makefile, '%s_%s_sources' % (osname, arch), asm_files)
 
 
+class AndroidStandalone(Android):
+  """AndroidStandalone is for Android builds outside of the Android-system, i.e.
+
+  for applications that wish wish to ship BoringSSL.
+  """
+
+  def ExtraFiles(self):
+    return []
+
+
+class Bazel(object):
+  """Bazel outputs files suitable for including in Bazel files."""
+
+  def __init__(self):
+    self.firstSection = True
+    self.header = \
+"""# This file is created by generate_build_files.py. Do not edit manually.
+
+"""
+
+  def PrintVariableSection(self, out, name, files):
+    if not self.firstSection:
+      out.write('\n')
+    self.firstSection = False
+
+    out.write('%s = [\n' % name)
+    for f in sorted(files):
+      out.write('    "%s",\n' % f)
+    out.write(']\n')
+
+  def WriteFiles(self, files, asm_outputs):
+    with open('BUILD.generated', 'w+') as out:
+      out.write(self.header)
+
+      self.PrintVariableSection(out, 'ssl_headers', files['ssl_headers'])
+      self.PrintVariableSection(
+          out, 'ssl_internal_headers', files['ssl_internal_headers'])
+      self.PrintVariableSection(out, 'ssl_sources', files['ssl'])
+      self.PrintVariableSection(out, 'crypto_headers', files['crypto_headers'])
+      self.PrintVariableSection(
+          out, 'crypto_internal_headers', files['crypto_internal_headers'])
+      self.PrintVariableSection(out, 'crypto_sources', files['crypto'])
+      self.PrintVariableSection(out, 'tool_sources', files['tool'])
+
+      for ((osname, arch), asm_files) in asm_outputs:
+        if osname is not 'linux':
+          continue
+        self.PrintVariableSection(
+            out, 'crypto_sources_%s' % arch, asm_files)
+
+
 def FindCMakeFiles(directory):
   """Returns list of all CMakeLists.txt files recursively in directory."""
   cmakefiles = []
@@ -188,6 +243,10 @@
   return True
 
 
+def SSLHeaderFiles(dent, is_dir):
+  return dent in ['ssl.h', 'tls1.h', 'ssl23.h', 'ssl3.h', 'dtls1.h']
+
+
 def FindCFiles(directory, filter_func):
   """Recurses through directory and returns a list of paths to all the C source
   files that pass filter_func."""
@@ -208,6 +267,21 @@
   return cfiles
 
 
+def FindHeaderFiles(directory, filter_func):
+  """Recurses through directory and returns a list of paths to all the header files that pass filter_func."""
+  hfiles = []
+
+  for (path, dirnames, filenames) in os.walk(directory):
+    for filename in filenames:
+      if not filename.endswith('.h'):
+        continue
+      if not filter_func(filename, False):
+        continue
+      hfiles.append(os.path.join(path, filename))
+
+  return hfiles
+
+
 def ExtractPerlAsmFromCMakeFile(cmakefile):
   """Parses the contents of the CMakeLists.txt file passed as an argument and
   returns a list of all the perlasm() directives found in the file."""
@@ -302,7 +376,7 @@
   return asmfiles
 
 
-def main(platform):
+def main(platforms):
   crypto_c_files = FindCFiles(os.path.join('src', 'crypto'), NoTests)
   ssl_c_files = FindCFiles(os.path.join('src', 'ssl'), NoTests)
   tool_cc_files = FindCFiles(os.path.join('src', 'tool'), NoTests)
@@ -320,9 +394,29 @@
   test_c_files = FindCFiles(os.path.join('src', 'crypto'), OnlyTests)
   test_c_files += FindCFiles(os.path.join('src', 'ssl'), OnlyTests)
 
+  ssl_h_files = (
+      FindHeaderFiles(
+          os.path.join('src', 'include', 'openssl'),
+          SSLHeaderFiles))
+
+  def NotSSLHeaderFiles(filename, is_dir):
+    return not SSLHeaderFiles(filename, is_dir)
+  crypto_h_files = (
+      FindHeaderFiles(
+          os.path.join('src', 'include', 'openssl'),
+          NotSSLHeaderFiles))
+
+  ssl_internal_h_files = FindHeaderFiles(os.path.join('src', 'ssl'), NoTests)
+  crypto_internal_h_files = FindHeaderFiles(
+      os.path.join('src', 'crypto'), NoTests)
+
   files = {
       'crypto': crypto_c_files,
+      'crypto_headers': crypto_h_files,
+      'crypto_internal_headers': crypto_internal_h_files,
       'ssl': ssl_c_files,
+      'ssl_headers': ssl_h_files,
+      'ssl_internal_headers': ssl_internal_h_files,
       'tool': tool_cc_files,
       'test': test_c_files,
       'test_support': test_support_cc_files,
@@ -330,26 +424,32 @@
 
   asm_outputs = sorted(WriteAsmFiles(ReadPerlAsmOperations()).iteritems())
 
-  platform.WriteFiles(files, asm_outputs)
+  for platform in platforms:
+    platform.WriteFiles(files, asm_outputs)
 
   return 0
 
 
 def Usage():
-  print 'Usage: python %s [chromium|android]' % sys.argv[0]
+  print 'Usage: python %s [chromium|android|android-standalone|bazel]' % sys.argv[0]
   sys.exit(1)
 
 
 if __name__ == '__main__':
-  if len(sys.argv) != 2:
+  if len(sys.argv) < 2:
     Usage()
 
-  platform = None
-  if sys.argv[1] == 'chromium':
-    platform = Chromium()
-  elif sys.argv[1] == 'android':
-    platform = Android()
-  else:
-    Usage()
+  platforms = []
+  for s in sys.argv[1:]:
+    if s == 'chromium' or s == 'gyp':
+      platforms.append(Chromium())
+    elif s == 'android':
+      platforms.append(Android())
+    elif s == 'android-standalone':
+      platforms.append(AndroidStandalone())
+    elif s == 'bazel':
+      platforms.append(Bazel())
+    else:
+      Usage()
 
-  sys.exit(main(platform))
+  sys.exit(main(platforms))