summaryrefslogtreecommitdiff
path: root/configure.in
blob: 7b89bbce48ae41109f6b775b3e9599fad0f68922 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

# This file is part of Hercules.
# http://herc.ws - http://github.com/HerculesWS/Hercules
#
# Copyright (C) 2012-2015  Hercules Dev Team
# Copyright (C)  Athena Dev Teams
#
# Hercules is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

AC_INIT(Hercules)
AC_REVISION([m4_esyscmd_s([type git >/dev/null 2>&1 && git describe --always 2>/dev/null || echo '(unknown version)'])])
AC_PREREQ([2.59])
AC_CONFIG_SRCDIR([src/common/cbasetypes.h])
AC_CONFIG_FILES([Makefile src/common/Makefile])
AC_CONFIG_FILES([3rdparty/mt19937ar/Makefile 3rdparty/libconfig/Makefile])
AC_CONFIG_FILES([src/char/Makefile src/login/Makefile])
AC_CONFIG_FILES([src/map/Makefile src/plugins/Makefile src/tool/Makefile])
AC_CONFIG_FILES([src/test/Makefile])
AC_CONFIG_FILES([tools/HPMHookGen/Makefile])

dnl AC_USE_SYSTEM_EXTENSIONS requires autoconf 2.60 or newer. Fall back to AC_GNU_SOURCE otherwise.
m4_ifdef([AC_USE_SYSTEM_EXTENSIONS],
	[AC_USE_SYSTEM_EXTENSIONS],
	[AC_GNU_SOURCE]
)

#
# Memory managers
#
AC_ARG_ENABLE(
	[manager],
	AC_HELP_STRING(
		[--enable-manager=ARG],
		[memory managers: no, builtin, memwatch, dmalloc, gcollect, bcheck (defaults to builtin)]
	),
	[
		enable_manager="$enableval"
		case $enableval in
			"no");;
			"builtin");;
			"memwatch");;
			"dmalloc");;
			"gcollect");;
			"bcheck");;
			*) AC_MSG_ERROR([[unknown memory manager '$enableval'... stopping]]);;
		esac
	],
	[enable_manager="builtin"]
)


#
# packetver
#
AC_ARG_ENABLE(
	[packetver],
	AC_HELP_STRING(
		[--enable-packetver=ARG],
		[Sets the PACKETVER define. (see src/common/mmo.h)]
	),
	[enable_packetver="$enableval"],
	[enable_packetver=""]
)


#
# packetver-RE
#
AC_ARG_ENABLE(
	[packetver-re],
	AC_HELP_STRING(
		[--enable-packetver-re],
		[Sets or unsets the PACKETVER_RE define - see src/common/mmo.h (currently disabled by default)]
	),
	[
		enable_packetver_re="$enableval"
		case $enableval in
			"no");;
			"yes");;
			*) AC_MSG_ERROR([[invalid argument --enable-packetver-re=$enableval... stopping]]);;
		esac
	],
	[enable_packetver_re="no"]
)


#
# Obfuscation keys
#
AC_ARG_WITH(
	[key1],
	AC_HELP_STRING(
		[--with-key1@<:@=ARG@:>@],
		[Set the first obfuscation key (ignored unless the other two are also specified)]
	),
	[
		obfuscationkey1="$( expr "0x$withval" : '0*x*\(0x@<:@A-Fa-f0-9@:>@\{8\}\)' )"
		if ! expr "x$obfuscationkey1" : 'x0x@<:@A-Fa-f0-9@:>@\{8\}' >/dev/null 2>&1; then
			obfuscationkey1=""
		fi
	],
	[
		obfuscationkey1=""
	]
)
AC_ARG_WITH(
	[key2],
	AC_HELP_STRING(
		[--with-key2@<:@=ARG@:>@],
		[Set the second obfuscation key (ignored unless the other two are also specified)]
	),
	[
		obfuscationkey2="$( expr "0x$withval" : '0*x*\(0x@<:@A-Fa-f0-9@:>@\{8\}\)' )"
		if ! expr "x$obfuscationkey2" : 'x0x@<:@A-Fa-f0-9@:>@\{8\}' >/dev/null 2>&1; then
			obfuscationkey2=""
		fi
	],
	[
		obfuscationkey2=""
	]
)
AC_ARG_WITH(
	[key3],
	AC_HELP_STRING(
		[--with-key3@<:@=ARG@:>@],
		[Set the third obfuscation key (ignored unless the other two are also specified)]
	),
	[
		obfuscationkey3="$( expr "0x$withval" : '0*x*\(0x@<:@A-Fa-f0-9@:>@\{8\}\)' )"
		if ! expr "x$obfuscationkey3" : 'x0x@<:@A-Fa-f0-9@:>@\{8\}' >/dev/null 2>&1; then
			obfuscationkey3=""
		fi
	],
	[
		obfuscationkey3=""
	]
)


#
# debug
#
AC_ARG_ENABLE(
	[debug],
	AC_HELP_STRING(
		[--enable-debug@<:@=ARG@:>@],
		[
			Compiles extra debug code. (yes by default)
			(available options: yes, no, gdb)
		]
	),
	[
		enable_debug="$enableval"
		case $enableval in
			"no");;
			"yes");;
			"gdb");;
			*) AC_MSG_ERROR([[invalid argument --enable-debug=$enableval... stopping]]);;
		esac
	],
	[enable_debug="yes"]
)

