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