Add bazel support for disabling enum traits
This adds bazel support for setting the `--no-cc-enum-traits` flag and
adds a test to exercise it.
diff --git a/build_defs.bzl b/build_defs.bzl
index 14e6045..0c1e6a8 100644
--- a/build_defs.bzl
+++ b/build_defs.bzl
@@ -26,7 +26,7 @@
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
-def emboss_cc_library(name, srcs, deps = [], visibility = None, import_dirs = []):
+def emboss_cc_library(name, srcs, deps = [], visibility = None, import_dirs = [], enable_enum_traits = True):
"""Constructs a C++ library from an .emb file."""
if len(srcs) != 1:
fail(
@@ -45,6 +45,7 @@
name = name,
deps = [":" + name + "_ir"],
visibility = visibility,
+ enable_enum_traits = enable_enum_traits,
)
# Full Starlark rules for emboss_library and cc_emboss_library.
@@ -174,6 +175,8 @@
args.add_all(emboss_info.direct_ir)
args.add("--output-file")
args.add_all(headers)
+ if not ctx.attr.enable_enum_traits:
+ args.add("--no-cc-enum-traits")
ctx.actions.run(
executable = emboss_cc_compiler,
arguments = [args],
@@ -222,6 +225,9 @@
"_emboss_cc_runtime": attr.label(
default = "@com_google_emboss//runtime/cpp:cpp_utils",
),
+ "enable_enum_traits": attr.bool(
+ default = True,
+ ),
},
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
)
@@ -244,6 +250,9 @@
allow_rules = ["emboss_library"],
allow_files = False,
),
+ "enable_enum_traits": attr.bool(
+ default = True,
+ ),
},
provides = [CcInfo, EmbossInfo],
)
diff --git a/compiler/back_end/cpp/BUILD b/compiler/back_end/cpp/BUILD
index e9ae268..1c496d6 100644
--- a/compiler/back_end/cpp/BUILD
+++ b/compiler/back_end/cpp/BUILD
@@ -225,6 +225,17 @@
)
emboss_cc_test(
+ name = "no_enum_traits_test",
+ srcs = [
+ "testcode/no_enum_traits_test.cc",
+ ],
+ deps = [
+ "//testdata:no_enum_traits_emboss",
+ "@com_google_googletest//:gtest_main",
+ ],
+)
+
+emboss_cc_test(
name = "start_size_range_test",
srcs = [
"testcode/start_size_range_test.cc",
diff --git a/compiler/back_end/cpp/testcode/no_enum_traits_test.cc b/compiler/back_end/cpp/testcode/no_enum_traits_test.cc
new file mode 100644
index 0000000..78dbd90
--- /dev/null
+++ b/compiler/back_end/cpp/testcode/no_enum_traits_test.cc
@@ -0,0 +1,48 @@
+// Copyright 2024 Google LLC
+//
+// 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
+//
+// https://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.
+
+// Tests that an emb compiled with enable_enum_traits = False actually compiles.
+
+#include <stdint.h>
+
+#include <vector>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "testdata/no_enum_traits.emb.h"
+
+namespace emboss {
+namespace test {
+namespace {
+
+TEST(NoEnumTraits, Compiles) {
+ ::std::vector<uint8_t> backing_store(1);
+ auto view = MakeBarView(&backing_store);
+ view.foo().Write(Foo::VALUE);
+ EXPECT_TRUE(view.Ok());
+
+ // Check that we don't accidentally include `emboss_text_util.h` via our
+ // generated header.
+#ifdef EMBOSS_RUNTIME_CPP_EMBOSS_TEXT_UTIL_H_
+ const bool emboss_text_util_is_present = true;
+#else
+ const bool emboss_text_util_is_present = false;
+#endif
+
+ EXPECT_FALSE(emboss_text_util_is_present);
+}
+
+} // namespace
+} // namespace test
+} // namespace emboss
diff --git a/testdata/BUILD b/testdata/BUILD
index a04b4a6..a886d39 100644
--- a/testdata/BUILD
+++ b/testdata/BUILD
@@ -242,6 +242,14 @@
)
emboss_cc_library(
+ name = "no_enum_traits_emboss",
+ srcs = [
+ "no_enum_traits.emb",
+ ],
+ enable_enum_traits = False,
+)
+
+emboss_cc_library(
name = "start_size_range_emboss",
srcs = [
"start_size_range.emb",
diff --git a/testdata/no_enum_traits.emb b/testdata/no_enum_traits.emb
new file mode 100644
index 0000000..bbe295e
--- /dev/null
+++ b/testdata/no_enum_traits.emb
@@ -0,0 +1,24 @@
+# Copyright 2019 Google LLC
+#
+# 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
+#
+# https://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.
+
+-- Test .emb to ensure that compilation succedes if enum traits are disabled.
+
+[$default byte_order: "LittleEndian"]
+[(cpp) namespace: "emboss::test"]
+
+enum Foo:
+ VALUE = 10
+
+struct Bar:
+ 0 [+1] Foo foo
\ No newline at end of file