#
# Buildbot
#
AC_ARG_ENABLE(
	[buildbot],
	AC_HELP_STRING(
		[--enable-buildbot@<:@=ARG@:>@],
		[(available options: yes, no)]
	),
	[
		enable_buildbot="$enableval"
		case $enableval in
			"no");;
			"yes");;
			*) AC_MSG_ERROR([[invalid argument --enable-buildbot=$enableval... stopping]]);;
		esac
	],
	[enable_buildbot="no"]
)

#
# RDTSC as Tick Source 
#
AC_ARG_ENABLE(
	[rdtsc],
	AC_HELP_STRING(
		[--enable-rdtsc],
		[
			Uses rdtsc as timing source (disabled by default)
			Enable it when you've timing issues.

			(For example:  in conjunction with XEN or Other Virtualization mechanisms)
		
			Note:
				Please ensure that you've disabled dynamic CPU-Frequencys, such as power saving options.
				(On most modern Dedicated Servers cpufreq is preconfigured, see your distribution's
				 manual how to disable it).
				Furthermore, If your CPU has built-in CPU-Frequency scaling features (such as Intel's
				 SpeedStep(R)), do not enable this option. Recent CPUs (Intel Core or newer) guarantee
				 a fixed increment rate for their TSC, so it should be safe to use, but please doublecheck
				 the documentation of both your CPU and OS before enabling this option.
		]
	),
	[
		enable_rdtsc=1
	],
	[enable_rdtsc=0]
)

#
# Profiler
#
AC_ARG_ENABLE(
	[profiler],
	AC_HELP_STRING(
		[--enable-profiler=ARG],
		[Profilers: no, gprof (disabled by default)]
	),
	[
		enable_profiler="$enableval"
		case $enableval in
			"no");;
			"gprof");;
			*) AC_MSG_ERROR([[invalid argument --enable-profiler=$enableval... stopping]]);;
		esac
	],
	[enable_profiler="no"]
)


#
# 64bit
#
AC_ARG_ENABLE(
	[64bit],
	AC_HELP_STRING(
		[--disable-64bit],
		[
			Enforce 32bit output on x86_64 systems.
		]
	),
	[
		enable_64bit="$enableval"
		case $enableval in
			"no");;
			"yes");;
			*) AC_MSG_ERROR([[invalid argument --enable-64bit=$enableval... stopping]]);;
		esac
	],
	[enable_64bit="yes"]
)


#
# LTO
#
AC_ARG_ENABLE(
	[lto],
	AC_HELP_STRING(
		[--enable-lto],
		[
			Enables or Disables Linktime Code Optimization (LTO is disabled by default)
		]
	),
	[
		enable_lto="$enableval"
		case $enableval in
			"no");;
			"yes");;
			*) AC_MSG_ERROR([[invalid argument --enable-lto=$disableval... stopping]]);;
		esac
	],
	[enable_lto="no"]
)


#
# static linking
#
AC_ARG_ENABLE(
	[static],
	AC_HELP_STRING(
		[--enable-static],
		[
			Enables or Disables Statick Linking (STATIC is disabled by default)
		]
	),
	[
		enable_static="$enableval"
		case $enableval in
			"no");;
			"yes");;
			*) AC_MSG_ERROR([[invalid argument --enable-static=$disableval... stopping]]);;
		esac
	],
	[enable_static="no"]
)


#
# adress sanitize
#
AC_ARG_ENABLE(
	[sanitize],
	AC_HELP_STRING(
		[--enable-sanitize@<:@=ARG@:>@],
		[
			Enables sanitizer. (disabled by default)
			(available options: yes, no, full)
		]
	),
	[
		enable_sanitize="$enableval"
		case $enableval in
			"no");;
			"yes");;
			"full");;
			*) AC_MSG_ERROR([[invalid argument --enable-sanitize=$disableval... stopping]]);;
		esac
	],
	[enable_sanitize="no"]
)


#
# Renewal
#
AC_ARG_ENABLE(
	[renewal],
	AC_HELP_STRING(
		[--disable-renewal],
		[
			Disable Ragnarok Renewal support (override settings in src/config/renewal.h)
		]
	),
	[
		enable_renewal="$enableval"
		case $enableval in
			"no");;
			"yes");;
			*) AC_MSG_ERROR([[invalid argument --enable-renewal=$enableval... stopping]]);;
		esac
	],
	[enable_renewal="yes"]
)


#
# Optionally set the max number of network conenctions
# the core will be support
#
AC_ARG_WITH(
	[maxconn],
	AC_HELP_STRING(
		[--with-maxconn@<:@=ARG@:>@],
		[optionally set the maximum connections the core can handle (default: 16384) NOT USED YET - EXPERIMENTAL]
	),
	[
		if test "$withval" == "no";	 then
			CPPFLAGS="$CPPFLAGS -DMAXCONN=16384"
		else
			
			if ! test "$withval" -ge 0 -o "$withval" -lt 0 2>&- ; then
				AC_MSG_ERROR([Invalid argument --with-maxconn=$withval ... stopping])
			else
				CPPFLAGS="$CPPFLAGS -DMAXCONN=$withval"
			fi
		fi
	],
	[
		CPPFLAGS="$CPPFLAGS -DMAXCONN=16384"
	]
)


