我对交叉编译世界非常陌生,我正在尝试将我的 cmake 项目从 Linux(特别是 Arch)交叉编译到 Windows。我从 Arch 存储库安装了 mingw-w64-gcc,并从 AUR 安装了 mingw-w64-cmake 和其他项目依赖项。 几个
find_package
调用似乎可以正确找到 mingw-w64 工具链中的依赖项。但随后我需要从我的主机环境而不是 mingw-w64 环境中
find_package(LibXslt)
,因为这是构建依赖项。 find_package docs指示我应该添加
NO_CMAKE_FIND_ROOT_PATH
选项,以便查找没有工具链文件中设置的 CMAKE_FIND_ROOT_PATH
的包。但是当使用 NO_CMAKE_FIND_ROOT_PATH
选项来 find_package 时,cmake 抱怨它找不到 Gcrypt,这是 LibXslt 的依赖项。使用 --debug-find 标志运行 cmake 似乎通过以下输出表明它仅在 mingw-w64 工具链中搜索 gcrypt,而不是像我预期的那样在我的主机环境中搜索。
CMake Debug Log at /usr/lib/cmake/libxslt/FindGcrypt.cmake:4 (find_path):
find_path called with the following settings:
VAR: GCRYPT_INCLUDE_DIRS
NAMES: gcrypt.h
Documentation: Path to a file.
Framework
Only Search Frameworks: 0
Search Frameworks Last: 0
Search Frameworks First: 0
AppBundle
Only Search AppBundle: 0
Search AppBundle Last: 0
Search AppBundle First: 0
CMAKE_FIND_USE_CMAKE_PATH: 1
CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
CMAKE_FIND_USE_INSTALL_PREFIX: 1
find_path considered the following locations:
/usr/x86_64-w64-mingw32/bin/gcrypt.h
/usr/x86_64-w64-mingw32/home/.../.local/bin/gcrypt.h
/usr/x86_64-w64-mingw32/home/.../bin/gcrypt.h
/usr/x86_64-w64-mingw32/usr/local/sbin/gcrypt.h
/usr/x86_64-w64-mingw32/usr/local/bin/gcrypt.h
/usr/x86_64-w64-mingw32/usr/bin/gcrypt.h
/usr/x86_64-w64-mingw32/usr/lib/jvm/default/bin/gcrypt.h
/usr/x86_64-w64-mingw32/usr/bin/site_perl/gcrypt.h
/usr/x86_64-w64-mingw32/usr/bin/vendor_perl/gcrypt.h
/usr/x86_64-w64-mingw32/usr/bin/core_perl/gcrypt.h
/usr/x86_64-w64-mingw32/usr/include/gcrypt.h
/usr/x86_64-w64-mingw32/usr/gcrypt.h
/usr/x86_64-w64-mingw32/include/gcrypt.h
/usr/x86_64-w64-mingw32/gcrypt.h
/usr/x86_64-w64-mingw32/include/gcrypt.h
The item was not found.
Call Stack (most recent call first):
/usr/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package)
/usr/lib/cmake/libxslt/libxslt-config.cmake:77 (find_dependency)
CMakeLists.txt:37 (find_package)
Called from: [4] /usr/lib/cmake/libxslt/FindGcrypt.cmake
[3] /usr/share/cmake/Modules/CMakeFindDependencyMacro.cmake
[2] /usr/lib/cmake/libxslt/libxslt-config.cmake
[1] /home/.../src/lincity-ng/CMakeLists.txt
那么我如何在交叉编译时从主机环境中
find_package
LibXslt 呢?我使用
NO_CMAKE_FIND_ROOT_PATH
是否不当?交叉编译环境尝试一下。它不存在这个问题(构建与运行时依赖关系),因为它天真地假装它不是交叉编译,通过在构建时使用 Wine(使用 binfmt_misc)运行 Windows 可执行文件。 我必须在 lincity-ng 中修补一些不适合 mingw 的东西,但除此之外效果很好。
这就是我所做的:
# Install host deps for quasi-msys2
sudo pacman -S --needed make wget tar zstd gnupg wine clang lld
# Install quasi-msys2:
git clone https://github.com/HolyBlackCat/quasi-msys2
cd quasi-msys2
make install _SDL2 _SDL2_gfx _SDL2_image _SDL2_mixer _SDL2_ttf _gcc _libxml2 _physfs _python _zlib
# Helps with some MSYS2 packages that assume they're always at `/ucrt64/...`.
# May or may not work without this.
sudo ln -nfs /path/to/quasi-msys2/root/ucrt64 /ucrt64
env/shell.sh
cd ..
# libxslt not in MSYS2 packages, build it:
git clone https://gitlab.gnome.org/GNOME/libxslt
cd libxslt
cmake -B build # Had to wait for a few minutes, as it runs Python in Wine...
cmake --build build -j8
cmake --install build
cd ..
# Lastly, build lincity-ng
git clone https://github.com/lincity-ng/lincity-ng
cd lincity-ng
cmake -B build
cmake --build build -j8
我在 lincity-ng 中遇到了一些构建错误,这是我必须修补的:
在
CMakeLists.txt
if (WIN32)
-> if (MSVC)
error(...)
exit(1)
strndup
这里复制实现