Skip to content

Commit

Permalink
Add buildifier to Bazel workspace and run it on the bazel build files. (
Browse files Browse the repository at this point in the history
#4413)

* Install buildifier.

* Run buildifier.

* Add a check and a fix target.

* Linter complaints

* CI.
  • Loading branch information
fruffy authored Feb 12, 2024
1 parent 194920f commit 0785b49
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 84 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci-bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
chmod +x $BAZEL
sudo mv $BAZEL /usr/local/bin/bazel
- name: First, pass the lint test
run: bazel run //:buildifier_check

- name: Install Flex and Bison
run: sudo apt-get update && sudo apt-get install bison flex libfl-dev

Expand All @@ -64,6 +67,9 @@ jobs:
restore-keys: |
${{ runner.os }}-build-indirect-
- name: First, pass the lint test
run: bazel run //:buildifier_check

- name: Install bazelisk
run: |
curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.18.0/$BAZEL"
Expand Down
52 changes: 35 additions & 17 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@rules_license//rules:license.bzl", "license")
load("//:bazel/flex.bzl", "genlex")
load("//:bazel/bison.bzl", "genyacc")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

package(
default_applicable_licenses = ["//:license"],
Expand Down Expand Up @@ -134,10 +135,10 @@ cc_library(
":ir_generator_lex",
":ir_generator_yacc",
],
visibility = ["//visibility:private"],
deps = [
":lib",
],
visibility = ["//visibility:private"],
)

# The next rule builds the ir-generator tool binary.
Expand Down Expand Up @@ -206,6 +207,13 @@ cc_library(
"backends/dpdk/dbprint-dpdk.cpp",
"backends/dpdk/printUtils.cpp",
],
copts = [
# Where p4c should look for p4include at runtime.
("-DCONFIG_PKGDATADIR=\\\"external/%s\\\"" % repository_name()),
# This will work only if the binary is executed by Bazel. For a general
# solution, we would need to make p4c aware of Bazel, specifically:
# https://github.com/bazelbuild/bazel/blob/master/tools/cpp/runfiles/runfiles_src.h
],
textual_hdrs = glob([
"ir/**/*.h",
"frontends/**/*.h",
Expand All @@ -231,13 +239,6 @@ cc_library(
"@com_github_p4lang_p4runtime//:p4types_cc_proto",
"@com_google_protobuf//:protobuf",
],
copts = [
# Where p4c should look for p4include at runtime.
("-DCONFIG_PKGDATADIR=\\\"external/%s\\\"" % repository_name()),
# This will work only if the binary is executed by Bazel. For a general
# solution, we would need to make p4c aware of Bazel, specifically:
# https://github.com/bazelbuild/bazel/blob/master/tools/cpp/runfiles/runfiles_src.h
],
)

cc_library(
Expand All @@ -258,9 +259,9 @@ cc_library(
),
hdrs = glob(["backends/bmv2/simple_switch/*.h"]),
deps = [
":p4c_bmv2_common_lib",
":ir_frontend_midend_control_plane",
":lib",
":p4c_bmv2_common_lib",
],
)

Expand All @@ -278,13 +279,13 @@ cc_binary(
"backends/bmv2/simple_switch/main.cpp",
"backends/bmv2/simple_switch/version.h",
],
data = [":p4include"],
deps = [
":p4c_bmv2_common_lib",
":p4c_bmv2_simple_lib",
":ir_frontend_midend_control_plane",
":lib",
":p4c_bmv2_common_lib",
":p4c_bmv2_simple_lib",
],
data = [":p4include"],
)

# dpdk backend
Expand Down Expand Up @@ -349,15 +350,14 @@ cc_binary(
"backends/dpdk/main.cpp",
"backends/dpdk/version.h",
],
data = [":p4include"],
deps = [
":p4c_dpdk_lib",
":ir_frontend_midend_control_plane",
":lib",
":p4c_dpdk_lib",
],
data = [":p4include"],
)


# This builds the p4test backend.
cc_binary(
name = "p4c_backend_p4test",
Expand Down Expand Up @@ -421,15 +421,15 @@ cc_library(
name = "p4c_backend_graphs_lib",
srcs = [
"backends/graphs/controls.cpp",
"backends/graphs/graphs.cpp",
"backends/graphs/graph_visitor.cpp",
"backends/graphs/graphs.cpp",
"backends/graphs/p4c-graphs.cpp",
"backends/graphs/parsers.cpp",
],
hdrs = [
"backends/graphs/controls.h",
"backends/graphs/graphs.h",
"backends/graphs/graph_visitor.h",
"backends/graphs/graphs.h",
"backends/graphs/parsers.h",
"backends/graphs/version.h",
],
Expand All @@ -445,3 +445,21 @@ cc_library(
"@boost//:graph",
],
)

buildifier(
name = "buildifier_check",
diff_command = "diff -u",
exclude_patterns = [
"./build/*",
],
lint_mode = "warn",
mode = "diff",
)

buildifier(
name = "buildifier_fix",
exclude_patterns = [
"./build/*",
],
lint_mode = "fix",
)
29 changes: 28 additions & 1 deletion WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
workspace(name = "com_github_p4lang_p4c")

# -- Load Buildifier -----------------------------------------------------
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
name = "io_bazel_rules_go",
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies")

go_rules_dependencies()

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3",
strip_prefix = "buildtools-4.2.2",
urls = [
"https://github.com/bazelbuild/buildtools/archive/refs/tags/4.2.2.tar.gz",
],
)