#
# Optionally specify the path to mysql_config
#
AC_ARG_WITH(
	[mysql],
	AC_HELP_STRING(
		[--with-mysql@<:@=ARG@:>@],
		[optionally specify the path to the mysql_config executable]
	),
	[
		if test "$withval" != "no" ; then
			if test ! -x "$withval" ; then
				AC_MSG_ERROR([$withval is not an executable file])
			fi
			MYSQL_CONFIG_HOME="$withval"
		fi
	]
)


#
# Manual MYSQL_CFLAGS (optional)
#
AC_ARG_WITH(
	[MYSQL_CFLAGS],
	AC_HELP_STRING(
		[--with-MYSQL_CFLAGS=ARG],
		[specify MYSQL_CFLAGS manually (instead of using "mysql_config --include")]
	),
	[
		manual_MYSQL_CFLAGS="yes"
		MYSQL_CFLAGS="$withval"
	],
	[manual_MYSQL_CFLAGS="no"]
)


#
# Manual MYSQL_LIBS (optional)
#
AC_ARG_WITH(
	[MYSQL_LIBS],
	AC_HELP_STRING(
		[--with-MYSQL_LIBS=ARG],
		[specify MYSQL_LIBS manually (instead of using "mysql_config --libs")]
	),
	[
		manual_MYSQL_LIBS="yes"
		MYSQL_LIBS="$withval"
	],
	[manual_MYSQL_LIBS="no"]
)


#
# Specify the path of the zlib library (required library)
#
AC_ARG_WITH(
	[zlib],
	AC_HELP_STRING(
		[--with-zlib=DIR],
		[root directory path of zlib installation (defaults to /usr/local or /usr if not found in /usr/local).
		 Assumes that the header files are in DIR/include and the library files are in DIR/lib]
	),
	[
		test -n "$withval" && ZLIB_HOME="$withval"
	],
	[
		ZLIB_HOME=/usr/local
		test ! -f "${ZLIB_HOME}/include/zlib.h" && ZLIB_HOME=/usr
	]
)



###############################################################################
# Check for programs and types.
#



AC_PROG_MAKE_SET
AC_PROG_CC
AC_PROG_CPP
AC_PATH_PROG(AR, ar)
AC_LANG([C])


CFLAGS="$CFLAGS -pipe -ffast-math -fvisibility=hidden -Wall -Wextra -Wno-sign-compare"
CPPFLAGS="$CPPFLAGS -I../common"
LDFLAGS="$LDFLAGS"

AC_C_BIGENDIAN(
	[AC_MSG_ERROR([[bigendian is not supported... stopping]])],
	,
	[AC_MSG_WARN([[unable to determine endianess, only little endian is supported]])]
)


if test "$enable_64bit" = "no" ; then
	AC_MSG_CHECKING([whether pointers can be stored in ints (old code)])
	pointers_fit_in_ints="no"
	AC_COMPILE_IFELSE(
		[AC_LANG_PROGRAM([[static int test_array[((long int)sizeof(int)) == ((long int)sizeof(void*)) ? 1 : -1];]])],
		[pointers_fit_in_ints="yes"],
		[]
	)
	if test "$pointers_fit_in_ints" = "no" ; then
		CFLAGS="$CFLAGS -m32"
		LDFLAGS="$LDFLAGS -m32"
		AC_COMPILE_IFELSE(
			[AC_LANG_PROGRAM([[static int test_array[((long int)sizeof(int)) == ((long int)sizeof(void *)) ? 1 : -1];]])],
			[pointers_fit_in_ints="yes (with -m32)"],
			[]
		)
	fi
	AC_MSG_RESULT($pointers_fit_in_ints)
	if test "$pointers_fit_in_ints" = "no" ; then
		AC_MSG_ERROR([pointers cannot be stored in ints, required for old code... stopping])
	fi
fi


# 
# check if we're producing 32bit code - so well produce binarys for at least i686 (speedup: cmovs, and cmpchg8 support)
#
AC_MSG_CHECKING([whether $CC produces 32bit code])
AC_RUN_IFELSE(
	[AC_LANG_SOURCE([
		int main(int argc, char **argv){
			if(sizeof(void*) == 4) return 0;
			else return 1;
		}
	])],
	[
		AC_MSG_RESULT([yes])
		AC_MSG_CHECKING([whether $CC supports -march=i686 (and we can run it)])
		OLD_CFLAGS="$CFLAGS"
		OLD_LDFLAGS="$LDFLAGS"
		CFLAGS="$CFLAGS -march=i686"
		LDFLAGS="$LDFLAGS -march=i686"
		AC_RUN_IFELSE(
			[AC_LANG_SOURCE([
				int main(int argc, char **argv) { return 0; }
			])],
			[
				AC_MSG_RESULT([yes])
			],
			[
				AC_MSG_RESULT([no])
				CFLAGS="$OLD_CFLAGS"
				LDFLAGS="$OLD_LDFLAGS"
			],
			[
				AC_MSG_RESULT([guessing no])
				CFLAGS="$OLD_CFLAGS"
				LDFLAGS="$OLD_LDFLAGS"
			]
		)
	],
	[
		AC_MSG_RESULT([no])
	],
	[
		AC_MSG_RESULT([guessing no])
	]
)


