-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
313 lines (249 loc) · 8.84 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
# TODO
# - [DONE] get copying over of libtorch .so and .so.1 files
# - have bundle install maybe? idk
# - upload a release with a readme
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(rave VERSION 0.0.1)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT rave)
#######################
# Download pytorch libs
#######################
include(FetchContent)
# Set(FETCHCONTENT_QUIET FALSE)
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
# Set download url for system-specific pytorch libs
if(WIN32)
if(GPU_BUILD)
message("building with GPU libs...")
set(url "https://download.pytorch.org/libtorch/cu124/libtorch-win-shared-with-deps-2.4.0%2Bcu124.zip")
else()
set(url "https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-2.1.2%2Bcpu.zip")
endif()
elseif(APPLE)
if(CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
# set(url "https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.2.2.zip")
set(url "https://chuck.stanford.edu/release/files/extra/lib/libtorch-macos-arm64-2.2.2.zip")
set(urlomp "https://chuck.stanford.edu/release/files/extra/lib/libomp-macos-arm64.zip")
elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
# set(url "https://download.pytorch.org/libtorch/cpu/libtorch-macos-2.0.1.zip")
set(url "https://chuck.stanford.edu/release/files/extra/lib/libtorch-macos-2.0.1.zip")
else()
message(STATUS "Unknown architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
else() # linux
set(url "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip")
endif()
message("URL: ${url}")
# Download pytorch libs
FetchContent_Declare(
pytorch
URL ${url}
)
# Needed to get package locations and such
FetchContent_GetProperties(pytorch)
if(NOT pytorch_POPULATED)
FetchContent_Populate(pytorch)
endif()
# Download libomp (workaround for macOS arm64 build)
if(APPLE AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
FetchContent_Declare(
libomp
URL ${urlomp}
)
# Needed to get package locations and such
FetchContent_MakeAvailable(libomp)
endif()
# Cmake doesn't pick this up automatically for whatever reason...
set(Torch_DIR "${pytorch_SOURCE_DIR}/share/cmake/Torch")
find_package(Torch REQUIRED)
#######################
# Download chuck headers
#######################
#FetchContent_Declare(
# cheaders
# GIT_REPOSITORY https://github.com/nshaheed/cheaders.git
# GIT_TAG test
#)
#FetchContent_MakeAvailable(cheaders)
if(MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MT")
endif()
# the rpath is where the library will search for the *.so pytorch files
# set(CMAKE_INSTALL_RPATH "/usr/local/lib/chuck/Rave")
# this allows the chugin to search for dylibs in ./
if (UNIX AND NOT APPLE)
set(CMAKE_INSTALL_RPATH "$ORIGIN")
endif()
# Enable BUILD_RPATH_USE_ORIGIN
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
# subdirectories from original nn_tilde code
add_subdirectory(nn_tilde/src/backend) # DEEP LEARNING BACKEND
# message(cmake_source_dir="${CMAKE_CURRENT_SOURCE_DIR}")
#############
# Add sources
#############
# list of chuck files
FILE(GLOB CK_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cheaders/include/*.h
)
FILE(GLOB NN_TILDE_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/nn_tilde/src/backend/*.h
${CMAKE_CURRENT_SOURCE_DIR}/nn_tilde/src/frontend/maxmsp/shared/*.h)
# find all libtorch dlls that eventually need to be copied over
if (MSVC)
FILE(GLOB Torch_DLLs
"${pytorch_SOURCE_DIR}/lib/*.dll")
elseif(UNIX AND NOT APPLE) # linux??
Message("in linux")
FILE(GLOB Torch_so
"${pytorch_SOURCE_DIR}/lib/*.so"
"${pytorch_SOURCE_DIR}/lib/*.so.1"
)
elseif(APPLE)
FILE(GLOB Torch_so
"${pytorch_SOURCE_DIR}/lib/*.dylib"
"${libomp_SOURCE_DIR}/*.dylib"
)
endif()
FILE(GLOB readme
"${CMAKE_CURRENT_SOURCE_DIR}/README.md"
)
message(STATUS "torch_so")
message(Torch_so="${Torch_so}")
message(CMAKE_BINARY_DIR="${CMAKE_BINARY_DIR}")
message(NN_TILDE_SOURCES="${NN_TILDE_SOURCES}")
message(Torch_DLLS="${Torch_DLLs}")
message(Torch_DIR="${Torch_DIR}")
set(Sources
"rave.cpp"
)
source_group("Sources" FILES ${Sources})
add_library(rave SHARED ${CK_SOURCES} ${NN_TILDE_SOURCES} ${Sources})
set_target_properties(rave PROPERTIES CXX_STANDARD 17)
# output as .chug file
set_target_properties(rave PROPERTIES OUTPUT_NAME "Rave.chug")
set_target_properties(rave PROPERTIES PREFIX "")
set_target_properties(rave PROPERTIES SUFFIX "")
# this allows the chugin to search for dylibs in ./
if (APPLE)
set_target_properties(rave PROPERTIES
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH "@loader_path"
)
endif()
# set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
# target_compile_definitions(rave PUBLIC)
target_compile_definitions(rave
PUBLIC
# USE_BUILTIN_FFT
__PLATFORM_LINUX__
USE_PTHREADS
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fPIC")
if(MSVC)
target_compile_definitions(rave
PUBLIC
USE_BUILTIN_FFT
NOMINMAX
_WINDOWS
WIN32
__WINDOWS_MODERN__
__PLATFORM_WIN32__
__WINDOWS_DS__
)
endif()
target_link_libraries(rave PRIVATE backend "${TORCH_LIBRARIES}")
# Include header directories
target_include_directories(rave PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/cheaders/include>
# $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../chuck/include/chuck_dl.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/nn_tilde/src/backend>
$<INSTALL_INTERFACE:rave
)
# Install library
# INSTALL(
# TARGETS rave
# LIBRARY DESTINATION "$ENV{HOME}/.chuck/lib/Rave"
# LIBRARY DESTINATION "/usr/local/lib/chuck/"
# LIBRARY DESTINATION ${CMAKE_INSTALL_RPATH}
# ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
# RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
# )
INSTALL(
TARGETS rave
LIBRARY DESTINATION "${CMAKE_BINARY_DIR}/Release/"
)
message("library install dir: ${CMAKE_INSTALL_LIBDIR}")
file(GENERATE
OUTPUT "$<TARGET_FILE_DIR:rave>/archive.zip"
CONTENT "$<TARGET_FILE:rave>"
)
get_target_property(EXECUTABLE_LOCATION rave RUNTIME_OUTPUT_DIRECTORY)
message("Executable Location: ${EXECUTABLE_LOCATION}/rave")
#INSTALL(
# FILES ${Torch_so}
# DESTINATION "$ENV{HOME}/.chuck/lib/Rave")
Message("torch so ${Torch_so}")
INSTALL(
FILES ${Torch_so}
DESTINATION "${CMAKE_BINARY_DIR}/Release/")
INSTALL(
FILES ${readme}
DESTINATION "${CMAKE_BINARY_DIR}/Release/")
# rename to .chug file
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE:rave>"
"$<TARGET_FILE_DIR:rave>/rave.chug")
if (MSVC)
set_target_properties(${PROJECT_NAME} PROPERTIES
VS_DEBUGGER_COMMAND "C:\\Users\\nick\\Documents\\chuck\\src\\visual-studio\\Debug\\chuck.exe"
VS_DEBUGGER_COMMAND_ARGUMENTS "--chugin:build/Debug/rave.chug ../rave_test.ck")
message(STATUS "in MSVC!")
# copy chugin over to the relevant chuck dir
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE:rave>"
"%USERPROFILE%/Documents/ChucK/chugins/rave.chug")
# rename to .chug file
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE:rave>"
"$<TARGET_FILE_DIR:rave>/rave.chug")
# copy libtoch dlls to build directory (for unit tests)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${Torch_DLLs}
$<TARGET_FILE_DIR:rave>)
endif()
############################
### ~~~~~~ Unit Tests ~~~~~~
############################
#[[
set(UNIT_TEST_TARGET_NAME rave_test)
add_library(${UNIT_TEST_TARGET_NAME} SHARED)
target_sources(${UNIT_TEST_TARGET_NAME} PRIVATE
rave_test.cpp
)
target_link_libraries(${UNIT_TEST_TARGET_NAME}
PRIVATE
MSUnitTestFramework::MSUnitTestFramework
rave
backend
)
enable_testing()
add_library(MSUnitTestFramework::MSUnitTestFramework SHARED IMPORTED)
set_target_properties(MSUnitTestFramework::MSUnitTestFramework PROPERTIES
# INTERFACE_INCLUDE_DIRECTORIES "$ENV{VCInstallDir}Auxiliary/VS/UnitTest/include"
# IMPORTED_IMPLIB "$ENV{VCInstallDir}Auxiliary/VS/UnitTest/lib/x86/Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib"
IMPORTED_IMPLIB "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/UnitTest/lib/x64/Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib"
INTERFACE_INCLUDE_DIRECTORIES "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/UnitTest/include"
)
set(MSUnitTestFramework_FOUND TRUE)
]]