summaryrefslogtreecommitdiffstats
path: root/CMakeLists.txt
blob: 5f1fecb8907583565afa9db4036fae21d6428a46 (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
# travis build slaves ony feature 2.8.7 - let's hope it's sufficient
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)

# 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()

# swig_add_module complains quite verbosly on newer cmake versions
SET(CMAKE_WARN_DEPRECATED OFF)

PROJECT(uSCXML)

#################################################
# General Setup
#################################################

# where to find more cmake scripts
list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/contrib/cmake")
set (CMAKE_BUILD_SCRIPTS "${PROJECT_SOURCE_DIR}/contrib/cmake")
# add modules that were introduced with later cmake versions if possible
if (CMAKE_VERSION LESS 3.0.0)
	list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/contrib/cmake/3.0.0")
endif()

include("${PROJECT_SOURCE_DIR}/contrib/cmake/USCXMLMacros.cmake")

# use folders in the IDEs for the various targets
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# compile with fPIC if required
# see https://stackoverflow.com/questions/38296756/what-is-the-idiomatic-way-in-cmake-to-add-the-fpic-compiler-option
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# use rpath with MacOSX
set(CMAKE_MACOSX_RPATH 1)

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

# Normalize CMAKE_SYSTEM_PROCESSOR for build target
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	set(64BIT_HOST ON)
	set(64BIT_SUFFIX "_64")
endif()

if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
  set(CMAKE_SYSTEM_PROCESSOR "x86${64BIT_SUFFIX}")
endif()
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  set(CMAKE_SYSTEM_PROCESSOR "x86${64BIT_SUFFIX}")
endif()
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  set(CMAKE_SYSTEM_PROCESSOR "x86${64BIT_SUFFIX}")
endif()


# determine platform id
SET(USCXML_PLATFORM_ID)
string(TOLOWER ${CMAKE_SYSTEM_NAME} CMAKE_SYSTEM_NAME_LC)
string(TOLOWER ${CMAKE_CXX_COMPILER_ID} CMAKE_CXX_COMPILER_ID_LC)
string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
set(USCXML_PLATFORM_ID "${CMAKE_SYSTEM_NAME_LC}-${CMAKE_SYSTEM_PROCESSOR_LC}-${CMAKE_CXX_COMPILER_ID_LC}")

# figure out MacOSX version and C++ library
if (APPLE)
	set(LIBCPP_NAME "libstdc++") # before mavericks
	# get MacOSX version
	execute_process(
		COMMAND /usr/bin/sw_vers -productVersion 
		OUTPUT_VARIABLE MACOSX_VERSION
		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 and above
		# set(CMAKE_FIND_ROOT_PATH ${CMAKE_OSX_SYSROOT} ${CMAKE_FIND_ROOT_PATH})
	endif()
	set(USCXML_PLATFORM_ID "${USCXML_PLATFORM_ID}-${LIBCPP_NAME}")
endif()

if (MSVC)
	set(USCXML_PLATFORM_ID "${USCXML_PLATFORM_ID}-${MSVC_VERSION}")
endif()

# where do libraries and binaries go
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# additional system library directories
# if (EXISTS "/usr/lib/x86_64-linux-gnu")
# 	LIST(APPEND CMAKE_FIND_ROOT_PATH "/usr/lib/x86_64-linux-gnu")
# endif()

# setup header include paths
include_directories(src)
include_directories(contrib/src)
include_directories(${CMAKE_BINARY_DIR})
include_directories(${PROJECT_SOURCE_DIR}/contrib/src/jsmn)
include_directories(${PROJECT_SOURCE_DIR}/contrib/src/evws)
include_directories(${PROJECT_SOURCE_DIR}/contrib/src/uriparser/include)

set(GETOPT_FILES ${PROJECT_SOURCE_DIR}/contrib/src/uscxml/InterpreterOptions.cpp)	
if (WIN32)
	include_directories(${PROJECT_SOURCE_DIR}/contrib/src/getopt)
	include_directories(${PROJECT_SOURCE_DIR}/contrib/src/inttypes)
	list(APPEND GETOPT_FILES ${PROJECT_SOURCE_DIR}/contrib/src/getopt/getopt.c)	
endif()

################################
# Compiler Features and Flags

# Check for C++11 support
# message("Your C++ compiler supports these C++ features:")
# foreach(i ${CMAKE_CXX_COMPILE_FEATURES})
#   message("${i}")
# endforeach()

# http://stackoverflow.com/a/25836953/990120
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11_FLAG)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X_FLAG)
if(COMPILER_SUPPORTS_CXX11_FLAG)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X_FLAG)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()

