Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
498 lines (444 loc) · 23.6 KB
/
Copy pathCMakeLists.txt
File metadata and controls
498 lines (444 loc) · 23.6 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
cmake_minimum_required(VERSION 3.16)
# -----------------------------------------------------------------------------
# Version (single source of truth: include/langid.h)
# -----------------------------------------------------------------------------
#
# The LANGID_VERSION_MAJOR/MINOR/PATCH macros in the public header are the one
# place the library version is defined. Parse them here, before project(), so
# the CMake project version, the shared-library VERSION/SOVERSION, and the
# value langid_version() reports all derive from the same source and cannot
# drift. To bump the library version, edit include/langid.h.
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/include/langid.h" _langid_header)
string(REGEX MATCH "define +LANGID_VERSION_MAJOR +([0-9]+)" _ "${_langid_header}")
set(LANGID_VERSION_MAJOR "${CMAKE_MATCH_1}")
string(REGEX MATCH "define +LANGID_VERSION_MINOR +([0-9]+)" _ "${_langid_header}")
set(LANGID_VERSION_MINOR "${CMAKE_MATCH_1}")
string(REGEX MATCH "define +LANGID_VERSION_PATCH +([0-9]+)" _ "${_langid_header}")
set(LANGID_VERSION_PATCH "${CMAKE_MATCH_1}")
if(NOT LANGID_VERSION_MAJOR MATCHES "^[0-9]+$" OR
NOT LANGID_VERSION_MINOR MATCHES "^[0-9]+$" OR
NOT LANGID_VERSION_PATCH MATCHES "^[0-9]+$")
message(FATAL_ERROR
"Failed to parse LANGID_VERSION_{MAJOR,MINOR,PATCH} from include/langid.h")
endif()
project(langid
VERSION ${LANGID_VERSION_MAJOR}.${LANGID_VERSION_MINOR}.${LANGID_VERSION_PATCH}
LANGUAGES C CXX)
# Short git commit for langid_version_commit(), captured at configure time.
# Best-effort: source tarballs and non-git trees fall back to "unknown". This
# is a configure-time snapshot (same posture as ggml's GGML_COMMIT);
# reconfigure to refresh it.
set(LANGID_BUILD_COMMIT "unknown")
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
execute_process(
COMMAND ${GIT_EXECUTABLE} -C "${CMAKE_CURRENT_SOURCE_DIR}" rev-parse --short HEAD
OUTPUT_VARIABLE _langid_git_sha
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
if(_langid_git_sha)
set(LANGID_BUILD_COMMIT "${_langid_git_sha}")
endif()
endif()
message(STATUS "langid version: ${PROJECT_VERSION} (commit ${LANGID_BUILD_COMMIT})")
# -----------------------------------------------------------------------------
# Standards
# -----------------------------------------------------------------------------
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()
include(GNUInstallDirs)
# Hidden symbol visibility by default: a shared liblangid exports only the
# LANGID_API surface, and the vendored ggml stays an implementation detail.
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
# -----------------------------------------------------------------------------
# Apple Silicon detection (drives the Metal default)
# -----------------------------------------------------------------------------
set(LANGID_IS_APPLE_SILICON OFF)
if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
set(LANGID_IS_APPLE_SILICON ON)
endif()
# -----------------------------------------------------------------------------
# Options
# -----------------------------------------------------------------------------
option(LANGID_BUILD_TESTS "Build unit / smoke tests" ON)
option(LANGID_BUILD_EXAMPLES "Build the example CLI" ON)
option(LANGID_METAL "Enable Metal backend" ${LANGID_IS_APPLE_SILICON})
option(LANGID_VULKAN "Enable Vulkan backend" OFF)
option(LANGID_CUDA "Enable CUDA backend" OFF)
option(LANGID_SANITIZE "Enable ASan + UBSan" OFF)
# Library link mode. OFF (default) builds a static liblangid + static ggml, so
# `cmake -B build` yields a self-contained langid-cli with no shared libraries
# to chase at runtime. ON produces a shared liblangid an FFI layer can dlopen.
option(LANGID_BUILD_SHARED "Build liblangid + ggml as shared libraries" OFF)
# ggml lane selection. See docs/distribution.md for the full table.
#
# LANGID_USE_SYSTEM_GGML=ON links an ALREADY-INSTALLED ggml (find_package)
# instead of building the vendored ggml/ tree. This is the lane an application
# uses to run several ggml engines (langid.cpp, transcribe.cpp, llama.cpp, ...)
# on ONE ggml: build ggml once at the tag in ggml/UPSTREAM, install it to a
# prefix, then build every engine with its *_USE_SYSTEM_GGML pointed there.
# Same spelling as LLAMA_USE_SYSTEM_GGML / WHISPER_USE_SYSTEM_GGML.
option(LANGID_USE_SYSTEM_GGML
"Link an installed ggml (find_package) instead of the vendored ggml/ tree" OFF)
# LANGID_GGML_BACKEND_DL=ON builds each ggml backend (CPU, Metal, Vulkan, ...)
# as a separate loadable MODULE living next to liblangid instead of being
# linked in. That is the plugin artifact shape: a backend whose driver/loader
# is missing on the host simply fails to dlopen and is skipped, so one package
# can ship GPU support without hard-requiring it. Nothing is compiled in, so
# the host MUST call langid_init_backends(dir) (or let langid_model_load fall
# back to langid_init_backends_default()) before there is anything to compute
# on. Requires LANGID_BUILD_SHARED=ON, and is meaningless with a system ggml
# (the module layout belongs to whoever built that ggml).
option(LANGID_GGML_BACKEND_DL
"Build ggml backends as loadable modules next to liblangid" OFF)
# OpenMP is opt-in, not the default: we use ggml's native CPU threadpool
# everywhere. OpenMP's runtime pool is process-global and outlives any single
# compute, which is a teardown and coexistence liability for an embeddable
# library (a vendored libgomp/libiomp can collide with numpy/torch/MKL in one
# process, and the loader can unmap the runtime out from under still-live pool
# threads at teardown). The native pool joins its threads per graph_compute.
option(LANGID_USE_OPENMP "Use OpenMP for ggml's CPU threadpool" OFF)
# Numerical-parity validation hooks: per-layer tensor dumps driven by
# LANGID_DUMP_DIR. OFF for release so the hook code is not compiled into
# shipped binaries. scripts/validate.py builds with this ON (see the
# "validation" preset in CMakePresets.json).
option(LANGID_ENABLE_VALIDATION_HOOKS
"Compile in numerical-parity validation hooks" OFF)
# Real-model gated tests. OFF by default because they need a converted GGUF on
# disk that CI cannot ship. When ON, the test reads the model path from a
# family-specific environment variable at run time.
option(LANGID_BUILD_REAL_MODEL_TESTS
"Build tests that load real model files via env var" OFF)
# Install rules (headers, libraries, lib/langid-link.json). ON when langid is
# the top-level project, OFF when it is vendored via add_subdirectory() so an
# embedder's `cmake --install` does not gain our artifacts uninvited.
# PROJECT_IS_TOP_LEVEL needs CMake 3.21; compare the source dirs instead so
# this holds at our declared 3.16 floor.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(LANGID_INSTALL_DEFAULT ON)
else()
set(LANGID_INSTALL_DEFAULT OFF)
endif()
option(LANGID_INSTALL "Generate install rules for liblangid" ${LANGID_INSTALL_DEFAULT})
# -----------------------------------------------------------------------------
# Warnings
# -----------------------------------------------------------------------------
#
# langid_apply_warnings(target): apply our project warning flags to one target.
# Used per-target so we never pollute the vendored ggml subdirectory with
# -Wshadow / -Wpedantic, which fire on a lot of legitimate ggml code.
function(langid_apply_warnings tgt)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
target_compile_options(${tgt} PRIVATE
-Wall
-Wextra
-Wshadow
-Wpedantic
-Wno-unused-parameter
# The output-side *_init helpers are deliberate zero-fill aggregate
# initializers: struct_size is set and every other field falls to
# initializer zero by C aggregate-init rules. The zero-means-absent
# contract is documented and load-bearing, so
# -Wmissing-field-initializers has no signal here.
-Wno-missing-field-initializers
)
endif()
endfunction()
# Sanitizers must be applied to every target in the build (including ggml) so
# the resulting binary is consistent. add_compile_options / add_link_options is
# the right scope for this.
#
# -fno-sanitize-recover=undefined makes UBSan reports fatal at runtime instead
# of merely printing them. ASan already aborts on error by default; UBSan does
# not, which would otherwise let ctest report success while the sanitizer
# printed real diagnostics.
if(LANGID_SANITIZE)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
message(FATAL_ERROR
"LANGID_SANITIZE requires a Clang or GNU compiler "
"(got ${CMAKE_CXX_COMPILER_ID})")
endif()
add_compile_options(
-fsanitize=address,undefined
-fno-sanitize-recover=undefined
# ggml's `incr_ptr_aligned` (ggml/src/ggml.c) uses the standard "use
# NULL as base, walk by offsets to compute struct layout sizes" trick.
# UBSan flags this as a `pointer-overflow` runtime error even though it
# is a widely-used and safe-in-practice C idiom. Make this specific
# check non-fatal so the run path does not abort under build-san; real
# bugs in our own code still fail loud.
-fsanitize-recover=pointer-overflow
-fno-omit-frame-pointer
)
add_link_options(-fsanitize=address,undefined)
endif()
# -----------------------------------------------------------------------------
# Output layout
# -----------------------------------------------------------------------------
#
# Put all executables in build/bin/ regardless of which subdirectory built
# them. ggml's CMakeLists already does this when invoked standalone; pinning it
# at the top level keeps the same layout when ggml is added as a subdirectory.
# Deliberately NOT set for LIBRARY/ARCHIVE outputs: liblangid stays in
# build/src/ where the link-smoke and symbol-visibility checks look for it.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# -----------------------------------------------------------------------------
# MSVC exception model
# -----------------------------------------------------------------------------
#
# Use /EHs so exceptions thrown by ggml's C-ABI frames unwind to langid.cpp's
# public-entry guards. Applies to every ggml lane, so it sits above the lane
# switch and before ggml and src are added.
if(MSVC)
string(REPLACE "/EHsc" "/EHs" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
if(NOT CMAKE_CXX_FLAGS MATCHES "/EHs")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs")
endif()
endif()
# -----------------------------------------------------------------------------
# The ggml pin (read from ggml/UPSTREAM)
# -----------------------------------------------------------------------------
#
# ggml/UPSTREAM is the vendor recipe: the upstream repo, the exact commit, the
# release tag that commit belongs to (langid.cpp pins RELEASES, not branch
# tips), and the ordered downstream patches. Parse it here so the identity of
# the ggml we ship travels into langid-link.json and langid_ggml_commit()
# instead of living only in a text file nobody reads.
#
# Why we do not simply trust ggml's own GGML_BUILD_COMMIT in this lane: ggml's
# CMake derives it from `git rev-parse --short HEAD` run in ggml/, which in a
# VENDORED (non-submodule) tree resolves to LANGID's HEAD, not ggml's. That is
# not merely unhelpful, it is wrong, so the vendored lane stamps the UPSTREAM
# sha instead (see LANGID_GGML_COMMIT in src/CMakeLists.txt).
# ggml's own GGML_VERSION is set inside ggml's directory scope and does not
# propagate back up to us, so parse the three version components out of
# ggml/CMakeLists.txt the same way we parse ours out of include/langid.h. The
# system lane overwrites this with the found package's GGML_VERSION.
set(LANGID_GGML_VERSION "unknown")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ggml/CMakeLists.txt")
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/ggml/CMakeLists.txt" _langid_ggml_cml)
if(_langid_ggml_cml MATCHES "set\\(GGML_VERSION_MAJOR ([0-9]+)\\)" )
set(_langid_ggml_v_major "${CMAKE_MATCH_1}")
endif()
if(_langid_ggml_cml MATCHES "set\\(GGML_VERSION_MINOR ([0-9]+)\\)" )
set(_langid_ggml_v_minor "${CMAKE_MATCH_1}")
endif()
if(_langid_ggml_cml MATCHES "set\\(GGML_VERSION_PATCH ([0-9]+)\\)" )
set(_langid_ggml_v_patch "${CMAKE_MATCH_1}")
endif()
if(DEFINED _langid_ggml_v_major AND DEFINED _langid_ggml_v_minor AND DEFINED _langid_ggml_v_patch)
set(LANGID_GGML_VERSION
"${_langid_ggml_v_major}.${_langid_ggml_v_minor}.${_langid_ggml_v_patch}")
endif()
endif()
set(LANGID_GGML_PIN_SHA "unknown")
set(LANGID_GGML_PIN_TAG "")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ggml/UPSTREAM")
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/ggml/UPSTREAM" _langid_ggml_upstream)
if(_langid_ggml_upstream MATCHES "sha:[ \t]*([0-9a-fA-F]+)")
set(LANGID_GGML_PIN_SHA "${CMAKE_MATCH_1}")
endif()
# Optional: absent in UPSTREAM files written before the release-tag pin.
if(_langid_ggml_upstream MATCHES "tag:[ \t]*([^ \t\r\n]+)")
set(LANGID_GGML_PIN_TAG "${CMAKE_MATCH_1}")
endif()
endif()
# -----------------------------------------------------------------------------
# ggml lane
# -----------------------------------------------------------------------------
#
# LANGID_GGML_LANE is the single name for which of the four postures this build
# is, and it is what langid-link.json reports:
#
# vendored-static ggml/ built in, static (default)
# vendored-shared ggml/ built in, shared (-DLANGID_BUILD_SHARED=ON)
# vendored-shared ... + backend plugins (+ -DLANGID_GGML_BACKEND_DL=ON)
# system find_package(ggml) (-DLANGID_USE_SYSTEM_GGML=ON)
#
# LANGID_GGML_LINK_TARGETS is the target name(s) everything in this project
# links for ggml: the in-tree `ggml` target, or the imported `ggml::ggml` the
# installed package exports. A plain add_library(ggml ALIAS ggml::ggml) (what
# llama.cpp/whisper.cpp do) would need CMake 3.18 for a non-GLOBAL imported
# target; our floor is 3.16, so use a variable instead.
if(LANGID_USE_SYSTEM_GGML)
# --- options that only mean something for the tree WE build --------------
# These configure the ggml build. With a system ggml there is no ggml build
# to configure, so accepting them would silently produce an artifact that
# does not match what was asked for. Fail loudly instead.
if(LANGID_GGML_BACKEND_DL)
message(FATAL_ERROR
"LANGID_GGML_BACKEND_DL is incompatible with LANGID_USE_SYSTEM_GGML: "
"whether ggml backends are loadable modules is decided when THAT ggml "
"is built. Configure the system ggml with -DGGML_BACKEND_DL=ON and "
"call langid_init_backends(<its module dir>) yourself.")
endif()
if(LANGID_USE_OPENMP)
message(FATAL_ERROR
"LANGID_USE_OPENMP is incompatible with LANGID_USE_SYSTEM_GGML: the "
"CPU threadpool posture is compiled into ggml. Configure the system "
"ggml with -DGGML_OPENMP=ON instead.")
endif()
# find_package honours CMAKE_PREFIX_PATH and ggml_DIR with no help from us.
# Version 0.20 is the floor the loader is written against; ggml's own
# config uses SameMajorVersion compatibility, so an older 0.x is rejected.
find_package(ggml 0.20 REQUIRED)
set(LANGID_GGML_LINK_TARGETS ggml::ggml)
set(LANGID_GGML_LANE "system")
# The GGML_* variables below are set by ggml-config.cmake (generated from
# the ggml build's own variables), NOT by us. Report them: in this lane the
# backend set is a property of the ggml that was found, and the caller's
# LANGID_METAL / LANGID_VULKAN / LANGID_CUDA are informational only.
set(LANGID_GGML_VERSION "${GGML_VERSION}")
message(STATUS "langid: using SYSTEM ggml ${LANGID_GGML_VERSION} (commit ${GGML_BUILD_COMMIT})")
message(STATUS "langid: system ggml provides backends: ${GGML_AVAILABLE_BACKENDS}")
message(STATUS "langid: system ggml shared=${GGML_SHARED_LIB} backend_dl=${GGML_BACKEND_DL} "
"metal=${GGML_METAL} vulkan=${GGML_VULKAN} cuda=${GGML_CUDA} blas=${GGML_BLAS}")
foreach(_opt METAL VULKAN CUDA)
if(LANGID_${_opt} AND NOT GGML_${_opt})
message(WARNING
"LANGID_${_opt}=ON is informational under LANGID_USE_SYSTEM_GGML and the "
"ggml that was found does NOT provide it; that backend will be absent.")
endif()
endforeach()
if(LANGID_SANITIZE)
message(WARNING
"LANGID_SANITIZE with LANGID_USE_SYSTEM_GGML instruments only langid's own "
"code: the installed ggml was compiled without sanitizers, so allocations "
"and accesses inside it are not checked.")
endif()
else()
# -------------------------------------------------------------------------
# Vendored ggml
# -------------------------------------------------------------------------
#
# ggml lives at the repo root (matching whisper.cpp / llama.cpp), at the
# SHA/tag in ggml/UPSTREAM. We map our LANGID_* options onto ggml's GGML_*
# options BEFORE add_subdirectory() so ggml's option() calls become no-ops
# and our defaults win.
set(GGML_METAL ${LANGID_METAL} CACHE BOOL "" FORCE)
set(GGML_VULKAN ${LANGID_VULKAN} CACHE BOOL "" FORCE)
set(GGML_CUDA ${LANGID_CUDA} CACHE BOOL "" FORCE)
# No BLAS: langid.cpp has no host-side GEMM path, everything runs through
# ggml.
set(GGML_BLAS OFF CACHE BOOL "" FORCE)
# tinyBLAS (llamafile_sgemm CPU kernels): faster CPU matmuls, numerically
# equivalent. On by default; CPU-backend only.
set(GGML_LLAMAFILE ON CACHE BOOL "" FORCE)
# OpenMP: honor an explicit -DGGML_OPENMP=... (opt-in); otherwise force OFF
# and use ggml's native threadpool (see the LANGID_USE_OPENMP option above).
if(NOT LANGID_USE_OPENMP AND NOT DEFINED CACHE{GGML_OPENMP})
set(GGML_OPENMP OFF CACHE BOOL "" FORCE)
endif()
# We don't want ggml's tests / examples leaking into our build tree. They
# default to OFF when ggml is added as a subdirectory (GGML_STANDALONE OFF),
# but pin them anyway so the contract is explicit.
set(GGML_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GGML_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
# Library link mode: both ggml and liblangid follow this single switch.
# Symbol visibility is already hidden by default (set above), so a shared
# build exports only the LANGID_API surface.
set(BUILD_SHARED_LIBS ${LANGID_BUILD_SHARED} CACHE BOOL "" FORCE)
# Embed the Metal shader library in the binary so a Metal build has no
# sidecar .metal files to locate at runtime.
if(LANGID_METAL)
set(GGML_METAL_EMBED_LIBRARY ON CACHE BOOL "" FORCE)
endif()
# Dynamic backend modules (see the option's comment above). ggml requires
# shared libs for GGML_BACKEND_DL, so gate it on LANGID_BUILD_SHARED here
# for a clear configure-time error instead of ggml's later one.
if(LANGID_GGML_BACKEND_DL)
if(NOT LANGID_BUILD_SHARED)
message(FATAL_ERROR
"LANGID_GGML_BACKEND_DL requires LANGID_BUILD_SHARED=ON "
"(backend modules are shared libraries loaded at runtime)")
endif()
set(GGML_BACKEND_DL ON CACHE BOOL "" FORCE)
# langid_init_backends_default() scans the directory that contains
# liblangid itself. Install the backend modules there too, unless a
# caller explicitly chose a different provider layout and will pass it
# to langid_init_backends(dir).
#
# Test emptiness, not DEFINED: ggml declares GGML_BACKEND_DIR as an
# empty cache var of its own, so on any reconfigure it is already
# DEFINED (as "") and a `NOT DEFINED CACHE{...}` guard would skip our
# override, leaving the modules where the lib-dir loader cannot find
# them. `NOT GGML_BACKEND_DIR` treats an empty/unset value as "ours to
# set" while still respecting a non-empty caller-provided path.
if(NOT GGML_BACKEND_DIR)
# The directory that holds liblangid itself: bin/ on Windows (a DLL
# is a RUNTIME artifact) and lib/ elsewhere. Using lib/
# unconditionally would strand the modules away from langid.dll on
# Windows, where the loader cannot find them.
if(WIN32)
set(GGML_BACKEND_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH "" FORCE)
else()
set(GGML_BACKEND_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "" FORCE)
endif()
endif()
# ggml hard-errors on GGML_NATIVE + GGML_BACKEND_DL (x86): native tuning
# is incompatible with feature-scored modules. A module build is a
# distribution build, so never tune it to the build host.
set(GGML_NATIVE OFF CACHE BOOL "" FORCE)
# Metal's capability queries (ggml_backend_is_metal,
# ggml_backend_metal_supports_family) live in the ggml-metal MODULE and
# are not link-time symbols in this lane, so the pre-Apple7 simdgroup
# guard in load_common cannot be compiled in (see src/CMakeLists.txt).
# Consequence: on an Intel Mac with a pre-MTLGPUFamilyApple7 GPU, AUTO
# can pick Metal and get silently wrong numbers. Apple silicon is
# Apple7+ and unaffected. Request LANGID_BACKEND_CPU on such a host.
if(LANGID_METAL)
message(WARNING
"LANGID_GGML_BACKEND_DL + Metal: the pre-MTLGPUFamilyApple7 "
"capability guard is compiled out in the plugin lane (the Metal "
"queries are not link-time symbols there). Affects Intel Macs "
"with pre-Apple7 GPUs only.")
endif()
# BUILD-TREE co-location. ggml puts backend MODULEs in
# ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} (= <build>/bin), while liblangid
# would otherwise land in <build>/src — and the default loader scans the
# directory containing LIBLANGID, so <build>/bin/langid-cli would find
# nothing. Point library output at bin/ too, for THIS LANE ONLY, so an
# in-tree run behaves like an installed one. GGML_BACKEND_DIR above is
# install-relative and does not help here.
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif()
# ggml's CMake declares its own warning flags; let it.
add_subdirectory(ggml)
set(LANGID_GGML_LINK_TARGETS ggml)
if(LANGID_BUILD_SHARED)
set(LANGID_GGML_LANE "vendored-shared")
else()
set(LANGID_GGML_LANE "vendored-static")
endif()
message(STATUS
"langid: using VENDORED ggml ${LANGID_GGML_VERSION} "
"(${LANGID_GGML_PIN_TAG} ${LANGID_GGML_PIN_SHA})")
endif()
# -----------------------------------------------------------------------------
# Subdirs
# -----------------------------------------------------------------------------
add_subdirectory(src)
if(LANGID_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
if(LANGID_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
# -----------------------------------------------------------------------------
# Install
# -----------------------------------------------------------------------------
#
# Last, so every target the install rules reference (langid, ggml, the backend
# libraries) already exists and GGML_AVAILABLE_BACKENDS is populated.
if(LANGID_INSTALL)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/langid-install.cmake)
endif()