summaryrefslogtreecommitdiffstats
path: root/CMakeLists.txt
blob: 707e9913d9c706d4bfd29cd3588b85b709050ef3 (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
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
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
cmake_minimum_required(VERSION 2.8.4)

# TODO:
# - Have a look at PGO:
# https://mailman.videolan.org/pipermail/x265-devel/2015-April/007399.html

cmake_policy(PUSH)
if(POLICY CMP0042)
	# new behavior defaults to ON for MACOSX_RPATH
	cmake_policy(SET CMP0042 NEW)
endif()
if(POLICY CMP0045)
	# Error on non-existent target in get_target_property, 
	# set to old as we actually use it to check for specific targets
	cmake_policy(SET CMP0045 OLD)
endif()
if(POLICY CMP0046)
	# Error on non-existent dependency in add_dependencies
	cmake_policy(SET CMP0046 NEW)
endif()
if(POLICY CMP0054)
	# Escape variables in if
	cmake_policy(SET CMP0054 OLD)
endif()
if(POLICY CMP0059)
	# Don't treat ``DEFINITIONS`` as a built-in directory property - required for cotire
	cmake_policy(SET CMP0059 OLD)
endif()

# specify USCXML version
SET(USCXML_VERSION_MAJOR "0")
SET(USCXML_VERSION_MINOR "4")
SET(USCXML_VERSION_PATCH "0")
SET(USCXML_VERSION ${USCXML_VERSION_MAJOR}.${USCXML_VERSION_MINOR}.${USCXML_VERSION_PATCH})

# build type has to be set before the project definition
SET(BUILD_TYPE_HELP "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug, Release, RelWithDebInfo, MinSizeRel.")
IF(DEFINED CMAKE_BUILD_TYPE)
   SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING ${BUILD_TYPE_HELP})
ELSE()
   SET(CMAKE_BUILD_TYPE Release CACHE STRING ${BUILD_TYPE_HELP})
ENDIF()

project(uscxml)

# where to find the cmake modules we distribute
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/contrib/cmake)
include("${CMAKE_MODULE_PATH}/USCXMLMacros.cmake")
include("${CMAKE_MODULE_PATH}/FunctionExists.cmake")
include("${CMAKE_MODULE_PATH}/HeaderExists.cmake")
include("${CMAKE_MODULE_PATH}/BinaryExists.cmake")
include("${CMAKE_MODULE_PATH}/TryCompile.cmake")

# CMake 2.8.11 reports AMD64 for Windows 64Bit, where earlier versions reported x86
# we resolve it with a 64bit check later
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
  set(CMAKE_SYSTEM_PROCESSOR "x86")
endif()
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  set(CMAKE_SYSTEM_PROCESSOR "x86")
endif()
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  set(CMAKE_SYSTEM_PROCESSOR "x86")
endif()

# use folders in the IDEs for the various targets (e.g. Library, Testing, Tools ..)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# lower case version of system name and compiler for library paths
string(TOLOWER ${CMAKE_SYSTEM_NAME} CMAKE_SYSTEM_NAME_LC)
string(TOLOWER ${CMAKE_CXX_COMPILER_ID} CMAKE_CXX_COMPILER_ID_LC)

if (WIN32)
	set(CMAKE_CXX_COMPILER_ID_LC ${CMAKE_CXX_COMPILER_ID_LC}${MSVC_VERSION})
endif()

############################################################
# Search paths for cross compiling and prebuilds
############################################################

# this happens when we ssh into windows to build there, see also:
# http://publib.boulder.ibm.com/infocenter/wxdinfo/v6r1/index.jsp?topic=/com.ibm.websphere.ops.doc/info/odoe_task/tcimgr_sshwin.html
if (NOT CMAKE_SYSTEM_PROCESSOR)
	set(CMAKE_SYSTEM_PROCESSOR x86)
endif()

# is this a 64Bit host?
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	set(64BIT_HOST ON)
	set(64BIT_LIB_POSTFIX 64)
	set(64BIT_SUFFIX "_64")
	# additional library paths to be searched
	set(FIND_LIBRARY_USE_LIB64_PATHS ON)
# 	LIST(APPEND CMAKE_FIND_ROOT_PATH "/usr/local/lib64")
# 	LIST(APPEND CMAKE_FIND_ROOT_PATH "/usr/lib64")
endif()

if (APPLE)
	set(LIBCPP_NAME "libstdc++") # before mavericks
	# get MacOSX version
	execute_process(COMMAND /usr/bin/sw_vers -productVersion
		OUTPUT_VARIABLE MACOSX_VERSION
		ERROR_VARIABLE MACOSX_VERSION_errors
		RESULT_VARIABLE MACOSX_VERSION_result
	OUTPUT_STRIP_TRAILING_WHITESPACE)
	if (MACOSX_VERSION)
		THREE_PART_VERSION_TO_VARS(
			${MACOSX_VERSION}
			MACOSX_VERSION_MAJOR 
			MACOSX_VERSION_MINOR 
			MACOSX_VERSION_PATCH)
	endif()
	if (MACOSX_VERSION VERSION_GREATER "10.8.99")
		set(LIBCPP_NAME "libc++") # mavericks
		# LIST(APPEND CMAKE_FIND_ROOT_PATH ${CMAKE_OSX_SYSROOT})
		# make sure that we find libxml2 here first
		set(CMAKE_FIND_ROOT_PATH ${CMAKE_OSX_SYSROOT} ${CMAKE_FIND_ROOT_PATH})
	endif()
endif()

if (NOT $ENV{MACOSX_DEPLOYMENT_TARGET} STREQUAL "" 
    AND ENV{MACOSX_DEPLOYMENT_TARGET} VERSION_LESS "10.9" 
    AND MACOSX_VERSION VERSION_GREATER "10.8.99")
  message(WARNING "\nMACOSX_DEPLOYMENT_TARGET is set to $ENV{MACOSX_DEPLOYMENT_TARGET} implying libstdc++ instead of libc++ - linking against prebuilts from 10.8")
  set(MACOSX_VERSION_MINOR 8)
endif()

# We use the toolchain file from http://code.google.com/p/android-cmake/
if (CMAKE_CROSSCOMPILING AND ANDROID_ABI)
	set(ANDROID ON)
	set(CMAKE_CROSSCOMPILING_TARGET android)
	set(CMAKE_SYSTEM_VERSION ${ANDROID_NATIVE_API_LEVEL})
endif()

if (CMAKE_CROSSCOMPILING)
	if (IOS)
		add_definitions(-DTARGET_OS_IPHONE)
	elseif(IOSSIM)
		add_definitions(-DTARGET_IPHONE_SIMULATOR)
	endif()
endif()

#
# Setting the CMAKE_FIND_ROOT_PATH to a list of directories will cause all CMake modules
# to look in these directories in addition to the system search paths:
# http://www.vtk.org/Wiki/CMake_Cross_Compiling#Searching_and_finding_external_software
#


#
# determine platform id
#
SET(USCXML_PLATFORM_ID)
if (CMAKE_SYSTEM_NAME_LC)
	set(USCXML_PLATFORM_ID ${CMAKE_SYSTEM_NAME_LC})
else()
	set(USCXML_PLATFORM_ID "unknown")
endif()

if (CMAKE_SYSTEM_PROCESSOR)
	set(USCXML_PLATFORM_ID "${USCXML_PLATFORM_ID}-${CMAKE_SYSTEM_PROCESSOR}${64BIT_SUFFIX}")
else()
	set(USCXML_PLATFORM_ID "unknown")
endif()

if (CMAKE_CXX_COMPILER_ID_LC)
	set(USCXML_PLATFORM_ID "${USCXML_PLATFORM_ID}-${CMAKE_CXX_COMPILER_ID_LC}")
endif()

if (LIBCPP_NAME)
	set(USCXML_PLATFORM_ID "${USCXML_PLATFORM_ID}-${LIBCPP_NAME}")
endif()

# handle cross compiles
if (CMAKE_CROSSCOMPILING)
	if (IOS)
		SET(USCXML_PLATFORM_ID "ios-arm-clang")
		SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "ONLY")
	elseif (ANDROID)
		# handle mips, x86 and arm
		SET(USCXML_PLATFORM_ID "android-${ANDROID_ABI}")
		
		SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "ONLY") # we need both to find jni - we don't?
		SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM "BOTH")
		SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE "BOTH")
		
		LIST(APPEND CMAKE_PREFIX_PATH "/usr/local/bin") # this is needed to find swig
		LIST(APPEND CMAKE_PREFIX_PATH "/opt/local/bin") # this is needed to find protoc
	endif()
