Skip to content

Commit 0014ce6

Browse files
committed
cmake: add option to build and link BoringSSL
This commit adds an easy way to fetch, build, and statically link the BoringSSL library when LLAMA_BUILD_BORINGSSL is enabled. The version can be set via the LLAMA_BORINGSSL_VERSION cache variable. Signed-off-by: Adrien Gallouët <[email protected]>
1 parent 7f09a68 commit 0014ce6

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ option(LLAMA_BUILD_SERVER "llama: build server example" ${LLAMA_STANDALONE})
9191
option(LLAMA_TOOLS_INSTALL "llama: install tools" ${LLAMA_TOOLS_INSTALL_DEFAULT})
9292

9393
# 3rd party libs
94-
option(LLAMA_CURL "llama: use libcurl to download model from an URL" ON)
95-
option(LLAMA_OPENSSL "llama: use openssl to support HTTPS" OFF)
96-
option(LLAMA_LLGUIDANCE "llama-common: include LLGuidance library for structured output in common utils" OFF)
94+
option(LLAMA_CURL "llama: use libcurl to download model from an URL" ON)
95+
option(LLAMA_OPENSSL "llama: use openssl to support HTTPS" OFF)
96+
option(LLAMA_BUILD_BORINGSSL "llama: build and statically link BoringSSL" OFF)
97+
option(LLAMA_LLGUIDANCE "llama-common: include LLGuidance library for structured output in common utils" OFF)
9798

9899
# Required for relocatable CMake package
99100
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/build-info.cmake)

common/CMakeLists.txt

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,56 @@ if (LLAMA_CURL)
9090
set(LLAMA_COMMON_EXTRA_LIBS ${LLAMA_COMMON_EXTRA_LIBS} ${CURL_LIBRARIES})
9191
endif()
9292

93-
if (LLAMA_OPENSSL)
93+
set(LLAMA_BORINGSSL_VERSION "0.20251002.0" CACHE STRING "llama: BoringSSL version")
94+
95+
if (LLAMA_BUILD_BORINGSSL)
96+
include(FetchContent)
97+
message("Building BoringSSL version ${LLAMA_BORINGSSL_VERSION}")
98+
99+
FetchContent_Declare(
100+
boringssl
101+
GIT_REPOSITORY https://boringssl.googlesource.com/boringssl
102+
GIT_TAG ${LLAMA_BORINGSSL_VERSION}
103+
)
104+
105+
# TODO: fix FetchContent_MakeAvailable with EXCLUDE_FROM_ALL
106+
if(POLICY CMP0169)
107+
cmake_policy(SET CMP0169 OLD)
108+
endif()
109+
110+
if(NOT boringssl_POPULATED)
111+
# force BUILD_SHARED_LIBS=OFF, avoid installing SSL libs
112+
set(SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
113+
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
114+
115+
FetchContent_Populate(boringssl)
116+
add_subdirectory(${boringssl_SOURCE_DIR} ${boringssl_BINARY_DIR} EXCLUDE_FROM_ALL)
117+
118+
set(BUILD_SHARED_LIBS ${SAVED_BUILD_SHARED_LIBS} CACHE BOOL "" FORCE)
119+
endif()
120+
121+
set(BORINGSSL_FLAGS
122+
-Wno-error
123+
-Wno-unused-parameter
124+
-Wno-missing-noreturn
125+
)
126+
127+
if(TARGET fipsmodule)
128+
target_compile_options(fipsmodule PRIVATE ${BORINGSSL_FLAGS})
129+
endif()
130+
131+
if(TARGET ssl)
132+
target_compile_options(ssl PRIVATE ${BORINGSSL_FLAGS})
133+
endif()
134+
135+
if(TARGET crypto)
136+
target_compile_options(crypto PRIVATE ${BORINGSSL_FLAGS})
137+
endif()
138+
139+
set(CPPHTTPLIB_OPENSSL_SUPPORT TRUE)
140+
target_link_libraries(${TARGET} PUBLIC ssl crypto)
141+
142+
elseif (LLAMA_OPENSSL)
94143
find_package(OpenSSL)
95144
if (OpenSSL_FOUND)
96145
include(CheckCSourceCompiles)
@@ -112,20 +161,24 @@ if (LLAMA_OPENSSL)
112161
set(CMAKE_REQUIRED_INCLUDES ${SAVED_CMAKE_REQUIRED_INCLUDES})
113162
if (OPENSSL_VERSION_SUPPORTED)
114163
message(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
115-
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_OPENSSL_SUPPORT)
164+
set(CPPHTTPLIB_OPENSSL_SUPPORT TRUE)
116165
target_link_libraries(${TARGET} PUBLIC OpenSSL::SSL OpenSSL::Crypto)
117-
if (APPLE AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
118-
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
119-
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation REQUIRED)
120-
find_library(SECURITY_FRAMEWORK Security REQUIRED)
121-
target_link_libraries(${TARGET} PUBLIC ${CORE_FOUNDATION_FRAMEWORK} ${SECURITY_FRAMEWORK})
122-
endif()
123166
endif()
124167
else()
125168
message(STATUS "OpenSSL not found, SSL support disabled")
126169
endif()
127170
endif()
128171

172+
if (CPPHTTPLIB_OPENSSL_SUPPORT)
173+
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_OPENSSL_SUPPORT)
174+
if (APPLE AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
175+
target_compile_definitions(${TARGET} PUBLIC CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
176+
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation REQUIRED)
177+
find_library(SECURITY_FRAMEWORK Security REQUIRED)
178+
target_link_libraries(${TARGET} PUBLIC ${CORE_FOUNDATION_FRAMEWORK} ${SECURITY_FRAMEWORK})
179+
endif()
180+
endif()
181+
129182
if (LLAMA_LLGUIDANCE)
130183
include(ExternalProject)
131184
set(LLGUIDANCE_SRC ${CMAKE_BINARY_DIR}/llguidance/source)

0 commit comments

Comments
 (0)