diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -48,6 +48,21 @@ # Path libc/scripts directory. set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts") +# Defining a global namespace for 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}'") + # 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=...)") diff --git a/libc/cmake/modules/LLVMLibCCompileOptions.cmake b/libc/cmake/modules/LLVMLibCCompileOptions.cmake new file mode 100644 --- /dev/null +++ b/libc/cmake/modules/LLVMLibCCompileOptions.cmake @@ -0,0 +1,11 @@ +function(add_namespace_compile_option target_name) + if(NOT LLVM_LIBC_NAMESPACE) + message(FATAL_ERROR "LLVM_LIBC_NAMESPACE is not defined.") + endif() + + if(LLVM_COMPILER_IS_GCC_COMPATIBLE) + target_compile_options(${target_name} PRIVATE "-DLIBC_NAMESPACE=${LLVM_LIBC_NAMESPACE}") + elseif(MSVC) + target_compile_options(${target_name} PRIVATE "/DLIBC_NAMESPACE=${LLVM_LIBC_NAMESPACE}") + endif() +endfunction() diff --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake --- a/libc/cmake/modules/LLVMLibCObjectRules.cmake +++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake @@ -178,6 +178,7 @@ ${ADD_GPU_OBJ_HDRS} ) + add_namespace_compile_option(${gpu_target_name}) target_compile_options(${gpu_target_name} PRIVATE ${compile_options}) target_include_directories(${gpu_target_name} PRIVATE ${include_dirs}) target_compile_definitions(${gpu_target_name} PRIVATE LIBC_COPT_PUBLIC_PACKAGING) @@ -245,6 +246,7 @@ OBJECT ${CMAKE_CURRENT_BINARY_DIR}/stubs/${stub_filename} ) + add_namespace_compile_option(${fq_target_name}) target_compile_options(${fq_target_name} BEFORE PRIVATE ${common_compile_options} -nostdlib) foreach(packaged_gpu_binary ${packaged_gpu_binaries}) @@ -265,6 +267,7 @@ ${ADD_GPU_OBJ_SRCS} ${ADD_GPU_OBJ_HDRS} ) + add_namespace_compile_option(${internal_target_name}) target_compile_options(${internal_target_name} BEFORE PRIVATE ${common_compile_options} --target=${LIBC_GPU_TARGET_TRIPLE}) if(LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU) @@ -364,6 +367,7 @@ ${LIBC_INCLUDE_DIR} ) target_compile_options(${fq_target_name} PRIVATE ${compile_options}) + add_namespace_compile_option(${fq_target_name}) endif() if(SHOW_INTERMEDIATE_OBJECTS) @@ -644,6 +648,7 @@ ${ADD_ENTRYPOINT_OBJ_SRCS} ${ADD_ENTRYPOINT_OBJ_HDRS} ) + add_namespace_compile_option(${internal_target_name}) target_compile_options(${internal_target_name} BEFORE PRIVATE ${common_compile_options}) target_include_directories(${internal_target_name} PRIVATE ${include_dirs}) add_dependencies(${internal_target_name} ${full_deps_list}) @@ -657,6 +662,7 @@ ${ADD_ENTRYPOINT_OBJ_SRCS} ${ADD_ENTRYPOINT_OBJ_HDRS} ) + add_namespace_compile_option(${fq_target_name}) target_compile_options(${fq_target_name} BEFORE PRIVATE ${common_compile_options} -DLIBC_COPT_PUBLIC_PACKAGING) target_include_directories(${fq_target_name} PRIVATE ${include_dirs}) add_dependencies(${fq_target_name} ${full_deps_list}) @@ -902,4 +908,5 @@ ${target_name} BEFORE PRIVATE -fPIC ${LIBC_COMPILE_OPTIONS_DEFAULT} ) + add_namespace_compile_option(${target_name}) endfunction(add_redirector_object) diff --git a/libc/cmake/modules/LLVMLibCRules.cmake b/libc/cmake/modules/LLVMLibCRules.cmake --- a/libc/cmake/modules/LLVMLibCRules.cmake +++ b/libc/cmake/modules/LLVMLibCRules.cmake @@ -1,3 +1,4 @@ +include(LLVMLibCCompileOptions) include(LLVMLibCTargetNameUtils) include(LLVMLibCFlagRules) include(LLVMLibCObjectRules) diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -154,6 +154,7 @@ ${LIBC_SOURCE_DIR} ${LIBC_INCLUDE_DIR} ) + add_namespace_compile_option(${fq_build_target_name}) target_compile_options( ${fq_build_target_name} PRIVATE -fpie ${LIBC_COMPILE_OPTIONS_DEFAULT} @@ -407,6 +408,7 @@ ) add_dependencies(libc-fuzzer ${fq_target_name}) + add_namespace_compile_option(${fq_target_name}) target_compile_options(${fq_target_name} PRIVATE ${LIBC_FUZZER_COMPILE_OPTIONS}) @@ -522,6 +524,7 @@ ${LIBC_SOURCE_DIR} ${LIBC_INCLUDE_DIR} ) + add_namespace_compile_option(${fq_build_target_name}) target_compile_options(${fq_build_target_name} PRIVATE -fpie -ffreestanding -fno-exceptions -fno-rtti ${INTEGRATION_TEST_COMPILE_OPTIONS}) # The GPU build requires overriding the default CMake triple and architecture. @@ -665,6 +668,7 @@ PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_target_properties(${fq_target_name}.__libc__ PROPERTIES ARCHIVE_OUTPUT_NAME ${fq_target_name}.libc) + add_namespace_compile_option(${fq_target_name}.__libc__) set(fq_build_target_name ${fq_target_name}.__build__) add_executable( @@ -686,6 +690,7 @@ ${LIBC_SOURCE_DIR} ${LIBC_INCLUDE_DIR} ) + add_namespace_compile_option(${fq_build_target_name}) target_compile_options(${fq_build_target_name} PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} ${HERMETIC_TEST_COMPILE_OPTIONS}) diff --git a/libc/src/__support/common.h b/libc/src/__support/common.h --- a/libc/src/__support/common.h +++ b/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" diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt --- a/libc/test/UnitTest/CMakeLists.txt +++ b/libc/test/UnitTest/CMakeLists.txt @@ -27,9 +27,11 @@ ${TEST_LIB_HDRS} ) target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR}) + add_namespace_compile_option(${lib}) target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti) endforeach() target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include) + add_namespace_compile_option(${name}.hermetic) target_compile_options(${name}.hermetic PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} -ffreestanding -nostdinc++)