-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
202 lines (188 loc) · 8.68 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
cmake_minimum_required(VERSION 3.17 FATAL_ERROR)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)
project(faiss-node LANGUAGES CXX)
if (APPLE)
find_package(OpenMP)
if (NOT OpenMP_FOUND)
execute_process(COMMAND brew --prefix libomp
OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -Wno-unused-command-line-argument -I${HOMEBREW_LIBOMP_PREFIX}/include")
set(OpenMP_C_LIB_NAMES "libomp")
set(OpenMP_libomp_LIBRARY "${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib")
endif ()
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -Wno-unused-command-line-argument -I${HOMEBREW_LIBOMP_PREFIX}/include")
set(OpenMP_CXX_LIB_NAMES "libomp")
set(OpenMP_libomp_LIBRARY "${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib")
endif ()
endif ()
endif ()
# if(WIN32)
set(BUILD_TESTING OFF)
# endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 17)
set(FAISS_ENABLE_GPU OFF)
set(FAISS_ENABLE_PYTHON OFF)
if(DEFINED <napi_build_version>)
message("napi_build_version: " ${napi_build_version})
add_compile_definitions(NAPI_VERSION=${napi_build_version})
else()
add_definitions(-DNAPI_VERSION=6)
endif()
include_directories(${CMAKE_JS_INC})
file(GLOB SOURCE_FILES "src/*.cc" "src/*.h")
set(SOURCE ${SOURCE_FILES})
add_subdirectory("deps/faiss")
set_target_properties(faiss PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>
LIBRARY_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>
RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${PROJECT_NAME}>
)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# https://stackoverflow.com/questions/43330165/how-to-link-a-shared-library-with-cmake-with-relative-path/69707790#69707790
set_target_properties(${PROJECT_NAME} PROPERTIES
BUILD_WITH_INSTALL_RPATH FALSE
LINK_FLAGS "-Wl,-rpath,$ORIGIN/")
endif()
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/node_modules/node-addon-api")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} faiss)
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})
endif()
if(WIN32)
# Copy mkl_sequential.2.dll
if(DEFINED ENV{MKLROOT})
file(TO_CMAKE_PATH "$ENV{MKLROOT}\\redist\\intel64\\mkl_sequential.2.dll" MKL_sequential)
message("MKL_sequential: " ${MKL_sequential})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${MKL_sequential}
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endif()
endif()
# https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/How-To-Write-Platform-Checks#cmake-system
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Copy libmkl_intel_lp64.so, libmkl_sequential.so, libmkl_core.so
if(DEFINED ENV{MKLROOT})
set(DEPENDANCES_MKL
libmkl_intel_lp64.so
libmkl_sequential.so
libmkl_core.so)
foreach(dep ${DEPENDANCES_MKL})
file(TO_CMAKE_PATH "$ENV{MKLROOT}/lib/intel64/${dep}" MKL_dep)
message("Copy DEPENDANCES_MKL: " ${MKL_dep})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${MKL_dep}
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endforeach()
endif()
if(EXISTS "/etc/debian_version")
file(READ "/etc/issue" ETC_ISSUE)
string(REGEX MATCH "Debian|Ubuntu" DIST ${ETC_ISSUE})
if(DIST STREQUAL "Debian")
# Debian
EXECUTE_PROCESS( COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE )
message("Debian:" ${ARCHITECTURE})
if( ${ARCHITECTURE} STREQUAL "x86_64" )
# Copy libgomp.so.1, libopenblas.so.0, libgfortran.so.5, libquadmath.so.0
set(DEPENDANCES
libgomp.so.1
libopenblas.so.0
libgfortran.so.5
libquadmath.so.0)
foreach(dep ${DEPENDANCES})
file(TO_CMAKE_PATH "/usr/lib/x86_64-linux-gnu/${dep}" MUSL_dep)
message("Copy DEPENDANCES: " ${MUSL_dep})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${MUSL_dep}
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endforeach()
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND patchelf --set-rpath "\\$$ORIGIN/" $<TARGET_FILE_DIR:${PROJECT_NAME}>/libopenblas.so.0
COMMAND patchelf --set-rpath "\\$$ORIGIN/" $<TARGET_FILE_DIR:${PROJECT_NAME}>/libgfortran.so.5
)
else()
# Copy libgomp.so.1, libopenblas.so.0, libgfortran.so.5
set(DEPENDANCES
libgomp.so.1
libopenblas.so.0
libgfortran.so.5)
foreach(dep ${DEPENDANCES})
file(TO_CMAKE_PATH "/lib/aarch64-linux-gnu/${dep}" MUSL_dep)
message("Copy DEPENDANCES: " ${MUSL_dep})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${MUSL_dep}
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endforeach()
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND patchelf --set-rpath "\\$$ORIGIN/" $<TARGET_FILE_DIR:${PROJECT_NAME}>/libopenblas.so.0
COMMAND patchelf --set-rpath "\\$$ORIGIN/" $<TARGET_FILE_DIR:${PROJECT_NAME}>/libgfortran.so.5
)
endif()
endif()
endif()
if(EXISTS "/etc/alpine-release")
# Alpine
EXECUTE_PROCESS( COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE )
message("Alpine:" ${ARCHITECTURE})
if( ${ARCHITECTURE} STREQUAL "x86_64" )
# Copy libgomp.so.1, libopenblas.so.3, libgfortran.so.5, libquadmath.so.0
set(DEPENDANCES_MUSL
libgomp.so.1
libopenblas.so.3
libgfortran.so.5
libquadmath.so.0)
else()
# Copy libgomp.so.1, libopenblas.so.3, libgfortran.so.5
set(DEPENDANCES_MUSL
libgomp.so.1
libopenblas.so.3
libgfortran.so.5)
endif()
foreach(dep ${DEPENDANCES_MUSL})
file(TO_CMAKE_PATH "/usr/lib/${dep}" MUSL_dep)
message("Copy DEPENDANCES_MUSL: " ${MUSL_dep})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${MUSL_dep}
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endforeach()
endif()
endif()
if(APPLE)
# Copy libomp.dylib
message("OpenMP_libomp_LIBRARY: " ${OpenMP_libomp_LIBRARY})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${OpenMP_libomp_LIBRARY}
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
# fix loader path
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -change ${OpenMP_libomp_LIBRARY} @rpath/libomp.dylib
$<TARGET_FILE:${PROJECT_NAME}>)
# add /opt/homebrew/opt/libomp/lib into rpath for M1/M2
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -add_rpath /opt/homebrew/opt/libomp/lib
$<TARGET_FILE:${PROJECT_NAME}>)
# add /usr/local/opt/libomp/lib into rpath for Intel
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -add_rpath /usr/local/opt/libomp/lib
$<TARGET_FILE:${PROJECT_NAME}>)
# add @loader_path into rpath as a fallback
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_INSTALL_NAME_TOOL} -add_rpath @loader_path
$<TARGET_FILE:${PROJECT_NAME}>)
endif()