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

ZIR-249: Support out-of-tree builds #89

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
common --enable_bzlmod
build -c opt
# Make sure we still detect assertion failures
build --copt=-UNDEBUG
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.0
7.4.1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
bazel-*
compile_commands.json
dist/
MODULE.bazel.lock
rust-project.json
target/
tmp/
Expand Down
97 changes: 97 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2024 RISC Zero, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""The Zirgen Compiler"""

module(
name = "zirgen",
repo_name = "risc0_zirgen",
)

bazel_dep(name = "rules_license", version = "0.0.7")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")

bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True)

# Generate compile_commands.json file for IDE integration
# https://github.com/hedronvision/bazel-compile-commands-extractor
# tip: use `bazel run @hedron_compile_commands//:refresh_all`
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
commit = "4f28899228fb3ad0126897876f147ca15026151e",
)

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# LLVM doesn't support bzlmod yet, so we have to pull it in the WORKSPACE way
LLVM_COMMIT = "fea7b65f23632b42ff8f7e2595ac0641e2c1d214"
LLVM_SHA256 = "7f8da8de897f20824e7d11204768ccb29a47419385bb6c9b3f5eccfd738d7510"
http_archive(
name = "llvm-raw",
build_file_content = "# empty",
sha256 = LLVM_SHA256,
strip_prefix = "llvm-project-" + LLVM_COMMIT,
urls = ["https://github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT)],
)

http_archive(
name = "llvm_zlib",
build_file = "@llvm-raw//utils/bazel/third_party_build:zlib-ng.BUILD",
sha256 = "e36bb346c00472a1f9ff2a0a4643e590a254be6379da7cddd9daeb9a7f296731",
strip_prefix = "zlib-ng-2.0.7",
urls = [
"https://github.com/zlib-ng/zlib-ng/archive/refs/tags/2.0.7.zip",
],
)

http_archive(
name = "llvm_zstd",
build_file = "@llvm-raw//utils/bazel/third_party_build:zstd.BUILD",
sha256 = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0",
strip_prefix = "zstd-1.5.2",
urls = [
"https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz",
],
)

http_archive(
name = "riscv_tests",
build_file = "//bazel/third_party:riscv_tests.BUILD",
sha256 = "831a91955287865f9c09bbb7102f7ba2987d86823fd7b67c8a62eff46e21a83c",
strip_prefix = "riscv-tests-a6ab6ae6008ffc2ea907ea9f6d2b8379583e7d56",
url = "https://github.com/riscv/riscv-tests/archive/a6ab6ae6008ffc2ea907ea9f6d2b8379583e7d56.zip",
)

http_archive(
name = "rules_conda",
patch_args = ["-p1"],
patches = ["//bazel/third_party/rules_conda:prefix.patch"],
sha256 = "9793f86162ec5cfb32a1f1f13f5bf776e2c06b243c4f1ee314b9ec870144220d",
url = "https://github.com/spietras/rules_conda/releases/download/0.1.0/rules_conda-0.1.0.zip",
)

HERMETIC_CC_TOOLCHAIN_VERSION = "v3.1.0"

http_archive(
name = "hermetic_cc_toolchain",
sha256 = "df091afc25d73b0948ed371d3d61beef29447f690508e02bc24e7001ccc12d38",
urls = [
"https://mirror.bazel.build/github.com/uber/hermetic_cc_toolchain/releases/download/{0}/hermetic_cc_toolchain-{0}.tar.gz".format(HERMETIC_CC_TOOLCHAIN_VERSION),
"https://github.com/uber/hermetic_cc_toolchain/releases/download/{0}/hermetic_cc_toolchain-{0}.tar.gz".format(HERMETIC_CC_TOOLCHAIN_VERSION),
],
)
163 changes: 0 additions & 163 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,163 +0,0 @@
workspace(name = "zirgen")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

http_archive(
name = "bazel_skylib",
sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.3.0.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

LLVM_COMMIT = "fea7b65f23632b42ff8f7e2595ac0641e2c1d214"

LLVM_SHA256 = "7f8da8de897f20824e7d11204768ccb29a47419385bb6c9b3f5eccfd738d7510"

http_archive(
name = "llvm-raw",
build_file_content = "# empty",
sha256 = LLVM_SHA256,
strip_prefix = "llvm-project-" + LLVM_COMMIT,
urls = ["https://github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT)],
)

load("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure")

llvm_configure(name = "llvm-project")

maybe(
http_archive,
name = "llvm_zlib",
build_file = "@llvm-raw//utils/bazel/third_party_build:zlib-ng.BUILD",
sha256 = "e36bb346c00472a1f9ff2a0a4643e590a254be6379da7cddd9daeb9a7f296731",
strip_prefix = "zlib-ng-2.0.7",
urls = [
"https://github.com/zlib-ng/zlib-ng/archive/refs/tags/2.0.7.zip",
],
)

maybe(
http_archive,
name = "llvm_zstd",
build_file = "@llvm-raw//utils/bazel/third_party_build:zstd.BUILD",
sha256 = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0",
strip_prefix = "zstd-1.5.2",
urls = [
"https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz",
],
)

load("//bazel/toolchain/risc0:repo.bzl", "risc0_toolchain")

risc0_toolchain(name = "risc0_toolchain")

load("//bazel/toolchain/rv32im-linux:repo.bzl", "rv32im_linux_toolchain")

rv32im_linux_toolchain(name = "rv32im_linux_toolchain")

http_archive(
name = "riscv_tests",
build_file = "//bazel/third_party:riscv_tests.BUILD",
sha256 = "831a91955287865f9c09bbb7102f7ba2987d86823fd7b67c8a62eff46e21a83c",
strip_prefix = "riscv-tests-a6ab6ae6008ffc2ea907ea9f6d2b8379583e7d56",
url = "https://github.com/riscv/riscv-tests/archive/a6ab6ae6008ffc2ea907ea9f6d2b8379583e7d56.zip",
)

http_archive(
name = "rules_conda",
patch_args = ["-p1"],
patches = ["//bazel/third_party/rules_conda:prefix.patch"],
sha256 = "9793f86162ec5cfb32a1f1f13f5bf776e2c06b243c4f1ee314b9ec870144220d",
url = "https://github.com/spietras/rules_conda/releases/download/0.1.0/rules_conda-0.1.0.zip",
)

load("@rules_conda//:defs.bzl", "conda_create", "load_conda", "register_toolchain")

load_conda(
install_mamba = True,
installer = "miniforge",
quiet = False,
)

conda_create(
name = "py3_env",
environment = "@//:environment.yml",
quiet = False,
use_mamba = True,
)

register_toolchain(py3_env = "py3_env")

register_toolchains(
"//bazel/rules/clang_format:toolchain",
)

http_archive(
name = "com_google_googletest",
sha256 = "ad7fdba11ea011c1d925b3289cf4af2c66a352e18d4c7264392fead75e919363",
strip_prefix = "googletest-1.13.0",
url = "https://github.com/google/googletest/archive/refs/tags/v1.13.0.tar.gz",
)

http_archive(
name = "rules_pkg",
sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
],
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
# tip: use `bazel run @hedron_compile_commands//:refresh_all`
http_archive(
name = "hedron_compile_commands",
strip_prefix = "bazel-compile-commands-extractor-4f28899228fb3ad0126897876f147ca15026151e",
url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/4f28899228fb3ad0126897876f147ca15026151e.tar.gz",
)

load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()

load("@hedron_compile_commands//:workspace_setup_transitive.bzl", "hedron_compile_commands_setup_transitive")

hedron_compile_commands_setup_transitive()

load("@hedron_compile_commands//:workspace_setup_transitive_transitive.bzl", "hedron_compile_commands_setup_transitive_transitive")

hedron_compile_commands_setup_transitive_transitive()

load("@hedron_compile_commands//:workspace_setup_transitive_transitive_transitive.bzl", "hedron_compile_commands_setup_transitive_transitive_transitive")

hedron_compile_commands_setup_transitive_transitive_transitive()

HERMETIC_CC_TOOLCHAIN_VERSION = "v3.1.0"

http_archive(
name = "hermetic_cc_toolchain",
sha256 = "df091afc25d73b0948ed371d3d61beef29447f690508e02bc24e7001ccc12d38",
urls = [
"https://mirror.bazel.build/github.com/uber/hermetic_cc_toolchain/releases/download/{0}/hermetic_cc_toolchain-{0}.tar.gz".format(HERMETIC_CC_TOOLCHAIN_VERSION),
"https://github.com/uber/hermetic_cc_toolchain/releases/download/{0}/hermetic_cc_toolchain-{0}.tar.gz".format(HERMETIC_CC_TOOLCHAIN_VERSION),
],
)

load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains")

# Plain zig_toolchains() will pick reasonable defaults. See
# toolchain/defs.bzl:toolchains on how to change the Zig SDK version and
# download URL.
zig_toolchains()
44 changes: 44 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
workspace(name = "zirgen")

load("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure")
llvm_configure(name = "llvm-project")

load("//bazel/toolchain/risc0:repo.bzl", "risc0_toolchain")

risc0_toolchain(name = "risc0_toolchain")

load("//bazel/toolchain/rv32im-linux:repo.bzl", "rv32im_linux_toolchain")

rv32im_linux_toolchain(name = "rv32im_linux_toolchain")

load("@rules_conda//:defs.bzl", "conda_create", "load_conda", "register_toolchain")

load_conda(
install_mamba = True,
installer = "miniforge",
quiet = False,
)

conda_create(
name = "py3_env",
environment = "@//:environment.yml",
quiet = False,
use_mamba = True,
)

register_toolchain(py3_env = "py3_env")

register_toolchains(
"//bazel/rules/clang_format:toolchain",
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains")

# Plain zig_toolchains() will pick reasonable defaults. See
# toolchain/defs.bzl:toolchains on how to change the Zig SDK version and
# download URL.
zig_toolchains()
1 change: 1 addition & 0 deletions bazel/toolchain/risc0/gcc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def gcc_toolchain_config(name, sysroot):
"gcov",
"ld",
"nm",
"objcopy",
"objdump",
"strip",
]}
Expand Down
1 change: 1 addition & 0 deletions bazel/toolchain/rv32im-linux/gcc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def gcc_toolchain_config(name, sysroot):
"gcov",
"ld",
"nm",
"objcopy",
"objdump",
"strip",
]}
Expand Down
2 changes: 0 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
channels:
- conda-forge
dependencies:
- bison=3.8
- clang-format=13.0.1
- flex=2.6.4
- python=3.10
2 changes: 1 addition & 1 deletion risc0/core/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//risc0/core",
"@com_google_googletest//:gtest",
"@googletest//:gtest",
],
)
6 changes: 3 additions & 3 deletions zirgen/Dialect/BigInt/Precompiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ First, create files under [`zirgen/circuit/bigint/`] which will hold the acceler
void genAdd128(mlir::OpBuilder& builder, mlir::Location loc) {
auto lhs = builder.create<BigInt::LoadOp>(loc, 128, 11, 0);
auto rhs = builder.create<BigInt::LoadOp>(loc, 128, 12, 0);
auto sum = builder.create<BigInt::AddOp>(loc, lhs, rhs);
auto sum = builder.create<BigInt::AddOp>(loc, lhs, rhs);
builder.create<BigInt::StoreOp>(loc, sum, 13, 0);
}
```
Expand All @@ -33,7 +33,7 @@ enum class Program {

Make this option available by extending the arg parser's declaration:
```

clEnumValN(Program::Add128, "add128", "Add128")),
```

Expand All @@ -53,7 +53,7 @@ The `bigint2c` program can now compile our addition function into a "bibc" forma
genrule(
name = "add128",
outs = ["add128.blob"],
exec_tools = [":bigint2c"],
tools = [":bigint2c"],
cmd = "$(location //zirgen/circuit/bigint:bigint2c) --program=add128 --bitwidth 128 > $(OUTS)"
)
```
Expand Down
Loading
Loading