Files
bun.sh/cmake/tools/SetupWebKit.cmake
Claude Bot 07371ca964 Improve CMake download/extract reliability by switching to curl/tar
This improves reliability of WebKit downloads that were causing intermittent
build failures with errors like:

    ninja: error: '<dir>/webkit-<sha>/lib/libWTF.a', needed by 'bun-profile', missing and no known rule to make it

Root cause: CMake's file(DOWNLOAD) and file(ARCHIVE_EXTRACT) can have timing
issues when downloads/extracts are interrupted or when filesystems are slow.

Changes:
- Replace file(DOWNLOAD) with curl in DownloadUrl.cmake for more reliable downloads
- Replace file(ARCHIVE_EXTRACT) with tar/unzip for more atomic extraction
- Update SetupWebKit.cmake to use the DownloadUrl.cmake script wrapper
- Add proper error handling and directory creation

Note: This still runs during CMake's configure phase (not build phase) because
the library files must be known at configure time for target_link_libraries().
Moving to build phase would require significant refactoring of how WebKit
libraries are referenced. The curl/tar approach provides better atomicity and
error handling than the built-in file() commands.

Testing:
- Deleted WebKit cache and verified clean build works
- WebKit downloads and extracts correctly using curl/tar
- Build completes successfully with all libraries present

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-30 03:11:48 +00:00

113 lines
3.4 KiB
CMake

option(WEBKIT_VERSION "The version of WebKit to use")
option(WEBKIT_LOCAL "If a local version of WebKit should be used instead of downloading")
if(NOT WEBKIT_VERSION)
set(WEBKIT_VERSION 6d0f3aac0b817cc01a846b3754b21271adedac12)
endif()
string(SUBSTRING ${WEBKIT_VERSION} 0 16 WEBKIT_VERSION_PREFIX)
if(WEBKIT_LOCAL)
set(DEFAULT_WEBKIT_PATH ${VENDOR_PATH}/WebKit/WebKitBuild/${CMAKE_BUILD_TYPE})
else()
set(DEFAULT_WEBKIT_PATH ${CACHE_PATH}/webkit-${WEBKIT_VERSION_PREFIX})
endif()
option(WEBKIT_PATH "The path to the WebKit directory")
if(NOT WEBKIT_PATH)
set(WEBKIT_PATH ${DEFAULT_WEBKIT_PATH})
endif()
set(WEBKIT_INCLUDE_PATH ${WEBKIT_PATH}/include)
set(WEBKIT_LIB_PATH ${WEBKIT_PATH}/lib)
if(WEBKIT_LOCAL)
if(EXISTS ${WEBKIT_PATH}/cmakeconfig.h)
# You may need to run:
# make jsc-compile-debug jsc-copy-headers
include_directories(
${WEBKIT_PATH}
${WEBKIT_PATH}/JavaScriptCore/Headers/JavaScriptCore
${WEBKIT_PATH}/JavaScriptCore/PrivateHeaders
${WEBKIT_PATH}/bmalloc/Headers
${WEBKIT_PATH}/WTF/Headers
${WEBKIT_PATH}/JavaScriptCore/DerivedSources/inspector
${WEBKIT_PATH}/JavaScriptCore/PrivateHeaders/JavaScriptCore
)
endif()
# After this point, only prebuilt WebKit is supported
return()
endif()
if(WIN32)
set(WEBKIT_OS "windows")
elseif(APPLE)
set(WEBKIT_OS "macos")
elseif(UNIX)
set(WEBKIT_OS "linux")
else()
message(FATAL_ERROR "Unsupported operating system: ${CMAKE_SYSTEM_NAME}")
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
set(WEBKIT_ARCH "arm64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64|x64|AMD64")
set(WEBKIT_ARCH "amd64")
else()
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
if(LINUX AND ABI STREQUAL "musl")
set(WEBKIT_SUFFIX "-musl")
endif()
if(DEBUG)
set(WEBKIT_SUFFIX "${WEBKIT_SUFFIX}-debug")
elseif(ENABLE_LTO)
set(WEBKIT_SUFFIX "${WEBKIT_SUFFIX}-lto")
else()
set(WEBKIT_SUFFIX "${WEBKIT_SUFFIX}")
endif()
if(ENABLE_ASAN)
# We cannot mix and match ASan Bun + non-ASan WebKit, or vice versa, because some WebKit classes
# change their layout according to whether ASan is used, for example:
# https://github.com/oven-sh/WebKit/blob/eda8b0fb4fb1aa23db9c2b00933df8b58bcdd289/Source/WTF/wtf/Vector.h#L682
set(WEBKIT_SUFFIX "${WEBKIT_SUFFIX}-asan")
endif()
setx(WEBKIT_NAME bun-webkit-${WEBKIT_OS}-${WEBKIT_ARCH}${WEBKIT_SUFFIX})
set(WEBKIT_FILENAME ${WEBKIT_NAME}.tar.gz)
setx(WEBKIT_DOWNLOAD_URL https://github.com/oven-sh/WebKit/releases/download/autobuild-${WEBKIT_VERSION}/${WEBKIT_FILENAME})
if(EXISTS ${WEBKIT_PATH}/package.json)
file(READ ${WEBKIT_PATH}/package.json WEBKIT_PACKAGE_JSON)
if(WEBKIT_PACKAGE_JSON MATCHES ${WEBKIT_VERSION})
return()
endif()
endif()
# Use DownloadUrl.cmake script for reliable download/extract with curl/tar
# This still runs at configure time, but curl/tar are more atomic than file() commands
execute_process(
COMMAND
${CMAKE_COMMAND}
-DDOWNLOAD_URL=${WEBKIT_DOWNLOAD_URL}
-DDOWNLOAD_PATH=${WEBKIT_PATH}
-P ${CMAKE_CURRENT_LIST_DIR}/../scripts/DownloadUrl.cmake
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE WEBKIT_DOWNLOAD_ERROR
RESULT_VARIABLE WEBKIT_DOWNLOAD_RESULT
)
if(NOT WEBKIT_DOWNLOAD_RESULT EQUAL 0)
message(FATAL_ERROR "WebKit download failed: ${WEBKIT_DOWNLOAD_ERROR}")
endif()
if(APPLE)
file(REMOVE_RECURSE ${WEBKIT_INCLUDE_PATH}/unicode)
endif()