endif()

SET(USCXML_PREBUILT_LIBRARY_PATH)
SET(USCXML_PREBUILT_HEADER_PATH  "${PROJECT_SOURCE_DIR}/contrib/prebuilt")
SET(USCXML_PREBUILT_LIBRARY_PATH "${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}")

SET(USCXML_LIBRARY_HOST_URL_PREFIX "http://uscxml.tk.informatik.tu-darmstadt.de/prebuilt" CACHE STRING "The root path of an URL where to look for prebuilt libraries." FORCE)
SET(USCXML_LIBRARY_ARCHIVE_NAME "uscxml-prebuilt-${USCXML_PLATFORM_ID}.tgz")

#
# Download platform independent headers
#

# delete if too old or unversioned
if (EXISTS "${PROJECT_SOURCE_DIR}/contrib/prebuilt/include/VERSION.txt")
	file (STRINGS "${PROJECT_SOURCE_DIR}/contrib/prebuilt/include/VERSION.txt" PREBUILT_INCUDES_VERSION)
endif()
if (NOT "${USCXML_VERSION}" VERSION_EQUAL "${PREBUILT_INCUDES_VERSION}")
	message(STATUS "Prebuilt headers unversioned, too old or non-existent - deleting and downloading again")
	execute_process(
		COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_SOURCE_DIR}/contrib/prebuilt/include
	)
endif()

# download if not existing
if (NOT EXISTS ${PROJECT_SOURCE_DIR}/contrib/prebuilt/include)
	message(STATUS "Downloading ${USCXML_LIBRARY_HOST_URL_PREFIX}/${USCXML_VERSION}/include.tgz")

	file(
		DOWNLOAD 
			${USCXML_LIBRARY_HOST_URL_PREFIX}/${USCXML_VERSION}/include.tgz
			${PROJECT_SOURCE_DIR}/contrib/prebuilt/include.tgz
		INACTIVITY_TIMEOUT 60 
		STATUS DOWNLOAD_STATUS 
		SHOW_PROGRESS)
		
	list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
	list(GET DOWNLOAD_STATUS 1 STATUS_STRING)
	if(STATUS_CODE EQUAL 0)
		execute_process(
			COMMAND ${CMAKE_COMMAND} -E tar xzf ${PROJECT_SOURCE_DIR}/contrib/prebuilt/include.tgz
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/
		)
		execute_process(
			COMMAND ${CMAKE_COMMAND} -E remove ${PROJECT_SOURCE_DIR}/contrib/prebuilt/include.tgz
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/
		)
		file(WRITE ${PROJECT_SOURCE_DIR}/contrib/prebuilt/include/VERSION.txt "${USCXML_VERSION}")
	else()
		message(FATAL_ERROR "\nDownloading header files\nFAILED with ${STATUS_STRING} (${STATUS_CODE}) \nThis should not happen, retry?")
	endif()
endif()

#
# Download platform dependent headers and libraries
#

# only delete if too old
if (EXISTS "${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}/VERSION.txt")
	file (STRINGS "${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}/VERSION.txt" PREBUILT_LIBRARIES_VERSION)
	if (NOT "${USCXML_VERSION}" VERSION_EQUAL "${PREBUILT_LIBRARIES_VERSION}")
		message(STATUS "Prebuilt libraries unversioned, too old or non-existent - deleting and downloading again")
		execute_process(
			COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}
		)
	endif()
endif()


if (NOT EXISTS ${USCXML_PREBUILT_LIBRARY_PATH})
	message(STATUS "Cannot find prebuilt libraries in ${USCXML_PREBUILT_LIBRARY_PATH}")
	message(STATUS "Trying to download ${USCXML_LIBRARY_HOST_URL_PREFIX}/${USCXML_VERSION}/${USCXML_LIBRARY_ARCHIVE_NAME}")
	file(
		DOWNLOAD 
			${USCXML_LIBRARY_HOST_URL_PREFIX}/${USCXML_VERSION}/${USCXML_LIBRARY_ARCHIVE_NAME}
			${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_LIBRARY_ARCHIVE_NAME}
		INACTIVITY_TIMEOUT 60 
		STATUS DOWNLOAD_STATUS 
		SHOW_PROGRESS)
	
	list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
	list(GET DOWNLOAD_STATUS 1 STATUS_STRING)
	if(STATUS_CODE EQUAL 0)
		# everything worked out fine! - create dir, unpack and delete
		file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID})
		execute_process(
			COMMAND ${CMAKE_COMMAND} -E tar xzf ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_LIBRARY_ARCHIVE_NAME}
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}
		)
		execute_process(
			COMMAND ${CMAKE_COMMAND} -E remove ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_LIBRARY_ARCHIVE_NAME}
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/
		)
		file(WRITE ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}/VERSION.txt "${USCXML_VERSION}")
	elseif(
		NOT STATUS_CODE EQUAL 19 AND   # FTP: Invalid file / file not found
		NOT STATUS_CODE EQUAL 22)      # HTTP: HTTP response code
		# server was unreachable, do not create dir to retry
		message(FATAL_ERROR "\nDownloading prebuilt libraries\nFAILED with ${STATUS_STRING} (${STATUS_CODE}) \nThis should not happen, retry?")
	else()
		# we did not know the file, create dir for manual libs
		file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID})
		execute_process(
			COMMAND ${CMAKE_COMMAND} -E remove ${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_LIBRARY_ARCHIVE_NAME}
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/contrib/prebuilt/
		)
		message(FATAL_ERROR "\nPlatform is not supported!\nProvide headers and libraries in\n${PROJECT_SOURCE_DIR}/contrib/prebuilt/${USCXML_PLATFORM_ID}/")
	endif()
endif()

if (CMAKE_CROSSCOMPILING)
	OPTION(BUILD_SHARED_LIBS "Build shared libraries" OFF)
	OPTION(ENABLE_COTIRE "Enable compile time reduction techniques" OFF)
else()
	OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON)
endif()

if (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
	OPTION(ENABLE_COTIRE "Enable compile time reduction techniques" OFF)
else()
	OPTION(ENABLE_COTIRE "Enable compile time reduction techniques" ON)
endif()

if (NOT BUILD_SHARED_LIBS)
	add_definitions("-DUSCXML_STATIC")
endif()

#
# BUILD_PREFER_PREBUILT_LIBS:
# Do we want to search system paths or contrib/prebuilt first?
#
if (CMAKE_CROSSCOMPILING)
	# always prefer prebuilt libraries for cross-compiling
	OPTION(BUILD_PREFER_PREBUILT_LIBS "Search libraries in contrib/prebuilt first" ON)
	SET(CMAKE_PREFIX_PATH "${USCXML_PREBUILT_LIBRARY_PATH};${USCXML_PREBUILT_HEADER_PATH};${CMAKE_PREFIX_PATH}")
else()
	OPTION(BUILD_PREFER_PREBUILT_LIBS "Search libraries in contrib/prebuilt first" ON)
	if (BUILD_PREFER_PREBUILT_LIBS)
		# for everything, we start our search in contrib/prebuilt
		SET(CMAKE_PREFIX_PATH "${USCXML_PREBUILT_LIBRARY_PATH};${USCXML_PREBUILT_HEADER_PATH};${CMAKE_PREFIX_PATH}")
	else()
		# using this trick, we search paths from find_* before CMAKE_FIND_ROOT_PATH as '/' is prepended first
		SET(CMAKE_PREFIX_PATH "/;${CMAKE_PREFIX_PATH};${USCXML_PREBUILT_LIBRARY_PATH};${USCXML_PREBUILT_HEADER_PATH}")
	endif()
endif()
include_directories(${USCXML_PREBUILT_LIBRARY_PATH}/include)

if (CMAKE_CROSSCOMPILING)
	set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${CMAKE_PREFIX_PATH})
endif()
# message(STATUS "CMAKE_FIND_ROOT_PATH: ${CMAKE_FIND_ROOT_PATH}")
# message(STATUS "CMAKE_PREFIX_PATH   : ${CMAKE_PREFIX_PATH}")
# message(FATAL_ERROR "")

if (WIN32)
	set(BUILD_SHARED_LIBS OFF)
	include_directories(${PROJECT_SOURCE_DIR}/contrib/src/getopt)
	include_directories(${PROJECT_SOURCE_DIR}/contrib/src/inttypes)
endif()
include_directories(${PROJECT_SOURCE_DIR}/contrib/src/jsmn)
include_directories(${PROJECT_SOURCE_DIR}/contrib/src/evws)
#include_directories(${PROJECT_SOURCE_DIR}/contrib/src/google-url)

