From 7baf5f8021361373191b35e3ba8d55c688b11992 Mon Sep 17 00:00:00 2001 From: bcsgh <33939446+bcsgh@users.noreply.github.com> Date: Tue, 16 Jan 2024 09:27:57 -0800 Subject: [PATCH 1/6] Sketch a way to set OPENTELEMETRY_STL_VERSION under bazel. --- api/BUILD | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/api/BUILD b/api/BUILD index e650d5f7ce..e0afe8b921 100644 --- a/api/BUILD +++ b/api/BUILD @@ -1,7 +1,7 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag") package(default_visibility = ["//visibility:public"]) @@ -10,12 +10,35 @@ bool_flag( build_setting_default = False, ) +CPP_STDLIBS = [ + "none", + "best", + "2014", + "2017", + "2020", + "2023", +] + +string_flag( + name = "with_cxx_stdlib", + values = CPP_STDLIBS, + build_setting_default = "best", +) + cc_library( name = "api", hdrs = glob(["include/**/*.h"]), defines = select({ ":with_external_abseil": ["HAVE_ABSEIL"], "//conditions:default": [], + }) + select({ + ":set_cxx_stdlib_none": [], + ":set_cxx_stdlib_best": ["OPENTELEMETRY_STL_VERSION=(__cplusplus/100)"], + ":set_cxx_stdlib_2014": ["OPENTELEMETRY_STL_VERSION=2014"], + ":set_cxx_stdlib_2017": ["OPENTELEMETRY_STL_VERSION=2017"], + ":set_cxx_stdlib_2020": ["OPENTELEMETRY_STL_VERSION=2020"], + ":set_cxx_stdlib_2023": ["OPENTELEMETRY_STL_VERSION=2023"], + "//conditions:default": [], }), strip_include_prefix = "include", tags = ["api"], @@ -33,3 +56,8 @@ config_setting( name = "with_external_abseil", flag_values = {":with_abseil": "true"}, ) + +[config_setting( + name = "set_cxx_stdlib_%s" % v, + flag_values = {":with_cxx_stdlib": v}, +) for v in CPP_STDLIBS] From 3cc5c1bbe3d2bc5b829b32ab1c54fb6fba7e6793 Mon Sep 17 00:00:00 2001 From: bcsgh <33939446+bcsgh@users.noreply.github.com> Date: Wed, 17 Jan 2024 10:25:05 -0800 Subject: [PATCH 2/6] Add some '#if defined' to generate a better error if OPENTELEMETRY_STL_VERSION is set to a version unsupported by the current enviornment. --- api/include/opentelemetry/version.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/include/opentelemetry/version.h b/api/include/opentelemetry/version.h index 7b9fa1da02..af5604c7ea 100644 --- a/api/include/opentelemetry/version.h +++ b/api/include/opentelemetry/version.h @@ -6,6 +6,13 @@ #include "opentelemetry/common/macros.h" #include "opentelemetry/detail/preprocessor.h" +#if defined(OPENTELEMETRY_STL_VERSION) +# if OPENTELEMETRY_STL_VERSION > (__cplusplus/100) +# pragma message OPENTELEMETRY_STRINGIFY(OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY(__cplusplus) +# error "OPENTELEMETRY_STL_VERSION set to version newer than compilation version." +# endif +#endif + #ifndef OPENTELEMETRY_ABI_VERSION_NO # define OPENTELEMETRY_ABI_VERSION_NO 1 #endif From b54329539e4fb971f8ce59bff603cf6d691399d4 Mon Sep 17 00:00:00 2001 From: bcsgh <33939446+bcsgh@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:54:22 -0800 Subject: [PATCH 3/6] Special case MSVC (because it prefers breaking correct future usage over existing incorrect usage). --- api/BUILD | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/BUILD b/api/BUILD index e0afe8b921..5a3a68c34b 100644 --- a/api/BUILD +++ b/api/BUILD @@ -33,7 +33,11 @@ cc_library( "//conditions:default": [], }) + select({ ":set_cxx_stdlib_none": [], + ":set_cxx_stdlib_best": ["OPENTELEMETRY_STL_VERSION=(__cplusplus/100)"], + # See https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus + ":set_cxx_stdlib_best_and_msvc": ["OPENTELEMETRY_STL_VERSION=(_MSVC_LANG/100)"], + ":set_cxx_stdlib_2014": ["OPENTELEMETRY_STL_VERSION=2014"], ":set_cxx_stdlib_2017": ["OPENTELEMETRY_STL_VERSION=2017"], ":set_cxx_stdlib_2020": ["OPENTELEMETRY_STL_VERSION=2020"], @@ -61,3 +65,9 @@ config_setting( name = "set_cxx_stdlib_%s" % v, flag_values = {":with_cxx_stdlib": v}, ) for v in CPP_STDLIBS] + +config_setting( + name = "set_cxx_stdlib_best_and_msvc", + flag_values = {":with_cxx_stdlib": "best"}, + constraint_values = ["@bazel_tools//tools/cpp:msvc"], +) From daca53bf11f2d286fec67849704d0e0393efc3d6 Mon Sep 17 00:00:00 2001 From: bcsgh <33939446+bcsgh@users.noreply.github.com> Date: Thu, 18 Jan 2024 08:39:42 -0800 Subject: [PATCH 4/6] Special case MSVC in version guard (and update message). --- api/include/opentelemetry/version.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/version.h b/api/include/opentelemetry/version.h index af5604c7ea..fe3a228560 100644 --- a/api/include/opentelemetry/version.h +++ b/api/include/opentelemetry/version.h @@ -7,9 +7,10 @@ #include "opentelemetry/detail/preprocessor.h" #if defined(OPENTELEMETRY_STL_VERSION) -# if OPENTELEMETRY_STL_VERSION > (__cplusplus/100) +# if (!defined(_MSVC_LANG) && OPENTELEMETRY_STL_VERSION > (__cplusplus/100)) || \ + (defined(_MSVC_LANG) && OPENTELEMETRY_STL_VERSION > (_MSVC_LANG/100)) # pragma message OPENTELEMETRY_STRINGIFY(OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY(__cplusplus) -# error "OPENTELEMETRY_STL_VERSION set to version newer than compilation version." +# error "OPENTELEMETRY_STL_VERSION is set to a version newer than the curent C++ version." # endif #endif From 697794d098af57cb582c19dd51bda79d2d446ed1 Mon Sep 17 00:00:00 2001 From: bcsgh <33939446+bcsgh@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:48:28 -0800 Subject: [PATCH 5/6] Make formatting happy. --- api/BUILD | 8 ++++---- api/include/opentelemetry/version.h | 13 ++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/api/BUILD b/api/BUILD index 5a3a68c34b..fa4da45a66 100644 --- a/api/BUILD +++ b/api/BUILD @@ -21,8 +21,8 @@ CPP_STDLIBS = [ string_flag( name = "with_cxx_stdlib", - values = CPP_STDLIBS, build_setting_default = "best", + values = CPP_STDLIBS, ) cc_library( @@ -33,11 +33,11 @@ cc_library( "//conditions:default": [], }) + select({ ":set_cxx_stdlib_none": [], - + ### automatic selection ":set_cxx_stdlib_best": ["OPENTELEMETRY_STL_VERSION=(__cplusplus/100)"], # See https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus ":set_cxx_stdlib_best_and_msvc": ["OPENTELEMETRY_STL_VERSION=(_MSVC_LANG/100)"], - + ### manual selection ":set_cxx_stdlib_2014": ["OPENTELEMETRY_STL_VERSION=2014"], ":set_cxx_stdlib_2017": ["OPENTELEMETRY_STL_VERSION=2017"], ":set_cxx_stdlib_2020": ["OPENTELEMETRY_STL_VERSION=2020"], @@ -68,6 +68,6 @@ config_setting( config_setting( name = "set_cxx_stdlib_best_and_msvc", - flag_values = {":with_cxx_stdlib": "best"}, constraint_values = ["@bazel_tools//tools/cpp:msvc"], + flag_values = {":with_cxx_stdlib": "best"}, ) diff --git a/api/include/opentelemetry/version.h b/api/include/opentelemetry/version.h index fe3a228560..a3b527aa6d 100644 --- a/api/include/opentelemetry/version.h +++ b/api/include/opentelemetry/version.h @@ -7,9 +7,16 @@ #include "opentelemetry/detail/preprocessor.h" #if defined(OPENTELEMETRY_STL_VERSION) -# if (!defined(_MSVC_LANG) && OPENTELEMETRY_STL_VERSION > (__cplusplus/100)) || \ - (defined(_MSVC_LANG) && OPENTELEMETRY_STL_VERSION > (_MSVC_LANG/100)) -# pragma message OPENTELEMETRY_STRINGIFY(OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY(__cplusplus) +# if (!defined(_MSVC_LANG) && OPENTELEMETRY_STL_VERSION > (__cplusplus / 100)) || \ + (defined(_MSVC_LANG) && OPENTELEMETRY_STL_VERSION > (_MSVC_LANG / 100)) +# if defined(_MSVC_LANG) +# pragma message OPENTELEMETRY_STRINGIFY( \ + OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY(_MSVC_LANG) +# else +# pragma message OPENTELEMETRY_STRINGIFY( \ + OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY( \ + __cplusplus) +# endif # error "OPENTELEMETRY_STL_VERSION is set to a version newer than the curent C++ version." # endif #endif From d486cd71f1b7b8885f024420d24ea4a6a7497a47 Mon Sep 17 00:00:00 2001 From: bcsgh <33939446+bcsgh@users.noreply.github.com> Date: Thu, 18 Jan 2024 11:08:17 -0800 Subject: [PATCH 6/6] Make formatting (even more) happy. --- api/include/opentelemetry/version.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/include/opentelemetry/version.h b/api/include/opentelemetry/version.h index a3b527aa6d..b1c61ebff9 100644 --- a/api/include/opentelemetry/version.h +++ b/api/include/opentelemetry/version.h @@ -14,8 +14,7 @@ OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY(_MSVC_LANG) # else # pragma message OPENTELEMETRY_STRINGIFY( \ - OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY( \ - __cplusplus) + OPENTELEMETRY_STL_VERSION) " vs. " OPENTELEMETRY_STRINGIFY(__cplusplus) # endif # error "OPENTELEMETRY_STL_VERSION is set to a version newer than the curent C++ version." # endif