1cb93a386Sopenharmony_ci#!/usr/bin/python
2cb93a386Sopenharmony_ci
3cb93a386Sopenharmony_ciimport subprocess
4cb93a386Sopenharmony_ciimport os
5cb93a386Sopenharmony_ciimport argparse
6cb93a386Sopenharmony_ci
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci# This script compiles and runs a skia app using Swiftshader in a docker container, making it easy
9cb93a386Sopenharmony_ci# to use Swiftshade w/o having to over-write /usr/local/lib/libEGL.so and related on the
10cb93a386Sopenharmony_ci# host development machine.
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci# The Skia repo to be compiled will be the one on the host machine, which will
13cb93a386Sopenharmony_ci# default to the one specified by the environment variable $SKIA_ROOT with a fallback to the
14cb93a386Sopenharmony_ci# current working directory.
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci# Example usage
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci# Prove SwiftShader is really being used:
19cb93a386Sopenharmony_ci#   build-with-swift-shader-and-run "out/with-swift-shader/fuzz --gpuInfo -t api -n NativeGLCanvas"
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci# Notice the output says GL_RENDERER Google SwiftShader
22cb93a386Sopenharmony_ci# After running the above, feel free to check out $SKIA_OUT/out/with-swift-shader. It has binaries
23cb93a386Sopenharmony_ci# but if you try to run out/with-swift-shader/fuzz --gpuInfo -t api -n NativeGLCanvas w/o using
24cb93a386Sopenharmony_ci# Docker, it will use the host's GPU (e.g. GL_VENDOR NVIDIA Corporation).
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci# Reproduce a fuzzer bug in SwiftShader:
27cb93a386Sopenharmony_ci# First, copy the test case into $SKIA_ROOT, say $SKIA_ROOT/skbug_1234
28cb93a386Sopenharmony_ci#    build-with-swift-shader-and-run "out/with-swift-shader/fuzz -t filter_fuzz -b /skia/skbug_1234"
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci# $SKIA_ROOT gets mapped to /skia - other than that, the docker container does not have
31cb93a386Sopenharmony_ci# access to the host file system.
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ciIMAGE = 'gcr.io/skia-public/skia-with-swift-shader-base:prod'
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ciBUILD_SCRIPT_PATH = '/skia/docker/skia-with-swift-shader-base/build.sh'
37cb93a386Sopenharmony_ciEXECUTABLE_DIR = 'out/with-swift-shader/'
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ciparser = argparse.ArgumentParser()
40cb93a386Sopenharmony_ciparser.add_argument('--sync_deps', action='store_true', help='Sync the deps before building?')
41cb93a386Sopenharmony_ciparser.add_argument('command', help='A string containing the command to be run '
42cb93a386Sopenharmony_ci                                    '(e.g. out/with-swift-shader/fuzz --help)')
43cb93a386Sopenharmony_ciargs = parser.parse_args()
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ciskia_root = os.environ['SKIA_ROOT'] or os.getcwd()
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ciprint 'Assuming SKIA_ROOT to be %s' % skia_root
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_cibuild_cmd = ['docker', 'run', '--rm', '-v', '%s:/skia' % skia_root, IMAGE, BUILD_SCRIPT_PATH]
50cb93a386Sopenharmony_ciif args.sync_deps:
51cb93a386Sopenharmony_ci    build_cmd += ['sync-deps']
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ciprint 'Compiling executables to %s/%s' % (skia_root, EXECUTABLE_DIR)
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ciprint subprocess.check_output(build_cmd)
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_cisupplied_cmd = args.command.split(' ')
58cb93a386Sopenharmony_ciprint 'Running supplied command %s' % supplied_cmd
59cb93a386Sopenharmony_cirun_cmd = ['docker', 'run', '--rm', '-w=/skia', '-v', '%s:/skia' % skia_root, IMAGE] + supplied_cmd
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ciprint subprocess.check_output(run_cmd)