Skip to content

Add bazel build system support for simple_switch_grpc#1401

Open
c8ef wants to merge 7 commits into
p4lang:mainfrom
c8ef:bazel
Open

Add bazel build system support for simple_switch_grpc#1401
c8ef wants to merge 7 commits into
p4lang:mainfrom
c8ef:bazel

Conversation

@c8ef
Copy link
Copy Markdown
Contributor

@c8ef c8ef commented May 26, 2026

c8ef added 4 commits May 26, 2026 21:41
Signed-off-by: c8ef <c8ef@outlook.com>
Signed-off-by: c8ef <c8ef@outlook.com>
Signed-off-by: c8ef <c8ef@outlook.com>
Signed-off-by: c8ef <c8ef@outlook.com>
Comment thread targets/simple_switch_grpc/tests/base_test.h
@c8ef c8ef marked this pull request as ready for review May 26, 2026 14:38
@c8ef
Copy link
Copy Markdown
Contributor Author

c8ef commented May 26, 2026

cc @matthewtlam @qobilidop for review.

Comment thread .github/workflows/bazel.yml
Comment thread .bazelversion.license
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file needed? I would remove it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is necessary for the copyright check. We cannot include the copyright notice in the .bazelversion file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewtlam If .bazelversion files have an explicit comment syntax that they support (e.g. everything after a '#' character on a line is a comment), then it is usually preferred to have the copyright and license notice in comments in the file itself.

If .bazelversion files do not suport comments, then the REUSE tool that is now being used in behavioral-model (and soon hopefully all p4lang repositories) requires that there be a separate file with a suffix of ".license" in its name that contains the copyright and license info. There is also a way to put this info in a different separate file, but the ".license" file is good if there are only a few files that don't support comment syntax.

Comment thread MODULE.bazel
# SPDX-License-Identifier: Apache-2.0

module(
name = "behavioral-model",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: behavioral_model

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on this, but is using this different from the project name on GitHub?

Comment thread MODULE.bazel
bazel_dep(name = "rules_cc", version = "0.2.17")
bazel_dep(name = "xxhash", version = "0.8.3.bcr.1")

bazel_dep(name = "googletest", version = "1.17.0.bcr.2", dev_dependency = True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you put everything in alphabetical order. Is there an obvious reason why this one isn't either?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buildifier requires dev dependencies to be listed separately.

Comment thread MODULE.bazel
# SPDX-FileCopyrightText: 2026 Yuao Ma
#
# SPDX-License-Identifier: Apache-2.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is apart of Google standard to have the following https://github.com/p4lang/p4c/blob/main/BUILD.bazel#L8C1-L20C27

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that there isn't a root-level license file for bmv2. Given this, I'm not sure if simply adding an Apache 2.0 notice is the best approach. Love to hear your thoughts on this @jafingerhut.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a root-level LICENSE file that is a symbol link to LICENSES/Apache-2.0.txt soon.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done a few minutes ago when I merged this PR: #1403

If you update this PR's branch to the latest master/main version, the LICENSE file should be there.

Comment thread BUILD.bazel
# SPDX-FileCopyrightText: 2026 Yuao Ma
#
# SPDX-License-Identifier: Apache-2.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I still add package level visibility even though I have explicitly added visibility = ["//visibility:public"] for all cc_library?

Comment thread BUILD.bazel
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

exports_files(["VERSION"])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you say a little bit of why we export the version in this file specifically?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/c8ef/behavioral-model/blob/bc6ea00ee82890d856eca97192f9c3a47dd9da92/src/bm_sim/BUILD.bazel#L11

Because I want to use the content of the VERSION file directly to parse the version number. It's a bit hacky, but it will make release easier.

Comment thread PI/BUILD.bazel
"src/*.h",
]),
hdrs = ["bm/PI/pi.h"],
includes = ["."],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be okay for now since we are adding support for Bazel in legacy code, but is generally discouraged in Bazel projects because of
Header Collisions: If multiple libraries use includes = ["."], and both have a file named utils.h, it can lead to compiler confusion and hard-to-debug header resolution errors.
Explicit is Better: Bazel prefers absolute paths relative to the workspace root (e.g., #include "src/parser/parser.h") because it makes it immediately clear where a header is coming from.

Perhaps try removing them and seeing if the tests still pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this approach, but it didn't work. After investigating, I found that bmv2 includes BM-related headers using angle brackets (< >), which means the paths must be passed explicitly using -I. I attempted to use strip_include_prefix, but it passes the paths to the compiler via -iquote, which doesn't resolve angle bracket includes. Simply removing these includes is not an option.

ref: https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

Directories specified with -iquote apply only to the quote form of the directive, #include "file". Directories specified with -I, -isystem, or -idirafter apply to lookup for both the #include "file" and #include <file> directives.


load("@rules_cc//cc:cc_test.bzl", "cc_test")

_COMMON_SRCS = [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason why these are not apart of the _COMMON_DEPS?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_COMMON_SRCS is a list of source files, whereas _COMMON_DEPS is a list of local or remote dependencies.

"test_optional",
]

[
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall Bazel strongly discourages adding unnecessary dependencies and explicit library dependencies, but this is okay since I feel like all the test require all the exact same dependencies

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it seems like the same dependencies are being used but different scenarios are being tested.

Signed-off-by: c8ef <c8ef@outlook.com>
This reverts commit f4bc3c7.

Signed-off-by: c8ef <c8ef@outlook.com>
@c8ef c8ef requested review from jafingerhut and matthewtlam June 2, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants