[libc-commits] [PATCH] D159112: [libc] POC for namespace customization

Guillaume Chatelet via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Aug 31 06:34:34 PDT 2023


gchatelet updated this revision to Diff 555020.
gchatelet added a comment.

- fix auto-formatting


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159112/new/

https://reviews.llvm.org/D159112

Files:
  libc/CMakeLists.txt
  libc/src/__support/common.h
  utils/bazel/llvm-project-overlay/libc/BUILD.bazel


Index: utils/bazel/llvm-project-overlay/libc/BUILD.bazel
===================================================================
--- utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -12,6 +12,7 @@
 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")
+load("//:vars.bzl", "LLVM_VERSION_MAJOR", "LLVM_VERSION_MINOR", "LLVM_VERSION_PATCH")
 
 package(
     default_visibility = ["//visibility:public"],
@@ -34,6 +35,8 @@
     # "LIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING",
 ]
 
+llvm_libc_namespace = "__llvm_libc_{}_{}_{}_git".format(LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH)
+
 # A flag to pick which `mpfr` to use for math tests.
 # Usage: `-- at llvm-project//libc:mpfr=<disable|external|system>`.
 # Flag documentation: https://bazel.build/extending/config
@@ -69,6 +72,7 @@
 # paths of the kind "../../" to other libc targets.
 cc_library(
     name = "libc_root",
+    defines = ["LIBC_NAMESPACE=" + llvm_libc_namespace],
     includes = ["."],
 )
 
Index: libc/src/__support/common.h
===================================================================
--- libc/src/__support/common.h
+++ libc/src/__support/common.h
@@ -9,6 +9,10 @@
 #ifndef LLVM_LIBC_SUPPORT_COMMON_H
 #define LLVM_LIBC_SUPPORT_COMMON_H
 
+#ifndef LIBC_NAMESPACE
+#error "LIBC_NAMESPACE macro is not defined."
+#endif
+
 #include "src/__support/macros/attributes.h"
 #include "src/__support/macros/properties/architectures.h"
 
Index: libc/CMakeLists.txt
===================================================================
--- libc/CMakeLists.txt
+++ libc/CMakeLists.txt
@@ -44,10 +44,37 @@
 endif()
 
 option(LIBC_CMAKE_VERBOSE_LOGGING
+  "Log details warnings and notifications during CMake configuration." OFF)
+  "Log details warnings and notifications during CMake configuration." OFF)
+
        "Log details warnings and notifications during CMake configuration." OFF)
+
 # Path libc/scripts directory.
 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
 
+# Defining a global namespace to enclose all libc functions.
+set(LIBC_CMAKE_NAMESPACE "" CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'." FORCE)
+
+if(LIBC_CMAKE_NAMESPACE)
+  set(LLVM_LIBC_NAMESPACE "${LIBC_CMAKE_NAMESPACE}")
+else()
+  set(LLVM_LIBC_NAMESPACE "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}")
+endif()
+
+if(NOT LLVM_LIBC_NAMESPACE MATCHES "^__llvm_libc")
+  message(FATAL_ERROR "Invalid LLVM_LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LLVM_LIBC_NAMESPACE}'")
+endif()
+
+message(STATUS "Setting LLVM_LIBC_NAMESPACE namespace to '${LLVM_LIBC_NAMESPACE}'")
+
+if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
+  add_definitions("-DLIBC_NAMESPACE=${LLVM_LIBC_NAMESPACE}")
+elseif(MSVC)
+  add_definitions("/DLIBC_NAMESPACE=${LLVM_LIBC_NAMESPACE}")
+else()
+  message(FATAL_ERROR "Unsupported compiler")
+endif()
+
 # Flags to pass down to the compiler while building the libc functions.
 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
 
@@ -137,7 +164,11 @@
                       OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
       string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
       string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
+        "${CMAKE_CXX_COMPILER_VERSION}")
+        "${CMAKE_CXX_COMPILER_VERSION}")
+
              "${CMAKE_CXX_COMPILER_VERSION}")
+
       if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
         set(LLVM_LIBC_ENABLE_LINTING OFF)
         message(WARNING "


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159112.555020.patch
Type: text/x-patch
Size: 3777 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230831/46f4adea/attachment.bin>


More information about the libc-commits mailing list