Skip to content

[libc] Find Python 3 in standalone #118871

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

Closed

Conversation

RossComputerGuy
Copy link
Member

Python3_Executable wasn't defined when libc is trying to be built in standalone mode. Also adds a project define so CMake quits complaining.

@llvmbot llvmbot added the libc label Dec 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2024

@llvm/pr-subscribers-libc

Author: Tristan Ross (RossComputerGuy)

Changes

Python3_Executable wasn't defined when libc is trying to be built in standalone mode. Also adds a project define so CMake quits complaining.


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

1 Files Affected:

  • (modified) libc/CMakeLists.txt (+12)
diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 5196735bef4e76..cfe3e347980a37 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -8,6 +8,13 @@ endif()
 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
   NO_POLICY_SCOPE)
 
+# If we are not building as a part of LLVM, build libc as an
+# standalone project, using LLVM as an external library:
+if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
+  project(libc)
+  set(LIBC_BUILT_STANDALONE TRUE)
+endif()
+
 if (LIBC_CMAKE_VERBOSE_LOGGING)
   get_directory_property(LIBC_OLD_PREPROCESSOR_DEFS COMPILE_DEFINITIONS)
   foreach(OLD_DEF ${LIBC_OLD_PREPROCESSOR_DEFS})
@@ -30,6 +37,11 @@ endif()
 # Default to C++17
 set(CMAKE_CXX_STANDARD 17)
 
+if(LIBC_BUILT_STANDALONE)
+  find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
+    COMPONENTS Interpreter)
+endif()
+
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
 
 # The top-level source directory.

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

Isn't there some external use of libc more standalone? I'll defer to @nickdesaulniers.

@nickdesaulniers
Copy link
Member

Thanks for the patch! How are you invoking the build @RossComputerGuy ? (i.e. what explicit command line commands are you running)?

@RossComputerGuy
Copy link
Member Author

I'm running with these flags:

-GNinja -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/share/doc/libc -DCMAKE_INSTALL_INFODIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/include -DCMAKE_INSTALL_SBINDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/2r2xi5pbg29bsmqywsm5zgl8l7adky4i-gcc-wrapper-13.3.0/bin/strip -DCMAKE_RANLIB=/nix/store/2r2xi5pbg29bsmqywsm5zgl8l7adky4i-gcc-wrapper-13.3.0/bin/ranlib -DCMAKE_AR=/nix/store/2r2xi5pbg29bsmqywsm5zgl8l7adky4i-gcc-wrapper-13.3.0/bin/ar -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=/nix/store/lfasxjrpvnnwd8imf3wfwf784mkirc3w-libc-20.0.0-unstable-2024-12-05 -DLLVM_LIBC_FULL_BUILD:BOOL=TRUE -DPython3_EXECUTABLE:STRING=/nix/store/imz4prdh762l7jvwak2s7pqpq6hy5fyy-python3-3.12.7/bin/python3.12

I have to specify Python3_EXECUTABLE since it tries executing the yaml_to_classes.py script in a way that makes Nix not happy.

@nickdesaulniers
Copy link
Member

What's the full cmake command? Specifically, what directory are you using as the root for cmake?

@RossComputerGuy
Copy link
Member Author

I'm not sure how to extract the full on CMake command but I know it runs with cmake ... That command is ran inside libc/build which that directory is created.

Comment on lines 40 to 46
if(LIBC_BUILT_STANDALONE)
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
COMPONENTS Interpreter)
endif()
Copy link
Contributor

Choose a reason for hiding this comment

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

we also need pyyaml for newhdrgen. Not sure if that needs to be specified in the cmake, but adding a comment at least would be useful.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add a comment about it at least, I'm not sure how we could add a check inside CMake to ensure the module exists.

@nickdesaulniers
Copy link
Member

Right, so in #78479, I'm trying to move to only documenting+supporting using llvm-project/runtimes dir as the root for cmake for building llvm-libc.

It sounds like you (and @SchrodingerZhu ) are trying to use llvm-project/libc/ as the root for cmake for building llvm-libc.

Is there a reason why you cannot use llvm-project/runtimes/ as the root for cmake for llvm-libc?

i.e.

$ cd llvm-project
$ mkdir build
$ cd build
$ cmake ../runtimes ...
<rather than>
$ cmake ../libc ...

@RossComputerGuy
Copy link
Member Author

Oh, I wasn't aware of that change. I'm not sure what would be stopping Nixpkgs from using that once it's merged. Would this allow for building completely separate from the rest of things just like what is possible with llvm, clang, and the various other projects inside of LLVM?

@nickdesaulniers
Copy link
Member

