Skip to content

[libc] Add base for target config within cmake #72318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -247,21 +282,41 @@ include(CMakeParseArguments)
include(LLVMLibCCheckCpuFeatures)
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)
Expand Down
1 change: 1 addition & 0 deletions libc/cmake/modules/system_features/check_sys_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <sys/random.h>
2 changes: 2 additions & 0 deletions libc/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#TODO: Properly select the correct subdirectory.

add_subdirectory(linux)
21 changes: 21 additions & 0 deletions libc/config/linux/x86_64/exclude.txt
Original file line number Diff line number Diff line change
@@ -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()