[libc][bazel] Mark all libc public functions to have "default" visibility.
authorSiva Chandra Reddy <sivachandra@google.com>
Thu, 8 Dec 2022 23:26:21 +0000 (23:26 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Mon, 12 Dec 2022 17:22:35 +0000 (17:22 +0000)
For the bazel build, we also build all source files with
-fvisibility=hidden.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D139678

utils/bazel/llvm-project-overlay/libc/BUILD.bazel
utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl

index 3565c0d..f0e0af8 100644 (file)
@@ -3,7 +3,8 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 # LLVM libc project.
-load(":libc_build_rules.bzl", "libc_function", "libc_math_function")
+load(":libc_build_rules.bzl", "libc_function", "libc_math_function",
+     "libc_support_library")
 load(":platforms.bzl", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_X86_64")
 load("@bazel_skylib//lib:selects.bzl", "selects")
 load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
@@ -55,7 +56,7 @@ cc_library(
 
 ############################## Support libraries #############################
 
-cc_library(
+libc_support_library(
     name = "__support_common",
     hdrs = [
         "src/__support/architectures.h",
@@ -65,25 +66,25 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_array",
     hdrs = ["src/__support/CPP/array.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_bit",
     hdrs = ["src/__support/CPP/bit.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_bitset",
     hdrs = ["src/__support/CPP/bitset.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_cstddef",
     hdrs = ["src/__support/CPP/cstddef.h"],
     deps = [
@@ -92,25 +93,25 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_functional",
     hdrs = ["src/__support/CPP/functional.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_limits",
     hdrs = ["src/__support/CPP/limits.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_optional",
     hdrs = ["src/__support/CPP/optional.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_span",
     hdrs = ["src/__support/CPP/span.h"],
     deps = [
@@ -120,19 +121,19 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_string_view",
     hdrs = ["src/__support/CPP/string_view.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_type_traits",
     hdrs = ["src/__support/CPP/type_traits.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_utility",
     hdrs = ["src/__support/CPP/utility.h"],
     deps = [
@@ -141,13 +142,13 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_vector",
     hdrs = ["src/__support/CPP/vector.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_cpp_atomic",
     hdrs = ["src/__support/CPP/atomic.h"],
     deps = [
@@ -156,7 +157,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_number_pair",
     hdrs = ["src/__support/number_pair.h"],
     deps = [
@@ -166,7 +167,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_integer_utils",
     hdrs = ["src/__support/integer_utils.h"],
     deps = [
@@ -177,7 +178,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_uint",
     hdrs = ["src/__support/UInt.h"],
     deps = [
@@ -192,7 +193,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_uint128",
     hdrs = ["src/__support/UInt128.h"],
     deps = [
@@ -201,7 +202,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_str_to_num_result",
     hdrs = ["src/__support/str_to_num_result.h"],
     deps = [
@@ -209,18 +210,18 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_integer_operations",
     hdrs = ["src/__support/integer_operations.h"],
     deps = [":__support_cpp_type_traits"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_ctype_utils",
     hdrs = ["src/__support/ctype_utils.h"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_str_to_integer",
     hdrs = ["src/__support/str_to_integer.h"],
     deps = [
@@ -230,7 +231,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_basic_operations",
     hdrs = ["src/__support/FPUtil/BasicOperations.h"],
     deps = [
@@ -240,13 +241,13 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_named_pair",
     hdrs = ["src/__support/named_pair.h"],
     deps = [":libc_root"],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_builtin_wrappers",
     hdrs = ["src/__support/builtin_wrappers.h"],
     deps = [
@@ -256,7 +257,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_division_and_remainder_operations",
     hdrs = ["src/__support/FPUtil/DivisionAndRemainderOperations.h"],
     deps = [
@@ -268,7 +269,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_except_value_utils",
     hdrs = ["src/__support/FPUtil/except_value_utils.h"],
     deps = [
@@ -279,7 +280,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_fenv_impl",
     hdrs = ["src/__support/FPUtil/FEnvImpl.h"],
     textual_hdrs = [
@@ -294,7 +295,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_float_properties",
     hdrs = ["src/__support/FPUtil/FloatProperties.h"],
     deps = [
@@ -304,7 +305,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_fp_bits",
     hdrs = ["src/__support/FPUtil/FPBits.h"],
     textual_hdrs = ["src/__support/FPUtil/x86_64/LongDoubleBits.h"],
@@ -320,7 +321,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_hypot",
     hdrs = ["src/__support/FPUtil/Hypot.h"],
     deps = [
@@ -335,7 +336,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_manipulation_functions",
     hdrs = ["src/__support/FPUtil/ManipulationFunctions.h"],
     textual_hdrs = ["src/__support/FPUtil/x86_64/NextAfterLongDouble.h"],
@@ -352,7 +353,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_nearest_integer_operations",
     hdrs = ["src/__support/FPUtil/NearestIntegerOperations.h"],
     deps = [
@@ -363,7 +364,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_normal_float",
     hdrs = ["src/__support/FPUtil/NormalFloat.h"],
     deps = [
@@ -373,7 +374,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_platform_defs",
     hdrs = ["src/__support/FPUtil/PlatformDefs.h"],
     deps = [
@@ -398,7 +399,7 @@ sqrt_hdrs = selects.with_or({
     ],
 })
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_sqrt",
     hdrs = sqrt_hdrs,
     deps = [
@@ -424,7 +425,7 @@ fma_platform_hdrs = [
     "src/__support/FPUtil/aarch64/FMA.h",
 ]
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_fma",
     hdrs = fma_common_hdrs,
     # These are conditionally included and will #error out if the platform
@@ -443,7 +444,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_multiply_add",
     hdrs = [
         "src/__support/FPUtil/multiply_add.h",
@@ -454,7 +455,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_polyeval",
     hdrs = [
         "src/__support/FPUtil/PolyEval.h",
@@ -473,7 +474,7 @@ nearest_integer_platform_hdrs = [
     "src/__support/FPUtil/aarch64/nearest_integer.h",
 ]
 
-cc_library(
+libc_support_library(
     name = "__support_fputil_nearest_integer",
     hdrs = nearest_integer_common_hdrs,
     # These are conditionally included and will #error out if the platform
@@ -486,7 +487,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "__support_osutil",
     hdrs = ["src/__support/OSUtil/syscall.h"],
     textual_hdrs = [
@@ -644,7 +645,7 @@ libc_function(
 
 ################################ math targets ################################
 
-cc_library(
+libc_support_library(
     name = "math_utils",
     srcs = ["src/math/generic/math_utils.cpp"],
     hdrs = ["src/math/generic/math_utils.h"],
@@ -656,7 +657,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "common_constants",
     srcs = ["src/math/generic/common_constants.cpp"],
     hdrs = ["src/math/generic/common_constants.h"],
@@ -665,7 +666,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "range_reduction",
     hdrs = [
         "src/math/generic/range_reduction.h",
@@ -680,7 +681,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "sincosf_utils",
     hdrs = ["src/math/generic/sincosf_utils.h"],
     deps = [
@@ -1052,7 +1053,7 @@ no_sanitize_features = [
     "-ubsan",
 ]
 
-cc_library(
+libc_support_library(
     name = "string_memory_utils",
     hdrs = [
         "src/string/memory_utils/op_aarch64.h",
@@ -1079,7 +1080,7 @@ cc_library(
     ],
 )
 
-cc_library(
+libc_support_library(
     name = "string_utils",
     hdrs = ["src/string/string_utils.h"],
     deps = [
index f960d97..14b1dbd 100644 (file)
@@ -10,7 +10,33 @@ load("@bazel_skylib//lib:selects.bzl", "selects")
 LIBC_ROOT_TARGET = ":libc_root"
 INTERNAL_SUFFIX = ".__internal__"
 
-def libc_function(name, srcs, weak = False, deps = None, copts = None, **kwargs):
+def _libc_library(name, copts = None, **kwargs):
+    """Internal macro to serve as a base for all other libc library rules.
+
+    Args:
+      name: Target name.
+      copts: The special compiler options for the target.
+      **kwargs: All other attributes relevant for the cc_library rule.
+    """
+    copts = copts or []
+    # We want all libc sources to be compiled with "hidden" visibility.
+    # The public symbols will be given "default" visibility explicitly.
+    # See src/__support/common.h for more information.
+    copts.append("-fvisibility=hidden")
+    native.cc_library(
+        name = name,
+        copts = copts,
+        linkstatic = 1,
+        **kwargs
+    )
+
+# A convenience var which should be used to list all libc support libraries.
+# Any library which does not define a public function should be listed with
+# libc_support_library.
+libc_support_library = _libc_library
+
+def libc_function(name, srcs, weak = False, deps = None, copts = None,
+                  local_defines = None, **kwargs):
     """Add target for a libc function.
 
     The libc function is eventually available as a cc_library target by name
@@ -23,9 +49,12 @@ def libc_function(name, srcs, weak = False, deps = None, copts = None, **kwargs)
       name: Target name. It is normally the name of the function this target is
             for.
       srcs: The .cpp files which contain the function implementation.
-      weak: Whether the symbol is marked weak.
+      weak: Make the symbol corresponding to the libc function "weak".
       deps: The list of target dependencies if any.
       copts: The list of options to add to the C++ compilation command.
+      local_defines: The preprocessor defines which will be prepended with -D
+                     and passed to the compile command of this target but not
+                     its deps.
       **kwargs: Other attributes relevant for a cc_library. For example, deps.
     """
     deps = deps or []
@@ -47,15 +76,18 @@ def libc_function(name, srcs, weak = False, deps = None, copts = None, **kwargs)
     )
 
     # This second target is the llvm libc C function.
-    copts.append("-DLLVM_LIBC_PUBLIC_PACKAGING")
+
+    func_attrs = ["__attribute__((visibility(\"default\")))"]
     if weak:
-        copts.append("-DLLVM_LIBC_FUNCTION_ATTR=__attribute__((weak))")
-    native.cc_library(
+        func_attrs.append("__attribute__((weak))")
+    local_defines = local_defines or ["LLVM_LIBC_PUBLIC_PACKAGING"]
+    local_defines.append("LLVM_LIBC_FUNCTION_ATTR='%s'" % ' '.join(func_attrs))
+    _libc_library(
         name = name,
         srcs = srcs,
         deps = deps,
         copts = copts,
-        linkstatic = 1,
+        local_defines = local_defines,
         **kwargs
     )