我在Ubuntu 22.04中编译并安装了WebKitGTK:https://trac.webkit.org/wiki/BuildingGtk#BuildingWebKitGTKfromareleasetarball
现在我想对 another CmakeLists.txt 中的 find_package 和 FetchContent 执行相同的操作,以便下载它并在本地构建它:
cmake_minimum_required(VERSION 3.21)
project(my_application LANGUAGES CXX)
find_package(webkit2 QUIET)
if (NOT webkit2_FOUND)
message("webkit2 could not be located in CMake module search path. Downloading it,
and building it locally")
include(FetchContent)
FetchContent_Declare(
WebKit2
URL https://webkitgtk.org/releases/webkitgtk-2.42.3.tar.xz
)
FetchContent_MakeAvailable(webkit2)
endif(NOT webkit2_FOUND)
但我收到此错误:
cmake -B build
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
webkit2 could not be located in CMake module search path. Downloading it, and building it locally
-- The C compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at build/_deps/webkit2-src/CMakeLists.txt:21 (include):
include could not find requested file:
WebKitCommon
CMake Error at build/_deps/webkit2-src/Source/bmalloc/CMakeLists.txt:698 (WEBKIT_FRAMEWORK_DECLARE):
Unknown CMake command "WEBKIT_FRAMEWORK_DECLARE".
-- Configuring incomplete, errors occurred!
See also "build/CMakeFiles/CMakeOutput.log"
/build/_deps
文件夹:
build/_deps$ ls -lah
total 20K
drwxrwxr-x 5 raphy raphy 4,0K dic 15 21:18 .
drwxrwxr-x 4 raphy raphy 4,0K dic 15 21:18 ..
drwxrwxr-x 4 raphy raphy 4,0K dic 15 21:18 webkit2-build
drwxrwxr-x 5 raphy raphy 4,0K dic 15 21:18 webkit2-src
drwxrwxr-x 4 raphy raphy 4,0K dic 15 21:18 webkit2-subbuild
/build/_deps/webkit2-src/CMakeLists.txt
文件包含与 include(WebKitCommon)
的 CmakeLists.txt 文件中包含的相同的 https://webkitgtk.org/releases/webkitgtk-2.42.3.tar.xz
:
build/_deps/webkit2-src/CMakeLists.txt
:
cat build/_deps/webkit2-src/CMakeLists.txt
# -----------------------------------------------------------------------------
# Determine CMake version and build type.
# -----------------------------------------------------------------------------
# NOTE: cmake_minimum_required() and project() *MUST* be the two first commands
# used, see https://cmake.org/cmake/help/v3.3/command/project.html -- the
# latter in particular handles loading a bunch of shared CMake definitions
# and loading the cross-compilation settings from CMAKE_TOOLCHAIN_FILE.
cmake_minimum_required(VERSION 3.16)
project(WebKit)
# Remove this cmake_policy() after upgrading cmake_minimum_required() to 3.20.
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20")
cmake_policy(SET CMP0116 OLD)
endif ()
# -----------------------------------------------------------------------------
# Common configuration
#------------------------------------------------------------------------------
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Source/cmake")
include(WebKitCommon)
# -----------------------------------------------------------------------------
# Enable API unit tests and create a target for the test runner
# -----------------------------------------------------------------------------
if (ENABLE_API_TESTS)
enable_testing()
endif ()
# -----------------------------------------------------------------------------
# Add module directories
# -----------------------------------------------------------------------------
add_subdirectory(Source)
# -----------------------------------------------------------------------------
# Add tools
# -----------------------------------------------------------------------------
if (ENABLE_TOOLS)
add_subdirectory(Tools)
endif ()
if (DEVELOPER_MODE)
add_subdirectory(PerformanceTests)
endif ()
# -----------------------------------------------------------------------------
# Print the features list last, for maximum visibility.
# -----------------------------------------------------------------------------
PRINT_WEBKIT_OPTIONS()
webkitgtk-2.42.3/CMakeLists.txt
:
raphy@raohy:~/webkitgtk-2.42.3$ cat CMakeLists.txt
# -----------------------------------------------------------------------------
# Determine CMake version and build type.
# -----------------------------------------------------------------------------
# NOTE: cmake_minimum_required() and project() *MUST* be the two first commands
# used, see https://cmake.org/cmake/help/v3.3/command/project.html -- the
# latter in particular handles loading a bunch of shared CMake definitions
# and loading the cross-compilation settings from CMAKE_TOOLCHAIN_FILE.
cmake_minimum_required(VERSION 3.16)
project(WebKit)
# Remove this cmake_policy() after upgrading cmake_minimum_required() to 3.20.
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20")
cmake_policy(SET CMP0116 OLD)
endif ()
# -----------------------------------------------------------------------------
# Common configuration
#------------------------------------------------------------------------------
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Source/cmake")
include(WebKitCommon)
# -----------------------------------------------------------------------------
# Enable API unit tests and create a target for the test runner
# -----------------------------------------------------------------------------
if (ENABLE_API_TESTS)
enable_testing()
endif ()
# -----------------------------------------------------------------------------
# Add module directories
# -----------------------------------------------------------------------------
add_subdirectory(Source)
# -----------------------------------------------------------------------------
# Add tools
# -----------------------------------------------------------------------------
if (ENABLE_TOOLS)
add_subdirectory(Tools)
endif ()
if (DEVELOPER_MODE)
add_subdirectory(PerformanceTests)
endif ()
# -----------------------------------------------------------------------------
# Print the features list last, for maximum visibility.
# -----------------------------------------------------------------------------
PRINT_WEBKIT_OPTIONS()
这是
build/CMakeFiles/CMakeOutput.log
:https://drive.google.com/file/d/1MBR22aYzs9IdbTZ_eyc02zBPUm41Go6x/view?usp=drive_link
我需要做什么才能将 webkitgtk 正确包含在另一个 CmakeLists.txt 中以便下载它并在本地构建它?
不幸的是,并非所有项目的定义方式都允许它们与 FetchContent 一起使用。在这种情况下,webkit2 在其顶级 CMakeLists.txt 文件中包含以下行:
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Source/cmake")
该行假设 webkit2 是顶级项目。
${CMAKE_SOURCE_DIR}
变量扩展到顶级目录,当 webkit2 通过 FetchContent 或 git 子模块吸收到更大的父项目中时,该目录将不再指向 webkit2 项目期望的目录。在这种情况下,它应该是 ${CMAKE_CURRENT_SOURCE_DIR}
,无论 webkit2 是否是顶级目录,它都可以工作。
您可以向上游项目贡献补丁并等待他们将其合并到版本中,或者您可以分叉他们的项目并将补丁自己应用到您自己的分支。不过,您可能会发现整个 webkit2 项目中还存在其他类似的问题,因此您可能需要先解决其中的一些问题,然后才能获得工作结果。
对于那些从未想过会被吸收到更大的父项目中的项目来说,这是一个相对常见的问题,无论是使用 FetchContent 还是其他一些方法(如 git 子模块)。如果你能做到的话,在上游进行修复总是更好的解决方案。
顺便说一句,在您自己的顶级项目的
CMakeLists.txt
文件中,如果您可以将最低 CMake 版本增加到 3.24,则有一种更简单、更灵活的方法来实现“首先尝试 find_package()
,然后回退到 FetchContent”逻辑。请参阅 FIND_PACKAGE_ARGS 关键字,它引入了 CMake 3.24 中添加的新 FetchContent/find_package()
集成支持。这不会解决您的主要构建问题,我只是顺便为您指出。
cmake_minimum_required(VERSION 3.24)
project(my_application LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(
webkit2
URL https://webkitgtk.org/releases/webkitgtk-2.42.3.tar.xz
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(webkit2)