From ddc35f19c02e904b0583e6e61d2c7ae943d98bc0 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 21 Sep 2022 12:03:27 -0600 Subject: [PATCH] Add bazel build for native code (#1224) Adds an experimental bazel build for C/C++ artifacts, along with a submodule of common build files. Co-authored-by: Krzysztof Naglik --- .gitignore | 10 ++ .gitmodules | 3 + WORKSPACE | 37 ++++++++ bazel | 1 + c/BUILD.bazel | 137 ++++++++++++++++++++++++++++ c/test/legacy/cpp/CMakeLists.txt | 2 +- c/test/legacy/cpp/test_sbp_stdio.cc | 6 +- 7 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 WORKSPACE create mode 160000 bazel create mode 100644 c/BUILD.bazel diff --git a/.gitignore b/.gitignore index 946c93f2fb..d6f8c795eb 100644 --- a/.gitignore +++ b/.gitignore @@ -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- symlink. +/bazel-* +# Compiled output -> don't check in +/compile_commands.json +# Directory where clangd puts its indexing work +/.cache/ diff --git a/.gitmodules b/.gitmodules index 96ad675bbc..2d8b28a251 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000000..5ce9d14ed2 --- /dev/null +++ b/WORKSPACE @@ -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() diff --git a/bazel b/bazel new file mode 160000 index 0000000000..5f1974302a --- /dev/null +++ b/bazel @@ -0,0 +1 @@ +Subproject commit 5f1974302a0fbe4729cd77781ed6bd2bb6ad0465 diff --git a/c/BUILD.bazel b/c/BUILD.bazel new file mode 100644 index 0000000000..ce63736787 --- /dev/null +++ b/c/BUILD.bazel @@ -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", + ], +) diff --git a/c/test/legacy/cpp/CMakeLists.txt b/c/test/legacy/cpp/CMakeLists.txt index b1fb48472b..0331081338 100644 --- a/c/test/legacy/cpp/CMakeLists.txt +++ b/c/test/legacy/cpp/CMakeLists.txt @@ -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/) diff --git a/c/test/legacy/cpp/test_sbp_stdio.cc b/c/test/legacy/cpp/test_sbp_stdio.cc index 58a2acbc96..5de76451dd 100644 --- a/c/test/legacy/cpp/test_sbp_stdio.cc +++ b/c/test/legacy/cpp/test_sbp_stdio.cc @@ -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) { @@ -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); }