|
| 1 | +# DownloadCEF.cmake |
| 2 | +# |
| 3 | +# Downloads and extracts a CEF binary distribution at CMake configure time. |
| 4 | +# Based on the official chromiumembedded/cef-project approach. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# DownloadCEF(<platform> <version> <download_dir>) |
| 8 | +# |
| 9 | +# Where: |
| 10 | +# platform - "windows32" or "windows64" |
| 11 | +# version - CEF version string (e.g. "144.0.6+g5f7e671+chromium-144.0.7559.59") |
| 12 | +# download_dir - Directory to download and extract into |
| 13 | +# |
| 14 | +# Sets the following cache variable: |
| 15 | +# CEF_ROOT - Path to the extracted CEF distribution directory |
| 16 | + |
| 17 | +function(DownloadCEF platform version download_dir) |
| 18 | + # Use minimal distribution (no sample apps, significantly smaller download) |
| 19 | + set(CEF_DISTRIBUTION "cef_binary_${version}_${platform}_minimal") |
| 20 | + set(CEF_DOWNLOAD_DIR "${download_dir}") |
| 21 | + |
| 22 | + # The location where we expect the extracted binary distribution |
| 23 | + set(CEF_ROOT "${CEF_DOWNLOAD_DIR}/${CEF_DISTRIBUTION}" CACHE INTERNAL "CEF_ROOT") |
| 24 | + |
| 25 | + # Skip if already extracted |
| 26 | + if(IS_DIRECTORY "${CEF_ROOT}") |
| 27 | + message(STATUS "CEF: Using existing distribution at ${CEF_ROOT}") |
| 28 | + return() |
| 29 | + endif() |
| 30 | + |
| 31 | + set(CEF_DOWNLOAD_FILENAME "${CEF_DISTRIBUTION}.tar.bz2") |
| 32 | + set(CEF_DOWNLOAD_PATH "${CEF_DOWNLOAD_DIR}/${CEF_DOWNLOAD_FILENAME}") |
| 33 | + |
| 34 | + # Build URL with '+' encoded as '%2B' |
| 35 | + set(CEF_DOWNLOAD_URL "https://cef-builds.spotifycdn.com/${CEF_DOWNLOAD_FILENAME}") |
| 36 | + string(REPLACE "+" "%2B" CEF_DOWNLOAD_URL_ESCAPED "${CEF_DOWNLOAD_URL}") |
| 37 | + |
| 38 | + if(NOT EXISTS "${CEF_DOWNLOAD_PATH}") |
| 39 | + # Download SHA1 hash for verification |
| 40 | + message(STATUS "CEF: Downloading checksum ...") |
| 41 | + file(DOWNLOAD "${CEF_DOWNLOAD_URL_ESCAPED}.sha1" "${CEF_DOWNLOAD_PATH}.sha1" |
| 42 | + STATUS DOWNLOAD_SHA1_STATUS |
| 43 | + ) |
| 44 | + list(GET DOWNLOAD_SHA1_STATUS 0 SHA1_STATUS_CODE) |
| 45 | + if(NOT SHA1_STATUS_CODE EQUAL 0) |
| 46 | + list(GET DOWNLOAD_SHA1_STATUS 1 SHA1_ERROR_MSG) |
| 47 | + file(REMOVE "${CEF_DOWNLOAD_PATH}.sha1") |
| 48 | + message(FATAL_ERROR |
| 49 | + "CEF: Failed to download SHA1 checksum: ${SHA1_ERROR_MSG}\n" |
| 50 | + " URL: ${CEF_DOWNLOAD_URL_ESCAPED}.sha1\n" |
| 51 | + " Check that CEF_VERSION is valid at https://cef-builds.spotifycdn.com/index.html" |
| 52 | + ) |
| 53 | + endif() |
| 54 | + |
| 55 | + file(READ "${CEF_DOWNLOAD_PATH}.sha1" CEF_SHA1) |
| 56 | + string(STRIP "${CEF_SHA1}" CEF_SHA1) |
| 57 | + |
| 58 | + # Validate SHA1 looks like a 40-char hex string |
| 59 | + string(LENGTH "${CEF_SHA1}" SHA1_LEN) |
| 60 | + if(NOT SHA1_LEN EQUAL 40) |
| 61 | + file(REMOVE "${CEF_DOWNLOAD_PATH}.sha1") |
| 62 | + message(FATAL_ERROR |
| 63 | + "CEF: Downloaded SHA1 checksum is invalid (length ${SHA1_LEN}, expected 40).\n" |
| 64 | + " URL: ${CEF_DOWNLOAD_URL_ESCAPED}.sha1\n" |
| 65 | + " Check that CEF_VERSION is valid at https://cef-builds.spotifycdn.com/index.html" |
| 66 | + ) |
| 67 | + endif() |
| 68 | + |
| 69 | + # Download the binary distribution with hash verification |
| 70 | + message(STATUS "CEF: Downloading ${CEF_DOWNLOAD_FILENAME} (~100MB, this may take a while) ...") |
| 71 | + file(DOWNLOAD "${CEF_DOWNLOAD_URL_ESCAPED}" "${CEF_DOWNLOAD_PATH}" |
| 72 | + EXPECTED_HASH SHA1=${CEF_SHA1} |
| 73 | + SHOW_PROGRESS |
| 74 | + STATUS DOWNLOAD_STATUS |
| 75 | + ) |
| 76 | + list(GET DOWNLOAD_STATUS 0 STATUS_CODE) |
| 77 | + if(NOT STATUS_CODE EQUAL 0) |
| 78 | + list(GET DOWNLOAD_STATUS 1 ERROR_MSG) |
| 79 | + file(REMOVE "${CEF_DOWNLOAD_PATH}") |
| 80 | + message(FATAL_ERROR "CEF: Download failed: ${ERROR_MSG}\n URL: ${CEF_DOWNLOAD_URL_ESCAPED}") |
| 81 | + endif() |
| 82 | + |
| 83 | + message(STATUS "CEF: Download complete.") |
| 84 | + else() |
| 85 | + message(STATUS "CEF: Using cached archive ${CEF_DOWNLOAD_PATH}") |
| 86 | + endif() |
| 87 | + |
| 88 | + # Extract the binary distribution |
| 89 | + message(STATUS "CEF: Extracting ${CEF_DOWNLOAD_FILENAME} ...") |
| 90 | + execute_process( |
| 91 | + COMMAND ${CMAKE_COMMAND} -E tar xzf "${CEF_DOWNLOAD_PATH}" |
| 92 | + WORKING_DIRECTORY "${CEF_DOWNLOAD_DIR}" |
| 93 | + RESULT_VARIABLE EXTRACT_RESULT |
| 94 | + ) |
| 95 | + if(NOT EXTRACT_RESULT EQUAL 0) |
| 96 | + message(FATAL_ERROR "CEF: Extraction failed (exit code ${EXTRACT_RESULT})") |
| 97 | + endif() |
| 98 | + |
| 99 | + # Verify extraction succeeded |
| 100 | + if(NOT IS_DIRECTORY "${CEF_ROOT}") |
| 101 | + message(FATAL_ERROR "CEF: Extraction completed but expected directory not found: ${CEF_ROOT}") |
| 102 | + endif() |
| 103 | + |
| 104 | + message(STATUS "CEF: Ready at ${CEF_ROOT}") |
| 105 | +endfunction() |
0 commit comments