# 
# Check if CC supports __thread attribute (Thread Local Storage) 
# (Usually our OSX friends 're lacking support of it in older llvm versions ..)
# 
AC_MSG_CHECKING([whether $CC supports __thread specifier (TLS)])
AC_RUN_IFELSE(
	[AC_LANG_SOURCE([
		__thread int g_Test = -1;

		int main(int argc, char **argv){
			g_Test = 0;
			return g_Test;		
		}
	])],
	[
		AC_MSG_RESULT([yes])
		CPPFLAGS="$CPPFLAGS -DHAS_TLS"
	],
	[
		AC_MSG_RESULT([no])
	]
)

#
# Check if the linker supports/accepts -rdynamic
# Generally only needed by the ELF linker, in order to produce backtraces.
# On non-ELF platforms, some compilers (i.e. gcc < 5 and clang on OSX) are able to ignore it, others will error out.
#
OLD_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS -rdynamic"
AC_MSG_CHECKING([whether $CC accepts -rdynamic])
AC_LINK_IFELSE(
	[AC_LANG_SOURCE([
		int main(int argc, char **argv)
		{
			return 0;
		}
	])],
	[
		AC_MSG_RESULT([yes])
	],
	[
		AC_MSG_RESULT([no])
		LDFLAGS="$OLD_LDFLAGS"
	],
	[
		AC_MSG_RESULT([guessing no])
	]
)

#
# LTO Support test
#
if test "$enable_lto" != "no" ; then

	OLD_CFLAGS="$CFLAGS"
	CFLAGS="$CFLAGS -flto"

	OLD_LDFLAGS="$LDFLAGS"
	LDFLAGS="$LDFLAGS -flto"

	AC_MSG_CHECKING([whether $CC supports -flto])
	AC_RUN_IFELSE(
		[AC_LANG_SOURCE([
			int main(int argc, char **argv){
				return 0;
			}
		])],
		[
			AC_MSG_RESULT([yes])
		],
		[
			AC_MSG_RESULT([no])
			CFLAGS="$OLD_CFLAGS"
			LDFLAGS="$OLD_LDFLAGS"
		],
		[
			AC_MSG_RESULT([guessing no])
		]
	)
fi

AC_DEFUN(AC_CHECK_SANITIZER_FLAG,
	[
		AC_MSG_CHECKING([whether $CC supports -fsanitize=$1])
		OLD_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS -fsanitize=$1"
		OLD_LDFLAGS="$LDFLAGS"
		LDFLAGS="$LDFLAGS -fsanitize=$1"
		AC_COMPILE_IFELSE(
			[AC_LANG_SOURCE([int foo;])],
			[
				AC_MSG_RESULT([yes])
			],
			[
				AC_MSG_RESULT([no])

				AC_MSG_CHECKING([whether $CC requires -fsanitize-undefined-trap-on-error for -fsanitize=$1])
				CFLAGS="$CFLAGS -fsanitize-undefined-trap-on-error"
				AC_COMPILE_IFELSE(
					[AC_LANG_SOURCE([int foo;])],
					[
						AC_MSG_RESULT([yes])
					],
					[
						AC_MSG_RESULT([no])
						CFLAGS="$OLD_CFLAGS"
						LDFLAGS="$OLD_LDFLAGS"
					]
				)
			],
			[
				AC_MSG_RESULT([guessing no])
				CFLAGS="$OLD_CFLAGS"
				LDFLAGS="$OLD_LDFLAGS"
			]
		)
	]
)

#
# sanitize Support test
#
if test "$enable_sanitize" != "no" ; then
	AC_CHECK_SANITIZER_FLAG(address)
	if test "$enable_sanitize" == "full" ; then
		# skipped because server have multiple issues: -fsanitize=alignment
		AC_CHECK_SANITIZER_FLAG(shift)
		AC_CHECK_SANITIZER_FLAG(integer-divide-by-zero)
		AC_CHECK_SANITIZER_FLAG(unreachable)
		AC_CHECK_SANITIZER_FLAG(vla-bound)
		AC_CHECK_SANITIZER_FLAG(null)
		AC_CHECK_SANITIZER_FLAG(return)
		AC_CHECK_SANITIZER_FLAG(signed-integer-overflow)
		AC_CHECK_SANITIZER_FLAG(bounds)
		AC_CHECK_SANITIZER_FLAG(object-size)
		AC_CHECK_SANITIZER_FLAG(float-divide-by-zero)
		AC_CHECK_SANITIZER_FLAG(float-cast-overflow)
		AC_CHECK_SANITIZER_FLAG(nonnull-attribute)
		AC_CHECK_SANITIZER_FLAG(returns-nonnull-attribute)
		AC_CHECK_SANITIZER_FLAG(bool)
		AC_CHECK_SANITIZER_FLAG(enum)
		AC_CHECK_SANITIZER_FLAG(vptr)
	fi
fi


