forked from NexaAI/nexa-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
210 lines (193 loc) · 7.56 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
cmake_minimum_required(VERSION 3.16)
project(nexa_gguf)
include(ExternalProject)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 17)
# Function to collect all user-defined options
function(get_all_options output_var)
get_cmake_property(variables VARIABLES)
set(options)
foreach(var ${variables})
if(var MATCHES "^[A-Z]" AND NOT var MATCHES "^CMAKE_" AND NOT var MATCHES "^_")
list(APPEND options "-D${var}=${${var}}")
endif()
endforeach()
set(${output_var} ${options} PARENT_SCOPE)
endfunction()
# Create empty file if GGML_CUDA or GGML_METAL is ON
if (GGML_CUDA OR GGML_METAL OR GGML_HIPBLAS OR GGML_VULKAN)
set(SOURCE_EMPTY_FILE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/nexa/gguf/lib/empty_file.txt")
add_custom_command(
OUTPUT ${SOURCE_EMPTY_FILE_PATH}
COMMAND ${CMAKE_COMMAND} -E touch ${SOURCE_EMPTY_FILE_PATH}
COMMENT "Creating an empty file to source folder because gpu option is ON"
)
set(WHEEL_EMPTY_FILE_PATH "${SKBUILD_PLATLIB_DIR}/nexa/gguf/lib/empty_file.txt")
add_custom_command(
OUTPUT ${WHEEL_EMPTY_FILE_PATH}
COMMAND ${CMAKE_COMMAND} -E touch ${WHEEL_EMPTY_FILE_PATH}
COMMENT "Creating an empty file to lib folder because gpu option is ON"
)
add_custom_target(create_empty_file ALL DEPENDS ${SOURCE_EMPTY_FILE_PATH} ${WHEEL_EMPTY_FILE_PATH})
endif()
# Function to set up common installation paths
function(setup_install_paths target install_dir)
install(
TARGETS ${target}
LIBRARY DESTINATION ${install_dir}
RUNTIME DESTINATION ${install_dir}
ARCHIVE DESTINATION ${install_dir}
)
endfunction()
# Collect all user-defined options
get_all_options(USER_DEFINED_OPTIONS)
if(APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path")
else()
set(CMAKE_INSTALL_RPATH "$ORIGIN")
endif()
# Define common CMake options
set(COMMON_CMAKE_OPTIONS
-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE
-DCMAKE_SKIP_BUILD_RPATH=FALSE
-DCMAKE_SKIP_RPATH=FALSE
-DCMAKE_INSTALL_RPATH=${CMAKE_INSTALL_RPATH}
)
# stable_diffusion_cpp project
option(STABLE_DIFFUSION_BUILD "Build stable-diffusion.cpp" ON)
if(STABLE_DIFFUSION_BUILD)
ExternalProject_Add(stable_diffusion_project
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependency/stable-diffusion.cpp
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_build
CMAKE_ARGS
${USER_DEFINED_OPTIONS}
${COMMON_CMAKE_OPTIONS}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_install
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_CXX_STANDARD=17
-DSD_BUILD_SHARED_LIBS=ON
-DBUILD_SHARED_LIBS=ON
-DSD_METAL=${GGML_METAL}
-DSD_CUBLAS=${GGML_CUDA}
BUILD_ALWAYS 1
INSTALL_COMMAND
${CMAKE_COMMAND} --build . --target install
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<IF:$<PLATFORM_ID:Windows>,
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_build/bin/$<CONFIG>/stable-diffusion${CMAKE_SHARED_LIBRARY_SUFFIX},
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_build/bin/libstable-diffusion${CMAKE_SHARED_LIBRARY_SUFFIX}>
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_install/lib/
LOG_INSTALL 1
)
endif()
# llama_cpp project
option(LLAMA_BUILD "Build llama.cpp" ON)
if(LLAMA_BUILD)
set(LLAMA_CUDA ${GGML_CUDA})
set(LLAMA_METAL ${GGML_METAL})
ExternalProject_Add(llama_project
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependency/llama.cpp
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/llama_build
CMAKE_ARGS
${USER_DEFINED_OPTIONS}
${COMMON_CMAKE_OPTIONS}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/llama_install
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_CXX_STANDARD=17
-DLLAMA_CUDA=${LLAMA_CUDA}
-DLLAMA_METAL=${LLAMA_METAL}
BUILD_ALWAYS 1
INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install
)
if (APPLE AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
ExternalProject_Add_Step(llama_project disable_optimizations
COMMAND ${CMAKE_COMMAND} -E echo "Disabling optimizations for x86_64 Apple"
COMMAND ${CMAKE_COMMAND} -DGGML_AVX=Off -DGGML_AVX2=Off -DGGML_FMA=Off -DGGML_F16C=Off ${CMAKE_CURRENT_BINARY_DIR}/llama_build
DEPENDEES configure
DEPENDERS build
)
endif()
if (APPLE)
ExternalProject_Add_Step(llama_project enable_metal_embed
COMMAND ${CMAKE_COMMAND} -E echo "Enabling Metal library embedding for Apple"
COMMAND ${CMAKE_COMMAND} -DGGML_METAL_EMBED_LIBRARY=On ${CMAKE_CURRENT_BINARY_DIR}/llama_build
DEPENDEES configure
DEPENDERS build
)
endif()
endif()
# bark_cpp project
option(BARK_BUILD "Build bark.cpp" ON)
if(BARK_BUILD)
ExternalProject_Add(bark_project
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependency/bark.cpp
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bark_build
CMAKE_ARGS
${USER_DEFINED_OPTIONS}
${COMMON_CMAKE_OPTIONS}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/bark_install
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_CXX_STANDARD=17
-DGGML_CUDA=${GGML_CUDA}
-DGGML_METAL=${GGML_METAL}
-DBUILD_SHARED_LIBS=ON
-DBARK_BUILD_EXAMPLES=OFF
BUILD_ALWAYS 1
INSTALL_COMMAND
${CMAKE_COMMAND} --build . --target install
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<IF:$<PLATFORM_ID:Windows>,
${CMAKE_CURRENT_BINARY_DIR}/bark_build/bin/$<CONFIG>/ggml${CMAKE_SHARED_LIBRARY_SUFFIX},
${CMAKE_CURRENT_BINARY_DIR}/bark_build/lib/libggml${CMAKE_SHARED_LIBRARY_SUFFIX}>
${CMAKE_CURRENT_BINARY_DIR}/bark_install/$<IF:$<PLATFORM_ID:Windows>,bin,lib>/
)
endif()
# Install the built libraries to the final destination
if(WIN32)
install(
DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_install/lib/
${CMAKE_CURRENT_BINARY_DIR}/llama_install/bin/
${CMAKE_CURRENT_BINARY_DIR}/bark_install/bin/
DESTINATION ${SKBUILD_PLATLIB_DIR}/nexa/gguf/lib
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.dll"
)
install(
DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_install/lib/
${CMAKE_CURRENT_BINARY_DIR}/llama_install/bin/
${CMAKE_CURRENT_BINARY_DIR}/bark_install/bin/
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/nexa/gguf/lib
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.dll"
)
else()
install(
DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_install/lib/
${CMAKE_CURRENT_BINARY_DIR}/llama_install/lib/
${CMAKE_CURRENT_BINARY_DIR}/bark_install/lib/
DESTINATION ${SKBUILD_PLATLIB_DIR}/nexa/gguf/lib
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.so"
PATTERN "*.dylib"
)
install(
DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/stable_diffusion_install/lib/
${CMAKE_CURRENT_BINARY_DIR}/llama_install/lib/
${CMAKE_CURRENT_BINARY_DIR}/bark_install/lib/
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/nexa/gguf/lib
USE_SOURCE_PERMISSIONS
FILES_MATCHING
PATTERN "*.so"
PATTERN "*.dylib"
)
endif()