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

Add Bazel tooling support #248

Merged
merged 4 commits into from
Jul 12, 2024
Merged
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
14 changes: 14 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
common --enable_bzlmod
common --registry=https://bcr.bazel.build
try-import %workspace%/.bazelrc.user

# bazel from apt needs access to this cacerts location
startup --host_jvm_args=-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts

# So we can support private dependencies
build --experimental_cc_implementation_deps

build --action_env=BAZEL_CXXOPTS="-std=c++17"
cassava marked this conversation as resolved.
Show resolved Hide resolved
build --strip=never
build --copt='-O2'
build --conlyopt='-std=c11'
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ callgrind.out.*
# Miscellaneous
*.log
/.gtm/

# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
/bazel-*
MODULE.bazel.lock
.bazelrc.user
external/

cassava marked this conversation as resolved.
Show resolved Hide resolved
# Directories for the Bazel IntelliJ plugin containing the generated
# IntelliJ project files and plugin configuration. Seperate directories are
# for the IntelliJ, Android Studio and CLion versions of the plugin.
/.ijwb/
/.aswb/
/.clwb/
8 changes: 8 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//:plugin.bzl", "CLOE_PLUGINS")
cassava marked this conversation as resolved.
Show resolved Hide resolved

sh_binary(
name = "cloe_shell",
srcs = ["cloe_shell.sh"],
args = ["$(location //engine:cloe-engine)"] + ["$(location {})".format(p) for p in CLOE_PLUGINS],
data = ["//engine:cloe-engine"] + CLOE_PLUGINS + glob(["tests/*"]),
)
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_subdirectory(runtime)
add_subdirectory(models)
add_subdirectory(osi)
add_subdirectory(oak)
add_subdirectory(stack)
add_subdirectory(engine)
add_subdirectory(plugins)

Expand Down
31 changes: 31 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module(name = "cloe", version = "0.25.0")

# Build dependencies:
bazel_dep(name = "cmake_configure_file", version = "0.1.0")
bazel_dep(name = "rules_cc", version = "0.0.9")

# Test dependencies:
bazel_dep(name = "googletest", version = "1.14.0", repo_name = "gtest")

# Library dependencies:
bazel_dep(name = "boost", version = "1.83.0.bcr.1")
cassava marked this conversation as resolved.
Show resolved Hide resolved
bazel_dep(name = "cli11", version = "2.3.2")
bazel_dep(name = "eigen", version = "3.4.0.bcr.1")
bazel_dep(name = "fmt", version = "10.2.1")
bazel_dep(name = "incbin", version = "20211106.0")
bazel_dep(name = "inja", version = "3.4.0")
bazel_dep(name = "lua", version = "5.4.6")
bazel_dep(name = "nlohmann_json", version = "3.11.3")
bazel_dep(name = "oatpp", version = "1.3.0")
bazel_dep(name = "sol", version = "3.3.1")
bazel_dep(name = "spdlog", version = "1.13.0")
bazel_dep(name = "open-simulation-interface", version = "3.5.0")
bazel_dep(name = "protobuf", version = "26.0")

# Tooling dependencies:
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
commit = "1e08f8e0507b6b6b1f4416a9a22cf5c28beaba93", # 2024-07-04
)
4 changes: 3 additions & 1 deletion Makefile.all
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ALL_PKGS := \
fable \
runtime \
models \
stack \
osi \
oak \
engine \
Expand Down Expand Up @@ -77,7 +78,8 @@ runtime: fable
models: runtime
osi: runtime models
oak: runtime
engine: models oak
stack: runtime models
engine: models oak stack
$(PLUGIN_PKGS): runtime models
plugins/esmini: osi

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,37 @@ When using `make` to build the project, add this to the command line:

CONAN_OPTIONS="-c tools.build:skip_test=1"

## Building with Bazel

Bazel tooling is currently experimental, and is meant to support users
who already are using Bazel in their projects and are not afraid of
providing their own registries and modules for Cloe's dependencies.

(That allows us to not have to vendor massive amounts of Bazel modules
in this repository, of which Boost is the main problematic point.
Once Boost makes it into the Bazel Central Registry, it may be worthwhile
to vendor the remaining libraries in this repository.)

You will need to create a `.bazelrc.user` file in the repository root
with the following contents:

common --registry=file:///path/to/your/bazel/registry

This file is ignored by Git to prevent you from exposing your secrets.
The registry should contain the following modules:

boost
cli11
esmini
incbin
inja
luajit (optional)
oatpp
open-simulation-interface
sol
cassava marked this conversation as resolved.
Show resolved Hide resolved