AC_DEFUN(AC_CHECK_COMPILER_WFLAG,
	[
		AC_MSG_CHECKING([whether $CC supports -W$1])
		OLD_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS -Werror -W$1"
		AC_COMPILE_IFELSE(
			[AC_LANG_SOURCE([int foo;])],
			[
				AC_MSG_RESULT([yes])
				CFLAGS="$OLD_CFLAGS -W$1"
				# Optionally, run a test
				if test "x$2" != "x"; then
					AC_MSG_CHECKING([whether $CC can actually use -W$1])
					CFLAGS="$OLD_CFLAGS -Werror -W$1"
					AC_COMPILE_IFELSE(
						[AC_LANG_SOURCE([$2])],
						[
							AC_MSG_RESULT([no])
							CFLAGS="$OLD_CFLAGS"
						]
						[
							AC_MSG_RESULT([yes])
							CFLAGS="$OLD_CFLAGS -W$1"
						],
					)
				fi
			],
			[
				AC_MSG_RESULT([no])
				CFLAGS="$OLD_CFLAGS"
			]
		)
	]
)

AC_DEFUN(AC_CHECK_COMPILER_WNOFLAG,
	[
		AC_MSG_CHECKING([whether $CC supports -Wno-$1])
		OLD_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS -Werror -Wno-$1"
		AC_COMPILE_IFELSE(
			[AC_LANG_SOURCE([int foo;])],
			[
				# Recent versions of gcc don't fail if -Wno-foo is not recognized
				# (unless there are also other warnings), so we also check for -Wfoo
				# which always fails if not supported
				CFLAGS="$OLD_CFLAGS -Werror -W$1"
				AC_COMPILE_IFELSE(
					[AC_LANG_SOURCE([int foo;])],
					[
						AC_MSG_RESULT([yes])
						CFLAGS="$OLD_CFLAGS -Wno-$1"
						# Optionally, run a test
						if test "x$2" != "x"; then
							AC_MSG_CHECKING([whether $CC can actually use -Wno-$1])
							CFLAGS="$OLD_CFLAGS -Werror -W$1"
							AC_COMPILE_IFELSE(
								[AC_LANG_SOURCE([$2])],
								[
									AC_MSG_RESULT([not needed but enabled])
									CFLAGS="$OLD_CFLAGS"
								],
								[
									CFLAGS="$OLD_CFLAGS -Werror -Wno-$1"
									AC_COMPILE_IFELSE(
										[AC_LANG_SOURCE([$2])],
										[
											AC_MSG_RESULT([yes])
											CFLAGS="$OLD_CFLAGS -Wno-$1"
										],
										[
											AC_MSG_RESULT([no])
											CFLAGS="$OLD_CFLAGS"
										]
									)
								]
							)
						fi
					],
					[
						AC_MSG_RESULT([no])
						CFLAGS="$OLD_CFLAGS"
					]
				)
			],
			[
				AC_MSG_RESULT([no])
				CFLAGS="$OLD_CFLAGS"
			]
		)
	]
)

AC_CHECK_COMPILER_WNOFLAG(unused-parameter, [int foo(int bar) { return 0; }])
AC_CHECK_COMPILER_WNOFLAG(clobbered)
AC_CHECK_COMPILER_WFLAG(empty-body)
AC_CHECK_COMPILER_WFLAG(newline-eof)
AC_CHECK_COMPILER_WFLAG(int-conversion)
AC_CHECK_COMPILER_WFLAG(enum-conversion)
AC_CHECK_COMPILER_WFLAG(shorten-64-to-32)
AC_CHECK_COMPILER_WFLAG(constant-conversion)
AC_CHECK_COMPILER_WFLAG(bool-conversion)
AC_CHECK_COMPILER_WFLAG(format-security)
AC_CHECK_COMPILER_WNOFLAG(format-nonliteral)
AC_CHECK_COMPILER_WNOFLAG(switch)
AC_CHECK_COMPILER_WNOFLAG(missing-field-initializers)

# Certain versions of gcc make -Wshadow completely useless by making it flood
# you with unnecessary warnings <https://lkml.org/lkml/2006/11/28/239>
# Let's check if we can really use it
SAVED_OLD_CFLAGS="$CFLAGS"
AC_CHECK_COMPILER_WFLAG(shadow)
if test "x$CFLAGS" != "x$SAVED_OLD_CFLAGS"; then
	AC_MSG_CHECKING([whether $CC can efficiently use -Wshadow])
	NEW_CFLAGS="$CFLAGS"
	CFLAGS="$CFLAGS -Werror -Wshadow"
	AC_COMPILE_IFELSE(
		[AC_LANG_SOURCE([
			int foo(void) {
				return 0;
			}
			int bar(void) {
				int foo = 0;
				return foo + 1;
			}
		])],
		[
			AC_MSG_RESULT([yes])
			CFLAGS="$NEW_CFLAGS"
		],
		[
			AC_MSG_RESULT([no])
			CFLAGS="$SAVED_OLD_CFLAGS"
		]
	)
fi

AC_MSG_CHECKING([whether $CC supports -fPIC])
OLD_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -fPIC"
AC_COMPILE_IFELSE(
	[AC_LANG_SOURCE([int foo;])],
	[
		AC_MSG_RESULT([yes])
		SOFLAGS="$SOFLAGS -fPIC"
	],
	[
		AC_MSG_RESULT([no])
	]
)
CFLAGS="$OLD_CFLAGS"

