From c547c9bc51d987e2cca3850843477006d2a92c6f Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Tue, 8 Oct 2024 09:48:38 -0500 Subject: [PATCH] Build gz-utils with bzlmod (#128) Signed-off-by: Michael Carroll --- .bazelrc | 12 +++ .bazelversion | 1 + .gitignore | 6 ++ BUILD.bazel | 168 ++++++++++++++++++++-------------- MODULE.bazel | 12 +++ WORKSPACE | 0 cli/BUILD.bazel | 49 ++++++---- include/gz/utils/config.hh.in | 14 +-- log/BUILD.bazel | 65 +++++++++++++ 9 files changed, 233 insertions(+), 94 deletions(-) create mode 100644 .bazelrc create mode 100644 .bazelversion create mode 100644 MODULE.bazel create mode 100644 WORKSPACE create mode 100644 log/BUILD.bazel diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..f2d0613 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,12 @@ +common --enable_bzlmod +common --lockfile_mode=off + +# Add C++17 compiler flags. +build --cxxopt=-std=c++17 +build --host_cxxopt=-std=c++17 + +build --force_pic +build --strip=never +build --strict_system_includes +build --fission=dbg +build --features=per_object_debug_info diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..643916c --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +7.3.1 diff --git a/.gitignore b/.gitignore index 62b9898..39555d6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,9 @@ build_* # OS generated files .DS_Store *.swp + +# Bazel generated files +bazel-bin/ +bazel-out/ +bazel-testlogs/ +bazel-* diff --git a/BUILD.bazel b/BUILD.bazel index 435829a..395826f 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,18 +1,11 @@ -load( - "@gz//bazel/skylark:build_defs.bzl", - "GZ_FEATURES", - "GZ_ROOT", - "GZ_VISIBILITY", - "gz_configure_header", - "gz_export_header", - "gz_include_header", -) +load("@buildifier_prebuilt//:rules.bzl", "buildifier", "buildifier_test") +load("@rules_gazebo//gazebo:headers.bzl", "gz_configure_header", "gz_export_header") load("@rules_license//rules:license.bzl", "license") package( - default_applicable_licenses = [GZ_ROOT + "utils:license"], - default_visibility = GZ_VISIBILITY, - features = GZ_FEATURES, + default_applicable_licenses = [":license"], + default_visibility = ["__subpackages__"], + features = ["layering_check"], ) license( @@ -20,51 +13,87 @@ license( package_name = "gz-utils", ) -licenses(["notice"]) +exports_files([ + "LICENSE", + "MODULE.bazel", +]) -exports_files(["LICENSE"]) +gz_export_header( + name = "Export", + out = "include/gz/utils/Export.hh", + export_base = "GZ_UTILS", + lib_name = "gz-utils", +) gz_configure_header( - name = "config", + name = "Config", src = "include/gz/utils/config.hh.in", - cmakelists = ["CMakeLists.txt"], - package = "utils", + package_xml = "package.xml", ) -gz_export_header( - name = "include/gz/utils/Export.hh", - export_base = "GZ_UTILS", - lib_name = "gz-utils", - visibility = ["//visibility:private"], +cc_library( + name = "Environment", + srcs = ["src/Environment.cc"], + hdrs = ["include/gz/utils/Environment.hh"], + includes = ["include"], + deps = [ + ":Config", + ":Export", + ], ) -public_headers_no_gen = glob([ - "include/gz/utils/*.hh", - "include/gz/utils/detail/*.hh", - "include/gz/utils/detail/*.h", -]) - -gz_include_header( - name = "utilshh_genrule", - out = "include/gz/utils.hh", - hdrs = public_headers_no_gen + [ - "include/gz/utils/Export.hh", - "include/gz/utils/config.hh", +cc_library( + name = "ImplPtr", + hdrs = [ + "include/gz/utils/ImplPtr.hh", + "include/gz/utils/detail/DefaultOps.hh", + "include/gz/utils/detail/ImplPtr.hh", + ], + includes = ["include"], + visibility = ["//visibility:public"], + deps = [ + ":Export", + ":SuppressWarning", ], ) -public_headers = public_headers_no_gen + [ - "include/gz/utils/config.hh", - "include/gz/utils/Export.hh", - "include/gz/utils.hh", -] +cc_library( + name = "NeverDestroyed", + hdrs = ["include/gz/utils/NeverDestroyed.hh"], + includes = ["include"], + visibility = ["//visibility:public"], +) cc_library( - name = "utils", - srcs = ["src/Environment.cc"], - hdrs = public_headers, - copts = ["-fexceptions"], + name = "SuppressWarning", + hdrs = [ + "include/gz/utils/SuppressWarning.hh", + "include/gz/utils/detail/SuppressWarning.hh", + ], + includes = ["include"], + visibility = ["//visibility:public"], +) + +cc_library( + name = "Subprocess", + hdrs = [ + "include/gz/utils/Subprocess.hh", + "include/gz/utils/detail/subprocess.h", + ], includes = ["include"], + visibility = ["//visibility:public"], +) + +cc_library( + name = "gz-utils", + visibility = ["//visibility:public"], + deps = [ + ":Environment", + ":ImplPtr", + ":NeverDestroyed", + ":Subprocess", + ":SuppressWarning", + ], ) # Tests @@ -77,7 +106,7 @@ cc_library( "test/integration/implptr/implptr_test_classes.hh", ], includes = ["test/integration/implptr"], - deps = [":utils"], + deps = [":ImplPtr"], ) cc_test( @@ -86,8 +115,7 @@ cc_test( srcs = ["test/integration/implptr/ImplPtr_TEST.cc"], deps = [ ":implptr_test_classes", - "@gtest", - "@gtest//:gtest_main", + "@googletest//:gtest_main", ], ) @@ -95,39 +123,43 @@ cc_test( name = "Environment_TEST", srcs = ["src/Environment_TEST.cc"], deps = [ - ":utils", - "@gtest", - "@gtest//:gtest_main", + ":Environment", + "@googletest//:gtest_main", ], ) -cc_test( - name = "NeverDestroyed_TEST", - srcs = ["src/NeverDestroyed_TEST.cc"], - copts = ["-fexceptions"], +cc_binary( + name = "subprocess_main", + srcs = ["test/integration/subprocess/subprocess_main.cc"], deps = [ - ":utils", - "@gtest", - "@gtest//:gtest_main", + ":Environment", + "//cli", ], ) -cc_binary( - name = "subprocess_main", - srcs = ["test/integration/subprocess/subprocess_main.cc"], - deps = [ - GZ_ROOT + "utils/cli", - ] -) - cc_test( name = "subprocess_TEST", srcs = ["test/integration/subprocess_TEST.cc"], + local_defines = ['SUBPROCESS_EXECUTABLE_PATH=\\"subprocess_main\\"'], deps = [ - ":utils", + ":gz-utils", ":subprocess_main", - "@gtest", - "@gtest//:gtest_main", + "@googletest//:gtest_main", ], - local_defines = ['SUBPROCESS_EXECUTABLE_PATH=\\"utils/subprocess_main\\"'], +) + +buildifier( + name = "buildifier.fix", + exclude_patterns = ["./.git/*"], + lint_mode = "fix", + mode = "fix", +) + +buildifier_test( + name = "buildifier.test", + exclude_patterns = ["./.git/*"], + lint_mode = "warn", + mode = "diff", + no_sandbox = True, + workspace = "//:MODULE.bazel", ) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..6a8b2ce --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,12 @@ +"gz-utils" + +module( + name = "gz-utils", + repo_name = "org_gazebosim_gz-utils", +) + +bazel_dep(name = "buildifier_prebuilt", version = "7.3.1") +bazel_dep(name = "googletest", version = "1.14.0") +bazel_dep(name = "rules_gazebo", version = "0.0.2") +bazel_dep(name = "rules_license", version = "1.0.0") +bazel_dep(name = "spdlog", version = "1.14.1") diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..e69de29 diff --git a/cli/BUILD.bazel b/cli/BUILD.bazel index b97bc2d..f1d18fa 100644 --- a/cli/BUILD.bazel +++ b/cli/BUILD.bazel @@ -1,12 +1,7 @@ -load( - "@gz//bazel/skylark:build_defs.bzl", - "GZ_ROOT", - "GZ_VISIBILITY", -) load("@rules_license//rules:license.bzl", "license") package( - default_applicable_licenses = [GZ_ROOT + "utils/cli:license"], + default_applicable_licenses = ["//:license"], ) license( @@ -14,23 +9,39 @@ license( package_name = "gz-utils-cli", ) -public_headers = [ - "include/gz/utils/cli/GzFormatter.hpp", -] + glob([ - "include/external-cli/gz/utils/cli/*.hpp", -]) +cc_library( + name = "cli11", + hdrs = glob([ + "include/vendored-cli/gz/utils/cli/*.hpp", + ]), + includes = ["include/vendored-cli"], +) + +cc_library( + name = "GzFormatter", + hdrs = [ + "include/gz/utils/cli/GzFormatter.hpp", + ], + includes = ["include"], + deps = [ + "//:Export", + ], +) cc_library( name = "cli", - hdrs = public_headers, - copts = ["-fexceptions"], - includes = [ - "include", - "include/external-cli", + visibility = ["//visibility:public"], + deps = [ + ":GzFormatter", + ":cli11", ], - visibility = GZ_VISIBILITY, +) + +cc_test( + name = "cli_TEST", + srcs = ["src/cli_TEST.cc"], deps = [ - GZ_ROOT + "utils:utils", - "@cli11" + ":cli", + "@googletest//:gtest_main", ], ) diff --git a/include/gz/utils/config.hh.in b/include/gz/utils/config.hh.in index 60984a8..4c05ce3 100644 --- a/include/gz/utils/config.hh.in +++ b/include/gz/utils/config.hh.in @@ -21,15 +21,15 @@ #define GZ_UTILS_CONFIG_HH_ /* Version number */ -#define GZ_UTILS_MAJOR_VERSION ${PROJECT_VERSION_MAJOR} -#define GZ_UTILS_MINOR_VERSION ${PROJECT_VERSION_MINOR} -#define GZ_UTILS_PATCH_VERSION ${PROJECT_VERSION_PATCH} +#define GZ_UTILS_MAJOR_VERSION @PROJECT_VERSION_MAJOR@ +#define GZ_UTILS_MINOR_VERSION @PROJECT_VERSION_MINOR@ +#define GZ_UTILS_PATCH_VERSION @PROJECT_VERSION_PATCH@ -#define GZ_UTILS_VERSION "${PROJECT_VERSION}" -#define GZ_UTILS_VERSION_FULL "${PROJECT_VERSION_FULL}" +#define GZ_UTILS_VERSION "@PROJECT_VERSION@" +#define GZ_UTILS_VERSION_FULL "@PROJECT_VERSION_FULL@" -#define GZ_UTILS_VERSION_NAMESPACE v${PROJECT_VERSION_MAJOR} +#define GZ_UTILS_VERSION_NAMESPACE v@PROJECT_VERSION_MAJOR@ -#define GZ_UTILS_VERSION_HEADER "Gazebo Utils, version ${PROJECT_VERSION_FULL}\nCopyright (C) 2020 Open Source Robotics Foundation.\nReleased under the Apache 2.0 License.\n\n" +#define GZ_UTILS_VERSION_HEADER "Gazebo Utils, version @PROJECT_VERSION_FULL@\nCopyright (C) 2020 Open Source Robotics Foundation.\nReleased under the Apache 2.0 License.\n\n" #endif diff --git a/log/BUILD.bazel b/log/BUILD.bazel new file mode 100644 index 0000000..6e314c7 --- /dev/null +++ b/log/BUILD.bazel @@ -0,0 +1,65 @@ +load("@rules_gazebo//gazebo:headers.bzl", "gz_export_header") +load("@rules_license//rules:license.bzl", "license") + +package( + default_applicable_licenses = ["//:license"], +) + +license( + name = "license", + package_name = "gz-utils-log", +) + +gz_export_header( + name = "Export", + out = "include/gz/utils/log/Export.hh", + export_base = "GZ_UTILS_LOG", + lib_name = "gz-utils-log", +) + +cc_library( + name = "SplitSink", + srcs = [ + "src/SplitSink.cc", + ], + hdrs = [ + "include/gz/utils/log/SplitSink.hh", + ], + includes = ["include"], + deps = [ + ":Export", + "//:Config", + "//:ImplPtr", + "@spdlog", + ], +) + +cc_library( + name = "Logger", + srcs = [ + "src/Logger.cc", + ], + hdrs = [ + "include/gz/utils/log/Logger.hh", + ], + includes = ["include"], + deps = [":SplitSink"], +) + +cc_library( + name = "log", + visibility = ["//visibility:public"], + deps = [ + ":Logger", + ":SplitSink", + ], +) + +cc_test( + name = "SplitSink_TEST", + srcs = ["src/SplitSink_TEST.cc"], + deps = [ + ":log", + "@googletest//:gtest_main", + ], +)