The rest of the dependencies are taken from the Bazel Central Registry.
See `MODULE.bazel` for the full list.

### Building Docker Images

Expand Down
Empty file added WORKSPACE.bazel
Empty file.
18 changes: 18 additions & 0 deletions cloe_shell.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
cassava marked this conversation as resolved.
Show resolved Hide resolved

export CLOE_LOG_LEVEL=debug
export CLOE_STRICT_MODE=1
export CLOE_ROOT="$(pwd)"
cassava marked this conversation as resolved.
Show resolved Hide resolved
export CLOE_ENGINE="${CLOE_ROOT}/$1"
export CLOE_LUA_PATH="${CLOE_ROOT}/engine/lua"
export PATH="$(dirname "$CLOE_ENGINE"):$PATH"

# Set plugin paths
shift 1
while [[ $# -ne 0 ]]; do
CLOE_PLUGIN_PATH="${CLOE_ROOT}/$(dirname "$1"):$CLOE_PLUGIN_PATH"
shift 1
done
export CLOE_PLUGIN_PATH

exec $SHELL
65 changes: 65 additions & 0 deletions engine/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
load("//:version.bzl", "PROJECT_VERSION")
cassava marked this conversation as resolved.
Show resolved Hide resolved

cc_library(
cassava marked this conversation as resolved.
Show resolved Hide resolved
name = "enginelib",
hdrs = glob(["src/**/*.hpp"], exclude=["src/main*.hpp"]),
srcs = glob(
["src/**/*.cpp"],
exclude=[
"src/main*.cpp",
"src/**/*_test.cpp",
"src/server_mock.cpp",
]
),
data = glob(["lua/**"]),
includes = ["src"],
additional_compiler_inputs = glob(["webui/**"]),
deps = [
"//engine/vendor/lrdb",
"//engine/vendor/linenoise",
cassava marked this conversation as resolved.
Show resolved Hide resolved
"//stack",
"//fable",
"//runtime",
"//models",
"//oak",
"@boost//:algorithm",
"@boost//:conversion",
"@boost//:optional",
"@boost//:process",
"@boost//:uuid",
"@sol",
],
defines = [
"CLOE_ENGINE_VERSION=\\\"{}\\\"".format(PROJECT_VERSION),
"CLOE_ENGINE_TIMESTAMP=\\\"unknown\\\"",
"CLOE_ENGINE_WITH_SERVER=1",
"CLOE_ENGINE_WITH_LRDB=1",
"PROJECT_SOURCE_DIR=\\\"engine\\\"",
],
linkopts = [
"-lpthread",
],
)

cc_test(
name = "engine_test",
srcs = glob(["src/**/*_test.cpp"]),
defines = [
"CLOE_LUA_PATH=\\\"" + package_name() + "/lua\\\"",
],
deps = [
":enginelib",
"@gtest//:gtest_main",
],
)

cc_binary(
name = "cloe-engine",
srcs = glob(["src/main*.hpp", "src/main*.cpp"]),
includes = ["src"],
deps = [
":enginelib",
"@cli11",
],
visibility = ["//visibility:public"],
)
72 changes: 8 additions & 64 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if(CLOE_FIND_PACKAGES)
find_package(fable REQUIRED QUIET)
find_package(cloe-runtime REQUIRED QUIET)
find_package(cloe-models REQUIRED QUIET)
find_package(cloe-stack REQUIRED QUIET)
endif()
find_package(Boost REQUIRED QUIET)
find_package(CLI11 REQUIRED QUIET)
Expand All @@ -22,67 +23,6 @@ string(TIMESTAMP CLOE_ENGINE_TIMESTAMP "%Y-%m-%d")
set(CLOE_ENGINE_VERSION ${CLOE_PROJECT_VERSION})
set(PROJECT_GIT_REF "unknown")

# Library libstack ---------------------------------------------------
message(STATUS "Building cloe-stacklib library.")
add_library(cloe-stacklib STATIC
src/config.hpp
src/stack.hpp
src/stack.cpp
src/stack_factory.hpp
src/stack_factory.cpp
src/plugin.hpp
src/plugin.cpp

# Built-in plugins:
src/plugins/nop_controller.cpp
src/plugins/nop_controller.hpp
src/plugins/nop_simulator.cpp
src/plugins/nop_simulator.hpp
)
add_library(cloe::stacklib ALIAS cloe-stacklib)
set_target_properties(cloe-stacklib PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
OUTPUT_NAME stack
)
target_include_directories(cloe-stacklib
PRIVATE
src
)
target_link_libraries(cloe-stacklib
PUBLIC
cloe::runtime
cloe::models
fable::fable
Boost::headers
Threads::Threads
${CMAKE_DL_LIBS}
)

include(CTest)
if(BUILD_TESTING)
find_package(GTest REQUIRED QUIET)
include(GoogleTest)

message(STATUS "Building test-stacklib executable.")
add_executable(test-stacklib
src/stack_test.cpp
src/stack_component_test.cpp
)
set_target_properties(test-stacklib PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(test-stacklib
GTest::gtest
GTest::gtest_main
Boost::boost
cloe::models
cloe::stacklib
)
gtest_add_tests(TARGET test-stacklib)
endif()

# Library libengine ----------------------------------------------
message(STATUS "Building cloe-enginelib library.")
add_library(cloe-enginelib STATIC
Expand Down Expand Up @@ -157,7 +97,7 @@ target_include_directories(cloe-enginelib
)
target_link_libraries(cloe-enginelib
PUBLIC
cloe::stacklib
cloe::stack
cloe::models
cloe::runtime
fable::fable
Expand Down Expand Up @@ -189,8 +129,11 @@ else()
target_compile_definitions(cloe-enginelib PUBLIC CLOE_ENGINE_WITH_LRDB=0)
endif()

include(CTest)
if(BUILD_TESTING)
message(STATUS "Building test-enginelib executable.")
find_package(GTest REQUIRED QUIET)
include(GoogleTest)
add_executable(test-enginelib
src/lua_stack_test.cpp
src/lua_setup_test.cpp
Expand All @@ -204,11 +147,12 @@ if(BUILD_TESTING)
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(test-enginelib
PRIVATE
GTest::gtest
GTest::gtest_main
Boost::boost
cloe::models
cloe::stacklib
cloe::stack
cloe::enginelib
)
gtest_add_tests(TARGET test-enginelib)
Expand Down Expand Up @@ -245,7 +189,7 @@ target_include_directories(cloe-engine
)
target_link_libraries(cloe-engine
PRIVATE
cloe::stacklib
cloe::stack
cloe::enginelib
CLI11::CLI11
linenoise::linenoise
Expand Down
1 change: 1 addition & 0 deletions engine/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def set_version(self):
def requirements(self):
self.requires(f"cloe-runtime/{self.version}@cloe/develop")
self.requires(f"cloe-models/{self.version}@cloe/develop")
self.requires(f"cloe-stack/{self.version}@cloe/develop")
self.requires("cli11/2.3.2", private=True)
self.requires("sol2/3.3.1")
if self.options.server:
Expand Down
2 changes: 1 addition & 1 deletion engine/src/lua_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@

#include <cloe/utility/std_extensions.hpp> // for split_string

#include <cloe/stack.hpp>
#include <fable/utility/sol.hpp> // for Json(sol::object)
#include <fable/utility/string.hpp> // for join_vector

#include "error_handler.hpp" // for format_cloe_error
#include "lua_api.hpp"
#include "stack.hpp"
#include "utility/command.hpp" // for CommandExecuter, CommandResult

// This variable is set from CMakeLists.txt, but in case it isn't,
Expand Down
2 changes: 1 addition & 1 deletion engine/src/lua_setup_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/

#include <fable/utility/sol.hpp>
#include <cloe/stack.hpp>

#include "lua_api.hpp"
#include "lua_setup.hpp"
#include "stack.hpp"

namespace cloe {

Expand Down
5 changes: 3 additions & 2 deletions engine/src/lua_setup_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#include <fstream>
#include <string_view>

#include <cloe/stack.hpp> // for Stack
#include <sol/state.hpp>

#include "lua_api.hpp" // for lua_safe_script_file
#include "lua_setup.hpp" // for setup_lua
#include "stack.hpp" // for Stack
using namespace cloe; // NOLINT(build/namespaces)

#ifndef CLOE_LUA_PATH
Expand Down Expand Up @@ -87,7 +87,8 @@ TEST_F(cloe_lua_setup, cloe_engine_is_available) {

TEST_F(cloe_lua_setup, describe_cloe) {
setup_lua(lua, opt, stack);
ASSERT_EQ(lua.script("local cloe = require('cloe'); return cloe.inspect(cloe.LogLevel.CRITICAL)").get<std::string>(),
ASSERT_EQ(lua.script("local cloe = require('cloe'); return cloe.inspect(cloe.LogLevel.CRITICAL)")
.get<std::string>(),
std::string("\"critical\""));
}

Expand Down
Loading
Loading