Skip to content

Commit

Permalink
Merge pull request #27 from ApexAI/20-enable-buildifier
Browse files Browse the repository at this point in the history
Enable buildifier
  • Loading branch information
kilian-funk authored Oct 8, 2024
2 parents 38d3c91 + 3990aa5 commit b9e6eee
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 296 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ jobs:
with:
path: "/home/runner/.cache/bazel"
key: ${{ hashFiles('.bazelrc', '.bazelversion', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel') }}
- name: Install buildifier
run: |
curl -f -s -L -o /usr/local/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/buildifier-linux-$(arch | sed 's/aarch64/arm64/;s/x86_64/amd64/')
chmod a+x /usr/local/bin/buildifier
- name: Run buildifier
run: buildifier -mode check -r .
- name: Build
# TODO(evan.flynn): uncomment once the rules are properly fixed
# run: bazel build //...
Expand Down
54 changes: 30 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ Here is a short list of major restrictions:

### Prerequisites

Bazel needs to be available. We recommend using [bazelisk](https://github.com/bazelbuild/bazelisk)
as a launch tool for Bazel.
Bazel needs to be available. It is recommended to use [bazelisk](https://github.com/bazelbuild/bazelisk)
as the launch tool for Bazel.

### Workspace setup

Create an empty folder and add the following files to it:
* `WORKSPACE` file:

```python
workspace(name = "my_first_bazel_ros_workspace") # choose your workspace name here
workspace(name = "my_first_bazel_ros_workspace") # choose a workspace name here
# load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

RULES_ROS_VERSION = "xxx" # TODO: where to find the right version
RULES_ROS_VERSION = "xxx" # TODO: where to find the right version
RUIES_ROS_SHA = "xxx"

# until we have a first release, please use this:
# until there is an initial release, use the following:
git_repository(
name = "rules_ros",
remote = "https://github.com/ApexAI/rules_ros.git",
Expand All @@ -67,7 +67,7 @@ Create an empty folder and add the following files to it:
#)

load("@rules_ros//repos/config:defs.bzl", "configure_ros2")
configure_ros2(distro = "humble") # currently only Humble is supported
configure_ros2(distro = "humble") # currently only Humble is supported

load("@ros2_config//:setup.bzl", "setup")
setup()
Expand All @@ -87,7 +87,7 @@ Create an empty folder and add the following files to it:

* `.bazelrc` file:

```bash
```shell
# enable incompatible Python init mode
build --incompatible_default_to_explicit_init_py

Expand All @@ -99,41 +99,44 @@ Create an empty folder and add the following files to it:
build --host_cxxopt="-std=c++17"
```

* `.bazelversion` file (in case you are using bazelisk):
* `.bazelversion` file (if `bazelisk` is being used):

```text
6.5.0
```
```text
6.5.0
```

### Run Bazel example

To **build** an example delivered in the `rules_ros` repository run, e.g.

```bash
```shell
bazel build @rules_ros//examples/hello_world
```
from anywhere within your workspace.

from anywhere within the workspace.

**Executing** the example can be done by calling

```bash
```shell
bazel run @rules_ros//examples/hello_world
```
Note that no sourcing is necessary. Bazel will take care of all the dependencies.

**Deploying** a package archive to an install folder can be done by

```bash
```shell
bazel run @rules_ros//examples:rules_ros_examples.install <install_folder>
```
Now we are back to working with ROS as usual. Source the package as usual:

```bash
Now the environment is ready to work with ROS as usual. Source the package as usual with:

```shell
source <install_folder>/setup.bash
```

and run an executable with

```bash
```shell
ros2 run hello_world hello_world
```

Expand All @@ -143,11 +146,12 @@ ros2 run hello_world hello_world

In this setup, a hermetic Python interpreter is included. The version is specified in
`thirdparty/python/repositories.bzl`. Python packages specified in
`thirdparty/python/requirements_lock.in` are available for use. If you need to add a package,
you must run
```console
`thirdparty/python/requirements_lock.in` are available for use. If a package needs to be added, run

```shell
bazel run @rules_ros//thirdparty/python:requirements_lock.update
```

to pin specific versions of the dependencies. In the future there will be a possibility to
inject a customization in the WORKSPACE file.

Expand All @@ -159,12 +163,14 @@ will be available as `@ros2.rclcpp` with all targets specified in the BUILD file
`repos/config/ros2.rclcpp.BUILD`. The precise location where the build files will be injected
into the `rclcpp` repo is specified in the index file `repos/config/bazel.repos`.

Therefore, if you need to depend on `rclcpp` in you code, you need to add `"@ros2.rclcpp//rclcpp"`
as a dependency in your `cc_binary` or `cc_library` target.
Therefore, if there is a dependency on `rclcpp`, `"@ros2.rclcpp//rclcpp"` needs to be added
as a dependency in the `cc_binary` or `cc_library` target.

The exact version of a ROS 2 repository is pinned in the file `repos/config/ros2_<distro>.lock`.
This file can be updated for the configured distro by running:
```console

```shell
bazel run @rules_ros//repos/config:repos_lock.update
```

In the future there will be a possibility to inject any customization in the WORKSPACE file.
2 changes: 1 addition & 1 deletion examples/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ ros_pkg(
pkg_name = "hello_world",
version = "1.0.0",
visibility = ["//visibility:public"],
)
)
10 changes: 5 additions & 5 deletions pkg/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
# limitations under the License.

load("@rules_pkg//pkg:tar.bzl", _pkg_tar = "pkg_tar")
load(
"@rules_ros//pkg/detail:ros_pkg.bzl",
_ros_pkg = "ros_pkg",
_ros_pkg_set = "ros_pkg_set",
)
load(
"@rules_ros//pkg/detail:ros_archive.bzl",
_ros_archive_install_command = "ros_archive_install_command",
_ros_archive_pkg_files = "ros_archive_pkg_files",
)
load(
"@rules_ros//pkg/detail:ros_pkg.bzl",
_ros_pkg = "ros_pkg",
_ros_pkg_set = "ros_pkg_set",
)

ros_pkg = _ros_pkg
ros_pkg_set = _ros_pkg_set
Expand Down
2 changes: 1 addition & 1 deletion pkg/detail/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_python//python:defs.bzl", "py_binary")
load("@python_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_binary")

exports_files(["templates/install.template"])

Expand Down
2 changes: 1 addition & 1 deletion pkg/detail/ros_archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
load("@rules_ros//pkg:providers.bzl", _RosPkgInfo = "RosPkgInfo")
load(
":utils.bzl",
_add_filegroup = "add_filegroup",
Expand All @@ -21,7 +22,6 @@ load(
_create_ros_pkg_set_info = "create_ros_pkg_set_info",
_unique_pkg_names_or_fail = "unique_pkg_names_or_fail",
)
load("@rules_ros//pkg:providers.bzl", _RosPkgInfo = "RosPkgInfo")

def _build_setup_bash(ctx):
output = ctx.actions.declare_file("/".join([ctx.label.name, "setup.bash"]))
Expand Down
2 changes: 1 addition & 1 deletion pkg/detail/ros_pkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
load("@rules_python//python:packaging.bzl", "PyWheelInfo")
load("@rules_ros//pkg:providers.bzl", _RosPkgInfo = "RosPkgInfo")
load(
":utils.bzl",
_add_filegroup = "add_filegroup",
Expand All @@ -23,7 +24,6 @@ load(
_create_ros_pkg_set_info = "create_ros_pkg_set_info",
_unique_pkg_names_or_fail = "unique_pkg_names_or_fail",
)
load("@rules_ros//pkg:providers.bzl", _RosPkgInfo = "RosPkgInfo")

RESOURCE_PATH_PREFIX = "share/bazel-bin/"
LIBRARY_PATH = "lib/{pkg_name}/"
Expand Down
2 changes: 1 addition & 1 deletion repos/config/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_ros//repos/config/detail:ros2_config.bzl", "ros2_config")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_ros//repos/config:distros.bzl", "DISTROS")
load("@rules_ros//repos/config/detail:ros2_config.bzl", "ros2_config")

def _configure_ros2(*, name, distro_src, repos_index_overlays):
distro_src_wo_setup_file = {k: v for k, v in distro_src.items() if k != "setup_file"}
Expand Down
14 changes: 10 additions & 4 deletions repos/config/detail/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
name = "repos_lock.update",
srcs = ["lock_repos.py", "generate_ros2_config.py"],
main = "lock_repos.py",
data = [REPOS_INDEX_FILE, REPOS_SETUP_FILE] + REPOS_OVERLAY_FILES,
srcs = [
"generate_ros2_config.py",
"lock_repos.py",
],
args = [
"$(execpath {})".format(REPOS_INDEX_FILE),
"$(execpath {})".format(REPOS_SETUP_FILE),
] + ["$(execpath {})".format(f) for f in REPOS_OVERLAY_FILES],
deps = [requirement("pyyaml")],
data = [
REPOS_INDEX_FILE,
REPOS_SETUP_FILE,
] + REPOS_OVERLAY_FILES,
main = "lock_repos.py",
visibility = ["//visibility:public"],
deps = [requirement("pyyaml")],
)
10 changes: 7 additions & 3 deletions repos/config/detail/generate_ros2_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ def setup():

def print_setup(repos, output_file):
print(HEADER, file=output_file)
printed_first_load = False
for repo, spec in repos.items():
if spec.get("bazel") is not None:
print(build_load_command(repo, spec), file=output_file)
if printed_first_load:
output_file.write("\n")
output_file.write(build_load_command(repo, spec))
printed_first_load = True


def build_load_command(repo, spec):
Expand All @@ -45,7 +49,7 @@ def build_load_command(repo, spec):
"local": build_local_load_command,
}
if spec.get('type') not in builder.keys():
return f"""
return f"""\
print("WARNING: Unknown repo type {spec.get('type')} for repo @{repo.replace('/', '.')}")
"""
return builder[spec.get('type')](repo, spec)
Expand All @@ -54,7 +58,7 @@ def build_load_command(repo, spec):
def build_build_files_attr(build_files):
if not build_files:
return ""
content = '\n'.join(f" '{k}': '{v}'," for k,v in build_files.items())
content = '\n'.join(f' "{k}": "{v}",' for k,v in build_files.items())
return f"""build_files = {{
{content}
}},"""
Expand Down
4 changes: 2 additions & 2 deletions repos/config/detail/lock_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def extract_archive_root_folder(path, origin):

def fetch_git_details(url, version, **kwargs):
cwd = os.getcwd()
max_retries = 2
max_retries = 4
for i in range(max_retries + 1):
with tempfile.TemporaryDirectory() as tempdir:
try:
Expand All @@ -113,7 +113,7 @@ def fetch_git_details(url, version, **kwargs):
"--branch", version, "--bare", "-q"],
capture_output = True,
encoding='utf8',
timeout=20
timeout=5
)
if result.returncode != 0:
if max_retries == i:
Expand Down
2 changes: 1 addition & 1 deletion repos/config/detail/ros2_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _archive_attrs = {

def _ros2_config_impl(ctx):
ctx.file("repos_index_file.bzl", content = "REPOS_INDEX_FILE = '{}'".format(ctx.attr.repos_index))
ctx.file("repos_overlay_files.bzl", content = "REPOS_OVERLAY_FILES = {}".format(["{}".format(l) for l in ctx.attr.repos_index_overlays]))
ctx.file("repos_overlay_files.bzl", content = "REPOS_OVERLAY_FILES = {}".format(["{}".format(i) for i in ctx.attr.repos_index_overlays]))
ctx.file("repos_setup_file.bzl", content = "REPOS_SETUP_FILE = '{}'".format(ctx.attr.setup_file))
ctx.symlink(ctx.attr.setup_file, "setup.bzl")
ctx.file("WORKSPACE", content = "workspace(name = {})".format(ctx.name), executable = False)
Expand Down
4 changes: 2 additions & 2 deletions repos/config/distros.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
ROS_PROJECT = "https://github.com/ros2/ros2.git"

_VERSIONS = [
("humble", "20230925", "57495eab51338591a0117b6763827607808e26344d134d6666ded66e479bdf8b"),
("iron", "20230912", "fd40b4d80eb9c27f57b2b59ad8a947cd5f7f34fc67c8df1d7cc0a659127fc9f7"),
("foxy", "20230620", "2cf7e3f9c5b01b7de2ec3c80097837758f3554e4f5c99a2aeca2bd7f4eb0bc1f"),
("humble", "20230925", "57495eab51338591a0117b6763827607808e26344d134d6666ded66e479bdf8b"),
("galactic", "20221209", "fd251be0e1d16c1f943a8f083dce7b75c60fc2095404c5834209a68846be48c7"),
("foxy", "20230620", "2cf7e3f9c5b01b7de2ec3c80097837758f3554e4f5c99a2aeca2bd7f4eb0bc1f"),
("eloquent", "2020-1212", "76f4b08bc4ecc6b126d2bdf5e8b86fa3d4b6d5101c122f7d0fd973aa77ef819a"),
("dashing", "20210610", "f0e00b81e93f764bef7591b0d9e3b89d73660696764663f18f926cd9795028c9"),
]
Expand Down
Loading

0 comments on commit b9e6eee

Please sign in to comment.