Skip to content

Commit

Permalink
Add support for Bazel
Browse files Browse the repository at this point in the history
Add examples/bazel with a sample downstream use, licensed as MIT-0.

Other fixes:
- Adjust lcm_eventlog.py test to not require copying the example log.
- Fix a small typo in the go example.
  • Loading branch information
jwnimmer-tri committed Feb 16, 2025
1 parent 29044b6 commit a3808b3
Show file tree
Hide file tree
Showing 51 changed files with 1,618 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# When traversing //... don't incorporate the example.
examples/bazel
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.0.1
22 changes: 22 additions & 0 deletions .github/workflows/test_bazel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: test_bazel

on: [push, pull_request]

jobs:
bazel:
strategy:
matrix:
os: [ubuntu-24.04, macos-14]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: bazel-contrib/setup-bazel@0.13.0
with:
bazelisk-cache: true
disk-cache: ${{ github.workflow }}
repository-cache: true
- name: Test Main
run : 'bazel test //...'
- name: Test Example
working-directory: examples/bazel
run : 'bazel test //...'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
build*/
__pycache__
docs/_build/
bazel-*
MODULE.bazel.lock

.vscode/
.idea/
Expand Down
16 changes: 16 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@rules_license//rules:license.bzl", "license")

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

exports_files([
"COPYING",
])

license(
name = "license",
license_kinds = ["@rules_license//licenses/spdx:LGPL-2.1"],
license_text = "COPYING",
)
39 changes: 39 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file specifies a Bazel workspace (see https://bazel.build/).
#
# For now, LCM's Bazel build only supports C, C++, Java, and Python.
# We invite users to submit pull requests for other languages if needed.

module(
name = "lcm",
bazel_compatibility = [">=8.0.1"],
)

# Load bazel rules.

bazel_dep(name = "apple_support", version = "1.17.1", repo_name = "build_bazel_apple_support")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_java", version = "8.6.1")
bazel_dep(name = "rules_jvm_external", version = "6.6")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_python", version = "0.40.0")

# Load runtime dependencies.

bazel_dep(name = "pcre2", version = "10.45")
bazel_dep(name = "glib", version = "2.82.2.bcr.1")

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "lcm_maven",
artifacts = [
"junit:junit:4.11",
"net.sf.jchart2d:jchart2d:3.3.2",
],
)
use_repo(maven, "lcm_maven")

# Load dev dependencies.

bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True)
13 changes: 13 additions & 0 deletions docs/content/build-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,16 @@ In addition, `pkgconfig` can be configured to find lcm.pc:
```shell
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$LCM_LIBRARY_DIR/pkgconfig
```

## Bazel

