skia2/modules/canvaskit/BUILD.bazel
Kevin Lubick 888d4efa77 [canvaskit] Add Freetype/Fonts to Bazel Build
This re-works src/ports/BUILD.bazel to work like our other
BUILD files, i.e. one rule "srcs" that brings in the necessary
private filegroups.

To work around an abort with LLVM [1], we have to go back to an
earlier version of emscripten (temporarily?).

Future work should look at using transitions [2] to allow various
executables (e.g. CanvasKit, DM) to set their own set of Bazel
flags, w/o the build invokers having to specify them.

These transitions might be able to handle more complex cases
that we currently use if statements in GN to deal with.

The Freetype build rule was created by taking the BUILD.gn
rule, adding in all the sources listed there and then playing
compile-whack-a-mole to add in all the headers and included
.c files.

Suggested Review Order:
 - third_party/BUILD.bazel to see freetype build rules
 - bazel/common_config_settings/ to see treatment of fontmgr
   like codecs (many possible) and fontmgr_factory (only one).
 - src/ports/BUILD.bazel
 - BUILD.bazel
 - modules/canvaskit/BUILD.bazel. Take note of the gen_rule that
   calls tools/embed_resources.py to produce the .cpp file
   containing the embedded font data.
 - Everything else.

[1] https://github.com/emscripten-core/emscripten/issues/15528
[2] https://github.com/bazelbuild/examples/tree/main/rules/starlark_configurations/cc_binary_selectable_copts
Bug: skia:12541
Change-Id: I08dab82a901d80507007b354ca20cbfad2c2388f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/471636
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
2021-11-17 13:06:35 +00:00

206 lines
5.5 KiB
Python