Would this allow for building completely separate from the rest of things just like what is possible with llvm, clang, and the various other projects inside of LLVM?

Yes. We recently removed a dependency on tablegen, which implied a depedency on llvm. This should let us bootstrap clang itself from the libc (avoiding having to build llvm twice, one for libc, again for clang).

@SchrodingerZhu
Copy link
Contributor

In my case, it is just that llvm-project is too large to be packaged in a single rust package. But that experiment did not go out well.

@SchrodingerZhu
Copy link
Contributor

See https://github.com/SchrodingerZhu/rust-linux-llvm/blob/main/llvmlibc-src/src/update.sh for how to build libc fully out of tree. It may be broken already as @jhuber6 has reverted some needed changes.

@nickdesaulniers
Copy link
Member

But that experiment did not go out well.

In that case, if there are no existing use cases, I'd prefer NOT to add more ways to build llvm-libc. Our documentation is already too confusing; we're actively trying to simplify it. We're also not continuously testing such a method of building, so we could regress it by accident.

@RossComputerGuy can you please confirm whether my suggestion above (using runtimes dir, rather than libc dir) works for you?

@RossComputerGuy
Copy link
Member Author

@nickdesaulniers I did try it but I've ran into problems:

libc> CMake Error at CMakeLists.txt:174 (include):
libc>   include could not find requested file:
libc>
libc>     AddLLVM
libc>
libc>
libc> CMake Error at CMakeLists.txt:175 (include):
libc>   include could not find requested file:
libc>
libc>     HandleLLVMOptions
libc>
libc>
libc> -- Found Python3: /nix/store/imz4prdh762l7jvwak2s7pqpq6hy5fyy-python3-3.12.7/bin/python3.12 (found version "3.12.7") found components: Interpreter
libc> CMake Error at CMakeLists.txt:183 (include):
libc>   include could not find requested file:
libc>
libc>     GetHostTriple
libc>
libc>
libc> CMake Error at CMakeLists.txt:184 (get_host_triple):
libc>   Unknown CMake command "get_host_triple".

@SchrodingerZhu
Copy link
Contributor

SchrodingerZhu commented Dec 10, 2024

It can be fixed by adding in the cmake modules under llvm's root and llvm/cmake.

@RossComputerGuy
Copy link
Member Author

Yeah, that did make it work.

Comment on lines +13 to +16
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
project(libc)
set(LIBC_BUILT_STANDALONE TRUE)
endif()
Copy link
Member

Choose a reason for hiding this comment

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

Given that libc doesn't support builds rooted in the libc directory (and I don't think we want to add support for it), maybe we should be erroring out here instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so, would we stop supporting standalone builds of everything else (clang, llvm, bolt, lldb, etc.) unless they use runtimes? I don't really think an error is necessary.

Copy link
Member

Choose a reason for hiding this comment

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

No, but that's because clang, lldb, and maybe bolt, etc do support standalone builds rooted in their subdirectory. libc does not.

So erroring out here has no implications for those other subprojects, nor does the fact that they support standalone builds imply anything for what libc should do about them.

A better comparison would be libcxx, which I believe does (or, at least, should) error out on builds rooted in the libcxx directory.

Copy link
Member

Choose a reason for hiding this comment

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

I think a warning (or error) would be a very user-friendly feature to add. Then we can point users in the right direction if they make a mistake.

Copy link
Member

Choose a reason for hiding this comment

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

Opened #119426 for this.

carlocab added a commit to carlocab/llvm-project that referenced this pull request Dec 10, 2024
Building with the source directory rooted in the libc subdirectory isn't
tested in CI and can lead to subtle build problems (cf. llvm#118871).

Let's fail fast with a helpful error message instead to help users
configure libc correctly.
carlocab added a commit to carlocab/llvm-project that referenced this pull request Dec 10, 2024
Building with the source directory rooted in the libc subdirectory isn't
tested in CI and can lead to subtle build problems (cf. llvm#118871).

Let's fail fast with a helpful error message instead to help users
configure libc correctly.

Co-authored-by: Nick Desaulniers <[email protected]>
@nickdesaulniers
Copy link
Member

Closing in favor of #119426.

@RossComputerGuy RossComputerGuy deleted the feat/llvm-libc-fixes branch December 10, 2024 21:25
carlocab added a commit that referenced this pull request Dec 12, 2024
Building with the source directory rooted in the libc subdirectory isn't
tested in CI and can lead to subtle build problems (cf. #118871).

Let's fail fast with a helpful error message instead to help users
configure libc correctly.

Co-authored-by: Nick Desaulniers <[email protected]>
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.

7 participants