forked from argosopentech/MetalTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
184 lines (157 loc) · 6.41 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
cmake_minimum_required(VERSION 3.13)
project(MetalTranslate)
include(ExternalProject)
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external)
if(APPLE)
# Set ICU root directory (where Homebrew installed ICU)
execute_process(
COMMAND brew --prefix icu4c
OUTPUT_VARIABLE ICU_ROOT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Manually specify the include and library directories for ICU
set(ICU_INCLUDE_DIR ${ICU_ROOT}/include)
set(ICU_LIBRARY_DIR ${ICU_ROOT}/lib)
# Find and link ICU
include_directories(${ICU_INCLUDE_DIR})
find_package(ICU REQUIRED COMPONENTS uc i18n data)
# Execute brew command to get the ONEDNN_ROOT
execute_process(
COMMAND brew --prefix onednn
OUTPUT_VARIABLE ONEDNN_ROOT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Include oneDNN directory and library
include_directories(${ONEDNN_ROOT}/include)
message(STATUS "CMAKE DIR: ${CMAKE_CURRENT_LIST_DIR}")
elseif(UNIX)
# find libdnnl.so
execute_process(
COMMAND dpkg --listfiles libdnnl-dev
COMMAND grep libdnnl.so
OUTPUT_VARIABLE LIBDNNL_SO
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(APPLE)
ExternalProject_Add(CTranslate2
PREFIX third_party/CTranslate2
GIT_REPOSITORY https://github.com/OpenNMT/CTranslate2
GIT_TAG v4.3.1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DWITH_MKL=OFF -DWITH_DNNL=ON -DOPENMP_RUNTIME=NONE
INSTALL_RPATH "@loader_path"
BUILD_WITH_INSTALL_RPATH TRUE
)
elseif(UNIX)
ExternalProject_Add(CTranslate2
PREFIX third_party/CTranslate2
GIT_REPOSITORY https://github.com/OpenNMT/CTranslate2
GIT_TAG v4.3.1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DWITH_MKL=OFF -DWITH_DNNL=ON -DOPENMP_RUNTIME=NONE
INSTALL_RPATH "$$ORIGIN"
BUILD_WITH_INSTALL_RPATH TRUE
)
else()
ExternalProject_Add(CTranslate2
PREFIX third_party/CTranslate2
GIT_REPOSITORY https://github.com/OpenNMT/CTranslate2
GIT_TAG v4.3.1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DWITH_MKL=OFF -DMKL_CBWR=COMPATIBLE -DWITH_DNNL=ON -DOPENMP_RUNTIME=COMP
)
endif()
if(UNIX)
ExternalProject_Add(Tokenizer
PREFIX third_party/Tokenizer
GIT_REPOSITORY https://github.com/OpenNMT/Tokenizer
GIT_TAG v1.37.1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
-DCMAKE_CXX_FLAGS="-Wno-enum-constexpr-conversion"
CXX_STANDARD 11
)
else()
ExternalProject_Add(Tokenizer
PREFIX third_party/Tokenizer
GIT_REPOSITORY https://github.com/OpenNMT/Tokenizer
GIT_TAG v1.37.1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
CXX_STANDARD 11
)
endif()
include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
add_library(metaltranslate SHARED
src/MetalTranslate.cpp
src/metal_api.cpp
)
target_include_directories(metaltranslate PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${EXTERNAL_INSTALL_LOCATION}/include
)
# target_compile_definitions(metaltranslate PRIVATE METALTRANSLATE_EXPORTS)
add_dependencies(metaltranslate CTranslate2 Tokenizer)
if(WIN32)
add_definitions(-DMETALTRANSLATE_EXPORTS)
endif()
message("DIRS: ")
message(STATUS "${EXTERNAL_INSTALL_LOCATION}")
message(STATUS "${CMAKE_CURRENT_BINARY_DIR}")
message(STATUS "${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "${LIBDNNL_SO}")
target_link_libraries(metaltranslate PRIVATE ctranslate2 OpenNMTTokenizer)
# Ensure rpath is set to include the directory where dependencies will be located
if(APPLE)
set_target_properties(metaltranslate PROPERTIES
CXX_EXTENSIONS NO
CXX_STANDARD 17
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
INSTALL_RPATH "@loader_path"
BUILD_WITH_INSTALL_RPATH TRUE
)
elseif(UNIX)
set_target_properties(metaltranslate PROPERTIES
CXX_EXTENSIONS NO
CXX_STANDARD 17
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
INSTALL_RPATH "$ORIGIN"
BUILD_WITH_INSTALL_RPATH TRUE
)
else()
set_target_properties(metaltranslate PROPERTIES
CXX_EXTENSIONS NO
CXX_STANDARD 17
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
endif()
if(APPLE)
# Set RPATH for all binaries
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
list(APPEND CMAKE_INSTALL_RPATH "@loader_path")
endif()
if(APPLE)
add_custom_command(TARGET metaltranslate POST_BUILD
COMMAND ${CMAKE_COMMAND} -E env EXTERNAL_INSTALL_LOCATION=${EXTERNAL_INSTALL_LOCATION} ONEDNN_ROOT=${ONEDNN_ROOT} ICU_LIBRARY_DIR=${ICU_LIBRARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/post_build_mac.sh $<TARGET_FILE_DIR:metaltranslate>
COMMENT "Running post-build script to adjust LC_LOAD_DYLIB paths and copy files"
)
elseif(UNIX)
add_custom_command(TARGET metaltranslate POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${EXTERNAL_INSTALL_LOCATION}/lib/libctranslate2.so $<TARGET_FILE_DIR:metaltranslate>/libctranslate2.so
COMMAND ${CMAKE_COMMAND} -E copy ${EXTERNAL_INSTALL_LOCATION}/lib/libOpenNMTTokenizer.so $<TARGET_FILE_DIR:metaltranslate>
COMMAND ${CMAKE_COMMAND} -E copy ${LIBDNNL_SO} $<TARGET_FILE_DIR:metaltranslate>
COMMAND patchelf --replace-needed libdnnl.so.3 '$$ORIGIN/libdnnl.so' ${CMAKE_CURRENT_BINARY_DIR}/libctranslate2.so
COMMAND patchelf --replace-needed libctranslate2.so.4 'libctranslate2.so' ${CMAKE_CURRENT_BINARY_DIR}/libmetaltranslate.so
COMMENT "Adjusting ctranslate2 rpath and copying shared objects"
)
else()
add_custom_command(TARGET metaltranslate POST_BUILD
# Check permissions of the target directory
COMMAND ${CMAKE_COMMAND} -E echo "Checking permissions of target directory:"
COMMAND ${CMAKE_COMMAND} -E echo "Directory: $<TARGET_FILE_DIR:metaltranslate>"
COMMAND ${CMAKE_COMMAND} -E chdir $<TARGET_FILE_DIR:metaltranslate> ${CMAKE_COMMAND} -E echo *
# Copy runtime DLLs
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:metaltranslate> $<TARGET_RUNTIME_DLLS:metaltranslate>
# List contents of the target directory after copying
COMMAND ${CMAKE_COMMAND} -E echo "Contents of target directory after copy:"
COMMAND ${CMAKE_COMMAND} -E chdir $<TARGET_FILE_DIR:metaltranslate> ${CMAKE_COMMAND} -E echo *
COMMAND_EXPAND_LISTS
)
endif()