LCM also supports [Bazel](https://bazel.build/) for a subset of languages
(C, C++, Java, Python). The Bazel build only provides libraries and command
line tools; it doesn't support building wheels or documentation.

The Bazel build uses very few system packages, so most of the text above about
required packages does not apply. The only required tool is a C/C++ compiler,
e.g., `apt install build-essential` on Ubuntu and Debian. If Java will
be used, a local JDK is also recommended, e.g., `apt install default-jdk`.
See the [example](https://github.com/lcm-proj/lcm/tree/master/examples/bazel)
for details.
3 changes: 3 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
/cpp/lcm_log_writer/pronto/
/java/temperature_demo_java/lcmtypes/
!/go/listener/

# Don't ignore the bazel/exlcm source file symlinks.
!bazel/exlcm/
1 change: 1 addition & 0 deletions examples/bazel/.bazelversion
4 changes: 4 additions & 0 deletions examples/bazel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: MIT-0

bazel-*
MODULE.bazel.lock
132 changes: 132 additions & 0 deletions examples/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# SPDX-License-Identifier: MIT-0
#
# Using LGPL-2.1 for examples leads to unnecessary hassle for users, so this
# example is licensed under the https://spdx.org/licenses/MIT-0.html license,
# except for the exlcm/*.lcm message definitions.

load("@lcm//lcm-bazel:lcm_c_library_srcs.bzl", "lcm_c_library_srcs")
load("@lcm//lcm-bazel:lcm_cc_library_srcs.bzl", "lcm_cc_library_srcs")
load("@lcm//lcm-bazel:lcm_java_library_srcs.bzl", "lcm_java_library_srcs")
load("@lcm//lcm-bazel:lcm_library.bzl", "lcm_library")
load("@lcm//lcm-bazel:lcm_py_library_srcs.bzl", "lcm_py_library_srcs")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load("@rules_java//java:java_binary.bzl", "java_binary")
load("@rules_java//java:java_library.bzl", "java_library")
load("@rules_license//rules:license.bzl", "license")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")

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

exports_files([
"LICENSE.TXT",
])

license(
name = "license",
license_kinds = ["@rules_license//licenses/spdx:MIT-0"],
license_text = "LICENSE.TXT",
)

# Run lcm-gen on 'exlcm' for C, C++, Java, and Python.

lcm_library(
name = "exlcm",
srcs = glob(["exlcm/*.lcm"]),
lcm_package = "exlcm",
)

lcm_c_library_srcs(
name = "exlcm_c_srcs",
src = ":exlcm",
)

cc_library(
name = "exlcm_c",
srcs = [":exlcm_c_srcs"],
deps = ["@lcm//lcm:lcm-static"],
)

lcm_cc_library_srcs(
name = "exlcm_cc_srcs",
src = ":exlcm",
)

cc_library(
name = "exlcm_cc",
srcs = [":exlcm_cc_srcs"],
deps = ["@lcm//lcm:lcm-coretypes"],
)

lcm_java_library_srcs(
name = "exlcm_java_srcs",
src = ":exlcm",
)

java_library(
name = "exlcm_java",
srcs = [":exlcm_java_srcs"],
deps = ["@lcm//lcm-java"],
)

lcm_py_library_srcs(
name = "exlcm_py_srcs",
src = ":exlcm",
)

py_library(
name = "exlcm_py",
srcs = [":exlcm_py_srcs"],
imports = ["."],
)

# Perform some basic testing on the generated messages.

cc_test(
name = "test_c",
srcs = ["test_c.c"],
deps = [
":exlcm_c",
"@lcm//lcm:lcm-static",
],
)

cc_test(
name = "test_cc",
srcs = ["test_cc.cc"],
deps = [
":exlcm_cc",
"@lcm//lcm:lcm-static",
],
)

py_test(
name = "test_py",
srcs = ["test_py.py"],
deps = [
":exlcm_py",
"@lcm//lcm-python",
],
)

# Create aliases for the Java tools, adding our messages to the classpath.

java_binary(
name = "lcm-spy",
main_class = "lcm.spy.Spy",
runtime_deps = [
":exlcm_java",
],
)

java_binary(
name = "lcm-logplayer-gui",
main_class = "lcm.logging.LogPlayer",
runtime_deps = [
":exlcm_java",
],
)
12 changes: 12 additions & 0 deletions examples/bazel/LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions examples/bazel/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: MIT-0

bazel_dep(name = "lcm", version = "1.5.1")

# This example uses lcm from our same git checkout. In most cases, you'll want
# to remove this override in your own projects.
local_path_override(
module_name = "lcm",
path = "../..",
)

bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_java", version = "8.6.1")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_python", version = "0.40.0")
1 change: 1 addition & 0 deletions examples/bazel/exlcm/example_list_t.lcm
1 change: 1 addition & 0 deletions examples/bazel/exlcm/example_t.lcm
1 change: 1 addition & 0 deletions examples/bazel/exlcm/exampleconst_t.lcm
1 change: 1 addition & 0 deletions examples/bazel/exlcm/multidim_array_t.lcm
1 change: 1 addition & 0 deletions examples/bazel/exlcm/node_t.lcm
32 changes: 32 additions & 0 deletions examples/bazel/test_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: MIT-0 */

#include <lcm/lcm.h>
#include <stdio.h>

#include "exlcm_example_t.h"

int main()
{
exlcm_example_t message = {};
message.name = "";
const int size = exlcm_example_t_encoded_size(&message);
if (size != 82) {
fprintf(stderr, "size = %d\n", size);
fflush(stderr);
return 1;
}

lcm_t *memq = lcm_create("memq://");
int handled = -1;
if (memq != NULL) {
handled = lcm_handle_timeout(memq, 0);
lcm_destroy(memq);
}
if (handled != 0) {
fprintf(stderr, "memq failure\n");
fflush(stderr);
return 1;
}

return 0;
}
24 changes: 24 additions & 0 deletions examples/bazel/test_cc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* SPDX-License-Identifier: MIT-0 */

#include <iostream>

#include <lcm/lcm-cpp.hpp>

#include "exlcm/example_t.hpp"

int main() {
exlcm::example_t message = {};
const auto size = message.getEncodedSize();
if (size != 82) {
std::cerr << "size = " << size << "\n";
return 1;
}

lcm::LCM memq("memq://");
if (memq.handleTimeout(0) != 0) {
std::cerr << "memq failure\n";
return 1;
}

return 0;
}
26 changes: 26 additions & 0 deletions examples/bazel/test_py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-License-Identifier: MIT-0

import unittest

import lcm

from exlcm import example_t


class TestPy(unittest.TestCase):
def test_size(self):
message = example_t()
size = len(message.encode())
self.assertEqual(size, 82)

def test_memq(self):
memq = lcm.LCM("memq://")
self.assertEqual(memq.handle_timeout(0), 0)


def main():
unittest.main()


assert __name__ == '__main__'
main()
2 changes: 1 addition & 1 deletion examples/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package exlcm
//go:generate lcm-gen -g ../types/exampleconst_t.lcm
//go:generate lcm-gen -g ../types/example_list_t.lcm
//go:generate lcm-gen -g ../types/example_t.lcm
//go:generate lcm-gen -g ../types/multidim_array_t.lcm
//go:generate lcm-gen -g ../types/muldim_array_t.lcm
//go:generate lcm-gen -g ../types/node_t.lcm
File renamed without changes.
4 changes: 4 additions & 0 deletions lcm-bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package(
default_applicable_licenses = ["//:license"],
default_visibility = ["//visibility:private"],
)
Loading

0 comments on commit a3808b3

Please sign in to comment.