forked from numenta/nupic.core-legacy
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
472 lines (422 loc) · 15.1 KB
/
Copy pathCMakeLists.txt
File metadata and controls
472 lines (422 loc) · 15.1 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# -----------------------------------------------------------------------------
# HTM Community Edition of NuPIC
# Copyright (C) 2013-2015, Numenta, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program. If not, see http://www.gnu.org/licenses.
# -----------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.7)
project(htm_core CXX)
message(STATUS "Configuring htm_core src")
message(STATUS "Using MUSL LIBC= ${NTA_LIBC_MUSL}")
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_VERBOSE_MAKEFILE ON) # toggle for cmake debug
include(src/NupicLibraryUtils) # for combining static libs
include(src/ExpandStaticLib) # for creating objects from a static lib
#
# NuPIC CMake options
#
option(HTM_IWYU "Enable include-what-you-use
(http://include-what-you-use.org/). This requires the iwyu binary to be
discoverable by CMake's find_program, with a minimum CMake version of 3.3.")
if(${HTM_IWYU})
find_program(iwyu_path NAMES include-what-you-use iwyu)
if(NOT iwyu_path)
message(FATAL_ERROR "Could not find the program include-what-you-use")
endif()
endif()
#--------------------------------------------------------
# Identify includes from this directory
set(CORE_LIB_INCLUDES ${PROJECT_SOURCE_DIR} # for htm/xxx/*.h
${PROJECT_BINARY_DIR} # for Version.hpp
)
set(CORE_LIB_INCLUDES ${CORE_LIB_INCLUDES} PARENT_SCOPE)
#--------------------------------------------------------
# Create Version.hpp
# Extract current git sha and record in htm/Version.hpp.in
#
set(HTM_CORE_VERSION ${VERSION})
configure_file(
"${PROJECT_SOURCE_DIR}/htm/Version.hpp.in"
"${PROJECT_BINARY_DIR}/Version.hpp")
############ htm_core Library ######################################
# Setup htm_core static library, consisting of our own sources and yaml libraries;
#
# this is an intermediate archive that will be merged with external static
# libraries in a subsequent step to produce the htm_core "combined" static
# library.
#
set(src_objlib LibrarySource)
set(algorithm_files
htm/algorithms/Anomaly.cpp
htm/algorithms/Anomaly.hpp
htm/algorithms/AnomalyLikelihood.cpp
htm/algorithms/AnomalyLikelihood.hpp
htm/algorithms/Connections.cpp
htm/algorithms/Connections.hpp
htm/algorithms/SDRClassifier.cpp
htm/algorithms/SDRClassifier.hpp
htm/algorithms/SpatialPooler.cpp
htm/algorithms/SpatialPooler.hpp
htm/algorithms/TemporalMemory.cpp
htm/algorithms/TemporalMemory.hpp
)
set(encoders_files
htm/encoders/BaseEncoder.hpp
htm/encoders/ScalarEncoder.cpp
htm/encoders/ScalarEncoder.hpp
htm/encoders/RandomDistributedScalarEncoder.hpp
htm/encoders/RandomDistributedScalarEncoder.cpp
htm/encoders/SimHashDocumentEncoder.hpp
htm/encoders/SimHashDocumentEncoder.cpp
)
set(engine_files
htm/engine/Input.cpp
htm/engine/Input.hpp
htm/engine/Link.cpp
htm/engine/Link.hpp
htm/engine/Network.cpp
htm/engine/Network.hpp
htm/engine/Output.cpp
htm/engine/Output.hpp
htm/engine/Region.cpp
htm/engine/Region.hpp
htm/engine/RegionImpl.cpp
htm/engine/RegionImpl.hpp
htm/engine/RegionImplFactory.cpp
htm/engine/RegionImplFactory.hpp
htm/engine/RegisteredRegionImpl.hpp
htm/engine/RegisteredRegionImplCpp.hpp
htm/engine/Spec.cpp
htm/engine/Spec.hpp
htm/engine/Watcher.cpp
htm/engine/Watcher.hpp
)
set(ntypes_files
htm/ntypes/Array.hpp
htm/ntypes/ArrayBase.cpp
htm/ntypes/ArrayBase.hpp
htm/ntypes/BasicType.cpp
htm/ntypes/BasicType.hpp
htm/ntypes/Collection.hpp
htm/ntypes/Dimensions.hpp
htm/ntypes/Value.cpp
htm/ntypes/Value.hpp
)
set(os_files
htm/os/Directory.cpp
htm/os/Directory.hpp
htm/os/Env.cpp
htm/os/Env.hpp
htm/os/ImportFilesystem.hpp
htm/os/Path.cpp
htm/os/Path.hpp
htm/os/Timer.cpp
htm/os/Timer.hpp
)
set(regions_files
htm/regions/ScalarSensor.cpp
htm/regions/ScalarSensor.hpp
htm/regions/SPRegion.cpp
htm/regions/SPRegion.hpp
htm/regions/TestNode.cpp
htm/regions/TestNode.hpp
htm/regions/TMRegion.cpp
htm/regions/TMRegion.hpp
htm/regions/VectorFile.cpp
htm/regions/VectorFile.hpp
htm/regions/VectorFileEffector.cpp
htm/regions/VectorFileEffector.hpp
htm/regions/VectorFileSensor.cpp
htm/regions/VectorFileSensor.hpp
)
set(types_files
htm/types/Exception.hpp
htm/types/Types.hpp
htm/types/Serializable.hpp
htm/types/Sdr.hpp
htm/types/Sdr.cpp
)
set(utils_files
htm/utils/GroupBy.hpp
htm/utils/Log.hpp
htm/utils/MovingAverage.cpp
htm/utils/MovingAverage.hpp
htm/utils/Random.cpp
htm/utils/Random.hpp
htm/utils/SlidingWindow.hpp
htm/utils/VectorHelpers.hpp
htm/utils/SdrMetrics.cpp
htm/utils/SdrMetrics.hpp
htm/utils/Topology.cpp
htm/utils/Topology.hpp
)
set(examples_files
examples/hotgym/Hotgym.cpp # contains conflicting main()
examples/hotgym/HelloSPTP.cpp
examples/hotgym/HelloSPTP.hpp
examples/mnist/MNIST_SP.cpp
)
#set up file tabs in Visual Studio
source_group("algorithms" FILES ${algorithm_files})
source_group("encoders" FILES ${encoders_files})
source_group("engine" FILES ${engine_files})
source_group("ntypes" FILES ${ntypes_files})
source_group("os" FILES ${os_files})
source_group("regions" FILES ${regions_files})
source_group("types" FILES ${types_files})
source_group("utils" FILES ${utils_files})
source_group("examples" FILES ${examples_files})
#--------------------------------------------------------
# Create an OBJECT library (compiles sources only once)
# from the CMake docs:
# An object library compiles source files but does not archive or link their object
# files into a library. Instead other targets created by add_library() or add_executable()
# may reference the objects using an expression of the form $<TARGET_OBJECTS:objlib> as a
# source, where objlib is the object library name.
#
# The price you pay is that the object files must be built as position-independent code.
#
add_library(${src_objlib} OBJECT
${algorithm_files}
${encoders_files}
${engine_files}
${ntypes_files}
${os_files}
${regions_files}
${types_files}
${utils_files}
)
# shared libraries need PIC
target_compile_options( ${src_objlib} PUBLIC ${INTERNAL_CXX_FLAGS})
if(MSVC)
set_property(TARGET ${src_objlib} PROPERTY LINK_LIBRARIES ${INTERNAL_LINKER_FLAGS})
endif()
target_compile_definitions(${src_objlib} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
target_compile_definitions(${src_objlib} PRIVATE ${yaml_DEFINE})
target_include_directories(${src_objlib} PRIVATE
${CORE_LIB_INCLUDES}
SYSTEM ${EXTERNAL_INCLUDES}
)
if(${HTM_IWYU})
set_target_properties(${src_objlib}
PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path})
endif()
############ Building Static LIB #############################################
# build static libhtm_core_solo.a without yaml and boost.
# uses objects compiled for src_objlib
set(src_lib_static htm_core_solo)
add_library(${src_lib_static} STATIC $<TARGET_OBJECTS:${src_objlib}>)
if(MSVC)
set_property(TARGET ${src_lib_static} PROPERTY LINK_LIBRARIES ${INTERNAL_LINKER_FLAGS})
elseif(${NTA_LIBC_MUSL})
message("Doing static build with MUSL libc")
target_link_libraries(${src_lib_static} -static)
endif()
#
# Create the libhtm_core.a as a "combined" static library by merging htm_core_static
# with our external static libraries
# NOTE: cannot use target_link_libraries( ) with the add_library for STATIC.
# so we have to merge them in a different way.
#
set(src_combined_htmcore_source_archives
${src_lib_static}
${yaml_LIBRARIES}
${Boost_LIBRARIES}
${common_LIBRARIES}
)
merge_static_libraries(${core_library} "${src_combined_htmcore_source_archives}")
############ Building Shared LIB #############################################
# builds libhtm_core.so for Linux,OSx and
# NOTE: This does not work for Windows.
# If there is community demand for a Windows DLL interface, we can add it later.
#
# For Linux, OSx; the .so file will contain all symbols.
#
if(MSVC)
# NOTE: this disables shared lib for Windows.
else()
# First extract static libraries into an Object Library
# Note that these libraries must already exist (from depenancies)
set(all_object_locations)
set(src_externLibs
${yaml_LIBRARIES}
${Boost_LIBRARIES}
${common_LIBRARIES}
)
set(lib_locations_separator "++++")
string(REPLACE ";" ${lib_locations_separator}
escaped_lib_locations_arg "${src_externLibs}")
ExpandStaticLib( ${src_lib_shared}
"${escaped_lib_locations_arg}"
"${lib_locations_separator}"
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_AR}"
)
# build shared lib with yaml and boost resolved
# uses objects compiled for src_objlib
add_library(${src_lib_shared} SHARED
$<TARGET_OBJECTS:${src_objlib}>
${all_object_locations})
set_target_properties(${src_lib_shared} PROPERTIES OUTPUT_NAME ${core_library})
target_link_libraries(${src_lib_shared}
${COMMON_OS_LIBS}
)
endif()
############ TEST #############################################
# Test
# The tests were placed in a separate file to avoid clutering up this file.
add_subdirectory(test)
########### EXAMPLES ##############################################
## Example custom executable
#
## Setup benchmark_hotgym
#
set(src_executable_hotgym benchmark_hotgym)
add_executable(${src_executable_hotgym} examples/hotgym/Hotgym.cpp examples/hotgym/HelloSPTP.hpp examples/hotgym/HelloSPTP.cpp)
# link with the static library
target_link_libraries(${src_executable_hotgym}
${INTERNAL_LINKER_FLAGS}
${core_library}
${COMMON_OS_LIBS}
)
# for future reference; to link to shared library, (Not on MSVC)
# link with ${src_lib_shared} rather than ${core_library}
target_compile_options( ${src_executable_hotgym} PUBLIC ${INTERNAL_CXX_FLAGS})
if(${NTA_LIBC_MUSL})
target_compile_definitions(${src_executable_hotgym} PRIVATE ${COMMON_COMPILER_DEFINITIONS} -DNTA_LIBC_MUSL=${NTA_LIBC_MUSL})
else()
target_compile_definitions(${src_executable_hotgym} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
endif()
target_include_directories(${src_executable_hotgym} PRIVATE
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
)
add_custom_target(hotgym
COMMAND ${src_executable_hotgym}
DEPENDS ${src_executable_hotgym}
COMMENT "Executing ${src_executable_hotgym}"
VERBATIM)
#########################################################
## Dynamicly linked version of hotgym
if(MSVC)
else()
set(src_dyn_executable_hotgym dynamic_hotgym)
add_executable(${src_dyn_executable_hotgym} examples/hotgym/Hotgym.cpp examples/hotgym/HelloSPTP.hpp examples/hotgym/HelloSPTP.cpp)
# for Linux,OSx, link with the shared library
target_link_libraries(${src_dyn_executable_hotgym}
${INTERNAL_LINKER_FLAGS}
${src_lib_shared}
${COMMON_OS_LIBS}
)
target_compile_options( ${src_dyn_executable_hotgym} PUBLIC ${INTERNAL_CXX_FLAGS})
if(${NTA_LIBC_MUSL})
target_compile_definitions(${src_dyn_executable_hotgym} PRIVATE ${COMMON_COMPILER_DEFINITIONS} -DNTA_LIBC_MUSL=${NTA_LIBC_MUSL})
else()
target_compile_definitions(${src_dyn_executable_hotgym} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
endif()
target_include_directories(${src_dyn_executable_hotgym} PRIVATE
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
)
endif()
#########################################################
## MNIST Spatial Pooler Example
#
set(src_executable_mnistsp mnist_sp)
add_executable(${src_executable_mnistsp} examples/mnist/MNIST_SP.cpp)
target_link_libraries(${src_executable_mnistsp}
${INTERNAL_LINKER_FLAGS}
${core_library}
${COMMON_OS_LIBS}
)
target_compile_options(${src_executable_mnistsp} PUBLIC ${INTERNAL_CXX_FLAGS})
target_compile_definitions(${src_executable_mnistsp} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
# Pass MNIST data directory to main.cpp
target_compile_definitions(${src_executable_mnistsp} PRIVATE MNIST_DATA_LOCATION=${mnist_SOURCE_DIR})
target_include_directories(${src_executable_mnistsp} PRIVATE
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
)
add_custom_target(mnist
COMMAND ${src_executable_mnistsp}
DEPENDS ${src_executable_mnistsp}
COMMENT "Executing ${src_executable_mnistsp}"
VERBATIM)
############ INSTALL ######################################
#
# Install targets into CMAKE_INSTALL_PREFIX
#
install(TARGETS
${core_library}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
install(TARGETS
${src_executable_hotgym}
${src_executable_mnistsp}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
if(MSVC)
else()
install(TARGETS
${src_dyn_executable_hotgym}
${src_lib_shared}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
endif()
# Version.hpp is also used by the htm.bindings release/deployment system
install(FILES ${PROJECT_BINARY_DIR}/Version.hpp
DESTINATION include)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/htm
MESSAGE_NEVER
DESTINATION include/
FILES_MATCHING PATTERN "*.h*"
PATTERN "*.hpp.in" EXCLUDE)
install(DIRECTORY "${REPOSITORY_DIR}/external/common/include/"
MESSAGE_NEVER
DESTINATION include)
# include the python wheel if it exists.
# run "python3 setup.py install --user"
# and "python3 setup.py bdist_wheel"
# prior to this to create the whl file.
install(DIRECTORY "${CMAKE_INSTALL_PREFIX}/distr/dist/"
MESSAGE_NEVER
DESTINATION py
FILES_MATCHING PATTERN "*.whl")
# copy requirements.txt into dist/
install(FILES ${REPOSITORY_DIR}/requirements.txt
DESTINATION distr/dist)
############### PACKAGE ###########################
#
# `make package` results in
# htm_cpp-${VERSION}-${PLATFORM}${BITNESS}${PLATFORM_SUFFIX}.tar.gz binary release
#
# TODO: The version numbers need to be automatically incremented.
message(STATUS "Packaging version: ${VERSION} in CPACK")
set(CPACK_PACKAGE_NAME "htm.core")
set(CPACK_PACKAGE_VENDOR "https://github.com/htm-community/htm.core")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "htm.core - Community implementation of Numenta's algorthms.")
set(CPACK_PACKAGE_VERSION ${VERSION})
set(CPACK_PACKAGE_VERSION_MAJOR ${MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PATCH})
set(CPACK_PACKAGE_INSTALL_DIRECTORY "htm.core")
if(MSVC)
set(CPACK_GENERATOR "ZIP")
else()
set(CPACK_GENERATOR "TGZ")
endif()
set(CPACK_RESOURCE_FILE_LICENSE "${REPOSITORY_DIR}/LICENSE.txt")
set(CPACK_PACKAGE_FILE_NAME "htm_core-${VERSION}-${PLATFORM}${BITNESS}${PLATFORM_SUFFIX}")
include(CPack)