#
# option for shared objects
#
AC_MSG_CHECKING([how to make shared objects])
OLD_CFLAGS="$CFLAGS"
compiler_shared_objects=""
compiler_supports_shared_objects="no"
my_shared_test_flags="$CFLAGS $SOFLAGS"
# default
CFLAGS="$my_shared_test_flags -shared"
AC_LINK_IFELSE(
	[AC_LANG_SOURCE([
		int bar = 0;

		int foo(void)
		{
			return bar;
		}
	])],
	[
		compiler_shared_objects="-shared"
		compiler_supports_shared_objects="yes"
	]
)
# BeOS specific
CFLAGS="$my_shared_test_flags -nostart"
AC_LINK_IFELSE(
	[AC_LANG_SOURCE([
		int bar = 0;

		int foo(void)
		{
			return bar;
		}
	])],
	[
		compiler_shared_objects="-nostart"
		compiler_supports_shared_objects="yes"
	]
)
CFLAGS="$OLD_CFLAGS"
if test "$compiler_supports_shared_objects" = "no" ; then
	AC_MSG_RESULT([not supported])
	AC_MSG_NOTICE([compiler is unable to generate shared objects, disabled plugins (optional)])
	WITH_PLUGINS="no"
else
	AC_MSG_RESULT([$compiler_shared_objects $compiler_supports_shared_objects])
	SOFLAGS="$SOFLAGS $compiler_shared_objects"
	WITH_PLUGINS="yes"

	#
	# On certain platforms, undefined references on shared libraries won't be checked
	# unless explicitly required with the --no-undefined linker option
	#
	AC_MSG_CHECKING([whether $CC needs -Wl,--no-undefined to check for undefined references in shared objects])
	OLD_CFLAGS="$CFLAGS"
	CFLAGS="$SOFLAGS"
	AC_LINK_IFELSE(
		[AC_LANG_SOURCE([
			void foo(void) {
				foobar();
			}
		])],
		[
			CFLAGS="$SOFLAGS -Wl,--no-undefined"
			AC_LINK_IFELSE(
				[AC_LANG_SOURCE([
					int bar = 0;

					int foo(void) {
						return bar;
					}
				])],
				[
					AC_MSG_RESULT([yes])
					SOFLAGS="$SOFLAGS -Wl,--no-undefined"
				],
				[
					AC_MSG_RESULT([unsupported (undefined references check will be ignored)])
				]
			)
		],
		[
			AC_MSG_RESULT([no])
		]
	)
	CFLAGS="$OLD_CFLAGS"
fi
my_shared_test_flags=""
AC_SUBST([WITH_PLUGINS])
AC_SUBST([SOFLAGS])


#
# -O2 implies -fstrict-aliasing, but the code is not safe for that
#
AC_MSG_CHECKING([whether $CC supports -fno-strict-aliasing])
OLD_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -fno-strict-aliasing"
AC_COMPILE_IFELSE(
	[AC_LANG_SOURCE([int foo;])],
	[AC_MSG_RESULT([yes])],
	[
		AC_MSG_RESULT([no])
		CFLAGS="$OLD_CFLAGS"
	]
)


###############################################################################
# Check for libraries and header files.
#


#
# setrlimit - used to set the socket limit
#
AC_CHECK_FUNC([setrlimit],[CPPFLAGS="$CPPFLAGS -DHAVE_SETRLIMIT"])


#
# strnlen - string length with upper scan bound
#
AC_CHECK_FUNC([strnlen],[CPPFLAGS="$CPPFLAGS -DHAVE_STRNLEN"])

# libconfig
AC_CHECK_FUNCS([uselocale])
AC_CHECK_FUNCS([newlocale])
AC_CHECK_FUNCS([freelocale])
AC_CHECK_HEADERS([xlocale.h])

#
# Memory manager
#
case $enable_manager in
	"no")
		CPPFLAGS="$CPPFLAGS -DNO_MEMMGR"
		;;
	"builtin")
		# enabled by default
		;;
	"memwatch")
		CPPFLAGS="$CPPFLAGS -DMEMWATCH"
		AC_CHECK_HEADER([memwatch.h], , [AC_MSG_ERROR([memwatch header not found... stopping])])
		;;
	"dmalloc")
		CPPFLAGS="$CPPFLAGS -DDMALLOC -DDMALLOC_FUNC_CHECK"
		LIBS="$LIBS -ldmalloc"
		AC_CHECK_HEADER([dmalloc.h], , [AC_MSG_ERROR([dmalloc header not found... stopping])])
		;;
	"gcollect")
		CPPFLAGS="$CPPFLAGS -DGCOLLECT"
		LIBS="$LIBS -lgc"
		AC_CHECK_HEADER([gc.h], , [AC_MSG_ERROR([gcollect header not found... stopping])])
		;;
	"bcheck")
		CPPFLAGS="$CPPFLAGS -DBCHECK"
		;;
esac


#
# Packetver
#
if test -n "$enable_packetver" ; then
	CPPFLAGS="$CPPFLAGS -DPACKETVER=$enable_packetver"
fi

#
# Packetver-RE
#
case $enable_packetver_re in
	"yes")
		CPPFLAGS="$CPPFLAGS -DENABLE_PACKETVER_RE"
		;;
	"no")
		# default value
		;;
