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 compile_swiftshader(api, extra_tokens, swiftshader_root, ninja_root, cc, cxx, out): 10 """Build SwiftShader with CMake. 11 12 Building SwiftShader works differently from any other Skia third_party lib. 13 See discussion in skia:7671 for more detail. 14 15 Args: 16 swiftshader_root: root of the SwiftShader checkout. 17 ninja_root: A folder containing a ninja binary 18 cc, cxx: compiler binaries to use 19 out: target directory for libvk_swiftshader.so 20 """ 21 swiftshader_opts = [ 22 '-DSWIFTSHADER_BUILD_TESTS=OFF', 23 '-DSWIFTSHADER_WARNINGS_AS_ERRORS=OFF', 24 '-DREACTOR_ENABLE_MEMORY_SANITIZER_INSTRUMENTATION=OFF', # Way too slow. 25 ] 26 cmake_bin = str(api.vars.workdir.joinpath('cmake_linux', 'bin')) 27 env = { 28 'CC': cc, 29 'CXX': cxx, 30 'PATH': '%s:%%(PATH)s:%s' % (ninja_root, cmake_bin), 31 # We arrange our MSAN/TSAN prebuilts a little differently than 32 # SwiftShader's CMakeLists.txt expects, so we'll just keep our custom 33 # setup (everything mentioning libcxx below) and point SwiftShader's 34 # CMakeLists.txt at a harmless non-existent path. 35 'SWIFTSHADER_MSAN_INSTRUMENTED_LIBCXX_PATH': '/totally/phony/path', 36 } 37 38 # Extra flags for MSAN/TSAN, if necessary. 39 san = None 40 if 'MSAN' in extra_tokens: 41 san = ('msan','memory') 42 43 if san: 44 short,full = san 45 clang_linux = str(api.vars.workdir.joinpath('clang_linux')) 46 libcxx = clang_linux + '/' + short 47 cflags = ' '.join([ 48 '-fsanitize=' + full, 49 '-stdlib=libc++', 50 '-L%s/lib' % libcxx, 51 '-lc++abi', 52 '-I%s/include' % libcxx, 53 '-I%s/include/c++/v1' % libcxx, 54 '-Wno-unused-command-line-argument' # Are -lc++abi and -Llibcxx/lib always unused? 55 ]) 56 swiftshader_opts.extend([ 57 '-DSWIFTSHADER_{}=ON'.format(short.upper()), 58 '-DCMAKE_C_FLAGS=%s' % cflags, 59 '-DCMAKE_CXX_FLAGS=%s' % cflags, 60 ]) 61 62 # Build SwiftShader. 63 api.file.ensure_directory('makedirs swiftshader_out', out) 64 with api.context(cwd=out, env=env): 65 api.run(api.step, 'swiftshader cmake', 66 cmd=['cmake'] + swiftshader_opts + [swiftshader_root, '-GNinja']) 67 # See https://swiftshader-review.googlesource.com/c/SwiftShader/+/56452 for when the 68 # deprecated targets were added. See skbug.com/12386 for longer-term plans. 69 api.run(api.step, 'swiftshader ninja', cmd=['ninja', '-C', out, 'vk_swiftshader']) 70 71 72def compile_fn(api, checkout_root, out_dir): 73 skia_dir = checkout_root.joinpath('skia') 74 compiler = api.vars.builder_cfg.get('compiler', '') 75 configuration = api.vars.builder_cfg.get('configuration', '') 76 extra_tokens = api.vars.extra_tokens 77 os = api.vars.builder_cfg.get('os', '') 78 target_arch = api.vars.builder_cfg.get('target_arch', '') 79 80 clang_linux = str(api.vars.workdir.joinpath('clang_linux')) 81 win_toolchain = str(api.vars.workdir.joinpath('win_toolchain')) 82 dwritecore = str(api.vars.workdir.joinpath('dwritecore')) 83 84 cc, cxx, ccache = None, None, None 85 extra_cflags = [] 86 extra_ldflags = [] 87 args = {'werror': 'true', 'link_pool_depth':'2'} 88 env = {} 89 90 with api.context(cwd=skia_dir): 91 api.run(api.step, 'fetch-gn', 92 cmd=['python3', skia_dir.joinpath('bin', 'fetch-gn')], 93 infra_step=True) 94 95 api.run(api.step, 'fetch-ninja', 96 cmd=['python3', skia_dir.joinpath('bin', 'fetch-ninja')], 97 infra_step=True) 98 99 if os == 'Mac' or os == 'Mac10.15.7': 100 api.xcode.install() 101 102 extra_cflags.append( 103 '-DREBUILD_IF_CHANGED_xcode_build_version=%s' % api.xcode.version) 104 if 'iOS12' in extra_tokens: 105 # Ganesh has a lower minimum iOS version than Graphite but there are dedicated jobs that 106 # test with the lower SDK. 107 env['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0' 108 args['ios_min_target'] = '"12.0"' 109 elif 'iOS18' in extra_tokens: 110 env['IPHONEOS_DEPLOYMENT_TARGET'] = '18.2' 111 args['ios_min_target'] = '"18.0"' 112 elif 'iOS' in extra_tokens: 113 env['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0' 114 args['ios_min_target'] = '"13.0"' 115 else: 116 # We have some machines on 10.15. 117 env['MACOSX_DEPLOYMENT_TARGET'] = '10.15' 118 119 # ccache + clang-tidy.sh chokes on the argument list. 120 if (api.vars.is_linux or os == 'Mac' or os == 'Mac10.15.5' or os == 'Mac10.15.7') and 'Tidy' not in extra_tokens: 121 if api.vars.is_linux: 122 ccache = api.vars.workdir.joinpath('ccache_linux', 'bin', 'ccache') 123 # As of 2020-02-07, the sum of each Debian10-Clang-x86 124 # non-flutter/android/chromebook build takes less than 75G cache space. 125 env['CCACHE_MAXSIZE'] = '75G' 126 else: 127 ccache = api.vars.workdir.joinpath('ccache_mac', 'bin', 'ccache') 128 # As of 2020-02-10, the sum of each Build-Mac-Clang- non-android build 129 # takes ~30G cache space. 130 env['CCACHE_MAXSIZE'] = '50G' 131 132 args['cc_wrapper'] = '"%s"' % ccache 133 134 env['CCACHE_DIR'] = api.vars.cache_dir.joinpath('ccache') 135 env['CCACHE_MAXFILES'] = '0' 136 # Compilers are unpacked from cipd with bogus timestamps, only contribute 137 # compiler content to hashes. If Ninja ever uses absolute paths to changing 138 # directories we'll also need to set a CCACHE_BASEDIR. 139 env['CCACHE_COMPILERCHECK'] = 'content' 140 141 if compiler == 'Clang' and api.vars.is_linux: 142 cc = clang_linux + '/bin/clang' 143 cxx = clang_linux + '/bin/clang++' 144 extra_cflags .append('-B%s/bin' % clang_linux) 145 extra_ldflags.append('-B%s/bin' % clang_linux) 146 extra_ldflags.append('-fuse-ld=lld') 147 extra_cflags.append('-DPLACEHOLDER_clang_linux_version=%s' % 148 api.run.asset_version('clang_linux', skia_dir)) 149 if 'Static' in extra_tokens: 150 extra_ldflags.extend(['-static-libstdc++', '-static-libgcc']) 151 152 elif compiler == 'Clang': 153 cc, cxx = 'clang', 'clang++' 154 155 if 'Tidy' in extra_tokens: 156 # Swap in clang-tidy.sh for clang++, but update PATH so it can find clang++. 157 cxx = skia_dir.joinpath("tools/clang-tidy.sh") 158 env['PATH'] = '%s:%%(PATH)s' % (clang_linux + '/bin') 159 # Increase ClangTidy code coverage by enabling features. 160 args.update({ 161 'skia_enable_fontmgr_empty': 'true', 162 'skia_enable_graphite': 'true', 163 'skia_enable_pdf': 'true', 164 'skia_use_dawn': 'true', 165 'skia_use_expat': 'true', 166 'skia_use_freetype': 'true', 167 'skia_use_vulkan': 'true', 168 }) 169 170 if 'Coverage' in extra_tokens: 171 # See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html for 172 # more info on using llvm to gather coverage information. 173 extra_cflags.append('-fprofile-instr-generate') 174 extra_cflags.append('-fcoverage-mapping') 175 extra_ldflags.append('-fprofile-instr-generate') 176 extra_ldflags.append('-fcoverage-mapping') 177 178 if compiler != 'MSVC' and configuration == 'Debug': 179 extra_cflags.append('-O1') 180 if compiler != 'MSVC' and configuration == 'OptimizeForSize': 181 # build IDs are required for Bloaty if we want to use strip to ignore debug symbols. 182 # https://github.com/google/bloaty/blob/master/doc/using.md#debugging-stripped-binaries 183 extra_ldflags.append('-Wl,--build-id=sha1') 184 args.update({ 185 'skia_use_runtime_icu': 'true', 186 'skia_enable_optimize_size': 'true', 187 'skia_use_jpeg_gainmaps': 'false', 188 }) 189 190 if 'Exceptions' in extra_tokens: 191 extra_cflags.append('/EHsc') 192 if 'Fast' in extra_tokens: 193 extra_cflags.extend(['-march=native', '-fomit-frame-pointer', '-O3', 194 '-ffp-contract=off']) 195 196 if len(extra_tokens) == 1 and extra_tokens[0].startswith('SK'): 197 extra_cflags.append('-D' + extra_tokens[0]) 198 # If we're limiting Skia at all, drop skcms to portable code. 199 if 'SK_CPU_LIMIT' in extra_tokens[0]: 200 extra_cflags.append('-DSKCMS_PORTABLE') 201 202 if 'MSAN' in extra_tokens: 203 extra_ldflags.append('-L' + clang_linux + '/msan') 204 elif 'TSAN' in extra_tokens: 205 extra_ldflags.append('-L' + clang_linux + '/tsan') 206 elif api.vars.is_linux: 207 extra_ldflags.append('-L' + clang_linux + '/lib') 208 209 if configuration != 'Debug': 210 args['is_debug'] = 'false' 211 if 'Dawn' in extra_tokens: 212 util.set_dawn_args_and_env(args, env, api, extra_tokens, skia_dir) 213 if 'ANGLE' in extra_tokens: 214 args['skia_use_angle'] = 'true' 215 if 'SwiftShader' in extra_tokens: 216 swiftshader_root = skia_dir.joinpath('third_party', 'externals', 'swiftshader') 217 # Swiftshader will need to make ninja be on the path 218 ninja_root = skia_dir.joinpath('third_party', 'ninja') 219 swiftshader_out = out_dir.joinpath('swiftshader_out') 220 compile_swiftshader(api, extra_tokens, swiftshader_root, ninja_root, cc, cxx, swiftshader_out) 221 args['skia_use_vulkan'] = 'true' 222 extra_cflags.extend(['-DSK_GPU_TOOLS_VK_LIBRARY_NAME=%s' % 223 api.vars.swarming_out_dir.joinpath('swiftshader_out', 'libvk_swiftshader.so'), 224 ]) 225 if 'MSAN' in extra_tokens: 226 args['skia_use_fontconfig'] = 'false' 227 if 'ASAN' in extra_tokens: 228 args['skia_enable_spirv_validation'] = 'false' 229 if 'NoPrecompile' in extra_tokens: 230 args['skia_enable_precompile'] = 'false' 231 if 'Graphite' in extra_tokens: 232 args['skia_enable_graphite'] = 'true' 233 if 'Vello' in extra_tokens: 234 args['skia_enable_vello_shaders'] = 'true' 235 if 'Fontations' in extra_tokens: 236 args['skia_use_fontations'] = 'true' 237 args['skia_use_freetype'] = 'true' # we compare with freetype in tests 238 args['skia_use_system_freetype2'] = 'false' 239 if 'RustPNG' in extra_tokens: 240 args['skia_use_rust_png_decode'] = 'true' 241 args['skia_use_rust_png_encode'] = 'true' 242 args['skia_use_libpng_decode'] = 'false' 243 # TODO(b/356875275) set skia_use_libpng_encode to false also 244 if 'FreeType' in extra_tokens: 245 args['skia_use_freetype'] = 'true' 246 args['skia_use_system_freetype2'] = 'false' 247 extra_cflags.extend(['-DSK_USE_FREETYPE_EMBOLDEN']) 248 249 if 'NoGpu' in extra_tokens: 250 args['skia_enable_ganesh'] = 'false' 251 if 'NoDEPS' in extra_tokens: 252 args.update({ 253 'is_official_build': 'true', 254 'skia_enable_fontmgr_empty': 'true', 255 'skia_enable_ganesh': 'true', 256 257 'skia_enable_pdf': 'false', 258 'skia_use_expat': 'false', 259 'skia_use_freetype': 'false', 260 'skia_use_harfbuzz': 'false', 261 'skia_use_icu': 'false', 262 'skia_use_libjpeg_turbo_decode': 'false', 263 'skia_use_libjpeg_turbo_encode': 'false', 264 'skia_use_libpng_decode': 'false', 265 'skia_use_libpng_encode': 'false', 266 'skia_use_libwebp_decode': 'false', 267 'skia_use_libwebp_encode': 'false', 268 'skia_use_vulkan': 'false', 269 'skia_use_wuffs': 'false', 270 'skia_use_zlib': 'false', 271 }) 272 elif configuration != 'OptimizeForSize': 273 args.update({ 274 'skia_use_client_icu': 'true', 275 'skia_use_libgrapheme': 'true', 276 }) 277 278 if 'Fontations' in extra_tokens: 279 args['skia_use_icu4x'] = 'true' 280 281 if 'Shared' in extra_tokens: 282 args['is_component_build'] = 'true' 283 if 'Vulkan' in extra_tokens and not 'Android' in extra_tokens and not 'Dawn' in extra_tokens: 284 args['skia_use_vulkan'] = 'true' 285 args['skia_enable_vulkan_debug_layers'] = 'true' 286 # When running TSAN with Vulkan on NVidia, we experienced some timeouts. We found 287 # a workaround (in GrContextFactory) that requires GL (in addition to Vulkan). 288 if 'TSAN' in extra_tokens: 289 args['skia_use_gl'] = 'true' 290 else: 291 args['skia_use_gl'] = 'false' 292 if 'Direct3D' in extra_tokens and not 'Dawn' in extra_tokens: 293 args['skia_use_direct3d'] = 'true' 294 args['skia_use_gl'] = 'false' 295 if 'Metal' in extra_tokens and not 'Dawn' in extra_tokens: 296 args['skia_use_metal'] = 'true' 297 args['skia_use_gl'] = 'false' 298 if 'iOS' in extra_tokens or 'iOS18' in extra_tokens: 299 # Bots use Chromium signing cert. 300 args['skia_ios_identity'] = '".*83FNP.*"' 301 # Get mobileprovision via the CIPD package. 302 args['skia_ios_profile'] = '"%s"' % api.vars.workdir.joinpath( 303 'provisioning_profile_ios', 304 'Upstream_Com_Testing_Provisioning_Profile.mobileprovision') 305 if compiler == 'Clang' and 'Win' in os: 306 args['clang_win'] = '"%s"' % api.vars.workdir.joinpath('clang_win') 307 extra_cflags.append('-DPLACEHOLDER_clang_win_version=%s' % 308 api.run.asset_version('clang_win', skia_dir)) 309 310 sanitize = '' 311 for t in extra_tokens: 312 if t.endswith('SAN'): 313 sanitize = t 314 if api.vars.is_linux and t == 'ASAN': 315 # skia:8712 and skia:8713 316 extra_cflags.append('-DSK_ENABLE_SCOPED_LSAN_SUPPRESSIONS') 317 if 'SafeStack' in extra_tokens: 318 assert sanitize == '' 319 sanitize = 'safe-stack' 320 321 if 'Wuffs' in extra_tokens: 322 args['skia_use_wuffs'] = 'true' 323 324 if 'AVIF' in extra_tokens: 325 args['skia_use_libavif'] = 'true' 326 327 for (k,v) in { 328 'cc': cc, 329 'cxx': cxx, 330 'sanitize': sanitize, 331 'target_cpu': target_arch, 332 'target_os': 'ios' if ('iOS' in extra_tokens or 'iOS18' in extra_tokens) else '', 333 'win_sdk': win_toolchain + '/win_sdk' if 'Win' in os else '', 334 'win_vc': win_toolchain + '/VC' if 'Win' in os else '', 335 'skia_dwritecore_sdk': dwritecore if 'DWriteCore' in extra_tokens else '', 336 }.items(): 337 if v: 338 args[k] = '"%s"' % v 339 if extra_cflags: 340 args['extra_cflags'] = repr(extra_cflags).replace("'", '"') 341 if extra_ldflags: 342 args['extra_ldflags'] = repr(extra_ldflags).replace("'", '"') 343 344 gn_args = ' '.join('%s=%s' % (k,v) for (k,v) in sorted(args.items())) 345 gn = skia_dir.joinpath('bin', 'gn') 346 ninja = skia_dir.joinpath('third_party', 'ninja', 'ninja') 347 348 with api.context(cwd=skia_dir): 349 with api.env(env): 350 if ccache: 351 api.run(api.step, 'ccache stats-start', cmd=[ccache, '-s']) 352 api.run(api.step, 'gn gen', 353 cmd=[gn, 'gen', out_dir, '--args=' + gn_args]) 354 if 'Fontations' in extra_tokens: 355 api.run(api.step, 'gn clean', 356 cmd=[gn, 'clean', out_dir]) 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 configuration = api.vars.builder_cfg.get('configuration', '') 367 368 if 'SwiftShader' in extra_tokens: 369 util.copy_listed_files(api, 370 src.joinpath('swiftshader_out'), 371 api.vars.swarming_out_dir.joinpath('swiftshader_out'), 372 util.DEFAULT_BUILD_PRODUCTS) 373 374 if configuration == 'OptimizeForSize': 375 util.copy_listed_files(api, src, dst, ['skottie_tool_cpu', 'skottie_tool_gpu']) 376 377 if os == 'Mac' and any('SAN' in t for t in extra_tokens): 378 # The XSAN dylibs are in 379 # Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib 380 # /clang/11.0.0/lib/darwin, where 11.0.0 could change in future versions. 381 xcode_clang_ver_dirs = api.file.listdir( 382 'find XCode Clang version', 383 api.vars.cache_dir.joinpath( 384 'Xcode.app', 'Contents', 'Developer', 'Toolchains', 385 'XcodeDefault.xctoolchain', 'usr', 'lib', 'clang'), 386 test_data=['11.0.0']) 387 # Allow both clang/16 and clang/16.0.0, so long as they are equivalent. 388 assert len({api.path.realpath(d) for d in xcode_clang_ver_dirs}) == 1 389 dylib_dir = xcode_clang_ver_dirs[0].joinpath('lib', 'darwin') 390 dylibs = api.file.glob_paths('find xSAN dylibs', dylib_dir, 391 'libclang_rt.*san_osx_dynamic.dylib', 392 test_data=[ 393 'libclang_rt.asan_osx_dynamic.dylib', 394 'libclang_rt.tsan_osx_dynamic.dylib', 395 'libclang_rt.ubsan_osx_dynamic.dylib', 396 ]) 397 for f in dylibs: 398 api.file.copy('copy %s' % api.path.basename(f), f, dst) 399