Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
855 lines (745 loc) · 35 KB
/
Copy pathCMakeLists.txt
File metadata and controls
855 lines (745 loc) · 35 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
cmake_minimum_required(VERSION 3.19)
cmake_policy(SET CMP0011 NEW)
cmake_policy(SET CMP0025 NEW)
cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0067 NEW)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
### Add defaults for cmake
# These defaults need to be included before the project() call.
include(DefineCMakeDefaults)
option(DT_MACOS_UNIVERSAL "Build universal macOS binaries" OFF)
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND DT_MACOS_UNIVERSAL)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "Build universal Apple binaries" FORCE)
endif()
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not permitted. Make a separate folder for building:\nmkdir build; cd build; cmake ..\nBefore that, remove the files already created:\nrm -rf CMakeCache.txt CMakeFiles")
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
if(DEFINED PROJECT_VERSION)
project(ansel VERSION ${PROJECT_VERSION} LANGUAGES CXX C)
else()
# Actual version string will be generated from git later
project(ansel VERSION 0 LANGUAGES CXX C)
endif()
set(CMAKE_C_STANDARD 11)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.30)
# CMake parallelizes installation per subdirectory; install-time ordering
# dependencies therefore need to stay in the subdirectory that consumes them.
set_property(GLOBAL PROPERTY INSTALL_PARALLEL TRUE)
endif()
# Shell to use for scripts (prefer bash for bash-specific scripts)
if(WIN32)
if(CMAKE_SH)
set(ANSEL_SH_EXECUTABLE "${CMAKE_SH}")
else()
find_program(ANSEL_SH_EXECUTABLE sh)
endif()
else()
find_program(ANSEL_SH_EXECUTABLE bash)
if(NOT ANSEL_SH_EXECUTABLE)
set(ANSEL_SH_EXECUTABLE "/bin/sh")
endif()
endif()
# Generated files in the build tree (shared across subdirectories)
set(ANSEL_AUTHORS_FILE "${CMAKE_BINARY_DIR}/AUTHORS")
# Ensure AUTHORS exists at install time (fallbacks to static AUTHORS if needed)
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/cmake")
file(GENERATE
OUTPUT "${CMAKE_BINARY_DIR}/cmake/install_generate_authors.cmake"
CONTENT
"execute_process(
COMMAND \"${CMAKE_COMMAND}\"
\"-DANSEL_SOURCE_DIR=${CMAKE_SOURCE_DIR}\"
\"-DANSEL_AUTHORS_FILE=${ANSEL_AUTHORS_FILE}\"
\"-DANSEL_SH_EXECUTABLE=${ANSEL_SH_EXECUTABLE}\"
\"-DANSEL_BASE=release-3.9.0\"
\"-DANSEL_HEAD=HEAD\"
\"-P\"
\"${CMAKE_SOURCE_DIR}/tools/generate-authors.cmake\"
)
"
)
# Allow forcing the C/CPP compiler that is actually used during the compilation
# to something other than what is used by the cmake run. This is useful when
# the compiler for some reason breaks the initial cmake checks but works fine
# for actually compiling ansel. This allows building ansel using
# afl-clang-fast achieving a >4x speedup in fuzzing.
if(DEFINED DT_FORCE_C_COMPILER)
set(CMAKE_C_COMPILER ${DT_FORCE_C_COMPILER})
endif()
if(DEFINED DT_FORCE_CXX_COMPILER)
set(CMAKE_CXX_COMPILER ${DT_FORCE_CXX_COMPILER})
endif()
include(DefineOptions.cmake)
# Include GNUInstallDirs, which sets sensible defaults for install directories.
# See https://cmake.org/cmake/help/v3.0/module/GNUInstallDirs.html for further information.
# These values can be easily overridden if required.
# Some defaults are set for OpenBSD as well (info and man pages).
include(GNUInstallDirs)
include(FeatureSummary)
# Those are used to allow to copy program to other dir after installing. To
# archieve this, 2 requirements are needed:
#
# 1. Binaries and libraries must be relocatable. We achieve this by using RPATH.
# 2. Program must load libraries and resources relativly from where it is, so
# with Requirement 1 we could copy the installed files anywhere to run, just
# keep the strucrure. That's why we need such REL_* here.
#
# We will use REL_* in program to load files relatively, that is done by C code.
# XXX(AZ): Some people may set `CMAKE_INSTALL_BINDIR` to absolute path, we
# should warn that this is not supported.
get_filename_component(CMAKE_INSTALL_BINDIR_ABS "${CMAKE_INSTALL_FULL_BINDIR}" REALPATH "${CMAKE_INSTALL_PREFIX}")
# Get relative dir name from system install dirs.
file(RELATIVE_PATH REL_BIN_TO_LIBDIR ${CMAKE_INSTALL_FULL_BINDIR} "${CMAKE_INSTALL_FULL_LIBDIR}")
file(RELATIVE_PATH REL_BIN_TO_SHAREDIR ${CMAKE_INSTALL_FULL_BINDIR} "${CMAKE_INSTALL_FULL_DATADIR}")
file(RELATIVE_PATH REL_BIN_TO_LOCALEDIR ${CMAKE_INSTALL_FULL_BINDIR} "${CMAKE_INSTALL_FULL_LOCALEDIR}")
# MODULEDIR is where we put all libraries that will be loaded dynamically at
# runtime, so it is `${LIBDIR}/ansel`.
set(REL_BIN_TO_MODULEDIR "${REL_BIN_TO_LIBDIR}/ansel")
set(REL_BIN_TO_DATADIR "${REL_BIN_TO_SHAREDIR}/ansel")
# https://medium.com/@alasher/colored-c-compiler-output-with-ninja-clang-gcc-10bfe7f2b949
if(FORCE_COLORED_OUTPUT)
# Vendored dependencies may enable ASM with a compiler different from the
# C/C++ compiler. Keep these compiler-specific flags off ASM command lines.
add_compile_options(
"$<$<COMPILE_LANG_AND_ID:C,GNU>:-fdiagnostics-color=always>"
"$<$<COMPILE_LANG_AND_ID:CXX,GNU>:-fdiagnostics-color=always>"
"$<$<COMPILE_LANG_AND_ID:C,Clang>:-fcolor-diagnostics>"
"$<$<COMPILE_LANG_AND_ID:CXX,Clang>:-fcolor-diagnostics>"
)
endif()
# Unify the RPATH origin.
# Those 2 vars need to be TRUE to let CMake obey the INSTALL_RPATH we set.
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
set(RPATH_ORIGIN @loader_path)
else()
set(RPATH_ORIGIN $ORIGIN)
endif()
if(USE_OPENMP)
find_package(OpenMP 4.5 REQUIRED)
if(OpenMP_C_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
endif()
if(OpenMP_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
endif(USE_OPENMP)
# Check for base threading library
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
# Fail if returned library is not pthread
if(NOT CMAKE_USE_PTHREADS_INIT)
message(FATAL_ERROR "POSIX threads: not found")
endif()
# liblensfun: optional, and neither a build nor a runtime requirement of the application.
#
# It used to be a hard requirement, because data/CMakeLists.txt converted upstream's
# calibration XML into lenses.db here, at configure time, and the importer needs lensfun to
# read that XML. The conversion has moved to LensSerious' own CI, which publishes the
# database with its hashes; a build fetches a pinned file and converts nothing. So nothing
# in an ordinary build needs lensfun any more -- not the pixel pipeline, which never linked
# it, and no longer the data either.
#
# What still uses it is ansel-lens-db-update, the command that rebuilds the database on a
# user's machine against THEIR lensfun profiles -- the ones a package build could not have
# seen. src/CMakeLists.txt builds it only where LensSerious found lensfun, so its absence
# costs that one command and nothing else.
#
# Probed with its own prefix and without IMPORTED_TARGET: src/external/LensSerious runs its
# own pkg_check_modules(LENSFUN IMPORTED_TARGET lensfun), and creating PkgConfig::LENSFUN
# twice is a configure error in itself.
find_package(PkgConfig REQUIRED)
pkg_check_modules(LENSFUN_PROBE lensfun)
if(NOT LENSFUN_PROBE_FOUND)
message(STATUS
"liblensfun not found: this build will not carry ansel-lens-db-update, so users "
"of it cannot fold their own lens profiles into the shipped database. The "
"database itself is unaffected -- it is fetched, not built here. Install "
"liblensfun-dev / lensfun-devel to get the command back.\n"
"Note that lensfun.pc has `Requires: glib-2.0', so a missing libglib2.0-dev makes "
"this probe fail without ever mentioning lensfun itself.")
endif()
include(ConfigureChecks.cmake)
include(CheckCCompilerFlag)
# Check if this is source package build
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git)
set(SOURCE_PACKAGE 1)
else()
set(SOURCE_PACKAGE 0)
endif()
#
# Set build flags
#
if(WIN32 AND (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
set(ANSEL_WIN32_CLANG ON)
# Force lld on Windows/Clang for all build modes.
string(APPEND CMAKE_EXE_LINKER_FLAGS " -fuse-ld=lld")
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -fuse-ld=lld")
string(APPEND CMAKE_MODULE_LINKER_FLAGS " -fuse-ld=lld")
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
)
list(REMOVE_DUPLICATES CMAKE_TRY_COMPILE_PLATFORM_VARIABLES)
endif()
# This enables LTO (`-flto`): https://gcc.gnu.org/onlinedocs/gccint/LTO-Overview.html
# We keep it target-scoped and selectively disable it for known incompatible files.
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "IPO/LTO disabled for Debug configuration")
set(ANSEL_IPO_SUPPORTED OFF)
elseif(WIN32 AND CMAKE_C_COMPILER_ID MATCHES "GNU")
# On MinGW/Windows, GNU ld does not support LTO at all, so we disable it on that platform.
message(STATUS "IPO/LTO disabled for MinGW/Windows with GNU toolchain")
set(ANSEL_IPO_SUPPORTED OFF)
else()
if(ANSEL_WIN32_CLANG)
# On MinGW/Windows, Clang's default linker is ld.bfd which requires LLVMgold.dll for LTO,
# but that plugin does not load on Windows. So we force lld which supports LTO natively.
# This also fixes check_ipo_supported()'s try_compile probe which uses the same linker.
# Scope CMP0137 to this probe so try_compile() honors platform variables
# (including our linker flags) during check_ipo_supported(), without
# changing policy behavior for the rest of the project.
if(POLICY CMP0137)
cmake_policy(PUSH)
cmake_policy(SET CMP0137 NEW)
endif()
endif()
include(CheckIPOSupported)
check_ipo_supported(RESULT ANSEL_IPO_SUPPORTED OUTPUT ANSEL_IPO_ERROR)
# Reset
if(ANSEL_WIN32_CLANG AND POLICY CMP0137)
cmake_policy(POP)
endif()
if(ANSEL_IPO_SUPPORTED)
message(STATUS "IPO/LTO enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
set(CMAKE_C_FLAGS_IPO "-flto=auto")
set(CMAKE_CXX_FLAGS_IPO "-flto=auto")
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
set(CMAKE_C_FLAGS_IPO "${CMAKE_C_FLAGS_IPO} -fuse-linker-plugin")
set(CMAKE_CXX_FLAGS_IPO "${CMAKE_CXX_FLAGS_IPO} -fuse-linker-plugin")
endif()
else()
message(STATUS "IPO/LTO not supported: ${ANSEL_IPO_ERROR}")
endif()
endif()
## Override default CMake build flags
# Release flags
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG ${CMAKE_C_FLAGS_IPO}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG ${CMAKE_CXX_FLAGS_IPO}" CACHE STRING "" FORCE)
# RelWithDebInfo flags
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -g3 -ggdb3 -DNDEBUG ${CMAKE_C_FLAGS_IPO}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g3 -ggdb3 -DNDEBUG ${CMAKE_CXX_FLAGS_IPO}" CACHE STRING "" FORCE)
# Debug flags
#
# -Wno-error=thread-safety-analysis is a RATCHET, not a setting, and it lives here rather
# than in cmake/compiler-warnings.cmake because a -Wno-error= must follow the -Werror it
# relaxes on the command line -- and CMAKE_<LANG>_FLAGS_DEBUG is appended after
# CMAKE_<LANG>_FLAGS, so anything added there is overridden by the -Werror below.
#
# Making dt_pthread_rwlock_t a clang CAPABILITY -- the prerequisite for GUARDED_BY, and so
# for -Wthread-safety checking anything at all -- surfaced 30 conditional-locking patterns
# clang cannot prove correct, in 5 files. They are real questions rather than obvious bugs;
# see doc/lock-audit.md. Without this they are hard errors on every clang build while every
# GCC build passes silently, GCC ignoring the flag entirely. Keeping them as warnings means
# the findings stay visible instead of being deleted along with the annotations that found
# them. Remove once those files are resolved.
set(_ansel_debug_flags "-O0 -g3 -ggdb3 -D_DEBUG -fno-omit-frame-pointer -Werror -Wuninitialized -Wno-error=format-truncation")
# Clang only. GCC has no thread-safety-analysis warning, and -Wno-error= naming an option it
# does not know is a hard error there -- "cc1plus: error: '-Wno-error=thread-safety-analysis':
# no option" -- not the silent no-op an unknown -Wno-* would be. Adding it unconditionally
# breaks every GCC Debug build.
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
string(APPEND _ansel_debug_flags " -Wno-error=thread-safety-analysis")
endif()
set(CMAKE_C_FLAGS_DEBUG "${_ansel_debug_flags}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "${_ansel_debug_flags}" CACHE STRING "" FORCE)
if(NOT CUSTOM_CFLAGS)
include(march-mtune)
# -------------------------------------------------------------------------
# Base flags (shared across all build types)
# -------------------------------------------------------------------------
# The code uses NAN as a special value meaning "undefined"
# in several places. There is no reason to do so, just use binary flags.
# TODO: remove the use of NAN in code as a special "undefined" flag for float values
# TODO: no looping over pixels in place, aka input ptr = output ptr
set(DT_BASE_C_FLAGS "${MARCH} -Wall -funroll-loops -ftree-vectorize -fno-strict-aliasing")
set(DT_BASE_CXX_FLAGS "${MARCH} -Wall -funroll-loops -ftree-vectorize -fno-strict-aliasing")
# -------------------------------------------------------------------------
# Build-type-specific flags
# -------------------------------------------------------------------------
# Release
set(DT_RELEASE_FLAGS "-ffast-math -ffp-contract=fast -fno-finite-math-only")
# RelWithDebInfo (same perf as release, but no dangerous float arithmetics)
set(DT_RELWITHDEBINFO_FLAGS "-ffast-math -ffp-contract=fast -fno-finite-math-only")
# Debug
set(DT_DEBUG_FLAGS "")
# -------------------------------------------------------------------------
# Compiler-specific tuning
# -------------------------------------------------------------------------
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
set(DT_BASE_C_FLAGS "${DT_BASE_C_FLAGS} -Wmaybe-uninitialized -Wno-undef -Wimplicit-function-declaration -Winfinite-recursion -fno-semantic-interposition -fno-signaling-nans -floop-nest-optimize -ftree-loop-im -fvariable-expansion-in-unroller -ftree-loop-im -fira-loop-pressure")
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(DT_BASE_C_FLAGS "${DT_BASE_C_FLAGS} -fvectorize -fslp-vectorize -ftree-slp-vectorize")
endif()
# -------------------------------------------------------------------------
# Apply to C flags
# -------------------------------------------------------------------------
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${DT_RELEASE_FLAGS}")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${DT_RELWITHDEBINFO_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${DT_DEBUG_FLAGS}")
set(CMAKE_C_FLAGS "${DT_BASE_C_FLAGS} ${CMAKE_C_FLAGS}")
# -------------------------------------------------------------------------
# Again to C++ flags
# -------------------------------------------------------------------------
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${DT_RELEASE_FLAGS}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${DT_RELWITHDEBINFO_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${DT_DEBUG_FLAGS}")
set(CMAKE_CXX_FLAGS "${DT_BASE_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
endif(NOT CUSTOM_CFLAGS)
# ---------------------------------------------------------------------------
# Final aggregation
# ---------------------------------------------------------------------------
string(TOUPPER "${CMAKE_BUILD_TYPE}" DT_BUILD_TYPE_UPPER)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${DT_BUILD_TYPE_UPPER}}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${DT_BUILD_TYPE_UPPER}}")
message(STATUS "CMAKE_C_FLAGS = ${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS = ${CMAKE_CXX_FLAGS}")
if(BINARY_PACKAGE_BUILD)
set(DT_BUILD_CPU_MODE "generic package")
else()
set(DT_BUILD_CPU_MODE "native/local")
endif()
#
# Set platform defaults...
#
if(APPLE)
message("-- Mac OS X build detected, setting default features")
message("-- Apple target architectures: ${CMAKE_OSX_ARCHITECTURES}")
if(HAVE_APPLE_X86_TARGET_CLONES)
message("-- Apple x86_64 target_clones enabled")
add_compile_definitions(DT_APPLE_X86_TARGET_CLONES=1)
else()
message("-- Apple x86_64 target_clones disabled")
endif()
find_program(HOMEBREW_EXISTS "brew")
if(HOMEBREW_EXISTS)
execute_process(COMMAND brew --prefix OUTPUT_VARIABLE HOMEBREW_PREFIX)
string(STRIP "${HOMEBREW_PREFIX}" HOMEBREW_PREFIX)
message("-- Homebrew detected, setting CMAKE_PREFIX_PATH to ${HOMEBREW_PREFIX}")
list(APPEND CMAKE_PREFIX_PATH ${HOMEBREW_PREFIX})
# Explicitly add llvm, icu4c and libsoup@2 to prefix path
list(APPEND CMAKE_PREFIX_PATH ${HOMEBREW_PREFIX}/opt/llvm)
list(APPEND CMAKE_PREFIX_PATH ${HOMEBREW_PREFIX}/opt/icu4c)
list(APPEND CMAKE_PREFIX_PATH ${HOMEBREW_PREFIX}/opt/libsoup@2)
else()
message("-- Setting CMAKE_PREFIX_PATH to prefer MacPorts and/or user-installed libraries over system ones")
list(APPEND CMAKE_PREFIX_PATH /opt/local /usr/local)
endif()
set(CMAKE_FIND_FRAMEWORK "LAST")
# except libstdc++ (only one linked via -l flag, not full path)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/lib")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L/usr/lib")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/usr/lib")
add_definitions("-DOS_OBJECT_USE_OBJC=0")
endif(APPLE)
include(compiler-versions)
if(WIN32)
message("-- Win32 build detected, setting default features")
set(CMAKE_FIND_LIBRARY_SUFFIXES .dll ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(USE_COLORD OFF)
set(BUILD_CMSTEST OFF)
set(BUILD_PRINT OFF)
set(TESTBUILD_OPENCL_PROGRAMS OFF)
if(BUILD_MSYS2_INSTALL)
add_definitions(-DMSYS2_INSTALL)
endif()
endif(WIN32)
#
# Set package version
#
# The src/ subdirectory in build dir won't exist yet.
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src)
# adds custom command to generate header containing version info.
# takes 1 optional parameter - version override.
function(generate_version_gen_c)
if(ARGC EQUAL 2)
# if a version override was specified, use it
set(_VERSION "${ARGV0}")
set(_TYPE "${ARGV1}")
else()
# else, the tool will autodetect the version
set(_VERSION "")
set(_TYPE "git checkout")
endif()
add_custom_target(
create_version_gen ALL
COMMAND sh ${CMAKE_SOURCE_DIR}/tools/create_version_c.sh ${CMAKE_BINARY_DIR}/src/version_gen.c ${_VERSION}
DEPENDS ${CMAKE_SOURCE_DIR}/tools/create_version_c.sh
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Updating version string (${_TYPE})"
VERBATIM # else might break when export-subst was needed but did not happen
)
endfunction(generate_version_gen_c)
if(DEFINED PROJECT_VERSION AND PROJECT_VERSION VERSION_GREATER 0)
# project version is defined by -D on the cmake command line
# only use that value, do not update it at make time
generate_version_gen_c(${PROJECT_VERSION} "version override")
else(DEFINED PROJECT_VERSION AND PROJECT_VERSION VERSION_GREATER 0)
if(NOT SOURCE_PACKAGE) # i.e., a git checkout
# this part is setting the corresponding CMake variable which gets used for example when creating a source package
execute_process(
COMMAND sh ${CMAKE_SOURCE_DIR}/tools/get_git_version_string.sh OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE PROJECT_VERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
# FIXME: PROJECT_VERSION will not be updated automatically, until you rerun cmake
generate_version_gen_c()
else(NOT SOURCE_PACKAGE)
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/src/version_gen.c)
# should be expanded by git archive due to export-subst in .gitattributes
set(PROJECT_VERSION "archive-$Format:%H$")
# but was it expanded?
if(PROJECT_VERSION MATCHES Format)
set(PROJECT_VERSION "unknown-version")
endif(PROJECT_VERSION MATCHES Format)
generate_version_gen_c(${PROJECT_VERSION} "source package")
else(NOT EXISTS ${CMAKE_SOURCE_DIR}/src/version_gen.c)
# no need to create version_gen.c if it's already shipped. that is for example the case with our release tarballs
execute_process(
COMMAND sh ${CMAKE_SOURCE_DIR}/tools/parse_version_c.sh ${CMAKE_SOURCE_DIR}/src/version_gen.c OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE PROJECT_VERSION
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
# FIXME: (irrelevant) PROJECT_VERSION will not be updated automatically, until you rerun cmake
# but generate_version target expects it to be in build dir, so we need to copy it
add_custom_target(
create_version_gen ALL
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/src/version_gen.c ${CMAKE_BINARY_DIR}/src/version_gen.c
DEPENDS ${CMAKE_SOURCE_DIR}/src/version_gen.c
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Updating version string (source package) - ${PROJECT_VERSION}"
)
endif(NOT EXISTS ${CMAKE_SOURCE_DIR}/src/version_gen.c)
endif(NOT SOURCE_PACKAGE)
endif(DEFINED PROJECT_VERSION AND PROJECT_VERSION VERSION_GREATER 0)
# needed to make sure that version string is actually updated.
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/src/version_gen.c
COMMAND ${CMAKE_COMMAND} -E echo
DEPENDS create_version_gen
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
# WARNING: no target should reference version_gen.c directly. instead, they should add_dependencies(yourtarget generate_version)
add_custom_target(
generate_version ALL
DEPENDS ${CMAKE_BINARY_DIR}/src/version_gen.c
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
# Add a sensible build type default and warning because empty means no optimization and no debug info.
if(NOT CMAKE_BUILD_TYPE)
message("WARNING: CMAKE_BUILD_TYPE is not defined!\n Defaulting to CMAKE_BUILD_TYPE=RelWithDebInfo. Use ccmake to set a proper value.")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif(NOT CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE MATCHES "^[Dd][Ee][Bb][Uu][Gg]$" AND SOURCE_PACKAGE)
message(FATAL_ERROR "ERROR: Debug build type most likely isn't what you want, use RelWithDebInfo instead. If you're absolutely sure that this is what you want then just comment out this line.")
endif()
include(compiler-warnings)
include(windows-macros)
# we need some external programs for building ansel
message(STATUS "Looking for external programs")
set(EXTERNAL_PROGRAMS_FOUND 1)
# we need perl for introspection
find_program(perl_BIN perl)
if(${perl_BIN} STREQUAL "perl_BIN-NOTFOUND")
message(STATUS "Missing perl")
set(EXTERNAL_PROGRAMS_FOUND 0)
else(${perl_BIN} STREQUAL "perl_BIN-NOTFOUND")
message(STATUS "Found perl")
endif(${perl_BIN} STREQUAL "perl_BIN-NOTFOUND")
# we need intltool-merge for ansel.desktop
find_program(intltool_merge_BIN intltool-merge)
if(${intltool_merge_BIN} STREQUAL "intltool_merge_BIN-NOTFOUND")
message(STATUS "Missing intltool-merge")
set(EXTERNAL_PROGRAMS_FOUND 0)
else(${intltool_merge_BIN} STREQUAL "intltool_merge_BIN-NOTFOUND")
message(STATUS "Found intltool-merge")
endif(${intltool_merge_BIN} STREQUAL "intltool_merge_BIN-NOTFOUND")
# we need desktop-file-validate to check ansel.desktop
find_program(desktop_file_validate_BIN desktop-file-validate)
if(${desktop_file_validate_BIN} STREQUAL "desktop_file_validate_BIN-NOTFOUND")
message(STATUS "Missing desktop-file-validate, problems in ansel.desktop might go unnoticed")
set(VALIDATE_DESKTOP_FILE 0)
else(${desktop_file_validate_BIN} STREQUAL "desktop_file_validate_BIN-NOTFOUND")
message(STATUS "Found desktop-file-validate")
set(VALIDATE_DESKTOP_FILE 1)
endif(${desktop_file_validate_BIN} STREQUAL "desktop_file_validate_BIN-NOTFOUND")
# we need appstream-util to check ansel.appdata.xml
if(VALIDATE_APPDATA_FILE)
find_program(appstream_util_BIN appstream-util)
if(${appstream_util_BIN} STREQUAL "appstream_util_BIN-NOTFOUND")
message(STATUS "Missing appstream-util, problems in ansel.appdata.xml might go unnoticed")
set(VALIDATE_APPDATA_FILE OFF)
else(${appstream_util_BIN} STREQUAL "appstream_util_BIN-NOTFOUND")
message(STATUS "Found appstream-util")
endif(${appstream_util_BIN} STREQUAL "appstream_util_BIN-NOTFOUND")
endif(VALIDATE_APPDATA_FILE)
if(TESTBUILD_OPENCL_PROGRAMS)
set(TESTBUILD_OPENCL_PROGRAMS OFF)
# LLVM's config resolves zstd through the Findzstd.cmake module it ships,
# which declares zstd::libzstd_shared and zstd::libzstd_static only, where
# zstd's own export set carries zstd::libzstd as well. A later config-mode
# find_package(zstd) - OpenEXR 3.5's find_dependency - then refuses an
# export set that is half defined. Resolve zstd through its own config
# first, so the whole set exists and LLVM's module adds nothing.
find_package(zstd CONFIG QUIET)
find_package(LLVM CONFIG)
if(LLVM_FOUND)
if(${LLVM_VERSION} VERSION_LESS 7)
set(LLVM_FOUND FALSE)
endif()
endif()
if(LLVM_FOUND)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
find_program(CLANG_OPENCL_COMPILER
NAMES clang-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} clang-${LLVM_PACKAGE_VERSION} clang${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR} clang-${LLVM_VERSION_MAJOR} clang${LLVM_VERSION_MAJOR}
)
if(NOT ${CLANG_OPENCL_COMPILER} STREQUAL "CLANG_OPENCL_COMPILER-NOTFOUND")
message(STATUS "Found clang compiler - ${CLANG_OPENCL_COMPILER}")
find_path(CLANG_OPENCL_INCLUDE_DIR opencl-c.h
HINTS ${LLVM_INSTALL_PREFIX}/lib/clang ${LLVM_INSTALL_PREFIX}/lib64/clang
PATH_SUFFIXES include ${LLVM_PACKAGE_VERSION}/include ${LLVM_VERSION_MAJOR}/include
NO_DEFAULT_PATH
)
if(NOT ${CLANG_OPENCL_INCLUDE_DIR} STREQUAL "CLANG_OPENCL_INCLUDE_DIR-NOTFOUND")
message(STATUS "Found clang opencl-c.h header in ${CLANG_OPENCL_INCLUDE_DIR}")
set(TESTBUILD_OPENCL_PROGRAMS ON)
else()
message(WARNING "Could not find clang opencl-c.h header include dir")
message(WARNING "Test-compilation of OpenCL programs can not be done.")
endif()
else()
message(WARNING "Could not find appropriate clang compiler")
message(WARNING "Test-compilation of OpenCL programs can not be done.")
endif()
else()
message(WARNING "Could not find LLVM 7 or above")
message(WARNING "Test-compilation of OpenCL programs can not be done.")
endif()
endif()
if(USE_OPENCL AND TESTBUILD_OPENCL_PROGRAMS)
message(STATUS "Will be able to test-compile OpenCL programs. Nice.")
elseif(USE_OPENCL)
message(STATUS "Test-compilation of OpenCL programs is disabled.")
endif()
# we need the jsonschema python library to check noiseprofiles.json.
# We call its validation API ourselves (tools/validate_json_schema.py) instead
# of the `jsonschema` command-line tool, which is deprecated upstream in favor
# of the separate, not-universally-packaged `check-jsonschema`.
# On Windows/MSYS2, CMake's Python3 finder prefers registry-registered
# installs (the standalone Windows python.org/Store build) over whatever
# `python3` is on PATH inside the MSYS2 shell build.sh runs from — that
# registry Python never has the MSYS2-packaged jsonschema module. Make PATH
# win so we pick up the same interpreter the MSYS2 package manager provides.
set(Python3_FIND_REGISTRY "LAST")
find_package(Python3 COMPONENTS Interpreter)
set(VALIDATE_JSON 0)
if(Python3_Interpreter_FOUND)
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import jsonschema"
RESULT_VARIABLE jsonschema_import_result
OUTPUT_QUIET ERROR_QUIET
)
if(jsonschema_import_result EQUAL 0)
message(STATUS "Found jsonschema")
set(VALIDATE_JSON 1)
else()
message(STATUS "Missing jsonschema, problems in noiseprofiles.json might go unnoticed")
endif()
else()
message(STATUS "Missing Python3, problems in noiseprofiles.json might go unnoticed")
endif()
# we need an XSLT interpreter to generate preferences_gen.h and anselrc
find_program(Xsltproc_BIN xsltproc)
if(${Xsltproc_BIN} STREQUAL "Xsltproc_BIN-NOTFOUND")
message(STATUS "Missing xsltproc")
find_program(Saxon_BIN saxon-xslt)
if(${Saxon_BIN} STREQUAL "Saxon_BIN-NOTFOUND")
message(STATUS "Missing Saxon-XSLT")
message(STATUS "No XSLT interpreter found")
set(EXTERNAL_PROGRAMS_FOUND 0)
else(${Saxon_BIN} STREQUAL "Saxon_BIN-NOTFOUND")
message(STATUS "Found Saxon-XSLT")
endif(${Saxon_BIN} STREQUAL "Saxon_BIN-NOTFOUND")
else(${Xsltproc_BIN} STREQUAL "Xsltproc_BIN-NOTFOUND")
message(STATUS "Found xsltproc")
endif(${Xsltproc_BIN} STREQUAL "Xsltproc_BIN-NOTFOUND")
# do we have xmllint?
if(USE_XMLLINT)
find_program(Xmllint_BIN xmllint)
if(${Xmllint_BIN} STREQUAL "Xmllint_BIN-NOTFOUND")
message(STATUS "Missing xmllint")
set(USE_XMLLINT OFF)
else(${Xmllint_BIN} STREQUAL "Xmllint_BIN-NOTFOUND")
message(STATUS "Found xmllint")
endif(${Xmllint_BIN} STREQUAL "Xmllint_BIN-NOTFOUND")
endif(USE_XMLLINT)
find_program(exiftool_BIN exiftool)
if(${exiftool_BIN} STREQUAL "exiftool_BIN-NOTFOUND")
message(STATUS "Missing exiftool")
set(HAVE_EXIFTOOL 0)
else()
message(STATUS "Found exiftool")
set(HAVE_EXIFTOOL 1)
endif()
# done with looking for programs
if(NOT EXTERNAL_PROGRAMS_FOUND)
message(FATAL_ERROR "Some external programs couldn't be found")
else(NOT EXTERNAL_PROGRAMS_FOUND)
message(STATUS "All external programs found")
endif(NOT EXTERNAL_PROGRAMS_FOUND)
# The path can be modified by setting CMAKE_INSTALL_LOCALEDIR
if(USE_NLS)
find_package(Gettext)
if(GETTEXT_MSGFMT_EXECUTABLE)
message(STATUS "Found msgfmt to convert .po file. Translation enabled")
add_subdirectory(po)
else()
message(STATUS "Cannot find msgfmt to convert .po file. Translation won't be enabled")
endif()
endif(USE_NLS)
# needed to generate file "preferences_gen.h" accordingly
if(USE_OPENCL)
set(HAVE_OPENCL 1)
else()
set(HAVE_OPENCL 0)
endif(USE_OPENCL)
if(NOT SOURCE_PACKAGE AND NOT (CMAKE_VERSION VERSION_LESS 3.3) AND DEFINED ENV{_DO_IWYU})
find_program(iwyu_path NAMES include-what-you-use iwyu)
if(iwyu_path)
set(DT_CMAKE_INCLUDE_WHAT_YOU_USE ${iwyu_path} -Xiwyu --mapping_file=${CMAKE_SOURCE_DIR}/iwyu.imp -Xiwyu --prefix_header_includes=add)
endif()
find_program(iwyu_tool_path NAMES iwyu_tool.py)
if(iwyu_tool_path)
add_custom_command(
OUTPUT "${CMAKE_BINARY_DIR}/iwyu.log"
COMMAND "${iwyu_tool_path}" -v -p "${CMAKE_BINARY_DIR}"
-- --mapping_file=${CMAKE_SOURCE_DIR}/iwyu.imp
--prefix_header_includes=add 2>
"${CMAKE_BINARY_DIR}/iwyu.log"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT "Running include-what-you-use tool"
VERBATIM
)
add_custom_target(iwyu
DEPENDS "${CMAKE_BINARY_DIR}/iwyu.log"
VERBATIM
)
endif()
find_program(fix_includes_path NAMES fix_includes.py)
if(fix_includes_path)
add_custom_target(iwyu_fix
COMMAND "${fix_includes_path}" --noblank_lines --comments
--nosafe_headers < "${CMAKE_BINARY_DIR}/iwyu.log" || true
COMMAND ${CMAKE_COMMAND} -E remove "${CMAKE_BINARY_DIR}/iwyu.log"
DEPENDS "${CMAKE_BINARY_DIR}/iwyu.log"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT "Running include-what-you-use fix_includes tool"
VERBATIM
)
endif()
endif()
# Debug builds carry the unit tests by default; any configuration can opt in or out
# explicitly with -DBUILD_TESTING=ON/OFF. An EMPTY value counts as undecided -- existing
# build trees carry an empty BUILD_TESTING:BOOL in their cache, which is defined but
# answers no question. Not cached on purpose: flipping the build type flips the default.
#
# The DEFAULT is tolerant: a Debug machine without cmocka builds without tests and says so,
# because several CI Debug runners are not provisioned for it. An EXPLICIT
# -DBUILD_TESTING=ON stays a hard requirement.
#
# Linux and Windows. Windows links every executable with -municode, so the CRT enters
# through wmain(): each test includes win/main_wrapper.h under _WIN32 for that. Apple's ld64
# has no --wrap, so the cmocka mock tests can never link there.
set(_build_testing_defaulted FALSE)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND "${BUILD_TESTING}" STREQUAL "" AND NOT APPLE)
set(BUILD_TESTING ON)
set(_build_testing_defaulted TRUE)
endif()
if(BUILD_TESTING)
if(_build_testing_defaulted)
find_package(cmocka 1.1.0 CONFIG QUIET)
if(NOT cmocka_FOUND)
message(STATUS "Debug build without unit tests: cmocka not found (install it, or pass -DBUILD_TESTING=ON to make this an error)")
set(BUILD_TESTING OFF)
endif()
else()
find_package(cmocka 1.1.0 CONFIG REQUIRED)
endif()
endif()
if(BUILD_TESTING)
# cmocka >= 2 exports cmocka::cmocka and its plain `cmocka` name no longer carries the
# include directory (homebrew, notably); older packages export only `cmocka`.
if(TARGET cmocka::cmocka)
set(CMOCKA_LINK_TARGET cmocka::cmocka)
else()
set(CMOCKA_LINK_TARGET cmocka)
endif()
include(AddCMockaTest)
include(AddCMockaMockTest)
include_directories(${CMOCKA_INCLUDE_DIR} include)
add_subdirectory(tests)
endif(BUILD_TESTING)
# ---- Windows ----
if(WIN32)
add_compile_definitions(_USE_MATH_DEFINES)
endif()
# ---- Unix (Linux + macOS) ----
if(UNIX)
add_compile_definitions(_DEFAULT_SOURCE)
endif()
# ---- Linux-only POSIX level ----
if(UNIX AND NOT APPLE)
add_compile_definitions(_POSIX_C_SOURCE=200809L)
endif()
# ---- macOS extensions ----
if(APPLE)
add_compile_definitions(_DARWIN_C_SOURCE)
endif()
# Set default component name - that way external modules like RawSpeed will install their
# materials under the default component and not under 'Unspecified'
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME DTApplication)
# uninstall target
#
# Declared HERE, above add_subdirectory(src), rather than at the bottom of this file
# where it used to sit: the bundled Exiv2 (src/external/CMakeLists.txt) creates an
# `uninstall` target of its own unless one already exists, and overwrites
# ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake on its way past. Getting there first is what
# keeps that guard shut. src/external/CMakeLists.txt asserts the target exists, so moving
# this back down fails the configure rather than producing a mystery duplicate target.
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
# lets continue into build directories
# this file needs to be included first as it gets amended in src/ in build dir
include(data/supported_extensions.cmake)
# directory needs to be there before data directory so that the correct CSS file gets installed
add_subdirectory(src)
add_subdirectory(data)
add_subdirectory(doc)
add_subdirectory(tools)
# This contains fixup_bundle
# And adding a separate subderectory as a last one will make sure
# that fixup_bundle will run _after_ all files has been installed
add_subdirectory(packaging)
include(cmake/ansel-packaging.cmake)
# NOTE: Sentry debug-information (symbol) upload is intentionally NOT wired into
# the build. It is handled out-of-band by tools/sentry-upload-symbols.sh, invoked
# only by the nightly release workflows (and manually for local testing), so that
# building - via build.sh, the regular CI, or plain cmake - never uploads symbols.
feature_summary(WHAT ALL)