############################################################
# General setup
############################################################

# enable testing and actual output with tests
if (CMAKE_CROSSCOMPILING)
	OPTION(BUILD_TESTS "Build USCXML tests" OFF)
else()
	OPTION(BUILD_TESTS "Build USCXML tests" ON)
	OPTION(BUILD_TESTS_W3C_ECMA "Create W3C ECMAScript tests" ON)
	OPTION(BUILD_TESTS_W3C_XPATH "Create W3C XPath tests" ON)
	OPTION(BUILD_TESTS_W3C_LUA "Create W3C Lua tests" ON)
	OPTION(BUILD_TESTS_W3C_PROLOG "Create W3C Prolog tests" ON)
	OPTION(BUILD_TESTS_W3C_PROMELA "Create W3C Promela tests" ON)
	
	OPTION(BUILD_TESTS_FSM "Build FSM converted W3C tests" OFF)
	OPTION(BUILD_TESTS_FSM_ECMA "Create FSM converted W3C ECMAScript tests" ON)
	OPTION(BUILD_TESTS_FSM_XPATH "Create FSM converted W3C XPath tests" ON)
	OPTION(BUILD_TESTS_FSM_LUA "Create FSM converted W3C Lua tests" ON)
	OPTION(BUILD_TESTS_FSM_PROLOG "Create FSM converted W3C Prolog tests" ON)
	OPTION(BUILD_TESTS_FSM_PROMELA "Create FSM converted W3C Promela tests" ON)
	OPTION(BUILD_TESTS_GENERATED_C "Create tests for generated C machines" ON)

endif()
OPTION(ENABLE_GCOV "Compile with gcov support" OFF)

OPTION(BUILD_PROFILING "Build with profiling information" OFF)
OPTION(BUILD_MINIMAL "Build only features mandated by specification" OFF)
OPTION(BUILD_DM_ECMA "Build with ECMAScript datamodel" ON)
OPTION(BUILD_DM_XPATH "Build with XPath datamodel" ON)
OPTION(BUILD_DM_PROLOG "Build with Prolog datamodel" ON)
OPTION(BUILD_DM_PROMELA "Build with Promela datamodel" ON)
OPTION(BUILD_DM_LUA "Build with Lua datamodel" ON)

OPTION(BUILD_BINDING_JAVA "Build language bindings for Java" ON)
OPTION(BUILD_BINDING_CSHARP "Build language bindings for CSharp" ON)
OPTION(BUILD_BINDING_PHP "Build language bindings for PHP" OFF)

# ccache:
# http://stackoverflow.com/questions/1815688/how-to-use-ccache-with-cmake
#
# see also:
# http://blogs.s-osg.org/a-conclusion-to-accelerating-your-build-with-clang/

find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif(CCACHE_FOUND)


# a dummy target to depend on the targets needed for tests, see:
# http://stackoverflow.com/questions/733475/cmake-ctest-make-test-doesnt-build-tests
add_custom_target(ALL_TESTS COMMENT "Building all tests when BUILD_TESTS is enabled")
if (BUILD_TESTS)
	enable_testing()
	SET(ENV{CTEST_OUTPUT_ON_FAILURE} ON)
endif()

OPTION(DIST_PREPARE "Put libraries into the lib folder of the source tree" OFF)

# we need USCXML_CORE_LIBS here for -lgcov
set(USCXML_CORE_LIBS)
set(USCXML_OPT_LIBS)
set(USCXML_FILES)
set(USCXML_TRANSFORM_FILES)
set(USCXML_INCLUDE_DIRS)

# some compiler flags
#message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")

if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
	execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)

	# best practices from scott meyers
	#	add_definitions(-Weffc++)

  # order of arguments of gcc matters again
#	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
	add_definitions(-fPIC)
	#add_definitions(-Wunreachable-code)

	if (CMAKE_BUILD_TYPE MATCHES ".*Deb.*" AND ENABLE_GCOV) # when not building for debug
		# coverage information
		add_definitions(-fprofile-arcs)
		add_definitions(-ftest-coverage)
		list (APPEND USCXML_CORE_LIBS "gcov")
	endif()

	# all warnings
	add_definitions(-Wall)
#	add_definitions(-Wno-parentheses-equality)
	add_definitions(-Wno-attributes)
	add_definitions(-Wno-unused-variable)

	# we cannot tread warning as error with the "narrowing problem"
	# add_definitions(-Werror)

	if(GCC_VERSION VERSION_EQUAL 4.7 OR GCC_VERSION VERSION_GREATER 4.7)
		# when compiling as C++11, narrowing is a warning but older compilers complain about the option
		add_definitions(-Wno-narrowing)
	endif()

	if(GCC_VERSION VERSION_EQUAL 4.8 OR GCC_VERSION VERSION_GREATER 4.8)
		add_definitions(-Wno-unused-local-typedefs)
	endif()

	# swig will throw a warning with optimization otherwise
	add_definitions(-fno-strict-aliasing)
	add_definitions("-Wno-unused-value -Wno-sign-compare")
#	add_definitions(-Wno-unused-function)
	if (NOT CMAKE_BUILD_TYPE MATCHES ".*Deb.*") # when not building for debug
		# add_definitions("-s")
    	#set(CMAKE_CXX_FLAGS "-s")  ## Strip binary for everything but debug builds
    	# set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections")
		# add_definitions("-mpreferred-stack-boundary=4")
		# add_definitions("-fmerge-constants")
		# add_definitions("-fno-rtti -DBOOST_NO_TYPEID")
		# add_definitions("-fno-exceptions")
		# add_definitions("-fno-inline")
		# add_definitions("-ffunction-sections -fdata-sections")
	else()
		add_definitions(-rdynamic)
	endif()
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
	add_definitions("-DZMQ_STATIC")
	add_definitions("-DPCRE_STATIC")
	add_definitions("-DUMUNDO_STATIC")
	add_definitions("-D_SCL_SECURE_NO_WARNINGS")
	add_definitions("-D_CRT_SECURE_NO_WARNINGS")
	add_definitions("/bigobj")

	# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL") # /LTCG is implied with /GL.

	# be very clear about linking debug / non-debug C++ runtimes
	foreach(FLAGS CMAKE_EXE_LINKER_FLAGS_RELEASE CMAKE_SHARED_LINKER_FLAGS_RELEASE CMAKE_MODULE_LINKER_FLAGS_RELEASE)
		set(${FLAGS} "${${FLAGS}} /ignore:4099 /NODEFAULTLIB:MSVCRTD")
	endforeach()

	# use static MSVCRT
	# if (NOT BUILD_AS_PLUGINS)
	# 	foreach(FLAGS 
	# 		CMAKE_C_FLAGS
	# 		CMAKE_CXX_FLAGS
	# 		CMAKE_C_FLAGS_DEBUG
	# 		CMAKE_C_FLAGS_MINSIZEREL
	# 		CMAKE_C_FLAGS_RELEASE
	# 		CMAKE_C_FLAGS_RELWITHDEBINFO
	# 		CMAKE_CXX_FLAGS_DEBUG
	# 		CMAKE_CXX_FLAGS_MINSIZEREL
	# 		CMAKE_CXX_FLAGS_RELEASE
	# 		CMAKE_CXX_FLAGS_RELWITHDEBINFO)
	# 		if(${FLAGS} MATCHES "/MD")
	# 			string(REGEX REPLACE "/MD" "/MT" ${FLAGS} "${${FLAGS}}")
	# 		endif()
	# 	endforeach()
	# endif()
	
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-parentheses-equality")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-string-plus-int")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-non-literal-null-conversion")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-null-conversion")
	
	# clang throws these for boost all over the place!
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-local-typedefs")
	
	#SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -stdlib=libstdc++")
	
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
	# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -read_only_relocs suppress")
	# set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -read_only_relocs suppress")

	# not too sure when this happened, 'void' has to be qualified as 'std::type_info' on __cxa_throw
	if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.3 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.3)
		set(CXA_THROW_TYPEINFO_SIGNATURE ON)
	endif()
	
