diff --git a/BUILD.bazel b/BUILD.bazel index aa1656d..97b2cf1 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,10 +1,4 @@ -load("@rules_zig//zig:defs.bzl", "zls_completion") -load("@zml//third_party/zls:zls.bzl", "zls_runner") - -zls_runner( - name = "zls", - target = ":completion", -) +load("//third_party/zls:zls_completion.bzl", "zls_completion") zls_completion( name = "completion", diff --git a/MODULE.bazel b/MODULE.bazel index 72ad53a..1b638ce 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,7 +6,7 @@ bazel_dep(name = "abseil-cpp", version = "20250814.0") bazel_dep(name = "aspect_bazel_lib", version = "2.20.0") bazel_dep(name = "aspect_rules_py", version = "1.6.3") bazel_dep(name = "bazel_skylib", version = "1.8.1") -bazel_dep(name = "libxev", version = "20250718.0-9f785d2") +bazel_dep(name = "libxev", version = "20251010.0-9f785d2") bazel_dep(name = "patchelf", version = "0.18.0") bazel_dep(name = "pcre2", version = "10.45") bazel_dep(name = "platforms", version = "1.0.0") @@ -18,7 +18,7 @@ bazel_dep(name = "rules_oci", version = "2.2.6") 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_zig", version = "20250827.0-35b6d57") +bazel_dep(name = "rules_zig", version = "0.12.1") bazel_dep(name = "toolchains_llvm_bootstrapped", version = "0.2.4") bazel_dep(name = "with_cfg.bzl", version = "0.11.0") @@ -92,10 +92,10 @@ use_repo(tpu, "libpjrt_tpu") neuron = use_extension("//runtimes/neuron:neuron.bzl", "neuron_packages") use_repo(neuron, "aws-neuronx-collectives", "aws-neuronx-runtime-lib", "libgomp1", "libpjrt_neuron", "zlib1g") -zls = use_extension("//third_party/zls:zls.bzl", "repo") -use_repo(zls, "zls_aarch64-macos", "zls_x86_64-linux", "zls_x86_64-macos") +zls = use_extension("//third_party/zls:zls_toolchain.bzl", "zls_toolchains") +use_repo(zls, "zls_toolchains") -register_toolchains("//third_party/zls:all") +register_toolchains("@zls_toolchains//:all") non_module_deps = use_extension("//:third_party/non_module_deps.bzl", "non_module_deps") use_repo(non_module_deps, "com_github_hejsil_clap", "com_google_sentencepiece", "mnist", "org_swig_swig", "xla") diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 5fa256d..e5f0dd8 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -18,7 +18,9 @@ "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.0.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.14.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.16.0/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.19.3/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.20.0/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.21.2/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.7.2/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.8.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/aspect_bazel_lib/2.9.4/MODULE.bazel": "not found", @@ -53,8 +55,10 @@ "https://bazel-registry.zml.ai/modules/bazel_skylib/1.7.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/bazel_skylib/1.7.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/bazel_skylib/1.8.1/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/bazel_skylib/1.8.2/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/buildifier_prebuilt/8.2.0.2/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/buildozer/7.1.2/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/gawk/5.3.2.bcr.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/google_benchmark/1.8.2/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/googletest/1.11.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "not found", @@ -107,6 +111,7 @@ "https://bazel-registry.zml.ai/modules/rules_cc/0.0.9/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_cc/0.1.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_cc/0.2.0/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/rules_cc/0.2.9/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_distroless/0.5.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_fuzzing/0.5.2/MODULE.bazel": "not found", @@ -165,6 +170,7 @@ "https://bazel-registry.zml.ai/modules/rules_shell/0.4.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_swift/1.16.0/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/rules_swift/2.1.1/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/rules_zig/0.12.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/stardoc/0.5.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/stardoc/0.5.3/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/stardoc/0.5.4/MODULE.bazel": "not found", @@ -175,6 +181,7 @@ "https://bazel-registry.zml.ai/modules/stardoc/0.7.2/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/swift_argument_parser/1.3.1.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/tar.bzl/0.2.1/MODULE.bazel": "not found", + "https://bazel-registry.zml.ai/modules/tar.bzl/0.5.1/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/toolchains_llvm_bootstrapped/0.2.4/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "not found", "https://bazel-registry.zml.ai/modules/with_cfg.bzl/0.11.0/MODULE.bazel": "not found", @@ -203,8 +210,10 @@ "https://bcr.bazel.build/modules/aspect_bazel_lib/2.0.0/MODULE.bazel": "e118477db5c49419a88d78ebc7a2c2cea9d49600fe0f490c1903324a2c16ecd9", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.14.0/MODULE.bazel": "2b31ffcc9bdc8295b2167e07a757dbbc9ac8906e7028e5170a3708cecaac119f", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.16.0/MODULE.bazel": "852f9ebbda017572a7c113a2434592dd3b2f55cd9a0faea3d4be5a09a59e4900", + "https://bcr.bazel.build/modules/aspect_bazel_lib/2.19.3/MODULE.bazel": "253d739ba126f62a5767d832765b12b59e9f8d2bc88cc1572f4a73e46eb298ca", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.20.0/MODULE.bazel": "c5565bac49e1973227225b441fad1c938d498d83df62dc5da95b2fab0f0626a2", - "https://bcr.bazel.build/modules/aspect_bazel_lib/2.20.0/source.json": "3eaada79dd3c65b6c57d5fc33c57ffd2896c4ebd78c4c9001a790a70f7f50e61", + "https://bcr.bazel.build/modules/aspect_bazel_lib/2.21.2/MODULE.bazel": "276347663a25b0d5bd6cad869252bea3e160c4d980e764b15f3bae7f80b30624", + "https://bcr.bazel.build/modules/aspect_bazel_lib/2.21.2/source.json": "f42051fa42629f0e59b7ac2adf0a55749144b11f1efcd8c697f0ee247181e526", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.7.2/MODULE.bazel": "780d1a6522b28f5edb7ea09630748720721dfe27690d65a2d33aa7509de77e07", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.8.1/MODULE.bazel": "812d2dd42f65dca362152101fbec418029cc8fd34cbad1a2fde905383d705838", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.9.4/MODULE.bazel": "ccc41028429f894b02fde7ef67d416cba3ba5084ed9ddb9bb6107aa82d118776", @@ -242,11 +251,14 @@ "https://bcr.bazel.build/modules/bazel_skylib/1.7.0/MODULE.bazel": "0db596f4563de7938de764cc8deeabec291f55e8ec15299718b93c4423e9796d", "https://bcr.bazel.build/modules/bazel_skylib/1.7.1/MODULE.bazel": "3120d80c5861aa616222ec015332e5f8d3171e062e3e804a2a0253e1be26e59b", "https://bcr.bazel.build/modules/bazel_skylib/1.8.1/MODULE.bazel": "88ade7293becda963e0e3ea33e7d54d3425127e0a326e0d17da085a5f1f03ff6", - "https://bcr.bazel.build/modules/bazel_skylib/1.8.1/source.json": "7ebaefba0b03efe59cac88ed5bbc67bcf59a3eff33af937345ede2a38b2d368a", + "https://bcr.bazel.build/modules/bazel_skylib/1.8.2/MODULE.bazel": "69ad6927098316848b34a9142bcc975e018ba27f08c4ff403f50c1b6e646ca67", + "https://bcr.bazel.build/modules/bazel_skylib/1.8.2/source.json": "34a3c8bcf233b835eb74be9d628899bb32999d3e0eadef1947a0a562a2b16ffb", "https://bcr.bazel.build/modules/buildifier_prebuilt/8.2.0.2/MODULE.bazel": "a9b689711d5b69f9db741649b218c119b9fdf82924ba390415037e09798edd03", "https://bcr.bazel.build/modules/buildifier_prebuilt/8.2.0.2/source.json": "51eb0a4b38aaaeab7fa64361576d616c4d8bfd0f17a0a10184aeab7084d79f8e", "https://bcr.bazel.build/modules/buildozer/7.1.2/MODULE.bazel": "2e8dd40ede9c454042645fd8d8d0cd1527966aa5c919de86661e62953cd73d84", "https://bcr.bazel.build/modules/buildozer/7.1.2/source.json": "c9028a501d2db85793a6996205c8de120944f50a0d570438fcae0457a5f9d1f8", + "https://bcr.bazel.build/modules/gawk/5.3.2.bcr.1/MODULE.bazel": "cdf8cbe5ee750db04b78878c9633cc76e80dcf4416cbe982ac3a9222f80713c8", + "https://bcr.bazel.build/modules/gawk/5.3.2.bcr.1/source.json": "fa7b512dfcb5eafd90ce3959cf42a2a6fe96144ebbb4b3b3928054895f2afac2", "https://bcr.bazel.build/modules/google_benchmark/1.8.2/MODULE.bazel": "a70cf1bba851000ba93b58ae2f6d76490a9feb74192e57ab8e8ff13c34ec50cb", "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "22c31a561553727960057361aa33bf20fb2e98584bc4fec007906e27053f80c6", @@ -312,7 +324,8 @@ "https://bcr.bazel.build/modules/rules_cc/0.0.9/MODULE.bazel": "836e76439f354b89afe6a911a7adf59a6b2518fafb174483ad78a2a2fde7b1c5", "https://bcr.bazel.build/modules/rules_cc/0.1.1/MODULE.bazel": "2f0222a6f229f0bf44cd711dc13c858dad98c62d52bd51d8fc3a764a83125513", "https://bcr.bazel.build/modules/rules_cc/0.2.0/MODULE.bazel": "b5c17f90458caae90d2ccd114c81970062946f49f355610ed89bebf954f5783c", - "https://bcr.bazel.build/modules/rules_cc/0.2.0/source.json": "5f7f4e578e950adbf194217d4b607237a8197fc53ba46c367b3d61a86ecf35c2", + "https://bcr.bazel.build/modules/rules_cc/0.2.9/MODULE.bazel": "34263f1dca62ea664265438cef714d7db124c03e1ed55ebb4f1dc860164308d1", + "https://bcr.bazel.build/modules/rules_cc/0.2.9/source.json": "4e49b40effcbd14fbfb233eb929de42dfff7b66538b4ffda310ad501638e7986", "https://bcr.bazel.build/modules/rules_distroless/0.5.1/MODULE.bazel": "2a63f4744d30749128105da5f96adf7caf5628e37548293f89e7fa39c3b3f2c2", "https://bcr.bazel.build/modules/rules_distroless/0.5.1/source.json": "c6b9ff7f325bfed89c3671757f14c1d1bc6077d0fcef809b8aa2d007cac7dd1d", "https://bcr.bazel.build/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "c9e8c682bf75b0e7c704166d79b599f93b72cfca5ad7477df596947891feeef6", @@ -383,6 +396,8 @@ "https://bcr.bazel.build/modules/rules_swift/1.16.0/MODULE.bazel": "4a09f199545a60d09895e8281362b1ff3bb08bbde69c6fc87aff5b92fcc916ca", "https://bcr.bazel.build/modules/rules_swift/2.1.1/MODULE.bazel": "494900a80f944fc7aa61500c2073d9729dff0b764f0e89b824eb746959bc1046", "https://bcr.bazel.build/modules/rules_swift/2.1.1/source.json": "40fc69dfaac64deddbb75bd99cdac55f4427d9ca0afbe408576a65428427a186", + "https://bcr.bazel.build/modules/rules_zig/0.12.1/MODULE.bazel": "5953094b681c212eebe3dd4275809c52b9177aeaa058d8f1bff433822c0dcd01", + "https://bcr.bazel.build/modules/rules_zig/0.12.1/source.json": "d880cdc3473bfcf172c9aad9292e856430496e198d805539bb8be98e79f58a7c", "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", "https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c", "https://bcr.bazel.build/modules/stardoc/0.5.4/MODULE.bazel": "6569966df04610b8520957cb8e97cf2e9faac2c0309657c537ab51c16c18a2a4", @@ -395,7 +410,8 @@ "https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.1/MODULE.bazel": "5e463fbfba7b1701d957555ed45097d7f984211330106ccd1352c6e0af0dcf91", "https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.1/source.json": "32bd87e5f4d7acc57c5b2ff7c325ae3061d5e242c0c4c214ae87e0f1c13e54cb", "https://bcr.bazel.build/modules/tar.bzl/0.2.1/MODULE.bazel": "52d1c00a80a8cc67acbd01649e83d8dd6a9dc426a6c0b754a04fe8c219c76468", - "https://bcr.bazel.build/modules/tar.bzl/0.2.1/source.json": "600ac6ff61744667a439e7b814ae59c1f29632c3984fccf8000c64c9db8d7bb6", + "https://bcr.bazel.build/modules/tar.bzl/0.5.1/MODULE.bazel": "7c2eb3dcfc53b0f3d6f9acdfd911ca803eaf92aadf54f8ca6e4c1f3aee288351", + "https://bcr.bazel.build/modules/tar.bzl/0.5.1/source.json": "deed3094f7cc779ed1d37a68403847b0e38d9dd9d931e03cb90825f3368b515f", "https://bcr.bazel.build/modules/toolchains_llvm_bootstrapped/0.2.4/MODULE.bazel": "859f02226bc313a622f56b816582fb55d5aaced4a1ecde7397dd4a9ebd24321e", "https://bcr.bazel.build/modules/toolchains_llvm_bootstrapped/0.2.4/source.json": "43c5fd6b585cbf0b642157b6843a25425d541fabe638acb7bc908846c4bda8ca", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", @@ -413,7 +429,7 @@ "moduleExtensions": { "@@apple_support+//crosstool:setup.bzl%apple_cc_configure_extension": { "general": { - "bzlTransitiveDigest": "JJV/C5bhYKZip41t/GGZvqIMmmrcwr0Ydybkn97uXxQ=", + "bzlTransitiveDigest": "N6yHPMaF17im7GO6Gu+cdCtGXs+lAlBIhCeB1/ZZoPk=", "usagesDigest": "39X2JjPCOAk6sThDALGv1L4q85GNjda2yfszm/phxxw=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -444,7 +460,7 @@ }, "@@aspect_rules_py+//py:extensions.bzl%py_tools": { "general": { - "bzlTransitiveDigest": "geB/ya+5nbONwFUgzta9iVBeMKtviiSBwlZnE7Avcgs=", + "bzlTransitiveDigest": "Udhh0icjIe0dHLjbgfmj2tzBz0geB4SXJl0FoPcpF84=", "usagesDigest": "tVlELaqQFX7HPl1uFzRY9m7nVV7tY9roq1qfK75ApwE=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -626,7 +642,7 @@ }, "@@rules_apple+//apple:apple.bzl%provisioning_profile_repository_extension": { "general": { - "bzlTransitiveDigest": "BRNIrN/yKvsUnyouziBgSAp7x+TIcFZhks4bYDTh9iA=", + "bzlTransitiveDigest": "7XgPyOlTI0qJefux0gxRds30ZZCLPjr/7R9gFtU/jgE=", "usagesDigest": "vsJl8Rw5NL+5Ag2wdUDoTeRF/5klkXO8545Iy7U1Q08=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -800,7 +816,7 @@ }, "@@rules_oci+//oci:extensions.bzl%oci": { "general": { - "bzlTransitiveDigest": "4LJBCkExDO/zET3DzuMS4pDRRr4V0gc6YgFMhM6wIjI=", + "bzlTransitiveDigest": "PTQgYMhZAtC3X0ttcTBbDaaOBjIGyz0G4vX/ThEUHDY=", "usagesDigest": "vK9qUa3pyGJik2hb1NI2fKVJrjIszor+wN/44XMkKi0=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -1161,7 +1177,7 @@ }, "@@rules_rust+//crate_universe:extensions.bzl%crate": { "general": { - "bzlTransitiveDigest": "HHPRUh/mbCXdC11w2axMRLWeweoYK1bLuS1e2NpvcoM=", + "bzlTransitiveDigest": "uv5JLD8zajzRT1Hh+FYGrK5o1QjgTE4Ohde5NQMsUlk=", "usagesDigest": "mup6mgSFatk34Q0znMZPpIEOFZyk9v3xf6jRIXEFTxc=", "recordedFileInputs": { "@@//zml/tokenizer/hftokenizers/Cargo.lock": "d78ffab37ef69bf8ff45a4d686b25175f30e8fed4d8375612e84dcf6db0cfc9c", @@ -2492,7 +2508,7 @@ }, "@@rules_rust+//crate_universe/private:internal_extensions.bzl%cu_nr": { "general": { - "bzlTransitiveDigest": "A72J7FdqiIlmYkdw1rlcP/8uguFzwKWCs3Q3Mn4fVpQ=", + "bzlTransitiveDigest": "BEf6td+6l3OplJk+TxURVgZT9CK8AKbw6H/uSseljfQ=", "usagesDigest": "3vKI8uvqTpJCf+t8aU6UD5d5cUWinWhtMjKkRpCLR+A=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -2791,6 +2807,215 @@ ] ] } + }, + "@@rules_zig+//zig:extensions.bzl%zig": { + "general": { + "bzlTransitiveDigest": "wV3euhBZSI0D2ZlqbY1YW592mghFY88d9H6zBMQRaJY=", + "usagesDigest": "T0xLzuDAWCJjFcphcIcT31t1HM/Yg7RxtfOUb+XmxTw=", + "recordedFileInputs": { + "@@//bazel/zig_index.json": "42ea73420b15741770d5d8d0ded1311a2dfda1f963f0802dea3418f6e060ffef", + "@@rules_zig+//zig/private/versions.json": "6eb85ebaee72c4e6fbc82d8ad73f69e0899d839b70a7e19caa792ab6a5fa0c36" + }, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "zig_0.15.1_aarch64-linux": { + "repoRuleId": "@@rules_zig+//zig/private/repo:zig_repository.bzl%zig_repository", + "attributes": { + "url": "https://ziglang.org/download/0.15.1/zig-aarch64-linux-0.15.1.tar.xz", + "mirrors": [ + "https://mirror.zml.ai/zig", + "https://ziglang.org/builds/" + ], + "sha256": "bb4a8d2ad735e7fba764c497ddf4243cb129fece4148da3222a7046d3f1f19fe", + "zig_version": "0.15.1", + "platform": "aarch64-linux" + } + }, + "zig_0.15.1_aarch64-macos": { + "repoRuleId": "@@rules_zig+//zig/private/repo:zig_repository.bzl%zig_repository", + "attributes": { + "url": "https://ziglang.org/download/0.15.1/zig-aarch64-macos-0.15.1.tar.xz", + "mirrors": [ + "https://mirror.zml.ai/zig", + "https://ziglang.org/builds/" + ], + "sha256": "c4bd624d901c1268f2deb9d8eb2d86a2f8b97bafa3f118025344242da2c54d7b", + "zig_version": "0.15.1", + "platform": "aarch64-macos" + } + }, + "zig_0.15.1_aarch64-windows": { + "repoRuleId": "@@rules_zig+//zig/private/repo:zig_repository.bzl%zig_repository", + "attributes": { + "url": "https://ziglang.org/download/0.15.1/zig-aarch64-windows-0.15.1.zip", + "mirrors": [ + "https://mirror.zml.ai/zig", + "https://ziglang.org/builds/" + ], + "sha256": "1f1bf16228b0ffcc882b713dc5e11a6db4219cb30997e13c72e8e723c2104ec6", + "zig_version": "0.15.1", + "platform": "aarch64-windows" + } + }, + "zig_0.15.1_x86_64-linux": { + "repoRuleId": "@@rules_zig+//zig/private/repo:zig_repository.bzl%zig_repository", + "attributes": { + "url": "https://ziglang.org/download/0.15.1/zig-x86_64-linux-0.15.1.tar.xz", + "mirrors": [ + "https://mirror.zml.ai/zig", + "https://ziglang.org/builds/" + ], + "sha256": "c61c5da6edeea14ca51ecd5e4520c6f4189ef5250383db33d01848293bfafe05", + "zig_version": "0.15.1", + "platform": "x86_64-linux" + } + }, + "zig_0.15.1_x86_64-macos": { + "repoRuleId": "@@rules_zig+//zig/private/repo:zig_repository.bzl%zig_repository", + "attributes": { + "url": "https://ziglang.org/download/0.15.1/zig-x86_64-macos-0.15.1.tar.xz", + "mirrors": [ + "https://mirror.zml.ai/zig", + "https://ziglang.org/builds/" + ], + "sha256": "9919392e0287cccc106dfbcbb46c7c1c3fa05d919567bb58d7eb16bca4116184", + "zig_version": "0.15.1", + "platform": "x86_64-macos" + } + }, + "zig_0.15.1_x86_64-windows": { + "repoRuleId": "@@rules_zig+//zig/private/repo:zig_repository.bzl%zig_repository", + "attributes": { + "url": "https://ziglang.org/download/0.15.1/zig-x86_64-windows-0.15.1.zip", + "mirrors": [ + "https://mirror.zml.ai/zig", + "https://ziglang.org/builds/" + ], + "sha256": "91e69e887ca8c943ce9a515df3af013d95a66a190a3df3f89221277ebad29e34", + "zig_version": "0.15.1", + "platform": "x86_64-windows" + } + }, + "zig_toolchains": { + "repoRuleId": "@@rules_zig+//zig/private/repo:toolchains_repo.bzl%toolchains_repo", + "attributes": { + "names": [ + "zig_0.15.1_aarch64-linux", + "zig_0.15.1_aarch64-macos", + "zig_0.15.1_aarch64-windows", + "zig_0.15.1_x86_64-linux", + "zig_0.15.1_x86_64-macos", + "zig_0.15.1_x86_64-windows" + ], + "labels": [ + "@zig_0.15.1_aarch64-linux//:zig_toolchain", + "@zig_0.15.1_aarch64-macos//:zig_toolchain", + "@zig_0.15.1_aarch64-windows//:zig_toolchain", + "@zig_0.15.1_x86_64-linux//:zig_toolchain", + "@zig_0.15.1_x86_64-macos//:zig_toolchain", + "@zig_0.15.1_x86_64-windows//:zig_toolchain" + ], + "zig_versions": [ + "0.15.1", + "0.15.1", + "0.15.1", + "0.15.1", + "0.15.1", + "0.15.1" + ], + "exec_lengths": [ + 2, + 2, + 2, + 2, + 2, + 2 + ], + "exec_constraints": [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + "@platforms//os:macos", + "@platforms//cpu:aarch64", + "@platforms//os:windows", + "@platforms//cpu:aarch64", + "@platforms//os:linux", + "@platforms//cpu:x86_64", + "@platforms//os:macos", + "@platforms//cpu:x86_64", + "@platforms//os:windows", + "@platforms//cpu:x86_64" + ] + } + } + }, + "recordedRepoMappingEntries": [ + [ + "rules_zig+", + "bazel_skylib", + "bazel_skylib+" + ], + [ + "rules_zig+", + "bazel_tools", + "bazel_tools" + ] + ] + } + }, + "@@tar.bzl+//tar:extensions.bzl%toolchains": { + "general": { + "bzlTransitiveDigest": "ARFnga1qqjy0bT/hVunmIZnM4oZCkD8F253Z8POuLXs=", + "usagesDigest": "maF8qsAIqeH1ey8pxP0gNZbvJt34kLZvTFeQ0ntrJVA=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "bsd_tar_toolchains": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:toolchain.bzl%tar_toolchains_repo", + "attributes": { + "user_repository_name": "bsd_tar_toolchains" + } + }, + "bsd_tar_toolchains_darwin_amd64": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:platforms.bzl%bsdtar_binary_repo", + "attributes": { + "platform": "darwin_amd64" + } + }, + "bsd_tar_toolchains_darwin_arm64": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:platforms.bzl%bsdtar_binary_repo", + "attributes": { + "platform": "darwin_arm64" + } + }, + "bsd_tar_toolchains_linux_amd64": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:platforms.bzl%bsdtar_binary_repo", + "attributes": { + "platform": "linux_amd64" + } + }, + "bsd_tar_toolchains_linux_arm64": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:platforms.bzl%bsdtar_binary_repo", + "attributes": { + "platform": "linux_arm64" + } + }, + "bsd_tar_toolchains_windows_amd64": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:platforms.bzl%bsdtar_binary_repo", + "attributes": { + "platform": "windows_amd64" + } + }, + "bsd_tar_toolchains_windows_arm64": { + "repoRuleId": "@@tar.bzl+//tar/toolchain:platforms.bzl%bsdtar_binary_repo", + "attributes": { + "platform": "windows_arm64" + } + } + }, + "recordedRepoMappingEntries": [] + } } } } diff --git a/async/BUILD.bazel b/async/BUILD.bazel index 17cdcf9..82fc096 100644 --- a/async/BUILD.bazel +++ b/async/BUILD.bazel @@ -10,7 +10,7 @@ zig_library( "executor.zig", "stack.zig", ], - copts = ["-lc"], + zigopts = ["-lc"], extra_srcs = glob(["asm/*.s"]), main = "async.zig", visibility = ["//visibility:public"], diff --git a/runtimes/cuda/BUILD.bazel b/runtimes/cuda/BUILD.bazel index 83109cf..19eecbc 100644 --- a/runtimes/cuda/BUILD.bazel +++ b/runtimes/cuda/BUILD.bazel @@ -5,7 +5,7 @@ zig_shared_library( name = "zmlxcuda", # Use Clang's compiler-rt, but disable stack checking # to avoid requiring on the _zig_probe_stack symbol. - copts = ["-fno-stack-check", "-fllvm"], + zigopts = ["-fno-stack-check", "-fllvm"], main = "zmlxcuda.zig", shared_lib_name = "libzmlxcuda.so.0", visibility = ["@libpjrt_cuda//:__subpackages__"], @@ -14,6 +14,7 @@ zig_shared_library( cc_library( name = "empty", + defines = ["ZML_RUNTIME_CUDA_DISABLED"], ) cc_library( diff --git a/runtimes/neuron/BUILD.bazel b/runtimes/neuron/BUILD.bazel index 184925a..f1553ff 100644 --- a/runtimes/neuron/BUILD.bazel +++ b/runtimes/neuron/BUILD.bazel @@ -11,7 +11,7 @@ load("@zml//bazel:runfiles.bzl", "runfiles_to_default") # of neuronx-cc (see add_needed of the patchelf target below). zig_shared_library( name = "libpjrt_neuron", - copts = ["-fno-stack-check"], + zigopts = ["-fno-stack-check"], main = "libpjrt_neuron.zig", visibility = ["@libpjrt_neuron//:__subpackages__"], deps = [ @@ -48,6 +48,7 @@ cc_library( cc_library( name = "empty", + defines = ["ZML_RUNTIME_NEURON_DISABLED"], ) cc_library( @@ -79,7 +80,7 @@ upb_c_proto_library( zig_shared_library( name = "libneuronxla", - copts = [ + zigopts = [ "-fno-stack-check", "-fPIC", ], diff --git a/runtimes/rocm/BUILD.bazel b/runtimes/rocm/BUILD.bazel index 7355dc9..7f9be78 100644 --- a/runtimes/rocm/BUILD.bazel +++ b/runtimes/rocm/BUILD.bazel @@ -5,7 +5,7 @@ zig_shared_library( name = "zmlxrocm", # Use Clang's compiler-rt, but disable stack checking # to avoid requiring on the _zig_probe_stack symbol. - copts = ["-fno-stack-check"], + zigopts = ["-fno-stack-check"], main = "zmlxrocm.zig", shared_lib_name = "libzmlxrocm.so.0", visibility = ["@libpjrt_rocm//:__subpackages__"], @@ -24,6 +24,7 @@ alias( cc_library( name = "empty", + defines = ["ZML_RUNTIME_ROCM_DISABLED"], ) cc_library( diff --git a/runtimes/tpu/BUILD.bazel b/runtimes/tpu/BUILD.bazel index 8939fef..fe513d3 100644 --- a/runtimes/tpu/BUILD.bazel +++ b/runtimes/tpu/BUILD.bazel @@ -3,6 +3,7 @@ load("@rules_zig//zig:defs.bzl", "zig_library") cc_library( name = "empty", + defines = ["ZML_RUNTIME_TPU_DISABLED"], ) cc_library( diff --git a/stdx/BUILD.bazel b/stdx/BUILD.bazel index 2594731..23c270e 100644 --- a/stdx/BUILD.bazel +++ b/stdx/BUILD.bazel @@ -15,7 +15,7 @@ zig_library( "signature.zig", "time.zig", ], - copts = ["-lc"], + zigopts = ["-lc"], main = "stdx.zig", visibility = ["//visibility:public"], ) diff --git a/third_party/modules/libxev/20251010.0-9f785d2/MODULE.bazel b/third_party/modules/libxev/20251010.0-9f785d2/MODULE.bazel new file mode 100644 index 0000000..66f5a55 --- /dev/null +++ b/third_party/modules/libxev/20251010.0-9f785d2/MODULE.bazel @@ -0,0 +1,7 @@ +module( + name = "libxev", + version = "20251010.0-9f785d2", + compatibility_level = 1, +) + +bazel_dep(name = "rules_zig", version = "0.12.1") diff --git a/third_party/modules/libxev/20251010.0-9f785d2/overlay/BUILD.bazel b/third_party/modules/libxev/20251010.0-9f785d2/overlay/BUILD.bazel new file mode 100644 index 0000000..872ef7f --- /dev/null +++ b/third_party/modules/libxev/20251010.0-9f785d2/overlay/BUILD.bazel @@ -0,0 +1,13 @@ +load("@rules_zig//zig:defs.bzl", "zig_library") + +zig_library( + name = "xev", + srcs = glob([ + "src/*.zig", + "src/backend/*.zig", + "src/linux/*.zig", + "src/watcher/*.zig", + ]), + main = "src/main.zig", + visibility = ["//visibility:public"], +) diff --git a/third_party/modules/libxev/20251010.0-9f785d2/overlay/MODULE.bazel b/third_party/modules/libxev/20251010.0-9f785d2/overlay/MODULE.bazel new file mode 100644 index 0000000..66f5a55 --- /dev/null +++ b/third_party/modules/libxev/20251010.0-9f785d2/overlay/MODULE.bazel @@ -0,0 +1,7 @@ +module( + name = "libxev", + version = "20251010.0-9f785d2", + compatibility_level = 1, +) + +bazel_dep(name = "rules_zig", version = "0.12.1") diff --git a/third_party/modules/libxev/20251010.0-9f785d2/source.json b/third_party/modules/libxev/20251010.0-9f785d2/source.json new file mode 100644 index 0000000..daf7444 --- /dev/null +++ b/third_party/modules/libxev/20251010.0-9f785d2/source.json @@ -0,0 +1,11 @@ +{ + "strip_prefix": "libxev-9f785d202ddccf7a625e799250579253977978b6", + "url": "https://github.com/mitchellh/libxev/archive/9f785d202ddccf7a625e799250579253977978b6.tar.gz", + "integrity": "sha256-HaExxsAsATjH/pCP9NMo/EjVrG2KVX3SiTtZzmS8R90=", + "overlay": { + "MODULE.bazel": "", + "BUILD.bazel": "" + }, + "patch_strip": 1 +} + diff --git a/third_party/modules/libxev/metadata.json b/third_party/modules/libxev/metadata.json index 6d9687b..1fd34d1 100644 --- a/third_party/modules/libxev/metadata.json +++ b/third_party/modules/libxev/metadata.json @@ -21,6 +21,7 @@ "20250222.0-07bcffa", "20250313.0-5773f46", "20250716.0-9f785d2", + "20251010.0-9f785d2" ], "yanked_versions": {} } diff --git a/third_party/zls/BUILD.bazel b/third_party/zls/BUILD.bazel index c00511f..97064a4 100644 --- a/third_party/zls/BUILD.bazel +++ b/third_party/zls/BUILD.bazel @@ -1,5 +1,74 @@ -load(":zls.bzl", "targets") +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("@rules_zig//zig:defs.bzl", "zig_binary") +load(":resolved_toolchain.bzl", "resolved_toolchain") -toolchain_type(name = "toolchain_type") +# This is the ZLS binary toolchain type. Zig rule authors should use this +# target in the `toolchains` parameter in order to access the appropriate ZLS +# binary for the current execution platform. +toolchain_type( + name = "toolchain_type", + visibility = ["//visibility:public"], +) -targets() +resolved_toolchain( + name = "resolved_toolchain", + # Marked manual so that `bazel test //...` passes + # even if no toolchain is registered. + tags = ["manual"], + visibility = ["//visibility:public"], +) + +exports_files( + [ + "cat.zig", + "zls_build_runner.zig", + "zls_runner.zig", + "zls.completion.tpl", + ], +) + +zig_binary( + name = "runner", + main = "runner.zig", + data = [ + "//zig:resolved_toolchain", + ], + deps = ["@rules_zig//zig/runfiles"], +) + +bzl_library( + name = "zls_completion", + srcs = ["zls_completion.bzl"], + visibility = ["//visibility:public"], + deps = [ + "//zig/private/common:zig_build", + "//zig/private/providers:zig_module_info", + ], +) + +bzl_library( + name = "zls_toolchain", + srcs = ["zls_toolchain.bzl"], + visibility = ["//visibility:public"], + deps = ["@bazel_tools//tools/build_defs/repo:http.bzl"], +) + +bzl_library( + name = "zls_runner", + srcs = ["zls_runner.bzl"], + visibility = ["//visibility:public"], +) + +# Execute `bazel run //util:update_filegroups` to update this target. +filegroup( + name = "all_files", + srcs = [ + ":BUILD.bazel", + ":zls_build_runner.zig", + ":zls.completion.tpl", + ":zls_completion.bzl", + ":zls_runner.zig", + ":zls_toolchain.bzl", + ], + visibility = ["//zig:__pkg__"], +) diff --git a/third_party/zls/cat.zig b/third_party/zls/cat.zig new file mode 100644 index 0000000..ebca6f5 --- /dev/null +++ b/third_party/zls/cat.zig @@ -0,0 +1,31 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +pub fn main() !void { + const gpa = std.heap.page_allocator; + + const args = try std.process.argsAlloc(gpa); + defer std.process.argsFree(gpa, args); + + const file_path = args[1]; + var file = try std.fs.cwd().openFile(file_path, .{ .mode = .read_only}); + defer file.close(); + + if (builtin.zig_version.major == 0 and builtin.zig_version.minor >= 15) { + var write_buffer: [512]u8 = undefined; + var writer = std.fs.File.stdout().writer(&write_buffer); + var stdout = &writer.interface; + + var reader = file.reader(&.{}); + _ = try reader.interface.streamRemaining(stdout); + try stdout.flush(); + } else { + var stdout = std.io.getStdOut().writer(); + var buffer: [4096]u8 = undefined; + while (true) { + const n = try file.read(&buffer); + if (n == 0) break; // EOF + try stdout.writeAll(buffer[0..n]); + } + } +} diff --git a/third_party/zls/resolved_toolchain.bzl b/third_party/zls/resolved_toolchain.bzl new file mode 100644 index 0000000..c5e38e6 --- /dev/null +++ b/third_party/zls/resolved_toolchain.bzl @@ -0,0 +1,24 @@ +"""This module implements an alias rule to the resolved toolchain.""" + +DOC = """\ +Exposes a concrete toolchain which is the result of Bazel resolving the +toolchain for the execution or target platform. +Workaround for https://github.com/bazelbuild/bazel/issues/14009 +""" + +# Forward all the providers +def _resolved_toolchain_impl(ctx): + toolchain_info = ctx.toolchains["//third_party/zls:toolchain_type"] + return [ + toolchain_info, + toolchain_info.default, + toolchain_info.zlstoolchaininfo, + ] + +# Copied from java_toolchain_alias +# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl +resolved_toolchain = rule( + implementation = _resolved_toolchain_impl, + toolchains = ["//third_party/zls:toolchain_type"], + doc = DOC, +) diff --git a/third_party/zls/zls.completion.tpl b/third_party/zls/zls.completion.tpl new file mode 100644 index 0000000..154b4c1 --- /dev/null +++ b/third_party/zls/zls.completion.tpl @@ -0,0 +1,10 @@ +{ + "deps_build_roots": [], + "packages": [ + @@PACKAGES@@ + ], + "include_dirs": [], + "top_level_steps": [], + "available_options": {}, + "c_macros": [] +} diff --git a/third_party/zls/zls_build_runner.zig b/third_party/zls/zls_build_runner.zig new file mode 100644 index 0000000..9ec12c9 --- /dev/null +++ b/third_party/zls/zls_build_runner.zig @@ -0,0 +1,50 @@ +const builtin = @import("builtin"); +const std = @import("std"); + +pub fn main() !void { + var gpa: std.heap.GeneralPurposeAllocator(.{}) = .{}; + defer _ = gpa.deinit(); + var arena_ = std.heap.ArenaAllocator.init(gpa.allocator()); + defer arena_.deinit(); + const arena = arena_.allocator(); + + const build_workspace_directory = try std.process.getEnvVarOwned(arena, "BUILD_WORKSPACE_DIRECTORY"); + var child = std.process.Child.init(&.{ + "bazel", + "run", + "@@__TARGET__@@", + }, arena); + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Inherit; + child.cwd = build_workspace_directory; + + try child.spawn(); + + var out = child.stdout.?; + + const file = try out.readToEndAlloc(arena, 16 * 1024 * 1024); + + const need = std.mem.replacementSize( + u8, + file, + "@@__BUILD_WORKSPACE_DIRECTORY__@@", + build_workspace_directory, + ); + + const replaced = try arena.alloc(u8, need); + + _ = std.mem.replace( + u8, + file, + "@@__BUILD_WORKSPACE_DIRECTORY__@@", + build_workspace_directory, + replaced, + ); + + if (comptime builtin.zig_version.order(.{ .major = 0, .minor = 15, .patch = 0 }).compare(.gte)) { + _ = try std.fs.File.stdout().writeAll(replaced); + } else { + _ = try std.io.getStdOut().writeAll(replaced); + } +} diff --git a/third_party/zls/zls_completion.bzl b/third_party/zls/zls_completion.bzl new file mode 100644 index 0000000..02f11f3 --- /dev/null +++ b/third_party/zls/zls_completion.bzl @@ -0,0 +1,69 @@ +"""Implementation of the zls_completion macro.""" +load("@aspect_bazel_lib//lib:utils.bzl", "utils") +load("@bazel_skylib//rules:expand_template.bzl", "expand_template") +load("@rules_zig//zig:defs.bzl", "zig_binary") +load(":zls_write_build_config.bzl", "zls_write_build_config") +load(":zls_write_runner_zig_src.bzl", "zls_write_runner_zig_src") + +def zls_completion(name, deps, **kwargs): + """Entry point for ZLS completion. + + Args: + name: The name of the completion target. + deps: The List of Zig modules to include for completion. + **kwargs: Additional keyword arguments passed to the `zig_binary` + """ + # Generate the ZLS BuildConfig file. + # It contains the list of Zig packages alongside their main Zig file paths. + build_config = name + ".build_config" + zls_write_build_config( + name = build_config, + out = name + ".build_config.json", + deps = deps, + ) + + # Create a target that will be invoked by ZLS using our customer build_runner. + build_config_printer = "print_{}_build_config".format(name) + zig_binary( + name = build_config_printer, + main = Label("//third_party/zls:cat.zig"), + data = [ + ":{}".format(build_config), + ], + args = [ + "$(location :{})".format(build_config), + ], + visibility = ["//visibility:private"], + ) + + # Generate the Zig build runner that will be used by ZLS to query the build config. + expand_template( + name = name + ".build_runner", + out = name + ".build_runner.zig", + substitutions = { + "@@__TARGET__@@": str(utils.to_label(build_config_printer)), + }, + template = Label(":zls_build_runner.zig"), + ) + + # Generate the Zig source file for the ZLS runner binary which embeds the + # rlocationpath of all runtime dependencies of the ZLS runner binary. + zls_write_runner_zig_src( + name = name + ".runner", + out = name + ".runner.zig", + build_runner = ":" + name + ".build_runner.zig", + ) + + zig_binary( + name = name, + main = name + ".runner", + data = [ + "@rules_zig//zig:resolved_toolchain", + "@zml//third_party/zls:resolved_toolchain", + name + ".build_runner", + ], + deps = [ + "@rules_zig//zig/runfiles", + ], + **kwargs + ) diff --git a/third_party/zls/zls_runner.zig b/third_party/zls/zls_runner.zig new file mode 100644 index 0000000..81555e0 --- /dev/null +++ b/third_party/zls/zls_runner.zig @@ -0,0 +1,123 @@ +/// Custom ZLS launcher. +/// +/// Sets up paths to ZLS dependencies with Bazel runfiles. +/// +/// This file is used as a template by `zls_write_runner_zig_src.bzl`. +const std = @import("std"); +const runfiles = @import("runfiles"); +const bazel_builtin = @import("bazel_builtin"); + +fn getRandomFilename(buf: *[std.fs.max_name_bytes]u8, extension: []const u8) ![]const u8 { + const random_bytes_count = 12; + const sub_path_len = comptime std.fs.base64_encoder.calcSize(random_bytes_count); + + var random_bytes: [random_bytes_count]u8 = undefined; + std.crypto.random.bytes(&random_bytes); + var random_name: [sub_path_len]u8 = undefined; + _ = std.fs.base64_encoder.encode(&random_name, &random_bytes); + + const fmt_template = "/tmp/{s}{s}"; + const fmt_args = .{ + @as([]const u8, &random_name), + extension, + }; + return std.fmt.bufPrint(buf, fmt_template, fmt_args) catch @panic("OOM"); +} + +const Config = struct { + /// Override the Zig library path. Will be automatically resolved using the 'zig_exe_path'. + zig_lib_path: ?[]const u8 = null, + + /// Specify the path to the Zig executable (not the directory). If unset, zig is looked up in `PATH`. e.g. `/path/to/zig-templeos-armless-1.0.0/zig`. + zig_exe_path: ?[]const u8 = null, + + /// Specify a custom build runner to resolve build system information. + build_runner_path: ?[]const u8 = null, + + /// Path to a directory that will be used as zig's cache. Will default to `${KnownFolders.Cache}/zls`. + global_cache_path: ?[]const u8 = null, +}; + +pub fn main() !void { + const gpa = std.heap.page_allocator; + var arena = std.heap.ArenaAllocator.init(gpa); + const allocator = arena.allocator(); + + var r_ = try runfiles.Runfiles.create(.{ .allocator = allocator }) orelse + return error.RunfilesNotFound; + defer r_.deinit(allocator); + + const r = r_.withSourceRepo(bazel_builtin.current_repository); + + const zls_bin_rpath = "@@__ZLS_BIN_RPATH__@@"; + var zls_bin_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const zls_bin_path = try r.rlocation(zls_bin_rpath, &zls_bin_path_buf) orelse + return error.RLocationNotFound; + + const zig_exe_rpath = "@@__ZIG_EXE_RPATH__@@"; + var zig_exe_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const zig_exe_path = blk2: { + if (zig_exe_rpath[0] == '/') { + break :blk2 zig_exe_rpath; + } else { + break :blk2 try r.rlocation(zig_exe_rpath, &zig_exe_path_buf) orelse + return error.RLocationNotFound; + } + }; + + const zig_lib_path = "@@__ZIG_LIB_PATH__@@"; + const zig_lib_computed_path = blk: { + if (zig_lib_path[0] == '/') { + break :blk zig_lib_path; + } else { + break :blk null; + } + }; + + const zls_build_runner_rpath = "@@__ZLS_BUILD_RUNNER_RPATH__@@"; + var zls_build_runner_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const zls_build_runner_path = try r.rlocation(zls_build_runner_rpath, &zls_build_runner_path_buf) orelse + return error.RLocationNotFound; + + const global_cache_path = "@@__GLOBAL_CACHE_PATH__@@"; + + const config: Config = .{ + .zig_exe_path = zig_exe_path, + .zig_lib_path = zig_lib_computed_path, + .build_runner_path = zls_build_runner_path, + .global_cache_path = global_cache_path, + }; + + var buf: [std.fs.max_name_bytes]u8 = undefined; + const tmp_file_path = try getRandomFilename(&buf, ".json"); + + { + var tmp_file = try std.fs.createFileAbsolute(tmp_file_path, .{}); + defer tmp_file.close(); + + var out_buf: [4096]u8 = undefined; + var tmp_file_writer = tmp_file.writer(&out_buf); + + const formatter = std.json.fmt(config, .{ .whitespace = .indent_2 }); + + try tmp_file_writer.interface.print("{f}", .{formatter}); + try tmp_file_writer.interface.flush(); + } + + const args = std.process.argsAlloc(allocator) catch return error.ArgsAllocFailed; + defer std.process.argsFree(allocator, args); + + const exec_args_len = args.len - 1 + 3; // Skip args[0] and add "zls" + --config-path + tmp_file_path + var exec_args = try allocator.alloc([]const u8, exec_args_len); + + exec_args[0] = zls_bin_path; // "zls" + + // ${@} + for (args[1..], 0..) |arg, i| { + exec_args[1 + i] = arg; + } + exec_args[exec_args.len - 2] = "--config-path"; + exec_args[exec_args.len - 1] = tmp_file_path; + + return std.process.execv(arena.allocator(), exec_args); +} diff --git a/third_party/zls/zls_toolchain.bzl b/third_party/zls/zls_toolchain.bzl new file mode 100644 index 0000000..b5f7e6c --- /dev/null +++ b/third_party/zls/zls_toolchain.bzl @@ -0,0 +1,123 @@ +"""Implementation of the zls_toolchain rule.""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +_VERSION = "0.15.0" + +_ZLS_PLATFORMS = { + "x86_64-linux": struct( + compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + ), + "x86_64-macos": struct( + compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + ), + "aarch64-macos": struct( + compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", + ], + ), +} + +_ZLS_VERSIONS = { + "x86_64-linux": "508bfe3fd637d2a02f07f3fc7da8900351f407116b03685c5dae26b4f01a30de", + "x86_64-macos": "46c31838bfef5adcc7fee82428c3ec2b9abbfae38242639afea5f242ee133d93", + "aarch64-macos": "76c7a23190f67e67970024065f689c2c49b3c7b0fc16876fb24ef199fb05fc2a", +} + +ZlsToolchainInfo = provider( + doc = "Provide info for executing zls", + fields = { + "bin": "Executable ZLS binary", + }, +) + +def _zls_toolchain_impl(ctx): + binary = ctx.file.bin + + default_info = DefaultInfo( + files = depset([binary]), + runfiles = ctx.runfiles(files = [binary]), + ) + zlstoolchaininfo = ZlsToolchainInfo( + bin = binary, + ) + toolchain_info = platform_common.ToolchainInfo( + default = default_info, + zlstoolchaininfo = zlstoolchaininfo, + ) + + return [ + default_info, + zlstoolchaininfo, + toolchain_info, + ] + +zls_toolchain = rule( + implementation = _zls_toolchain_impl, + attrs = { + "bin": attr.label( + executable = True, + allow_single_file = True, + cfg = "exec", + ), + }, +) + +def _zls_toolchains_repos_impl(mctx): + for platform, _ in _ZLS_PLATFORMS.items(): + http_archive( + name = "zls_{}".format(platform), + url = "https://github.com/zigtools/zls/releases/download/{version}/zls-{platform}.tar.xz".format( + version = _VERSION, + platform = platform, + ), + sha256 = _ZLS_VERSIONS[platform], + build_file_content = """\ +load("@zml//third_party/zls:zls_toolchain.bzl", "zls_toolchain") +zls_toolchain(name = "toolchain", bin = "zls") +""", + ) + + zls_toolchains_repo(name = "zls_toolchains") + + return mctx.extension_metadata( + reproducible = True, + root_module_direct_deps = ["zls_toolchains"], + root_module_direct_dev_deps = [], + ) + +zls_toolchains = module_extension( + implementation = _zls_toolchains_repos_impl, +) + +def _zls_toolchains_repo_impl(rctx): + build_content = """# @generated by @zml//third_party/zls:zls_toolchain.bzl""" + + for platform, config in _ZLS_PLATFORMS.items(): + build_content += """ +toolchain( + name = "{platform}_toolchain", + exec_compatible_with = {compatible_with}, + target_compatible_with = {compatible_with}, + toolchain = "@zls_{platform}//:toolchain", + toolchain_type = "@zml//third_party/zls:toolchain_type", +) +""".format( + platform = platform, + compatible_with = config.compatible_with, + ) + + rctx.file("BUILD.bazel", build_content) + +zls_toolchains_repo = repository_rule( + _zls_toolchains_repo_impl, + doc = """Creates a repository with toolchain definitions for all known platforms + which can be registered or selected.""", +) diff --git a/third_party/zls/zls_write_build_config.bzl b/third_party/zls/zls_write_build_config.bzl new file mode 100644 index 0000000..27f9be2 --- /dev/null +++ b/third_party/zls/zls_write_build_config.bzl @@ -0,0 +1,138 @@ +"""Generates the ZLS build config file.""" + +load("@rules_cc//cc/common:cc_common.bzl", "cc_common") +load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") +load("@rules_zig//zig/private:cc_helper.bzl", "need_translate_c") +load("@rules_zig//zig/private/common:translate_c.bzl", "zig_translate_c") +load("@rules_zig//zig/private/common:zig_cache.bzl", "zig_cache_output") +load("@rules_zig//zig/private/common:zig_lib_dir.bzl", "zig_lib_dir") +load("@rules_zig//zig/private/providers:zig_module_info.bzl", "ZigModuleInfo", "zig_module_info") +load( + "@rules_zig//zig/private/providers:zig_target_info.bzl", + "zig_target_platform", +) + +def _zls_construct_zig_module_info_impl(target, ctx): + """Aspect that constructs ZigModuleInfo for zig_binary and zig_library rules.""" + if ZigModuleInfo in target: + return [] + + if ctx.rule.kind not in ("zig_binary", "zig_static_library", "zig_shared_library"): + return [] + + cdeps = [] + zdeps = [] + for dep in ctx.rule.attr.deps: + if ZigModuleInfo in dep: + zdeps.append(dep[ZigModuleInfo]) + elif CcInfo in dep: + cdeps.append(dep[CcInfo]) + + root_module_is_only_dep = len(ctx.rule.attr.deps) == 1 and ZigModuleInfo in ctx.rule.attr.deps[0] + if root_module_is_only_dep: + root_module = ctx.rule.attr.deps[0][ZigModuleInfo] + else: + root_module = zig_module_info( + name = ctx.rule.attr.name, + canonical_name = target.label.name, + main = ctx.rule.file.main, + srcs = ctx.rule.files.srcs, + extra_srcs = ctx.rule.files.extra_srcs, + deps = zdeps, # [bazel_builtin_module(ctx)], + cdeps = cdeps, + zigopts = [], + ) + + return [ + root_module, + ] + +zls_construct_zig_module_info = aspect( + implementation = _zls_construct_zig_module_info_impl, +) + +def _add_context(context): + return json.encode(struct(name = context.name, path = "@@__BUILD_WORKSPACE_DIRECTORY__@@/" + context.main)) + +def _zls_write_build_config_impl(ctx): + zigtoolchaininfo = ctx.toolchains["@rules_zig//zig:toolchain_type"].zigtoolchaininfo + zigtargetinfo = ctx.toolchains["@rules_zig//zig/target:toolchain_type"].zigtargetinfo + + c_module_contexts = [] + c_module_inputs = [] + cc_info = cc_common.merge_cc_infos(cc_infos = [dep[ZigModuleInfo].cc_info for dep in ctx.attr.deps if dep[ZigModuleInfo].cc_info]) + if need_translate_c(cc_info): + global_args = ctx.actions.args() + global_args.use_param_file("@%s") + + zig_target_platform( + target = zigtargetinfo, + args = global_args, + ) + + zig_lib_dir( + zigtoolchaininfo = zigtoolchaininfo, + args = global_args, + ) + + zig_cache_output( + zigtoolchaininfo = zigtoolchaininfo, + args = global_args, + ) + + c_module = zig_translate_c( + ctx = ctx, + name = "c", + zigtoolchaininfo = zigtoolchaininfo, + global_args = global_args, + cc_infos = [cc_info], + ) + + c_module_contexts = [depset(direct = [c_module.module_context])] + c_module_inputs = [c_module.transitive_inputs] + + contexts = depset( + direct = [dep[ZigModuleInfo].module_context for dep in ctx.attr.deps], + transitive = [dep[ZigModuleInfo].transitive_module_contexts for dep in ctx.attr.deps] + c_module_contexts, + ) + + template_dict = ctx.actions.template_dict() + template_dict.add_joined("@@PACKAGES@@", contexts, map_each = _add_context, join_with = ",\n ") + + config = ctx.outputs.out + ctx.actions.expand_template( + output = config, + template = ctx.file._zls_completion_tpl, + computed_substitutions = template_dict, + ) + + return [ + DefaultInfo( + files = depset(direct = [config]), + # Not really runfiles but we need to pull the output of translate_c... + runfiles = ctx.runfiles(transitive_files = depset(transitive = c_module_inputs)), + ), + ] + +zls_write_build_config = rule( + implementation = _zls_write_build_config_impl, + attrs = { + "deps": attr.label_list( + providers = [ZigModuleInfo], + mandatory = True, + aspects = [zls_construct_zig_module_info], + ), + "out": attr.output( + mandatory = True, + ), + "_zls_completion_tpl": attr.label( + default = ":zls.completion.tpl", + allow_single_file = True, + ), + }, + toolchains = [ + "@rules_zig//zig:toolchain_type", + "@rules_zig//zig/target:toolchain_type", + ], + fragments = ["cpp"], +) diff --git a/third_party/zls/zls_write_runner_zig_src.bzl b/third_party/zls/zls_write_runner_zig_src.bzl new file mode 100644 index 0000000..ac89e08 --- /dev/null +++ b/third_party/zls/zls_write_runner_zig_src.bzl @@ -0,0 +1,45 @@ +"""Generates the ZLS runner source file.""" + +load("@aspect_bazel_lib//lib:paths.bzl", "to_rlocation_path") + +def _zls_write_runner_zig_src_impl(ctx): + zigtoolchaininfo = ctx.toolchains["@rules_zig//zig:toolchain_type"].zigtoolchaininfo + zlstoolchaininfo = ctx.toolchains["//third_party/zls:toolchain_type"].zlstoolchaininfo + + zls_runner = ctx.outputs.out + ctx.actions.expand_template( + output = zls_runner, + template = ctx.file._runner_tpl, + substitutions = { + "@@__ZIG_EXE_RPATH__@@": zigtoolchaininfo.zig_exe_rpath, + "@@__ZIG_LIB_PATH__@@": zigtoolchaininfo.zig_lib_rpath, + "@@__ZLS_BIN_RPATH__@@": to_rlocation_path(ctx, zlstoolchaininfo.bin), + "@@__ZLS_BUILD_RUNNER_RPATH__@@": to_rlocation_path(ctx, ctx.file.build_runner), + "@@__GLOBAL_CACHE_PATH__@@": zigtoolchaininfo.zig_cache, + }, + ) + + return [ + DefaultInfo(files = depset([zls_runner])), + ] + +zls_write_runner_zig_src = rule( + implementation = _zls_write_runner_zig_src_impl, + attrs = { + "out": attr.output( + mandatory = True, + ), + "build_runner": attr.label( + allow_single_file = True, + mandatory = True, + ), + "_runner_tpl": attr.label( + default = "zls_runner.zig", + allow_single_file = True, + ), + }, + toolchains = [ + "@rules_zig//zig:toolchain_type", + "//third_party/zls:toolchain_type", + ], +) diff --git a/tools/zls.sh b/tools/zls.sh index 86827c8..8428ee0 100755 --- a/tools/zls.sh +++ b/tools/zls.sh @@ -1,4 +1,4 @@ #!/bin/bash cd "$(dirname "${BASH_SOURCE[0]}")" cd "$(bazel info workspace)" -exec bazel run -- @zml//:zls "${@}" +exec bazel run -- @zml//:completion "${@}" diff --git a/zml/BUILD.bazel b/zml/BUILD.bazel index 3d2ae08..47a3b5c 100644 --- a/zml/BUILD.bazel +++ b/zml/BUILD.bazel @@ -54,7 +54,7 @@ zig_library( "torch.zig", "zml.zig", ], - copts = ["-lc", "-freference-trace=20"], + zigopts = ["-lc", "-freference-trace=20"], main = "zml.zig", visibility = ["//visibility:public"], deps = [