Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel build #2225

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# don't use bzlmod yet.
common --enable_workspace=true
common --enable_bzlmod=false

# use c++20 by default
build --cxxopt=-std=c++20

# remove this when https://github.com/abseil/abseil-cpp/issues/740 is resolved
build --copt=-Iexternal/abseil-cpp

# Due to the repetitive directory names (e.g., `mujoco/mjx/mujoco/mjx`), `__init__.py` should not
# be automatically created. Otherwise, importing with `import mujoco.mjx` will reference
# `mujoco/mjx` instead of `mujoco/mjx/mujoco/mjx`.
build --incompatible_default_to_explicit_init_py

# Compiler configuration.
# Note that the current configuration relies on system-installed toolchains, which may cause
# non-hermetic behavior in builds. This could be improved by defining a `cc_toolchain` with
# self-contained toolchains.
# See https://docs.bazel.build/versions/1.2.0/tutorial/cc-toolchain-config.html#configuring-the-c-toolchain

# clang by default
build --platforms=@mujoco//tools:linux_clang_x86_64
build --action_env=CC=clang

# gcc
build:linux_gcc_x86_64 --platforms=@mujoco//tools:linux_gcc_x86_64
build:linux_gcc_x86_64 --action_env=CC=gcc

# ASan Build
build:asan --build_tests_only
build:asan --features=asan
# This flag effectively tells Bazel to avoid using certain hardware-dependent features that may
# cause ASan or TSan to fail unexpectedly or produce inaccurate results.
build:asan --incompatible_use_host_features

# LSan is run with ASan by default
build:asan --test_tag_filters=-no_asan,-no_lsan
build:asan --test_lang_filters=-sh
# Typical slowdown introduced by AddressSanitizer is 2x.
# See https://clang.llvm.org/docs/AddressSanitizer.html
build:asan --test_timeout=150,750,2250,9000

# TSan Build
build:tsan --build_tests_only
build:tsan --features=tsan
# This flag effectively tells Bazel to avoid using certain hardware-dependent features that may
# cause ASan or TSan to fail unexpectedly or produce inaccurate results.
build:tsan --incompatible_use_host_features

# We assume anything that didn't want asan doesn't want tsan
build:tsan --test_tag_filters=-no_asan,-no_tsan
build:tsan --test_lang_filters=-sh
# Typical slowdown introduced by ThreadSanitizer is about 5x-15x
# See https://clang.llvm.org/docs/ThreadSanitizer.html
build:tsan --test_timeout=150,750,2250,9000

try-import %workspace%/user.bazelrc
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ MUJOCO_LOG.TXT

# Clang cache
.cache/

# Bazel build artifacts (symlinks)
/bazel-*
173 changes: 173 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
load("@mujoco//tools:symlink_files.bzl", "symlink_files")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@mujoco//tools:def.bzl", "mj_cc_library")

package(default_visibility = ["@mujoco//:__subpackages__"])

filegroup(
name = "license_filegroup",
srcs = [
"LICENSE",
],
tags = ["license_filegroup"],
)

mj_cc_library(
name = "mujoco_hdrs",
hdrs = [":mujoco_headers"],
includes = [
"include",
],
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
)

mj_cc_library(
name = "_core",
target_compatible_with = ["@platforms//os:linux"],
deps = [
"//src/engine",
"//src/thread:thread_pool",
"//src/xml:xml_api",
],
)

mj_cc_library(
name = "_graphics_native",
target_compatible_with = ["@platforms//os:linux"],
deps = [
"//src/render",
"//src/ui:ui_main",
],
)

# The headers are needed to generate ctypes Python bindings.
filegroup(
name = "mujoco_headers",
srcs = [
"include/mujoco/mjdata.h",
"include/mujoco/mjexport.h",
"include/mujoco/mjmacro.h",
"include/mujoco/mjmodel.h",
"include/mujoco/mjplugin.h",
"include/mujoco/mjrender.h",
"include/mujoco/mjsan.h",
"include/mujoco/mjspec.h",
"include/mujoco/mjthread.h",
"include/mujoco/mjtnum.h",
"include/mujoco/mjui.h",
"include/mujoco/mjvisualize.h",
"include/mujoco/mjxmacro.h",
"include/mujoco/mujoco.h",
],
)

mj_cc_library(
name = "mujoco",
hdrs = [":mujoco_headers"],
data = [
":license_filegroup",
],
includes = [
"include",
],
target_compatible_with = ["@platforms//os:linux"],
deps = [
":_core",
":_graphics_native",
],
)

