Building LLVM with CMake

1.Introduction

CMAKE是一个跨平台构建生成器工具。cmake不会构建项目,它会生成构建llvm所需的构建工具(gnu make、Visual Studio等)所需的文件。

如果您是新的参与者,请从“LLVM系统入门”页面开始。此页面适用于从旧配置/生成系统移动的现有参与者。

如果您真的很担心得到一个功能性的LLVM构建,请转到快速启动部分。如果你是一个CMAKE新手,从基本的CMAKE用法开始,然后在你知道你在做什么之后回到快速入门部分。选项和变量部分是自定义构建的参考。如果您已经有过cmake的经验,这是建议的起点。

此页面面向llvm cmake构建的用户。如果您正在寻找有关修改llvm cmake构建系统的信息,您可能希望看到cmake primer页面。它对cmake语言有一个基本的概述。

2.Quick start

我们在这里使用命令行,非交互式cmake界面。

  • 下载并安装cmake。最低要求是3.4.3版。
  • 打开一个shell。您的开发工具必须可以通过path环境变量从此shell访问。
  • 创建生成目录。llvm不支持在源目录中编译。cd到这个目录:
$ mkdir mybuilddir
$ cd mybuilddir
  • 在shell中执行此命令,将path/to/llvm/source/root替换为llvm源树的根路径:
$ cmake path/to/llvm/source/root

CMake将检测您的开发环境,执行一系列测试,并生成构建LLVM所需的文件。CMake将为所有构建参数使用默认值。有关可修改的生成参数列表,请参阅“选项和变量”部分。

如果CMake无法检测到您的工具集,或者它认为环境不够健全,则此操作可能会失败。在本例中,请确保您打算使用的工具集是惟一可以从shell访问的工具集,并且shell本身是适合您的开发环境的工具集。例如,如果您有一个可以通过PATH环境变量访问的POSIX shell, CMake将拒绝构建MinGW makefile。您可以强制CMake使用给定的构建工具;有关说明,请参见下面的用法部分。

  • CMake运行完成后,继续使用IDE项目文件,或者从build目录开始构建:
$ cmake --build .

--build选项告诉cmake调用底层构建工具(make、ninja、xcodebuild、msbuild等)。

当然,底层构建工具可以直接调用,但是--build选项是可移植的。

  • LLVM完成构建后,从build目录中安装:
$ cmake --build . --target install

除--build选项外,--target选项还带有install参数,它告诉cmake构建安装目标。

可以通过调用cmake_install在安装时设置不同的安装前缀。生成在build目录中的cmake脚本:

$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/llvm -P cmake_install.cmake

3.Basic CMake usage

本节解释CMake的基本方面,您可能在日常使用中需要这些方面。

CMake附带了大量的文档,以html文件的形式提供,并通过CMake可执行文件本身提供在线帮助。执行cmake --help以获得更多的帮助选项。

CMake允许您指定一个构建工具(例如,GNU make、Visual Studio或Xcode)。如果没有在命令行中指定,CMake将根据您的环境尝试猜测要使用哪个构建工具。一旦确定了构建工具,CMake就使用相应的生成器为构建工具创建文件(例如,makefile或Visual Studio或Xcode项目文件)。您可以使用命令行选项-G“Name of the generator”显式指定生成器。要查看系统上可用生成器的列表,请执行

$ cmake --help

这将在帮助文本的末尾列出生成器名称。

生成器的名称区分大小写,并且可能包含空格。出于这个原因,您应该按照cmake -help输出中所列的方式,在引号中输入它们。例如,要为Visual Studio 12生成特定的项目文件,您可以执行:

$ cmake -G "Visual Studio 12" path/to/llvm/source/root

对于给定的开发平台,可以有多个足够的生成器。如果您使用Visual Studio,“NMake makefile”是一个可以用NMake构建的生成器。默认情况下,CMake选择您的开发环境支持的最特定的生成器。如果你想要一个替代的生成器,你必须告诉CMake使用-G选项。

4.Options and variables

变量自定义生成构建的方式。选项是布尔变量,可能的值是ON/OFF。选项和变量在CMake命令行上定义如下:

$ cmake -DVARIABLE=value path/to/llvm/source

您可以在初始CMake调用之后设置一个变量来更改其值。你也可以取消定义一个变量:

