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

Conversation

michaelrj-google
Copy link
Contributor

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.

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.
Instead of having a user-provided file that modifies the list of
entrypoints, now the user-provided file is used instead of the list of
entrypoints.
@lntue lntue added the libc label Nov 15, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2023

@llvm/pr-subscribers-libc

Author: None (michaelrj-google)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/72318.diff

2 Files Affected:

  • (modified) libc/CMakeLists.txt (+35-11)
  • (modified) libc/config/CMakeLists.txt (+2)
diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 414be906336bf3f..4460820d86973b3 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -103,6 +103,8 @@ 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_SYSTEM_CONFIG_FILE "The path to user provided cmake file that configures the build for the target system." OFF)
+
 set(LIBC_ENABLE_UNITTESTS ON)
 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
 
@@ -247,21 +249,43 @@ 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")
+# If the user wants to provide their own list of entrypoints and headers
+if(LIBC_SYSTEM_CONFIG_FILE)
+  # Use the file provided by the user if it exists
+  if(EXISTS "${LIBC_SYSTEM_CONFIG_FILE}")
+    include("${LIBC_SYSTEM_CONFIG_FILE}")
+  else()
+    message(FATAL_ERROR "System Config File set to unavailable file '${LIBC_SYSTEM_CONFIG_FILE}'")
+  endif()
 else()
-  message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
-endif()
-include(${entrypoint_file})
+  # Else use the files in config/OS/CPU/ or config/OS/
+  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")
+  else()
+    message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
+  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")
+  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")
+  endif()
 endif()
 
+# #TODO: Set up support for premade configs.
+
+# 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/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)

Comment on lines 254 to 288
# Use the file provided by the user if it exists
if(EXISTS "${LIBC_SYSTEM_CONFIG_FILE}")
include("${LIBC_SYSTEM_CONFIG_FILE}")
else()
message(FATAL_ERROR "System Config File set to unavailable file '${LIBC_SYSTEM_CONFIG_FILE}'")
endif()
else()
# Else use the files in config/OS/CPU/ or config/OS/
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")
else()
message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
endif()
include(${entrypoint_file})

#TODO: Set up support for premade configs.

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()
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")
endif()
endif()

# #TODO: Set up support for premade configs.

# 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()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT of the following reorg:

if(NOT LIBC_CONFIG_PATH)
  if(EXISTS "${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()

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()

# Check entrypoints.txt
if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
    set(entrypoint_file "${LIBC_CONFIG_PATH}/entrypoints.txt")
else()
  message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
endif()

# Check header.txt
if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
    include("${LIBC_CONFIG_PATH}/headers.txt")
endif()

# Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
    include("${LIBC_CONFIG_PATH}/exclude.txt")
endif()

// Remove entry points:
foreach(removed_entrypoint IN LISTS LIBC_EXCLUDE_ENTRYPOINTS)
   ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did something similar to this, though I had to reorder it a bunch to handle the config files.

Instead of the user only providing one file, they now provide a file
with entrypoints, headers, and the new exclude file. It also now
supports them providing their own config.json and properly uses it
instead of the provided ones for the given target.
Comment on lines 3 to 7
try_compile(
has_sys_random
${CMAKE_CURRENT_BINARY_DIR}
SOURCES ${LIBC_SOURCE_DIR}/src/sys/random/linux/getrandom.cpp
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only do this in overlay mode right? Since we do know whether the header exists or not in full build mode.

Copy link
Contributor

@lntue lntue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So currently we (potentially) include multiple config.json files. I think each build should only has 1 single config.json, similar to entrypoints.txt, and should be put in the same path. If multiple architectures on the same OS want to share some configs, then they should explicitly include the shared config.json inside their own config.json.

Another the thing we should do is probably refactor all of these logic into LibcConfig file.

All of these improvements can be done in the followup changes. This PR is good to me as it is to enable the feature.

I had to add a new folder for files to use try_compile on.
@michaelrj-google michaelrj-google marked this pull request as ready for review November 16, 2023 22:36
@michaelrj-google michaelrj-google merged commit 4db99c8 into llvm:main Nov 17, 2023
@michaelrj-google michaelrj-google deleted the libcUserConfig branch November 17, 2023 19:32
sr-tream pushed a commit to sr-tream/llvm-project that referenced this pull request Nov 20, 2023
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.
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants