From e7d96f033759478efa06d69552df0b58e3160aec Mon Sep 17 00:00:00 2001 From: Malte Poll <1780588+malt3@users.noreply.github.com> Date: Thu, 23 Nov 2023 17:29:32 +0100 Subject: [PATCH] bazel: import C libraries from nix as cc_libary This also includes aliases to select the correct library based on the target platform. --- WORKSPACE.bazel | 9 ++- nix/cc/BUILD.bazel | 68 ++++++++++++++++++++ nix/cc/nixpkgs_cc_libraries.bzl | 109 ++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 nix/cc/BUILD.bazel create mode 100644 nix/cc/nixpkgs_cc_libraries.bzl diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 664992c34..94d308a7b 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -98,6 +98,10 @@ nixpkgs_package( repository = "@nixpkgs", ) +load("//nix/cc:nixpkgs_cc_libraries.bzl", "nixpkgs_cc_library_deps") + +nixpkgs_cc_library_deps() + load("//bazel/mkosi:mkosi_configure.bzl", "register_mkosi") register_mkosi( @@ -201,7 +205,10 @@ load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains" zig_toolchains() -nixpkgs_cc_configure(repository = "@nixpkgs") +nixpkgs_cc_configure( + name = "nixpkgs_cc_toolchain", + repository = "@nixpkgs", +) register_toolchains( "@zig_sdk//libc_aware/toolchain:linux_amd64_gnu.2.23", diff --git a/nix/cc/BUILD.bazel b/nix/cc/BUILD.bazel new file mode 100644 index 000000000..fd3fed60f --- /dev/null +++ b/nix/cc/BUILD.bazel @@ -0,0 +1,68 @@ +load("@bazel_skylib//lib:selects.bzl", "selects") + +alias( + name = "org_openssl", + actual = select({ + ":aarch64-darwin": "@org_openssl_aarch64-darwin//:org_openssl", + ":aarch64-linux": "@org_openssl_aarch64-linux//:org_openssl", + ":x86_64-darwin": "@org_openssl_x86_64-darwin//:org_openssl", + ":x86_64-linux": "@org_openssl_x86_64-linux//:org_openssl", + }), + visibility = ["//visibility:public"], +) + +alias( + name = "cryptsetup", + actual = select({ + ":x86_64-linux": "@cryptsetup_x86_64-linux//:cryptsetup", + }), + visibility = ["//visibility:public"], +) + +alias( + name = "libvirt", + actual = select({ + ":x86_64-linux": "@libvirt_x86_64-linux//:libvirt", + }), + visibility = ["//visibility:public"], +) + +alias( + name = "cryptsetup_rpath", + actual = select({ + ":x86_64-linux": "@cryptsetup_x86_64-linux//:rpath", + }), + visibility = ["//visibility:public"], +) + +selects.config_setting_group( + name = "aarch64-linux", + match_all = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], +) + +selects.config_setting_group( + name = "aarch64-darwin", + match_all = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], +) + +selects.config_setting_group( + name = "x86_64-darwin", + match_all = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], +) + +selects.config_setting_group( + name = "x86_64-linux", + match_all = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) diff --git a/nix/cc/nixpkgs_cc_libraries.bzl b/nix/cc/nixpkgs_cc_libraries.bzl new file mode 100644 index 000000000..929ccf156 --- /dev/null +++ b/nix/cc/nixpkgs_cc_libraries.bzl @@ -0,0 +1,109 @@ +""" Bazel cc_library definitions for Nixpkgs. """ + +load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_flake_package") + +def nixpkgs_cc_library_deps(): + """ Generate cc_library rules for Nixpkgs. """ + return [ + nixpkgs_flake_package( + name = "org_openssl_%s" % system, + nix_flake_file = "//:flake.nix", + nix_flake_lock_file = "//:flake.lock", + package = "packages.%s.openssl" % system, + build_file_content = OPENSSL_BUILD, + ) + for system in openssl_systems + ] + [ + nixpkgs_flake_package( + name = "cryptsetup_%s" % system, + nix_flake_file = "//:flake.nix", + nix_flake_lock_file = "//:flake.lock", + package = "cryptsetup", + build_file_content = CRYPTSETUP_BUILD, + ) + for system in cryptsetup_systems + ] + [ + nixpkgs_flake_package( + name = "libvirt_%s" % system, + nix_flake_file = "//:flake.nix", + nix_flake_lock_file = "//:flake.lock", + package = "libvirt", + build_file_content = LIBVIRT_BUILD, + ) + for system in libvirt_systems + ] + +openssl_systems = [ + "aarch64-linux", + "aarch64-darwin", + "x86_64-linux", + "x86_64-darwin", +] + +cryptsetup_systems = [ + "x86_64-linux", +] + +libvirt_systems = [ + "x86_64-linux", +] + +OPENSSL_BUILD = """\ +load("@rules_cc//cc:defs.bzl", "cc_library") +filegroup( + name = "include", + srcs = glob(["include/**/*.h"]), + visibility = ["//visibility:public"], +) +cc_library( + name = "org_openssl", + srcs = glob(["lib/**/*.a"]), + hdrs = [":include"], + strip_include_prefix = "include", + visibility = ["//visibility:public"], +) +""" + +CRYPTSETUP_BUILD = """\ +exports_files(["closure.tar", "rpath", "dynamic-linker"]) +filegroup( + name = "include", + srcs = glob(["include/**/*.h"]), + visibility = ["//visibility:public"], +) +cc_library( + name = "cryptsetup", + srcs = glob(["lib/**/*.so*"]), + hdrs = [":include"], + strip_include_prefix = "include", + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + visibility = ["//visibility:public"], +) +""" + +LIBVIRT_BUILD = """\ +exports_files(["bin-linktree.tar", "closure.tar", "rpath", "dynamic-linker"]) +load("@rules_cc//cc:defs.bzl", "cc_library") +filegroup( + name = "include", + srcs = glob(["include/**/*.h"]), + visibility = ["//visibility:public"], +) +cc_library( + name = "libvirt", + srcs = glob([ + "lib/*.so", + "lib/*.so.*", + ]), + hdrs = [":include"], + strip_include_prefix = "include", + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + visibility = ["//visibility:public"], +) +"""