esac

#
# Obfuscation keys
#
if test -n "$obfuscationkey1" -a -n "$obfuscationkey2" -a -n "$obfuscationkey3"; then
	CPPFLAGS="$CPPFLAGS -DOBFUSCATIONKEY1=$obfuscationkey1 -DOBFUSCATIONKEY2=$obfuscationkey2 -DOBFUSCATIONKEY3=$obfuscationkey3"
fi

#
# Debug
#
case $enable_debug in
	"no")
		# default value
#		CFLAGS="$CFLAGS -Wno-unused -Wno-parentheses"
		;;
	"yes")
		CFLAGS="$CFLAGS -g"
		CPPFLAGS="$CPPFLAGS -DDEBUG"
		;;
	"gdb")
		CFLAGS="$CFLAGS -ggdb"
		CPPFLAGS="$CPPFLAGS -DDEBUG"
		;;
esac

#
# Buildbot
#
case $enable_buildbot in
	"no")
		# default value
		;;
	"yes")
		CPPFLAGS="$CPPFLAGS -DBUILDBOT"
		;;
esac

#
# RDTSC
#
case $enable_rdtsc in
	0)
		#default value
		;;
	1)
		CPPFLAGS="$CPPFLAGS -DENABLE_RDTSC"
		;;
esac


#
# Profiler
#
case $enable_profiler in
	"no")
		# default value
		;;
	"gprof")
		CFLAGS="$CFLAGS -pg"
		LDFLAGS="$LDFLAGS -pg"
		;;
esac

#
# Renewal
#
case $enable_renewal in
	"no")
		CPPFLAGS="$CPPFLAGS -DDISABLE_RENEWAL"
		;;
	"yes")
		# default value
		;;
esac

#
# Host specific stuff
#
AC_MSG_CHECKING([host OS])
host_os="`uname`"
AC_MSG_RESULT([$host_os])
fd_setsize=""
DLLEXT=".so"
case $host_os in
Solaris* )
	LIBS="$LIBS -lsocket -lnsl -ldl"
	;;
Linux* )
	LIBS="$LIBS -ldl"
	;;
FreeBSD*)
	CPPFLAGS="$CPPFLAGS -D__FREEBSD__"
	;;
NetBSD*)
	CPPFLAGS="$CPPFLAGS -D__NETBSD__"
	;;
CYGWIN*)
	CPPFLAGS="$CPPFLAGS -DFD_SETSIZE=4096 -DCYGWIN"
	fd_setsize="done"
	DLLEXT=".dll"
	;;
Darwin*)
	DLLEXT=".dylib"
	;;
esac
AC_SUBST([DLLEXT])

AC_MSG_CHECKING([for MinGW])
if test -n "`$CC --version 2>/dev/null | grep -i mingw`" ; then
	AC_MSG_RESULT([yes])
	CPPFLAGS="$CPPFLAGS -DMINGW"
	if test -z "$fd_setsize" ; then
		CPPFLAGS="$CPPFLAGS -DFD_SETSIZE=4096"
	fi
	LIBS="$LIBS -lws2_32"
else
	AC_MSG_RESULT([no])
fi



#
# zlib library (required)
#
if test -n "${ZLIB_HOME}" ; then
	LDFLAGS="$LDFLAGS -L${ZLIB_HOME}/lib"
	CPPFLAGS="$CPPFLAGS -I${ZLIB_HOME}/include"
fi
AC_SEARCH_LIBS([inflateEnd], [z], [],[AC_MSG_ERROR([zlib library not found or incompatible, please specify the correct path with --with-zlib=DIR... stopping])])
AC_CHECK_HEADER([zlib.h], [], [AC_MSG_ERROR([zlib header not found, please specify the correct path with --with-zlib=DIR... stopping])])


#
# math library (required)
#
AC_SEARCH_LIBS([sqrt], [m], [], [AC_MSG_ERROR([math library not found... stopping])])


#
# clock_gettime (optional, rt on Debian)
#
AC_SEARCH_LIBS([clock_gettime], [rt])


#
# CLOCK_MONOTONIC clock for clock_gettime
# Normally defines _POSIX_TIMERS > 0 and _POSIX_MONOTONIC_CLOCK (for posix
# compliant systems) and __FreeBSD_cc_version >= 500005 (for FreeBSD
# >= 5.1.0, which does not have the posix defines (ref. r11983)) would be
# checked but some systems define them even when they do not support it
# (ref. bugreport:1003).
#
if test "$ac_cv_search_clock_gettime" != "no" ; then
	AC_MSG_CHECKING([whether CLOCK_MONOTONIC is supported and works])
	AC_RUN_IFELSE(
		[AC_LANG_SOURCE([
			#include <sys/time.h>
			#include <time.h>
			#include <unistd.h>
			
			int main(int argc, char** argv)
			{
				struct timespec tval;
				return clock_gettime(CLOCK_MONOTONIC, &tval);
			}
		])],
		[
			AC_MSG_RESULT([yes])
			CPPFLAGS="$CPPFLAGS -DHAVE_MONOTONIC_CLOCK"
		],
		[
			# either it failed to compile (CLOCK_MONOTONIC undefined)
			# or clock_gettime has returned a non-zero value
			AC_MSG_RESULT([no])
		],
		[
			AC_MSG_RESULT([guessing no])
		]
	)
