aboutsummaryrefslogtreecommitdiffstats
path: root/CMakeLists.txt
blob: e9e41b6a0be3b4cea118cd399d841c3ba0988be4 (plain)
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
#
# Rspamd - rambler antispam system
#
# Cmake configuration file
#
PROJECT(rspamd C)

SET(RSPAMD_VERSION_MAJOR 0)
SET(RSPAMD_VERSION_MINOR 4)
SET(RSPAMD_VERSION_PATCH 5)


SET(RSPAMD_VERSION         "${RSPAMD_VERSION_MAJOR}.${RSPAMD_VERSION_MINOR}.${RSPAMD_VERSION_PATCH}")
SET(RSPAMD_MASTER_SITE_URL "http://bitbucket.org/vstakhov/rspamd")
SET(RSPAMD_USER "nobody")
SET(RSPAMD_GROUP "nobody")

CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR)

OPTION(DEBUG_MODE          "Enable debug output [default: ON]"                  ON)
OPTION(ENABLE_OPTIMIZATION "Enable optimization [default: OFF]"                 OFF)
OPTION(SKIP_RELINK_RPATH   "Skip relinking and full RPATH for the install tree" OFF)
OPTION(ENABLE_REDIRECTOR   "Enable redirector install [default: OFF]"           OFF)
OPTION(ENABLE_PROFILING    "Enable profiling [default: OFF]"                    OFF)
OPTION(ENABLE_GPERF_TOOLS  "Enable google perftools [default: OFF]"             OFF)
OPTION(ENABLE_STATIC       "Enable static compiling [default: OFF]"             OFF)
OPTION(ENABLE_LUAJIT       "Link with libluajit [default: OFF]"                 OFF)
OPTION(FORCE_GMIME24       "Link with gmime2.4 [default: OFF]"                  OFF)

# Build optimized code for following CPU (default i386)
#SET(CPU_TUNE               "i686")

############################# CONFIG SECTION #############################################

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckCSourceCompiles)
INCLUDE(CheckLibraryExists)
INCLUDE(FindPkgConfig)
INCLUDE(CheckCCompilerFlag)
INCLUDE(FindPerl)

# Initial set

IF(CMAKE_INSTALL_PREFIX)
    SET(PREFIX ${CMAKE_INSTALL_PREFIX})
ENDIF(CMAKE_INSTALL_PREFIX)

# Platform specific configuration
IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE -DFREEBSD")
	CONFIGURE_FILE(freebsd/rspamd.sh.in freebsd/rspamd @ONLY)

	IF(ENABLE_REDIRECTOR MATCHES "ON")
		CONFIGURE_FILE(freebsd/redirector.sh.in freebsd/redirector @ONLY)
	ENDIF(ENABLE_REDIRECTOR MATCHES "ON")
	IF(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /usr/local/etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/local/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr/local)
    	ENDIF(NOT PREFIX)
	ENDIF(CMAKE_INSTALL_PREFIX)
    FIND_LIBRARY(LIBINTL_LIBRARY NAMES intl PATHS   /lib
                                               /opt/lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the libintl library can be found")
    LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBINTL_LIBRARY})
	MESSAGE(STATUS "Configuring for FreeBSD")
 
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")

IF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE -DDARWIN")
    
    IF(CMAKE_INSTALL_PREFIX)
    	IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	   	ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    	
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /usr/local/etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/local/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr/local)
    	ENDIF(NOT PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)

    FIND_LIBRARY(LIBINTL_LIBRARY NAMES intl PATHS   /lib
                                               /opt/lib
                                               /usr/lib
                                               /usr/local/lib
                 DOC "Path where the libintl library can be found")
    LIST(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBINTL_LIBRARY})
	MESSAGE(STATUS "Configuring for Darwin")

ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -DLINUX")
	IF(CMAKE_INSTALL_PREFIX)
		IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/share/man")
	    ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    	
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr)
    	ENDIF(NOT PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/share/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)
    
    # Workaround with architecture specific includes
    IF(IS_DIRECTORY "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/") 
    	INCLUDE_DIRECTORIES("/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    	SET(CMAKE_REQUIRED_INCLUDES "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    ENDIF(IS_DIRECTORY "/usr/include/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu/")
    
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES dl)
	MESSAGE(STATUS "Configuring for Linux")
	IF(EXISTS "/etc/debian_version")
		SET(LINUX_START_SCRIPT "rspamd_debian.in")
	ELSE(EXISTS "/etc/debian_version")
		SET(LINUX_START_SCRIPT "rspamd_rh.in")
	ENDIF(EXISTS "/etc/debian_version")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")