load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
load("//bazel/common_config_settings:defs.bzl", "bool_flag")
package(default_visibility = ["//:__subpackages__"])
BASE_LINKOPTS = [
#"-flto", # https://github.com/emscripten-core/emsdk/issues/807
"--bind", # Compiles the source code using the Embind bindings to connect C/C++ and JavaScript
"--no-entry",
"-sALLOW_MEMORY_GROWTH",
"-sUSE_PTHREADS=0", # Disable pthreads
"-sMODULARIZE",
"-sDISABLE_EXCEPTION_CATCHING", # Disable all exception catching
"-sNODEJS_CATCH_EXIT=0", # We don't have a 'main' so disable exit() catching
"-sWASM",
"-sMAX_WEBGL_VERSION=2",
"-sFORCE_FILESYSTEM=0",
"-sFILESYSTEM=0",
]
RELEASE_OPTS = [
# We disable closure for now, because we need a way to pass in the externs file,
# which does not appear to be exposed on the emscripten toolchain.
# "--closure 1", # Run the closure compiler
"-sASSERTIONS=0", # Turn off assertions
]
DEBUG_OPTS = [
"--closure 0", # Do not use closure
"-sASSERTIONS", # Turn on assertions
"-sGL_ASSERTIONS",
]
GM_OPTS = [
"-sEXPORT_NAME=InitWasmGMTests",
"--pre-js",
"modules/canvaskit/gm.js",
]
filegroup(
name = "hdrs",
srcs = [
"WasmCommon.h",
],
)
cc_binary(
name = "gm-bindings",
testonly = True,
srcs = [
"gm_bindings.cpp",
":hdrs",
"//gm:gms", # Required for the registry to work
],
additional_linker_inputs = ["gm.js"],
linkopts = select({
"//bazel/common_config_settings:debug_build": BASE_LINKOPTS + GM_OPTS + DEBUG_OPTS,
"//bazel/common_config_settings:release_build": BASE_LINKOPTS + GM_OPTS + RELEASE_OPTS,
"//conditions:default": BASE_LINKOPTS + GM_OPTS + RELEASE_OPTS,
}),
local_defines = [
"SK_GL",
"SK_USE_WEBGL",
],
# This target won't build successfully on its own because of missing emscripten
# headers etc. Therefore, we hide it from wildcards.
tags = ["manual"],
deps = [
"//:gms",
"//:hash_and_encode",
"//:tests",
],
)
wasm_cc_binary(
name = "gm-bindings-wasm",
testonly = True,
cc_target = ":gm-bindings",
)
# See https://stackoverflow.com/a/57499321 for reference.
genrule(
name = "create_notomono_cpp",
srcs = ["fonts/NotoMono-Regular.ttf"],
outs = ["fonts/NotoMono-Regular.ttf.bazel.cpp"], # Distinct name from compile.sh's version
cmd = "$(location //tools:embed_resources) --name=SK_EMBEDDED_FONTS " +
"--input=modules/canvaskit/fonts/NotoMono-Regular.ttf " +
# The $@ means substitute in the one and only output location, which will be located
# in //bazel-out, not in the fonts subdirectory (although it will be available to clients
# in the fonts/ subdirectory as if it had been there all along.
"--output=$@ " +
"--align=4",
tools = ["//tools:embed_resources"],
)
CK_DEFINES = [
"SK_DISABLE_LEGACY_SHADERCONTEXT",
"SK_FORCE_8_BYTE_ALIGNMENT", # working around https://github.com/emscripten-core/emscripten/issues/10072
] + select({
"//bazel/common_config_settings:gl_backend": [
"SK_GL",
"SK_SUPPORT_GPU=1",
],
"//conditions:default": [
"SK_SUPPORT_GPU=0",
],
}) + select({
":enable_fonts_true": [],
":enable_fonts_false": ["SK_NO_FONTS"],
})
CK_OPTS = BASE_LINKOPTS + [
"-sEXPORT_NAME=CanvasKitInit",
"-sINITIAL_MEMORY=128MB",
# The order of these --pre-js flags matters! The preamble is a partially open scope and the
# postamble closes it.
"--pre-js",
"modules/canvaskit/preamble.js",
"--pre-js",
"modules/canvaskit/color.js",
"--pre-js",
"modules/canvaskit/memory.js",
"--pre-js",
"modules/canvaskit/util.js",
"--pre-js",
"modules/canvaskit/interface.js",
] + select({
"//bazel/common_config_settings:gl_backend": [
"--pre-js",
"modules/canvaskit/cpu.js",
"--pre-js",
"modules/canvaskit/gpu.js",
],
"//conditions:default": [
"--pre-js",
"modules/canvaskit/cpu.js",
],
}) + select({
":enable_fonts_true": [
"--pre-js",
"modules/canvaskit/font.js",
],
":enable_fonts_false": [],
}) + [
"--pre-js",
"modules/canvaskit/postamble.js",
] + select({
"//bazel/common_config_settings:debug_build": DEBUG_OPTS + [
"--pre-js",
"modules/canvaskit/debug.js",
],
"//conditions:default": RELEASE_OPTS + [
"--pre-js",
"modules/canvaskit/release.js",
],
})
CK_SRCS = [
"canvaskit_bindings.cpp",
":hdrs",
] + select({
":include_embedded_font_true": ["fonts/NotoMono-Regular.ttf.bazel.cpp"],
":include_embedded_font_false": [],
})
cc_binary(
name = "canvaskit",
srcs = CK_SRCS,
additional_linker_inputs = [
"color.js",
"cpu.js",
"debug.js",
"font.js",
"gpu.js",
"interface.js",
"memory.js",
"postamble.js",
"preamble.js",
"release.js",
"util.js",
],
linkopts = CK_OPTS,
local_defines = CK_DEFINES,
# This target won't build successfully on its own because of missing emscripten
# headers etc. Therefore, we hide it from wildcards.
tags = ["manual"],
deps = [
"//:skia-core",
],
)
wasm_cc_binary(
name = "canvaskit-wasm",
cc_target = ":canvaskit",
)
bool_flag(
default = True,
flag_name = "enable_fonts",
)
bool_flag(
default = True,
flag_name = "include_embedded_font",
)