From 7b3ac075177212dd75476253d72db992513e8024 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Wed, 20 Jul 2022 20:34:39 +0100 Subject: [PATCH] Stop generating the export header and just check it in (#1435) * Stop generating the export header and just check it in * format the new header * support windows * format the header again * avoid depending on internal macro * ensure we define the right thing for windows static builds * support older cmake * and for tests --- BUILD.bazel | 9 -- CMakeLists.txt | 1 - LICENSE | 32 ------ config/generate_export_header.bzl | 168 ------------------------------ include/benchmark/export.h | 47 +++++++++ src/CMakeLists.txt | 7 +- test/CMakeLists.txt | 4 + 7 files changed, 55 insertions(+), 213 deletions(-) delete mode 100644 config/generate_export_header.bzl create mode 100644 include/benchmark/export.h diff --git a/BUILD.bazel b/BUILD.bazel index 872adb0f..16215047 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,14 +1,5 @@ licenses(["notice"]) -load("//:config/generate_export_header.bzl", "generate_export_header") - -# Generate header to provide ABI export symbols -generate_export_header( - out = "include/benchmark/export.h", - lib = "benchmark", - static_define = "BENCHMARK_STATIC_DEFINE", -) - config_setting( name = "qnx", constraint_values = ["@platforms//os:qnx"], diff --git a/CMakeLists.txt b/CMakeLists.txt index f8c86c3f..a44613e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,6 @@ include(AddCXXCompilerFlag) include(CheckCXXCompilerFlag) include(CheckLibraryExists) include(CXXFeatureCheck) -include(GenerateExportHeader) check_library_exists(rt shm_open "" HAVE_LIB_RT) diff --git a/LICENSE b/LICENSE index a5c40b3e..d6456956 100644 --- a/LICENSE +++ b/LICENSE @@ -200,35 +200,3 @@ 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. - - -Only benchmark/config/generate_export_header.bzl depends on the following licence: - - BSD 3-Clause License - -Copyright (c) [year], [fullname] - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/config/generate_export_header.bzl b/config/generate_export_header.bzl deleted file mode 100644 index bf98092d..00000000 --- a/config/generate_export_header.bzl +++ /dev/null @@ -1,168 +0,0 @@ -# -# Original file is located at: -# https://github.com/RobotLocomotion/drake/blob/bad032aeb09b13c7f8c87ed64b624c8d1e9adb30/tools/workspace/generate_export_header.bzl -# -# All components of Drake are licensed under the BSD 3-Clause License -# shown below. Where noted in the source code, some portions may -# be subject to other permissive, non-viral licenses. -# -# Copyright 2012-2016 Robot Locomotion Group @ CSAIL -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# Redistributions of source code must retain the above copyright notice, -# this list of conditions and the following disclaimer. Redistributions -# in binary form must reproduce the above copyright notice, this list of -# conditions and the following disclaimer in the documentation and/or -# other materials provided with the distribution. Neither the name of -# the Massachusetts Institute of Technology nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# -*- python -*- - -# Defines the implementation actions to generate_export_header. -def _generate_export_header_impl(ctx): - windows_constraint = ctx.attr._windows_constraint[platform_common.ConstraintValueInfo] - output = ctx.outputs.out - - if ctx.target_platform_has_constraint(windows_constraint): - export_attr = "__declspec(dllexport)" - import_attr = "__declspec(dllimport)" - no_export_attr = "" - deprecated_attr = "__declspec(deprecated)" - else: - export_attr = "__attribute__((visibility(\"default\")))" - import_attr = "__attribute__((visibility(\"default\")))" - no_export_attr = "__attribute__((visibility(\"hidden\")))" - deprecated_attr = "__attribute__((__deprecated__))" - - content = [ - "#ifndef %s_H" % ctx.attr.export_macro_name, - "#define %s_H" % ctx.attr.export_macro_name, - "", - "#ifdef %s" % ctx.attr.static_define, - "# define %s" % ctx.attr.export_macro_name, - "# define %s" % ctx.attr.no_export_macro_name, - "#else", - "# ifndef %s" % ctx.attr.export_macro_name, - "# ifdef %s" % ctx.attr.export_import_condition, - "# define %s %s" % (ctx.attr.export_macro_name, export_attr), - "# else", - "# define %s %s" % (ctx.attr.export_macro_name, import_attr), - "# endif", - "# endif", - "# ifndef %s" % ctx.attr.no_export_macro_name, - "# define %s %s" % (ctx.attr.no_export_macro_name, no_export_attr), - "# endif", - "#endif", - "", - "#ifndef %s" % ctx.attr.deprecated_macro_name, - "# define %s %s" % (ctx.attr.deprecated_macro_name, deprecated_attr), - "#endif", - "", - "#ifndef %s" % ctx.attr.export_deprecated_macro_name, - "# define %s %s %s" % (ctx.attr.export_deprecated_macro_name, ctx.attr.export_macro_name, ctx.attr.deprecated_macro_name), # noqa - "#endif", - "", - "#ifndef %s" % ctx.attr.no_export_deprecated_macro_name, - "# define %s %s %s" % (ctx.attr.no_export_deprecated_macro_name, ctx.attr.no_export_macro_name, ctx.attr.deprecated_macro_name), # noqa - "#endif", - "", - "#endif", - ] - - ctx.actions.write(output = output, content = "\n".join(content) + "\n") - -# Defines the rule to generate_export_header. -_generate_export_header_gen = rule( - attrs = { - "out": attr.output(mandatory = True), - "export_import_condition": attr.string(), - "export_macro_name": attr.string(), - "deprecated_macro_name": attr.string(), - "export_deprecated_macro_name": attr.string(), - "no_export_macro_name": attr.string(), - "no_export_deprecated_macro_name": attr.string(), - "static_define": attr.string(), - "_windows_constraint": attr.label(default = "@platforms//os:windows"), - }, - output_to_genfiles = True, - implementation = _generate_export_header_impl, -) - -def generate_export_header( - lib = None, - name = None, - out = None, - export_import_condition = None, - export_macro_name = None, - deprecated_macro_name = None, - export_deprecated_macro_name = None, - no_export_macro_name = None, - no_export_deprecated_macro_name = None, - static_define = None, - **kwargs): - """ - Creates a rule to generate an export header for a named library. - - This is an incomplete implementation of CMake's generate_export_header. (In - particular, it assumes a platform that uses - __attribute__((visibility("default"))) to decorate exports.) - - By default, the rule will have a mangled name related to the library name, - and will produce "_export.h". - - The CMake documentation of the generate_export_header macro is: - https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html - - """ - - if name == None: - name = "__%s_export_h" % lib - if out == None: - out = "%s_export.h" % lib - if export_import_condition == None: - # CMake does not uppercase the _EXPORTS define. - export_import_condition = "%s_EXPORTS" % lib - if export_macro_name == None: - export_macro_name = "%s_EXPORT" % lib.upper() - if deprecated_macro_name == None: - deprecated_macro_name = "%s_DEPRECATED" % lib.upper() - if export_deprecated_macro_name == None: - export_deprecated_macro_name = "%s_DEPRECATED_EXPORT" % lib.upper() - if no_export_macro_name == None: - no_export_macro_name = "%s_NO_EXPORT" % lib.upper() - if no_export_deprecated_macro_name == None: - no_export_deprecated_macro_name = \ - "%s_DEPRECATED_NO_EXPORT" % lib.upper() - if static_define == None: - static_define = "%s_STATIC_DEFINE" % lib.upper() - - _generate_export_header_gen( - name = name, - out = out, - export_import_condition = export_import_condition, - export_macro_name = export_macro_name, - deprecated_macro_name = deprecated_macro_name, - export_deprecated_macro_name = export_deprecated_macro_name, - no_export_macro_name = no_export_macro_name, - no_export_deprecated_macro_name = no_export_deprecated_macro_name, - static_define = static_define, - **kwargs - ) diff --git a/include/benchmark/export.h b/include/benchmark/export.h new file mode 100644 index 00000000..f96f8596 --- /dev/null +++ b/include/benchmark/export.h @@ -0,0 +1,47 @@ +#ifndef BENCHMARK_EXPORT_H +#define BENCHMARK_EXPORT_H + +#if defined(_WIN32) +#define EXPORT_ATTR __declspec(dllexport) +#define IMPORT_ATTR __declspec(dllimport) +#define NO_EXPORT_ATTR +#define DEPRECATED_ATTR __declspec(deprecated) +#else // _WIN32 +#define EXPORT_ATTR __attribute__((visibility("default"))) +#define IMPORT_ATTR __attribute__((visibility("default"))) +#define NO_EXPORT_ATTR __attribute__((visibility("hidden"))) +#define DEPRECATE_ATTR __attribute__((__deprecated__)) +#endif // _WIN32 + +#ifdef BENCHMARK_STATIC_DEFINE +#define BENCHMARK_EXPORT +#define BENCHMARK_NO_EXPORT +#else // BENCHMARK_STATIC_DEFINE +#ifndef BENCHMARK_EXPORT +#ifdef benchmark_EXPORTS +/* We are building this library */ +#define BENCHMARK_EXPORT EXPORT_ATTR +#else // benchmark_EXPORTS +/* We are using this library */ +#define BENCHMARK_EXPORT IMPORT_ATTR +#endif // benchmark_EXPORTS +#endif // !BENCHMARK_EXPORT + +#ifndef BENCHMARK_NO_EXPORT +#define BENCHMARK_NO_EXPORT NO_EXPORT_ATTR +#endif // !BENCHMARK_NO_EXPORT +#endif // BENCHMARK_STATIC_DEFINE + +#ifndef BENCHMARK_DEPRECATED +#define BENCHMARK_DEPRECATED DEPRECATE_ATTR +#endif // BENCHMARK_DEPRECATED + +#ifndef BENCHMARK_DEPRECATED_EXPORT +#define BENCHMARK_DEPRECATED_EXPORT BENCHMARK_EXPORT BENCHMARK_DEPRECATED +#endif // BENCHMARK_DEPRECATED_EXPORT + +#ifndef BENCHMARK_DEPRECATED_NO_EXPORT +#define BENCHMARK_DEPRECATED_NO_EXPORT BENCHMARK_NO_EXPORT BENCHMARK_DEPRECATED +#endif // BENCHMARK_DEPRECATED_EXPORT + +#endif /* BENCHMARK_EXPORT_H */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 05ea0238..3961f816 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,9 +29,6 @@ target_include_directories(benchmark PUBLIC $ ) -generate_export_header(benchmark - EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/include/benchmark/export.h) - # libpfm, if available if (HAVE_LIBPFM) target_link_libraries(benchmark PRIVATE pfm) @@ -58,6 +55,10 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") target_link_libraries(benchmark PRIVATE kstat) endif() +if (NOT BUILD_SHARED_LIBS) + add_definitions(-DBENCHMARK_STATIC_DEFINE) +endif() + # Benchmark main library add_library(benchmark_main "benchmark_main.cc") add_library(benchmark::benchmark_main ALIAS benchmark_main) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d528ee94..a49ab195 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,6 +24,10 @@ if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) endforeach() endif() +if (NOT BUILD_SHARED_LIBS) + add_definitions(-DBENCHMARK_STATIC_DEFINE) +endif() + check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG) set(BENCHMARK_O3_FLAG "") if (BENCHMARK_HAS_O3_FLAG)