$ cmake -UVARIABLE path/to/llvm/source

变量存储在CMake缓存中。这是一个名为CMakeCache.txt的文件,存储在由cmake生成的构建目录的根目录中。不建议您自己编辑它。

变量列在CMake缓存中,稍后在本文档中,变量名和类型由冒号分隔。您还可以在CMake命令行上指定变量和类型:

$ cmake -DVARIABLE:TYPE=value path/to/llvm/source

5.Frequently-used CMake variables

下面是一些经常使用的CMake变量,以及简单的解释和特定于llvm的注释。要获得完整的文档,请参考CMake手册,或者执行CMake --help-variable VARIABLE_NAME。

CMAKE_BUILD_TYPE:字符串

为基于令牌的生成器设置生成类型。可能的值是Release、Debug、RelWithDebInfo和MinSizeRel。如果您正在使用诸如Visual Studio之类的IDE,您应该使用IDE设置来设置构建类型。注意Release和RelWithDebInfo在大多数平台上使用不同的优化级别。

CMAKE_INSTALL_PREFIX:路径

调用“make install”或构建“install”目标时,LLVM将安装到的路径。

LLVM_LIBDIR_SUFFIX:字符串

附加附加后缀到要安装库的目录。在64位体系结构上,可以使用-DLLVM_LIBDIR_SUFFIX=64将库安装到/usr/lib64。

CMAKE_C_FLAGS:字符串

编译C源文件时要使用的额外标志。

CMAKE_CXX_FLAGS:字符串

编译c++源文件时要使用的额外标志。

6.LLVM-specific变量

LLVM_TARGETS_TO_BUILD:字符串

以分号分隔的要构建的目标列表,或用于构建所有目标的all。大小写敏感的。默认为全部。例如:-DLLVM_TARGETS_TO_BUILD = " X86; PowerPC”。

LLVM_BUILD_TOOLS: BOOL

构建LLVM工具。默认为ON。在任何情况下,都会生成构建每个工具的目标。您可以通过调用工具的目标来单独构建工具。例如,您可以通过在构建目录的根目录下执行make llvm-as来使用基于makefile的系统构建llvm-as。

LLVM_INCLUDE_TOOLS: BOOL

为LLVM工具生成生成目标。默认为ON。您可以使用此选项来禁用为LLVM工具生成构建目标。

LLVM_INSTALL_BINUTILS_SYMLINKS: BOOL

安装从binutils工具名称到相应LLVM工具的符号链接。例如,ar将符号链接到llvm-ar。

LLVM_BUILD_EXAMPLES: BOOL

构建LLVM的例子。缺省值为OFF。在任何情况下都会生成构建每个示例的目标。有关更多细节,请参见上面的LLVM_BUILD_TOOLS文档。

LLVM_INCLUDE_EXAMPLES: BOOL

为LLVM示例生成构建目标。默认为ON。您可以使用此选项来禁用生成LLVM示例的构建目标。

LLVM_BUILD_TESTS: BOOL

构建LLVM单元测试。在任何情况下,都会生成构建每个单元测试的目标。您可以使用在单元测试下定义的目标来构建一个特定的单元测试,例如ADTTests、IRTests、SupportTests等等(在单元测试的子目录中搜索add_llvm_unittest,以获得完整的单元测试列表)。可以使用目标单元测试构建所有单元测试。

LLVM_INCLUDE_TESTS: BOOL

为LLVM单元测试生成生成目标。默认为ON。您可以使用此选项来禁用为LLVM单元测试生成构建目标。

LLVM_BUILD_BENCHMARKS: BOOL

将基准添加到默认目标列表中。默认为OFF。

LLVM_INCLUDE_BENCHMARKS: BOOL

为LLVM基准生成构建目标。默认为ON。

LLVM_APPEND_VC_REV: BOOL

嵌入版本控制修订信息(svn revision number或Git revision id))。版本信息由llvm/include/llvm/Support/vcsupdate.h中的LLVM_REVISION宏提供。不需要修订信息的git开发人员可以禁用此选项,以避免在分支切换之后重新链接大多数二进制文件。默认为ON。

