Skip to content

Commit 4db99c8

Browse files
[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.
1 parent a1ae7e9 commit 4db99c8

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

libc/CMakeLists.txt

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,41 @@ option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
103103
option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
104104
set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
105105

106+
option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
107+
106108
set(LIBC_ENABLE_UNITTESTS ON)
107109
set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
108110

109111
# Defines LIBC_TARGET_ARCHITECTURE and associated macros.
110112
include(LLVMLibCArchitectures)
111113

114+
set(LIBC_CONFIG_JSON_FILE_LIST "")
115+
116+
if(NOT LIBC_CONFIG_PATH)
117+
list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
118+
if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
119+
list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
120+
set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
121+
elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
122+
set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
123+
endif()
124+
else()
125+
list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
126+
endif()
127+
128+
if(NOT LIBC_CONFIG_PATH)
129+
message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
130+
elseif(LIBC_CMAKE_VERBOSE_LOGGING)
131+
message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
132+
endif()
133+
134+
# option(LIBC_ENABLE_WIDE_CHARACTERS
135+
# "Whether to enable wide character functions on supported platforms. This may
136+
# also set flags to enable or disable wide character support within other
137+
# functions (e.g. printf)." ON)
138+
139+
#TODO: Add carve-out specific config files to the list here.
140+
112141
include(LibcConfig)
113142
# Config loading happens in three steps:
114143
# 1. Load the config file config/config.json and set up config vars.
@@ -147,8 +176,14 @@ foreach(opt IN LISTS global_config)
147176
set(${opt_name} ${opt_value})
148177
endforeach()
149178
generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
150-
load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/config.json ${cmd_line_conf})
151-
load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/config.json ${cmd_line_conf})
179+
180+
# Load each target specific config.
181+
foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
182+
if(LIBC_CMAKE_VERBOSE_LOGGING)
183+
message(STATUS "Loading additional config: '${config_path}/config.json'")
184+
endif()
185+
load_libc_config(${config_path}/config.json ${cmd_line_conf})
186+
endforeach()
152187

153188
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
154189
set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
@@ -248,21 +283,41 @@ include(LLVMLibCCheckCpuFeatures)
248283
include(CheckCompilerFeatures)
249284
include(LLVMLibCRules)
250285

251-
if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
252-
set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
253-
elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
254-
set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
286+
set(TARGET_LLVMLIBC_ENTRYPOINTS "")
287+
set(TARGET_LIBC_ENTRYPOINTS "")
288+
set(TARGET_LIBM_ENTRYPOINTS "")
289+
set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
290+
291+
# Check entrypoints.txt
292+
if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
293+
include("${LIBC_CONFIG_PATH}/entrypoints.txt")
255294
else()
256-
message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
295+
message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
296+
endif()
297+
298+
# Check headers.txt
299+
if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
300+
include("${LIBC_CONFIG_PATH}/headers.txt")
301+
elseif(LLVM_LIBC_FULL_BUILD)
302+
message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
257303
endif()
258-
include(${entrypoint_file})
259304

260-
if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
261-
include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
262-
elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
263-
include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
305+
# Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
306+
if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
307+
include("${LIBC_CONFIG_PATH}/exclude.txt")
264308
endif()
265309

310+
# #TODO: Set up support for premade configs adding their own exclude lists.
311+
312+
foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
313+
if(LIBC_CMAKE_VERBOSE_LOGGING)
314+
message(STATUS "Removing entrypoint ${removed_entrypoint}")
315+
endif()
316+
list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
317+
list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
318+
list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
319+
endforeach()
320+
266321
set(TARGET_ENTRYPOINT_NAME_LIST "")
267322
foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
268323
string(FIND ${entrypoint} "." last_dot_loc REVERSE)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <sys/random.h>

libc/config/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
#TODO: Properly select the correct subdirectory.
2+
13
add_subdirectory(linux)

libc/config/linux/x86_64/exclude.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This optional file is used to exclude entrypoints/headers for specific targets.
2+
3+
# Check if sys/random.h is available. If it isn't that implies we're on an older
4+
# version of linux, so we probably also don't have the statx syscall.
5+
try_compile(
6+
has_sys_random
7+
${CMAKE_CURRENT_BINARY_DIR}
8+
SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/system_features/check_sys_random.cpp
9+
)
10+
11+
if(NOT has_sys_random)
12+
list(APPEND TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS
13+
libc.src.sys.stat.stat
14+
)
15+
# If we're doing a fullbuild we provide the random header ourselves.
16+
if(NOT LLVM_LIBC_FULL_BUILD)
17+
list(APPEND TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS
18+
libc.src.sys.random.getrandom
19+
)
20+
endif()
21+
endif()

0 commit comments

Comments
 (0)