c++cmakewebkitwebkitgtk

CMake Error: include could not find requested file: WebKitCommon


I compiled and installed in Ubuntu 22.04 WebKitGTK : https://trac.webkit.org/wiki/BuildingGtk#BuildingWebKitGTKfromareleasetarball

Now I would like to do the same with find_package and FetchContent in another CmakeLists.txt in order to download it and build it locally:

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)

But I get this error:

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 folder :

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

The /build/_deps/webkit2-src/CMakeLists.txt file contains the same include(WebKitCommon) that is included in the CmakeLists.txt file of the 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()

This is the build/CMakeFiles/CMakeOutput.log: https://drive.google.com/file/d/1MBR22aYzs9IdbTZ_eyc02zBPUm41Go6x/view?usp=drive_link

What do I have to do to correctly include webkitgtk in another CmakeLists.txt in order to download it and build it locally?


Solution

  • Unfortunately, not all projects are defined in a way that allows them to be used with FetchContent. In this case, webkit2 contains the following line in its top level CMakeLists.txt file:

    set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Source/cmake")
    

    That line assumes that webkit2 is the top level project. The ${CMAKE_SOURCE_DIR} variable expands to the top level directory, which when webkit2 is absorbed into a larger parent project via FetchContent or git submodules, will no longer be pointing to the directory the webkit2 project expecteed. In this case, it should be ${CMAKE_CURRENT_SOURCE_DIR}, which would work whether webkit2 is the top level directory or not.

    You could contribute a patch to the upstream project and wait for them to incorporate that into a release, or you could fork their project and apply the patch yourself to your own branch. You may find there are other similar problems throughout the webkit2 project though, so you may have to work through a few of them before you get a working result.

    This is a relatively common problem for projects that never expected to be absorbed into a larger parent project, whether that be with FetchContent or some other method like git submodules. Getting a fix upstream is always the better solution if you can get that done.

    As an aside, in your own top level project's CMakeLists.txt file, if you can increase your minimum CMake version to 3.24, there's a simpler and more flexible way of implementing the "try find_package() first and fall back to FetchContent" logic. See the FIND_PACKAGE_ARGS keyword, which brings in the new FetchContent/find_package() integration support added in CMake 3.24. This won't fix your main build problem, I'm just noting in passing for you.

    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)