From d4c5f96bc8794f057edfa82b5ef49ee60c2c3756 Mon Sep 17 00:00:00 2001 From: baydakov-georgiy Date: Sat, 18 Apr 2026 19:30:45 +0300 Subject: [PATCH] feat (worker): add cross compiling with Yocto SDK --- worker/.bazelrc | 2 + worker/BUILD | 98 +++++++++++++------ worker/MODULE.bazel | 9 ++ worker/third_party/system_zlib/BUILD | 5 + worker/third_party/system_zlib/MODULE.bazel | 4 + worker/toolchains/riscv/BUILD | 2 +- .../toolchains/riscv/cc_toolchain_config.bzl | 71 ++++++++------ 7 files changed, 131 insertions(+), 60 deletions(-) create mode 100644 worker/.bazelrc create mode 100644 worker/third_party/system_zlib/BUILD create mode 100644 worker/third_party/system_zlib/MODULE.bazel diff --git a/worker/.bazelrc b/worker/.bazelrc new file mode 100644 index 0000000..4f53628 --- /dev/null +++ b/worker/.bazelrc @@ -0,0 +1,2 @@ +build:riscv64 --platforms=//platforms:riscv64_linux +build:riscv64 --extra_toolchains=//toolchains/riscv:cc_toolchain_for_linux_riscv64 diff --git a/worker/BUILD b/worker/BUILD index c949c49..755ffca 100644 --- a/worker/BUILD +++ b/worker/BUILD @@ -71,42 +71,82 @@ cc_binary( "@curl//:curl", ], copts = [ - "-mssse3", - "-msse4.2", - "-mpclmul", - "-maes", "-I$(GENDIR)/..", - "-I/usr/include", - ], + ] + select({ + "@platforms//cpu:x86_64": [ + "-mssse3", + "-msse4.2", + "-mpclmul", + "-maes", + "-I/usr/include", + ], + "@platforms//cpu:riscv64": [], + "//conditions:default": ["-I/usr/include"], + }), cxxopts = [ - "-std=c++17", + "-std=c++17", ], linkopts = [ - "-L/usr/local/openssl/lib", - "-lssl", - "-lcrypto", - - "-L/usr/local/lib", "-lprometheus-cpp-push", "-lprometheus-cpp-core", - - "-L/usr/lib", - "-lrte_eal", - "-lrte_ethdev", - "-lrte_mempool", - "-lrte_mbuf", - "-lrte_bus_vdev", - "-lrte_ring", - "-lrte_telemetry", - "-lrte_kvargs", - "-lrte_log", - "-lrte_net", - "-lrte_hash", - "-lrte_timer", - "-lsqlite3", - + "-lsqlite3", + "-lssl", + "-lcrypto", "-lnuma", "-ldl", "-lpthread", - ], + ] + select({ + "@platforms//cpu:x86_64": [ + "-L/usr/local/openssl/lib", + "-L/usr/local/lib", + "-L/usr/lib", + "-lrte_eal", + "-lrte_ethdev", + "-lrte_mempool", + "-lrte_mbuf", + "-lrte_bus_vdev", + "-lrte_ring", + "-lrte_telemetry", + "-lrte_kvargs", + "-lrte_log", + "-lrte_net", + "-lrte_hash", + "-lrte_timer", + ], + "@platforms//cpu:riscv64": [ + "-Wl,--whole-archive", + "-lrte_eal", + "-lrte_ethdev", + "-lrte_mempool", + "-lrte_mbuf", + "-lrte_bus_vdev", + "-lrte_ring", + "-lrte_telemetry", + "-lrte_kvargs", + "-lrte_log", + "-lrte_net", + "-lrte_hash", + "-lrte_timer", + "-lrte_net_af_xdp", + "-Wl,--no-whole-archive", + "-lbpf", + "-lxdp", + "-lelf", + "-lz", + ], + "//conditions:default": [ + "-lrte_eal", + "-lrte_ethdev", + "-lrte_mempool", + "-lrte_mbuf", + "-lrte_bus_vdev", + "-lrte_ring", + "-lrte_telemetry", + "-lrte_kvargs", + "-lrte_log", + "-lrte_net", + "-lrte_hash", + "-lrte_timer", + ], + }), ) diff --git a/worker/MODULE.bazel b/worker/MODULE.bazel index 6f3f40d..40f1906 100644 --- a/worker/MODULE.bazel +++ b/worker/MODULE.bazel @@ -12,3 +12,12 @@ bazel_dep(name = "bazel_skylib", version = "1.8.1") bazel_dep(name = "rules_proto", version = "7.0.2") bazel_dep(name = "protobuf", version = "31.1", repo_name = "com_google_protobuf") bazel_dep(name = "grpc", version = "1.78.0") + +register_toolchains( + "//toolchains/riscv:cc_toolchain_for_linux_riscv64", +) + +local_path_override( + module_name = "zlib", + path = "third_party/system_zlib", +) diff --git a/worker/third_party/system_zlib/BUILD b/worker/third_party/system_zlib/BUILD new file mode 100644 index 0000000..12d5f25 --- /dev/null +++ b/worker/third_party/system_zlib/BUILD @@ -0,0 +1,5 @@ +cc_library( + name = "zlib", + linkopts = ["-lz"], + visibility = ["//visibility:public"], +) diff --git a/worker/third_party/system_zlib/MODULE.bazel b/worker/third_party/system_zlib/MODULE.bazel new file mode 100644 index 0000000..73f79ce --- /dev/null +++ b/worker/third_party/system_zlib/MODULE.bazel @@ -0,0 +1,4 @@ +module( + name = "zlib", + version = "1.3.1", +) diff --git a/worker/toolchains/riscv/BUILD b/worker/toolchains/riscv/BUILD index e4eb4c0..857f10d 100644 --- a/worker/toolchains/riscv/BUILD +++ b/worker/toolchains/riscv/BUILD @@ -7,7 +7,7 @@ filegroup(name = "empty") cc_toolchain( name = "linux_riscv64_toolchain", - toolchain_identifier = "linux_riscv64-toolchain", + toolchain_identifier = "riscv-yocto-toolchain", toolchain_config = ":linux_riscv64_toolchain_config", all_files = ":empty", compiler_files = ":empty", diff --git a/worker/toolchains/riscv/cc_toolchain_config.bzl b/worker/toolchains/riscv/cc_toolchain_config.bzl index b948c60..a954a7b 100644 --- a/worker/toolchains/riscv/cc_toolchain_config.bzl +++ b/worker/toolchains/riscv/cc_toolchain_config.bzl @@ -18,19 +18,24 @@ all_compile_actions = [ ACTION_NAMES.c_compile, ] +_SDK_BASE = "/opt/riscv-sdk" +_SDK_HOST = _SDK_BASE + "/sysroots/x86_64-pokysdk-linux" +_SDK_TARGET = _SDK_BASE + "/sysroots/riscv64-poky-linux" +_TOOLCHAIN_BIN = _SDK_HOST + "/usr/bin/riscv64-poky-linux" + def _impl(ctx): tool_paths = [ tool_path( name = "gcc", - path = "/usr/bin/riscv64-linux-gnu-g++", + path = _TOOLCHAIN_BIN + "/riscv64-poky-linux-gcc", ), tool_path( name = "ld", - path = "/usr/bin/riscv64-linux-gnu-ld", + path = _TOOLCHAIN_BIN + "/riscv64-poky-linux-ld", ), tool_path( name = "ar", - path = "/usr/bin/riscv64-linux-gnu-ar", + path = _TOOLCHAIN_BIN + "/riscv64-poky-linux-ar", ), tool_path( name = "cpp", @@ -42,19 +47,37 @@ def _impl(ctx): ), tool_path( name = "nm", - path = "/usr/bin/riscv64-linux-gnu-nm", + path = _TOOLCHAIN_BIN + "/riscv64-poky-linux-nm", ), tool_path( name = "objdump", - path = "/usr/bin/riscv64-linux-gnu-objdump", + path = _TOOLCHAIN_BIN + "/riscv64-poky-linux-objdump", ), tool_path( name = "strip", - path = "/usr/bin/riscv64-linux-gnu-strip", + path = _TOOLCHAIN_BIN + "/riscv64-poky-linux-strip", ), ] features = [ + feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = [ + "--sysroot=" + _SDK_TARGET, + "-O2", + "-pipe", + ], + ), + ], + ), + ], + ), feature( name = "default_linker_flags", enabled = True, @@ -64,16 +87,10 @@ def _impl(ctx): flag_groups = [ flag_group( flags = [ - "-L/usr/riscv64-linux-gnu/lib", - "-Wl,-rpath=/usr/riscv64-linux-gnu/lib", - "-Wl,--start-group", - "-lprometheus-cpp-push", - "-lprometheus-cpp-core", - "-lcurl", - "-lssl", - "-lcrypto", - "-lz", - "-Wl,--end-group", + "--sysroot=" + _SDK_TARGET, + "-L" + _SDK_TARGET + "/usr/lib", + "-Wl,-rpath=" + _SDK_TARGET + "/usr/lib", + "-lstdc++", ], ), ], @@ -90,9 +107,6 @@ def _impl(ctx): flag_group( flags = [ "-std=c++17", - "-isystem/usr/riscv64-linux-gnu/include/c++/11", - "-isystem/usr/riscv64-linux-gnu/include/c++/11/riscv64-linux-gnu", - "-isystem/usr/lib/gcc-cross/riscv64-linux-gnu/11/include", ], ), ], @@ -105,22 +119,19 @@ def _impl(ctx): ctx = ctx, features = features, cxx_builtin_include_directories = [ - "/usr/riscv64-linux-gnu/include/c++/11", - "/usr/riscv64-linux-gnu/include/c++/11/riscv64-linux-gnu", - "/usr/lib/gcc-cross/riscv64-linux-gnu/11/include", - "/usr/lib/gcc-cross/riscv64-linux-gnu/11/include-fixed", - "/usr/riscv64-linux-gnu/include", - "/usr/include", - "/usr/local/include", + _SDK_HOST + "/usr/lib/riscv64-poky-linux/gcc/riscv64-poky-linux/13.4.0/include", + _SDK_TARGET + "/usr/include/c++/13.4.0", + _SDK_TARGET + "/usr/include/c++/13.4.0/riscv64-poky-linux", + _SDK_TARGET + "/usr/include", ], - toolchain_identifier = "riscv-toolchain", + toolchain_identifier = "riscv-yocto-toolchain", host_system_name = "local", - target_system_name = "riscv64-linux-gnu", + target_system_name = "riscv64-poky-linux", target_cpu = "riscv64", target_libc = "glibc", compiler = "gcc", abi_version = "lp64d", - abi_libc_version = "glibc_2.36", + abi_libc_version = "glibc_2.39", tool_paths = tool_paths, ) @@ -128,4 +139,4 @@ cc_toolchain_config = rule( implementation = _impl, attrs = {}, provides = [CcToolchainConfigInfo], -) \ No newline at end of file +)