-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
627 lines (547 loc) · 25.9 KB
/
Copy pathCMakeLists.txt
File metadata and controls
627 lines (547 loc) · 25.9 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
# Copyright (c) 2017, Mate Soos
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
# CMP0104 (CUDA architectures default) was introduced in 3.18; guard needed
if(POLICY CMP0104)
cmake_policy(SET CMP0104 NEW)
endif()
# -----------------------------------------------------------------------------
# Make RelWithDebInfo the default build type if otherwise not set
# -----------------------------------------------------------------------------
set(build_types Debug Release RelWithDebInfo MinSizeRel)
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "You can choose the type of build, options are:${build_types}")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Options are ${build_types}"
FORCE
)
# Provide drop down menu options in cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${build_types})
endif()
message(STATUS "Doing a ${CMAKE_BUILD_TYPE} build")
# -----------------------------------------------------------------------------
# Option to enable/disable assertions
# -----------------------------------------------------------------------------
# Filter out definition of NDEBUG from the default build configuration flags.
# We will add this ourselves if we want to disable assertions
foreach(build_config ${build_types})
string(TOUPPER ${build_config} upper_case_build_config)
foreach(language CXX C)
set(VAR_TO_MODIFY "CMAKE_${language}_FLAGS_${upper_case_build_config}")
string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )"
" "
replacement
"${${VAR_TO_MODIFY}}")
set(${VAR_TO_MODIFY} "${replacement}" CACHE STRING "Default flags for ${build_config} configuration" FORCE)
endforeach()
endforeach()
set(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY ON)
set(CMAKE_EXPORT_PACKAGE_REGISTRY OFF)
project(ganak)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(GenerateExportHeader)
include(GNUInstallDirs)
message(STATUS "LIB directory is '${CMAKE_INSTALL_LIBDIR}'")
message(STATUS "BIN directory is '${CMAKE_INSTALL_BINDIR}'")
# contains some library search cmake scripts
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# generate JSON file of compile commands -- useful for code extension
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
# Python extension — defined early so the GMP/MPFR/flint discovery below can use it.
option(BUILD_PYTHON_EXTENSION "Build the pyganak Python extension module" OFF)
# static compilation
option(BUILD_SHARED_LIBS "Build the shared library" ON)
# STATIC_BINARY controls whether the ganak executable is linked fully statically.
# Defaults to ON when BUILD_SHARED_LIBS=OFF, but can be set to OFF independently
# (e.g. when building the Python wheel: BUILD_SHARED_LIBS=OFF embeds static .a
# deps in the .so, but we don't want a fully static ganak binary).
if(NOT BUILD_SHARED_LIBS)
option(STATIC_BINARY "Link the ganak binary fully statically" ON)
else()
option(STATIC_BINARY "Link the ganak binary fully statically" OFF)
endif()
if((${CMAKE_SYSTEM_NAME} MATCHES "Linux") OR (${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
if(STATIC_BINARY AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
message(STATUS "Linking ganak binary fully statically")
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++")
# CMake probes the compiler without -static, so its captured
# CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES name the *shared* libgcc/libatomic
# variants (gcc_s, gcc_s_asneeded, atomic_asneeded) whose .a counterparts
# don't exist. Under -static -static-libgcc -static-libstdc++ the gcc/clang
# driver links libgcc.a + libgcc_eh.a + libstdc++.a itself, so we don't
# need CMake to re-inject anything. Clear both lists outright rather than
# playing whack-a-mole with distro-specific names.
set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
endif()
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
include(CheckLinkerFlag)
macro(add_cxx_flag_if_supported flagname)
check_cxx_compiler_flag("${flagname}" HAVE_FLAG_${flagname})
if(HAVE_FLAG_${flagname})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flagname}")
endif()
endmacro()
macro(add_c_flag_if_supported flagname)
check_c_compiler_flag("${flagname}" HAVE_FLAG_${flagname})
if(HAVE_FLAG_${flagname})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flagname}")
endif()
endmacro()
macro(add_link_flag_if_supported flagname)
check_linker_flag(CXX "${flagname}" HAVE_FLAG_${flagname})
if(HAVE_FLAG_${flagname})
add_link_options("${flagname}")
endif()
endmacro()
# Note: O3 gives slight speed increase, 1 more solved from SAT Comp'14 @ 3600s
if(NOT MSVC)
add_compile_options(-g)
add_compile_options("$<$<CONFIG:RelWithDebInfo>:-O3>")
add_compile_options("$<$<CONFIG:Release>:-O3>")
add_compile_options("$<$<CONFIG:Release>:-g0>")
add_compile_options("$<$<CONFIG:Debug>:-O0>")
foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG)
if(${flag_var} MATCHES "-O3")
string(REGEX REPLACE "-O3" "-O0" ${flag_var} "${${flag_var}}")
endif()
if(${flag_var} MATCHES "-O2")
string(REGEX REPLACE "-O2" "-O0" ${flag_var} "${${flag_var}}")
endif()
endforeach()
else()
# see https://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx for details
# /ZI = include debug info
# /Wall = all warnings
add_compile_options("$<$<CONFIG:RelWithDebInfo>:/Ox>")
add_compile_options("$<$<CONFIG:RelWithDebInfo>:/ZI>")
add_compile_options("$<$<CONFIG:Release>:/Ox>")
add_compile_options("$<$<CONFIG:Release>:/DNDEBUG>")
add_compile_options("$<$<CONFIG:Release>:/ZI>")
add_compile_options("$<$<CONFIG:Debug>:/Od>")
# buffers security check
add_compile_options(/GS)
# Proper warning level
add_compile_options(/W1)
# Disable STL used in DLL-boundary warning
add_compile_options(/wd4251)
add_compile_options(/D_CRT_SECURE_NO_WARNINGS)
# Wall is MSVC's Weverything, so annoying unless used from the start
# and with judiciously used warning disables
# add_compile_options(/Wall)
# /Za = only ansi C98 & C++11
# /Za is not recommended for use, not tested, etc.
# see: http://stackoverflow.com/questions/5489326/za-compiler-directive-does-not-compile-system-headers-in-vs2010
# add_compile_options(/Za)
add_compile_options(/fp:precise)
# exception handling. s = The exception-handling model that catches C++ exceptions only and tells the compiler to assume that functions declared as extern "C" may throw an exception.
# exception handling. c = If used with s (/EHsc), catches C++ exceptions only and tells the compiler to assume that functions declared as extern "C" never throw a C++ exception.
add_compile_options(/EHsc)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /INCREMENTAL:NO")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /PDBCOMPRESS")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
set(DEF_INSTALL_CMAKE_DIR CMake)
endif()
option(ENABLE_ASSERTIONS "Build with assertions enabled" ON)
message(STATUS "build type is ${CMAKE_BUILD_TYPE}")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(ENABLE_ASSERTIONS OFF)
endif()
if(ENABLE_ASSERTIONS)
# NDEBUG was already removed.
else()
# Note this definition doesn't appear in the cache variables.
add_compile_definitions(NDEBUG)
add_cxx_flag_if_supported("-fno-stack-protector")
add_compile_definitions(_FORTIFY_SOURCE=0)
endif()
# -----------------------------------------------------------------------------
# Flags
# -----------------------------------------------------------------------------
if(NOT WIN32)
if(NOT ENABLE_TESTING AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
#add_cxx_flag_if_supported("-fvisibility=hidden")
endif()
add_cxx_flag_if_supported("-mpopcnt")
add_cxx_flag_if_supported("-msse4.2")
add_cxx_flag_if_supported("-mpclmul")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
# add_cxx_flag_if_supported("-flto")
# add_c_flag_if_supported("-flto")
# add_link_flag_if_supported("-flto")
else()
add_cxx_flag_if_supported("-Wall")
add_cxx_flag_if_supported("-Wextra")
add_cxx_flag_if_supported("-Wunused")
add_cxx_flag_if_supported("-Wsign-compare")
add_cxx_flag_if_supported("-fno-omit-frame-pointer")
add_cxx_flag_if_supported("-ggdb3")
add_cxx_flag_if_supported("-g")
add_cxx_flag_if_supported("-Wtype-limits")
add_cxx_flag_if_supported("-Wuninitialized")
add_cxx_flag_if_supported("-Wstrict-aliasing")
add_cxx_flag_if_supported("-Wpointer-arith")
add_cxx_flag_if_supported("-Wheader-guard")
add_cxx_flag_if_supported("-Wformat-nonliteral")
add_cxx_flag_if_supported("-Winit-self")
add_cxx_flag_if_supported("-Wparentheses")
add_cxx_flag_if_supported("-Wunreachable-code")
add_cxx_flag_if_supported("-Wno-class-memaccess")
add_cxx_flag_if_supported("-Wextra-semi-stmt")
add_cxx_flag_if_supported("-Wnoweak-vtables")
add_cxx_flag_if_supported("-Wdouble-promotion")
add_cxx_flag_if_supported("-Warith-conversion")
endif()
endif()
if(NOT WIN32)
message(STATUS "${CMAKE_C_FLAGS}")
add_c_flag_if_supported("-Wall")
add_c_flag_if_supported("-msse4.2")
add_c_flag_if_supported("-mpclmul")
add_c_flag_if_supported("-mpopcnt")
endif()
option(SANITIZE "Use Clang sanitizers. You MUST use clang++ as the compiler for this to work" OFF)
if(SANITIZE)
message(WARNING " --Using clang sanitizers -- you MUST use clang++ or the compile WILL fail")
add_compile_options("-fsanitize=address")
add_link_options("-fsanitize=address")
add_link_options("-fsanitize=undefined")
# add_compile_options("-fsanitize=integer")
add_compile_options("-fsanitize=undefined")
add_compile_options("-fsanitize=null")
add_compile_options("-fsanitize=alignment")
add_compile_options("-fno-sanitize-recover")
add_compile_options("-fsanitize=return")
add_compile_options("-fsanitize=bounds")
# add_compile_options("-fsanitize=float-divide-by-zero")
# add_compile_options("-fsanitize=integer-divide-by-zero")
# add_compile_options("-fsanitize=unsigned-integer-overflow")
# add_compile_options("-fsanitize=signed-integer-overflow")
add_compile_options("-fsanitize=bool")
add_compile_options("-fsanitize=enum")
add_compile_options("-fsanitize=float-cast-overflow")
add_compile_options("$<$<CONFIG:RelWithDebInfo>:-D_GLIBCXX_ASSERTIONS>")
#add_compile_options("-Weverything")
#add_compile_options("-Wshorten-64-to-32")
#add_compile_options("-Wweak-vtables")
#add_compile_options("-Wsign-conversion")
#add_compile_options("-Wconversion")
endif()
option(ANALYZE "Run Clang static analyzer. You MUST use clang++ as the compiler for this to work" OFF)
if(ANALYZE)
message(WARNING " --Using clang static analyzer -- you MUST use clang++ or the compile WILL fail")
set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=clang-analyzer-*")
set(CMAKE_C_CLANG_TIDY "clang-tidy;-checks=clang-analyzer-*")
endif()
# -----------------------------------------------------------------------------
# Dependencies
# -----------------------------------------------------------------------------
# When building statically, prefer .a over .so for all find_library/find_package calls
if(NOT BUILD_SHARED_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".so" ".dylib")
endif()
find_package(PkgConfig REQUIRED)
if(APPLE)
execute_process(COMMAND brew --prefix OUTPUT_VARIABLE _homebrew_prefix OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
if(_homebrew_prefix)
set(ENV{PKG_CONFIG_PATH} "${_homebrew_prefix}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}")
endif()
endif()
# Ensure /usr/local/lib/pkgconfig is searched (for locally-installed libraries).
if(EXISTS "/usr/local/lib/pkgconfig")
set(ENV{PKG_CONFIG_PATH} "/usr/local/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}")
endif()
# GMP, MPFR, and flint are external pre-built system libraries. On Linux, a
# source-built static .a typically lacks -fPIC and cannot be linked into a shared
# object (.so). Restore the platform-default suffix order (.so first) when building
# the Python extension on Linux so the shared versions are found instead.
# On macOS, Homebrew always builds static libs with PIC so the .a works fine, and
# using the .dylib would drag in a macOS-version-specific deployment target that
# breaks the wheel's declared minimum OS version.
if(BUILD_PYTHON_EXTENSION AND UNIX AND NOT APPLE)
set(_saved_find_suffixes "${CMAKE_FIND_LIBRARY_SUFFIXES}")
unset(CMAKE_FIND_LIBRARY_SUFFIXES)
endif()
pkg_check_modules(GMP REQUIRED IMPORTED_TARGET gmp)
find_library(GMPXX_LIBRARY NAMES gmpxx HINTS ${GMP_LIBRARY_DIRS})
if(GMPXX_LIBRARY)
get_property(_gmp_iface_libs TARGET PkgConfig::GMP PROPERTY INTERFACE_LINK_LIBRARIES)
set_property(TARGET PkgConfig::GMP PROPERTY INTERFACE_LINK_LIBRARIES "${GMPXX_LIBRARY}" ${_gmp_iface_libs})
endif()
pkg_check_modules(MPFR REQUIRED IMPORTED_TARGET mpfr)
pkg_check_modules(flint REQUIRED IMPORTED_TARGET flint)
if(BUILD_PYTHON_EXTENSION AND UNIX AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES "${_saved_find_suffixes}")
endif()
option(NOZLIB "Don't use zlib" OFF)
if(NOT NOZLIB AND NOT (NOT BUILD_SHARED_LIBS AND WIN32))
find_package(ZLIB)
if(ZLIB_FOUND)
message(STATUS "OK, Found ZLIB!")
add_compile_definitions(USE_ZLIB)
else()
message(STATUS "WARNING: Did not find ZLIB, gzipped file support will be disabled")
endif()
endif()
include(FetchContent)
# FetchContent_Populate with declared details is deprecated in CMake 3.30+ (CMP0169).
# We call it intentionally to clone-only without add_subdirectory, so silence the warning.
if(POLICY CMP0169)
cmake_policy(SET CMP0169 OLD)
endif()
# ---- cadical / cadiback (transitive deps of arjun/cryptominisat) ----
# These are not used directly by ganak, but arjun's exported target lists
# them in its INTERFACE_LINK_LIBRARIES. Import them here so CMake resolves
# them to actual library paths rather than bare -l flags.
set(cadical_DIR "" CACHE PATH "cadical build dir containing cadicalConfig.cmake")
if(cadical_DIR AND NOT TARGET cadical)
find_package(cadical CONFIG REQUIRED HINTS "${cadical_DIR}")
message(STATUS "cadical: using pre-built at ${cadical_DIR}")
endif()
set(cadiback_DIR "" CACHE PATH "cadiback build dir containing cadibackConfig.cmake")
if(cadiback_DIR AND NOT TARGET cadiback)
find_package(cadiback CONFIG REQUIRED HINTS "${cadiback_DIR}")
message(STATUS "cadiback: using pre-built at ${cadiback_DIR}")
endif()
# ---- CryptoMiniSat5 ----
set(cryptominisat5_DIR "" CACHE PATH "cryptominisat5 install/build prefix (contains lib/cmake/cryptominisat5/). Auto-resolved if empty.")
if(NOT TARGET cryptominisat5)
if(cryptominisat5_DIR)
find_package(cryptominisat5 CONFIG REQUIRED HINTS "${cryptominisat5_DIR}")
message(STATUS "CryptoMiniSat5: using pre-built at ${cryptominisat5_DIR}")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../cryptominisat/CMakeLists.txt")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../cryptominisat"
"${CMAKE_CURRENT_BINARY_DIR}/cryptominisat-build")
message(STATUS "CryptoMiniSat5: using sibling repo")
else()
FetchContent_Declare(cryptominisat5
GIT_REPOSITORY https://github.com/msoos/cryptominisat
GIT_TAG master
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(cryptominisat5)
message(STATUS "CryptoMiniSat5: fetched from GitHub")
endif()
endif()
# ---- Arjun ----
set(arjun_DIR "" CACHE PATH "arjun install/build prefix (contains lib/cmake/arjun/). Auto-resolved if empty.")
if(NOT TARGET arjun)
if(arjun_DIR)
find_package(arjun CONFIG REQUIRED HINTS "${arjun_DIR}")
message(STATUS "Arjun: using pre-built at ${arjun_DIR}")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../arjun/CMakeLists.txt")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../arjun"
"${CMAKE_CURRENT_BINARY_DIR}/arjun-build")
message(STATUS "Arjun: using sibling repo")
else()
FetchContent_Declare(arjun
GIT_REPOSITORY https://github.com/meelgroup/arjun.git
GIT_TAG master
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(arjun)
message(STATUS "Arjun: fetched from GitHub")
endif()
endif()
# ---- ApproxMC ----
set(approxmc_DIR "" CACHE PATH "approxmc install/build prefix (contains lib/cmake/approxmc/). Auto-resolved if empty.")
if(NOT TARGET approxmc)
if(approxmc_DIR)
find_package(approxmc CONFIG REQUIRED HINTS "${approxmc_DIR}")
message(STATUS "ApproxMC: using pre-built at ${approxmc_DIR}")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../approxmc/CMakeLists.txt")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../approxmc"
"${CMAKE_CURRENT_BINARY_DIR}/approxmc-build")
message(STATUS "ApproxMC: using sibling repo")
else()
FetchContent_Declare(approxmc
GIT_REPOSITORY https://github.com/meelgroup/ApproxMC.git
GIT_TAG master
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(approxmc)
message(STATUS "ApproxMC: fetched from GitHub")
endif()
endif()
# ---- TreeDecomp ----
# Used directly by ganak (counter.hpp / outer_counter.cpp include
# <treedecomp/TreeDecomposition.hpp>), so its imported target must be
# available to propagate INTERFACE_INCLUDE_DIRECTORIES.
set(treedecomp_DIR "" CACHE PATH "treedecomp install/build prefix (contains treedecompConfig.cmake). Auto-resolved if empty.")
if(NOT TARGET treedecomp)
if(treedecomp_DIR)
find_package(treedecomp CONFIG REQUIRED HINTS "${treedecomp_DIR}")
message(STATUS "TreeDecomp: using pre-built at ${treedecomp_DIR}")
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../treedecomp/CMakeLists.txt")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../treedecomp"
"${CMAKE_CURRENT_BINARY_DIR}/treedecomp-build")
message(STATUS "TreeDecomp: using sibling repo")
else()
FetchContent_Declare(treedecomp
GIT_REPOSITORY https://github.com/meelgroup/treedecomp.git
GIT_TAG main
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(treedecomp)
message(STATUS "TreeDecomp: fetched from GitHub")
endif()
endif()
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
# Collect all compile definitions set so far into a string for embedding
# via @COMPILE_DEFINES@ in GitSHA1.cpp.in
get_directory_property(DirDefs COMPILE_DEFINITIONS)
set(COMPILE_DEFINES "")
foreach(d ${DirDefs})
set(COMPILE_DEFINES "${COMPILE_DEFINES} -D${d}")
endforeach()
message(STATUS "All defines at startup: ${COMPILE_DEFINES}")
macro(ganak_add_public_header LIBTARGET HEADER)
get_target_property(EXISTING_PUBLIC_HEADERS ${LIBTARGET} PUBLIC_HEADER)
if(EXISTING_PUBLIC_HEADERS)
list(APPEND EXISTING_PUBLIC_HEADERS "${HEADER}")
else()
# Do not append to empty list
set(EXISTING_PUBLIC_HEADERS "${HEADER}")
endif()
set_target_properties(
${LIBTARGET}
PROPERTIES
PUBLIC_HEADER "${EXISTING_PUBLIC_HEADERS}"
)
endmacro()
set(DEF_INSTALL_CMAKE_DIR lib/cmake/ganak)
set(GANAK_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
"Installation directory for ganak CMake files")
# -----------------------------------------------------------------------------
# Add GIT version
# -----------------------------------------------------------------------------
function(SetVersionNumber PREFIX VERSION_MAJOR VERSION_MINOR VERSION_PATCH)
set(${PREFIX}_VERSION_MAJOR ${VERSION_MAJOR} PARENT_SCOPE)
set(${PREFIX}_VERSION_MINOR ${VERSION_MINOR} PARENT_SCOPE)
set(${PREFIX}_VERSION_PATCH ${VERSION_PATCH} PARENT_SCOPE)
set(${PREFIX}_VERSION
"${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
PARENT_SCOPE)
endfunction()
find_program(GIT_EXECUTABLE git)
if(GIT_EXECUTABLE)
include(GetGitRevisionDescription)
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
message(STATUS "GIT hash found: ${GIT_SHA1}")
else()
set(GIT_SHA1 "GIT-hash-notfound")
endif()
file(READ "${CMAKE_SOURCE_DIR}/pyproject.toml" PYPROJECT_TOML_CONTENTS)
string(REGEX MATCH "version = \"([0-9]+\\.[0-9]+\\.[0-9]+)\"" _ ${PYPROJECT_TOML_CONTENTS})
set(GANAK_FULL_VERSION "${CMAKE_MATCH_1}")
message(STATUS "Version read from pyproject.toml: ${GANAK_FULL_VERSION}")
string(REPLACE "." ";" GANAK_FULL_VERSION_LIST ${GANAK_FULL_VERSION})
SetVersionNumber("PROJECT" ${GANAK_FULL_VERSION_LIST})
message(STATUS "PROJECT_VERSION: ${PROJECT_VERSION}")
message(STATUS "PROJECT_VERSION_MAJOR: ${PROJECT_VERSION_MAJOR}")
message(STATUS "PROJECT_VERSION_MINOR: ${PROJECT_VERSION_MINOR}")
message(STATUS "PROJECT_VERSION_PATCH: ${PROJECT_VERSION_PATCH}")
# -----------------------------------------------------------------------------
# Provide an export name to be used by targets that wish to export themselves.
# -----------------------------------------------------------------------------
set(GANAK_EXPORT_NAME "ganakTargets")
add_subdirectory(src)
# -----------------------------------------------------------------------------
# Add uninstall target for makefiles
# -----------------------------------------------------------------------------
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY
)
add_custom_target(uninstall_ganak
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
option(ENABLE_TESTING "Enables testing" ON)
if(ENABLE_TESTING)
find_package(Python3 COMPONENTS NumPy Interpreter Development REQUIRED)
message(STATUS "Python 3 -- Python3_EXECUTABLE=${Python3_EXECUTABLE}")
message(STATUS "Python 3 -- Python3_LIBRARIES=${Python3_LIBRARIES}")
message(STATUS "Python 3 -- Python3_INCLUDE_DIRS=${Python3_INCLUDE_DIRS}")
message(STATUS "Python 3 -- Python3_VERSION=${Python3_VERSION}")
message(STATUS "Python 3 -- Python3_NumPy_INCLUDE_DIRS=${Python3_NumPy_INCLUDE_DIRS}")
message(STATUS "Python 3 -- Python3_NumPy_VERSION=${Python3_NumPy_VERSION}")
enable_testing()
add_subdirectory(tests/cnf-files)
# Property-based d-DNNF tests: generate random CNFs, brute-force the true
# count/model set as an oracle, and check the emitted circuit against it.
# Seeded so a regression reproduces identically; they self-check against the
# oracle, so there is no golden file to break when the circuit shape changes.
# Small/few by design: the oracle is 2^nv and each case spawns ganak, so we
# keep these quick for `make test`. For deep fuzzing run them by hand with a
# larger count / --maxvars (see CLAUDE.md).
add_test(NAME ddnnf_compile_fuzz
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/ddnnf_fuzz.py 40 --seed 1 --minvars 6 --maxvars 10)
endif()
# -----------------------------------------------------------------------------
# Export our targets so that other CMake based projects can interface with
# the build of ganak in the build-tree
# -----------------------------------------------------------------------------
set(GANAK_TARGETS_FILENAME "ganakTargets.cmake")
set(GANAK_CONFIG_FILENAME "ganakConfig.cmake")
# Export targets
set(MY_TARGETS ganak)
export(
TARGETS ${MY_TARGETS}
FILE "${CMAKE_CURRENT_BINARY_DIR}/${GANAK_TARGETS_FILENAME}"
)
# Build-tree config (absolute paths, for use without installation)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/ganakConfig-buildtree.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${GANAK_CONFIG_FILENAME}"
@ONLY
)
# Export this package to the CMake user package registry
# Now the user can just use find_package(ganak) on their system
export(PACKAGE ganak)
# Install-tree config and version files (relocatable via CMakePackageConfigHelpers)
set(GANAK_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
set(GANAK_VERSION_FILENAME "ganakConfigVersion.cmake")
include(CMakePackageConfigHelpers)
configure_package_config_file(ganakConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/${GANAK_CONFIG_FILENAME}"
INSTALL_DESTINATION "${GANAK_INSTALL_CMAKE_DIR}"
PATH_VARS GANAK_INSTALL_INCLUDEDIR
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${GANAK_VERSION_FILENAME}"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/${GANAK_CONFIG_FILENAME}"
"${CMAKE_CURRENT_BINARY_DIR}/${GANAK_VERSION_FILENAME}"
DESTINATION "${GANAK_INSTALL_CMAKE_DIR}"
)
# Install the export set for use with the install-tree
install(
EXPORT ${GANAK_EXPORT_NAME}
DESTINATION "${GANAK_INSTALL_CMAKE_DIR}"
)