-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
151 lines (128 loc) · 4.5 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
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)
project (llama-node)
set(CMAKE_CXX_STANDARD 17)
if(NOT DEFINED napi_build_version)
set(napi_build_version 6)
endif()
add_definitions(-DNAPI_VERSION=${napi_build_version})
message(STATUS "NAPI_VERSION: ${napi_build_version}")
set(CMAKE_SYSTEM_PROCESSOR ${NODE_ARCH})
string(TOLOWER ${CMAKE_SYSTEM_NAME} PLATFORM)
string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} ARCH)
# normalize platform to nodejs
string(REPLACE "windows" "win32" PLATFORM ${PLATFORM})
# normalize arch to nodejs: 'arm', 'arm64', 'ia32', 'loong64', 'mips', 'mipsel', 'ppc', 'ppc64', 'riscv64', 's390', 's390x', and 'x64'.
string(REPLACE "amd64" "x64" ARCH ${ARCH})
string(REPLACE "x86_64" "x64" ARCH ${ARCH})
string(REPLACE "i686" "ia32" ARCH ${ARCH})
string(REPLACE "i386" "ia32" ARCH ${ARCH})
string(REPLACE "armv7l" "arm" ARCH ${ARCH})
string(REPLACE "arm" "arm" ARCH ${ARCH})
string(REPLACE "arm64x" "arm64" ARCH ${ARCH})
string(REPLACE "aarch64" "arm64" ARCH ${ARCH})
if(DEFINED VARIANT)
set(VARIANT -${VARIANT})
else()
set(VARIANT "")
endif()
set(PLATFORM_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin/${PLATFORM}${VARIANT}/${ARCH})
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Platform: ${PLATFORM}")
message(STATUS "Architecture: ${ARCH}")
message(STATUS "PLATFORM_BINARY_DIR: ${PLATFORM_BINARY_DIR}")
# set strip flags
if(CMAKE_BUILD_TYPE STREQUAL "Release")
if(UNIX OR MINGW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s")
elseif(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /s")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /s")
endif()
endif()
# flags: -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# VULKAN_SDK
if (VULKAN_SDK)
set(ENV{VULKAN_SDK} ${VULKAN_SDK})
find_package(Vulkan REQUIRED)
endif()
set(LLAMA_BUILD_COMMON ON CACHE BOOL "Build common")
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries")
add_subdirectory("src/llama.cpp")
include_directories(
${CMAKE_JS_INC}
"src/llama.cpp"
)
file(
GLOB SOURCE_FILES
"src/addons.cc"
"src/common.hpp"
"src/DisposeWorker.cpp"
"src/DisposeWorker.h"
"src/LlamaCompletionWorker.cpp"
"src/LlamaCompletionWorker.h"
"src/LlamaContext.cpp"
"src/LlamaContext.h"
"src/TokenizeWorker.cpp"
"src/TokenizeWorker.h"
"src/DetokenizeWorker.cpp"
"src/DetokenizeWorker.h"
"src/EmbeddingWorker.cpp"
"src/EmbeddingWorker.h"
"src/LoadSessionWorker.cpp"
"src/LoadSessionWorker.h"
"src/SaveSessionWorker.cpp"
"src/SaveSessionWorker.h"
)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} llama ggml common)
add_custom_target(copy_assets ALL DEPENDS ${PROJECT_NAME})
add_custom_command(
TARGET copy_assets
COMMAND ${CMAKE_COMMAND} -E remove_directory ${PLATFORM_BINARY_DIR}
COMMENT "Cleaning bin folder"
)
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
# copy target to bin folder
get_filename_component(CMAKE_JS_NODELIB_TARGET_NAME ${CMAKE_JS_NODELIB_TARGET} NAME)
add_custom_command(TARGET copy_assets
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_JS_NODELIB_TARGET} ${PLATFORM_BINARY_DIR}/${CMAKE_JS_NODELIB_TARGET_NAME}
COMMENT "Copying to bin folder"
)
endif()
# copy target to bin folder
add_custom_command(TARGET copy_assets
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT_NAME}> ${PLATFORM_BINARY_DIR}/$<TARGET_FILE_NAME:${PROJECT_NAME}>
COMMENT "Copying to bin folder"
)
if (LLAMA_METAL)
# copy ${CMAKE_BINARY_DIR}/bin/default.metallib
add_custom_command(
TARGET copy_assets
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bin/default.metallib ${PLATFORM_BINARY_DIR}/default.metallib
COMMENT "Copying default.metallib to bin folder"
)
add_dependencies(copy_assets ggml-metal)
endif()
if (LLAMA_CLBLAST)
find_package(CLBlast)
if (CLBlast_FOUND)
message(STATUS "CLBlast found: ${CLBlast_DIR}")
file(
GLOB CLBlast_SO_FILES
${CLBlast_DIR}/../../../bin/clblast.dll
${CLBlast_DIR}/../../../lib/libclblast.so
)
add_custom_command(
TARGET copy_assets
COMMAND ${CMAKE_COMMAND} -E copy ${CLBlast_SO_FILES} ${PLATFORM_BINARY_DIR}
COMMENT "Copying CLBlast SO files to bin folder"
)
endif()
endif()