[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:30:33 PDT 2023


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

- make bazel load statement a one liner


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
@@ -48,6 +48,27 @@
 # 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}")
+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=...)")
 


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


More information about the libc-commits mailing list