fi


#
# pthread
#
AC_SEARCH_LIBS([pthread_create], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])
AC_SEARCH_LIBS([pthread_sigmask], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])
AC_SEARCH_LIBS([pthread_attr_init], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])
AC_SEARCH_LIBS([pthread_attr_setstacksize], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])
AC_SEARCH_LIBS([pthread_attr_destroy], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])
AC_SEARCH_LIBS([pthread_cancel], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])
AC_SEARCH_LIBS([pthread_join], [pthread], [], [AC_MSG_ERROR([pthread library not found or incompatible])])

# execinfo (backtrace)
AC_CHECK_HEADERS([execinfo.h],
	[
		AC_SEARCH_LIBS([backtrace],
			[execinfo],
			[
				CFLAGS="$CFLAGS -DHAVE_EXECINFO"
			],
			[
			]
		)
	],
	[
	]
)

#
# MySQL library
#

if test -z "$MYSQL_CONFIG_HOME"; then
	AC_PATH_PROG([MYSQL_CONFIG_HOME], [mysql_config], [no])
fi

if test "$MYSQL_CONFIG_HOME" != "no" ; then
	MYSQL_VERSION="`$MYSQL_CONFIG_HOME --version`"
	if test "$manual_MYSQL_CFLAGS" = "no" ; then
		MYSQL_CFLAGS="`$MYSQL_CONFIG_HOME --include`"
	fi
	if test "$manual_MYSQL_LIBS" = "no" ; then
		MYSQL_LIBS="`$MYSQL_CONFIG_HOME --libs`"
	fi
else
	MYSQL_VERSION="unknown"
fi

MYSQL_OLD_LIBS="$LIBS" ; LIBS="$LIBS $MYSQL_LIBS"
MYSQL_OLD_CPPFLAGS="$CPPFLAGS" ; CPPFLAGS="$CPPFLAGS $MYSQL_CFLAGS"
AC_CHECK_FUNC([mysql_init], [HAVE_MYSQL="yes"])
AC_CHECK_HEADER([mysql.h], [], [HAVE_MYSQL=""])
CPPFLAGS="$MYSQL_OLD_CPPFLAGS"
LIBS="$MYSQL_OLD_LIBS"

AC_MSG_CHECKING([MySQL library (required)])
if test "$HAVE_MYSQL" = "yes" ; then
	AC_MSG_RESULT([yes ($MYSQL_VERSION)])
else
	AC_MSG_RESULT([no])
	AC_MSG_ERROR([MySQL not found or incompatible])
fi

AC_SUBST([HAVE_MYSQL])
AC_SUBST([MYSQL_VERSION])
AC_SUBST([MYSQL_CFLAGS])
AC_SUBST([MYSQL_LIBS])


#
# PCRE library (optional)
#

AC_MSG_CHECKING([PCRE library])
AC_CHECK_LIB(pcre, pcre_study, , AC_MSG_ERROR([PCRE not found or incompatible]))
CFLAGS="$CFLAGS -DPCRE_SUPPORT"

#
# static Support test
#
if test "$enable_static" != "no" ; then

	STATIC="-static"
	PLUGINSTATIC="-static-libgcc"

	OLD_CFLAGS="$CFLAGS"
	OLD_LDFLAGS="$LDFLAGS"
	OLD_LIBS="$LIBS"

	CFLAGS="$OLD_CFLAGS $MYSQL_CFLAGS -static"
	LDFLAGS="$OLD_LDFLAGS -static"
	LIBS="$OLD_LIBS $MYSQL_LIBS"

	AC_MSG_CHECKING([whether $CC supports -static])
	AC_RUN_IFELSE(
		[AC_LANG_SOURCE([
			int main(int argc, char **argv){
				return 0;
			}
		])],
		[
			AC_MSG_RESULT([yes])
		],
		[
			AC_MSG_RESULT([no])
			STATIC=""
		],
		[
			AC_MSG_RESULT([guessing no])
			STATIC=""
		]
	)
	CFLAGS="$OLD_CFLAGS -static-libgcc"
	LDFLAGS="$OLD_LDFLAGS -static-libgcc"

	AC_MSG_CHECKING([whether $CC supports -static-libgcc])
	AC_RUN_IFELSE(
		[AC_LANG_SOURCE([
			int main(int argc, char **argv){
				return 0;
			}
		])],
		[
			AC_MSG_RESULT([yes])
		],
		[
			AC_MSG_RESULT([no])
			PLUGINSTATIC=""
		],
		[
			AC_MSG_RESULT([guessing no])
			PLUGINSTATIC=""
		]
	)

	CFLAGS="$OLD_CFLAGS"
	LDFLAGS="$OLD_LDFLAGS"
	LIBS="$OLD_LIBS"
fi

AC_SUBST(STATIC, ${STATIC})
AC_SUBST(PLUGINSTATIC, ${PLUGINSTATIC})



#
# Doxygen, perl (for HPMHookGen)
#

AC_CHECK_PROG([HAVE_DOXYGEN],[doxygen],[yes],[no])

AC_CHECK_PROG([HAVE_PERL],[perl],[yes],[no])

###############################################################################
AC_OUTPUT