skia2/toolchain/build_toolchain.bzl
Kevin Lubick 7a14f783bd [bazel] Sketching out HelloWorld sk_app using GL
bazel run //example:hello_world --config=clang
causes a window to open and draws a circle and a square.
Text to follow in a future CL.

To make this work, I had to get rid of musl and use glibc.
All the shared libraries (.so files) that were pre-built
and available for download (e.g. from https://packages.debian.org/bullseye/amd64/libgl1/download)
were compiled against glibc. When I tried to run a
program statically linked with musl and dynamically linked
against things using glibc, I got a segmentation fault
on things like calloc().

Initial attempts to use glibc had failed because it was thought
that the libc.so.6 file could only be referred to by absolute
path (and thus Bazel would not be happy about it). As it turns out,
that was simply a misconfiguration of the builtin_sysroot
parameter to cc_common.create_cc_toolchain_config_info
(see //toolchain/clang_toolchain_config.bzl). By setting that
to `external/clang_linux_amd64` and not
`external/clang_linux_amd64/usr`, the libc binary which had
been extracted to `external/clang_linux_amd64/lib/x86_64-linux-gnu`
was perfectly reachable from
`external/clang_linux_amd64/usr/usr/lib/x86_64-linux-gnu/libc.so`

To bring in the shared libraries to link against (e.g. X11, GL)
I made build_toolchain.bzl easier to modify in that we simply need
to add a debian download url and sha256 hash to a list (rather than
having to plumb this through via arguments).

Recommended Review Order:
 - example/BUILD.bazel (not sure if we always want to set bare
   link arguments like that or if we want to use "features" to
   pass those along to the toolchain).
 - tools/sk_app/BUILD.bazel to see initial cc_library for
   wrapping sk_app code.
 - toolchain/build_toolchain.bzl to see removal of musl and
   new list of debs.
 - toolchain/clang_toolchain_config.bzl (where use of the
   no-canonical-prefixes was key to compilation success).
   Notice also that we statically linked libc++ (I did not
   have any shared libraries for it locally, so I guessed
   a typical developer might not either).
 - Rest of toolchain/ for trivial renames.
 - bazel/Makefile to see extra docs on those targets and
   a new target that compiles all the exes so far for a
   quick way to test the build.
 - third_party/BUILD.bazel and src/gpu/BUILD.bazel which have
   non-generated changes. (all other BUILD.bazel files do).
 - go.mod, which needed to update the infra repo version in
   order to pick up http://review.skia.org/491736).

Change-Id: I8687bd227353040eca2dffa9465798d8bd395027
Bug: skia:12541
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/492117
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2022-01-11 13:06:19 +00:00

154 lines
6.6 KiB
Python

"""
This file assembles a toolchain for Linux using the Clang Compiler glibc.
It downloads the necessary header files and pre-compiled static/shared libraries to
"""
# From https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz.sha256
clang_prefix = "clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04/"
clang_sha256 = "2c2fb857af97f41a5032e9ecadf7f78d3eff389a5cd3c9ec620d24f134ceb3c8"
clang_url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz"
debs_to_install = [
# These three comprise glibc. libc6 has the shared libraries, like libc itself, the math library
# (libm), etc. linux-libc-dev has the header files specific to linux. libc6-dev has the libc
# system headers (e.g. malloc.h, math.h).
{
# From https://packages.debian.org/bullseye/amd64/libc6/download
"sha256": "3d9421c3fc0ef0d8ce57c0a149e1f8dbad78aba067f120be9e652af28902e346",
"url": "http://ftp.debian.org/debian/pool/main/g/glibc/libc6_2.31-13+deb11u2_amd64.deb",
},
{
# From https://packages.debian.org/bullseye/amd64/linux-libc-dev/download
"sha256": "1bb053863873916cb8d5fa877cc4972a6279931783c1fd9e4339d0369a617af4",
"url": "http://ftp.debian.org/debian/pool/main/l/linux/linux-libc-dev_5.10.84-1_amd64.deb",
},
{
# From https://packages.debian.org/bullseye/amd64/libc6-dev/download
"sha256": "1911bac1137f8f51359047d2fc94053f831abcfb50f1d7584e3ae95ea0831569",
"url": "http://ftp.debian.org/debian/pool/main/g/glibc/libc6-dev_2.31-13+deb11u2_amd64.deb",
},
# These two put the X11 include files in ${PWD}/usr/include/X11
# libx11-dev puts libX11.a in ${PWD}/usr/lib/x86_64-linux-gnu
{
# From https://packages.debian.org/bullseye/amd64/libx11-dev/download
"sha256": "11e5f9dcded1a1226b3ee02847b86edce525240367b3989274a891a43dc49f5f",
"url": "http://ftp.debian.org/debian/pool/main/libx/libx11/libx11-dev_1.7.2-1_amd64.deb",
},
{
# From https://packages.debian.org/bullseye/all/x11proto-dev/download
"sha256": "d5568d587d9ad2664c34c14b0ac538ccb3c567e126ee5291085a8de704a565f5",
"url": "http://ftp.debian.org/debian/pool/main/x/xorgproto/x11proto-dev_2020.1-1_all.deb",
},
# xcb is a dep of X11
{
# From https://packages.debian.org/bullseye/amd64/libxcb1-dev/download
"sha256": "b75544f334c8963b8b7b0e8a88f8a7cde95a714dddbcda076d4beb669a961b58",
"url": "http://ftp.debian.org/debian/pool/main/libx/libxcb/libxcb1-dev_1.14-3_amd64.deb",
},
# Xau is a dep of xcb
{
# From https://packages.debian.org/bullseye/amd64/libxau-dev/download
"sha256": "d1a7f5d484e0879b3b2e8d512894744505e53d078712ce65903fef2ecfd824bb",
"url": "http://ftp.debian.org/debian/pool/main/libx/libxau/libxau-dev_1.0.9-1_amd64.deb",
},
# Xdmcp is a dep of xcb. libxdmcp-dev provides the the libXdmcp.so symlink (and the
# .a if we want to statically include it). libxdmcp6 actually provides the .so file
{
# https://packages.debian.org/bullseye/amd64/libxdmcp-dev/download
"sha256": "c6733e5f6463afd261998e408be6eb37f24ce0a64b63bed50a87ddb18ebc1699",
"url": "http://ftp.debian.org/debian/pool/main/libx/libxdmcp/libxdmcp-dev_1.1.2-3_amd64.deb",
},
{
# https://packages.debian.org/bullseye/amd64/libxdmcp6/download
"sha256": "ecb8536f5fb34543b55bb9dc5f5b14c9dbb4150a7bddb3f2287b7cab6e9d25ef",
"url": "http://ftp.debian.org/debian/pool/main/libx/libxdmcp/libxdmcp6_1.1.2-3_amd64.deb",
},
# These two put GL include files in ${PWD}/usr/include/GL
{
# From https://packages.debian.org/bullseye/amd64/libgl-dev/download
"sha256": "a6487873f2706bbabf9346cdb190f47f23a1464f31cecf92c363bac37c342f2f",
"url": "http://ftp.debian.org/debian/pool/main/libg/libglvnd/libgl-dev_1.3.2-1_amd64.deb",
},
{
# From https://packages.debian.org/bullseye/amd64/libglx-dev/download
"sha256": "5a50549948bc4363eab32b1083dad2165402c3628f2ee85e9a32563228cc61c1",
"url": "http://ftp.debian.org/debian/pool/main/libg/libglvnd/libglx-dev_1.3.2-1_amd64.deb",
},
# This provides libGL.so for us to link against.
{
# From https://packages.debian.org/bullseye/amd64/libgl1/download
"sha256": "f300f9610b5f05f1ce566c4095f1bf2170e512ac5d201c40d895b8fce29dec98",
"url": "http://ftp.debian.org/debian/pool/main/libg/libglvnd/libgl1_1.3.2-1_amd64.deb",
},
]
def _download_and_extract_deb(ctx, deb, sha256, prefix, output = ""):
"""Downloads a debian file and extracts the data into the provided output directory"""
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download
download_info = ctx.download(
url = deb,
output = "deb.ar",
sha256 = sha256,
)
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#execute
# This uses the extracted llvm-ar that comes with clang.
ctx.execute(["bin/llvm-ar", "x", "deb.ar"])
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#extract
extract_info = ctx.extract(
archive = "data.tar.xz",
output = output,
stripPrefix = prefix,
)
# Clean up
ctx.delete("deb.ar")
ctx.delete("data.tar.xz")
ctx.delete("control.tar.xz")
def _build_cpp_toolchain_impl(ctx):
# Download the clang toolchain (the extraction can take a while)
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download
download_info = ctx.download_and_extract(
url = clang_url,
output = "",
stripPrefix = clang_prefix,
sha256 = clang_sha256,
)
# Extract all the debs into our sysroot. This is very similar to installing them, except their
# dependencies are not installed automatically.
for deb in debs_to_install:
_download_and_extract_deb(
ctx,
deb["url"],
deb["sha256"],
".",
)
# Create a BUILD.bazel file that makes all the files in this subfolder
# available for use in rules, i.e. in the toolchain declaration.
# https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#file
ctx.file(
"BUILD.bazel",
content = """
filegroup(
name = "all_files",
srcs = glob([
"**",
]),
visibility = ["//visibility:public"]
)
""",
executable = False,
)
build_cpp_toolchain = repository_rule(
implementation = _build_cpp_toolchain_impl,
attrs = {},
doc = "",
)