cc_shared_library(
name = "mujoco_shared",
shared_lib_name = "libmujoco.so",
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
deps = [
":mujoco",
],
)

alias(
name = "simulate",
actual = "//simulate:main",
visibility = ["//visibility:public"],
)

alias(
name = "mujoco-py",
actual = "//python/mujoco:mujoco-py",
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
)

alias(
name = "mujoco-mjx",
actual = "//mjx/mujoco/mjx:mjx",
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
)

# Symlink data for the test. Note that Bazel tests look for data in the `TEST.runfiles/mujoco`
# directory, so we create proper symlinks for the files instead of altering paths within the test.
symlink_files(
name = "model_data",
srcs = ["//model"],
)

# The reason we have `test_data` and `test_data_stripped` is that some tests (e.g., `spec_test.py`)
# reads `test/testdata/model.xml` while `pipeline_test.cc` reads `testdata/model.xml`.
symlink_files(
name = "test_data",
srcs = ["//test/testdata:model"],
)

symlink_files(
name = "test_data_stripped",
srcs = ["//test/testdata:model"],
strip_prefix = "test/",
)

symlink_files(
name = "test_engine_data",
srcs = ["//test/engine/testdata:model"],
)

symlink_files(
name = "test_engine_data_stripped",
srcs = ["//test/engine/testdata:model"],
strip_prefix = "test/",
)

symlink_files(
name = "test_plugin_data",
srcs = ["//test/plugin/sensor/testdata:model"],
strip_prefix = "test/",
)

symlink_files(
name = "test_user_data",
srcs = ["//test/user/testdata:model"],
strip_prefix = "test/",
)

symlink_files(
name = "test_xml_data",
srcs = ["//test/xml/testdata:model"],
strip_prefix = "test/",
)

symlink_files(
name = "test_benchmark_data",
srcs = ["//test/benchmark/testdata:model"],
)

symlink_files(
name = "test_benchmark_data_stripped",
srcs = ["//test/benchmark/testdata:model"],
strip_prefix = "test/",
)

buildifier(
name = "buildifier",
)
94 changes: 94 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
workspace(name = "mujoco")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is using the soon-to-be deprecated WORKSPACE system. It might be worth migrating this to use the new Bzlmod setup. I'll take a look at this if I have some extra time in the upcoming days. No guarantees though.

Copy link
Author

Choose a reason for hiding this comment

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

Good point. I noticed that some dependencies are already in Bazel Central Registry. Let me know how it goes - I’m happy to explore this as well.


"""
Download dependencies. Note that the functions below could potentially be called from a larger
Bazel project that fetches MuJoCo.
"""

load("@mujoco//tools/workspace:default_repositories.bzl", "add_default_repositories")

add_default_repositories()

load("@mujoco//tools/workspace:default_dependencies.bzl", "add_default_dependencies")

add_default_dependencies()

"""
Configure a Python toolchain and Buildifier (a formatting tool for BUILD.bazel files). Note that the functions below are in a local scope and won’t be called from a larger Bazel project that fetches MuJoCo.
"""

# Python
load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python_3_11",
python_version = "3.11",
)

load("@rules_python//python:pip.bzl", "pip_parse")
load("@python_3_11//:defs.bzl", "interpreter")

pip_parse(
name = "pip_deps",
python_interpreter_target = interpreter,
requirements_lock = "@mujoco//python:pip_requirements.txt",
)

load("@pip_deps//:requirements.bzl", install_pip_deps = "install_deps")

install_pip_deps()

# Buildifier to format BUILD.bazel files
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

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()

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

go_register_toolchains(version = "1.20.3")

http_archive(
name = "bazel_gazelle",
sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

http_archive(
name = "com_google_protobuf",
sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568",
strip_prefix = "protobuf-3.19.4",
urls = [
"https://github.com/protocolbuffers/protobuf/archive/v3.19.4.tar.gz",
],
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

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",
],
)
Empty file.
18 changes: 18 additions & 0 deletions devtools/bbcp/builddefs/build_defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#Copied from https://github.com/taylor-bsg/xls/blob/7afd772753d95078cdd513566de438d8fcc2cfbd/xls/build_rules/xls_oss_config_rules.bzl#L25

def generated_file(
name = None,
wrapped_target = None,
tags = None,
testonly = None):
"""The function is a placeholder for generated_file.

The function is intended to be empty.

Args:
name: Optional name of the marker rule created by this macro.
wrapped_target: The target to wrap.
tags: A list of tags to set on the artifacts.
testonly: Optional standard testonly attribute.
"""
pass
Empty file.
Loading