Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aarch64 toolchain #278

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ bazel-*
# python files
__pycache__
.ipynb_checkpoints
.virtual_documents/
24 changes: 21 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
workspace(name = "robot")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "aarch64-none-linux-gnu",
urls = [
"https://toolchains.bootlin.com/downloads/releases/toolchains/aarch64/tarballs/aarch64--glibc--bleeding-edge-2020.08-1.tar.bz2"
],
strip_prefix="aarch64--glibc--bleeding-edge-2020.08-1",
build_file="//third_party:BUILD.aarch64-none-linux-gnu",
sha256 = "212f3c05f3b2263b0e2f902d055aecc2755eba10c0011927788a38faee8fc9aa"
)

http_archive(
name = "jetson_sysroot",
urls = ["https://www.dropbox.com/scl/fi/39qmmgn3mdnhj14sa21zl/cleaned_jetson.tar?rlkey=2fq8ynle042p6ojhprz3vjis3&dl=1"],
build_file = "//third_party:BUILD.jetson_sysroot",
integrity = "sha256-CVtgQSRXe8o2wMjNcJrxtlUxiHZY9ZQJ0kde8BkrTPw="
)

http_archive(
name = "platforms",
urls = [
Expand All @@ -14,6 +31,7 @@ register_toolchains(
"//toolchain:clang_toolchain_for_linux_x84_64",
"//toolchain:gcc_10_toolchain_for_linux_x84_64",
"//toolchain:gcc_11_toolchain_for_linux_x84_64",
"//toolchain:gcc_toolchain_for_linux_aarch64",
)

http_archive(
Expand Down Expand Up @@ -105,7 +123,7 @@ py_repositories()
DEFAULT_PYTHON_VERSION = "3.10"
python_register_multi_toolchains(
name="python",
python_versions = ['3.10', '3.8.10'],
python_versions = ['3.10', '3.8'],
default_version = DEFAULT_PYTHON_VERSION
)

Expand All @@ -115,11 +133,11 @@ multi_pip_parse(
name="pip",
default_version = DEFAULT_PYTHON_VERSION,
python_interpreter_target = {
"3.8.10": "@python_3_8_10_host//:python",
"3.8": "@python_3_8_host//:python",
"3.10": "@python_3_10_host//:python"
},
requirements_lock = {
"3.8.10": "//third_party/python:requirements_3_8_10.txt",
"3.8": "//third_party/python:requirements_3_8.txt",
"3.10": "//third_party/python:requirements_3_10.txt"
},
)
Expand Down
6 changes: 3 additions & 3 deletions common/python/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@pip//:requirements.bzl", "requirement", "data_requirement")
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
load("@rules_python//python:defs.bzl", "py_binary")
load("@python//3.8.10:defs.bzl", py_test_3_8_10 = "py_test")
load("@python//3.8:defs.bzl", py_test_3_8 = "py_test")

py_binary(
name = "jupyter_notebook",
Expand Down Expand Up @@ -47,8 +47,8 @@ py_test(
data = [":pybind_example_python.so"],
)

py_test_3_8_10(
name = "pybind_example_3_8_10_test",
py_test_3_8(
name = "pybind_example_3_8_test",
srcs = ["pybind_example_test.py"],
main = "pybind_example_test.py",
data = [":pybind_example_python.so"],
Expand Down
6 changes: 3 additions & 3 deletions common/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To add new versions of Python, locate the `python_register_multi_toolchains` inv
DEFAULT_PYTHON_VERSION = "3.10"
python_register_multi_toolchains(
name="python",
python_versions = ['3.10', '3.8.10'], # add new entry here
python_versions = ['3.10', '3.8'], # add new entry here
default_version = DEFAULT_PYTHON_VERSION
)

Expand All @@ -19,11 +19,11 @@ multi_pip_parse(
name="pip",
default_version = DEFAULT_PYTHON_VERSION,
python_interpreter_target = {
"3.8.10": "@python_3_8_10_host//:python",
"3.8": "@python_3_8_host//:python",
"3.10": "@python_3_10_host//:python"
},
requirements_lock = {
"3.8.10": "//third_party/python:requirements_3_8_10.txt",
"3.8": "//third_party/python:requirements_3_8.txt",
"3.10": "//third_party/python:requirements_3_10.txt"
# Add new entry here
},
Expand Down
5 changes: 4 additions & 1 deletion experimental/beacon_sim/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,10 @@ py_wheel(
name = "beacon_sim_wheel",
distribution = "beacon_sim",
version = "0.0.1",
platform = "manylinux2014_x86_64",
platform = select({
"@platforms//cpu:x86_64": "manylinux2014_x86_64",
"@platforms//cpu:aarch64": "manylinux2014_aarch64",
}),
python_tag = select({
"@rules_python//python/config_settings:is_python_3.10": "cp310",
"@rules_python//python/config_settings:is_python_3.8": "cp38",
Expand Down
6 changes: 6 additions & 0 deletions third_party/BUILD.aarch64-none-linux-gnu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

filegroup(
name = "all",
srcs = glob(["**/*"]),
visibility=["//visibility:public"],
)
6 changes: 6 additions & 0 deletions third_party/BUILD.jetson_sysroot
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

filegroup(
name = "jetson_sysroot",
srcs = glob(["**/*"]),
visibility=["//visibility:public"],
)
8 changes: 4 additions & 4 deletions third_party/python/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

load("@python//3.8.10:defs.bzl", compile_pip_requirements_3_8_10 = "compile_pip_requirements")
load("@python//3.8:defs.bzl", compile_pip_requirements_3_8 = "compile_pip_requirements")
load("@python//3.10:defs.bzl", compile_pip_requirements_3_10 = "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary")
load("@pip//:requirements.bzl", "requirement")

compile_pip_requirements_3_8_10(
name = "requirements_3_8_10",
compile_pip_requirements_3_8(
name = "requirements_3_8",
requirements_in=":requirements.in",
requirements_txt=":requirements_3_8_10.txt",
requirements_txt=":requirements_3_8.txt",
extra_args=["--allow-unsafe"],
timeout="eternal",
)
Expand Down
52 changes: 52 additions & 0 deletions toolchain/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

load(":clang_toolchain_config.bzl", "clang_toolchain_config")
load(":gcc_toolchain_config.bzl", "gcc_toolchain_config")
load(":gcc_aarch64_toolchain_config.bzl", "gcc_aarch64_toolchain_config")

package(default_visibility = ["//visibility:public"])

Expand All @@ -20,6 +21,10 @@ gcc_toolchain_config(
gcc_version="10",
)

gcc_aarch64_toolchain_config(
name="aarch64_gcc_toolchain_config",
)

cc_toolchain(
name="k8_clang_toolchain",
all_files = ":empty",
Expand Down Expand Up @@ -101,6 +106,45 @@ toolchain(
],
)

filegroup(
name = "aarch64_wrapper_scripts",
srcs = [
":aarch64-linux-ar.sh",
":aarch64-linux-gcc.sh",
":aarch64-linux-ld.sh",
"@aarch64-none-linux-gnu//:all",
"@jetson_sysroot"
],
)

cc_toolchain(
name="aarch64_gcc_toolchain",
all_files = ":aarch64_wrapper_scripts",
ar_files = ":aarch64_wrapper_scripts",
as_files = ":aarch64_wrapper_scripts",
compiler_files = ":aarch64_wrapper_scripts",
strip_files = ":aarch64_wrapper_scripts",
objcopy_files = ":aarch64_wrapper_scripts",
linker_files = ":aarch64_wrapper_scripts",
dwp_files = ":empty",
toolchain_config = ":aarch64_gcc_toolchain_config",
supports_param_files=0,
)

toolchain(
name = "gcc_toolchain_for_linux_aarch64",
toolchain = ":aarch64_gcc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_compatible_with = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
)

constraint_setting(name = "compiler")
constraint_value(
name="clang",
Expand Down Expand Up @@ -143,3 +187,11 @@ platform(
"@platforms//os:linux",
]
)

platform(
name = "gcc_aarch64",
constraint_values = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
]
)
3 changes: 3 additions & 0 deletions toolchain/aarch64-linux-ar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -euo pipefail
external/aarch64-none-linux-gnu/bin/aarch64-linux-ar "$@"
3 changes: 3 additions & 0 deletions toolchain/aarch64-linux-gcc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -euo pipefail
external/aarch64-none-linux-gnu/bin/aarch64-linux-gcc "$@"
3 changes: 3 additions & 0 deletions toolchain/aarch64-linux-ld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -euo pipefail
external/aarch64-none-linux-gnu/bin/aarch64-linux-gcc "$@"
140 changes: 140 additions & 0 deletions toolchain/aarch64/extract_sysroot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#! /usr/bin/env python3

# Note that this is not a bazel target
import subprocess
import tarfile
import re
import tempfile
import os
from pathlib import Path


def build_docker_container():
label = "jetson_sysroot:latest"
DOCKERFILE_CONTENT = """
FROM nvcr.io/nvidia/l4t-base:35.4.1

RUN apt-get update && apt-get install libblas-dev liblapack-dev && rm -rf /var/lib/apt/lists/*
"""
subprocess.run(["docker", "build", "-t", label, '-'], input=DOCKERFILE_CONTENT, text=True)
return label


def export_docker_filesystem(tag):
# Launch a container
result = subprocess.run(["docker", "run", "-dt", tag], capture_output=True, text=True)
container_id = result.stdout.strip()

TAR_FILE = "/tmp/jetson.tar"
with open(TAR_FILE, 'wb') as file_out:
subprocess.run(["docker", "export", container_id], stdout=file_out)

# Kill the container
subprocess.run(["docker", "kill", container_id])
return TAR_FILE


def clean_up_tar_file(file_path):
paths_to_exclude = [
"^etc/(?!alternatives).*",
"^run/.*",
"^usr/sbin/.*",
"^usr/share.*",
"^var/.*",
"^usr/include/X11/.*",
"^usr/bin/X11.*",
"^usr/lib/python.*",
"^usr/lib/systemd.*",
"^usr/lib/udev.*",
"^usr/lib/apt.*",
"^usr/lib/aarch64-linux-gnu/perl.*",
"^usr/lib/aarch64-linux-gnu/gtk.*",
"^usr/lib/aarch64-linux-gnu/gstreamer.*",
"^usr/lib/aarch64-linux-gnu/gdk.*",
"^usr/lib/aarch64-linux-gnu/libLLVM.*",
"^usr/lib/aarch64-linux-gnu/libicudata.*",
"^usr/lib/aarch64-linux-gnu/dri/.*",
"^usr/lib/aarch64-linux-gnu/libcodec.*",
"^usr/lib/aarch64-linux-gnu/libavcodec.*",
"^usr/lib/aarch64-linux-gnu/libgtk.*",
"^usr/lib/aarch64-linux-gnu/librsvg.*",
"^usr/lib/python.*",
]

f = tarfile.TarFile(file_path, mode='r')
remaining = []

size_total = 0
total = 0
size_kept = 0
kept = 0
for member in f:
total += 1
size_total += member.size
should_keep = True
# print('to test:', member.path)
for pattern in paths_to_exclude:
match = re.match(pattern, member.path)
# print('on pattern:', pattern, match)
if match is not None:
should_keep = False
break

if should_keep:
kept += 1
size_kept += member.size
remaining.append(member)

for member in remaining:
print(member, member.size / (1024 ** 2))

print(f" Files kept / total: {kept} / {total}")
print(f" Megabytes kept / total: {size_kept / 1024 / 1024} / {size_total / 1024 / 1024}")
CLEANED_TAR_FILE = "/tmp/cleaned_jetson.tar"
with tempfile.TemporaryDirectory() as tempdir:
f.extractall(path=tempdir, members=remaining)
f.close()

# make some symlinks
for obj in ["crt1.o", "crti.o", "crtn.o"]:
src_path = os.path.join(tempdir, f'lib/aarch64-linux-gnu/{obj}')
target_path = os.path.join(tempdir, f'lib/{obj}')

target_dir = os.path.dirname(target_path)
source_dir = os.path.dirname(src_path)
rel_src = os.path.relpath(src_path, target_dir)
print(f'src: {src_path} target: {target_path} target_dir: {target_dir} rel_src: {rel_src}')
os.symlink(rel_src, target_path)

# edit /lib/aarch64-linux-gnu/libc.so
libc_path = Path(tempdir) / 'lib/aarch64-linux-gnu/libc.so'
contents = libc_path.read_text()
contents = contents.replace('/usr/lib/aarch64-linux-gnu/', '')
contents = contents.replace('/lib/aarch64-linux-gnu/', '')
libc_path.write_text(contents)

cleaned = tarfile.TarFile(CLEANED_TAR_FILE, mode='w')
cleaned.add(tempdir, arcname='/')
cleaned.close()
return CLEANED_TAR_FILE



def main():
# Build the docker container
print('Building Docker Container')
tag = build_docker_container()

# Export the filesystem as a tar file
print('Exporting Docker Container')
file_path = export_docker_filesystem(tag)

# Remove unneeded files
print('Removing Unneeded files')
cleaned_file_path = clean_up_tar_file(file_path)
print(cleaned_file_path)


if __name__ == "__main__":
main()