• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6from . import util
7
8
9def build_command_buffer(api, chrome_dir, skia_dir, out):
10  api.run(api.python, 'build command_buffer',
11      script=skia_dir.join('tools', 'build_command_buffer.py'),
12      args=[
13        '--chrome-dir', chrome_dir,
14        '--output-dir', out,
15        '--extra-gn-args', 'mac_sdk_min="10.13"',
16        '--no-sync', '--no-hooks', '--make-output-dir'])
17
18
19def compile_swiftshader(api, extra_tokens, swiftshader_root, cc, cxx, out):
20  """Build SwiftShader with CMake.
21
22  Building SwiftShader works differently from any other Skia third_party lib.
23  See discussion in skia:7671 for more detail.
24
25  Args:
26    swiftshader_root: root of the SwiftShader checkout.
27    cc, cxx: compiler binaries to use
28    out: target directory for libvk_swiftshader.so
29  """
30  swiftshader_opts = [
31      '-DSWIFTSHADER_BUILD_TESTS=OFF',
32      '-DSWIFTSHADER_WARNINGS_AS_ERRORS=OFF',
33      '-DREACTOR_ENABLE_MEMORY_SANITIZER_INSTRUMENTATION=OFF',  # Way too slow.
34  ]
35  cmake_bin = str(api.vars.workdir.join('cmake_linux', 'bin'))
36  env = {
37      'CC': cc,
38      'CXX': cxx,
39      'PATH': '%%(PATH)s:%s' % cmake_bin,
40      # We arrange our MSAN/TSAN prebuilts a little differently than
41      # SwiftShader's CMakeLists.txt expects, so we'll just keep our custom
42      # setup (everything mentioning libcxx below) and point SwiftShader's
43      # CMakeLists.txt at a harmless non-existent path.
44      'SWIFTSHADER_MSAN_INSTRUMENTED_LIBCXX_PATH': '/totally/phony/path',
45  }
46
47  # Extra flags for MSAN/TSAN, if necessary.
48  san = None
49  if 'MSAN' in extra_tokens:
50    san = ('msan','memory')
51
52  if san:
53    short,full = san
54    clang_linux = str(api.vars.workdir.join('clang_linux'))
55    libcxx = clang_linux + '/' + short
56    cflags = ' '.join([
57      '-fsanitize=' + full,
58      '-stdlib=libc++',
59      '-L%s/lib' % libcxx,
60      '-lc++abi',
61      '-I%s/include' % libcxx,
62      '-I%s/include/c++/v1' % libcxx,
63      '-Wno-unused-command-line-argument'  # Are -lc++abi and -Llibcxx/lib always unused?
64    ])
65    swiftshader_opts.extend([
66      '-DSWIFTSHADER_{}=ON'.format(short.upper()),
67      '-DCMAKE_C_FLAGS=%s' % cflags,
68      '-DCMAKE_CXX_FLAGS=%s' % cflags,
69    ])
70
71  # Build SwiftShader.
72  api.file.ensure_directory('makedirs swiftshader_out', out)
73  with api.context(cwd=out, env=env):
74    api.run(api.step, 'swiftshader cmake',
75            cmd=['cmake'] + swiftshader_opts + [swiftshader_root, '-GNinja'])
76    # See https://swiftshader-review.googlesource.com/c/SwiftShader/+/56452 for when the
77    # deprecated targets were added. See skbug.com/12386 for longer-term plans.
78    api.run(api.step, 'swiftshader ninja', cmd=['ninja', '-C', out, 'vk_swiftshader'])
79
80
81def compile_fn(api, checkout_root, out_dir):
82  skia_dir      = checkout_root.join('skia')
83  compiler      = api.vars.builder_cfg.get('compiler',      '')
84  configuration = api.vars.builder_cfg.get('configuration', '')
85  extra_tokens  = api.vars.extra_tokens
86  os            = api.vars.builder_cfg.get('os',            '')
87  target_arch   = api.vars.builder_cfg.get('target_arch',   '')
88
89  clang_linux      = str(api.vars.workdir.join('clang_linux'))
90  win_toolchain    = str(api.vars.workdir.join('win_toolchain'))
91
92  cc, cxx, ccache = None, None, None
93  extra_cflags = []
94  extra_ldflags = []
95  args = {'werror': 'true'}
96  env = {}
97
98  if os == 'Mac':
99    # XCode build is listed in parentheses after the version at
100    # https://developer.apple.com/news/releases/, or on Wikipedia here:
101    # https://en.wikipedia.org/wiki/Xcode#Version_comparison_table
102    # Use lowercase letters.
103    # https://chrome-infra-packages.appspot.com/p/infra_internal/ios/xcode
104    XCODE_BUILD_VERSION = '12c33'
105    if compiler == 'Xcode11.4.1':
106      XCODE_BUILD_VERSION = '11e503a'
107    extra_cflags.append(
108        '-DREBUILD_IF_CHANGED_xcode_build_version=%s' % XCODE_BUILD_VERSION)
109    mac_toolchain_cmd = api.vars.workdir.join(
110        'mac_toolchain', 'mac_toolchain')
111    xcode_app_path = api.vars.cache_dir.join('Xcode.app')
112    # Copied from
113    # https://chromium.googlesource.com/chromium/tools/build/+/e19b7d9390e2bb438b566515b141ed2b9ed2c7c2/scripts/slave/recipe_modules/ios/api.py#322
114    with api.step.nest('ensure xcode') as step_result:
115      step_result.presentation.step_text = (
116          'Ensuring Xcode version %s in %s' % (
117              XCODE_BUILD_VERSION, xcode_app_path))
118      install_xcode_cmd = [
119          mac_toolchain_cmd, 'install',
120          # "ios" is needed for simulator builds
121          # (Build-Mac-Clang-x64-Release-iOS).
122          '-kind', 'ios',
123          '-xcode-version', XCODE_BUILD_VERSION,
124          '-output-dir', xcode_app_path,
125      ]
126      api.step('install xcode', install_xcode_cmd)
127      api.step('select xcode', [
128          'sudo', 'xcode-select', '-switch', xcode_app_path])
129      if 'iOS' in extra_tokens:
130        # Our current min-spec for Skia is iOS 11
131        env['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
132        args['ios_min_target'] = '"11.0"'
133      else:
134        # We have some bots on 10.13.
135        env['MACOSX_DEPLOYMENT_TARGET'] = '10.13'
136
137  if 'CheckGeneratedFiles' in extra_tokens:
138    compiler = 'Clang'
139    args['skia_compile_processors'] = 'true'
140    args['skia_compile_sksl_tests'] = 'true'
141    args['skia_generate_workarounds'] = 'true'
142
143  # ccache + clang-tidy.sh chokes on the argument list.
144  if (api.vars.is_linux or os == 'Mac' or os == 'Mac10.15.5' or os == 'Mac10.15.7') and 'Tidy' not in extra_tokens:
145    if api.vars.is_linux:
146      ccache = api.vars.workdir.join('ccache_linux', 'bin', 'ccache')
147      # As of 2020-02-07, the sum of each Debian10-Clang-x86
148      # non-flutter/android/chromebook build takes less than 75G cache space.
149      env['CCACHE_MAXSIZE'] = '75G'
150    else:
151      ccache = api.vars.workdir.join('ccache_mac', 'bin', 'ccache')
152      # As of 2020-02-10, the sum of each Build-Mac-Clang- non-android build
153      # takes ~30G cache space.
154      env['CCACHE_MAXSIZE'] = '50G'
155
156    args['cc_wrapper'] = '"%s"' % ccache
157
158    env['CCACHE_DIR'] = api.vars.cache_dir.join('ccache')
159    env['CCACHE_MAXFILES'] = '0'
160    # Compilers are unpacked from cipd with bogus timestamps, only contribute
161    # compiler content to hashes. If Ninja ever uses absolute paths to changing
162    # directories we'll also need to set a CCACHE_BASEDIR.
163    env['CCACHE_COMPILERCHECK'] = 'content'
164
165  if compiler == 'Clang' and api.vars.is_linux:
166    cc  = clang_linux + '/bin/clang'
167    cxx = clang_linux + '/bin/clang++'
168    extra_cflags .append('-B%s/bin' % clang_linux)
169    extra_ldflags.append('-B%s/bin' % clang_linux)
170    extra_ldflags.append('-fuse-ld=lld')
171    extra_cflags.append('-DPLACEHOLDER_clang_linux_version=%s' %
172                        api.run.asset_version('clang_linux', skia_dir))
173    if 'Static' in extra_tokens:
174      extra_ldflags.extend(['-static-libstdc++', '-static-libgcc'])
175
176  elif compiler == 'Clang':
177    cc, cxx = 'clang', 'clang++'
178
179  if 'Tidy' in extra_tokens:
180    # Swap in clang-tidy.sh for clang++, but update PATH so it can find clang++.
181    cxx = skia_dir.join("tools/clang-tidy.sh")
182    env['PATH'] = '%s:%%(PATH)s' % (clang_linux + '/bin')
183    # Increase ClangTidy code coverage by enabling features.
184    args.update({
185      'skia_enable_fontmgr_empty':     'true',
186      'skia_enable_pdf':               'true',
187      'skia_use_expat':                'true',
188      'skia_use_freetype':             'true',
189      'skia_use_vulkan':               'true',
190    })
191
192  if 'Coverage' in extra_tokens:
193    # See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html for
194    # more info on using llvm to gather coverage information.
195    extra_cflags.append('-fprofile-instr-generate')
196    extra_cflags.append('-fcoverage-mapping')
197    extra_ldflags.append('-fprofile-instr-generate')
198    extra_ldflags.append('-fcoverage-mapping')
199
200  if compiler != 'MSVC' and configuration == 'Debug':
201    extra_cflags.append('-O1')
202
203  if 'Exceptions' in extra_tokens:
204    extra_cflags.append('/EHsc')
205  if 'Fast' in extra_tokens:
206    extra_cflags.extend(['-march=native', '-fomit-frame-pointer', '-O3',
207                         '-ffp-contract=off'])
208
209  if len(extra_tokens) == 1 and extra_tokens[0].startswith('SK'):
210    extra_cflags.append('-D' + extra_tokens[0])
211    # If we're limiting Skia at all, drop skcms to portable code.
212    if 'SK_CPU_LIMIT' in extra_tokens[0]:
213      extra_cflags.append('-DSKCMS_PORTABLE')
214
215  if 'MSAN' in extra_tokens:
216    extra_ldflags.append('-L' + clang_linux + '/msan')
217  elif 'TSAN' in extra_tokens:
218    extra_ldflags.append('-L' + clang_linux + '/tsan')
219  elif api.vars.is_linux:
220    extra_ldflags.append('-L' + clang_linux + '/lib')
221
222  if configuration != 'Debug':
223    args['is_debug'] = 'false'
224  if 'Dawn' in extra_tokens:
225    args['skia_use_dawn'] = 'true'
226    args['skia_use_gl'] = 'false'
227    # Dawn imports jinja2, which imports markupsafe. Along with DEPS, make it
228    # importable.
229    env['PYTHONPATH'] = api.path.pathsep.join([
230        str(skia_dir.join('third_party', 'externals')), '%%(PYTHONPATH)s'])
231  if 'ANGLE' in extra_tokens:
232    args['skia_use_angle'] = 'true'
233  if 'SwiftShader' in extra_tokens:
234    swiftshader_root = skia_dir.join('third_party', 'externals', 'swiftshader')
235    swiftshader_out = out_dir.join('swiftshader_out')
236    compile_swiftshader(api, extra_tokens, swiftshader_root, cc, cxx, swiftshader_out)
237    args['skia_use_vulkan'] = 'true'
238    extra_cflags.extend(['-DSK_GPU_TOOLS_VK_LIBRARY_NAME=%s' %
239        api.vars.swarming_out_dir.join('swiftshader_out', 'libvk_swiftshader.so'),
240    ])
241  if 'CommandBuffer' in extra_tokens:
242    # CommandBuffer runs against GLES version of CommandBuffer also, so
243    # include both.
244    args.update({
245      'skia_gl_standard': '""',
246    })
247    chrome_dir = checkout_root
248    api.run.run_once(build_command_buffer, api, chrome_dir, skia_dir, out_dir)
249  if 'MSAN' in extra_tokens:
250    args['skia_use_fontconfig'] = 'false'
251  if 'ASAN' in extra_tokens:
252    args['skia_enable_spirv_validation'] = 'false'
253  if 'Graphite' in extra_tokens:
254    args['skia_enable_graphite'] = 'true'
255    args['skia_use_metal'] = 'true'
256    if 'NoGpu' in extra_tokens:
257      args['skia_enable_gpu'] = 'false'
258  if 'NoDEPS' in extra_tokens:
259    args.update({
260      'is_official_build':             'true',
261      'skia_enable_fontmgr_empty':     'true',
262      'skia_enable_gpu':               'true',
263
264      'skia_enable_pdf':               'false',
265      'skia_use_expat':                'false',
266      'skia_use_freetype':             'false',
267      'skia_use_harfbuzz':             'false',
268      'skia_use_icu':                  'false',
269      'skia_use_libjpeg_turbo_decode': 'false',
270      'skia_use_libjpeg_turbo_encode': 'false',
271      'skia_use_libpng_decode':        'false',
272      'skia_use_libpng_encode':        'false',
273      'skia_use_libwebp_decode':       'false',
274      'skia_use_libwebp_encode':       'false',
275      'skia_use_vulkan':               'false',
276      'skia_use_zlib':                 'false',
277    })
278  if 'Shared' in extra_tokens:
279    args['is_component_build'] = 'true'
280  if 'Vulkan' in extra_tokens and not 'Android' in extra_tokens:
281    args['skia_use_vulkan'] = 'true'
282    args['skia_enable_vulkan_debug_layers'] = 'true'
283    # When running TSAN with Vulkan on NVidia, we experienced some timeouts. We found
284    # a workaround (in GrContextFactory) that requires GL (in addition to Vulkan).
285    if 'TSAN' in extra_tokens:
286      args['skia_use_gl'] = 'true'
287    else:
288      args['skia_use_gl'] = 'false'
289  if 'Direct3D' in extra_tokens:
290    args['skia_use_direct3d'] = 'true'
291    args['skia_use_gl'] = 'false'
292  if 'Metal' in extra_tokens:
293    args['skia_use_metal'] = 'true'
294    args['skia_use_gl'] = 'false'
295  if 'iOS' in extra_tokens:
296    # Bots use Chromium signing cert.
297    args['skia_ios_identity'] = '".*GS9WA.*"'
298    # Get mobileprovision via the CIPD package.
299    args['skia_ios_profile'] = '"%s"' % api.vars.workdir.join(
300        'provisioning_profile_ios',
301        'Upstream_Testing_Provisioning_Profile.mobileprovision')
302  if compiler == 'Clang' and 'Win' in os:
303    args['clang_win'] = '"%s"' % api.vars.workdir.join('clang_win')
304    extra_cflags.append('-DPLACEHOLDER_clang_win_version=%s' %
305                        api.run.asset_version('clang_win', skia_dir))
306
307  sanitize = ''
308  for t in extra_tokens:
309    if t.endswith('SAN'):
310      sanitize = t
311      if api.vars.is_linux and t == 'ASAN':
312        # skia:8712 and skia:8713
313        extra_cflags.append('-DSK_ENABLE_SCOPED_LSAN_SUPPRESSIONS')
314  if 'SafeStack' in extra_tokens:
315    assert sanitize == ''
316    sanitize = 'safe-stack'
317
318  if 'Wuffs' in extra_tokens:
319    args['skia_use_wuffs'] = 'true'
320
321  for (k,v) in {
322    'cc':  cc,
323    'cxx': cxx,
324    'sanitize': sanitize,
325    'target_cpu': target_arch,
326    'target_os': 'ios' if 'iOS' in extra_tokens else '',
327    'win_sdk': win_toolchain + '/win_sdk' if 'Win' in os else '',
328    'win_vc': win_toolchain + '/VC' if 'Win' in os else '',
329  }.items():
330    if v:
331      args[k] = '"%s"' % v
332  if extra_cflags:
333    args['extra_cflags'] = repr(extra_cflags).replace("'", '"')
334  if extra_ldflags:
335    args['extra_ldflags'] = repr(extra_ldflags).replace("'", '"')
336
337  gn_args = ' '.join('%s=%s' % (k,v) for (k,v) in sorted(args.items()))
338  gn = skia_dir.join('bin', 'gn')
339
340  with api.context(cwd=skia_dir):
341    api.run(api.python,
342            'fetch-gn',
343            script=skia_dir.join('bin', 'fetch-gn'),
344            infra_step=True)
345    if 'CheckGeneratedFiles' in extra_tokens:
346      env['PATH'] = '%s:%%(PATH)s' % skia_dir.join('bin')
347      api.run(api.python,
348              'fetch-clang-format',
349              script=skia_dir.join('bin', 'fetch-clang-format'),
350              infra_step=True)
351
352    with api.env(env):
353      if ccache:
354        api.run(api.step, 'ccache stats-start', cmd=[ccache, '-s'])
355      api.run(api.step, 'gn gen',
356              cmd=[gn, 'gen', out_dir, '--args=' + gn_args])
357      api.run(api.step, 'ninja', cmd=['ninja', '-C', out_dir])
358      if ccache:
359        api.run(api.step, 'ccache stats-end', cmd=[ccache, '-s'])
360
361
362def copy_build_products(api, src, dst):
363  util.copy_listed_files(api, src, dst, util.DEFAULT_BUILD_PRODUCTS)
364  extra_tokens  = api.vars.extra_tokens
365  os            = api.vars.builder_cfg.get('os', '')
366
367  if 'SwiftShader' in extra_tokens:
368    util.copy_listed_files(api,
369        src.join('swiftshader_out'),
370        api.vars.swarming_out_dir.join('swiftshader_out'),
371        util.DEFAULT_BUILD_PRODUCTS)
372
373  if os == 'Mac' and any('SAN' in t for t in extra_tokens):
374    # The XSAN dylibs are in
375    # Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib
376    # /clang/11.0.0/lib/darwin, where 11.0.0 could change in future versions.
377    xcode_clang_ver_dirs = api.file.listdir(
378        'find XCode Clang version',
379        api.vars.cache_dir.join(
380            'Xcode.app', 'Contents', 'Developer', 'Toolchains',
381            'XcodeDefault.xctoolchain', 'usr', 'lib', 'clang'),
382        test_data=['11.0.0'])
383    assert len(xcode_clang_ver_dirs) == 1
384    dylib_dir = xcode_clang_ver_dirs[0].join('lib', 'darwin')
385    dylibs = api.file.glob_paths('find xSAN dylibs', dylib_dir,
386                                 'libclang_rt.*san_osx_dynamic.dylib',
387                                 test_data=[
388                                     'libclang_rt.asan_osx_dynamic.dylib',
389                                     'libclang_rt.tsan_osx_dynamic.dylib',
390                                     'libclang_rt.ubsan_osx_dynamic.dylib',
391                                 ])
392    for f in dylibs:
393      api.file.copy('copy %s' % api.path.basename(f), f, dst)
394