IF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
 	IF(NOT BUILD_CPU_MODE)
	 	SET (BUILD_CPU_MODE "32")
	ENDIF(NOT BUILD_CPU_MODE)
	
	IF("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xa -xregs=no%frameptr -xstrconst")
		set(SUN_BUILD32 "-xtarget=generic")
		set(SUN_BUILD64 "-xtagret=generic64")
		IF(ENABLE_OPTIMIZATION MATCHES "ON")
			SET(CMAKE_C_OPT_FLAGS "-fast -xdepend")
		ELSE(ENABLE_OPTIMIZATION MATCHES "ON")
			SET(CMAKE_C_OPT_FLAGS "-xO0")
		ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")
	ELSE("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
		set(SUN_BUILD32 "-m32")
		set(SUN_BUILD64 "-m64")		
	ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)

 	IF (BUILD_CPU_MODE STREQUAL "32")
       message (STATUS "Building 32-bit mode on Solaris")
       # If we are asked to build 32 bit mode
       	set(CMAKE_C_FLAGS  "${SUN_BUILD32} ${CMAKE_C_FLAGS}")
       	set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${SUN_BUILD32}")
 	ELSE (BUILD_CPU_MODE STREQUAL "32")
       # solaris 64-bit mode
		message (STATUS "Building 64-bit mode on Solaris")
       	set(CMAKE_C_FLAGS  "${SUN_BUILD64} ${CMAKE_C_FLAGS}")
       	set(CMAKE_SHARED_LINKER_FLAGS  "${CMAKE_SHARED_LINKER_FLAGS} ${SUN_BUILD64}")
    ENDIF (BUILD_CPU_MODE STREQUAL "32")

 	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__EXTENSIONS__ -DSOLARIS")

	IF(CMAKE_INSTALL_PREFIX)
		IF(NOT ETC_PREFIX)
	    	SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	    ENDIF(NOT ETC_PREFIX)
	    IF(NOT MAN_PREFIX)
	    	SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	    ENDIF(NOT MAN_PREFIX)
	    IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
	    SET(PREFIX "${CMAKE_INSTALL_PREFIX}")
	ELSE(CMAKE_INSTALL_PREFIX)    
    	IF(NOT ETC_PREFIX)
    		SET(ETC_PREFIX /etc)
    	ENDIF(NOT ETC_PREFIX)
    	IF(NOT PREFIX)
    		SET(PREFIX /usr)
    	ENDIF(NOT PREFIX)
    	IF(NOT MAN_PREFIX)
    		SET(MAN_PREFIX /usr/share/man)
    	ENDIF(NOT MAN_PREFIX)
    	IF(NOT LOCALSTATES_PREFIX)
	    	SET(LOCALSTATES_PREFIX "/var/run/rspamd")
    	ENDIF(NOT LOCALSTATES_PREFIX)
    ENDIF(CMAKE_INSTALL_PREFIX)
    
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES rt)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES dl)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES resolv)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES nsl)
 	LIST(APPEND CMAKE_REQUIRED_LIBRARIES umem)
  	SET(CMAKE_VERBOSE_MAKEFILE ON)
  	SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
  	SET(CMAKE_INSTALL_RPATH "${PREFIX}/lib")	
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")

# Check prefixes
IF(NOT ETC_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		MESSAGE(WARNING "Your system is unknown for rspamd, assume defining ETC_PREFIX and MAN_PREFIX by -D option, now ETC_PREFIX and MAN_PREFIX are relative to ${CMAKE_INSTALL_PREFIX}")
		SET(ETC_PREFIX "${CMAKE_INSTALL_PREFIX}/etc")
	ELSE(CMAKE_INSTALL_PREFIX)
		MESSAGE(FATAL_ERROR "Your system is unknown for rspamd and no CMAKE_INSTALL_PREFIX specified, assume defining CMAKE_INSTALL_PREFIX")
	ENDIF(CMAKE_INSTALL_PREFIX) 
	MESSAGE(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}")
ENDIF(NOT ETC_PREFIX)
IF(NOT MAN_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		SET(MAN_PREFIX "${CMAKE_INSTALL_PREFIX}/man")
	ENDIF(CMAKE_INSTALL_PREFIX) 
ENDIF(NOT MAN_PREFIX)
IF(NOT LOCALSTATES_PREFIX)
	IF(CMAKE_INSTALL_PREFIX)
		SET(LOCALSTATES_PREFIX "/var/run/rspamd")
	ENDIF(CMAKE_INSTALL_PREFIX) 
ENDIF(NOT LOCALSTATES_PREFIX)

# Now CMAKE_INSTALL_PREFIX is a base prefix for everything
# ETC_PREFIX - for configuration
# MAN_PREFIX - for manual pages
# LOCALSTATES_PREFIX - for runtime files

# Now find libraries and headers

INCLUDE(FindLua51)
# Check for luajit
IF(ENABLE_LUAJIT MATCHES "ON")
    FIND_LIBRARY(LUAJIT_LIBRARY NAMES luajit51 luajit5.1 luajit-5.1 luajit
                  HINTS
                  $ENV{LUA_DIR}
                  PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                )
     IF(NOT LUAJIT_LIBRARY)
         MESSAGE(FATAL_ERROR "Luajit not found but enabled in configuration")
     ENDIF(NOT LUAJIT_LIBRARY)
ENDIF(ENABLE_LUAJIT MATCHES "ON")

IF(NOT LUA_FOUND)
    # Automatic check failed, check passed variable
    IF(LUA_INCLUDE_DIR)
        INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
        SET(WITH_LUA 1)
    ELSE(LUA_INCLUDE_DIR)
        MESSAGE(FATAL_ERROR "Lua not found, lua support is required for working")
    ENDIF(LUA_INCLUDE_DIR)
ELSE(NOT LUA_FOUND)
    SET(WITH_LUA 1)
    INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}")
ENDIF(NOT LUA_FOUND)

# Check and link to pcre
pkg_check_modules(PCRE REQUIRED libpcre)
IF(PCRE_INCLUDE_DIRS)
	INCLUDE_DIRECTORIES("${PCRE_INCLUDE_DIRS}")
ENDIF(PCRE_INCLUDE_DIRS)
IF(PCRE_LIBRARY_DIRS)
	LINK_DIRECTORIES("${PCRE_LIBRARY_DIRS}")
ENDIF(PCRE_LIBRARY_DIRS)

# Find optional sqlite3 support
pkg_check_modules(SQLITE sqlite3)
IF(SQLITE_FOUND)
	SET(WITH_SQLITE 1)
ENDIF(SQLITE_FOUND)
IF(SQLITE_INCLUDE_DIRS)
	INCLUDE_DIRECTORIES("${SQLITE_INCLUDE_DIRS}")
ENDIF(SQLITE_INCLUDE_DIRS)
IF(SQLITE_LIBRARY_DIRS)
	LINK_DIRECTORIES("${SQLITE_LIBRARY_DIRS}")
ENDIF(SQLITE_LIBRARY_DIRS)

IF(ENABLE_STATIC MATCHES "ON")
	pkg_check_modules(GLIB2 REQUIRED glib-2.0>=2.12)
ELSE(ENABLE_STATIC MATCHES "ON")
	pkg_check_modules(GLIB2 REQUIRED glib-2.0>=2.12 gmodule-2.0)
	SET(GLIB2_VERSION "${GLIB2_glib-2.0_VERSION}")
ENDIF(ENABLE_STATIC MATCHES "ON")
pkg_check_modules(GMIME2 gmime-2.0)

# Try to link with gmime24
IF(NOT GMIME2_FOUND OR FORCE_GMIME24)
	pkg_check_modules(GMIME24 REQUIRED gmime-2.4)
	SET(GMIME24 "yes")
	# Gmime2
	FOREACH(arg ${GMIME24_CFLAGS})
		SET(GMIME_CFLAGS "${GMIME_CFLAGS} ${arg}")
	ENDFOREACH(arg ${GMIME24_CFLAGS})

	IF(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME24_STATIC_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME24_LDFLAGS})
	ELSE(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME24_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME24_LDFLAGS})
	ENDIF(ENABLE_STATIC MATCHES "ON")
	IF(GMIME24_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES(${GMIME24_INCLUDE_DIRS})
	ENDIF(GMIME24_INCLUDE_DIRS)
	IF(GMIME24_LIBRARY_DIRS)
		LINK_DIRECTORIES(${GMIME24_LIBRARY_DIRS})
	ENDIF(GMIME24_LIBRARY_DIRS)
ELSE(NOT GMIME2_FOUND OR FORCE_GMIME24)
	# Gmime2
	FOREACH(arg ${GMIME2_CFLAGS})
		SET(GMIME_CFLAGS "${GMIME_CFLAGS} ${arg}")
	ENDFOREACH(arg ${GMIME2_CFLAGS})

	IF(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME2_STATIC_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME2_LDFLAGS})
	ELSE(ENABLE_STATIC MATCHES "ON")
		FOREACH(arg ${GMIME2_LDFLAGS})
			SET(GMIME_LDFLAGS "${GMIME_LDFLAGS} ${arg}")
		ENDFOREACH(arg ${GMIME2_LDFLAGS})
	ENDIF(ENABLE_STATIC MATCHES "ON")
	IF(GMIME2_INCLUDE_DIRS)
		INCLUDE_DIRECTORIES(${GMIME2_INCLUDE_DIRS})
	ENDIF(GMIME2_INCLUDE_DIRS)
	IF(GMIME2_LIBRARY_DIRS)
		LINK_DIRECTORIES(${GMIME2_LIBRARY_DIRS})
	ENDIF(GMIME2_LIBRARY_DIRS)
