Skip to content

Truly independent web browser

Meta+UI: Build (L)GPL vcpkg ports as dynamic libraries for distribution

Zaggy10243c3b512acc95
diff --git a/Meta/CMake/install_vcpkg_libraries.cmake b/Meta/CMake/install_vcpkg_libraries.cmake
new file mode 100644
index 00000000000..2bc4fd8a30c
--- /dev/null
+++ b/Meta/CMake/install_vcpkg_libraries.cmake
@@ -0,0 +1,58 @@
+include_guard()
+
+# CMake collects runtime dependencies only on Windows, Linux and macOS.
+# Windows is left out too, as its vcpkg DLLs live in bin/ and install as RUNTIME artifacts.
+set(LADYBIRD_INSTALLS_VCPKG_LIBRARIES OFF)
+if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin)$" AND NOT "${VCPKG_INSTALLED_DIR}" STREQUAL "")
+ set(LADYBIRD_INSTALLS_VCPKG_LIBRARIES ON)
+endif()
+
+function(ladybird_vcpkg_library_directory output_variable)
+ set(vcpkg_library_dir "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib")
+ if (NOT CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo")
+ set(vcpkg_library_dir "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/lib")
+ endif()
+ set(${output_variable} "${vcpkg_library_dir}" PARENT_SCOPE)
+endfunction()
+
+# Installs the shared libraries from vcpkg that the binaries in the runtime dependency set link. Call it after every
+# install() that adds to the set.
+function(ladybird_install_vcpkg_libraries dependency_set)
+ cmake_parse_arguments(PARSE_ARGV 1 ARG "" "DESTINATION;COMPONENT" "")
+
+ ladybird_vcpkg_library_directory(vcpkg_library_dir)
+ string(REGEX REPLACE "([][+.*()^$?|\\\\])" "\\\\\\1" vcpkg_library_dir_regex "${vcpkg_library_dir}")
+
+ set(system_library_regexes "")
+ if (APPLE)
+ # These are in the dyld shared cache rather than on disk.
+ set(system_library_regexes "^/usr/lib/" "^/System/")
+ endif()
+
+ install(RUNTIME_DEPENDENCY_SET ${dependency_set}
+ PRE_EXCLUDE_REGEXES ${system_library_regexes}
+ POST_INCLUDE_REGEXES "^${vcpkg_library_dir_regex}/"
+ POST_EXCLUDE_REGEXES ".*"
+ DIRECTORIES "${vcpkg_library_dir}"
+ LIBRARY DESTINATION ${ARG_DESTINATION} COMPONENT ${ARG_COMPONENT}
+ )
+
+ # The libraries are installed under the names of the files they resolve to, yet binaries may link them through the
+ # versioned links next to those files.
+ install(CODE "
+ set(library_install_dir \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${ARG_DESTINATION}\")
+ file(GLOB links LIST_DIRECTORIES false \"${vcpkg_library_dir}/*\")
+ foreach (link IN LISTS links)
+ cmake_path(GET link FILENAME link_name)
+ if (NOT IS_SYMLINK \"\${link}\" OR NOT link_name MATCHES \"\\\\.[0-9]+\\\\.dylib\$|\\\\.so\\\\.[0-9]+\")
+ continue()
+ endif()
+ file(REAL_PATH \"\${link}\" target)
+ cmake_path(GET target FILENAME target_name)
+ if (EXISTS \"\${library_install_dir}/\${target_name}\" AND NOT link_name STREQUAL target_name)
+ file(CREATE_LINK \"\${target_name}\" \"\${library_install_dir}/\${link_name}\" SYMBOLIC)
+ endif()
+ endforeach()
+ "
+ COMPONENT ${ARG_COMPONENT})
+endfunction()
diff --git a/Meta/CMake/lagom_install_options.cmake b/Meta/CMake/lagom_install_options.cmake
index 4cee4ba8121..850e3348aa8 100644
--- a/Meta/CMake/lagom_install_options.cmake
+++ b/Meta/CMake/lagom_install_options.cmake
@@ -34,14 +34,18 @@ if (APPLE)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
endif()
-# Static builds install no libraries of their own, so their executables have nothing to look for next to them.
-if (BUILD_SHARED_LIBS)
+# Static builds without vcpkg install no libraries, so their executables have nothing to look for next to them.
+if (BUILD_SHARED_LIBS OR NOT "${VCPKG_INSTALLED_DIR}" STREQUAL "")
if (APPLE)
set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
else()
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
endif()
endif()
-set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+
+# Installs carry the shared libraries from vcpkg, so they must not look for them in the build machine's vcpkg tree.
+if ("${VCPKG_INSTALLED_DIR}" STREQUAL "")
+ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+endif()
set(CMAKE_INSTALL_MESSAGE NEVER)
diff --git a/Meta/CMake/licenses.cmake b/Meta/CMake/licenses.cmake
index d64057a8c9f..d8791190b91 100644
--- a/Meta/CMake/licenses.cmake
+++ b/Meta/CMake/licenses.cmake
@@ -13,6 +13,7 @@ set(licenses_dependencies
"${licenses_generator}"
"${LADYBIRD_SOURCE_DIR}/LICENSE"
"${LADYBIRD_SOURCE_DIR}/Cargo.lock"
+ "${LADYBIRD_SOURCE_DIR}/Meta/CMake/vcpkg/dynamic-ports.txt"
)
if (NOT "${VCPKG_INSTALLED_DIR}" STREQUAL "" AND NOT "${VCPKG_TARGET_TRIPLET}" STREQUAL "")
diff --git a/Meta/CMake/packaging.cmake b/Meta/CMake/packaging.cmake
index 2c4f018b063..17c6e2756a4 100644
--- a/Meta/CMake/packaging.cmake
+++ b/Meta/CMake/packaging.cmake
@@ -1,3 +1,5 @@
+# Pass the values below to CPack as they are, rather than unescaping them once more.
+set(CPACK_VERBATIM_VARIABLES ON)
set(CPACK_GENERATOR "TGZ")
set(CPACK_STRIP_FILES TRUE)
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
@@ -63,6 +65,21 @@ set(CPACK_RPM_DEBUGSYMBOLS_PACKAGE_DESCRIPTION "Debug symbols for Ladybird, whic
# rpmbuild compresses on a single thread by default, which takes many minutes for packages of our size.
set(CPACK_RPM_SPEC_INSTALL_POST "/bin/true")
set(CPACK_RPM_SPEC_MORE_DEFINE "%define _build_id_links none\n%define _binary_payload w9T0.xzdio")
+
+# The package carries the shared libraries from vcpkg for itself alone, so it must not offer them to the system, nor
+# ask the system for them.
+include(install_vcpkg_libraries)
+if (LINUX AND LADYBIRD_INSTALLS_VCPKG_LIBRARIES)
+ ladybird_vcpkg_library_directory(vcpkg_library_dir)
+ file(GLOB vcpkg_shared_library_links RELATIVE "${vcpkg_library_dir}" "${vcpkg_library_dir}/*.so")
+ list(TRANSFORM vcpkg_shared_library_links REPLACE "\\.so$" "")
+ list(TRANSFORM vcpkg_shared_library_links REPLACE "([][+.*()^$?|\\\\])" "\\\\\\\\\\1")
+ list(JOIN vcpkg_shared_library_links "|" vcpkg_shared_library_names)
+ if (vcpkg_shared_library_names)
+ string(APPEND CPACK_RPM_SPEC_MORE_DEFINE "\n%global __provides_exclude ^(${vcpkg_shared_library_names})\\\\.so")
+ string(APPEND CPACK_RPM_SPEC_MORE_DEFINE "\n%global __requires_exclude ^(${vcpkg_shared_library_names})\\\\.so")
+ endif()
+endif()
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION /opt /usr/lib/debug /usr/lib/debug/.build-id /usr/share/applications
/usr/share/dbus-1 /usr/share/dbus-1/services /usr/share/icons /usr/share/icons/hicolor
/usr/share/icons/hicolor/scalable /usr/share/icons/hicolor/scalable/apps /usr/share/metainfo)
diff --git a/Meta/CMake/vcpkg/distribution-triplets/distribution.cmake b/Meta/CMake/vcpkg/distribution-triplets/distribution.cmake
index d19538159a8..5b7f7502a23 100644
--- a/Meta/CMake/vcpkg/distribution-triplets/distribution.cmake
+++ b/Meta/CMake/vcpkg/distribution-triplets/distribution.cmake
@@ -1,2 +1,12 @@
set(VCPKG_BUILD_TYPE release)
set(VCPKG_LIBRARY_LINKAGE static)
+
+block(SCOPE_FOR VARIABLES PROPAGATE VCPKG_LIBRARY_LINKAGE VCPKG_HASH_ADDITIONAL_FILES)
+ set(dynamic_ports_file "${CMAKE_CURRENT_LIST_DIR}/../dynamic-ports.txt")
+ file(STRINGS "${dynamic_ports_file}" dynamic_ports REGEX "^[^#]")
+ if (PORT IN_LIST dynamic_ports)
+ set(VCPKG_LIBRARY_LINKAGE dynamic)
+ # vcpkg hashes only the triplet file itself, so a cached static build would otherwise be reused.
+ list(APPEND VCPKG_HASH_ADDITIONAL_FILES "${dynamic_ports_file}")
+ endif()
+endblock()
diff --git a/Meta/CMake/vcpkg/dynamic-ports.txt b/Meta/CMake/vcpkg/dynamic-ports.txt
new file mode 100644
index 00000000000..aa4abb95834
--- /dev/null
+++ b/Meta/CMake/vcpkg/dynamic-ports.txt
@@ -0,0 +1,11 @@
+# These ports are under the (L)GPL, so builds link them dynamically to allow relinking.
+ffmpeg
+gettext-libintl
+libdwarf
+libidn2
+libunistring
+qtbase
+qtpositioning
+qtserialport
+# Qt and Ladybird must share one ICU, as Ladybird hands the ICU data to it once per process.
+icu
diff --git a/Meta/Utils/vcpkg_deps.py b/Meta/Utils/vcpkg_deps.py
index e7501186fba..e810153b304 100644
--- a/Meta/Utils/vcpkg_deps.py
+++ b/Meta/Utils/vcpkg_deps.py
@@ -263,6 +263,7 @@ def generate_build_files(
overlay_triplets_dir(preset).parent / "base-triplets",
):
triplet_inputs.extend(sorted(str(f) for f in triplet_dir.glob("*.cmake")))
+ triplet_inputs.append(str(overlay_triplets_dir(preset).parent / "dynamic-ports.txt"))
lines = [
"ninja_required_version = 1.5",
diff --git a/Meta/generate-licenses.py b/Meta/generate-licenses.py
index 0eb1da42385..c15713dbbed 100755
--- a/Meta/generate-licenses.py
+++ b/Meta/generate-licenses.py
@@ -32,6 +32,8 @@ SHIPPED_PORT_DIRECTORIES = {"bin", "Frameworks", "include", "lib"}
LGPL_MARKER = "GNU LESSER GENERAL PUBLIC LICENSE"
+DYNAMIC_PORTS_PATH = pathlib.PurePosixPath("Meta/CMake/vcpkg/dynamic-ports.txt")
+
CRATE_LICENSE_FILE_PATTERN = re.compile(r"^(licen[cs]e|copying|copyright|notice|unlicense|patents)", re.IGNORECASE)
C_FAMILY_COMMENT_PATTERN = re.compile(r"/\*.*?\*/|(?:^[ \t]*//[^\n]*\n?)+", re.DOTALL | re.MULTILINE)
@@ -133,6 +135,11 @@ def port_is_shipped(name: str, installed_files: list[str], triplet: str) -> bool
return False
+def read_dynamic_ports(source_dir: pathlib.Path) -> set[str]:
+ lines = read_text(source_dir / DYNAMIC_PORTS_PATH).splitlines()
+ return {line.strip() for line in lines if line.strip() and not line.startswith("#")}
+
+
def collect_vcpkg_ports(installed_dir: pathlib.Path, triplet: str) -> list[Port]:
ports = []
@@ -265,6 +272,10 @@ def generate(args: argparse.Namespace) -> str:
lgpl_ports = [port for port in ports if LGPL_MARKER in port.copyright]
+ dynamic_ports = read_dynamic_ports(args.source_dir)
+ if statically_linked_lgpl_ports := [port.name for port in lgpl_ports if port.name not in dynamic_ports]:
+ raise RuntimeError(f"LGPL ports missing from {DYNAMIC_PORTS_PATH}: {', '.join(statically_linked_lgpl_ports)}")
+
introduction = [
"Ladybird is distributed under the BSD 2-Clause License below. It includes the third-party software listed",
"after that, under the licenses reproduced with each of them.",
diff --git a/UI/cmake/InstallRules.cmake b/UI/cmake/InstallRules.cmake
index d80de58434d..37e3ede2134 100644
--- a/UI/cmake/InstallRules.cmake
+++ b/UI/cmake/InstallRules.cmake
@@ -14,6 +14,13 @@ endif()
set(ladybird_applications ladybird WebDriver ${ladybird_helper_processes})
+# Builds with vcpkg ship the shared libraries it built, which are found by what the installed binaries link.
+include(install_vcpkg_libraries)
+set(vcpkg_runtime_dependencies "")
+if (LADYBIRD_INSTALLS_VCPKG_LIBRARIES)
+ set(vcpkg_runtime_dependencies RUNTIME_DEPENDENCY_SET ladybird_vcpkg_libraries)
+endif()
+
# CMake installs the binaries unstripped when it has no strip tool, or ignores the failure to run it, for instance when
# packaging in a different environment than the build. Refuse to install anything in that case. MSVC-style toolchains
# have no strip tool to begin with.
@@ -28,6 +35,7 @@ endif()
install(TARGETS ladybird WebDriver
EXPORT ladybirdTargets
+ ${vcpkg_runtime_dependencies}
RUNTIME
COMPONENT ladybird_Runtime
DESTINATION ${ladybird_executable_install_dir}
@@ -46,6 +54,7 @@ install(TARGETS ladybird WebDriver
install(TARGETS ${ladybird_helper_processes}
EXPORT ladybirdTargets
+ ${vcpkg_runtime_dependencies}
RUNTIME
COMPONENT ladybird_Runtime
DESTINATION ${ladybird_helper_install_dir}
@@ -93,6 +102,7 @@ endif()
install(TARGETS ${all_required_lagom_libraries}
EXPORT ladybirdTargets
+ ${vcpkg_runtime_dependencies}
COMPONENT ladybird_Runtime
LIBRARY
COMPONENT ladybird_Runtime
@@ -104,6 +114,38 @@ install(TARGETS ${all_required_lagom_libraries}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
+if (vcpkg_runtime_dependencies)
+ set(vcpkg_library_install_dir ${CMAKE_INSTALL_LIBDIR})
+ if (APPLE)
+ set(vcpkg_library_install_dir bundle/Ladybird.app/Contents/lib)
+
+ # Qt loads these at runtime, so nothing links them. Static builds of Qt link them into the executable instead.
+ get_target_property(qt_core_type Qt6::Core TYPE)
+ if (qt_core_type STREQUAL "SHARED_LIBRARY")
+ set(bundle_plugins_dir bundle/Ladybird.app/Contents/PlugIns)
+ foreach (plugin IN ITEMS platforms/QCocoaIntegrationPlugin styles/QMacStylePlugin imageformats/QGifPlugin
+ imageformats/QICOPlugin imageformats/QJpegPlugin)
+ cmake_path(GET plugin PARENT_PATH plugin_type)
+ cmake_path(GET plugin FILENAME plugin_target)
+ if (NOT TARGET Qt6::${plugin_target})
+ message(FATAL_ERROR "Ladybird.app needs the Qt plugin ${plugin_target}, which Qt was built without")
+ endif()
+ install(IMPORTED_RUNTIME_ARTIFACTS Qt6::${plugin_target}
+ RUNTIME_DEPENDENCY_SET ladybird_vcpkg_libraries
+ LIBRARY DESTINATION ${bundle_plugins_dir}/${plugin_type} COMPONENT ladybird_Runtime
+ )
+ endforeach()
+
+ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/qt.conf" "[Paths]\nPlugins = PlugIns\n")
+ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/qt.conf" DESTINATION bundle/Ladybird.app/Contents/Resources
+ COMPONENT ladybird_Runtime)
+ endif()
+ endif()
+
+ ladybird_install_vcpkg_libraries(ladybird_vcpkg_libraries
+ DESTINATION ${vcpkg_library_install_dir} COMPONENT ladybird_Runtime)
+endif()
+
if (LADYBIRD_GENERATE_SYMBOL_FILES)
set(symbol_files_dir "${CMAKE_BINARY_DIR}/symbols")
set(symbol_files)
diff --git a/Utilities/CMakeLists.txt b/Utilities/CMakeLists.txt
index 11471acef52..695b26ca514 100644
--- a/Utilities/CMakeLists.txt
+++ b/Utilities/CMakeLists.txt
@@ -49,14 +49,29 @@ set(GIT_COMMIT_FILE "${CMAKE_BINARY_DIR}/COMMIT")
file(WRITE "${GIT_COMMIT_FILE}" "${GIT_COMMIT_HASH}\n")
if (NOT CMAKE_SKIP_INSTALL_RULES)
+ include(install_vcpkg_libraries)
+ set(js_vcpkg_libraries "")
+ set(wasm_vcpkg_libraries "")
+ if (LADYBIRD_INSTALLS_VCPKG_LIBRARIES)
+ set(js_vcpkg_libraries RUNTIME_DEPENDENCY_SET js_vcpkg_libraries)
+ set(wasm_vcpkg_libraries RUNTIME_DEPENDENCY_SET wasm_vcpkg_libraries)
+ endif()
+
if (TARGET js)
- install(TARGETS js COMPONENT js)
+ install(TARGETS js ${js_vcpkg_libraries} COMPONENT js)
endif()
install(FILES "${GIT_COMMIT_FILE}" COMPONENT js DESTINATION .)
- install(TARGETS wasm COMPONENT wasm)
+ install(TARGETS wasm ${wasm_vcpkg_libraries} COMPONENT wasm)
install(FILES "${GIT_COMMIT_FILE}" COMPONENT wasm DESTINATION .)
+ if (LADYBIRD_INSTALLS_VCPKG_LIBRARIES)
+ if (TARGET js)
+ ladybird_install_vcpkg_libraries(js_vcpkg_libraries DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT js)
+ endif()
+ ladybird_install_vcpkg_libraries(wasm_vcpkg_libraries DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT wasm)
+ endif()
+
if (LADYBIRD_ICU_DATA_FILE)
install(FILES "${LADYBIRD_ICU_DATA_FILE}" COMPONENT js DESTINATION "${CMAKE_INSTALL_DATADIR}/Lagom/icu")
install(FILES "${LADYBIRD_ICU_DATA_FILE}" COMPONENT wasm DESTINATION "${CMAKE_INSTALL_DATADIR}/Lagom/icu")