Skip to content

Commit

Permalink
Add bazel build for native code (#1224)
Browse files Browse the repository at this point in the history
Adds an experimental bazel build for C/C++ artifacts, 
along with a submodule of common build files.

Co-authored-by: Krzysztof Naglik <krzysztofnaglik96@gmail.com>
  • Loading branch information
jungleraptor and krisukox authored Sep 21, 2022
1 parent 16500e5 commit ddc35f1
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 4 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ gradle.properties
java/gradlew
java/gradlew.bat
java/gradle/wrapper

### Added by Hedron's Bazel Compile Commands Extractor: https://github.com/hedronvision/bazel-compile-commands-extractor
# The external link: Differs on Windows vs macOS/Linux, so we can't check it in. The pattern needs to not have a trailing / because it's a symlink on macOS/Linux.
/external
# Bazel output symlinks: Same reasoning as /external. You need the * because people can change the name of the directory your repository is cloned into, changing the bazel-<workspace_name> symlink.
/bazel-*
# Compiled output -> don't check in
/compile_commands.json
# Directory where clangd puts its indexing work
/.cache/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "c/third_party/googletest"]
path = c/third_party/googletest
url = https://github.com/google/googletest.git
[submodule "bazel"]
path = bazel
url = https://github.com/swift-nav/swiftnav-bazel.git
37 changes: 37 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
workspace(name = "root")

new_local_repository(
name = "my-check",
build_file = "bazel/check.BUILD",
path = "c/third_party/check",
)

local_repository(
name = "my-googletest",
path = "c/third_party/googletest",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_foreign_cc",
strip_prefix = "rules_foreign_cc-c65e8cfbaa002bcd1ce9c26e9fec63b0b866c94b",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/c65e8cfbaa002bcd1ce9c26e9fec63b0b866c94b.tar.gz",
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()

# Hedron's Compile Commands Extractor for Bazel
# Used to create compile_commands.json file
http_archive(
name = "hedron_compile_commands",
sha256 = "4b251a482a85de6c5cb0dc34c5671e73190b9ff348e9979fa2c033d81de0f928",
strip_prefix = "bazel-compile-commands-extractor-5bb5ff2f32d542a986033102af771aa4206387b9",
url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/5bb5ff2f32d542a986033102af771aa4206387b9.tar.gz",
)

load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()
1 change: 1 addition & 0 deletions bazel
Submodule bazel added at 5f1974
137 changes: 137 additions & 0 deletions c/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

SBP_INCLUDE = glob(["include/**/*.h"])

SBP_INCLUDE_INTERNAL = glob(["src/**/*.h"])

refresh_compile_commands(
name = "refresh_compile_commands",
)

cc_library(
name = "sbp",
srcs = [
"src/edc.c",
"src/sbp.c",
"src/v4/string/sbp_string.c",
"src/v4/string/multipart.c",
"src/v4/string/null_terminated.c",
"src/v4/string/double_null_terminated.c",
"src/v4/string/unterminated.c",
# generated files
"src/v4/acquisition.c",
"src/v4/bootload.c",
"src/v4/ext_events.c",
"src/v4/file_io.c",
"src/v4/flash.c",
"src/v4/gnss.c",
"src/v4/imu.c",
"src/v4/integrity.c",
"src/v4/linux.c",
"src/v4/logging.c",
"src/v4/mag.c",
"src/v4/navigation.c",
"src/v4/ndb.c",
"src/v4/observation.c",
"src/v4/orientation.c",
"src/v4/piksi.c",
"src/v4/sbas.c",
"src/v4/settings.c",
"src/v4/signing.c",
"src/v4/solution_meta.c",
"src/v4/ssr.c",
"src/v4/system.c",
"src/v4/tracking.c",
"src/v4/user.c",
"src/v4/vehicle.c",
],
hdrs = SBP_INCLUDE + SBP_INCLUDE_INTERNAL,
copts = ["-Ic/src/include"],
includes = [
"include",
],
visibility = ["//visibility:public"],
)

SBP_LEGACY_C_SOURCES = glob(["test/legacy/auto*.c"])

cc_test(
name = "sbp-legacy-test",
srcs = [
"test/check_main_legacy.c",
"test/check_edc.c",
"test/check_sbp.c",
"test/check_bitfield_macros.c",
"test/check_suites_legacy.h",
] + SBP_LEGACY_C_SOURCES,
includes = [
"include/libsbp",
],
deps = [
":sbp",
"@my-check//:check",
],
)

SBP_V4_C_SOURCES = glob(["test/auto*.c"])

cc_test(
name = "sbp-v4-test",
srcs = [
"test/check_main.c",
"test/check_edc.c",
"test/check_sbp.c",
"test/check_bitfield_macros.c",
"test/check_suites.h",
] + SBP_V4_C_SOURCES,
includes = ["include/libsbp"],
deps = [
":sbp",
"@my-check//:check",
],
)

SBP_CPP_V4_C_SOURCES = glob(["test/cpp/auto*.cc"])

cc_test(
name = "sbp-cpp-v4-test",
srcs = SBP_CPP_V4_C_SOURCES,
deps = [
":sbp",
"@my-googletest//:gtest_main",
],
)

cc_test(
name = "sbp-string-test",
srcs = [
"test/string/test_double_null_terminated.cc",
"test/string/test_multipart.cc",
"test/string/test_null_terminated.cc",
"test/string/test_unterminated.cc",
],
includes = [
"src/include",
],
deps = [
":sbp",
"@my-googletest//:gtest_main",
],
)

SBP_CPP_LEGACY_SOURCES = glob(["test/legacy/cpp/auto*.cc"])

cc_test(
name = "sbp-cpp-legacy-test",
srcs = [
"test/legacy/cpp/test_sbp_stdio.cc",
] + SBP_CPP_LEGACY_SOURCES,
data = [
"test/legacy/cpp/sbp_data/gnss_data.sbp",
],
deps = [
":sbp",
"@my-googletest//:gtest_main",
],
)
2 changes: 1 addition & 1 deletion c/test/legacy/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ swift_add_test(test-libsbp-cpp-legacy
swift_set_language_standards(test-libsbp-cpp-legacy)
swift_set_compile_options(test-libsbp-cpp-legacy ADD -Wno-error=array-bounds)

file(COPY sbp_data/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY sbp_data/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/c/test/legacy/cpp/sbp_data/)
6 changes: 3 additions & 3 deletions c/test/legacy/cpp/test_sbp_stdio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class SbpStdioTest : public ::testing::Test {
state.set_reader(&reader);
state.set_writer(&writer);
MsgObsHandler handler(&state);

while (true) {
s8 status = state.process();
if (status < SBP_OK) {
Expand All @@ -97,11 +96,12 @@ class SbpStdioTest : public ::testing::Test {
};

TEST_F(SbpStdioTest, ReadsSbpFiles) {
EXPECT_EQ(num_entries_in_file("gnss_data.sbp"), 3);
EXPECT_EQ(num_entries_in_file("c/test/legacy/cpp/sbp_data/gnss_data.sbp"), 3);
}

TEST_F(SbpStdioTest, WritesToSbpFiles) {
write_to_file("gnss_data.sbp", "gnss_data_output.sbp");
write_to_file("c/test/legacy/cpp/sbp_data/gnss_data.sbp",
"gnss_data_output.sbp");
EXPECT_EQ(num_entries_in_file("gnss_data_output.sbp"), 9);
}

Expand Down

0 comments on commit ddc35f1

Please sign in to comment.