Skip to content

Commit

Permalink
POC: run all contracts through solc with bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Sep 12, 2022
1 parent 1414dde commit 7c3e50d
Show file tree
Hide file tree
Showing 24 changed files with 1,758 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
bazel-out/
33 changes: 32 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")

##########
# Protobuf
Expand Down Expand Up @@ -91,3 +91,34 @@ go_rules_dependencies()
go_register_toolchains(version = "1.18")

gazelle_dependencies()

load("//sol:repositories.bzl", "LATEST_VERSION", "rules_sol_dependencies", "sol_register_toolchains")

rules_sol_dependencies()

sol_register_toolchains(
name = "solc",
sol_version = LATEST_VERSION,
)

# Fetch our contracts dependencies directly from git repos, like Forge does it:
# https://book.getfoundry.sh/projects/dependencies
new_git_repository(
name = "openzeppelin-contracts",
remote = "[email protected]:OpenZeppelin/openzeppelin-contracts.git",
# NOTE: this version ought to match what appears in yarn.lock, or behavior
# between Bazel and legacy npm-based dependency management will vary.
commit = "8c49ad74eae76ee389d038780d407cf90b4ae1de", # v4.7.0
shallow_since = "1656493217 +0200",
build_file = "openzeppelin-contracts.BUILD",
)

new_git_repository(
name = "erc721a",
remote = "[email protected]:chiru-labs/ERC721A.git",
# NOTE: this version ought to match what appears in yarn.lock, or behavior
# between Bazel and legacy npm-based dependency management will vary.
commit = "9859cd2edb1a8b4c2db5e46031abbb3253e42467", # v4.2.2
shallow_since = "1659313679 -0700",
build_file = "erc721a.BUILD",
)
7 changes: 7 additions & 0 deletions contracts/crypto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "crypto",
srcs = glob(["*.sol"]),
deps = ["@openzeppelin-contracts"],
)
11 changes: 11 additions & 0 deletions contracts/erc721/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "erc721",
srcs = glob(["*.sol"]),
deps = [
"//contracts/utils",
"@erc721a",
"@openzeppelin-contracts",
],
)
6 changes: 6 additions & 0 deletions contracts/factories/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "factories",
srcs = glob(["*.sol"]),
)
6 changes: 6 additions & 0 deletions contracts/random/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "random",
srcs = glob(["*.sol"]),
)
10 changes: 10 additions & 0 deletions contracts/sales/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "sales",
srcs = glob(["*.sol"]),
deps = [
"//contracts/utils",
"@openzeppelin-contracts",
],
)
10 changes: 10 additions & 0 deletions contracts/thirdparty/opensea/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "opensea",
srcs = glob(["*.sol"]),
deps = [
"//contracts/utils",
"@openzeppelin-contracts",
],
)
6 changes: 6 additions & 0 deletions contracts/thirdparty/weth/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "weth",
srcs = glob(["*.sol"]),
)
8 changes: 8 additions & 0 deletions contracts/utils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//sol:defs.bzl", "sol_sources")

sol_sources(
name = "utils",
srcs = glob(["*.sol"]),
visibility = ["//visibility:public"],
deps = ["@openzeppelin-contracts"],
)
9 changes: 9 additions & 0 deletions external/erc721a.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@//sol:defs.bzl", "sol_sources")

package(default_visibility = ["//visibility:public"])

sol_sources(
name = "erc721a",
srcs = glob(["**/*.sol"]),
remappings = {"erc721a/": "./external/erc721a/"},
)
9 changes: 9 additions & 0 deletions external/openzeppelin-contracts.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@//sol:defs.bzl", "sol_sources")

package(default_visibility = ["//visibility:public"])

sol_sources(
name = "openzeppelin-contracts",
srcs = glob(["**/*.sol"]),
remappings = {"@openzeppelin/contracts/": "./external/openzeppelin-contracts/contracts/"},
)
46 changes: 46 additions & 0 deletions sol/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# For stardoc to reference the files
exports_files(["defs.bzl"])

# This is the target rule authors should put in their "toolchains"
# attribute in order to get a runtime for the correct platform.
# See https://docs.bazel.build/versions/main/toolchains.html#writing-rules-that-use-toolchains
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

bzl_library(
name = "repositories",
srcs = ["repositories.bzl"],
visibility = ["//visibility:public"],
deps = [
"//sol/private:toolchains_repo",
"//sol/private:versions",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

bzl_library(
name = "defs",
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
deps = [
"//sol/private:sol_binary",
"//sol/private:sol_sources",
],
)

bzl_library(
name = "providers",
srcs = ["providers.bzl"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "toolchain",
srcs = ["toolchain.bzl"],
visibility = ["//visibility:public"],
)
24 changes: 24 additions & 0 deletions sol/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""# Bazel rules for Solidity
See <https://docs.soliditylang.org>
"""

load("//sol/private:sol_binary.bzl", lib = "sol_binary")
load("//sol/private:sol_sources.bzl", src = "sol_sources")
load(":providers.bzl", "SolSourcesInfo")

sol_binary = rule(
implementation = lib.implementation,
attrs = lib.attrs,
doc = """sol_binary compiles Solidity source files with solc""",
toolchains = lib.toolchains,
)

sol_sources = rule(
implementation = src.implementation,
attrs = src.attrs,
doc = """Collect .sol source files to be imported as library code.
Performs no actions, so semantically equivalent to filegroup().
""",
provides = [SolSourcesInfo],
)
36 changes: 36 additions & 0 deletions sol/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "toolchains_repo",
srcs = ["toolchains_repo.bzl"],
visibility = ["//sol:__subpackages__"],
)

bzl_library(
name = "versions",
srcs = ["versions.bzl"],
visibility = ["//sol:__subpackages__"],
)

bzl_library(
name = "sol_binary",
srcs = ["sol_binary.bzl"],
visibility = ["//sol:__subpackages__"],
deps = [
"//sol:providers",
"@aspect_rules_js//js:libs",
"@aspect_rules_js//js:providers",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "sol_sources",
srcs = ["sol_sources.bzl"],
visibility = ["//sol:__subpackages__"],
deps = [
"//sol:providers",
"@aspect_rules_js//js:libs",
"@aspect_rules_js//js:providers",
],
)
Loading

0 comments on commit 7c3e50d

Please sign in to comment.