1#!/usr/bin/env python
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import subprocess
19import sys
20
21MAC_BUILD_CONFIGS = {
22  'mac_debug': ['is_clang=true', 'is_debug=true'],
23  'mac_release': ['is_clang=true', 'is_debug=false'],
24  'mac_asan': ['is_clang=true', 'is_debug=false', 'is_asan=true'],
25  'mac_tsan': ['is_clang=true', 'is_debug=false', 'is_tsan=true'],
26  'mac_ubsan': ['is_clang=true', 'is_debug=false', 'is_ubsan=true'],
27}
28
29ANDROID_BUILD_CONFIGS = {
30  'android_debug': ['target_os="android"', 'is_clang=true', 'is_debug=true'],
31  'android_release': ['target_os="android"', 'is_clang=true', 'is_debug=false'],
32  'android_release_incl_heapprofd':
33    ['target_os="android"', 'is_clang=true', 'is_debug=false', 'android_api_level=26'],
34  'android_asan': ['target_os="android"', 'is_clang=true', 'is_debug=false', 'is_asan=true'],
35  'android_lsan': ['target_os="android"', 'is_clang=true', 'is_debug=false', 'is_lsan=true'],
36}
37
38ANDROID_ARCHS = [ 'arm', 'arm64' ]
39
40LINUX_BUILD_CONFIGS = {
41  'linux_gcc_debug': ['is_clang=false', 'is_debug=true'],
42  'linux_gcc_release': ['is_clang=false', 'is_debug=false'],
43  'linux_clang_debug': ['is_clang=true', 'is_debug=true'],
44  'linux_clang_release': ['is_clang=true', 'is_debug=false'],
45  'linux_asan': ['is_clang=true', 'is_debug=false', 'is_asan=true'],
46  'linux_lsan': ['is_clang=true', 'is_debug=false', 'is_lsan=true'],
47  'linux_msan': ['is_clang=true', 'is_debug=false', 'is_msan=true'],
48  'linux_tsan': ['is_clang=true', 'is_debug=false', 'is_tsan=true'],
49  'linux_ubsan': ['is_clang=true', 'is_debug=false', 'is_ubsan=true'],
50  'linux_fuzzer': ['is_clang=true', 'is_debug=false', 'is_fuzzer=true',
51                   'is_asan=true'],
52}
53
54ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
55
56def main():
57  parser = argparse.ArgumentParser()
58  parser.add_argument('--host-only', action='store_true', default=False)
59  parser.add_argument('--build', default=None)
60  args = parser.parse_args()
61
62  configs = {}
63  if not args.host_only:
64    for config_name, gn_args in ANDROID_BUILD_CONFIGS.iteritems():
65      for arch in ANDROID_ARCHS:
66        full_config_name = '%s_%s' % (config_name, arch)
67        configs[full_config_name] = gn_args + ['target_cpu="%s"' % arch]
68
69  if sys.platform == 'linux2':
70    configs.update(LINUX_BUILD_CONFIGS)
71  elif sys.platform == 'darwin':
72    configs.update(MAC_BUILD_CONFIGS)
73  else:
74    assert(False)
75
76  out_base_dir = os.path.join(ROOT_DIR, 'out')
77  if not os.path.isdir(out_base_dir):
78    os.mkdir(out_base_dir)
79
80  gn = os.path.join(ROOT_DIR, 'tools', 'gn')
81
82  for config_name, gn_args in configs.iteritems():
83    print '\n\033[32mBuilding %-20s[%s]\033[0m' % (config_name, ','.join(gn_args))
84    out_dir = os.path.join(ROOT_DIR, 'out', config_name)
85    if not os.path.isdir(out_dir):
86      os.mkdir(out_dir)
87    gn_cmd = [gn, 'gen', out_dir, '--args=%s' % (' '.join(gn_args)), '--check']
88    print ' '.join(gn_cmd)
89    subprocess.check_call(gn_cmd, cwd=ROOT_DIR)
90    if args.build:
91      ninja = os.path.join(ROOT_DIR, 'tools', 'ninja')
92      ninja_cmd = [ninja, '-C', '.', args.build]
93      subprocess.check_call(ninja_cmd, cwd=out_dir)
94
95
96if __name__ == '__main__':
97  sys.exit(main())
98