ENDIF(NOT GMIME2_FOUND OR FORCE_GMIME24)

# Make from ; separated list normal space separated list
# Glib2
FOREACH(arg ${GLIB2_CFLAGS})
	SET(GLIB_CFLAGS "${GLIB_CFLAGS} ${arg}")
ENDFOREACH(arg ${GLIB2_CFLAGS})

IF(ENABLE_STATIC MATCHES "ON")
	FOREACH(arg ${GLIB2_STATIC_LDFLAGS})
		SET(GLIB_LDFLAGS "${GLIB_LDFLAGS} ${arg}")
	ENDFOREACH(arg ${GLIB2_LDFLAGS})
ELSE(ENABLE_STATIC MATCHES "ON")
	FOREACH(arg ${GLIB2_LDFLAGS})
		SET(GLIB_LDFLAGS "${GLIB_LDFLAGS} ${arg}")
	ENDFOREACH(arg ${GLIB2_LDFLAGS})
ENDIF(ENABLE_STATIC MATCHES "ON")


INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS})


# Check for libevent

FIND_LIBRARY(LIBEVENT_LIBRARY NAMES event PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libevent library can be found")
IF(NOT LIBEVENT_LIBRARY)
	MESSAGE(FATAL_ERROR "libevent is required for building rspamd")
ENDIF(NOT LIBEVENT_LIBRARY)

FIND_PATH(LIBEVENT_INCLUDE event.h PATHS 	/opt/include
										/usr/include
										/usr/local/include
										DOC "Path where the libevent header files can be found")

GET_FILENAME_COMPONENT(LIBEVENT_PATH "${LIBEVENT_LIBRARY}" PATH)
INCLUDE_DIRECTORIES("${LIBEVENT_INCLUDE}")
LINK_DIRECTORIES("${LIBEVENT_PATH}")

# Find libjudy
FIND_LIBRARY(LIBJUDY_LIBRARY NAMES Judy PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libjudy library can be found")
IF(LIBJUDY_LIBRARY)
	FIND_PATH(LIBJUDY_INCLUDE Judy.h PATHS 	/opt/include
											/usr/include
											/usr/local/include
											DOC "Path where the judy header files can be found")

	GET_FILENAME_COMPONENT(LIBJUDY_PATH "${LIBJUDY_LIBRARY}" PATH)
	INCLUDE_DIRECTORIES("${LIBJUDY_INCLUDE}")
	LINK_DIRECTORIES("${LIBJUDY_PATH}")
	SET(WITH_JUDY 1)
ENDIF(LIBJUDY_LIBRARY)

# Find libbd
FIND_LIBRARY(LIBDB_LIBRARY NAMES db PATHS PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libdb library can be found")
IF(LIBDB_LIBRARY)
	FIND_PATH(LIBDB_INCLUDE db.h PATHS 	/opt/include
											/usr/include
											/usr/local/include
											DOC "Path where the bdb header files can be found")

	GET_FILENAME_COMPONENT(LIBDB_PATH "${LIBDB_LIBRARY}" PATH)
	INCLUDE_DIRECTORIES("${LIBDB_INCLUDE}")
	LINK_DIRECTORIES("${LIBDB_PATH}")
	SET(WITH_DB 1)
ENDIF(LIBDB_LIBRARY)

IF(ENABLE_PROFILING MATCHES "ON")
	SET(WITH_PROFILER 1)
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
ENDIF(ENABLE_PROFILING MATCHES "ON")

# Static build

IF(ENABLE_STATIC MATCHES "ON")
	SET(BUILD_STATIC 1)
	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -pthread")
	MESSAGE(STATUS "Static build of rspamd, no custom plugins support")
ENDIF(ENABLE_STATIC MATCHES "ON")

# Google performance tools

IF(ENABLE_GPERF_TOOLS MATCHES "ON")
	FIND_PATH(GPERF_INCLUDE google/profiler.h PATHS /opt/include
													/usr/include
													/usr/local/include
											  DOC "Path where google perftools includes can be found")
	INCLUDE_DIRECTORIES("${GPERF_INCLUDE}")
	FIND_LIBRARY(GPERF_LIBRARY NAMES profiler PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
					DOC "Path where the gperf library can be found")
	IF(NOT GPERF_LIBRARY)
		MESSAGE(FATAL_ERROR "gperf tools support is enabled but not found in system")
	ENDIF(NOT GPERF_LIBRARY)
	GET_FILENAME_COMPONENT(GPERF_PATH "${GPERF_LIBRARY}" PATH)
	LINK_DIRECTORIES("${GPERF_PATH}")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer")
	SET(WITH_GPERF_TOOLS 1)
ENDIF(ENABLE_GPERF_TOOLS MATCHES "ON")

# Find util library

FIND_LIBRARY(LIBUTIL_LIBRARY NAMES util PATHS	PATH_SUFFIXES lib64 lib
                  PATHS
                      ~/Library/Frameworks
                      /Library/Frameworks
                      /usr/local
                      /usr
                      /sw
                      /opt/local
                      /opt/csw
                      /opt
                 DOC "Path where the libutil library can be found")
IF(LIBUTIL_LIBRARY)
	LIST(APPEND CMAKE_REQUIRED_LIBRARIES util)
	CHECK_FUNCTION_EXISTS(pidfile_open HAVE_PIDFILE)

	FIND_FILE(HAVE_LIBUTIL_H NAMES libutil.h PATHS /usr/include
											   /opt/include
											   /usr/local/include
				DOC "Path to libutil header")
ENDIF(LIBUTIL_LIBRARY)

 # Process with warn flags
 IF(CMAKE_COMPILER_IS_GNUCC)
 CHECK_C_COMPILER_FLAG(-Wall SUPPORT_WALL)
 CHECK_C_COMPILER_FLAG(-Wall SUPPORT_W)
 CHECK_C_COMPILER_FLAG(-Wpointer-arith SUPPORT_WPOINTER)
 CHECK_C_COMPILER_FLAG(-Wno-unused-parameter SUPPORT_WPARAM)
 CHECK_C_COMPILER_FLAG(-Wno-unused-function SUPPORT_WFUNCTION)
 CHECK_C_COMPILER_FLAG(-Wunused-variable SUPPORT_WUNUSED_VAR)
 CHECK_C_COMPILER_FLAG(-Wno-pointer-sign SUPPORT_WPOINTER_SIGN)
 CHECK_C_COMPILER_FLAG(-pedantic SUPPORT_PEDANTIC_FLAG)
 CHECK_C_COMPILER_FLAG("-std=c99" SUPPORT_STD_FLAG)
 
 SET(CMAKE_C_WARN_FLAGS "")
 
 IF(SUPPORT_W)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -W")
 ENDIF(SUPPORT_W)
 IF(SUPPORT_WALL)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wall")
 ENDIF(SUPPORT_WALL)
 IF(SUPPORT_WPOINTER)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wpointer-arith")
 ENDIF(SUPPORT_WPOINTER)
 IF(SUPPORT_WPARAM)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-parameter")
 ENDIF(SUPPORT_WPARAM)
 IF(SUPPORT_WFUNCTION)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-unused-function")
 ENDIF(SUPPORT_WFUNCTION)
 IF(SUPPORT_WUNUSED_VAR)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wunused-variable")
 ENDIF(SUPPORT_WUNUSED_VAR)
 IF(SUPPORT_WPOINTER_SIGN)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -Wno-pointer-sign")
 ENDIF(SUPPORT_WPOINTER_SIGN)
 IF(SUPPORT_PEDANTIC_FLAG)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -pedantic")
 ENDIF(SUPPORT_PEDANTIC_FLAG)
 IF(SUPPORT_STD_FLAG)
 	SET(CMAKE_C_WARN_FLAGS "${CMAKE_C_WARN_FLAGS} -std=c99")
 ENDIF(SUPPORT_STD_FLAG)
 ENDIF(CMAKE_COMPILER_IS_GNUCC)
 
 # Optimization flags
IF(NOT CMAKE_C_OPT_FLAGS)
	IF(ENABLE_OPTIMIZATION MATCHES "ON")
		SET(CMAKE_C_OPT_FLAGS "-O3")
	ELSE(ENABLE_OPTIMIZATION MATCHES "ON")
		SET(CMAKE_C_OPT_FLAGS "-O0")
	ENDIF(ENABLE_OPTIMIZATION MATCHES "ON")
ENDIF(NOT CMAKE_C_OPT_FLAGS)

IF(DEBUG_MODE MATCHES "ON")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
ENDIF(DEBUG_MODE MATCHES "ON")

SET(CMAKE_C_FLAGS "${CMAKE_C_OPT_FLAGS} ${CMAKE_C_FLAGS} ${CMAKE_C_WARN_FLAGS}")
 
# Check platform specific includes
CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILES(sys/time.h  HAVE_SYS_TIME_H)
CHECK_INCLUDE_FILES(sys/uio.h   HAVE_SYS_UIO_H)

CHECK_INCLUDE_FILES(fcntl.h     HAVE_FCNTL_H)
CHECK_INCLUDE_FILES(math.h      HAVE_MATH_H)
CHECK_INCLUDE_FILES(stdio.h     HAVE_STDIO_H)
CHECK_INCLUDE_FILES(stdlib.h    HAVE_STDLIB_H)
CHECK_INCLUDE_FILES(string.h    HAVE_STRING_H)
CHECK_INCLUDE_FILES(strings.h   HAVE_STRINGS_H)
CHECK_INCLUDE_FILES(time.h      HAVE_TIME_H)
CHECK_INCLUDE_FILES(unistd.h    HAVE_UNISTD_H)
CHECK_INCLUDE_FILES(sysexits.h  HAVE_SYSEXITS_H)
CHECK_INCLUDE_FILES(stdint.h  HAVE_STDINT_H)
CHECK_INCLUDE_FILES(inttypes.h  HAVE_INTTYPES_H)
CHECK_INCLUDE_FILES(endian.h  HAVE_ENDIAN_H)
CHECK_INCLUDE_FILES(sys/endian.h  HAVE_SYS_ENDIAN_H)
CHECK_INCLUDE_FILES(machine/endian.h  HAVE_MACHINE_ENDIAN_H)
CHECK_INCLUDE_FILES(sys/queue.h  HAVE_SYS_QUEUE_H)
CHECK_INCLUDE_FILES(sys/socket.h  HAVE_SYS_SOCKET_H)
CHECK_INCLUDE_FILES(sys/mman.h  HAVE_SYS_MMAN_H)
CHECK_INCLUDE_FILES(sys/un.h  HAVE_SYS_UN_H)
CHECK_INCLUDE_FILES(sys/stat.h  HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILES(sys/wait.h  HAVE_SYS_WAIT_H)
CHECK_INCLUDE_FILES(sys/param.h  HAVE_SYS_PARAM_H)
CHECK_INCLUDE_FILES(sys/cdefs.h  HAVE_SYS_CDEFS_H)
CHECK_INCLUDE_FILES(sys/file.h  HAVE_SYS_FILE_H)
CHECK_INCLUDE_FILES(sys/utsname.h  HAVE_SYS_UTSNAME_H)
CHECK_INCLUDE_FILES(sys/resource.h  HAVE_SYS_RESOURCE_H)
CHECK_INCLUDE_FILES(netinet/in.h  HAVE_NETINET_IN_H)
CHECK_INCLUDE_FILES(arpa/inet.h  HAVE_ARPA_INET_H)
CHECK_INCLUDE_FILES(netdb.h  HAVE_NETDB_H)
CHECK_INCLUDE_FILES(syslog.h HAVE_SYSLOG_H)
CHECK_INCLUDE_FILES(siginfo.h HAVE_SIGINFO_H)
CHECK_INCLUDE_FILES(locale.h HAVE_LOCALE_H)
CHECK_INCLUDE_FILES(libgen.h HAVE_LIBGEN_H)
CHECK_INCLUDE_FILES(search.h HAVE_SEARCH_H)
CHECK_INCLUDE_FILES(pwd.h HAVE_PWD_H)
CHECK_INCLUDE_FILES(grp.h HAVE_GRP_H)
CHECK_INCLUDE_FILES(glob.h HAVE_GLOB_H)
CHECK_INCLUDE_FILES(poll.h HAVE_POLL_H)
CHECK_INCLUDE_FILES(sys/sendfile.h HAVE_SYS_SENDFILE_H)

# Some dependencies
IF(HAVE_SYS_WAIT_H)
	LIST(APPEND CMAKE_REQUIRED_INCLUDES sys/wait.h)
ENDIF(HAVE_SYS_WAIT_H)
IF(HAVE_TIME_H)
	LIST(APPEND CMAKE_REQUIRED_INCLUDES time.h)
ENDIF(HAVE_TIME_H)
IF(HAVE_SYS_TIME_H)
	LIST(APPEND CMAKE_REQUIRED_INCLUDES sys/time.h)
ENDIF(HAVE_SYS_TIME_H)

# Check platform API
CHECK_FUNCTION_EXISTS(setproctitle HAVE_SETPROCTITLE)
CHECK_FUNCTION_EXISTS(getpagesize HAVE_GETPAGESIZE)
CHECK_FUNCTION_EXISTS(nanosleep HAVE_NANOSLEEP)
CHECK_FUNCTION_EXISTS(vfork HAVE_VFORK)
CHECK_FUNCTION_EXISTS(wait4 HAVE_WAIT4)
CHECK_FUNCTION_EXISTS(waitpid HAVE_WAITPID)
CHECK_FUNCTION_EXISTS(flock HAVE_FLOCK)
CHECK_FUNCTION_EXISTS(tanhl HAVE_TANHL)
CHECK_FUNCTION_EXISTS(sendfile HAVE_SENDFILE)
CHECK_FUNCTION_EXISTS(mkstemp HAVE_MKSTEMP)
CHECK_FUNCTION_EXISTS(setitimer HAVE_SETITIMER)
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)

# Check macros
CHECK_SYMBOL_EXISTS(PATH_MAX limits.h HAVE_PATH_MAX)
CHECK_SYMBOL_EXISTS(MAXPATHLEN sys/param.h HAVE_MAXPATHLEN)
CHECK_SYMBOL_EXISTS(MAP_SHARED sys/mman.h HAVE_MMAP_SHARED)
CHECK_SYMBOL_EXISTS(MAP_ANON sys/mman.h HAVE_MMAP_ANON)
CHECK_SYMBOL_EXISTS(MAP_NOCORE sys/mman.h HAVE_MMAP_NOCORE)
CHECK_SYMBOL_EXISTS(O_DIRECT fcntl.h HAVE_O_DIRECT)
CHECK_SYMBOL_EXISTS(posix_fadvise fcntl.h HAVE_FADVISE)
CHECK_SYMBOL_EXISTS(fdatasync unistd.h HAVE_FDATASYNC)
CHECK_SYMBOL_EXISTS(_SC_NPROCESSORS_ONLN unistd.h HAVE_SC_NPROCESSORS_ONLN)
IF(HAVE_SIGINFO_H)
	CHECK_SYMBOL_EXISTS(SA_SIGINFO "signal.h;siginfo.h" HAVE_SA_SIGINFO)
ELSE(HAVE_SIGINFO_H)
	CHECK_SYMBOL_EXISTS(SA_SIGINFO "signal.h" HAVE_SA_SIGINFO)
ENDIF(HAVE_SIGINFO_H)

# Some hack for libevent 2.0
CHECK_C_SOURCE_COMPILES ("#include <event.h>
					#if _EVENT_NUMERIC_VERSION < 0x02000000
					#error Unsupported
					#endif
					int main() { return 0;}" HAVE_LIBEVENT2)

IF(NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")
IF(HAVE_CLOCK_GETTIME)
	CHECK_SYMBOL_EXISTS(CLOCK_PROCESS_CPUTIME_ID time.h HAVE_CLOCK_PROCESS_CPUTIME_ID)
	CHECK_SYMBOL_EXISTS(CLOCK_VIRTUAL time.h HAVE_CLOCK_VIRTUAL)
ELSE(HAVE_CLOCK_GETTIME)
	CHECK_INCLUDE_FILES(sys/timeb.h HAVE_SYS_TIMEB_H)
ENDIF(HAVE_CLOCK_GETTIME)

# Check asm pause instruction
CHECK_C_SOURCE_COMPILES ("#include <sys/types.h>
                          int main (int argc, char **argv) {
						  	__asm __volatile(\"pause\");
							return 0;
						  }" HAVE_ASM_PAUSE)
ENDIF(NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")

# Check queue.h compatibility
IF(NOT HAVE_COMPATIBLE_QUEUE_H)
	INCLUDE_DIRECTORIES(compat)
ENDIF(NOT HAVE_COMPATIBLE_QUEUE_H)


SET(CONTRIBSRC "")

IF(NOT DESTDIR)
    SET(DESTDIR $ENV{DESTDIR})
ENDIF(NOT DESTDIR)

# Try to detect tip version from hg
SET(ID "unknown")
FIND_PROGRAM(HG "hg")

IF(HG)
    EXECUTE_PROCESS(COMMAND "${HG} id > /dev/null" OUTPUT_VARIABLE HG_ID)   
    IF(HG_ID)
        STRING(REGEX MATCH ^[0-9a-z]+ ID "${HG_ID}")
    ELSE(HG_ID)
        FIND_FILE(HG_ARCH ".hg_archival.txt" "${CMAKE_CURRENT_SOURCE_DIR}")   
        IF(HG_ARCH)
            FILE(READ ${HG_ARCH} HG_ID)
            IF(HG_ID)
                STRING(REGEX MATCH "node: [0-9a-z]+" _tmp "${HG_ID}")
                STRING(SUBSTRING "${_tmp}" 6 12 ID)
            ENDIF(HG_ID)
        ENDIF(HG_ARCH)
    ENDIF(HG_ID)
ELSE(HG)
# Now check .hg_archival.txt
    FIND_FILE(HG_ARCH ".hg_archival.txt" "${CMAKE_CURRENT_SOURCE_DIR}")   
    IF(HG_ARCH)
        FILE(READ ${HG_ARCH} HG_ID)
        IF(HG_ID)
            STRING(REGEX MATCH "node: [0-9a-z]+" _tmp "${HG_ID}")
            STRING(SUBSTRING "${_tmp}" 6 12 ID)
        ENDIF(HG_ID)
    ENDIF(HG_ARCH)
ENDIF(HG)
 

################################ SOURCES SECTION ###########################

INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/src" "${CMAKE_BINARY_DIR}/src")

SET(RSPAMDSRC	src/modules.c
				src/controller.c
				src/fuzzy_storage.c
				src/greylist_storage.c
				src/kvstorage_server.c
				src/lmtp.c
				src/main.c
				src/map.c
				src/smtp.c
				src/worker.c)

SET(TOKENIZERSSRC  src/tokenizers/tokenizers.c
				src/tokenizers/osb.c)

SET(CLASSIFIERSSRC src/classifiers/classifiers.c
                src/classifiers/bayes.c
				src/classifiers/winnow.c)

SET(PLUGINSSRC	src/plugins/surbl.c
				src/plugins/regexp.c
				src/plugins/chartable.c
				src/plugins/fuzzy_check.c
				src/plugins/spf.c)

ADD_CUSTOM_COMMAND(OUTPUT src/modules.c
					COMMAND ${CMAKE_SOURCE_DIR}/utils/gen-modules.sh ${PLUGINSSRC}
					WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src)

ADD_SUBDIRECTORY(contrib/lgpl)
IF(GLIB_COMPAT)
	INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/contrib/lgpl")
ENDIF(GLIB_COMPAT)

ADD_SUBDIRECTORY(src/lua)
ADD_SUBDIRECTORY(src/json)
ADD_SUBDIRECTORY(src/cdb)

ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(src/client)

ADD_SUBDIRECTORY(utils)
ADD_SUBDIRECTORY(test)

LIST(LENGTH PLUGINSSRC RSPAMD_MODULES_NUM)

############################ TARGETS SECTION ###############################


CONFIGURE_FILE(config.h.in src/config.h)
CONFIGURE_FILE(contrib/exim/local_scan.c.in contrib/exim/local_scan_rspamd.c @ONLY)
CONFIGURE_FILE(conf/rspamd-basic.xml.in conf/rspamd.xml.sample @ONLY)

######################### LINK SECTION ###############################

ADD_EXECUTABLE(rspamd ${RSPAMDSRC} ${CONTRIBSRC} ${TOKENIZERSSRC} 
					  ${CLASSIFIERSSRC} ${PLUGINSSRC})
SET_TARGET_PROPERTIES(rspamd PROPERTIES LINKER_LANGUAGE C)
SET_TARGET_PROPERTIES(rspamd PROPERTIES COMPILE_FLAGS "-DRSPAMD_MAIN")
SET_TARGET_PROPERTIES(rspamd PROPERTIES VERSION ${RSPAMD_VERSION})

TARGET_LINK_LIBRARIES(rspamd rspamdserver)

IF(LIBJUDY_LIBRARY)
	TARGET_LINK_LIBRARIES(rspamd Judy)
ENDIF(LIBJUDY_LIBRARY)
IF(LIBDB_LIBRARY)
	TARGET_LINK_LIBRARIES(rspamd db)
ENDIF(LIBDB_LIBRARY)
IF(SQLITE_LIBRARIES)
	TARGET_LINK_LIBRARIES(rspamd ${SQLITE_LIBRARIES})
ENDIF(SQLITE_LIBRARIES)
TARGET_LINK_LIBRARIES(rspamd event)
IF(HAVE_LIBEVENT2)
	TARGET_LINK_LIBRARIES(rspamd event_pthreads)
ENDIF(HAVE_LIBEVENT2)
TARGET_LINK_LIBRARIES(rspamd ${PCRE_LIBRARIES})

TARGET_LINK_LIBRARIES(rspamd ${CMAKE_REQUIRED_LIBRARIES})
TARGET_LINK_LIBRARIES(rspamd ${GLIB2_LIBRARIES})
IF(GMIME24)
	TARGET_LINK_LIBRARIES(rspamd ${GMIME24_LIBRARIES})
ELSE(GMIME24)
	TARGET_LINK_LIBRARIES(rspamd ${GMIME2_LIBRARIES})
ENDIF(GMIME24)
IF(ENABLE_STATIC MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd ${PCRE_LIBRARIES})
ENDIF(ENABLE_STATIC MATCHES "ON")

IF(ENABLE_LUAJIT MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd "${LUAJIT_LIBRARY}")
ELSE(ENABLE_LUAJIT MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd "${LUA_LIBRARY}")
ENDIF(ENABLE_LUAJIT MATCHES "ON")

IF(ENABLE_GPERF_TOOLS MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd profiler)
ENDIF(ENABLE_GPERF_TOOLS MATCHES "ON")

IF(GLIB_COMPAT)
	TARGET_LINK_LIBRARIES(rspamd glibadditions)
ENDIF(GLIB_COMPAT)
TARGET_LINK_LIBRARIES(rspamd m)

##################### INSTALLATION ##########################################

# Binaries
INSTALL(TARGETS rspamd RUNTIME DESTINATION bin)

# Configs
INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${ETC_PREFIX}/rspamd)")

INSTALL(FILES conf/2tld.inc RENAME 2tld.inc.orig DESTINATION ${ETC_PREFIX}/rspamd)
IF(NOT EXISTS ${ETC_PREFIX}/rspamd/2tld.inc OR BUILD_PORT)
	INSTALL(FILES conf/2tld.inc DESTINATION ${ETC_PREFIX}/rspamd)
ENDIF(NOT EXISTS ${ETC_PREFIX}/rspamd/2tld.inc OR BUILD_PORT)

INSTALL(FILES conf/surbl-whitelist.inc RENAME surbl-whitelist.inc DESTINATION ${ETC_PREFIX}/rspamd)
IF(NOT EXISTS ${ETC_PREFIX}/rspamd/surbl-whitelist.inc OR BUILD_PORT)
	INSTALL(FILES conf/surbl-whitelist.inc DESTINATION ${ETC_PREFIX}/rspamd)
ENDIF(NOT EXISTS ${ETC_PREFIX}/rspamd/surbl-whitelist.inc OR BUILD_PORT)

INSTALL(FILES ${CMAKE_BINARY_DIR}/conf/rspamd.xml.sample DESTINATION ${ETC_PREFIX})

# Lua plugins
INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${ETC_PREFIX}/rspamd/plugins/lua)")

FILE(GLOB LUA_PLUGINS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/src/plugins/lua" "${CMAKE_CURRENT_SOURCE_DIR}/src/plugins/lua/*.lua" )
FOREACH(LUA_PLUGIN ${LUA_PLUGINS})
	GET_FILENAME_COMPONENT(_rp ${LUA_PLUGIN} PATH)
	INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${ETC_PREFIX}/rspamd/plugins/lua/${_rp})")
	INSTALL(FILES "src/plugins/lua/${LUA_PLUGIN}" DESTINATION ${ETC_PREFIX}/rspamd/plugins/lua/${_rp})
ENDFOREACH(LUA_PLUGIN)

# Lua config
INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${ETC_PREFIX}/rspamd/lua)")
FILE(GLOB_RECURSE LUA_CONFIGS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/conf/lua" "${CMAKE_CURRENT_SOURCE_DIR}/conf/lua/*.lua")
FOREACH(LUA_CONF ${LUA_CONFIGS})
	GET_FILENAME_COMPONENT(_rp ${LUA_CONF} PATH)
	INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}${ETC_PREFIX}/rspamd/lua/${_rp})")
	IF(NOT EXISTS ${DESTDIR}/${ETC_PREFIX}/rspamd/lua/${LUA_CONF} OR BUILD_PORT)
		INSTALL(FILES "conf/lua/${LUA_CONF}" DESTINATION ${ETC_PREFIX}/rspamd/lua/${_rp})
	ENDIF(NOT EXISTS ${DESTDIR}/${ETC_PREFIX}/rspamd/lua/${LUA_CONF} OR BUILD_PORT)
ENDFOREACH(LUA_CONF)

# Manual pages
INSTALL(FILES "doc/rspamd.8" DESTINATION ${MAN_PREFIX}/man8)
INSTALL(FILES "doc/rspamc.1" DESTINATION ${MAN_PREFIX}/man1)

# Start scripts
IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT BUILD_PORT)
	INSTALL(PROGRAMS ${CMAKE_BINARY_DIR}/freebsd/rspamd DESTINATION ${ETC_PREFIX}/rc.d)
	IF(ENABLE_REDIRECTOR MATCHES "ON")
		INSTALL(PROGRAMS ${CMAKE_BINARY_DIR}/freebsd/redirector DESTINATION ${ETC_PREFIX}/rc.d)
	ENDIF(ENABLE_REDIRECTOR MATCHES "ON")
	INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}/${LOCALSTATES_PREFIX})")
	INSTALL(CODE "EXECUTE_PROCESS(COMMAND chown ${RSPAMD_USER}:${RSPAMD_GROUP} \$ENV{DESTDIR}/${LOCALSTATES_PREFIX})")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT BUILD_PORT)
IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
	INSTALL(PROGRAMS "linux/${LINUX_START_SCRIPT}" DESTINATION ${ETC_PREFIX}/init.d RENAME rspamd)
	INSTALL(CODE "FILE(MAKE_DIRECTORY \$ENV{DESTDIR}/${LOCALSTATES_PREFIX})")
	INSTALL(CODE "EXECUTE_PROCESS(COMMAND chown ${RSPAMD_USER}:${RSPAMD_GROUP} \$ENV{DESTDIR}/${LOCALSTATES_PREFIX})")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")

# CPack section
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libevent-1.4-2, libgmime-2.0-2a")
SET(CPACK_PACKAGE_CONTACT "vsevolod@highsecure.ru")
SET(CPACK_PACKAGE_NAME rspamd)
SET(CPACK_SOURCE_IGNORE_FILES "\\\\.swp$" "/\\\\.hg/")
SET(CPACK_PACKAGE_VERSION_MAJOR ${RSPAMD_VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${RSPAMD_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${RSPAMD_VERSION_PATCH})
INCLUDE(CPack)

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    INCLUDE("linux/cmake/DpkgDeb.cmake")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")

IF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    INCLUDE("freebsd/cmake/PkgCreate.cmake")
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")

SET(RPMNAME "rspamd")
SET(PACKAGE_NAME "rspamd")
SET(PACKAGE_RELEASE "1")
SET(PACKAGE_SECTION "mail")
SET(PACKAGE_PRIORITY "optional")
SET(PACKAGE_HOMEPAGE "http://bitbucket.org/vstakhov/rspamd")
SET(PACKAGE_MAINTAINER_NAME "vstakhov")
SET(PACKAGE_MAINTAINER_EMAIL "vsevolod@highsecure.ru")
SET(PACKAGE_DESCRIPTION_SUMMARY "rspamd - fast and modular antispam system written in C")
SET(PACKAGE_DESCRIPTION "Fast and modular antispam system written in C")
IF(GMIME24)
    SET(PACKAGE_DEPENDS "libc6, libevent-1.4-2, libgmime-2.4-2, liblua5.1-0, libxml-parser-perl, libterm-readkey-perl, libglib2.0-0")
ELSE(GMIME24)
    SET(PACKAGE_DEPENDS "libc6, libevent-1.4-2, libgmime-2.0-2, libxml-parser-perl, libterm-readkey-perl, libglib2.0-0")
ENDIF(GMIME24)

SET(PACKAGE_VERSION "${RSPAMD_VERSION_MAJOR}.${RSPAMD_VERSION_MINOR}.${RSPAMD_VERSION_PATCH}")

IF(DPKG_FOUND STREQUAL "YES")
    ADD_DEBIAN_PACKAGE("rspamd")    
ENDIF(DPKG_FOUND STREQUAL "YES")
IF(PKGCREATE_FOUND STREQUAL "YES")
    ADD_FREEBSD_PACKAGE("rspamd" "freebsd/rspamd.plist")    
ENDIF(PKGCREATE_FOUND STREQUAL "YES")