else()
	message(FATAL_ERROR "Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

set(CMAKE_COMPILER_STRING "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")

if (APPLE)
#	set(CMAKE_OSX_ARCHITECTURES "i386;x86_64")
endif()

# see http://www.mail-archive.com/cmake@cmake.org/msg23240.html
if (APPLE)
	if (MACOSX_VERSION VERSION_LESS "10.9")
		# figure out what to do with Mavericks (10.9) later
	#	add_definitions("-D_DARWIN_UNLIMITED_SELECT")
		# support leopard and above
		set(CMAKE_OSX_DEPLOYMENT_TARGET 10.6)
		foreach(FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
			set(${FLAGS} "${${FLAGS}} -stdlib=libstdc++ -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
	  endforeach()
	else()
		set(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)
		foreach(FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
			set(${FLAGS} "${${FLAGS}} -stdlib=libc++ -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
	  endforeach()
	endif()
endif()

# if (APPLE AND MACOSX_VERSION VERSION_GREATER "10.9.99")
# 	set(CMAKE_OSX_DEPLOYMENT_TARGET 10.10) # ?
# 	foreach(FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
# 			set(${FLAGS} "${${FLAGS}} -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET} -arch x86_64")
# 			# we get "Illegal Instruction: 4" errors without on yosemite?!
# 			# set(${FLAGS} "${${FLAGS}} -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk")
#   endforeach()
# endif()

if (IOS)
	if (${CMAKE_GENERATOR} STREQUAL "Xcode")
	else()
		set(CMAKE_OSX_DEPLOYMENT_TARGET 4.3)
		foreach(FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
				set(${FLAGS} "${${FLAGS}} -miphoneos-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
	  endforeach()
	endif()
endif()

############################################################
# postfixes for all built targets depending on build type
############################################################

SET(CMAKE_DEBUG_POSTFIX "${64BIT_LIB_POSTFIX}_d")
SET(CMAKE_RELEASE_POSTFIX "${64BIT_LIB_POSTFIX}")
SET(CMAKE_RELWITHDEBINFO_POSTFIX "${64BIT_LIB_POSTFIX}_rd")
SET(CMAKE_MINSIZEREL_POSTFIX "${64BIT_LIB_POSTFIX}_s")

SET(CMAKE_LIBRARY_POSTFIX ${CMAKE_${BUILD_TYPE}_POSTFIX})

if (UNIX)
	add_definitions(-DHAVE_SYS_SOCKET_H)
endif()

############################################################
# Library location, type and linking
############################################################

# built shared or static libraries?
if(BUILD_SHARED_LIBS)
	add_definitions("-DUSCXML_EXPORT")
else()
	add_definitions("-DUSCXML_STATIC")
endif()

# file(GLOB 
# 	GURL_SOURCE ${PROJECT_SOURCE_DIR}/contrib/src/google-url/googleurl/src/*.cc
# 	GURL_SOURCE ${PROJECT_SOURCE_DIR}/contrib/src/google-url/googleurl/src/*.h)
# list(APPEND USCXML_FILES ${GURL_SOURCE})
# list(APPEND USCXML_FILES ${PROJECT_SOURCE_DIR}/contrib/src/google-url/base/string16.cc)


# library suffix order
if (IOS)
	LIST(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".dylib")
endif()
set(CMAKE_FIND_LIBRARY_SUFFIXES_SHARED ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES_STATIC .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})

if(BUILD_PREFER_STATIC_LIBRARIES)
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_STATIC})
endif()
SET(CMAKE_FIND_LIBRARY_SUFFIXES_ORIG ${CMAKE_FIND_LIBRARY_SUFFIXES})

# where should libraries end up?
if (DIST_PREPARE)
	if (CMAKE_CROSSCOMPILING)
		string(TOLOWER ${CMAKE_CROSSCOMPILING_TARGET} CMAKE_CROSSCOMPILING_TARGET_LC)
		set(OUTPUT_DIR
			${PROJECT_SOURCE_DIR}/package/cross-compiled/${CMAKE_CROSSCOMPILING_TARGET_LC}-${CMAKE_SYSTEM_VERSION}/${CMAKE_SYSTEM_PROCESSOR})
	else()
		set(OUTPUT_DIR
			${PROJECT_SOURCE_DIR}/package/${USCXML_PLATFORM_ID}/${CMAKE_CXX_COMPILER_ID_LC})
	endif()
else()
	set(OUTPUT_DIR ${PROJECT_BINARY_DIR})
endif()

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}/bin" )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}/lib" )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}/lib" )
# do not override configuration specific outputs
# foreach( OUTPUT_CONFIG ${CMAKE_CONFIGURATION_TYPES} )
#     string( TOUPPER ${OUTPUT_CONFIG} OUTPUT_CONFIG )
#     set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} "${OUTPUT_DIR}/bin" )
#     set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} "${OUTPUT_DIR}/lib" )
#     set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} "${OUTPUT_DIR}/lib" )
# endforeach()

OPTION(BUILD_AS_PLUGINS "Build invokers, ioprocessors and datamodels as plugins" OFF)

if (BUILD_AS_PLUGINS)
	add_definitions("-DBUILD_AS_PLUGINS")
	include_directories("src/uscxml/plugins")
#	list(APPEND USCXML_FILES "src/uscxml/plugins/Plugins.cpp")
endif()

# this is where the config.h ends up
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(src)

############################################################
# Dependent libraries
############################################################

if (NOT WIN32)
	# libxml2
	set(CMAKE_FIND_FRAMEWORK "LAST")

	# message("CMAKE_FIND_LIBRARY_SUFFIXES: ${CMAKE_FIND_LIBRARY_SUFFIXES}")
	# message("CMAKE_SYSTEM_PREFIX_PATH: ${CMAKE_SYSTEM_PREFIX_PATH}")
	# message("CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
	# message("CMAKE_SYSTEM_INCLUDE_PATH: ${CMAKE_SYSTEM_INCLUDE_PATH}")
	# message("CMAKE_SYSTEM_LIBRARY_PATH: ${CMAKE_SYSTEM_LIBRARY_PATH}")
	# message("CMAKE_SYSTEM_PROGRAM_PATH: ${CMAKE_SYSTEM_PROGRAM_PATH}")
	# message("CMAKE_FIND_ROOT_PATH: ${CMAKE_FIND_ROOT_PATH}")

	# MacOSX Mavericks moved libxml2 into SDK
	if (APPLE AND "${MACOSX_VERSION}" VERSION_LESS "10.9.0")
		set(PC_LIBXML_INCLUDEDIR "/usr/include/libxml2/")
	endif()

	find_package(LibXml2 REQUIRED)
	include_directories(${LIBXML2_INCLUDE_DIR})
	list (APPEND USCXML_CORE_LIBS ${LIBXML2_LIBRARIES})
	set(XML_LIBRARIES ${LIBXML2_LIBRARIES})
	list (APPEND USCXML_CORE_LIBS "dl")
	list (APPEND USCXML_CORE_LIBS "pthread")
	if (APPLE)
		find_library(CORELOCATION_LIBRARY CoreLocation)
		if (CORELOCATION_LIBRARY AND OFF)
			list (APPEND USCXML_OPT_LIBS ${CORELOCATION_LIBRARY}/CoreLocation)
			set(CORELOCATION_FOUND ON)
		endif()
	endif()
	if (IOS)
		find_library(COREFOUNDATION_LIBRARY CoreFoundation)
		list (APPEND USCXML_CORE_LIBS ${COREFOUNDATION_LIBRARY}/CoreFoundation)
		find_library(SECURITY_LIBRARY Security)
		list (APPEND USCXML_CORE_LIBS ${SECURITY_LIBRARY}/Security)
		list (APPEND USCXML_CORE_LIBS "z")
	elseif(APPLE)
		find_library(APP_SERVICES_LIBRARY ApplicationServices)
		find_library(COREFOUNDATION_LIBRARY CoreFoundation)
		find_library(FOUNDATION_LIBRARY Foundation)
		list (APPEND USCXML_CORE_LIBS ${APP_SERVICES_LIBRARY})
		list (APPEND USCXML_CORE_LIBS ${COREFOUNDATION_LIBRARY})
		list (APPEND USCXML_CORE_LIBS ${FOUNDATION_LIBRARY})
	endif()
elseif(WIN32)
	list (APPEND XML_LIBRARIES "Ws2_32")
	list (APPEND XML_LIBRARIES "Winmm")
	list (APPEND XML_LIBRARIES "Iphlpapi")
	list (APPEND XML_LIBRARIES "Netapi32")
	list (APPEND USCXML_CORE_LIBS ${XML_LIBRARIES})
endif()

# ICU
# find_package(ICU REQUIRED)
# include_directories(${ICU_INCLUDE_DIRS})
# list (APPEND USCXML_CORE_LIBS ${ICU_LIBRARIES})

# CURL
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
list (APPEND USCXML_CORE_LIBS ${CURL_LIBRARIES})
if (WIN32)
	add_definitions("-DCURL_STATICLIB")
endif()
include(CheckCXXSourceCompiles)

set(CMAKE_REQUIRED_INCLUDES ${CURL_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES ${CURL_LIBRARIES})

check_cxx_source_compiles("
	#include <curl/curl.h>
	int main(){return CURLOPT_MAIL_RCPT; }
" CURL_HAS_SMTP)

set(CMAKE_REQUIRED_INCLUDES)
set(CMAKE_REQUIRED_LIBRARIES)

# GLOG
# set(ENV{GLOG_SRC} ${CMAKE_SOURCE_DIR}/../glog)
find_package(GLOG REQUIRED)
include_directories(${GLOG_INCLUDE_DIR})
list (APPEND USCXML_CORE_LIBS ${GLOG_LIBRARY})
add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES)

# ARABICA
# set($ENV{ARABICA_SRC} ${PROJECT_SOURCE_DIR}/../arabica)
find_package(Arabica REQUIRED)
include_directories(${ARABICA_INCLUDE_DIR})
list (APPEND USCXML_CORE_LIBS ${ARABICA_LIBRARY})
if (WIN32)
	add_definitions("-DUSE_MSXML")
endif()

# BOOST - header only
FIND_PATH(Boost_INCLUDE_DIR boost/version.hpp)
include_directories(${Boost_INCLUDE_DIR})

# LIBEVENT
#set(ENV{EVENT_SRC} ${CMAKE_SOURCE_DIR}/../libevent)
find_package(EVENT REQUIRED)
include_directories(${EVENT_INCLUDE_DIR})
list (APPEND USCXML_CORE_LIBS ${EVENT_LIBRARY})
#set(EVENT_SSL_FOUND OFF) # deactivate for now

#################################################
# Optional libraries

OPTION(DISABLE_ALL "Ignore all optional libraries" OFF)
if (BUILD_MINIMAL)
	SET(DISABLE_ALL ON)
endif()

if (APPLE OR IOS)
	OPTION(DISABLE_AUDIOTOOLBOX "Ignore AudioToolbox" OFF)
	if (NOT DISABLE_AUDIOTOOLBOX AND NOT DISABLE_ALL)
		find_library(AUDIOTOOLBOX AudioToolbox REQUIRED)
		if (MACOSX_VERSION VERSION_GREATER "10.10.99")
			# header files were moved it seems
			list (APPEND USCXML_OPT_LIBS ${AUDIOTOOLBOX})
		else()
			list (APPEND USCXML_OPT_LIBS ${AUDIOTOOLBOX}/AudioToolbox)
		endif()
		set(AUDIOTOOLBOX_FOUND ON)
	else()
		set(AUDIOTOOLBOX_FOUND OFF)
	endif()
	find_library(FOUNDATION_LIBRARY Foundation)
	if (MACOSX_VERSION VERSION_GREATER "10.10.99")
		list (APPEND USCXML_OPT_LIBS ${FOUNDATION_LIBRARY})
	else()
		list (APPEND USCXML_OPT_LIBS ${FOUNDATION_LIBRARY}/Foundation)
	endif()
	
	if (IOS)
		find_library(WTF_LIBRARY WTF)
		find_library(ICU_LIBRARY icucore REQUIRED)
		list (APPEND USCXML_OPT_LIBS ${WTF_LIBRARY})
		list (APPEND USCXML_OPT_LIBS ${ICU_LIBRARY})
	endif()

endif()

OPTION(DISABLE_JSC "Ignore JavaScriptCore" OFF)
if (NOT DISABLE_JSC AND NOT DISABLE_ALL AND NOT ECMA_FOUND)
	find_package(JSC)
	if (JSC_FOUND)
		set(ECMA_FOUND ON)
		if (NOT APPLE)
			include_directories(${JSC_INCLUDE_DIR})
		endif()
		list (APPEND USCXML_OPT_LIBS ${JSC_LIBRARY})
	endif()
else()
	set(JSC_FOUND OFF)
endif()

OPTION(DISABLE_V8 "Ignore Google's v8" OFF)
if (NOT DISABLE_V8 AND NOT DISABLE_ALL AND NOT ECMA_FOUND)
	find_package(V8)
	if (V8_FOUND)
		set(ECMA_FOUND ON)
		include_directories(${V8_INCLUDE_DIR})
		list (APPEND USCXML_OPT_LIBS ${V8_LIBRARY})
	endif()
else()
	set(V8_FOUND OFF)
endif()

OPTION(DISABLE_SPIDERMONKEY "Ignore SpiderMonkey" ON)
if (NOT DISABLE_SPIDERMONKEY AND NOT DISABLE_ALL AND NOT ECMA_FOUND)
	find_package(SpiderMonkey)
	if (SPIDERMONKEY_FOUND)
		set(ECMA_FOUND ON)
		include_directories(${SPIDERMONKEY_INCLUDE_DIR})
		if (UNIX)
			add_definitions(-DXP_UNIX)
		endif()
		list (APPEND USCXML_OPT_LIBS ${SPIDERMONKEY_LIBRARY})
	endif()
else()
	set(SPIDERMONKEY_FOUND OFF)
endif()


OPTION(DISABLE_LUA "Ignore Lua" OFF)
if (NOT DISABLE_LUA AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(Lua)
	if (LUA_FOUND)
		include_directories (${LUA_INCLUDE_DIR})
		include_directories(${PROJECT_SOURCE_DIR}/contrib/src/LuaBridge)
		list (APPEND USCXML_OPT_LIBS ${LUA_LIBRARIES})
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(LUA_FOUND OFF)
endif()

OPTION(DISABLE_SSL "Ignore OpenSSL" OFF)
if (NOT DISABLE_SSL AND NOT DISABLE_ALL)
	find_package(OpenSSL)
	if (OPENSSL_FOUND)
		set(OPENSSL_HAS_ELIPTIC_CURVES OFF)
		include_directories(${OPENSSL_INCLUDE_DIR})
		list (APPEND USCXML_OPT_LIBS ${OPENSSL_LIBRARIES})
		if (EXISTS ${OPENSSL_INCLUDE_DIR}/openssl/ec.h)
			set(OPENSSL_HAS_ELIPTIC_CURVES ON)
		endif()
	endif()
else()
	set(OPENSSL_FOUND OFF)
endif()

if (EVENT_SSL_FOUND AND OPENSSL_FOUND)
	list (APPEND USCXML_CORE_LIBS ${OPENSSL_LIBRARIES})
endif()


OPTION(DISABLE_SWI "Ignore SWI Prolog" OFF)
if (NOT DISABLE_SWI AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(SWI)
	if (SWI_FOUND)
		if (SWI_CPP_INCLUDE_DIR)
			include_directories(${SWI_CPP_INCLUDE_DIR})
		else()
			include_directories(${PROJECT_SOURCE_DIR}/contrib/src/swi-pl)
		endif()
		include_directories(${SWI_INCLUDE_DIR})	
		if (BUILD_PREFER_STATIC_LIBRARIES)
			find_package(GMP)
			find_package(Curses)
			if (GMP_FOUND AND CURSES_FOUND)
				list (APPEND USCXML_OPT_LIBS ${SWI_LIBRARY} ${GMP_LIBRARY} ${CURSES_LIBRARIES})
			else()
				message("Not building datamodel with static SWI without NCurses and GMP")
				set(SWI_FOUND OFF)
			endif()
		else()
			list (APPEND USCXML_OPT_LIBS ${SWI_LIBRARY})
		endif()
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(SWI_FOUND OFF)
endif()

OPTION(DISABLE_SQLITE "Ignore Sqlite3" OFF)
if (NOT DISABLE_SQLITE AND NOT DISABLE_ALL)
	find_package(Sqlite3)
	if (SQLITE3_FOUND)
	endif()
else()
	set(SQLITE3_FOUND OFF)
endif()

OPTION(DISABLE_FFMPEG "Ignore FFMpeg" OFF)
if (NOT DISABLE_FFMPEG AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(FFMPEG)
	if (FFMPEG_FOUND)
		include_directories (${FFMPEG_INCLUDE_DIRS})
		list (APPEND USCXML_OPT_LIBS ${FFMPEG_LIBRARIES})
		# required with static ffmpeg builds
		# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-Bsymbolic")
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(FFMPEG_FOUND OFF)
endif()

OPTION(DISABLE_ICAL "Ignore iCal" OFF)
if (NOT DISABLE_ICAL AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(LIBICAL)
	if (LIBICAL_FOUND)
		include_directories (${LIBICAL_INCLUDE_DIR})
		list (APPEND USCXML_OPT_LIBS ${LIBICAL_LIBRARIES})
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})

else()
	set(LIBICAL_FOUND OFF)
endif()

OPTION(DISABLE_WEBRTC "Ignore libjingle (WebRTC)" OFF)
if (NOT DISABLE_WEBRTC AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	if (NOT DEFINED ENV{LIBJINGLE_ROOT_DIR})
		set(ENV{LIBJINGLE_ROOT_DIR} "/Users/sradomski/Documents/TK/Code/webrtc-work/trunk")
	endif()
	find_package(LibJingle)
	if (LIBJINGLE_FOUND)
		include_directories (${LIBJINGLE_INCLUDE_DIRS})
		list (APPEND USCXML_OPT_LIBS ${LIBJINGLE_LIBRARIES})
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(LIBJINGLE_FOUND OFF)
endif()

OPTION(DISABLE_EXPECT "Ignore TCL/Expect" OFF)
if (NOT DISABLE_EXPECT AND NOT DISABLE_ALL)
	find_package(Expect)
	find_package(TCL)
	if (EXPECT_FOUND AND TCL_FOUND)
		include_directories (${EXPECT_INCLUDE_DIR})
		include_directories (${TCL_INCLUDE_PATH})
		list (APPEND USCXML_OPT_LIBS ${EXPECT_LIBRARY})
		list (APPEND USCXML_OPT_LIBS ${TCL_LIBRARY})
	endif()
else()
	set(EXPECT_FOUND OFF)
	set(TCL_FOUND OFF)
endif()

OPTION(DISABLE_LIBPURPLE "Ignore libpurple (instant messaging)" OFF)
if (NOT DISABLE_LIBPURPLE AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(LibPurple)
	if (LIBPURPLE_FOUND)
		find_package(ICONV)
		find_package(GLIB2)
		find_package(GObject)
		if (GLIB2_FOUND AND ICONV_FOUND AND GOBJECT_FOUND)
			include_directories (${LIBPURPLE_INCLUDE_DIR})
			list (APPEND USCXML_OPT_LIBS ${LIBPURPLE_LIBRARY})
			include_directories (${GLIB2_INCLUDE_DIRS})
			list (APPEND USCXML_OPT_LIBS ${GLIB2_LIBRARIES})
			include_directories (${ICONV_INCLUDE_DIR})
			list (APPEND USCXML_OPT_LIBS ${ICONV_LIBRARIES})
			include_directories (${GOBJECT_INCLUDE_DIR})
			list (APPEND USCXML_OPT_LIBS ${GOBJECT_LIBRARIES})
		else()
			set(LIBPURPLE_FOUND OFF)
		endif()	
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(LIBPURPLE_FOUND OFF)
endif()

OPTION(DISABLE_UMUNDO "Ignore uMundo" OFF)
if (NOT DISABLE_UMUNDO AND NOT DISABLE_ALL)
	if (WIN32)
		find_package(UMUNDO COMPONENTS convenience)
	else()
		find_package(UMUNDO COMPONENTS rpc serial core)
		# find_package(UMUNDO COMPONENTS convenience)
	endif()
	if (UMUNDO_FOUND)
		include_directories (${UMUNDO_INCLUDE_DIR})
		list (APPEND USCXML_OPT_LIBS ${UMUNDO_LIBRARIES})
	#	add_definitions("-DUMUNDO_STATIC")
	endif()
else()
	set(UMUNDO_FOUND OFF)
endif()

OPTION(DISABLE_OSG "Ignore OpenSceneGraph" OFF)
if (NOT DISABLE_OSG AND NOT DISABLE_ALL)
	if (UNIX)
		set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED}) # link dynamically!
	elseif(WIN32)
		if (ENV{OSG_ROOT} STREQUAL "")
			set(ENV{OSG_ROOT} "C:/Program Files/OpenSceneGraph-3.0.1")
		endif()
	endif()
	find_package(OpenSceneGraph COMPONENTS osgViewer osgGA osgText osgFX osgManipulator osgDB osgUtil OpenThreads)
	find_package(OpenGL)
	if (OPENSCENEGRAPH_FOUND AND OPENGL_FOUND)	
		include_directories (${OPENSCENEGRAPH_INCLUDE_DIRS})
		include_directories (${OPENGL_INCLUDE_DIR})
		list (APPEND USCXML_OPT_LIBS ${OPENSCENEGRAPH_LIBRARIES} ${OPENGL_LIBRARIES})
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(OPENSCENEGRAPH_FOUND OFF)
	set(OPENGL_FOUND OFF)
endif()

OPTION(DISABLE_OPENAL "Ignore OpenAL" OFF)
if (NOT DISABLE_OPENAL AND NOT DISABLE_ALL)
	set(CMAKE_FIND_FRAMEWORK "FIRST")
	find_package(OpenAL)
	if (OPENAL_FOUND)
		list (APPEND USCXML_INCLUDE_DIRS ${OPENAL_INCLUDE_DIR})
		if (APPLE OR IOS)
			if (MACOSX_VERSION VERSION_GREATER "10.10.99")
				list (APPEND USCXML_OPT_LIBS ${OPENAL_LIBRARY})
			else()
				list (APPEND USCXML_OPT_LIBS ${OPENAL_LIBRARY}/OpenAL)
			endif()
		else()
			list (APPEND USCXML_OPT_LIBS ${OPENAL_LIBRARY})
		endif()
	else()
		find_package(OpenALKCAT)
		if (OPENAL_FOUND)
			list (APPEND USCXML_INCLUDE_DIRS ${OPENAL_INCLUDE_DIR})
			list (APPEND USCXML_OPT_LIBS ${OPENAL_LIBRARY})
		endif()
	endif()
	set(CMAKE_FIND_FRAMEWORK "LAST")
else()
	set(OPENAL_FOUND OFF)
endif()

if (NOT AUDIOTOOLBOX_FOUND)
	OPTION(DISABLE_LIBSNDFILE "Ignore libsndfile" OFF)
	if (NOT DISABLE_LIBSNDFILE AND NOT DISABLE_ALL)
		set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
		find_package(LIBSNDFILE)
		if (LIBSNDFILE_FOUND)
			list (APPEND USCXML_INCLUDE_DIRS ${LIBSNDFILE_INCLUDE_DIR})
			list (APPEND USCXML_OPT_LIBS ${LIBSNDFILE_LIBRARY})
		endif()
		set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
	else()
		set(LIBSNDFILE_FOUND OFF)
	endif()
endif()

OPTION(DISABLE_MILES "Ignore miles" OFF)
if (NOT DISABLE_MILES AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(MILES)
	#set(JPEG_ROOT_PATH "" CACHE STRING "Where to find libjpeg")
	if (MILES_FOUND)
		if (NOT WIN32)
			# openal is only needed for miles
			find_package(OpenAL)
			find_package(JPEG)
			find_package(ICONV)
	
			if (OPENAL_FOUND AND ICONV_FOUND AND JPEG_FOUND)
				include_directories (${MILES_INCLUDE_DIR})
				list (APPEND USCXML_OPT_LIBS ${MILES_LIBRARIES})
				include_directories (${ICONV_INCLUDE_DIR})
				list (APPEND USCXML_OPT_LIBS ${ICONV_LIBRARIES})
				include_directories (${JPEG_INCLUDE_DIR})
				list (APPEND USCXML_OPT_LIBS ${JPEG_LIBRARIES})
				include_directories (${OPENAL_INCLUDE_DIR})
				list (APPEND USCXML_OPT_LIBS ${OPENAL_LIBRARY})
			else()
				set(MILES_FOUND OFF)
			endif()
		else()
			include_directories (${MILES_INCLUDE_DIR})
			list (APPEND USCXML_OPT_LIBS ${MILES_LIBRARIES})
		endif()
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(MILES_FOUND OFF)
endif()

OPTION(DISABLE_PROTOBUF "Ignore protobuf" OFF)
if (NOT DISABLE_PROTOBUF AND NOT DISABLE_ALL)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SHARED})
	find_package(Protobuf)
	if (PROTOBUF_FOUND)
		include_directories(${PROTOBUF_INCLUDE_DIRS})
		LIST(APPEND USCXML_OPT_LIBS optimized ${PROTOBUF_LIBRARY})
		LIST(APPEND USCXML_OPT_LIBS debug ${PROTOBUF_LIBRARY_DEBUG})
	endif()
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
else()
	set(PROTOBUF_FOUND OFF)
endif()

#################################################
# Gather source files
#################################################

# we use include instead of add_subdirectory because
# source groups do not work otherwise.

SET(USCXML_INVOKERS)
SET(USCXML_IOPROCESSORS)
SET(USCXML_ELEMENTS)
SET(USCXML_DATAMODELS)
SET(USCXML_LANGUAGE_BINDINGS)

include_directories(${USCXML_INCLUDE_DIRS})
add_subdirectory(src/uscxml)
if (WIN32)
	list(APPEND USCXML_FILES "${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c")
#	SET_SOURCE_FILES_PROPERTIES( ${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c PROPERTIES LANGUAGE CXX )
endif()

if (UNIX AND NOT APPLE)
	list (APPEND USCXML_CORE_LIBS "rt")
endif()

############################################################
# Binaries and tests
############################################################

set(ALL_SOURCE_FILES ${USCXML_FILES} ${USCXML_TRANSFORM_FILES})
list(SORT USCXML_FILES)
list(SORT USCXML_TRANSFORM_FILES)
# we cannot define source groups in sub directories!
foreach( FILE ${ALL_SOURCE_FILES} )
	get_filename_component(PATH ${FILE} PATH)
	get_filename_component(NAME ${FILE} NAME)

	# if (${NAME} MATCHES "Factory.cpp")
	# 	set_property(SOURCE ${FILE} PROPERTY COMPILE_FLAGS -std=c++11)
	# endif()
	# if (${PATH} MATCHES ".*datamodel\\/ecmascript\\/v8\\/*")
	# 	set_property(SOURCE ${FILE} PROPERTY COMPILE_FLAGS -std=c++11)
	# 	# message(${FILE})
	# endif()

	if (${PATH} MATCHES ".*datamodel\\/ecmascript.*")
		STRING(REGEX MATCH "[^\\/]*$" COMP_NAME ${PATH})
		if (COMP_NAME STREQUAL "ecmascript")
			source_group("Datamodel\\EcmaScript" FILES ${FILE})
		else()
			source_group("Datamodel\\EcmaScript\\${COMP_NAME}" FILES ${FILE})
		endif()
	elseif (${PATH} MATCHES ".*datamodel\\/prolog.*")
		STRING(REGEX MATCH "[^\\/]*$" COMP_NAME ${PATH})
		source_group("Datamodel\\Prolog\\${COMP_NAME}" FILES ${FILE})
	elseif (${PATH} MATCHES ".*datamodel\\/promela.*")
		source_group("Datamodel\\Promela" FILES ${FILE})
	elseif (${PATH} MATCHES ".*datamodel\\/xpath.*")
		source_group("Datamodel\\XPath" FILES ${FILE})
	elseif (${PATH} MATCHES ".*datamodel\\/lua.*")
		source_group("Datamodel\\Lua" FILES ${FILE})
	elseif (${PATH} MATCHES ".*datamodel\\/null.*")
		source_group("Datamodel\\NULL" FILES ${FILE})
	
	elseif (${PATH} MATCHES ".*\\/invoker\\/.*")
		STRING(REGEX MATCH "[^\\/]*$" COMP_NAME ${PATH})
		source_group("Invoker\\${COMP_NAME}" FILES ${FILE})

	elseif (${PATH} MATCHES ".*\\/element\\/.*")
		STRING(REGEX MATCH "[^\\/]*$" COMP_NAME ${PATH})
		source_group("Element\\${COMP_NAME}" FILES ${FILE})

	elseif (${PATH} MATCHES ".*\\/ioprocessor\\/.*")
		STRING(REGEX MATCH "[^\\/]*$" COMP_NAME ${PATH})
		source_group("IOProcessor\\${COMP_NAME}" FILES ${FILE})

	# elseif (${PATH} MATCHES ".*\\/google-url\\/.*")
	# 	STRING(REGEX MATCH "[^\\/]*$" COMP_NAME ${PATH})
	# 	source_group("Interpreter\\URL" FILES ${FILE})

	elseif (${FILE} MATCHES ".*\\/debug\\/.*")
		source_group("Interpreter\\debug" FILES ${FILE})
	elseif (${FILE} MATCHES ".*\\/plugins\\/.*")
		source_group("Interpreter\\plugins" FILES ${FILE})
	elseif (${FILE} MATCHES ".*\\/transform\\/.*")
		# source_group("Interpreter\\transform" FILES ${FILE})
	elseif (${FILE} MATCHES ".*\\/util\\/.*")
		source_group("Interpreter\\util" FILES ${FILE})
	elseif (${FILE} MATCHES ".*\\/concurrency\\/.*")
		source_group("Interpreter\\concurrency" FILES ${FILE})
	elseif (${FILE} MATCHES ".*\\/server\\/.*")
		source_group("Interpreter\\server" FILES ${FILE})
	elseif (${FILE} MATCHES ".*\\/messages\\/.*")
		source_group("Interpreter\\messages" FILES ${FILE})

	else ()
		source_group(Interpreter FILES ${FILE})
	endif()
endforeach()

# if (V8_FOUND AND BUILD_DM_ECMA)
# 	set_property(SOURCE ${ALL_SOURCE_FILES} PROPERTY COMPILE_FLAGS -Wno-c++11-extensions)
# endif()

# add compile time reducer
# see https://github.com/sakra/cotire
if (ENABLE_COTIRE)
	include(cotire)
endif()

#
# We cannot do this in subdirs?!
#

if (NOT BUILD_MINIMAL)
	# include html template in source file
	find_program(XXD_BINARY xxd)
	if (XXD_BINARY)
		add_custom_command(
			OUTPUT  ${CMAKE_SOURCE_DIR}/src/uscxml/plugins/invoker/xhtml/template/xhtml-invoker.inc.h
			COMMAND ${XXD_BINARY} -i template/xhtml-invoker.html template/xhtml-invoker.inc.h
		  DEPENDS ${CMAKE_SOURCE_DIR}/src/uscxml/plugins/invoker/xhtml/template/xhtml-invoker.html
			WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/uscxml/plugins/invoker/xhtml
			COMMENT "Running xxd on uscxml/plugins/invoker/xhtml/template/xhtml-invoker.html"
		)
		LIST(APPEND USCXML_FILES ${CMAKE_SOURCE_DIR}/src/uscxml/plugins/invoker/xhtml/template/xhtml-invoker.inc.h)
	endif()
endif()

# build library
if (BUILD_AS_PLUGINS)
	add_library(uscxml ${USCXML_FILES})
	target_link_libraries(uscxml ${USCXML_CORE_LIBS})
	set_target_properties(uscxml PROPERTIES COMPILE_FLAGS "-DUSCXML_EXPORT")
#	set_target_properties(uscxml PROPERTIES COMPILE_FLAGS "-DPLUMA_EXPORTS")
	add_definitions(-DPLUMA_EXPORTS)
else()	
	add_library(uscxml ${USCXML_FILES})
	target_link_libraries(uscxml ${USCXML_OPT_LIBS} ${USCXML_CORE_LIBS})
endif()
INSTALL_LIBRARY(TARGETS uscxml COMPONENT library)

add_library(uscxml_transform ${USCXML_TRANSFORM_FILES})
target_link_libraries(uscxml_transform uscxml)
set_target_properties(uscxml_transform PROPERTIES PROJECT_LABEL "transform" FOLDER "uscxml")

INSTALL_LIBRARY(TARGETS uscxml_transform COMPONENT library)

if (NOT CMAKE_CROSSCOMPILING)
	if (ENABLE_COTIRE)
		set_target_properties(uscxml PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT "src/uscxml/pch.h")
		set_target_properties(uscxml PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
		cotire(uscxml)
	endif()
endif()

if (NOT CMAKE_CROSSCOMPILING)
	if (WIN32)
		add_executable(uscxml-browser apps/uscxml-browser.cpp ${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c)
	else()
		add_executable(uscxml-browser apps/uscxml-browser.cpp)
	endif()
	target_link_libraries(uscxml-browser uscxml)
	
	
	if (NOT CMAKE_CROSSCOMPILING)
		if (ENABLE_COTIRE)
			set_target_properties(uscxml-browser PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
		endif()
#		cotire(uscxml-browser)
	endif()
	set_target_properties(uscxml-browser PROPERTIES FOLDER "Apps")
	install_executable(TARGETS uscxml-browser COMPONENT tools)

	if (NOT BUILD_MINIMAL)
		if (WIN32)
			add_executable(uscxml-transform apps/uscxml-transform.cpp ${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c)
		else()
			add_executable(uscxml-transform apps/uscxml-transform.cpp)
		endif()
		target_link_libraries(uscxml-transform uscxml uscxml_transform)
		if (NOT CMAKE_CROSSCOMPILING)
			if (ENABLE_COTIRE)
				set_target_properties(uscxml-transform PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
			endif()
		endif()
		set_target_properties(uscxml-transform PROPERTIES FOLDER "Apps")
		install_executable(TARGETS uscxml-transform COMPONENT tools)

		if (WIN32)
			add_executable(uscxml-analyze apps/uscxml-analyze.cpp ${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c)
		else()
			add_executable(uscxml-analyze apps/uscxml-analyze.cpp)
		endif()
		target_link_libraries(uscxml-analyze uscxml uscxml_transform)
		if (NOT CMAKE_CROSSCOMPILING)
			if (ENABLE_COTIRE)
				set_target_properties(uscxml-analyze PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
			endif()
		endif()
		set_target_properties(uscxml-analyze PROPERTIES FOLDER "Apps")
		install_executable(TARGETS uscxml-analyze COMPONENT tools)

		if (WIN32)
			add_executable(uscxml-dot apps/uscxml-dot.cpp ${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c)
		else()
			add_executable(uscxml-dot apps/uscxml-dot.cpp)
		endif()
		target_link_libraries(uscxml-dot uscxml)
		if (NOT CMAKE_CROSSCOMPILING)
			if (ENABLE_COTIRE)
				set_target_properties(uscxml-dot PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
			endif()
		endif()
		set_target_properties(uscxml-dot PROPERTIES FOLDER "Apps")
		install_executable(TARGETS uscxml-dot COMPONENT tools)
	endif()

	if (PROTOBUF_FOUND AND OFF)
		file(GLOB W3C-MMI-COMMON ${PROJECT_SOURCE_DIR}/apps/w3c-mmi/*.cpp ${PROJECT_SOURCE_DIR}/apps/w3c-mmi/*.h)

		file(GLOB_RECURSE W3C-MMI-IM ${PROJECT_SOURCE_DIR}/apps/w3c-mmi/im/*.cpp ${PROJECT_SOURCE_DIR}/apps/w3c-mmi/im/*.h)
		add_executable(uscxml-interaction-manager ${W3C-MMI-IM} ${W3C-MMI-COMMON})
		target_link_libraries(uscxml-interaction-manager uscxml)
		set_target_properties(uscxml-interaction-manager PROPERTIES FOLDER "Apps")
		install_executable(TARGETS uscxml-interaction-manager COMPONENT tools)

		file(GLOB_RECURSE W3C-MMI-MC ${PROJECT_SOURCE_DIR}/apps/w3c-mmi/mc/*.cpp ${PROJECT_SOURCE_DIR}/apps/w3c-mmi/mc/*.h)
		add_executable(uscxml-modality-component ${W3C-MMI-MC} ${W3C-MMI-COMMON})
		target_link_libraries(uscxml-modality-component uscxml)
		set_target_properties(uscxml-modality-component PROPERTIES FOLDER "Apps")
		install_executable(TARGETS uscxml-modality-component COMPONENT tools)
	endif()

	if (BUILD_TESTS)
		add_subdirectory(test)
	endif()
	
	add_subdirectory(src/bindings)
endif()


############################################################
# Header Files
############################################################

file(GLOB_RECURSE USCXML_HEADERS 
  ${PROJECT_SOURCE_DIR}/src/*.h 
  ${PROJECT_SOURCE_DIR}/src/*.hpp 
  ${CMAKE_BINARY_DIR}/*.h)
INSTALL_HEADERS(HEADERS ${USCXML_HEADERS} COMPONENT headers)

############################################################
# Create config.h
############################################################

# determine path seperator to shorten filenames in Debug.cpp
if (WIN32)
    SET(PATH_SEPERATOR "\\\\")
else()
	SET(PATH_SEPERATOR "/")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
	set(CMAKE_BUILD_TYPE_RELEASE ON)
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
	set(CMAKE_BUILD_TYPE_DEBUG ON)
endif()



# enable config.h style compile time options and add as "uscxml/config.h"
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/uscxml/config.h)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test/ctest/CTestCustom.ctest.in ${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.ctest)

############################################################
# Some concluding remarks
############################################################

message(STATUS "Linking against external (check if you need to export in LD_LIBRARY_PATH or PATH):")
set(SKIP_NEXT OFF)
foreach(LIBRARY ${USCXML_OPT_LIBS})
	if (LIBRARY MATCHES "debug")
		if(CMAKE_BUILD_TYPE STREQUAL "Release")
			set(SKIP_NEXT ON)
		endif()
	elseif (LIBRARY MATCHES "optimized")
		if(CMAKE_BUILD_TYPE STREQUAL "Debug")
			set(SKIP_NEXT ON)
		endif()
	elseif (LIBRARY MATCHES "uscxml.*")
	elseif (LIBRARY MATCHES ".*\\.framework.*")
	elseif (LIBRARY MATCHES "mmi_proto.*")
	else()
		if (NOT SKIP_NEXT)
			message(STATUS "  ${LIBRARY}")
		else()
			set(SKIP_NEXT OFF)
		endif()
	endif()
endforeach()

message(STATUS "Targets:")
message(STATUS "  Available datamodels ........... : ${USCXML_DATAMODELS}")
message(STATUS "  Available invokers ............. : ${USCXML_INVOKERS}")
message(STATUS "  Available ioprocessors ......... : ${USCXML_IOPROCESSORS}")
message(STATUS "  Available custom elements ...... : ${USCXML_ELEMENTS}")
message(STATUS "  Available language bindings .... : ${USCXML_LANGUAGE_BINDINGS}")
if (BUILD_SHARED_LIBS AND BUILD_BINDINGS)
	message(STATUS "")
	message(STATUS "    Warning: Building language bindings BUILD_SHARED_LIBS=ON")
	message(STATUS "             introduces runtime dependency to libuscxml")
	message(STATUS "")
endif()

message(STATUS "General information:")
message(STATUS "  Build type ..................... : ${CMAKE_BUILD_TYPE} for ${USCXML_PLATFORM_ID}")
if (BUILD_SHARED_LIBS)
	if (BUILD_AS_PLUGINS)
		message(STATUS "  Building library as ............ : SHARED with plugins")
	else()
		message(STATUS "  Building library as ............ : SHARED without plugins")
	endif()
else()
	if (BUILD_AS_PLUGINS)
		message(STATUS "  Building library as ............ : STATIC with plugins")
	else()
		message(STATUS "  Building library as ............ : STATIC without plugins")
	endif()
endif()

if (BUILD_PREFER_STATIC_LIBRARIES)
	message(STATUS "  Preferring dependent libraries . : STATIC ")
else()
	message(STATUS "  Preferring dependent libraries . : SHARED")
endif()

if (BUILD_PREFER_PREBUILT_LIBS)
	STRING(REGEX REPLACE "${PROJECT_SOURCE_DIR}/" "" REL_USCXML_PREBUILT_LIBRARY_PATH ${USCXML_PREBUILT_LIBRARY_PATH})
	message(STATUS "  Preferring dependent libraries . : from ${REL_USCXML_PREBUILT_LIBRARY_PATH}")
else()
	message(STATUS "  Preferring dependent libraries . : installed on system")
endif()

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UC)
message(STATUS "  CXX Flags : " ${CMAKE_CXX_FLAGS} " " ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UC}})
message(STATUS "  C   Flags : " ${CMAKE_C_FLAGS} " " ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}})

STRING(REGEX REPLACE "${CMAKE_BINARY_DIR}" "BUILD_DIR" REL_CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
STRING(REGEX REPLACE "${CMAKE_SOURCE_DIR}" "SOURCE_DIR" REL_CMAKE_LIBRARY_OUTPUT_DIRECTORY ${REL_CMAKE_LIBRARY_OUTPUT_DIRECTORY})

STRING(REGEX REPLACE "${CMAKE_BINARY_DIR}" "BUILD_DIR" REL_CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
STRING(REGEX REPLACE "${CMAKE_SOURCE_DIR}" "SOURCE_DIR" REL_CMAKE_RUNTIME_OUTPUT_DIRECTORY ${REL_CMAKE_RUNTIME_OUTPUT_DIRECTORY})

message(STATUS "  Libraries will end up in ....... : " ${REL_CMAKE_LIBRARY_OUTPUT_DIRECTORY})
message(STATUS "  Binaries will end up in ........ : " ${REL_CMAKE_RUNTIME_OUTPUT_DIRECTORY})


############################################################
# Installation / Provide package target
############################################################

# see contrib/cmake/CPackUSCXML.cmake
include(contrib/cmake/CPackUSCXML.cmake)

cmake_policy(POP)

# This must always be last!
include(CPack)