Switch workspace build rules from zig_cc_binary to zig_binary, removing the hack and using the C linker directly.

This commit is contained in:
Tarry Singh 2025-07-03 15:10:36 +00:00
parent 78679817df
commit cf00506dbb
19 changed files with 1531 additions and 101 deletions

View File

@ -19,7 +19,7 @@ bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "rules_python", version = "1.5.3")
bazel_dep(name = "rules_rust", version = "0.63.0")
bazel_dep(name = "rules_uv", version = "0.87.0")
bazel_dep(name = "rules_zig", version = "20250714.0-b14a4f1")
bazel_dep(name = "rules_zig", version = "20250821.0-be53625")
bazel_dep(name = "toolchains_llvm_bootstrapped", version = "0.2.4")
bazel_dep(name = "with_cfg.bzl", version = "0.11.0")

View File

@ -1,6 +1,4 @@
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test")
load("@zml//bazel:zig_srcs.bzl", "zig_srcs")
zig_library(
name = "async",
@ -23,11 +21,6 @@ zig_library(
zig_test(
name = "test",
deps = [":async"],
testonly = False,
)
zig_srcs(
name = "sources",
zig_bin = ":test",
deps = [":async"],
)

View File

@ -1,7 +1,7 @@
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")
load("@rules_zig//zig:defs.bzl", "zig_binary", "BINARY_KIND")
load("@rules_zig//zig:defs.bzl", "zig_static_library")
def zig_srcs(name, zig_bin="", zig_lib=""):
def zig_srcs(name, zig_bin = "", zig_lib = ""):
"""For a given zig_library, recursively extract all zig sources into a tarball.
This also includes the files translated from C headers.
@ -10,21 +10,22 @@ def zig_srcs(name, zig_bin="", zig_lib=""):
"""
if zig_bin == "":
zig_bin = "{}_bin".format(name)
zig_binary(
zig_static_library(
name = zig_bin,
kind = BINARY_KIND.bc,
tags = ["manual", "@rules_zig//zig/lib:libc"],
tags = ["manual"],
deps = [zig_lib],
)
native.filegroup(
name = "{}_files".format(name),
srcs = [zig_bin],
tags = ["manual"],
output_group = "srcs",
)
mtree_spec(
name = "{}_mtree".format(name),
srcs = [":{}_files".format(name)],
tags = ["manual"],
)
tar(
name = name,

View File

@ -1,11 +1,9 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_zig//zig:defs.bzl", "zig_library")
load("//bazel:zig.bzl", "zig_cc_test")
load("//bazel:zig_srcs.bzl", "zig_srcs")
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test")
cc_library(
name = "c",
hdrs = ["c.h"],
hdrs = ["mlir.h"],
visibility = ["//mlir:__subpackages__"],
deps = [
"@llvm-project//mlir:CAPIArith",
@ -18,7 +16,6 @@ cc_library(
zig_library(
name = "mlir",
copts = ["-lc"],
main = "mlir.zig",
visibility = ["//visibility:public"],
deps = [
@ -27,12 +24,7 @@ zig_library(
],
)
zig_cc_test(
zig_test(
name = "test",
deps = [":mlir"],
)
zig_srcs(
name = "sources",
zig_bin = ":test_test_lib",
)

View File

@ -1,6 +1,4 @@
load("@rules_zig//zig:defs.bzl", "zig_library")
load("//bazel:zig.bzl", "zig_cc_test")
load("//bazel:zig_srcs.bzl", "zig_srcs")
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test")
zig_library(
name = "dialects",
@ -15,35 +13,14 @@ zig_library(
main = "dialects.zig",
visibility = ["//visibility:public"],
deps = [
":stablehlo",
"//mlir",
"//mlir/dialects/stablehlo",
],
)
zig_cc_test(
zig_test(
name = "test",
deps = [":dialects"],
)
zig_srcs(
name = "sources",
zig_bin = ":test_test_lib",
)
zig_library(
name = "stablehlo",
import_name = "mlir/dialects/stablehlo",
main = "stablehlo.zig",
visibility = ["//mlir/dialects:__subpackages__"],
deps = [
"//mlir",
"//mlir:c",
"//stdx",
"@stablehlo//:stablehlo_dialect_capi",
":dialects",
],
)
zig_cc_test(
name = "stablehlo_test",
deps = [":stablehlo"],
)

View File

@ -0,0 +1,19 @@
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test")
zig_library(
name = "stablehlo",
import_name = "mlir/dialects/stablehlo",
main = "stablehlo.zig",
visibility = ["//mlir/dialects:__subpackages__"],
deps = [
"//mlir",
"//mlir:c",
"//stdx",
"@stablehlo//:stablehlo_dialect_capi",
],
)
zig_test(
name = "test",
deps = [":stablehlo"],
)

File diff suppressed because it is too large Load Diff

9
mlir/mlir.h Normal file
View File

@ -0,0 +1,9 @@
#include <mlir-c/BuiltinAttributes.h>
#include <mlir-c/BuiltinTypes.h>
#include <mlir-c/Dialect/Arith.h>
#include <mlir-c/Dialect/Func.h>
#include <mlir-c/Dialect/Math.h>
#include <mlir-c/Dialect/SCF.h>
#include <mlir-c/IR.h>
#include <mlir-c/Pass.h>
#include <mlir-c/Transforms.h>

View File

@ -1,5 +1,4 @@
load("@rules_zig//zig:defs.bzl", "zig_library")
load("@zml//bazel:zig_srcs.bzl", "zig_srcs")
zig_library(
name = "pjrt",
@ -15,8 +14,3 @@ zig_library(
"@xla//xla/pjrt/c:pjrt_c_api_triton_extension_hdrs",
],
)
zig_srcs(
name = "sources",
zig_lib = ":pjrt",
)

View File

@ -2,7 +2,7 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
load("@rules_uv//uv:pip.bzl", "pip_compile")
load("@rules_zig//zig:defs.bzl", "BINARY_KIND", "zig_binary", "zig_library")
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_shared_library")
load(":neuron.bzl", "py_binary_with_script")
load(":pyenv.bzl", "pyenv_zig")
@ -21,12 +21,9 @@ zig_library(
#
# Additionally, it provides a way to load implicit transitive dependencies
# of neuronx-cc (see add_needed of the patchelf target below).
#
# TODO(cerisier): Use a zig_cc_shared_library instead.
zig_binary(
zig_shared_library(
name = "libpjrt_neuron_proxy",
copts = ["-lc"],
kind = BINARY_KIND.shared_lib,
copts = ["-fno-stack-check"],
main = "libpjrt_neuron.zig",
visibility = ["@libpjrt_neuron//:__subpackages__"],
deps = [

View File

@ -1,5 +1,5 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_zig//zig:defs.bzl", "zig_library")
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")
cc_library(
name = "zmlxrocm_lib",

View File

@ -1,5 +1,4 @@
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test")
load("@zml//bazel:zig_srcs.bzl", "zig_srcs")
zig_library(
name = "stdx",
@ -22,11 +21,6 @@ zig_library(
zig_test(
name = "test",
deps = [":stdx"],
testonly = False,
)
zig_srcs(
name = "sources",
zig_bin = ":test",
deps = [":stdx"],
)

View File

@ -1,5 +1,28 @@
const std = @import("std");
extern fn dladdr(addr: *anyopaque, info: *Dl_info) c_int;
const Dl_info = extern struct {
dli_fname: [*c]const u8,
dli_fbase: *anyopaque,
dli_sname: [*c]const u8,
dli_saddr: *anyopaque,
};
fn selfSharedObjectPathImpl(addr: usize) []const u8 {
var info: Dl_info = undefined;
_ = dladdr(@ptrFromInt(addr), &info);
return std.mem.span(info.dli_fname);
}
pub fn selfSharedObjectPath() []const u8 {
return selfSharedObjectPathImpl(@returnAddress());
}
pub fn selfSharedObjectDirPath() []const u8 {
return std.fs.path.dirname(selfSharedObjectPathImpl(@returnAddress())).?;
}
pub const path = struct {
pub fn bufJoin(buf: []u8, paths: []const []const u8) ![]u8 {
var fa: std.heap.FixedBufferAllocator = .init(buf);

View File

@ -0,0 +1,75 @@
module(
name = "rules_zig",
version = "20250821.0-be53625",
compatibility_level = 1,
)
bazel_dep(name = "aspect_bazel_lib", version = "2.8.1")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
zig = use_extension("//zig:extensions.bzl", "zig")
zig.index(file = "//zig/private:versions.json")
use_repo(zig, "zig_toolchains")
register_toolchains("@rules_zig//zig/target:all")
register_toolchains("@zig_toolchains//:all")
zig_dev = use_extension(
"//zig:extensions.bzl",
"zig",
dev_dependency = True,
)
zig_dev.toolchain(zig_version = "0.13.0")
zig_dev.toolchain(zig_version = "0.12.1")
zig_dev.toolchain(zig_version = "0.12.0")
zig_dev.toolchain(zig_version = "0.11.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "stardoc", version = "0.7.0", dev_dependency = True, repo_name = "io_bazel_stardoc")
bazel_dep(name = "gazelle", version = "0.38.0", dev_dependency = True, repo_name = "bazel_gazelle")
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.7.1", dev_dependency = True)
bazel_dep(
name = "buildifier_prebuilt",
version = "7.3.1",
dev_dependency = True,
)
bazel_dep(name = "rules_multirun", version = "0.9.0", dev_dependency = True)
bazel_dep(name = "rules_python", version = "0.35.0", dev_dependency = True)
bazel_dep(
name = "rules_bazel_integration_test",
version = "0.25.0",
dev_dependency = True,
)
bazel_binaries = use_extension(
"@rules_bazel_integration_test//:extensions.bzl",
"bazel_binaries",
dev_dependency = True,
)
# NOTE: Keep in sync with WORKSPACE.
bazel_binaries.download(version_file = "//:.bazelversion")
bazel_binaries.download(version = "7.0.0")
use_repo(
bazel_binaries,
"bazel_binaries",
"bazel_binaries_bazelisk",
"build_bazel_bazel_.bazelversion",
"build_bazel_bazel_7_0_0",
)
# TODO[AH] Should be an implicit transitive dependency through rules_bazel_integration_test.
# However, if we do not include it explicitly, then the runfiles resolution for
# cgrindel_bazel_starlib/shlib/lib/message.sh fails in
# rules_bazel_integration_test/tools/update_deleted_packages.sh when invoked
# through the rules_multirun target //util:update.
bazel_dep(name = "cgrindel_bazel_starlib", version = "0.21.0", dev_dependency = True)
# Hack to get around a cc_common.link limitation.
# See https://github.com/bazelbuild/bazel/pull/23838
cc_common_link = use_repo_rule("//zig:extensions.bzl", "cc_common_link")
cc_common_link(name = "build_bazel_rules_android")

View File

@ -0,0 +1,5 @@
{
"strip_prefix": "rules_zig-be53625afb13e73856ee4cab38d5aad9f86f63ef",
"url": "https://github.com/zml/rules_zig/archive/be53625afb13e73856ee4cab38d5aad9f86f63ef.tar.gz",
"integrity": "sha256-IsqEl3BHW/vttyHpVBx5WzO3an/K3eZZx8RJk7nVx8s="
}

View File

@ -7,9 +7,7 @@
"name": "ZML Engineering Team"
}
],
"repository": [
"github:zml/rules_zig"
],
"repository": ["github:zml/rules_zig"],
"versions": [
"20240904.0-010da15",
"20240909.0-37f17ff",
@ -18,7 +16,8 @@
"20250314.0-b9739c6",
"20250519.0-233b207",
"20250613.0-567662a",
"20250714.0-b14a4f1"
"20250714.0-b14a4f1",
"20250821.0-be53625"
],
"yanked_versions": {}
}

View File

@ -1,8 +1,6 @@
load("@com_google_protobuf//bazel:upb_proto_library.bzl", "upb_c_proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_zig//zig:defs.bzl", "zig_library")
load("//bazel:zig.bzl", "zig_cc_test")
load("//bazel:zig_srcs.bzl", "zig_srcs")
load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test")
upb_c_proto_library(
name = "xla_data_upb",
@ -52,7 +50,7 @@ zig_library(
# All ZML Tests
zig_cc_test(
zig_test(
name = "test",
data = [
"aio/torch/simple.pt",
@ -67,8 +65,3 @@ filegroup(
srcs = ["test_runner.zig"],
visibility = ["//visibility:public"],
)
zig_srcs(
name = "sources",
zig_bin = ":test_test_lib",
)

View File

@ -1,5 +1,4 @@
load("@rules_zig//zig:defs.bzl", "zig_library")
load("@zml//bazel:zig.bzl", "zig_cc_binary")
load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_library")
zig_library(
name = "tokenizer",
@ -15,7 +14,7 @@ zig_library(
],
)
zig_cc_binary(
zig_binary(
name = "main",
main = "main.zig",
visibility = ["//visibility:public"],

View File

@ -1,6 +1,5 @@
load("@rules_zig//zig:defs.bzl", "zig_library")
load("//bazel:swig.bzl", "swig_cc_library")
load("//bazel:zig_srcs.bzl", "zig_srcs")
swig_cc_library(
name = "sentencepiece_swig",
@ -22,8 +21,3 @@ zig_library(
"//ffi:zig",
],
)
zig_srcs(
name = "sources",
zig_lib = ":sentencepiece",
)