if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall")

elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall")
	
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
	add_definitions("-D_SCL_SECURE_NO_WARNINGS")
	add_definitions("-D_CRT_SECURE_NO_WARNINGS")
else()
	message(STATUS "Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()
set(CMAKE_COMPILER_STRING "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")

# https://cmake.org/Bug/view.php?id=15361
set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_CXX11_STANDARD_COMPILE_OPTION})

include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
	#include <thread>
	int main(){ return 0; }
" CXX_HAS_THREADS)
check_cxx_source_compiles("
	#include <memory>
	int main(){ std::shared_ptr<void> foo(NULL); return 0; }
" CXX_HAS_SHARED_PTR)
check_cxx_source_compiles("
	int main(){ int foo[10]; auto i = foo[0]; }
" CXX_HAS_AUTOTYPE)
check_cxx_source_compiles("
	int main(){ []{}(); }
" CXX_HAS_LAMBDAS)
check_cxx_source_compiles("
	class Foo { int x = 8; }; int main(){}
" CXX_HAS_MEMBER_INIT)
check_cxx_source_compiles("
	#include <list>
	#include <string>
	int main(){ std::list<std::string> foo = {\"a\", \"b\"}; }
" CXX_HAS_INLINE_INIT)

set(CXX_MISSING_FEATURES "")
if (NOT CXX_HAS_LAMBDAS)
	set(CXX_MISSING_FEATURES "${CXX_MISSING_FEATURES} lambdas")
endif()
if(NOT CXX_HAS_THREADS)
	set(CXX_MISSING_FEATURES "${CXX_MISSING_FEATURES} threads")
endif()
if(NOT CXX_HAS_AUTOTYPE)
	set(CXX_MISSING_FEATURES "${CXX_MISSING_FEATURES} autotype")
endif()
if(NOT CXX_HAS_SHARED_PTR)
	set(CXX_MISSING_FEATURES "${CXX_MISSING_FEATURES} shared_ptr")
endif()
if(NOT CXX_HAS_MEMBER_INIT)
	set(CXX_MISSING_FEATURES "${CXX_MISSING_FEATURES} member_init")
endif()
if(NOT CXX_HAS_INLINE_INIT)
	set(CXX_MISSING_FEATURES "${CXX_MISSING_FEATURES} inline_init")
endif()


if (CXX_MISSING_FEATURES)
	message(FATAL_ERROR "Compiler is missing required C++11 features: ${CXX_MISSING_FEATURES}")
endif()

if (CMAKE_CROSSCOMPILING OR WIN32)
	OPTION(BUILD_SHARED_LIBS "Build shared libraries" OFF)
else()
	OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON)
endif()

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

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

OPTION(WITH_CACHE_FILES "Support for caching data in temporary files" ON)

#################################################
# Dependent Libraries
#################################################

################################
# Required
set(USCXML_PREREQS)

# CURL
find_package(CURL)
if (CURL_FOUND)
	include_directories(${CURL_INCLUDE_DIRS})
	list (APPEND USCXML_CORE_LIBS ${CURL_LIBRARIES})

	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)
else()
	message(STATUS "--> will download and compile")
	include("${CMAKE_BUILD_SCRIPTS}/BuildLibCurl.cmake")
	if (LIBCURL_BUILT)
		include_directories(${LIBCURL_INCLUDE_DIR})
		list (APPEND USCXML_CORE_LIBS ${LIBCURL_LIBRARY})
		list (APPEND USCXML_PREREQS libcurl)
		set(CURL_HAS_SMTP ON)
	else()
		message(FATAL_ERROR "Could neither find nor build CURL")
	endif()
endif()


#OpenSSL
find_package(OpenSSL)
# https://slproweb.com/products/Win32OpenSSL.html
if (OPENSSL_FOUND)
	include_directories(${OPENSSL_INCLUDE_DIR})
	list (APPEND USCXML_CORE_LIBS ${OPENSSL_LIBRARIES})
endif()


# XercesC
CheckHasModule(XercesC)
if (HAS_MODULE_XercesC)
	find_package(XercesC)
else()
	# make a quick attempt to find xerces
	INCLUDE(FindPackageHandleStandardArgs)
	find_path(XercesC_INCLUDE_DIR NAMES "xercesc/util/PlatformUtils.hpp")
  find_library(XercesC_LIBRARIES NAMES "xerces-c" "xerces-c_3")
  mark_as_advanced(XercesC_LIBRARIES)
	mark_as_advanced(XercesC_INCLUDE_DIR)
	if (XercesC_INCLUDE_DIR AND XercesC_LIBRARIES)
		message(STATUS "Found XercesC: ${XercesC_LIBRARIES}")
		set(XercesC_FOUND ON)
	else()
		message(STATUS "Could NOT find XercesC (your CMake distribution does not include the FindXercesC module)")
	endif()
endif()

if (XercesC_FOUND)
	include_directories(${XercesC_INCLUDE_DIRS})
	list (APPEND USCXML_CORE_LIBS ${XercesC_LIBRARIES})
	set(XERCESC_NS "xercesc_3_1")
else()
	message(STATUS "--> will download and compile")
	include("${CMAKE_BUILD_SCRIPTS}/BuildXercesC.cmake")
	if (XercesC_BUILT)
		include_directories(${XercesC_INCLUDE_DIRS})
		list (APPEND USCXML_CORE_LIBS ${XercesC_LIBRARIES})
		list (APPEND USCXML_PREREQS xerces-c)
		set(XERCESC_NS "xercesc_3_1")
	else()
		message(FATAL_ERROR "Could neither find nor build XercesC")
	endif()
endif()

# LibEvent
find_package(LibEvent)
if (LIBEVENT_FOUND)
	include_directories(${LIBEVENT_INCLUDE_DIR})
	list (APPEND USCXML_CORE_LIBS ${LIBEVENT_LIBRARIES})

	include(CheckCXXSourceCompiles)
	set(CMAKE_REQUIRED_INCLUDES ${LIBEVENT_INCLUDE_DIR})
	set(CMAKE_REQUIRED_LIBRARIES ${LIBEVENT_LIBRARIES})
	check_cxx_source_compiles("
		#include <event2/http.h>
		struct bufferevent* cb(struct event_base *base, void *arg) { return NULL; }
		int main(){ evhttp_set_bevcb((struct evhttp*)NULL, cb, (void*)NULL); }
	" LIBEVENT_HAS_BEVCB)
	if (NOT LIBEVENT_HAS_BEVCB)
		message(STATUS "libevent (< 2.1) does not feature evhttp_set_bevcb, not building HTTPS support")
	endif()
else()
	message(STATUS "--> will download and compile")
	include("${CMAKE_BUILD_SCRIPTS}/BuildLibEvent.cmake")
	if (LIBEVENT_BUILT)
		SET(LIBEVENT_HAS_BEVCB YES)
		include_directories(${LIBEVENT_INCLUDE_DIR})
		list (APPEND USCXML_CORE_LIBS ${LIBEVENT_LIBRARIES})
		list (APPEND USCXML_PREREQS libevent)
	else()
		message(FATAL_ERROR "Could neither find nor build libevent")
	endif()
endif()
if (";${LIBEVENT_LIBRARIES};" MATCHES "openssl")
	# see https://stackoverflow.com/a/23326955/990120
	set(EVENT_SSL_FOUND ON)
endif()


if (UNIX)
	list (APPEND USCXML_CORE_LIBS "pthread")
endif()


if (OPENSSL_FOUND AND LIBEVENT_HAS_BEVCB AND EVENT_SSL_FOUND)
	set(HTTPS_ENABLED ON)
endif()


# uriparser - part of contrib now
# find_package(uriparser)
# if(URIPARSER_FOUND)
#     include_directories(${URIPARSER_INCLUDE_DIRS})
# 	list (APPEND USCXML_CORE_LIBS ${URIPARSER_LIBRARIES})
# else()
#     message(STATUS "Did not find UriParser")
#     message(STATUS "--> will download and compile")
#     include("${CMAKE_BUILD_SCRIPTS}/BuildURIParser.cmake")
#     if (URIPARSER_BUILT)
#         include_directories(${URIPARSER_INCLUDE_DIR})
#         list (APPEND USCXML_CORE_LIBS ${URIPARSER_LIBRARY})
#         list (APPEND USCXML_PREREQS uriparser)
#     else()
#         message(FATAL_ERROR "Could not find or build uriparser")
#     endif()
# endif()


################################
# Optional

#swig
if (WIN32)
	if(EXISTS "${PROJECT_BINARY_DIR}/../swig/")
		LIST(APPEND CMAKE_PROGRAM_PATH "${PROJECT_BINARY_DIR}/../swig/")
	elseif(EXISTS "${PROJECT_BINARY_DIR}/../../swig/")
		LIST(APPEND CMAKE_PROGRAM_PATH "${PROJECT_BINARY_DIR}/../../swig/")
	endif()

	file(GLOB POTENTIAL_SWIG "C:/Program Files/swig*" "C:/Program Files (x86)/swig*")
	LIST(APPEND CMAKE_PROGRAM_PATH ${POTENTIAL_SWIG}) # swig.exe
	# message(FATAL_ERROR "POTENTIAL_SWIG: ${POTENTIAL_SWIG}")
	
endif()
LIST(APPEND CMAKE_PROGRAM_PATH $ENV{SWIG_DIR})
find_package(SWIG)


# JavaScriptCore
# https://github.com/Lichtso/JSC-Standalone
OPTION(WITH_DM_ECMA_JSC "Do search for JavaScriptCore" ON)
if (WITH_DM_ECMA_JSC)
	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})
	else()
		set(WITH_DM_ECMA_JSC OFF)
	endif()
endif()

# V8
OPTION(WITH_DM_ECMA_V8 "Do search for the V8 ECMAScript implementation" ON)
if (WITH_DM_ECMA_V8)
	find_package(V8)
	if (V8_FOUND)
		set(ECMA_FOUND ON)
		include_directories(${V8_INCLUDE_DIR})
		list (APPEND USCXML_OPT_LIBS ${V8_LIBRARY})
	else()
		set(WITH_DM_ECMA_V8 OFF)
	endif()
endif()

# Lua
OPTION(WITH_DM_LUA "Do search for the Lua libraries" ON)
if (WITH_DM_LUA)
	if (WIN32)
		# LuaForWindows:
		# https://code.google.com/archive/p/luaforwindows/downloads
		# https://github.com/rjpcomputing/luaforwindows/releases
		set(ENV{LUA_DIR} "C:/Program Files (x86)/Lua/5.1/")
		
		# http://luabinaries.sourceforge.net/download.html
		# set(ENV{LUA_DIR} "C:/Program Files (x86)/Lua/5.3/")
	endif()
	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})
	else()
		set(WITH_DM_LUA OFF)
	endif()
endif()

# Python
OPTION(WITH_DM_PYTHON "Do search for the Python libraries" ON)
if (WITH_DM_PYTHON)
	find_package(PythonLibs)
	if (PYTHONLIBS_FOUND)
		include_directories (${PYTHON_INCLUDE_DIRS})
		list (APPEND USCXML_OPT_LIBS ${PYTHON_LIBRARIES})
	else()
		set(WITH_DM_PYTHON ON)
	endif()
endif()

OPTION(WITH_DM_C89 "Do build with C89 datamodel support" OFF)
if (WITH_DM_C89)
	include("${CMAKE_BUILD_SCRIPTS}/BuildPicoC.cmake")
	message(STATUS "Did not look for picoc interpreter")
	message(STATUS "--> will download and compile")
	if (PICOC_BUILT)
		include_directories(${PICOC_INCLUDE_DIR})
		list (APPEND USCXML_CORE_LIBS ${PICOC_LIBRARY})
		list (APPEND USCXML_PREREQS picoc)
	else()
		message(FATAL_ERROR "Could not build picoc")
	endif()
endif()

OPTION(WITH_DM_PROMELA "Do build with promela datamodel support" ON)
if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
	SET_SOURCE_FILES_PROPERTIES(src/uscxml/plugins/datamodel/promela/parser/promela.lex.yy.cpp PROPERTIES COMPILE_FLAGS -Wno-deprecated-register )
endif()

add_subdirectory(src/uscxml)
add_subdirectory(src/bindings)

if (BUILD_AS_PLUGINS)
	file(GLOB USCXML_PLUGINS
	  ${CMAKE_SOURCE_DIR}/contrib/src/Pluma/*.cpp
	)
	include_directories(${CMAKE_SOURCE_DIR}/contrib/src)
	source_group("Plugins" FILES ${USCXML_PLUGINS})
	list (APPEND USCXML_FILES ${USCXML_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()

set_property(TARGET uscxml PROPERTY CXX_STANDARD 11)
set_property(TARGET uscxml PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET uscxml PROPERTY SOVERSION ${USCXML_VERSION})
if (USCXML_PREREQS)
    add_dependencies(uscxml ${USCXML_PREREQS})
endif()
install_library(TARGETS uscxml)


add_library(uscxml_transform ${USCXML_TRANSFORM_FILES})
set_property(TARGET uscxml_transform PROPERTY CXX_STANDARD 11)
set_property(TARGET uscxml_transform PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET uscxml_transform PROPERTY SOVERSION ${USCXML_VERSION})
target_link_libraries(uscxml_transform uscxml)
install_library(TARGETS uscxml_transform)

if (NOT CMAKE_CROSSCOMPILING)
	enable_testing()
	add_subdirectory(test)
endif()

add_executable(uscxml-browser apps/uscxml-browser.cpp ${GETOPT_FILES})
set_property(TARGET uscxml-browser PROPERTY CXX_STANDARD 11)
set_property(TARGET uscxml-browser PROPERTY CXX_STANDARD_REQUIRED ON)
install_executable(TARGETS uscxml-browser COMPONENT tools)
target_link_libraries(uscxml-browser uscxml)

add_executable(uscxml-transform apps/uscxml-transform.cpp ${GETOPT_FILES})
set_property(TARGET uscxml-transform PROPERTY CXX_STANDARD 11)
set_property(TARGET uscxml-transform PROPERTY CXX_STANDARD_REQUIRED ON)
install_executable(TARGETS uscxml-transform COMPONENT tools)
target_link_libraries(uscxml-transform uscxml uscxml_transform)

############################################################
# Documentation
############################################################

add_subdirectory(docs)

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

# determine path seperator
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)

############################################################
# Sort files into goups in IDEs
############################################################


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)
	file(RELATIVE_PATH REL_PATH ${PROJECT_SOURCE_DIR} "${PATH}")
	string(REGEX REPLACE "src/uscxml" "" REL_PATH "${REL_PATH}")
	string(REGEX REPLACE "contrib/src" "contrib" REL_PATH "${REL_PATH}")
	
	string(REGEX REPLACE "^/" "" REL_PATH "${REL_PATH}") # leading slash
	string(REGEX REPLACE "/" "\\\\" SRC_GROUP "${REL_PATH}") # escape /
	
	source_group("${SRC_GROUP}" FILES ${FILE})

endforeach()


# all variables that cmake knows about
# get_cmake_property(_variableNames VARIABLES)
# foreach (_variableName ${_variableNames})
#     message(STATUS "${_variableName}=${${_variableName}}")
# endforeach()

############################################################
# Some concluding remarks
############################################################
message(STATUS "")

set(USCXML_LIBS ${USCXML_OPT_LIBS} ${USCXML_CORE_LIBS})
list(SORT USCXML_LIBS)
message(STATUS "Linking against libraries (check if you need to export in LD_LIBRARY_PATH or PATH):")
set(SKIP_NEXT OFF)
foreach(LIBRARY ${USCXML_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()
	else()
		if (NOT SKIP_NEXT)
			STRING(REGEX REPLACE "${CMAKE_BINARY_DIR}" "BUILD_DIR" LIBRARY ${LIBRARY})
			STRING(REGEX REPLACE "${CMAKE_SOURCE_DIR}" "SOURCE_DIR" LIBRARY ${LIBRARY})
			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 (CMAKE_BUILD_TYPE)
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UC)
endif()
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}})

get_directory_property( CDEFLIST DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS )
foreach( DEF ${CDEFLIST} )
    set( CDEFS "${CDEFS} ${DEF}" )
endforeach()
message(STATUS "  Definitions : " ${CDEFS})

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)