a 0:00:00 Installing build dependencies ... error error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> [229 lines of output] WARNING: Skip installing pip, this will break the python-pip package (termux). Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Ignoring numpy: markers 'python_version == "3.6" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment Ignoring numpy: markers 'python_version == "3.7" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment Ignoring numpy: markers 'python_version == "3.8" and platform_machine != "aarch64" and platform_machine != "arm64"' don't match your environment Ignoring numpy: markers 'python_version < "3.9" and sys_platform == "linux" and platform_machine == "aarch64"' don't match your environment Ignoring numpy: markers 'python_version < "3.9" and sys_platform == "darwin" and platform_machine == "arm64"' don't match your environment Ignoring setuptools: markers 'python_version < "3.12"' don't match your environment Collecting numpy>=2.0.0 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2e/19/d7c972dfe90a353dbd3efbbe1d14a5951de80c99c9dc1b93cd998d51dc0f/numpy-2.3.1.tar.gz (20.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 20.4/20.4 MB 8.5 MB/s eta 0:00:00 Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Installing backend dependencies: started Installing backend dependencies: still running... Installing backend dependencies: finished with status 'error' error: subprocess-exited-with-error × pip subprocess to install backend dependencies did not run successfully. │ exit code: 1 ╰─> [196 lines of output] Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting ninja>=1.8.2 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/95/d4/6b0324541018561c5e73e617bd16f20a4fc17d1179bb3b3520b6ca8beb7b/ninja-1.11.1.4.tar.gz (201 kB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Installing backend dependencies: started Installing backend dependencies: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' Collecting patchelf>=0.11.0 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0d/41/dc3ee5838db2d90be935adb53ae7745135d9c719d070b1989b246f983c7f/patchelf-0.17.2.2.tar.gz (149 kB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Installing backend dependencies: started Installing backend dependencies: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' Building wheels for collected packages: ninja, patchelf Building wheel for ninja (pyproject.toml): started Building wheel for ninja (pyproject.toml): still running... Building wheel for ninja (pyproject.toml): finished with status 'error' error: subprocess-exited-with-error × Building wheel for ninja (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [120 lines of output] /data/data/com.termux/files/usr/tmp/pip-build-env-4j78z2m7/normal/lib/python3.12/site-packages/setuptools_scm/git.py:310: UserWarning: git archive did not support describe output warnings.warn("git archive did not support describe output") /data/data/com.termux/files/usr/tmp/pip-build-env-4j78z2m7/normal/lib/python3.12/site-packages/setuptools_scm/git.py:328: UserWarning: unprocessed git archival found (no export subst applied) warnings.warn("unprocessed git archival found (no export subst applied)") *** scikit-build-core 0.11.5 using CMake 3.31.6 (wheel) *** Configuring CMake... loading initial cache file build/py3-none-linux_aarch64/CMakeInit.txt -- The C compiler identification is Clang 20.1.7 -- The CXX compiler identification is Clang 20.1.7 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- ************************************ -- Ninja Python Distribution -- -- RUN_NINJA_TEST : OFF -- ************************************ -- IPO / LTO not supported: <Change Dir: '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' Run Build Command(s): /data/data/com.termux/files/usr/bin/cmake -E env VERBOSE=1 /data/data/com.termux/files/usr/bin/make -f Makefile /data/data/com.termux/files/usr/bin/cmake -S/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src -B/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin --check-build-system CMakeFiles/Makefile.cmake 0 /data/data/com.termux/files/usr/bin/cmake -E cmake_progress_start /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin/CMakeFiles /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin//CMakeFiles/progress.marks /data/data/com.termux/files/usr/bin/make -f CMakeFiles/Makefile2 all make[1]: Entering directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' /data/data/com.termux/files/usr/bin/make -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/depend make[2]: Entering directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' cd /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin && /data/data/com.termux/files/usr/bin/cmake -E cmake_depends "Unix Makefiles" /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin/CMakeFiles/foo.dir/DependInfo.cmake make[2]: Leaving directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' /data/data/com.termux/files/usr/bin/make -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/build make[2]: Entering directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' [ 25%] Building CXX object CMakeFiles/foo.dir/foo.cpp.o /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang++ -O2 -g -DNDEBUG -flto=thin -MD -MT CMakeFiles/foo.dir/foo.cpp.o -MF CMakeFiles/foo.dir/foo.cpp.o.d -o CMakeFiles/foo.dir/foo.cpp.o -c /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src/foo.cpp [ 50%] Linking CXX static library libfoo.a /data/data/com.termux/files/usr/bin/cmake -P CMakeFiles/foo.dir/cmake_clean_target.cmake /data/data/com.termux/files/usr/bin/cmake -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=1 "/data/data/com.termux/files/usr/bin/llvm-ar" qc libfoo.a CMakeFiles/foo.dir/foo.cpp.o "/data/data/com.termux/files/usr/bin/llvm-ranlib" libfoo.a make[2]: Leaving directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' [ 50%] Built target foo /data/data/com.termux/files/usr/bin/make -f CMakeFiles/boo.dir/build.make CMakeFiles/boo.dir/depend make[2]: Entering directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' cd /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin && /data/data/com.termux/files/usr/bin/cmake -E cmake_depends "Unix Makefiles" /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin/CMakeFiles/boo.dir/DependInfo.cmake make[2]: Leaving directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' /data/data/com.termux/files/usr/bin/make -f CMakeFiles/boo.dir/build.make CMakeFiles/boo.dir/build make[2]: Entering directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' [ 75%] Building CXX object CMakeFiles/boo.dir/main.cpp.o /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang++ -O2 -g -DNDEBUG -flto=thin -MD -MT CMakeFiles/boo.dir/main.cpp.o -MF CMakeFiles/boo.dir/main.cpp.o.d -o CMakeFiles/boo.dir/main.cpp.o -c /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/src/main.cpp [100%] Linking CXX executable boo /data/data/com.termux/files/usr/bin/cmake -E cmake_link_script CMakeFiles/boo.dir/link.txt --verbose=1 aarch64-linux-android-clang++: error: invalid linker name in argument '-fuse-ld=gold' aarch64-linux-android-clang++: error: invalid linker name in argument '-fuse-ld=gold' /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang++ -O2 -g -DNDEBUG -flto=thin -fuse-ld=gold CMakeFiles/boo.dir/main.cpp.o -o boo libfoo.a make[2]: *** [CMakeFiles/boo.dir/build.make:104: boo] Error 1 make[2]: Leaving directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' make[1]: *** [CMakeFiles/Makefile2:125: CMakeFiles/boo.dir/all] Error 2 make[1]: Leaving directory '/data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64/_deps/ninja-build/CMakeFiles/_CMakeLTOTest-CXX/bin' make: *** [Makefile:94: all] Error 2 > -- Performing Test flag_no_deprecated -- Performing Test flag_no_deprecated - Success -- Performing Test flag_color_diag -- Performing Test flag_color_diag - Success CMake Warning at ninja-upstream/CMakeLists.txt:49 (message): re2c was not found; changes to src/*.in.cc will not affect your build. -- Looking for fork -- Looking for fork - found -- Looking for pipe -- Looking for pipe - found -- Configuring done (9.0s) -- Generating done (0.1s) -- Build files have been written to: /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/build/py3-none-linux_aarch64 *** Building project with Unix Makefiles... [ 1%] Building CXX object _deps/ninja-build/CMakeFiles/libninja-re2c.dir/src/depfile_parser.cc.o [ 2%] Building CXX object _deps/ninja-build/CMakeFiles/libninja-re2c.dir/src/lexer.cc.o [ 2%] Built target libninja-re2c [ 4%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/build_log.cc.o [ 5%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/build.cc.o [ 7%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/clean.cc.o [ 8%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/clparser.cc.o [ 10%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/dyndep.cc.o [ 11%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/dyndep_parser.cc.o [ 13%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/debug_flags.cc.o [ 14%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/deps_log.cc.o [ 16%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/disk_interface.cc.o [ 17%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/edit_distance.cc.o [ 19%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/eval_env.cc.o [ 20%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/graph.cc.o [ 22%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/graphviz.cc.o [ 23%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/json.cc.o [ 25%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/line_printer.cc.o [ 26%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/manifest_parser.cc.o [ 28%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/metrics.cc.o [ 29%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/missing_deps.cc.o [ 31%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/parser.cc.o [ 32%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/state.cc.o [ 34%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/status.cc.o [ 35%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/string_piece_util.cc.o [ 37%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/tokenpool-gnu-make.cc.o [ 38%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/util.cc.o [ 40%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/version.cc.o [ 41%] Building CXX object _deps/ninja-build/CMakeFiles/libninja.dir/src/subprocess-posix.cc.o /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/ninja_7200d3032e2b456abb23067d29afe294/ninja-upstream/src/subprocess-posix.cc:26:10: fatal error: 'spawn.h' file not found 26 | #include <spawn.h> | ^~~~~~~~~ 1 error generated. make[2]: *** [_deps/ninja-build/CMakeFiles/libninja.dir/build.make:429: _deps/ninja-build/CMakeFiles/libninja.dir/src/subprocess-posix.cc.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:193: _deps/ninja-build/CMakeFiles/libninja.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 *** CMake build failed [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for ninja Building wheel for patchelf (pyproject.toml): started Building wheel for patchelf (pyproject.toml): finished with status 'error' error: subprocess-exited-with-error × Building wheel for patchelf (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [29 lines of output] *** scikit-build-core 0.11.5 using CMake 3.31.6 (wheel) *** Configuring CMake... loading initial cache file build/py3-none-linux_aarch64/CMakeInit.txt -- The C compiler identification is Clang 20.1.7 -- The CXX compiler identification is Clang 20.1.7 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /data/data/com.termux/files/usr/bin/aarch64-linux-android-clang++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (2.6s) -- Generating done (0.0s) -- Build files have been written to: /data/data/com.termux/files/usr/tmp/pip-install-nuu7do6s/patchelf_96e1be08ef43472d93a934bd092c3537/build/py3-none-linux_aarch64 *** Building project with Unix Makefiles... [ 12%] Creating directories for 'build_patchelf' [ 25%] No download step for 'build_patchelf' [ 37%] No update step for 'build_patchelf' [ 50%] Performing patch step for 'build_patchelf' ./bootstrap.sh: 2: autoreconf: not found make[2]: *** [CMakeFiles/build_patchelf.dir/build.make:114: build_patchelf-prefix/src/build_patchelf-stamp/build_patchelf-patch] Error 127 make[1]: *** [CMakeFiles/Makefile2:87: CMakeFiles/build_patchelf.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 *** CMake build failed [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for patchelf Failed to build ninja patchelf ERROR: Failed to build installable wheels for some pyproject.toml based projects (ninja, patchelf) [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × pip subprocess to install backend dependencies did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. ~ $
最新发布
07-01
<think>我们正在解决Termux环境中使用pip安装numpy时遇到的构建错误,错误提示为`error: subprocess-exited-with-error`,并且涉及Ninja和patchelf的构建依赖问题。在Termux环境中,由于缺少一些构建工具和库,使用pip编译安装numpy可能会失败。常见的解决方案是安装必要的构建依赖,并确保环境配置正确。步骤:1.更新系统和安装基本构建工具。2.安装Ninja和patchelf。3.设置环境变量(如果需要)。4.尝试重新安装numpy。注意:Termux环境与标准Linux环境有所不同,因此需要特别处理。具体操作如下: ```bash#1.更新系统并安装基本编译工具pkgupdate &&pkgupgrade -ypkginstall pythonclangmake libffi openssl zlib libjpeg-turbo libpng openblas-y#2.安装Ninja和patchelf(这两个工具在Termux官方仓库中可能没有,需要使用pip安装) pipinstall ninja patchelf#注意:如果pip安装ninja或patchelf失败,可以尝试以下替代方法:#对于Ninja:Termux官方仓库中可能已经提供了ninja,可以尝试使用pkg安装:#pkginstall ninja#对于patchelf:Termux官方仓库中可能没有,所以只能通过pip安装。如果pip安装失败,可能需要从源码编译。 #3.设置环境变量(如果需要) #确保ninja和patchelf在PATH中。通常pip安装的可执行文件位于~/.local/bin,将其加入PATH:exportPATH=$PATH:$HOME/.local/bin#4.安装numpy,可以尝试使用--no-build-isolation以避免使用独立的构建环境,这样可以使用Termux已经安装的依赖pipinstall numpy--no-build-isolation#如果上述方法失败,可以尝试设置以下环境变量来禁用某些特性或指定编译器: exportCC=clangexportCXX=clang++pipinstall numpy--no-build-isolation```如果仍然遇到问题,可以尝试安装更多的开发包: ```bashpkginstall libffi-devopenssl-dev zlib-devlibjpeg-turbo-dev libpng-devopenblas-dev```然后重新运行numpy的安装。如果问题持续,可能需要查看具体的错误日志,然后针对性地解决。常见问题:-内存不足:在Termux中编译可能会因为内存不足而失败,可以尝试使用交换文件(swap)或者减少编译线程(如设置环境变量`export NPY_NUM_BUILD_JOBS=1`)。-链接库路径问题:Termux的库路径在`$PREFIX/lib`,而标准Linux在`/usr/lib`,因此可能需要设置`LIBRARY_PATH`和`LD_LIBRARY_PATH`: ```bashexport LIBRARY_PATH=$PREFIX/libexport LD_LIBRARY_PATH=$PREFIX/lib```最后,重新运行安装命令。验证安装:```pythonimportnumpyprint(numpy.__version__) ```如果以上步骤仍然无法解决问题,请提供具体的错误信息以便进一步诊断。相关问题:1. Termux中如何增加交换空间(swap)来解决编译时的内存不足问题?2.如何在Termux中设置环境变量使其永久生效?3.Termux中安装Python包时如何查看详细的错误日志?</think>在 Termux 环境中使用 pip 安装 NumPy 时遇到 `error: subprocess-exited-with-error` 错误,通常是由于缺少编译依赖或环境配置问题导致。以下是完整的解决方案: ### 解决方案:修复 Ninja 和 patchelf 依赖问题 ```bash # 步骤 1:更新系统并安装核心编译工具 pkg update -y && pkg upgrade -y pkg install python clang make cmake libffi openssl zlib libjpeg-turbo libpng openblas -y # 步骤 2:安装缺失的构建工具 pkg install ninja patchelf -y # 关键:安装 Ninja 构建系统和 patchelf # 步骤 3:设置 Python 环境变量 export LDFLAGS="-L${PREFIX}/lib" export CFLAGS="-I${PREFIX}/include" export CPPFLAGS="-I${PREFIX}/include" # 步骤 4:安装 NumPy 并绕过隔离构建 pip install --no-build-isolation numpy # 禁用隔离构建避免环境问题 # 备选方案:如果仍失败,使用预编译轮子 pip install numpy --prefer-binary # 优先使用二进制包 ``` ### 关键问题解析 1. **Ninja 缺失问题**: - Ninja 是 NumPy 编译需要的构建系统 - 安装命令:`pkg install ninja` - 验证:`ninja --version` 应显示版本号 2. **patchelf 缺失问题**: - patchelf 用于修复 ELF 二进制文件的依赖路径 - 安装命令:`pkg install patchelf` - 验证:`patchelf --version` 3. **环境变量配置**: ```bash # 必须设置库和头文件路径 export LDFLAGS="-L${PREFIX}/lib" # 链接器库路径 export CFLAGS="-I${PREFIX}/include" # C 编译器头文件路径 export CPPFLAGS="-I${PREFIX}/include" # C++ 编译器头文件路径 ``` 4. **`--no-build-isolation` 的作用**: - 禁用 pip 的隔离构建环境 - 允许使用 Termux 已安装的系统级依赖 - 避免因虚拟环境缺失依赖导致的编译失败 ### 常见错误处理 **错误 1:`ninja: command not found`** ```bash # 手动安装 Ninja pkg install ninja -y ``` **错误 2:`patchelf is required`** ```bash # 安装 patchelf 并重试 pkg install patchelf -y pip install --no-build-isolation numpy ``` **错误 3:内存不足** ```bash # 创建交换文件缓解内存压力 dd if=/dev/zero of=~/swapfile bs=1M count=512 mkswap ~/swapfile swapon ~/swapfile # 单线程编译 export NPY_NUM_BUILD_JOBS=1 pip install numpy ``` ### 验证安装 ```python import numpy as np print(np.__version__) # 应显示版本号如 1.26.0 print(np.show_config()) # 检查 OpenBLAS 链接状态 ``` > **提示**:如果持续失败,可尝试安装预编译版本: > ```bash > pip install numpy --prefer-binary --no-cache-dir > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值