• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1load("//bazel:macros.bzl", "bool_flag", "exports_files_legacy", "skia_filegroup", "wasm_cc_binary")
2load("//bazel:karma_test.bzl", "karma_test")
3load("@skia_user_config//:copts.bzl", "DEFAULT_COPTS")
4
5licenses(["notice"])
6
7exports_files_legacy()
8
9BASE_LINKOPTS = [
10    #"-flto",  # https://github.com/emscripten-core/emsdk/issues/807
11    "--bind",  # Compiles the source code using the Embind bindings to connect C/C++ and JavaScript
12    "-fno-rtti",
13    "--no-entry",
14    "-sALLOW_MEMORY_GROWTH",
15    "-sUSE_PTHREADS=0",  # Disable pthreads
16    "-sMODULARIZE",
17    "-sDISABLE_EXCEPTION_CATCHING",  # Disable all exception catching
18    "-sNODEJS_CATCH_EXIT=0",  # We don't have a 'main' so disable exit() catching
19    "-sWASM",
20    "-sMAX_WEBGL_VERSION=2",
21    "-sUSE_WEBGL2=1",
22    "-sFORCE_FILESYSTEM=0",
23    "-sDYNAMIC_EXECUTION=0",
24    "-sFILESYSTEM=0",
25    "-sEXPORTED_FUNCTIONS=['_malloc','_free']",
26]
27
28RELEASE_OPTS = [
29    "-sASSERTIONS=0",  # Turn off assertions
30    "-Oz",
31]
32
33DEBUG_OPTS = [
34    "--closure 0",  # Do not use closure
35    "-sASSERTIONS",  # Turn on assertions
36    "-sGL_ASSERTIONS",
37    "-O0",
38    "-g3",
39    "--source-map-base=/build/",
40]
41
42skia_filegroup(
43    name = "hdrs",
44    srcs = [
45        "WasmCommon.h",
46    ],
47)
48
49# See https://stackoverflow.com/a/57499321 for reference.
50genrule(
51    name = "create_notomono_cpp",
52    srcs = ["fonts/NotoMono-Regular.ttf"],
53    outs = ["fonts/NotoMono-Regular.ttf.bazel.cpp"],  # Distinct name from compile.sh's version
54    cmd = "$(location //tools:embed_resources) --name=SK_EMBEDDED_FONTS " +
55          "--input=modules/canvaskit/fonts/NotoMono-Regular.ttf " +
56          # The $@ means substitute in the one and only output location, which will be located
57          # in //bazel-out, not in the fonts subdirectory (although it will be available to clients
58          # in the fonts/ subdirectory as if it had been there all along.
59          "--output=$@ " +
60          "--align=4",
61    exec_tools = ["//tools:embed_resources"],
62)
63
64# Note: These are defines that only impact the _bindings.cpp files in this folder.
65# Any defines that need to effect the entire Skia build should go in //bazel/BUILD.bazel
66CK_DEFINES = [
67    "CK_INCLUDE_PATHOPS",
68    "EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0",  # Allows us to compile with -fno-rtti
69] + select({
70    ":enable_fonts_true": ["CK_INCLUDE_PARAGRAPH"],
71    ":enable_fonts_false": ["CK_NO_FONTS"],
72}) + select({
73    ":enable_skp_serialization_true": ["CK_SERIALIZE_SKP=1"],
74    ":enable_skp_serialization_false": [],
75}) + select({
76    ":enable_runtime_effect_true": ["CK_INCLUDE_RUNTIME_EFFECT=1"],
77    ":enable_runtime_effect_false": [],
78}) + select({
79    "//src/gpu:gl_backend": ["CK_ENABLE_WEBGL"],
80    "//conditions:default": [],
81})
82
83CK_RELEASE_OPTS = [
84    "--closure 1",  # Run the closure compiler
85    # pass the externs file in
86    "--closure-args=--externs=$(location externs.js)",
87]
88
89CK_LINKOPTS = BASE_LINKOPTS + [
90    "-sEXPORT_NAME=CanvasKitInit",
91    "-sINITIAL_MEMORY=128MB",
92    # The order of these --pre-js flags matters! The preamble is a partially open scope and the
93    # postamble closes it. TODO(kjlubick) do we need to do it this way anymore?
94    "--pre-js",
95    "modules/canvaskit/preamble.js",
96    "--pre-js",
97    "modules/canvaskit/color.js",
98    "--pre-js",
99    "modules/canvaskit/memory.js",
100    "--pre-js",
101    "modules/canvaskit/util.js",
102    "--pre-js",
103    "modules/canvaskit/interface.js",
104    "--pre-js",
105    "modules/canvaskit/pathops.js",
106] + select({
107    "//src/gpu:gl_backend": [
108        "--pre-js",
109        "modules/canvaskit/cpu.js",
110        "--pre-js",
111        "modules/canvaskit/webgl.js",
112    ],
113    "//conditions:default": [
114        "--pre-js",
115        "modules/canvaskit/cpu.js",
116    ],
117}) + select({
118    ":enable_fonts_true": [
119        "--pre-js",
120        "modules/canvaskit/font.js",
121        "--pre-js",
122        "modules/canvaskit/paragraph.js",
123    ],
124    ":enable_fonts_false": [],
125}) + select({
126    ":enable_canvas_polyfill_true": [
127        "--pre-js",
128        "modules/canvaskit/htmlcanvas/preamble.js",
129        "--pre-js",
130        "modules/canvaskit/htmlcanvas/util.js",
131        "--pre-js",
132        "modules/canvaskit/htmlcanvas/color.js",
133        "--pre-js",
134        "modules/canvaskit/htmlcanvas/font.js",
135        "--pre-js",
136        "modules/canvaskit/htmlcanvas/canvas2dcontext.js",
137        "--pre-js",
138        "modules/canvaskit/htmlcanvas/htmlcanvas.js",
139        "--pre-js",
140        "modules/canvaskit/htmlcanvas/htmlimage.js",
141        "--pre-js",
142        "modules/canvaskit/htmlcanvas/imagedata.js",
143        "--pre-js",
144        "modules/canvaskit/htmlcanvas/lineargradient.js",
145        "--pre-js",
146        "modules/canvaskit/htmlcanvas/path2d.js",
147        "--pre-js",
148        "modules/canvaskit/htmlcanvas/pattern.js",
149        "--pre-js",
150        "modules/canvaskit/htmlcanvas/radialgradient.js",
151        "--pre-js",
152        "modules/canvaskit/htmlcanvas/postamble.js",
153    ],
154    ":enable_canvas_polyfill_false": [],
155}) + select({
156    ":enable_skottie_true": [
157        "--pre-js",
158        "modules/canvaskit/skottie.js",
159    ],
160    ":enable_skottie_false": [],
161}) + select({
162    ":enable_skp_serialization_true": [
163        "--pre-js",
164        "modules/canvaskit/skp.js",
165    ],
166    ":enable_skp_serialization_false": [],
167}) + select({
168    ":enable_runtime_effect_true": [
169        "--pre-js",
170        "modules/canvaskit/rt_shader.js",
171    ],
172    ":enable_runtime_effect_false": [],
173}) + select({
174    ":include_matrix_js_true": [
175        "--pre-js",
176        "modules/canvaskit/matrix.js",
177    ],
178    ":include_matrix_js_false": [],
179}) + [
180    # This must come last
181    "--pre-js",
182    "modules/canvaskit/postamble.js",
183] + select({
184    "//bazel/common_config_settings:debug_build": DEBUG_OPTS + [
185        "--pre-js",
186        "modules/canvaskit/debug.js",
187    ],
188    "//conditions:default": RELEASE_OPTS + CK_RELEASE_OPTS + [
189        "--pre-js",
190        "modules/canvaskit/release.js",
191    ],
192})
193
194# All JS files that could possibly be included via --pre-js or --post-js.
195# Whether they actually will be or not will be controlled above in the construction of CK_LINKOPTS.
196JS_INTERFACE_FILES = [
197    "color.js",
198    "cpu.js",
199    "debug.js",
200    "font.js",
201    "interface.js",
202    "matrix.js",
203    "memory.js",
204    "paragraph.js",
205    "pathops.js",
206    "postamble.js",
207    "preamble.js",
208    "release.js",
209    "rt_shader.js",
210    "skottie.js",
211    "skp.js",
212    "util.js",
213    "webgl.js",
214    "webgpu.js",
215] + [
216    "htmlcanvas/canvas2dcontext.js",
217    "htmlcanvas/color.js",
218    "htmlcanvas/font.js",
219    "htmlcanvas/htmlcanvas.js",
220    "htmlcanvas/htmlimage.js",
221    "htmlcanvas/imagedata.js",
222    "htmlcanvas/lineargradient.js",
223    "htmlcanvas/path2d.js",
224    "htmlcanvas/pattern.js",
225    "htmlcanvas/postamble.js",
226    "htmlcanvas/preamble.js",
227    "htmlcanvas/radialgradient.js",
228    "htmlcanvas/util.js",
229]
230
231CK_SRCS = [
232    "canvaskit_bindings.cpp",
233    ":hdrs",
234] + select({
235    ":include_embedded_font_true": ["fonts/NotoMono-Regular.ttf.bazel.cpp"],
236    ":include_embedded_font_false": [],
237}) + select({
238    ":enable_fonts_true": [
239        "paragraph_bindings.cpp",
240        "paragraph_bindings_gen.cpp",
241    ],
242    ":enable_fonts_false": [],
243}) + select({
244    ":enable_skottie_true": ["skottie_bindings.cpp"],
245    ":enable_skottie_false": [],
246})
247
248CK_COPTS = [
249    "-Wno-header-hygiene",
250]
251
252cc_binary(
253    name = "canvaskit.build",
254    srcs = CK_SRCS,
255    additional_linker_inputs = JS_INTERFACE_FILES + ["externs.js"],
256    copts = DEFAULT_COPTS + CK_COPTS,
257    linkopts = CK_LINKOPTS,
258    local_defines = CK_DEFINES,
259    # This target won't build successfully on its own because of missing emscripten
260    # headers etc. Therefore, we hide it from wildcards.
261    tags = ["manual"],
262    deps = [
263        "//:skia_public",
264    ] + select({
265        ":enable_fonts_true": [
266            "//modules/skparagraph",
267            "//modules/skunicode",
268        ],
269        ":enable_fonts_false": [],
270    }) + select({
271        ":enable_skottie_true": [
272            "//modules/skottie",
273            "//modules/skottie:utils",
274        ],
275        ":enable_skottie_false": [],
276    }),
277)
278
279wasm_cc_binary(
280    name = "canvaskit",
281    # Whatever is before the dot will be the name of the output js and wasm, aka "the stem".
282    # https://github.com/emscripten-core/emsdk/blob/82ad00499a42abde16b363239d2bc83bf5d863ab/bazel/emscripten_toolchain/wasm_cc_binary.bzl#L91
283    cc_target = ":canvaskit.build",
284)
285
286bool_flag(
287    name = "enable_canvas_polyfill",
288    default = False,
289)
290
291bool_flag(
292    name = "enable_fonts",
293    default = False,
294)
295
296bool_flag(
297    name = "include_embedded_font",
298    default = False,
299)
300
301bool_flag(
302    name = "include_matrix_js",
303    default = False,
304)
305
306bool_flag(
307    name = "enable_skottie",
308    default = False,
309)
310
311bool_flag(
312    name = "enable_skp_serialization",
313    default = False,
314)
315
316bool_flag(
317    name = "enable_runtime_effect",
318    default = False,
319)
320
321karma_test(
322    name = "canvaskit_js_tests",
323    srcs = [
324        ":canvaskit/canvaskit.js",
325        # We want to make sure the CanvasKit JS is loaded before the loader script, so
326        # CanvasKitInit is defined. This loader script makes a promise...
327        "tests/init_with_gold_server.js",
328        "tests/util.js",
329        "tests/bazel_test_reporter.js",
330        # ...which is used by all of the tests
331        "tests/canvas_test.js",
332        "tests/canvas2d_test.js",
333        "tests/core_test.js",
334        "tests/font_test.js",
335        "tests/matrix_test.js",
336        "tests/paragraph_test.js",
337        "tests/path_test.js",
338        "tests/rtshader_test.js",
339        "tests/skottie_test.js",
340    ],
341    config_file = "karma.bazel.js",
342    # The tests need the Gold server to be up and running so they can make POST requests to
343    # exfiltrate the PNGs they create.
344    env = "//modules/canvaskit/go/gold_test_env:gold_test_env",
345    static_files = [
346        ":canvaskit/canvaskit.wasm",
347        "//modules/canvaskit/tests/assets:test_assets",
348    ],
349)
350