# -- Direct dependencies. ------------------------------------------------------

load("//:bazel/p4c_deps.bzl", "p4c_deps")
Expand All @@ -26,8 +53,8 @@ load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_languag

switched_rules_by_language(
name = "com_google_googleapis_imports",
grpc = True,
cc = True,
grpc = True,
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
Expand Down
10 changes: 5 additions & 5 deletions backends/p4tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ cc_library(
],
features = ["-use_header_modules"],
deps = [
"@boost//:multiprecision",
"//:ir_frontend_midend_control_plane",
"//:lib",
"@boost//:multiprecision",
"@com_github_z3prover_z3//:api",
],
)
Expand Down Expand Up @@ -149,11 +149,11 @@ cc_library(
deps = [
":common",
":register_testgen_targets",
"//:ir_frontend_midend_control_plane",
"//:lib",
"@boost//:multiprecision",
"@com_github_pantor_inja//:inja",
"@nlohmann_json//:json",
"//:ir_frontend_midend_control_plane",
"//:lib",
],
)

Expand All @@ -170,9 +170,9 @@ cc_binary(
visibility = ["//visibility:public"],
deps = [
":testgen_lib",
"@boost//:multiprecision",
"@boost//:filesystem",
"//:lib",
"@boost//:filesystem",
"@boost//:multiprecision",
],
)

Expand Down
2 changes: 1 addition & 1 deletion bazel/BUILD.inja.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cc_library(
name = "inja",
hdrs = ["inja/inja.hpp"],
includes = ["."],
visibility = ["//visibility:public"],
deps = ["@nlohmann_json//:json"],
includes = ["."],
)
2 changes: 1 addition & 1 deletion bazel/BUILD.json.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cc_library(
name = "json",
hdrs = ["nlohmann/json.hpp"],
visibility = ["//visibility:public"],
includes = ["."],
visibility = ["//visibility:public"],
)
26 changes: 13 additions & 13 deletions bazel/example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
filegroup(
name = "ir_extension",
srcs = glob(["*.def"]),
visibility = ["//visibility:public"], # So p4c can compile these.
name = "ir_extension",
srcs = glob(["*.def"]),
visibility = ["//visibility:public"], # So p4c can compile these.
)

cc_binary(
name = "main",
srcs = ["main.cc"],
data = ["//p4:program.p4info.txt"],
deps = [
"@com_github_p4lang_p4c//:ir_frontend_midend_control_plane",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_protobuf//:protobuf",
],
linkopts = [
],
name = "main",
srcs = ["main.cc"],
data = ["//p4:program.p4info.txt"],
linkopts = [
],
deps = [
"@com_github_p4lang_p4c//:ir_frontend_midend_control_plane",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_protobuf//:protobuf",
],
)
43 changes: 35 additions & 8 deletions bazel/example/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@ workspace(name = "example_p4_project")

# May replace this with `git_repository` or `http_archive` in your own project.
local_repository(
name = "com_github_p4lang_p4c",
path = "../..",
# This part is optional: only needed for custom backends with IR extensions.
repo_mapping = {
# Tells p4c where to look for `:ir_extension` target: in this project.
"@com_github_p4lang_p4c_extension" : "@example_p4_project",
},
name = "com_github_p4lang_p4c",
path = "../..",
# This part is optional: only needed for custom backends with IR extensions.
repo_mapping = {
# Tells p4c where to look for `:ir_extension` target: in this project.
"@com_github_p4lang_p4c_extension": "@example_p4_project",
},
)

# -- Load Buildifier -----------------------------------------------------
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
name = "io_bazel_rules_go",
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies")

go_rules_dependencies()

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3",
strip_prefix = "buildtools-4.2.2",
urls = [
"https://github.com/bazelbuild/buildtools/archive/refs/tags/4.2.2.tar.gz",
],
)

load("@com_github_p4lang_p4c//:bazel/p4c_deps.bzl", "p4c_deps")
Expand All @@ -35,8 +62,8 @@ load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_languag

switched_rules_by_language(
name = "com_google_googleapis_imports",
grpc = True,
cc = True,
grpc = True,
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
Expand Down
Loading

0 comments on commit 0785b49

Please sign in to comment.