-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
1221 lines (1063 loc) · 53.5 KB
/
Copy pathCMakeLists.txt
File metadata and controls
1221 lines (1063 loc) · 53.5 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ThemisDB Root CMakeLists.txt - New Modular Architecture
# Clean, minimal orchestration of the build system
cmake_minimum_required(VERSION 3.20)
# Optional dependency bootstrap during configure (OFF by default for reproducible builds)
set(THEMIS_AUTO_BOOTSTRAP_DEPS OFF CACHE BOOL "Automatically bootstrap missing third-party dependencies (vcpkg, submodules) during CMake configure")
# CRITICAL: Set CMAKE_PROJECT_INCLUDE before any project() call
# This ensures MSVC includes are applied to ALL subprojects (including llama.cpp)
# Must be set unconditionally (before MSVC is defined by project())
set(CMAKE_PROJECT_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/msvc_includes_fix.cmake")
# Initialize selected Visual Studio developer environment (optional)
# This runs VsDevCmd.bat and applies LIB/INCLUDE/LIBPATH/WindowsSdkDir/VCToolsInstallDir
# into the CMake process so compiler detection and configure steps use the selected
# MSVC toolset without external wrapper scripts. Control via -DTHEMIS_MSVC_SELECTION=vs2022
include(cmake/DetectVsDevCmd.cmake OPTIONAL)
# Windows/MSVC fresh-configure guard:
# some local shells lack complete SDK linker paths during CMake try-compile
# even though the actual project build can succeed with the repository's
# configured environment. Pre-validating the compiler avoids those fragile
# early link probes in fresh configure runs.
if(WIN32)
set(CMAKE_C_COMPILER_WORKS ON CACHE BOOL "C compiler pre-validated" FORCE)
set(CMAKE_CXX_COMPILER_WORKS ON CACHE BOOL "CXX compiler pre-validated" FORCE)
set(THREADS_PREFER_PTHREAD_FLAG OFF CACHE BOOL "Prefer Win32 threads on Windows" FORCE)
set(CMAKE_USE_PTHREADS_INIT OFF CACHE BOOL "Disable pthread detection on Windows" FORCE)
set(CMAKE_HAVE_PTHREAD_H OFF CACHE BOOL "Disable pthread header detection on Windows" FORCE)
set(CMAKE_HAVE_PTHREADS_CREATE OFF CACHE BOOL "Disable pthread create detection on Windows" FORCE)
set(CMAKE_THREAD_LIBS_INIT "" CACHE STRING "No pthread library on Windows" FORCE)
endif()
# Read VERSION first (before project())
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION")
message(FATAL_ERROR "VERSION file not found at ${CMAKE_CURRENT_SOURCE_DIR}/VERSION")
endif()
# Canonical dependency contract: strict in normal dev/release builds,
# diagnostic-only fallback in bootstrap troubleshooting.
include(cmake/DependencyContract.cmake)
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" _ver_raw)
string(STRIP "${_ver_raw}" _ver_raw)
# Parse semantic version
if(NOT _ver_raw MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)")
message(FATAL_ERROR "Invalid VERSION format: '${_ver_raw}'. Expected: 1.2.3 or 1.2.3-alpha")
endif()
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ver_numeric "${_ver_raw}")
# Define project with numeric version (CMake requirement)
project(Themis
VERSION ${_ver_numeric}
DESCRIPTION "ThemisDB - Enterprise Distributed Database"
LANGUAGES CXX
)
# Treat uncomponentized install() rules as runtime payload by default.
# This prevents third-party runtime DLLs from leaking into a separate
# 'Unspecified' package component and colliding with the explicit runtime
# package during WiX/MSI generation.
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME runtime)
# Canonical build-profile defaults:
# - Debug and test-focused dev builds should include unit tests by default.
# - Release builds should keep tests disabled unless explicitly requested.
# This keeps the default behavior consistent across presets, VS Code tasks, and
# direct CMake invocations without requiring wrapper scripts.
if(NOT DEFINED THEMIS_BUILD_TESTS)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(THEMIS_BUILD_TESTS ON CACHE BOOL "Build unit tests")
else()
set(THEMIS_BUILD_TESTS OFF CACHE BOOL "Build unit tests")
endif()
endif()
if(NOT DEFINED THEMIS_BUILD_BENCHMARKS)
set(THEMIS_BUILD_BENCHMARKS OFF CACHE BOOL "Build benchmarks")
endif()
option(ENABLE_FUZZING "Build standalone AFL++/libFuzzer harness targets" OFF)
if(ENABLE_FUZZING)
message(STATUS "Configuring standalone fuzz harness build")
set(THEMIS_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH "ThemisDB root directory")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(fuzz)
return()
endif()
function(themis_bootstrap_vcpkg_if_needed vcpkg_dir)
set(_themis_vcpkg_exe "${vcpkg_dir}/vcpkg")
if(WIN32)
set(_themis_vcpkg_exe "${vcpkg_dir}/vcpkg.exe")
endif()
if(EXISTS "${_themis_vcpkg_exe}")
message(STATUS "Auto-bootstrap: vcpkg executable already present at ${_themis_vcpkg_exe}")
return()
endif()
if(WIN32)
set(_themis_bootstrap_cmd cmd /c "${vcpkg_dir}/bootstrap-vcpkg.bat")
else()
find_program(THEMIS_SH_EXECUTABLE sh)
if(NOT THEMIS_SH_EXECUTABLE)
message(FATAL_ERROR
"Auto-bootstrap requires 'sh' on non-Windows hosts to run bootstrap-vcpkg.sh.")
endif()
set(_themis_bootstrap_cmd ${THEMIS_SH_EXECUTABLE} "${vcpkg_dir}/bootstrap-vcpkg.sh")
endif()
message(STATUS "Auto-bootstrap: bootstrapping vcpkg executable")
execute_process(
COMMAND ${_themis_bootstrap_cmd}
WORKING_DIRECTORY "${vcpkg_dir}"
RESULT_VARIABLE _themis_vcpkg_bootstrap_result
ERROR_VARIABLE _themis_vcpkg_bootstrap_error
)
if(NOT _themis_vcpkg_bootstrap_result EQUAL 0)
message(FATAL_ERROR
"Failed to bootstrap vcpkg executable in ${vcpkg_dir}.\n"
"vcpkg bootstrap error: ${_themis_vcpkg_bootstrap_error}\n"
"Retry with CMake-native bootstrap enabled:\n"
" cmake --preset <preset> -DTHEMIS_AUTO_BOOTSTRAP_DEPS=ON")
endif()
endfunction()
# Preflight for externally provided toolchain file
# Common failure: CMAKE_TOOLCHAIN_FILE points to missing repo-local vcpkg path.
# Guard: skip when CMAKE_TOOLCHAIN_FILE is empty (community/system-package builds set it to "").
if(DEFINED CMAKE_TOOLCHAIN_FILE AND NOT "${CMAKE_TOOLCHAIN_FILE}" STREQUAL "" AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
if(THEMIS_AUTO_BOOTSTRAP_DEPS)
find_program(THEMIS_GIT_EXECUTABLE git)
if(NOT THEMIS_GIT_EXECUTABLE)
message(FATAL_ERROR
"CMAKE_TOOLCHAIN_FILE is missing: ${CMAKE_TOOLCHAIN_FILE}\n"
"Auto-bootstrap is enabled but 'git' was not found in PATH.")
endif()
set(_themis_vcpkg_dir "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg")
set(_themis_vcpkg_toolchain "${_themis_vcpkg_dir}/scripts/buildsystems/vcpkg.cmake")
if(NOT EXISTS "${_themis_vcpkg_toolchain}")
message(STATUS "Auto-bootstrap: cloning vcpkg into ${_themis_vcpkg_dir}")
execute_process(
COMMAND ${THEMIS_GIT_EXECUTABLE} clone https://github.com/microsoft/vcpkg.git "${_themis_vcpkg_dir}"
RESULT_VARIABLE _themis_vcpkg_clone_result
OUTPUT_QUIET
ERROR_VARIABLE _themis_vcpkg_clone_error
)
if(NOT _themis_vcpkg_clone_result EQUAL 0)
message(FATAL_ERROR
"Failed to auto-bootstrap vcpkg.\n"
"git clone error: ${_themis_vcpkg_clone_error}\n"
"Retry with CMake-native bootstrap enabled:\n"
" cmake --preset <preset> -DTHEMIS_AUTO_BOOTSTRAP_DEPS=ON")
endif()
endif()
themis_bootstrap_vcpkg_if_needed("${_themis_vcpkg_dir}")
set(CMAKE_TOOLCHAIN_FILE "${_themis_vcpkg_toolchain}" CACHE STRING "vcpkg toolchain file" FORCE)
message(STATUS "Auto-bootstrap: using toolchain ${CMAKE_TOOLCHAIN_FILE}")
else()
message(FATAL_ERROR
"CMAKE_TOOLCHAIN_FILE points to a missing file:\n"
" ${CMAKE_TOOLCHAIN_FILE}\n\n"
"Fix options:\n"
" 1) Reconfigure with CMake-native bootstrap: -DTHEMIS_AUTO_BOOTSTRAP_DEPS=ON\n"
" 2) Set CMAKE_TOOLCHAIN_FILE to a valid vcpkg toolchain path")
endif()
endif()
# Store full semantic version
set(THEMIS_VERSION_STRING "${_ver_raw}")
# Default parallelism used by build wrappers and as a hint for developers.
# This cache entry can be inspected by tools and users; wrappers default to this
# value when no explicit `-j` is provided.
# Optional submodule initialization during configure
if(THEMIS_AUTO_BOOTSTRAP_DEPS AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
find_program(THEMIS_GIT_EXECUTABLE git)
if(THEMIS_GIT_EXECUTABLE)
set(_themis_missing_submodules "")
foreach(_themis_submodule IN ITEMS "vcpkg" "ffmpeg" "llama.cpp" "whisper.cpp" "stable-diffusion.cpp")
execute_process(
COMMAND ${THEMIS_GIT_EXECUTABLE} -C "${CMAKE_CURRENT_SOURCE_DIR}" ls-files -s -- "${_themis_submodule}"
RESULT_VARIABLE _themis_submodule_tracked_result
OUTPUT_VARIABLE _themis_submodule_tracked_output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT _themis_submodule_tracked_result EQUAL 0 OR
NOT _themis_submodule_tracked_output MATCHES "^160000 ")
continue()
endif()
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_themis_submodule}/.git" AND
NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_themis_submodule}/CMakeLists.txt")
list(APPEND _themis_missing_submodules "${_themis_submodule}")
endif()
endforeach()
if(_themis_missing_submodules)
message(STATUS "Auto-bootstrap: initializing submodules: ${_themis_missing_submodules}")
execute_process(
COMMAND ${THEMIS_GIT_EXECUTABLE} -C "${CMAKE_CURRENT_SOURCE_DIR}" submodule update --init --recursive ${_themis_missing_submodules}
RESULT_VARIABLE _themis_submodule_result
ERROR_VARIABLE _themis_submodule_error
)
if(NOT _themis_submodule_result EQUAL 0)
message(WARNING
"Failed to initialize submodules (${_themis_missing_submodules}): ${_themis_submodule_error}\n"
"You can run manually: git submodule update --init --recursive ${_themis_missing_submodules}")
endif()
endif()
endif()
endif()
# Note: wrapper-based build logging was removed; rely on CMake presets and
# the build system's own `--parallel` / `jobs` settings instead.
# ============================================================================
# MODULAR CMAKE INCLUDES - REFACTORED BUILD SYSTEM
# ============================================================================
# Build order (critical):
# 1. Version info
# 2. Compiler settings
# 3. Platform detection (arch/OS)
# 4. Architecture optimizations
# 5. Preload targets
# 6. Edition selection (BEFORE vcpkg to set feature flags!)
# 7. Feature configuration (BEFORE vcpkg to enable correct features!)
# 8. vcpkg Configuration (AFTER features are set!)
# 9. Dependencies
# 10. Validation (platform, edition, features)
# 1. Version management (version info only)
include(cmake/Versions.cmake)
# 2. Compiler setup
include(cmake/CompilerOptions.cmake)
# 2.5. Compiler cache launcher (sccache/ccache)
include(cmake/CompilerCache.cmake)
# Core helpers (sets global C++ standard, test helper)
include(cmake/helpers.cmake OPTIONAL)
# Emit deprecation/warning checks (e.g. add_definitions() usage)
include(cmake/deprecated_checks.cmake OPTIONAL)
# 3. Platform detection (OS, CPU, triplet)
include(cmake/platforms/PlatformDetection.cmake)
include(cmake/validation/CrossCompileValidation.cmake)
# 4. Architecture optimizations (AVX2, NEON, etc.)
include(cmake/platforms/ArchitectureOptimizations.cmake)
# 5. Preload targets for system packages
include(cmake/PreloadTargets.cmake)
# 6. Edition selection (MINIMAL, COMMUNITY, ENTERPRISE, HYPERSCALER)
include(cmake/editions/EditionDefaults.cmake)
# 7. Feature configuration (LLM, GPU, Network, Security, Tools, Optimizations)
include(cmake/features/FeatureDefaults.cmake)
# 7.5. Edition-Feature Matrix Validation (ensures features are compatible with edition)
include(cmake/EditionMatrix.cmake)
# 8. vcpkg Configuration (binary cache, feature dependencies)
include(cmake/VcpkgConfiguration.cmake)
# Fallback include directory for environments where imported targets do not
# propagate umbrella include paths reliably. This path is consumed by target-
# scoped include declarations in module/core targets.
set(THEMIS_VCPKG_INCLUDE_FALLBACK_DIR "" CACHE INTERNAL "vcpkg fallback include directory")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/installed/${VCPKG_TARGET_TRIPLET}/include")
set(THEMIS_VCPKG_INCLUDE_FALLBACK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/installed/${VCPKG_TARGET_TRIPLET}/include")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/installed/x64-windows/include")
set(THEMIS_VCPKG_INCLUDE_FALLBACK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/installed/x64-windows/include")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
set(THEMIS_VCPKG_INCLUDE_FALLBACK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg_installed/x64-windows/include")
endif()
if(THEMIS_VCPKG_INCLUDE_FALLBACK_DIR)
message(STATUS "Using target-scoped vcpkg fallback include directory: ${THEMIS_VCPKG_INCLUDE_FALLBACK_DIR}")
endif()
# 9. Dependencies
include(cmake/Dependencies.cmake)
# 10. Validation layer
include(cmake/validation/PlatformValidation.cmake)
include(cmake/validation/EditionValidation.cmake)
include(cmake/validation/FeatureValidation.cmake)
# Build-speed controls for script-adjacent generated artifacts.
# Default policy:
# - Debug or test-focused builds: do not eagerly build shaders and do not require
# shader artifacts for core target builds.
# - Non-debug builds without tests: keep eager/required behavior enabled.
set(_themis_shader_build_default ON)
if((DEFINED CMAKE_BUILD_TYPE AND CMAKE_BUILD_TYPE STREQUAL "Debug") OR
(DEFINED THEMIS_BUILD_TESTS AND THEMIS_BUILD_TESTS))
set(_themis_shader_build_default OFF)
endif()
option(THEMIS_EAGER_SHADER_BUILD
"Build GPU shader artifacts as part of the default ALL target"
${_themis_shader_build_default})
option(THEMIS_REQUIRE_SHADER_ARTIFACTS
"Require shader artifact targets as dependencies of GPU libraries/executables"
${_themis_shader_build_default})
message(STATUS "Shader build policy: eager=${THEMIS_EAGER_SHADER_BUILD}, required=${THEMIS_REQUIRE_SHADER_ARTIFACTS}")
# Redirect runtime/library/archive outputs to separate folders on Windows to
# avoid parallel linker/file-lock collisions in shared `bin/` during large
# parallel builds. Tests and runtime loaders should prefer the `bin/` path
# added below; PATH modifications in tests are updated accordingly.
if(WIN32)
set(_themis_bin_dir "${CMAKE_BINARY_DIR}/bin")
set(_themis_lib_out "${CMAKE_BINARY_DIR}/lib")
file(MAKE_DIRECTORY "${_themis_bin_dir}")
file(MAKE_DIRECTORY "${_themis_lib_out}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${_themis_bin_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${_themis_bin_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${_themis_lib_out})
foreach(_cfg IN ITEMS Debug Release RelWithDebInfo MinSizeRel)
string(TOUPPER "${_cfg}" _ucfg)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${_ucfg} ${_themis_bin_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${_ucfg} ${_themis_bin_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${_ucfg} ${_themis_lib_out})
endforeach()
endif()
# Testing - handled in cmake/CMakeLists.txt via add_subdirectory(tests)
# (Moved to cmake/CMakeLists.txt to be closer to target definitions)
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_Interpreter_FOUND)
add_custom_target(license-compliance
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/license-compliance"
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_vcpkg_licenses.py"
--manifest "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg.json"
--policy "${CMAKE_CURRENT_SOURCE_DIR}/.license-policy.json"
--output-dir "${CMAKE_BINARY_DIR}/license-compliance"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "Checking vcpkg dependency licenses against .license-policy.json"
)
else()
message(STATUS "Python3 interpreter not found; license-compliance target is unavailable")
endif()
# ============================================================================
# PERFORMANCE MEASUREMENT FEATURE FLAGS
# ============================================================================
option(THEMIS_ENABLE_CYCLE_METRICS
"Enable CPU cycle measurement (low overhead: ~0.05%)"
OFF)
option(THEMIS_ENABLE_DETAILED_METRICS
"Enable detailed per-function metrics (overhead: ~5-10%)"
OFF)
option(THEMIS_ENABLE_GPU_CYCLE_METRICS
"Enable GPU cycle measurement via CUDA events (overhead: ~10-20%)"
OFF)
option(THEMIS_ENABLE_PMU_COUNTERS
"Enable hardware performance counters via perf (overhead: ~10-50%)"
OFF)
option(THEMIS_ENABLE_IO_URING
"Enable io_uring zero-copy I/O path for network performance (Linux ≥ 5.1 only)"
OFF)
option(THEMIS_ENABLE_METRICS_EXPORT
"Enable Prometheus/CHIMERA export (adds async overhead)"
OFF)
option(THEMIS_BENCHMARK_MODE
"Enable all performance measurement features (FOR BENCHMARKING ONLY!)"
OFF)
# Compile definitions are accumulated here and applied in a target-scoped way
# from cmake/CMakeLists.txt and cmake/ModularBuild.cmake.
set(THEMIS_GLOBAL_COMPILE_DEFINITIONS "")
# ============================================================================
# CLOUD SDK INTEGRATION
# ============================================================================
option(THEMIS_WITH_S3_SDK
"Enable AWS S3 cloud backup provider (requires aws-sdk-cpp)"
OFF)
option(THEMIS_WITH_AZURE_SDK
"Enable Azure Blob Storage cloud backup provider (requires azure-storage-blobs-cpp)"
OFF)
option(THEMIS_WITH_GCS_SDK
"Enable Google Cloud Storage cloud backup provider (requires google-cloud-cpp)"
OFF)
# ============================================================================
# THREAD SANITIZER SUPPORT
# ============================================================================
option(THEMIS_ENABLE_TSAN
"Enable ThreadSanitizer for detecting data races (overhead: ~5-15x)"
OFF)
if(THEMIS_ENABLE_TSAN)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
message(FATAL_ERROR "ThreadSanitizer requires Clang or GCC compiler")
endif()
set(TSAN_FLAGS "-fsanitize=thread -g -O1 -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TSAN_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TSAN_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${TSAN_FLAGS}")
# Set TSan suppressions file if it exists
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tsan_suppressions.txt")
set(ENV{TSAN_OPTIONS} "suppressions=${CMAKE_CURRENT_SOURCE_DIR}/tsan_suppressions.txt")
message(STATUS "ThreadSanitizer suppressions: ${CMAKE_CURRENT_SOURCE_DIR}/tsan_suppressions.txt")
endif()
message(WARNING "==========================================")
message(WARNING "ThreadSanitizer (TSan) is ENABLED")
message(WARNING "Expected slowdown: 5-15x")
message(WARNING "Memory overhead: 5-10x")
message(WARNING "FOR TESTING ONLY - NOT FOR PRODUCTION")
message(WARNING "==========================================")
endif()
# When THEMIS_BENCHMARK_MODE is ON, enable all measurement features
if(THEMIS_BENCHMARK_MODE)
foreach(_themis_bench_flag IN ITEMS
THEMIS_ENABLE_CYCLE_METRICS
THEMIS_ENABLE_DETAILED_METRICS
THEMIS_ENABLE_GPU_CYCLE_METRICS
THEMIS_ENABLE_PMU_COUNTERS
THEMIS_ENABLE_METRICS_EXPORT)
if(NOT DEFINED CACHE{${_themis_bench_flag}})
set(${_themis_bench_flag} ON CACHE BOOL "Enabled by THEMIS_BENCHMARK_MODE")
elseif(NOT ${_themis_bench_flag})
message(STATUS "THEMIS_BENCHMARK_MODE requested, but ${_themis_bench_flag}=OFF is kept (user override)")
endif()
endforeach()
message(WARNING "==========================================")
message(WARNING "THEMIS_BENCHMARK_MODE is ENABLED")
message(WARNING "All performance measurement features are ON")
message(WARNING "Expected overhead: 5-50%")
message(WARNING "FOR BENCHMARKING ONLY - NOT FOR PRODUCTION")
message(WARNING "==========================================")
endif()
# Set compile definitions based on options
if(THEMIS_ENABLE_CYCLE_METRICS)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_ENABLE_CYCLE_METRICS)
message(STATUS "Cycle metrics: ENABLED")
endif()
if(THEMIS_ENABLE_DETAILED_METRICS)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_ENABLE_DETAILED_METRICS)
message(STATUS "Detailed metrics: ENABLED")
endif()
if(THEMIS_ENABLE_GPU_CYCLE_METRICS)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_ENABLE_GPU_CYCLE_METRICS)
message(STATUS "GPU cycle metrics: ENABLED")
endif()
if(THEMIS_ENABLE_PMU_COUNTERS)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_ENABLE_PMU_COUNTERS)
message(STATUS "PMU counters: ENABLED")
endif()
if(THEMIS_ENABLE_IO_URING)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_ENABLE_IO_URING)
message(STATUS "io_uring zero-copy I/O: ENABLED")
endif()
# ── Wave 3 optional dependencies (Kategorie C Prio 2, Q1 2027) ───────────────
# THEMIS_HAS_GGML — real ggml_new_tensor_1d + ggml_type_register
option(THEMIS_HAS_GGML
"Enable real ggml tensor allocation via ggml_new_tensor_1d() (requires ggml)"
OFF)
if(THEMIS_HAS_GGML)
find_package(ggml QUIET)
if(ggml_FOUND)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_HAS_GGML)
message(STATUS "THEMIS_HAS_GGML: ON (ggml found via find_package)")
else()
message(WARNING "THEMIS_HAS_GGML requested but ggml not found; falling back to FakeTensor")
endif()
endif()
# THEMIS_HAS_IO_URING — async readahead via liburing in ggml_tensor_bridge
option(THEMIS_HAS_IO_URING
"Enable io_uring async readahead in GgmlTensorBridge::prefetch() (requires liburing)"
OFF)
if(THEMIS_HAS_IO_URING)
find_library(LIBURING_LIB uring)
find_path(LIBURING_INCLUDE liburing.h)
if(LIBURING_LIB AND LIBURING_INCLUDE)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_HAS_IO_URING)
set(THEMIS_IO_URING_LIB "${LIBURING_LIB}" CACHE INTERNAL "liburing path for target-scoped linking")
set(THEMIS_IO_URING_INCLUDE "${LIBURING_INCLUDE}" CACHE INTERNAL "liburing include dir for target-scoped includes")
message(STATUS "THEMIS_HAS_IO_URING: ON (liburing found at ${LIBURING_LIB})")
else()
message(WARNING "THEMIS_HAS_IO_URING requested but liburing not found; prefetch is a no-op")
endif()
endif()
# THEMIS_HAS_YAML_CPP — full YAML spec compliance in KnowledgeBase::loadRulesFromYaml()
option(THEMIS_HAS_YAML_CPP
"Enable yaml-cpp for full YAML spec compliance in KnowledgeBase (requires yaml-cpp)"
OFF)
if(THEMIS_HAS_YAML_CPP)
find_package(yaml-cpp QUIET)
if(yaml-cpp_FOUND)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_HAS_YAML_CPP)
link_libraries(yaml-cpp)
message(STATUS "THEMIS_HAS_YAML_CPP: ON (yaml-cpp found)")
else()
message(WARNING "THEMIS_HAS_YAML_CPP requested but yaml-cpp not found; inline parser is used")
endif()
endif()
# THEMIS_HAS_NLI — real ONNX Runtime inference in NLIFaithfulnessVerifier
option(THEMIS_HAS_NLI
"Enable real ONNX Runtime NLI inference in NLIFaithfulnessVerifier (requires onnxruntime)"
OFF)
if(THEMIS_HAS_NLI)
find_package(onnxruntime QUIET)
if(onnxruntime_FOUND OR TARGET onnxruntime)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_HAS_NLI)
message(STATUS "THEMIS_HAS_NLI: ON (onnxruntime found)")
else()
message(WARNING "THEMIS_HAS_NLI requested but onnxruntime not found; heuristic fallback is used")
endif()
endif()
# ── End Wave 3 optional dependencies ─────────────────────────────────────────
if(THEMIS_ENABLE_METRICS_EXPORT)
list(APPEND THEMIS_GLOBAL_COMPILE_DEFINITIONS THEMIS_ENABLE_METRICS_EXPORT)
message(STATUS "Metrics export: ENABLED")
endif()
# Set root directory for use in subdirectories (MUST be before add_subdirectory(cmake))
set(THEMIS_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH "ThemisDB root directory")
# CHIMERA Suite (optional, vendor-neutral)
option(THEMIS_BUILD_CHIMERA "Build CHIMERA adapters, focused tests, and benchmark integration" OFF)
option(BUILD_CHIMERA_ADAPTERS "Deprecated alias for THEMIS_BUILD_CHIMERA" OFF)
if(BUILD_CHIMERA_ADAPTERS)
message(DEPRECATION "BUILD_CHIMERA_ADAPTERS is deprecated; use THEMIS_BUILD_CHIMERA instead.")
if(NOT THEMIS_BUILD_CHIMERA)
set(THEMIS_BUILD_CHIMERA ON CACHE BOOL "Build CHIMERA adapters, focused tests, and benchmark integration" FORCE)
endif()
endif()
# Module Externalization Options (Wave-2+ targets)
option(THEMIS_EXTERNALIZE_GEO_PLUGIN
"Build geospatial module as external plugin (default: OFF = integrated mode)" OFF)
if(THEMIS_EXTERNALIZE_GEO_PLUGIN)
message(STATUS "Geo module: externalized plugin mode (THEMIS_EXTERNALIZE_GEO_PLUGIN=ON)")
add_subdirectory(plugins/themisdb_geo)
else()
message(STATUS "Geo module: integrated mode (default, THEMIS_EXTERNALIZE_GEO_PLUGIN=OFF)")
endif()
# Enable CTest infrastructure at root level so `ctest --preset` can find tests
# registered inside add_subdirectory(cmake). Without this, CTestTestfile.cmake
# is only generated in build-dir/cmake/, not in the root binary dir.
enable_testing()
# Main build logic
add_subdirectory(cmake)
# Include CPack configuration if present (provides CPack packaging config and generators)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CPackConfig.cmake")
include(cmake/CPackConfig.cmake)
message(STATUS "CPack configuration included from cmake/CPackConfig.cmake")
else()
message(STATUS "No cmake/CPackConfig.cmake found — packaging disabled until added")
endif()
# ============================================================================
# DOCUMENTATION DATABASE GENERATION
# ============================================================================
# Control docs.db handling for local and CI/CD builds.
# Modes:
# AUTO - local: prefer cache, otherwise build; CI: prefer prebuilt from source
# BUILD - always build docs.db from sources
# PREBUILT - use prebuilt docs.db from THEMIS_DOCS_DB_PREBUILT_PATH
# OFF - disable docs.db packaging entirely
set(THEMIS_DOCS_DB_MODE "AUTO" CACHE STRING "docs.db mode: AUTO, BUILD, PREBUILT, OFF")
set_property(CACHE THEMIS_DOCS_DB_MODE PROPERTY STRINGS AUTO BUILD PREBUILT OFF)
set(THEMIS_DOCS_DB_PREBUILT_PATH "${CMAKE_SOURCE_DIR}/data/docs.db" CACHE PATH "Prebuilt docs.db directory path")
set(THEMIS_DOCS_DB_CACHE_PATH "${CMAKE_BINARY_DIR}/cache/docs.db" CACHE PATH "Local docs.db cache directory path")
# Backward-compatible flag support
option(THEMIS_BUILD_DOCS_DB "Legacy docs.db switch (ON=BUILD, OFF=PREBUILT)" ON)
if(NOT DEFINED CACHE{THEMIS_DOCS_DB_MODE})
if(THEMIS_BUILD_DOCS_DB)
set(THEMIS_DOCS_DB_MODE "BUILD" CACHE STRING "docs.db mode" FORCE)
else()
set(THEMIS_DOCS_DB_MODE "PREBUILT" CACHE STRING "docs.db mode" FORCE)
endif()
endif()
set(_themis_docs_db_mode "${THEMIS_DOCS_DB_MODE}")
if(_themis_docs_db_mode STREQUAL "AUTO")
if(EXISTS "${THEMIS_DOCS_DB_CACHE_PATH}")
set(_themis_docs_db_mode "PREBUILT")
set(_themis_docs_db_selected_prebuilt "${THEMIS_DOCS_DB_CACHE_PATH}")
message(STATUS "docs.db mode AUTO -> PREBUILT (using local cache: ${THEMIS_DOCS_DB_CACHE_PATH})")
elseif((DEFINED ENV{CI} OR DEFINED ENV{GITHUB_ACTIONS}) AND EXISTS "${THEMIS_DOCS_DB_PREBUILT_PATH}")
set(_themis_docs_db_mode "PREBUILT")
set(_themis_docs_db_selected_prebuilt "${THEMIS_DOCS_DB_PREBUILT_PATH}")
message(STATUS "docs.db mode AUTO -> PREBUILT (CI with source prebuilt docs.db)")
else()
set(_themis_docs_db_mode "BUILD")
message(STATUS "docs.db mode AUTO -> BUILD")
endif()
endif()
if(_themis_docs_db_mode STREQUAL "BUILD")
# Find Python3 for documentation database generation
find_package(Python3 COMPONENTS Interpreter)
if(Python3_FOUND)
# Documentation database generation target
set(DOCS_DB_OUTPUT "${CMAKE_BINARY_DIR}/data/docs.db")
set(DOCS_JSON_OUTPUT "${CMAKE_BINARY_DIR}/data/docs_database.json")
set(DOCS_IMPORTER_CPP "${CMAKE_BINARY_DIR}/data/import_docs_rocksdb.cpp")
# Generate JSON documentation database
add_custom_command(
OUTPUT ${DOCS_JSON_OUTPUT}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/data"
COMMAND ${Python3_EXECUTABLE}
${CMAKE_SOURCE_DIR}/scripts/generate_docs_database.py
--output ${DOCS_JSON_OUTPUT}
DEPENDS
${CMAKE_SOURCE_DIR}/scripts/generate_docs_database.py
${CMAKE_SOURCE_DIR}/tools/ingest.py
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Generating documentation JSON database (1151 documents)..."
VERBATIM
)
# Generate C++ RocksDB importer
add_custom_command(
OUTPUT ${DOCS_IMPORTER_CPP}
COMMAND ${Python3_EXECUTABLE}
${CMAKE_SOURCE_DIR}/scripts/generate_docs_rocksdb.py
--method cpp
--output ${DOCS_DB_OUTPUT}
DEPENDS
${DOCS_JSON_OUTPUT}
${CMAKE_SOURCE_DIR}/scripts/generate_docs_rocksdb.py
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Generating RocksDB importer C++ code..."
VERBATIM
)
# Build RocksDB importer executable (if RocksDB is available)
find_package(RocksDB QUIET)
if(RocksDB_FOUND)
add_executable(import_docs_rocksdb ${DOCS_IMPORTER_CPP})
target_link_libraries(import_docs_rocksdb
PRIVATE
RocksDB::rocksdb
nlohmann_json::nlohmann_json
)
target_compile_features(import_docs_rocksdb PRIVATE cxx_std_17)
# Import documentation into RocksDB
add_custom_command(
OUTPUT ${DOCS_DB_OUTPUT}
COMMAND import_docs_rocksdb ${DOCS_JSON_OUTPUT} ${DOCS_DB_OUTPUT}
COMMAND ${CMAKE_COMMAND} -E remove_directory "${THEMIS_DOCS_DB_CACHE_PATH}"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${DOCS_DB_OUTPUT} "${THEMIS_DOCS_DB_CACHE_PATH}"
DEPENDS
import_docs_rocksdb
${DOCS_JSON_OUTPUT}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Importing documentation into RocksDB (relational, graph, vector, metadata, :document)..."
VERBATIM
)
# Create top-level target
add_custom_target(docs_database
DEPENDS ${DOCS_DB_OUTPUT}
)
# Install documentation database
install(
DIRECTORY ${DOCS_DB_OUTPUT}
DESTINATION data
COMPONENT documentation
OPTIONAL
)
message(STATUS "Documentation database generation enabled")
message(STATUS " Output: ${DOCS_DB_OUTPUT}")
message(STATUS " Size: ~2-3 MB (1151 documents)")
message(STATUS " Cache: ${THEMIS_DOCS_DB_CACHE_PATH}")
else()
message(WARNING "RocksDB not found. Documentation database will be generated as JSON only.")
message(STATUS "JSON documentation database will be available at: ${DOCS_JSON_OUTPUT}")
endif()
else()
message(WARNING "Python3 not found. Documentation database generation disabled.")
message(STATUS "Install Python 3 to enable automatic documentation database generation.")
endif()
elseif(_themis_docs_db_mode STREQUAL "PREBUILT")
if(NOT DEFINED _themis_docs_db_selected_prebuilt)
set(_themis_docs_db_selected_prebuilt "${THEMIS_DOCS_DB_PREBUILT_PATH}")
endif()
if(EXISTS "${_themis_docs_db_selected_prebuilt}")
install(DIRECTORY "${_themis_docs_db_selected_prebuilt}"
DESTINATION data
COMPONENT documentation
OPTIONAL
)
message(STATUS "Using prebuilt documentation database at ${_themis_docs_db_selected_prebuilt}")
else()
message(WARNING "docs.db mode PREBUILT selected, but path not found: ${_themis_docs_db_selected_prebuilt}")
endif()
elseif(_themis_docs_db_mode STREQUAL "OFF")
message(STATUS "Documentation database packaging disabled (THEMIS_DOCS_DB_MODE=OFF)")
else()
message(FATAL_ERROR "Invalid THEMIS_DOCS_DB_MODE='${THEMIS_DOCS_DB_MODE}'. Allowed: AUTO, BUILD, PREBUILT, OFF")
endif()
# ─── AdaLoRA Rebuild-Gate (CMake target: adalora_cache_check) ────────────────
#
# Runs scripts/compute_model_fingerprint.py to determine whether the AdaLoRA
# adapter checkpoint needs to be rebuilt.
#
# Decision logic (in priority order):
# 1. THEMIS_ADALORA_FORCE_REBUILD=ON → always rebuild (exits 2)
# 2. THEMIS_LLM_MODEL_PATH not set → skip gate silently (no-op stamp)
# 3. Fingerprint matches cache → up-to-date (exit 0)
# 4. Fingerprint changed / no cache → rebuild required (exit 2)
#
# The target writes one of two stamp files into THEMIS_ADALORA_CACHE_DIR:
# adalora_rebuild_required.stamp — adapter must be retrained
# adalora_up_to_date.stamp — cached adapter may be loaded directly
#
# Downstream targets that depend on the adapter can check for these stamps.
option(THEMIS_ADALORA_FORCE_REBUILD
"Force AdaLoRA adapter rebuild regardless of cached fingerprint"
OFF)
set(THEMIS_ADALORA_CACHE_DIR
"${CMAKE_BINARY_DIR}/cache/adalora"
CACHE PATH "Directory for AdaLoRA checkpoint and model fingerprint cache")
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_FOUND)
set(_adalora_fp_script "${CMAKE_SOURCE_DIR}/scripts/compute_model_fingerprint.py")
if(EXISTS "${_adalora_fp_script}")
# Build the environment overrides for the subprocess
set(_adalora_env_args "")
if(THEMIS_ADALORA_FORCE_REBUILD)
list(APPEND _adalora_env_args
"${CMAKE_COMMAND}" -E env "THEMIS_ADALORA_FORCE_REBUILD=1")
endif()
add_custom_target(adalora_cache_check
COMMAND
${CMAKE_COMMAND} -E env
"THEMIS_ADALORA_CACHE_DIR=${THEMIS_ADALORA_CACHE_DIR}"
$<$<BOOL:${THEMIS_ADALORA_FORCE_REBUILD}>:THEMIS_ADALORA_FORCE_REBUILD=1>
"${Python3_EXECUTABLE}" "${_adalora_fp_script}"
COMMENT "AdaLoRA rebuild gate: checking model fingerprint…"
VERBATIM
)
message(STATUS "AdaLoRA cache-gate target 'adalora_cache_check' registered")
message(STATUS " Cache dir: ${THEMIS_ADALORA_CACHE_DIR}")
message(STATUS " Force rebuild: ${THEMIS_ADALORA_FORCE_REBUILD}")
else()
message(STATUS "AdaLoRA cache-gate: script not found at ${_adalora_fp_script} — target skipped")
endif()
else()
message(STATUS "AdaLoRA cache-gate: Python3 not found — adalora_cache_check target skipped")
endif()
if(DEFINED THEMIS_RELEASE_ROOT_DOC_FILES)
foreach(_themis_doc_file IN LISTS THEMIS_RELEASE_ROOT_DOC_FILES)
if(EXISTS "${CMAKE_SOURCE_DIR}/${_themis_doc_file}")
install(FILES "${CMAKE_SOURCE_DIR}/${_themis_doc_file}"
DESTINATION .
COMPONENT documentation
OPTIONAL
)
endif()
endforeach()
endif()
if(DEFINED THEMIS_RELEASE_LAYOUT_DOC)
if(EXISTS "${CMAKE_SOURCE_DIR}/${THEMIS_RELEASE_LAYOUT_DOC}")
install(FILES "${CMAKE_SOURCE_DIR}/${THEMIS_RELEASE_LAYOUT_DOC}"
DESTINATION docs
COMPONENT documentation
OPTIONAL
)
endif()
endif()
if(DEFINED THEMIS_RELEASE_SETUP_SCRIPT)
if(EXISTS "${CMAKE_SOURCE_DIR}/${THEMIS_RELEASE_SETUP_SCRIPT}")
install(FILES "${CMAKE_SOURCE_DIR}/${THEMIS_RELEASE_SETUP_SCRIPT}"
DESTINATION bin
COMPONENT tools
OPTIONAL
)
endif()
endif()
# ============================================================================
# OPTIONAL LLM MODEL PACKAGE CONTENT
# ============================================================================
# Models for release packages:
# - Gemma stays local for develop workflows
# - tinyllama is the deploy baseline via Ollama/local model directory
set(THEMIS_MODELS_MODE "SKIP" CACHE STRING "models packaging mode: AUTO, PACKAGE, SKIP")
set_property(CACHE THEMIS_MODELS_MODE PROPERTY STRINGS AUTO PACKAGE SKIP)
set(THEMIS_MODELS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/models" CACHE PATH "Directory containing packaged LLM model files")
option(THEMIS_PACKAGE_INCLUDE_TINYLLAMA "Include tinyllama artifacts in packaged models component" ON)
option(THEMIS_PACKAGE_INCLUDE_GEMMA "Include Gemma artifacts in packaged models component" OFF)
set(_themis_models_mode "${THEMIS_MODELS_MODE}")
if(_themis_models_mode STREQUAL "AUTO")
if(EXISTS "${THEMIS_MODELS_SOURCE_DIR}")
set(_themis_models_mode "PACKAGE")
message(STATUS "models mode AUTO -> PACKAGE (models dir found)")
else()
set(_themis_models_mode "SKIP")
message(STATUS "models mode AUTO -> SKIP (models dir not found: ${THEMIS_MODELS_SOURCE_DIR})")
endif()
endif()
if(_themis_models_mode STREQUAL "PACKAGE")
if(EXISTS "${THEMIS_MODELS_SOURCE_DIR}")
file(GLOB _themis_tinyllama_candidates
"${THEMIS_MODELS_SOURCE_DIR}/*tinyllama*.gguf"
"${THEMIS_MODELS_SOURCE_DIR}/*TinyLlama*.gguf"
"${THEMIS_MODELS_SOURCE_DIR}/default.gguf"
)
if(THEMIS_PACKAGE_INCLUDE_TINYLLAMA AND NOT _themis_tinyllama_candidates)
message(FATAL_ERROR
"models mode PACKAGE requires TinyLlama GGUF artifacts, but none were found in '${THEMIS_MODELS_SOURCE_DIR}'. "
"Provide TinyLlama files or set THEMIS_PACKAGE_INCLUDE_TINYLLAMA=OFF explicitly."
)
endif()
if(THEMIS_PACKAGE_INCLUDE_TINYLLAMA)
install(DIRECTORY "${THEMIS_MODELS_SOURCE_DIR}/"
DESTINATION models
COMPONENT models
OPTIONAL
FILES_MATCHING
PATTERN "*tinyllama*.gguf"
PATTERN "*TinyLlama*.gguf"
PATTERN "default.gguf"
PATTERN "*gemma*" EXCLUDE
PATTERN "*Gemma*" EXCLUDE
)
endif()
if(THEMIS_PACKAGE_INCLUDE_GEMMA)
install(DIRECTORY "${THEMIS_MODELS_SOURCE_DIR}/"
DESTINATION models
COMPONENT models
OPTIONAL
FILES_MATCHING
PATTERN "*gemma*"
PATTERN "*Gemma*"
)
endif()
message(STATUS "Including packaged runtime models from: ${THEMIS_MODELS_SOURCE_DIR}")
message(STATUS " - tinyllama in package: ${THEMIS_PACKAGE_INCLUDE_TINYLLAMA}")
message(STATUS " - gemma in package: ${THEMIS_PACKAGE_INCLUDE_GEMMA}")
else()
message(FATAL_ERROR "models mode PACKAGE selected, but directory not found: ${THEMIS_MODELS_SOURCE_DIR}")
endif()
elseif(_themis_models_mode STREQUAL "SKIP")
message(STATUS "Runtime model packaging disabled (THEMIS_MODELS_MODE=${_themis_models_mode})")
else()
message(FATAL_ERROR "Invalid THEMIS_MODELS_MODE='${THEMIS_MODELS_MODE}'. Allowed: AUTO, PACKAGE, SKIP")
endif()
# Local develop deployment helper: pull tinyllama via Ollama and export/copy into models/.
if(WIN32)
add_custom_target(deploy-ollama-tinyllama
COMMAND "${CMAKE_COMMAND}" -E echo "Deploying tinyllama:1.1b via Ollama to ${THEMIS_MODELS_SOURCE_DIR}"
COMMAND pwsh -NoProfile -ExecutionPolicy Bypass
-File "${CMAKE_SOURCE_DIR}/scripts/download-ollama-models-fixed.ps1"
-ModelNames tinyllama:1.1b
-OutputDir "${THEMIS_MODELS_SOURCE_DIR}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
COMMENT "Download and deploy tinyllama model for local develop workflow"
VERBATIM
)
endif()
# ============================================================================
# LEGAL TRAINING DATA INGESTION (HuggingFace legal-bert-de)
# ============================================================================
# Control whether legal training data is ingested during build. Defaults OFF.
# This is an optional feature that downloads and imports HuggingFace legal-bert-de dataset.
option(THEMIS_BUILD_LEGAL_TRAINING_DATA "Generate legal training data database during build" OFF)
if(THEMIS_BUILD_LEGAL_TRAINING_DATA)
# Find Python3 for legal training data ingestion
find_package(Python3 COMPONENTS Interpreter)
if(Python3_FOUND)
# Legal training data paths
set(LEGAL_JSON_OUTPUT "${CMAKE_BINARY_DIR}/data/legal_training_data.json")
set(LEGAL_DB_OUTPUT "${CMAKE_BINARY_DIR}/data/legal_training.db")
set(LEGAL_DB_STAMP "${CMAKE_BINARY_DIR}/data/legal_training.db.stamp")
set(LEGAL_IMPORTER_CPP "${CMAKE_BINARY_DIR}/data/import_legal_rocksdb.cpp")
# Step 1: Download and ingest HuggingFace dataset to JSON
add_custom_command(
OUTPUT ${LEGAL_JSON_OUTPUT}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/data"
COMMAND ${Python3_EXECUTABLE}
${CMAKE_SOURCE_DIR}/scripts/ingest_legal_training_data.py
--output ${LEGAL_JSON_OUTPUT}
--max-samples 10000
DEPENDS
${CMAKE_SOURCE_DIR}/scripts/ingest_legal_training_data.py
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Ingesting HuggingFace legal-bert-de dataset (10000 samples)..."
VERBATIM
)
# Step 2: Generate C++ RocksDB importer
add_custom_command(
OUTPUT ${LEGAL_IMPORTER_CPP}
COMMAND ${Python3_EXECUTABLE}
${CMAKE_SOURCE_DIR}/scripts/generate_legal_rocksdb.py
--method cpp
--output ${LEGAL_DB_OUTPUT}
DEPENDS
${LEGAL_JSON_OUTPUT}
${CMAKE_SOURCE_DIR}/scripts/generate_legal_rocksdb.py
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Generating RocksDB importer for legal training data..."
VERBATIM
)
# Step 3: Build and run RocksDB importer (if RocksDB is available)
find_package(RocksDB QUIET)
if(RocksDB_FOUND)
# Build importer executable
add_executable(import_legal_rocksdb ${LEGAL_IMPORTER_CPP})
target_link_libraries(import_legal_rocksdb
PRIVATE
RocksDB::rocksdb
nlohmann_json::nlohmann_json
)
target_compile_features(import_legal_rocksdb PRIVATE cxx_std_17)
# Step 4: Import legal training data into RocksDB
# Use a stamp file because RocksDB creates a directory, which CMake cannot track properly
add_custom_command(