[libc-commits] [libc] 4db99c8 - [libc] Add base for target config within cmake (#72318)

via libc-commits libc-commits at lists.llvm.org
Fri Nov 17 11:32:31 PST 2023


Author: michaelrj-google
Date: 2023-11-17T11:32:27-08:00
New Revision: 4db99c8b541592729cc0f8b2d1918bcb8c45f99f

URL: https://github.com/llvm/llvm-project/commit/4db99c8b541592729cc0f8b2d1918bcb8c45f99f
DIFF: https://github.com/llvm/llvm-project/commit/4db99c8b541592729cc0f8b2d1918bcb8c45f99f.diff

LOG: [libc] Add base for target config within cmake (#72318)

Currently the only way to add or remove entrypoints is to modify the
entrypoints.txt file for the current target. This isn't ideal since
a user would have to carry a diff for this file when updating their
checkout. This patch adds a basic mechanism to allow the user to remove
entrypoints without modifying the repository.

Added: 
    libc/cmake/modules/system_features/check_sys_random.cpp
    libc/config/linux/x86_64/exclude.txt

Modified: 
    libc/CMakeLists.txt
    libc/config/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 8eb3a465c20ba5a..9c82db754b5f730 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -103,12 +103,41 @@ option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
 
+option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
+
 set(LIBC_ENABLE_UNITTESTS ON)
 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
 
 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
 include(LLVMLibCArchitectures)
 
+set(LIBC_CONFIG_JSON_FILE_LIST "")
+
+if(NOT LIBC_CONFIG_PATH)
+  list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
+  if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
+    list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
+    set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
+  elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
+    set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
+  endif()
+else()
+  list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
+endif()
+
+if(NOT LIBC_CONFIG_PATH)
+  message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
+elseif(LIBC_CMAKE_VERBOSE_LOGGING)
+  message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
+endif()
+
+# option(LIBC_ENABLE_WIDE_CHARACTERS 
+# "Whether to enable wide character functions on supported platforms. This may
+# also set flags to enable or disable wide character support within other
+# functions (e.g. printf)." ON)
+
+#TODO: Add carve-out specific config files to the list here.
+
 include(LibcConfig)
 # Config loading happens in three steps:
 # 1. Load the config file config/config.json and set up config vars.
@@ -147,8 +176,14 @@ foreach(opt IN LISTS global_config)
   set(${opt_name} ${opt_value})
 endforeach()
 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
-load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/config.json ${cmd_line_conf})
-load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/config.json ${cmd_line_conf})
+
+# Load each target specific config.
+foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
+  if(LIBC_CMAKE_VERBOSE_LOGGING)
+    message(STATUS "Loading additional config: '${config_path}/config.json'")
+  endif()
+  load_libc_config(${config_path}/config.json ${cmd_line_conf})
+endforeach()
 
 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
   set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
@@ -248,21 +283,41 @@ include(LLVMLibCCheckCpuFeatures)
 include(CheckCompilerFeatures)
 include(LLVMLibCRules)
 
-if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
-  set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
-elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
-  set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
+set(TARGET_LLVMLIBC_ENTRYPOINTS "")
+set(TARGET_LIBC_ENTRYPOINTS "")
+set(TARGET_LIBM_ENTRYPOINTS "")
+set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
+
+# Check entrypoints.txt
+if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
+    include("${LIBC_CONFIG_PATH}/entrypoints.txt")
 else()
-  message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
+  message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
+endif()
+
+# Check headers.txt
+if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
+    include("${LIBC_CONFIG_PATH}/headers.txt")
+elseif(LLVM_LIBC_FULL_BUILD)
+  message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
 endif()
-include(${entrypoint_file})
 
-if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
-  include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
-elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
-  include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
+# Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
+if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
+    include("${LIBC_CONFIG_PATH}/exclude.txt")
 endif()
 
+# #TODO: Set up support for premade configs adding their own exclude lists.
+
+foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
+  if(LIBC_CMAKE_VERBOSE_LOGGING)
+    message(STATUS "Removing entrypoint ${removed_entrypoint}")
+  endif()
+  list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
+  list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
+  list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
+endforeach()
+
 set(TARGET_ENTRYPOINT_NAME_LIST "")
 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
   string(FIND ${entrypoint} "." last_dot_loc REVERSE)

diff  --git a/libc/cmake/modules/system_features/check_sys_random.cpp b/libc/cmake/modules/system_features/check_sys_random.cpp
new file mode 100644
index 000000000000000..7ab1949f0858d19
--- /dev/null
+++ b/libc/cmake/modules/system_features/check_sys_random.cpp
@@ -0,0 +1 @@
+#include <sys/random.h>

diff  --git a/libc/config/CMakeLists.txt b/libc/config/CMakeLists.txt
index a1034f9954740fe..853854b03be4e4e 100644
--- a/libc/config/CMakeLists.txt
+++ b/libc/config/CMakeLists.txt
@@ -1 +1,3 @@
+#TODO: Properly select the correct subdirectory.
+
 add_subdirectory(linux)

diff  --git a/libc/config/linux/x86_64/exclude.txt b/libc/config/linux/x86_64/exclude.txt
new file mode 100644
index 000000000000000..efe3eb9f4671b7e
--- /dev/null
+++ b/libc/config/linux/x86_64/exclude.txt
@@ -0,0 +1,21 @@
+# This optional file is used to exclude entrypoints/headers for specific targets.
+
+# Check if sys/random.h is available. If it isn't that implies we're on an older
+# version of linux, so we probably also don't have the statx syscall.
+try_compile(
+  has_sys_random
+  ${CMAKE_CURRENT_BINARY_DIR}
+  SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/system_features/check_sys_random.cpp
+)
+
+if(NOT has_sys_random)
+  list(APPEND TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS 
+    libc.src.sys.stat.stat
+  )
+  # If we're doing a fullbuild we provide the random header ourselves.
+  if(NOT LLVM_LIBC_FULL_BUILD)
+    list(APPEND TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS 
+      libc.src.sys.random.getrandom
+    )
+  endif()
+endif()


        


More information about the libc-commits mailing list