11# LiveKitSDK.cmake
22#
3- # A small helper for example repos:
3+ # Helper for example repos:
44# - Downloads the appropriate prebuilt LiveKit C++ SDK release asset for the host OS/arch
5- # - Extracts it into a local directory (default: build/_deps/livekit-sdk)
6- # - Prepends the extracted prefix to CMAKE_PREFIX_PATH so find_package(LiveKit CONFIG REQUIRED) works
5+ # - Extracts it into a local directory (default: <build>/_deps/livekit-sdk)
6+ # - Prepends the extracted prefix to CMAKE_PREFIX_PATH so:
7+ # find_package(LiveKit CONFIG REQUIRED)
8+ # works out of the box.
79#
810# Usage:
911# list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
1012# include(LiveKitSDK)
11- # livekit_sdk_setup(VERSION "0.1.9" SDK_DIR "${CMAKE_BINARY_DIR}/_deps/livekit-sdk")
12- #
13- # Latest:
1413# livekit_sdk_setup(VERSION "latest" SDK_DIR "${CMAKE_BINARY_DIR}/_deps/livekit-sdk")
1514#
1615# Optional:
1716# livekit_sdk_setup(VERSION "latest" REPO "livekit/client-sdk-cpp" GITHUB_TOKEN "$ENV{GITHUB_TOKEN}")
1817
1918include_guard (GLOBAL )
2019
20+ # -------------------- Host detection --------------------
2121function (_lk_detect_host out_os out_arch )
2222 if (WIN32 )
2323 set (_os "windows" )
@@ -29,10 +29,13 @@ function(_lk_detect_host out_os out_arch)
2929 message (FATAL_ERROR "LiveKitSDK: unsupported host OS" )
3030 endif ()
3131
32+ # Use host processor; normalize common variants (case-insensitive)
3233 set (_proc "${CMAKE_HOST_SYSTEM_PROCESSOR } " )
33- if (_proc MATCHES "^(x86_64|AMD64)$" )
34+ string (TOLOWER "${_proc} " _proc_l)
35+
36+ if (_proc_l MATCHES "^(x86_64|amd64)$" )
3437 set (_arch "x64" )
35- elseif (_proc MATCHES "^(arm64|aarch64)$" )
38+ elseif (_proc_l MATCHES "^(arm64|aarch64)$" )
3639 set (_arch "arm64" )
3740 else ()
3841 message (FATAL_ERROR "LiveKitSDK: unsupported host arch: ${_proc} " )
@@ -44,8 +47,7 @@ endfunction()
4447
4548function (_lk_default_triple out_triple )
4649 _lk_detect_host (_os _arch )
47- set (_triple "${_os} -${_arch} " )
48- set (${out_triple} "${_triple} " PARENT_SCOPE )
50+ set (${out_triple} "${_os} -${_arch} " PARENT_SCOPE )
4951endfunction ()
5052
5153function (_lk_archive_ext out_ext )
@@ -57,7 +59,8 @@ function(_lk_archive_ext out_ext)
5759 endif ()
5860endfunction ()
5961
60- # Resolve VERSION="latest" via GitHub API, returning a version without leading "v".
62+ # -------------------- GitHub API helpers --------------------
63+ # Resolve VERSION="latest" via GitHub API, returning version without leading "v".
6164function (_lk_resolve_latest_version out_version repo download_dir github_token )
6265 if (NOT download_dir)
6366 set (download_dir "${CMAKE_BINARY_DIR } /_downloads" )
@@ -66,40 +69,56 @@ function(_lk_resolve_latest_version out_version repo download_dir github_token)
6669
6770 set (_api "https://api.github.com/repos/${repo} /releases/latest" )
6871
69- # Sanitize only the repo part (NOT the whole path)
72+ # Sanitize repo for filename
7073 string (REPLACE "/" "_" _repo_sanitized "${repo} " )
7174 set (_json "${download_dir} /livekit_latest_release_${_repo_sanitized} .json" )
7275
73- # Build headers as a proper argument list (quoted per header)
76+ # Build headers as a proper LIST (each element is one full header line )
7477 set (_headers
7578 "User-Agent: cmake-livekit-sdk/1.0"
7679 "Accept: application/vnd.github+json"
80+ "X-GitHub-Api-Version: 2022-11-28"
7781 )
82+
83+ # Strip token (defensive: avoids accidental newline causing header splitting)
84+ if (NOT "${github_token} " STREQUAL "" )
85+ string (STRIP "${github_token} " github_token)
86+ endif ()
87+
88+ # Use Authorization only if token is non-empty
7889 if (NOT "${github_token} " STREQUAL "" )
79- list (APPEND _headers "Authorization: Bearer ${github_token} " )
90+ # "token" is broadly compatible
91+ list (APPEND _headers "Authorization: token ${github_token} " )
92+ else ()
93+ message (STATUS "LiveKitSDK: no GITHUB_TOKEN provided; GitHub API may rate-limit." )
8094 endif ()
8195
82- # Pass headers safely: keep each header as ONE argument
83- set (_download_args
96+ # Capture LOG for actionable failure output
97+ set (_dl_args
8498 TLS_VERIFY ON
8599 STATUS _st
100+ LOG _log
86101 )
87- list (APPEND _download_args HTTPHEADER)
88- foreach (h IN LISTS _headers)
89- list (APPEND _download_args "${h } " )
102+ list (APPEND _dl_args HTTPHEADER)
103+ foreach (_h IN LISTS _headers)
104+ list (APPEND _dl_args "${_h } " )
90105 endforeach ()
91-
92- file (DOWNLOAD "${_api} " "${_json} " ${_download_args} )
106+ file (DOWNLOAD "${_api} " "${_json} " ${_dl_args} )
93107
94108 list (GET _st 0 _code)
95109 list (GET _st 1 _msg)
96110 if (NOT _code EQUAL 0)
111+ message (STATUS "LiveKitSDK: GitHub API download log:\n ${_log} " )
112+ if (EXISTS "${_json} " )
113+ file (READ "${_json} " _body )
114+ message (STATUS "LiveKitSDK: GitHub API response body:\n ${_body} " )
115+ endif ()
97116 message (FATAL_ERROR
98117 "LiveKitSDK: failed to query latest release from GitHub API\n "
99118 "API: ${_api} \n "
100119 "Status: ${_code} \n "
101120 "Message: ${_msg} \n "
102- "Tip: set GITHUB_TOKEN to avoid rate limits."
121+ "Tip: set GITHUB_TOKEN to avoid rate limits, or use VERSION=<fixed> ."
103122 )
104123 endif ()
105124
@@ -111,13 +130,12 @@ function(_lk_resolve_latest_version out_version repo download_dir github_token)
111130 message (FATAL_ERROR "LiveKitSDK: GitHub API response missing tag_name" )
112131 endif ()
113132
114- # Strip leading "v" if present (v0.2.0 -> 0.2.0)
133+ # Strip leading "v" if present
115134 string (REGEX REPLACE "^v" "" _ver "${_tag} " )
116135 set (${out_version} "${_ver} " PARENT_SCOPE )
117136endfunction ()
118137
119-
120- # Public:
138+ # -------------------- Public entrypoint --------------------
121139# livekit_sdk_setup(
122140# VERSION <ver|latest>
123141# SDK_DIR <dir>
@@ -131,40 +149,35 @@ endfunction()
131149function (livekit_sdk_setup )
132150 set (options NO_DOWNLOAD)
133151 set (oneValueArgs VERSION SDK_DIR REPO SHA256 TRIPLE DOWNLOAD_DIR GITHUB_TOKEN)
134- set (multiValueArgs)
135- cmake_parse_arguments (LK "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
152+ cmake_parse_arguments (LK "${options} " "${oneValueArgs} " "" ${ARGN} )
136153
137154 if (NOT LK_VERSION)
138155 message (FATAL_ERROR "livekit_sdk_setup: VERSION is required (use \" latest\" if desired)" )
139156 endif ()
140-
141157 if (NOT LK_SDK_DIR)
142158 message (FATAL_ERROR "livekit_sdk_setup: SDK_DIR is required" )
143159 endif ()
144160
145161 if (NOT LK_REPO)
146162 set (LK_REPO "livekit/client-sdk-cpp" )
147163 endif ()
148-
149164 if (NOT LK_DOWNLOAD_DIR)
150165 set (LK_DOWNLOAD_DIR "${CMAKE_BINARY_DIR } /_downloads" )
151166 endif ()
152-
153167 if (NOT LK_TRIPLE)
154168 _lk_default_triple (LK_TRIPLE )
155169 endif ()
156170
157- # If VERSION is " latest", resolve it first.
171+ # Resolve latest tag if requested
158172 set (_resolved_version "${LK_VERSION} " )
159173 if (LK_VERSION STREQUAL "latest" )
160- # SHA256 cannot be reliably used with "latest" (changes over time).
161174 if (LK_SHA256)
162175 message (WARNING "LiveKitSDK: SHA256 was provided but VERSION=latest; ignoring SHA256." )
163176 set (LK_SHA256 "" )
164177 endif ()
165178
166179 if (NOT LK_GITHUB_TOKEN)
167- # Try env var by default (common in GitHub Actions).
180+ # Common in CI if you set env: GITHUB_TOKEN: ${{ github.token }}
168181 set (LK_GITHUB_TOKEN "$ENV{GITHUB_TOKEN} " )
169182 endif ()
170183
@@ -176,13 +189,13 @@ function(livekit_sdk_setup)
176189 set (_archive "livekit-sdk-${LK_TRIPLE} -${_resolved_version} .${_ext} " )
177190 set (_url "https://github.com/${LK_REPO} /releases/download/v${_resolved_version} /${_archive} " )
178191
179- set (_dl_dir "${LK_DOWNLOAD_DIR} " )
180- set (_archive_path "${_dl_dir} /${_archive} " )
192+ set (_archive_path "${LK_DOWNLOAD_DIR} /${_archive} " )
181193
182- # Extracted root folder name (matches your bundle root)
194+ # The archive is expected to contain a top-level folder named:
195+ # livekit-sdk-<triple>-<version>/
183196 set (_extracted_root "${LK_SDK_DIR} /livekit-sdk-${LK_TRIPLE} -${_resolved_version} " )
184197
185- file (MAKE_DIRECTORY "${_dl_dir } " )
198+ file (MAKE_DIRECTORY "${LK_DOWNLOAD_DIR } " )
186199 file (MAKE_DIRECTORY "${LK_SDK_DIR} " )
187200
188201 if (NOT EXISTS "${_extracted_root} " )
@@ -194,30 +207,35 @@ function(livekit_sdk_setup)
194207 endif ()
195208
196209 message (STATUS "LiveKitSDK: downloading ${_url} " )
210+
197211 if (LK_SHA256)
198212 file (DOWNLOAD "${_url} " "${_archive_path} "
199213 SHOW_PROGRESS
200214 TLS_VERIFY ON
201215 EXPECTED_HASH "SHA256=${LK_SHA256} "
202216 STATUS _st
217+ LOG _log
203218 )
204219 else ()
205220 file (DOWNLOAD "${_url} " "${_archive_path} "
206221 SHOW_PROGRESS
207222 TLS_VERIFY ON
208223 STATUS _st
224+ LOG _log
209225 )
210226 endif ()
211227
212228 list (GET _st 0 _code)
213229 list (GET _st 1 _msg)
214230 if (NOT _code EQUAL 0)
231+ message (STATUS "LiveKitSDK: download log:\n ${_log} " )
215232 message (FATAL_ERROR "LiveKitSDK: download failed\n URL: ${_url} \n Status: ${_code} \n Message: ${_msg} " )
216233 endif ()
217234
218- message ( STATUS "LiveKitSDK: extracting ${_archive_path} " )
235+ # Remove any previous partial extraction
219236 file (REMOVE_RECURSE "${_extracted_root} " )
220237
238+ message (STATUS "LiveKitSDK: extracting ${_archive_path} " )
221239 file (ARCHIVE_EXTRACT
222240 INPUT "${_archive_path} "
223241 DESTINATION "${LK_SDK_DIR} "
0 commit comments