-
Notifications
You must be signed in to change notification settings - Fork 30
/
CMakeLists.txt
336 lines (299 loc) · 14.9 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
cmake_minimum_required(VERSION 3.5)
CMAKE_POLICY(SET CMP0074 NEW) # -DLibName_ROOT
CMAKE_POLICY(SET CMP0020 NEW) # This should help linking on windows in some edge cases. It is also a new default behaviour and we will get warnings unless we set it with cmake 3.22+
if(APPLE) # This needs to happen before the project definition due to a cmake bug
# https://gitlab.kitware.com/cmake/cmake/-/issues/21608
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version")
endif(APPLE)
project(translateLocally LANGUAGES CXX)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(CMakeDependentOption)
find_package(Git QUIET)
include(GetVersionFromFile)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
##### translateLocally options begin #####
set(BUILD_EXTERNAL_LIBARCHIVE OFF CACHE BOOL "Build libarchive as external project.")
set(APPLE_FORCE_STATIC_LIBARCHIVE ON CACHE BOOL "Link to static libarchive on Mac.")
##### translateLocally options end #####
# Determine build arch
set(BUILD_ARCH native CACHE STRING "Compile for this CPU architecture.")
# Unfortunately MSVC supports a limited subset of BUILD_ARCH flags. Instead try to guess
# what architecture we can compile to reading BUILD_ARCH and mapping it to MSVC values
# references: https://clang.llvm.org/docs/UsersManual.html https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/i386-and-x86-64-Options.html
# https://docs.microsoft.com/en-us/cpp/build/reference/arch-x86?redirectedfrom=MSDN&view=vs-2019&view=msvc-170 https://devblogs.microsoft.com/oldnewthing/20201026-00/?p=104397
# This is by no means an exhaustive list but should match the most common flags Linux programmers expect to parse to MSVC
if(BUILD_ARCH STREQUAL "native") # Windows doesn't support native. hence, set to avx2. Very few systems have avx512
set(MSVC_BUILD_ARCH "/arch:AVX2")
elseif(BUILD_ARCH STREQUAL "skylake-avx512" OR BUILD_ARCH STREQUAL "cannonlake" OR BUILD_ARCH STREQUAL "x86-64-v4" OR BUILD_ARCH STREQUAL "tigerlake" OR BUILD_ARCH STREQUAL "cooperlake" OR BUILD_ARCH STREQUAL "cascadelake")
set(MSVC_BUILD_ARCH "/arch:AVX512")
set(CPU_FEATURE "AVX512")
elseif(BUILD_ARCH STREQUAL "core-avx2" OR BUILD_ARCH STREQUAL "haswell" OR BUILD_ARCH STREQUAL "x86-64-v3" OR BUILD_ARCH STREQUAL "broadwell" OR BUILD_ARCH STREQUAL "skylake")
set(MSVC_BUILD_ARCH "/arch:AVX2")
set(CPU_FEATURE "AVX2")
elseif(BUILD_ARCH STREQUAL "sandybridge" OR BUILD_ARCH STREQUAL "corei7-avx" OR BUILD_ARCH STREQUAL "core-avx-i" OR BUILD_ARCH STREQUAL "ivybridge")
set(MSVC_BUILD_ARCH "/arch:AVX")
set(CPU_FEATURE "AVX")
elseif(BUILD_ARCH STREQUAL "nehalem" OR BUILD_ARCH STREQUAL "westmere" OR BUILD_ARCH STREQUAL "x86-64-v2" OR BUILD_ARCH STREQUAL "corei7" OR BUILD_ARCH STREQUAL "core2")
set(MSVC_BUILD_ARCH "/arch:SSE2") # This is MSVC default. We won't go down to SSE because we don't support that hardware at all with intgemm. Marian recommends to only go down to SSE4.1 at most
set(CPU_FEATURE "SSE42")
else()
message(WARNING "Unknown BUILD_ARCH ${BUILD_ARCH} provided. Default to SSE2 for Windows build")
set(MSVC_BUILD_ARCH "/arch:SSE2")
set(CPU_FEATURE ${BUILD_ARCH})
endif()
if(MSVC)
add_compile_options(${MSVC_BUILD_ARCH})
else(MSVC)
add_compile_options(-march=${BUILD_ARCH})
endif(MSVC)
#MSVC can't seem to pick up correct flags otherwise:
if(MSVC)
add_definitions(-DUSE_SSE2=1) # Supposed to fix something in the sse_mathfun.h but not sure it does
set(INTRINSICS ${MSVC_BUILD_ARCH}) # ARCH we're targetting on win32. This is probably duplicated, but I'm afraid to change anything atm since my windows environment is broken...
set(CMAKE_CXX_FLAGS "/EHsc /DWIN32 /D_WINDOWS /DUNICODE /D_UNICODE /D_CRT_NONSTDC_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS /bigobj")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /MT /O2 ${INTRINSICS} /Zi /MP /GL /DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} /MTd /Od /Ob0 ${INTRINSICS} /RTC1 /Zi /D_DEBUG")
# ignores warning LNK4049: locally defined symbol free imported - this comes from zlib
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /LTCG:incremental /INCREMENTAL:NO /ignore:4049")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:MSVCRT")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:MSVCRTD")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /LTCG:incremental")
endif(MSVC)
message("Building ${PROJECT_NAME}-${PROJECT_VERSION_STRING_FULL}")
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package( ...) calls below.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
include(findLibarchiveCustom)
find_package(Threads REQUIRED) # Cross-platform compatible way to get threads?
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Gui PrintSupport Widgets LinguistTools Network DBus Svg REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui PrintSupport Widgets LinguistTools Network DBus Svg REQUIRED)
# Marian submodule things
# Documentation: https://cliutils.gitlab.io/modern-cmake/chapters/projects/submodule.html
# Ensures the submodules are set correctly during a build.
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE)
message(STATUS "Submodule update")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
endif()
SET(COMPILE_CUDA OFF CACHE BOOL "Compile GPU version")
SET(USE_SENTENCEPIECE ON CACHE BOOL "Download and compile SentencePiece")
SET(USE_STATIC_LIBS ON CACHE BOOL "Link statically against non-system libs")
SET(USE_WASM_COMPATIBLE_SOURCE OFF CACHE BOOL "Don't build wasm compatible sources")
add_subdirectory(3rd_party)
# end of marian options
#set(TS_FILES translateLocally_es_ES.ts) //Causes compilation to go on forever...
# Set up source
include_directories(src)
include_directories(logo)
add_subdirectory(src)
# This doesn't quite work if places in the subdirectory's CMakeLists.txt
set(PROJECT_SOURCES
src/main.cpp
src/mainwindow.cpp
src/mainwindow.h
src/mainwindow.ui
src/AlignmentHighlighter.cpp
src/AlignmentHighlighter.h
src/AlignmentWorker.cpp
src/AlignmentWorker.h
src/ColorWell.cpp
src/ColorWell.h
src/FilterTableView.cpp
src/FilterTableView.h
src/MarianInterface.cpp
src/MarianInterface.h
src/Network.cpp
src/Network.h
src/Translation.h
src/Translation.cpp
src/types.h
src/cli/CLIParsing.h
src/cli/CommandLineIface.cpp
src/cli/CommandLineIface.h
src/cli/NativeMsgIface.cpp
src/cli/NativeMsgIface.h
src/cli/NativeMsgManager.cpp
src/cli/NativeMsgManager.h
src/inventory/ModelManager.cpp
src/inventory/ModelManager.h
src/settings/NewRepoDialog.cpp
src/settings/NewRepoDialog.h
src/settings/NewRepoDialog.ui
src/settings/RepositoryTableModel.cpp
src/settings/RepositoryTableModel.h
src/settings/Settings.cpp
src/settings/Settings.h
src/settings/TranslatorSettingsDialog.cpp
src/settings/TranslatorSettingsDialog.h
src/settings/TranslatorSettingsDialog.ui
logo/logo_svg.h
${TS_FILES}
)
# Make PROJECT_VERSION_* available to our code as TRANSLATELOCALLY_VERSION_*
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in
${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY)
# Add CURRENT_BINARY_DIR as include path to find the version.h file
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# Set icon file for windows
if(WIN32)
set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/logo/translateLocally.rc")
elseif(APPLE)
set(MACOSX_BUNDLE_ICON_FILE "translateLocally.icns")
set(APP_ICON_MACOSX ${CMAKE_CURRENT_SOURCE_DIR}/logo/translateLocally.icns)
set(APP_ENTITLEMENTS_MACOSX ${CMAKE_CURRENT_SOURCE_DIR}/dist/translateLocally.entitlements)
set_source_files_properties(${APP_ICON_MACOSX} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
set_source_files_properties(${APP_ENTITLEMENTS_MACOSX} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
endif(WIN32)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
if(APPLE)
qt_add_executable(translateLocally MACOSX_BUNDLE
${PROJECT_SOURCES}
${APP_ICON_MACOSX}
${APP_ENTITLEMENTS_MACOSX}
)
endif()
qt_add_executable(translateLocally-bin
${PROJECT_SOURCES}
${APP_ICON_RESOURCE_WINDOWS}
)
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()
if(ANDROID)
add_library(translateLocally-bin SHARED
${PROJECT_SOURCES}
)
else()
if(APPLE)
add_executable(translateLocally MACOSX_BUNDLE
${PROJECT_SOURCES}
${APP_ICON_MACOSX}
${APP_ENTITLEMENTS_MACOSX}
)
endif()
add_executable(translateLocally-bin
${PROJECT_SOURCES}
${APP_ICON_RESOURCE_WINDOWS}
)
endif()
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()
if(WIN32) # Do not launch console on win32
set_property(TARGET translateLocally-bin PROPERTY WIN32_EXECUTABLE TRUE)
endif()
set(LINK_LIBRARIES
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::PrintSupport
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Network
Qt${QT_VERSION_MAJOR}::DBus
Qt${QT_VERSION_MAJOR}::Svg
bergamot-translator
${CMAKE_THREAD_LIBS_INIT} # This should work on all platforms?
${LibArchive_LIBRARIES}
${CMAKE_DL_LIBS}) # On unix necessary sometimes
# Reserve the translateLocally name for the MacOS executables. Rename the Linux and Windows executable to translateLocally after compilation
target_link_libraries(translateLocally-bin PRIVATE ${LINK_LIBRARIES})
set_target_properties(translateLocally-bin PROPERTIES OUTPUT_NAME translateLocally)
if(UNIX) # Add Linux and apple support for make install
include(GNUInstallDirs)
install(TARGETS translateLocally-bin
RUNTIME
)
install(FILES ${CMAKE_SOURCE_DIR}/dist/translateLocally.desktop DESTINATION share/applications)
install(FILES ${CMAKE_SOURCE_DIR}/logo/translateLocally_logo.png DESTINATION share/icons)
install(FILES ${CMAKE_SOURCE_DIR}/logo/translateLocally_logo.svg DESTINATION share/icons)
if(NOT APPLE)
## Support CPACK package generation on debian like systems
# this is triggered by running cpack after make to generate a .deb
# This won't work on apple and would need to be modified for rpm based distros
# Find ubuntu version
execute_process (
COMMAND bash -c "grep ^NAME /etc/os-release | tr -d \"\\\"\" | tr -d \"NAME=\""
OUTPUT_VARIABLE OS_NAME
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process (
COMMAND bash -c "grep VERSION_ID= /etc/os-release | tr -d \"\\\"\" | tr -d \"VERSION_ID=\""
OUTPUT_VARIABLE OS_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT DEFINED CPU_FEATURE) # Only set this in case we don't set BUILD_ARCH explicitly
execute_process (
COMMAND ${CMAKE_SOURCE_DIR}/dist/cpu_feature.sh
OUTPUT_VARIABLE CPU_FEATURE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif(NOT DEFINED CPU_FEATURE)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_PACKAGE_CONTACT "Nikolay Bogoychev <nheart@gmail.com>")
set(CPACK_PACKAGE_DIRECTORY ${CMAKE_BINARY_DIR}/deb)
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
set(CPACK_PACKAGE_FILE_NAME ${PROJECT_NAME}-${PROJECT_VERSION_STRING_FULL}-${OS_NAME}-${OS_VERSION}.${CPU_FEATURE})
set(CPACK_DEBIAN_PACKAGE_NAME ${PROJECT_NAME})
set(CPACK_DEBIAN_PACKAGE_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "A fast privacy focused machine translation client that translates on your own machine.")
include(CPack)
#
#
##
endif(NOT APPLE)
endif(UNIX)
if(APPLE) # Apple specific installation of the app
# Add the .app Target
target_link_libraries(translateLocally PRIVATE ${LINK_LIBRARIES})
set_target_properties(translateLocally PROPERTIES
BUNDLE True
MACOSX_BUNDLE_BUNDLE_NAME ${CMAKE_PROJECT_NAME}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION_STRING_FULL}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
MACOSX_BUNDLE_GUI_IDENTIFIER "com.translatelocally.translateLocally"
)
# When on MacOS, we have a non-standard libarchive location because we get it via brew
target_include_directories(translateLocally-bin PRIVATE ${LibArchive_INCLUDE_DIR})
target_include_directories(translateLocally PRIVATE ${LibArchive_INCLUDE_DIR})
# produce .dmg This requires custom target due to https://gitlab.kitware.com/cmake/cmake/-/issues/16270
# use dist/macdmg.sh to sign and notarize
include(macdeployqt)
macdeployqt(translateLocally)
endif(APPLE)
# Finally when using ARM we need to fix the build due to issues with RUY and QT's MOC
# First detect if we are on ARM
include(TargetArch)
target_architecture(CMAKE_TARGET_ARCHITECTURES)
list(LENGTH CMAKE_TARGET_ARCHITECTURES cmake_target_arch_len)
if(NOT "${cmake_target_arch_len}" STREQUAL "1")
set(CMAKE_TARGET_ARCHITECTURE_UNIVERSAL TRUE)
set(CMAKE_TARGET_ARCHITECTURE_CODE "universal")
else()
set(CMAKE_TARGET_ARCHITECTURE_UNIVERSAL FALSE)
set(CMAKE_TARGET_ARCHITECTURE_CODE "${CMAKE_TARGET_ARCHITECTURES}")
endif()
# Now echo the workaround
if(${CMAKE_TARGET_ARCHITECTURE_CODE} MATCHES "arm")
message(WARNING "Building on ARM. You need to manually fix the broken build due to RUY using bad header names and QT's MOC. Please run:")
execute_process(COMMAND echo -e "${CMAKE_SOURCE_DIR}/cmake/fix_ruy_build.sh ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}")
message(WARNING "Before building the project")
endif()