2026-01-16 10:43:34 +08:00
# common
find_package ( Threads REQUIRED )
llama_add_compile_flags ( )
# Build info header
#
if ( EXISTS "${PROJECT_SOURCE_DIR}/.git" )
set ( GIT_DIR "${PROJECT_SOURCE_DIR}/.git" )
# Is git submodule
if ( NOT IS_DIRECTORY "${GIT_DIR}" )
file ( READ ${ GIT_DIR } REAL_GIT_DIR_LINK )
string ( REGEX REPLACE "gitdir: (.*)\n$" "\\1" REAL_GIT_DIR ${ REAL_GIT_DIR_LINK } )
string ( FIND "${REAL_GIT_DIR}" "/" SLASH_POS )
if ( SLASH_POS EQUAL 0 )
set ( GIT_DIR "${REAL_GIT_DIR}" )
else ( )
set ( GIT_DIR "${PROJECT_SOURCE_DIR}/${REAL_GIT_DIR}" )
endif ( )
endif ( )
if ( EXISTS "${GIT_DIR}/index" )
# For build-info.cpp below
set_property ( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${GIT_DIR}/index" )
else ( )
message ( WARNING "Git index not found in git repository." )
endif ( )
else ( )
message ( WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository." )
endif ( )
set ( TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp.in" )
set ( OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/build-info.cpp" )
configure_file ( ${ TEMPLATE_FILE } ${ OUTPUT_FILE } )
set ( TARGET build_info )
add_library ( ${ TARGET } OBJECT ${ OUTPUT_FILE } )
if ( BUILD_SHARED_LIBS )
set_target_properties ( ${ TARGET } PROPERTIES POSITION_INDEPENDENT_CODE ON )
endif ( )
set ( TARGET common )
add_library ( ${ TARGET } STATIC
a r g . c p p
a r g . h
b a s e 6 4 . h p p
c h a t - p a r s e r . c p p
c h a t - p a r s e r . h
c h a t - p a r s e r - x m l - t o o l c a l l . h
c h a t - p a r s e r - x m l - t o o l c a l l . c p p
c h a t - p e g - p a r s e r . c p p
c h a t - p e g - p a r s e r . h
c h a t . c p p
c h a t . h
c o m m o n . c p p
c o m m o n . h
c o n s o l e . c p p
c o n s o l e . h
d o w n l o a d . c p p
d o w n l o a d . h
h t t p . h
j s o n - p a r t i a l . c p p
j s o n - p a r t i a l . h
j s o n - s c h e m a - t o - g r a m m a r . c p p
l l g u i d a n c e . c p p
l o g . c p p
l o g . h
n g r a m - c a c h e . c p p
n g r a m - c a c h e . h
p e g - p a r s e r . c p p
p e g - p a r s e r . h
p r e s e t . c p p
p r e s e t . h
r e g e x - p a r t i a l . c p p
r e g e x - p a r t i a l . h
s a m p l i n g . c p p
s a m p l i n g . h
s p e c u l a t i v e . c p p
s p e c u l a t i v e . h
u n i c o d e . c p p
u n i c o d e . h
)
target_include_directories ( ${ TARGET } PUBLIC . ../vendor )
target_compile_features ( ${ TARGET } PUBLIC cxx_std_17 )
if ( BUILD_SHARED_LIBS )
set_target_properties ( ${ TARGET } PROPERTIES POSITION_INDEPENDENT_CODE ON )
endif ( )
# TODO: use list(APPEND LLAMA_COMMON_EXTRA_LIBS ...)
set ( LLAMA_COMMON_EXTRA_LIBS build_info )
2026-01-16 11:16:14 +08:00
if ( LLAMA_CURL )
# Use curl to download model url
find_package ( CURL )
if ( NOT CURL_FOUND )
message ( FATAL_ERROR "Could NOT find CURL. Hint: to disable this feature, set -DLLAMA_CURL=OFF" )
endif ( )
target_compile_definitions ( ${ TARGET } PUBLIC LLAMA_USE_CURL )
include_directories ( ${ CURL_INCLUDE_DIRS } )
set ( LLAMA_COMMON_EXTRA_LIBS ${ LLAMA_COMMON_EXTRA_LIBS } ${ CURL_LIBRARIES } )
elseif ( LLAMA_HTTPLIB )
# otherwise, use cpp-httplib
2026-01-16 10:43:34 +08:00
target_compile_definitions ( ${ TARGET } PUBLIC LLAMA_USE_HTTPLIB )
set ( LLAMA_COMMON_EXTRA_LIBS ${ LLAMA_COMMON_EXTRA_LIBS } cpp-httplib )
endif ( )
if ( LLAMA_LLGUIDANCE )
include ( ExternalProject )
set ( LLGUIDANCE_SRC ${ CMAKE_BINARY_DIR } /llguidance/source )
set ( LLGUIDANCE_PATH ${ LLGUIDANCE_SRC } /target/release )
# Set the correct library file extension based on platform
if ( WIN32 )
set ( LLGUIDANCE_LIB_NAME "llguidance.lib" )
# Add Windows-specific libraries
set ( LLGUIDANCE_PLATFORM_LIBS
w s 2 _ 3 2 # Windows Sockets API
u s e r e n v # For GetUserProfileDirectoryW
n t d l l # For NT functions
b c r y p t # For BCryptGenRandom
)
else ( )
set ( LLGUIDANCE_LIB_NAME "libllguidance.a" )
set ( LLGUIDANCE_PLATFORM_LIBS "" )
endif ( )
ExternalProject_Add ( llguidance_ext
G I T _ R E P O S I T O R Y h t t p s : / / g i t h u b . c o m / g u i d a n c e - a i / l l g u i d a n c e
# v1.0.1:
G I T _ T A G d 7 9 5 9 1 2 f e d c 7 d 3 9 3 d e 7 4 0 1 7 7 e a 9 e a 7 6 1 e 7 9 0 5 7 7 4
P R E F I X $ { C M A K E _ B I N A R Y _ D I R } / l l g u i d a n c e
S O U R C E _ D I R $ { L L G U I D A N C E _ S R C }
B U I L D _ I N _ S O U R C E T R U E
C O N F I G U R E _ C O M M A N D " "
B U I L D _ C O M M A N D c a r g o b u i l d - - r e l e a s e - - p a c k a g e l l g u i d a n c e
I N S T A L L _ C O M M A N D " "
B U I L D _ B Y P R O D U C T S $ { L L G U I D A N C E _ P A T H } / $ { L L G U I D A N C E _ L I B _ N A M E } $ { L L G U I D A N C E _ P A T H } / l l g u i d a n c e . h
U P D A T E _ C O M M A N D " "
)
target_compile_definitions ( ${ TARGET } PUBLIC LLAMA_USE_LLGUIDANCE )
add_library ( llguidance STATIC IMPORTED )
set_target_properties ( llguidance PROPERTIES IMPORTED_LOCATION ${ LLGUIDANCE_PATH } / ${ LLGUIDANCE_LIB_NAME } )
add_dependencies ( llguidance llguidance_ext )
target_include_directories ( ${ TARGET } PRIVATE ${ LLGUIDANCE_PATH } )
# Add platform libraries to the main target
set ( LLAMA_COMMON_EXTRA_LIBS ${ LLAMA_COMMON_EXTRA_LIBS } llguidance ${ LLGUIDANCE_PLATFORM_LIBS } )
endif ( )
target_link_libraries ( ${ TARGET } PRIVATE ${ LLAMA_COMMON_EXTRA_LIBS } PUBLIC llama Threads::Threads )
2026-01-16 11:16:14 +08:00
#
# copy the license files
#
# Check if running in GitHub Actions
if ( DEFINED ENV{GITHUB_ACTIONS} AND "$ENV{GITHUB_ACTIONS}" STREQUAL "true" )
message ( STATUS "Running inside GitHub Actions - copying license files" )
# Copy all files from licenses/ to build/bin/
file ( GLOB LICENSE_FILES "${CMAKE_SOURCE_DIR}/licenses/*" )
foreach ( LICENSE_FILE ${ LICENSE_FILES } )
get_filename_component ( FILENAME ${ LICENSE_FILE } NAME )
add_custom_command (
P O S T _ B U I L D
T A R G E T $ { T A R G E T }
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y _ i f _ d i f f e r e n t
" $ { L I C E N S E _ F I L E } "
" $ < T A R G E T _ F I L E _ D I R : l l a m a > / $ { F I L E N A M E } "
C O M M E N T " C o p y i n g $ { F I L E N A M E } t o $ { C M A K E _ R U N T I M E _ O U T P U T _ D I R E C T O R Y } " )
message ( STATUS "Copying ${LICENSE_FILE} to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FILENAME}" )
endforeach ( )
endif ( )