Skip to content

Commit

Permalink
* Added custom Bazel flags (#74)
Browse files Browse the repository at this point in the history
* * Added custom Bazel flag `--//mbo/config:require_throws` which controls whether `MBO_CONFIG_REQUIRE` throw exceptions or use crash logging (the default `False` or `0`). This mostly affects containers.
* Added custom Bazel flag `--//mbo/config:limited_ordered_max_unroll_capacity`. This was undocumented as `--//mbo/container:limited_ordered_max_unroll_capacity` until now. It controls the maximum unroll size for LimitedOrdered/Map/Set.

* Make test `LimitedOrderedTest.ConstexprRequireSortedInputThrows` work in all configs and all compilation modes for all supported compilers.
  • Loading branch information
helly25 authored Oct 19, 2024
1 parent 853b5af commit bfa41cd
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 145 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 0.2.34

* Added `mbo::testing::WhenTransformedBy` which allows to compare containers after transforming them.
* Added custom Bazel flag `--//mbo/config:require_throws` which controls whether `MBO_CONFIG_REQUIRE` throw exceptions or use crash logging (the default `False` or `0`). This mostly affects containers.
* Added custom Bazel flag `--//mbo/config:limited_ordered_max_unroll_capacity`. This was undocumented as `--//mbo/container:limited_ordered_max_unroll_capacity` until now. It controls the maximum unroll size for LimitedOrdered/Map/Set.

# 0.2.33

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ The library is tested with Clang and GCC using continuous integration: [![Test](

The C++ library is organized in functional groups each residing in their own directory.

* Config
* `namespace mbo::config`
* mbo/config:config_cc, mbo/config/config.h
* Custom Bazel flag `--//mbo/config:limited_ordered_max_unroll_capacity` which controls the maximum unroll size for `LimitedOrdered` and thus `LimitedMap` and `LimitedSet`.
* Custom Bazel flag `--//mbo/config:require_throws` which controls whether `MBO_CONFIG_REQUIRE` throw exceptions or use crash logging (the default `False` or `0`). This mostly affects containers.
* mbo/config:require_cc, mbo/config/require.h
* Marcos `MBO_CONFIG_REQUIRE(condition, message)` which allows to check a `condition` and either throw an exception or crash with Abseil FATAL logging. The behavior is controlled by `--//mbo/config:require_throws`.
* Container
* `namespace mbo::container`
* mbo/container:any_scan_cc, mbo/container/any_scan.h
Expand Down
2 changes: 2 additions & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ Some rules for the code layout and its development.
* Flags in libraries should be prefixed with their path/namespace. E.g. the flag
`--mbo_log_timing_min_duration` has the prefix `mbo_log` as it is defined in
`mbo/log/log_timing.cc` (path `mbo/log`) and uses namespace `mbo::log`.
* API changes that are not backwards compatible should not occur in minor version changes.
* Undocumented and private/internal APIs may be changed in any way at any time.
62 changes: 62 additions & 0 deletions mbo/config/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# SPDX-FileCopyrightText: Copyright (c) The helly25/mbo authors (helly25.com)
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "int_flag")
load(":internal/config.bzl", "config_gen")

# Create custom bazel flag `--//mbo/container:limited_ordered_max_unroll_capacity`.
int_flag(
name = "limited_ordered_max_unroll_capacity",
build_setting_default = 16,
visibility = ["//visibility:private"],
)

# Create custom bazel flag `--//mbo/config:require_throws`.
bool_flag(
name = "require_throws",
build_setting_default = False,
visibility = ["//visibility:private"],
)

bzl_library(
name = "config_bzl",
srcs = [":internal/config.bzl"],
visibility = ["//visibility:private"],
deps = ["@bazel_skylib//rules:common_settings"],
)

config_gen(
name = "config_gen",
visibility = ["//visibility:private"],
output = "config.h",
template = "internal/config.h.in",
)

cc_library(
name = "config_cc",
hdrs = ["config.h"],
visibility = ["//mbo:__subpackages__"],
)

cc_library(
name = "require_cc",
hdrs = ["require.h"],
deps = [
":config_cc",
"@com_google_absl//absl/log:absl_log",
],
visibility = ["//mbo:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _limited_ordered_config_gen_impl(ctx):
def _config_gen_impl(ctx):
limited_ordered_max_unroll_capacity = ctx.attr._limited_ordered_max_unroll_capacity[BuildSettingInfo].value
require_throws = ctx.attr._require_throws[BuildSettingInfo].value
ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.output,
substitutions = {
"kUnrollMaxCapacityDefault = 16;": "kUnrollMaxCapacityDefault = " + repr(limited_ordered_max_unroll_capacity) + ";",
"kRequireThrows = false;": "kRequireThrows = " + repr(require_throws).lower() + ";",
},
)

limited_ordered_config_gen = rule(
config_gen = rule(
attrs = {
"output": attr.output(
mandatory = True,
Expand All @@ -36,7 +38,8 @@ limited_ordered_config_gen = rule(
allow_single_file = True,
mandatory = True,
),
"_limited_ordered_max_unroll_capacity": attr.label(default = Label("//mbo/container:limited_ordered_max_unroll_capacity")),
"_limited_ordered_max_unroll_capacity": attr.label(default = Label("//mbo/config:limited_ordered_max_unroll_capacity")),
"_require_throws": attr.label(default = Label("//mbo/config:require_throws")),
},
implementation = _limited_ordered_config_gen_impl,
implementation = _config_gen_impl,
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <cstddef>

namespace mbo::container::container_internal {
namespace mbo::config {

// The maximum unroll capacity for `LimitedOrdered` based containers: `LimitedSet` and `LimitedMap`.
// Beyond unrolling 32 comparison steps, unrolling has deminishing returns for any architecture.
Expand All @@ -33,6 +33,12 @@ namespace mbo::container::container_internal {
// `--@com_helly_25//mbo/container:limited_ordered_max_unroll_capacity`.
static constexpr std::size_t kUnrollMaxCapacityDefault = 16;

} // namespace mbo::container::container_internal
// Config macro `MBO_CONFIG_REQUIRE_THROWS` controls whether container requirement violations
// result in throwing a `std::runtime_error` or a `ABSL_LOG_IF` (the latter (0) being the default).
//
// When exceptions are used then the affected functions cannot be declared `noexcept`.
static constexpr bool kRequireThrows = false;

} // namespace mbo::config

#endif // MBO_CONTAINER_INTERNAL_LIMITED_ORDERED_CONFIG_H_
50 changes: 50 additions & 0 deletions mbo/config/require.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: Copyright (c) The helly25/mbo authors (helly25.com)
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MBO_CONTAINER_INTERNAL_REQUIRE_H_
#define MBO_CONTAINER_INTERNAL_REQUIRE_H_

#include <stdexcept> // IWYU pragma: keep

#include "absl/log/absl_log.h" // IWYU pragma: keep

// If `mbo/config/config.h` is available, then include that. In order to
// make indexers work, we also include the generator "...in" as a fallback.
#if __has_include("mbo/config/config.h")
# include "mbo/config/config.h" // IWYU pragma: keep
#else
# include "mbo/config/internal/config.h.in" // IWYU pragma: keep
#endif

// NOLINTBEGIN(cppcoreguidelines-macro-usage)

#define MBO_PRIVATE_CONFIG_CAT_CAT_(line) #line
#define MBO_PRIVATE_CONFIG_NUM2STR_(line) MBO_PRIVATE_CONFIG_CAT_CAT_(line)

#ifdef MBO_CONFIG_REQUIRE
# undef MBO_CONFIG_REQUIRE
#endif

#define MBO_CONFIG_REQUIRE(condition, message) \
if constexpr (!::mbo::config::kRequireThrows) { \
/* NOLINTNEXTLINE(bugprone-switch-missing-default-case) */ \
ABSL_LOG_IF(FATAL, !(condition)) << message; \
} else if ((condition)) { /* GOOD */ \
} else \
throw std::runtime_error(__FILE__ ":" MBO_PRIVATE_CONFIG_NUM2STR_(__LINE__) " : " #condition " : " message)

// NOLINTEND(cppcoreguidelines-macro-usage)

#endif // MBO_CONTAINER_INTERNAL_REQUIRE_H_
35 changes: 6 additions & 29 deletions mbo/container/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "int_flag")
load(":internal/limited_ordered_config.bzl", "limited_ordered_config_gen")

# Create custom bazel flag `--//mbo/container:limited_ordered_max_unroll_capacity`.
int_flag(
name = "limited_ordered_max_unroll_capacity",
build_setting_default = 16,
visibility = ["//visibility:private"],
)

bzl_library(
name = "limited_ordered_config_bzl",
srcs = [":internal/limited_ordered_config.bzl"],
visibility = ["//visibility:private"],
deps = ["@bazel_skylib//rules:common_settings"],
)

limited_ordered_config_gen(
name = "limited_ordered_config_gen",
visibility = ["//visibility:private"],
output = "internal/limited_ordered_config.h",
template = "internal/limited_ordered_config.h.in"
)

cc_library(
name = "any_scan_cc",
hdrs = ["any_scan.h"],
Expand Down Expand Up @@ -118,13 +93,12 @@ cc_library(

cc_library(
name = "limited_ordered_cc",
hdrs = [
"internal/limited_ordered_config.h",
"internal/limited_ordered.h",
],
hdrs = ["internal/limited_ordered.h"],
visibility = ["//visibility:private"],
deps = [
":limited_options_cc",
"//mbo/config:config_cc",
"//mbo/config:require_cc",
"//mbo/types:compare_cc",
"//mbo/types:traits_cc",
"@com_google_absl//absl/log:absl_log",
Expand All @@ -137,6 +111,8 @@ cc_test(
srcs = ["limited_ordered_test.cc"],
deps = [
":limited_ordered_cc",
"//mbo/config:config_cc",
"//mbo/config:require_cc",
"//mbo/testing:matchers_cc",
"@com_google_absl//absl/log:initialize",
"@com_google_googletest//:gtest_main",
Expand Down Expand Up @@ -189,6 +165,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":limited_options_cc",
"//mbo/config:require_cc",
"//mbo/types:traits_cc",
"@com_google_absl//absl/log:absl_log",
],
Expand Down
Loading

0 comments on commit bfa41cd

Please sign in to comment.