~zorba-coders/zorba/bug1151967

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
# Copyright 2006-2010 The FLWOR Foundation.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
PROJECT(zorba)
ENABLE_TESTING()
INCLUDE(CTest)


# overwriting the source and binary directories with the current zorba ones
# this is usefull for other projects reusing zorba as a subcomponent
SET(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})

INCLUDE(${CMAKE_SOURCE_DIR}/cmake_modules/CMakeCompareVersionStrings.cmake)

cmake_minimum_required(VERSION 2.6)
IF(CMAKE_VERSION STREQUAL "2.6.3")
  MESSAGE(FATAL_ERROR "You are running a buggy CMake version (2.6.3). Please install a different CMake version and execute CMake again.")
ENDIF(CMAKE_VERSION STREQUAL "2.6.3")
IF(COMMAND cmake_policy)
  cmake_policy(SET CMP0003 NEW)
  cmake_policy(SET CMP0002 OLD)
  # We want CMake to correctly count empty list elements.
  cmake_policy(SET CMP0007 NEW)
ENDIF(COMMAND cmake_policy)

IF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
  SET(LINUX "${CMAKE_SYSTEM}")
ENDIF("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")

# custom ctest options
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.cmake)

# prohibit in-source-builds
IF(${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
  MESSAGE(STATUS "In-source-builds are not allowed")
  MESSAGE(STATUS "Clean your source directory (e.g. delete the CMakeCache.txt file)")
  MESSAGE(FATAL_ERROR "Please create a separate build directory and call CMake again")
ENDIF(${CMAKE_BINARY_DIR} STREQUAL ${CMAKE_SOURCE_DIR})

# set the default build type to release
# NOTE: the "NMake Makefiles" generator will always set the default build type to "Debug"
IF (NOT CMAKE_BUILD_TYPE)
  SET (CMAKE_BUILD_TYPE Release CACHE STRING
       "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
       FORCE)
ENDIF (NOT CMAKE_BUILD_TYPE)

# set the default installation directory to "dist" in the build directory
IF (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  SET (CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/dist CACHE STRING
       "Set the defalt installation directory to \"dist\" in the binary directory." FORCE)
ENDIF (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

# Enable RPaths in installed binaries
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# turn on the folder grouping used by IDEs (VS or XCode)
SET(ZORBA_USE_TARGET_FOLDERS ON CACHE BOOL "Activate use of target grouping into folders")
IF(ZORBA_USE_TARGET_FOLDERS)
  SET_PROPERTY (GLOBAL PROPERTY USE_FOLDERS ON)
ENDIF(ZORBA_USE_TARGET_FOLDERS)

###############################################################################
# Check for system include files, features, etc.
###############################################################################
INCLUDE(CheckCXXSourceCompiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckIncludeFileCXX)
INCLUDE(CheckIncludeFiles)
INCLUDE(CheckLibraryExists)
INCLUDE(CheckStructHasMember)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckTypeSize)
INCLUDE(CheckVariableExists)

CHECK_INCLUDE_FILES ("unicode/coll.h"               ZORBA_HAVE_COLL_H)
CHECK_INCLUDE_FILES ("iconv.h"                      ZORBA_HAVE_ICONV_H)
CHECK_INCLUDE_FILES ("inttypes.h"                   ZORBA_HAVE_INTTYPES_H)
CHECK_INCLUDE_FILES ("limits.h"                     ZORBA_HAVE_LIMITS_H)
CHECK_INCLUDE_FILES ("stdint.h"                     ZORBA_HAVE_STDINT_H)
CHECK_INCLUDE_FILES ("stdlib.h"                     ZORBA_HAVE_STDLIB_H)
CHECK_INCLUDE_FILES ("sys/param.h;sys/mount.h"      ZORBA_HAVE_SYS_MOUNT_H)
CHECK_INCLUDE_FILES ("sys/types.h"                  ZORBA_HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILES ("tzfile.h"                     ZORBA_HAVE_TZFILE_H)
CHECK_INCLUDE_FILES ("unicode/ustring.h"            ZORBA_HAVE_USTRING_H)
CHECK_INCLUDE_FILES ("unicode/utypes.h"             ZORBA_HAVE_UTYPES_H)

IF (NOT APPLE OR ${CMAKE_SYSTEM_VERSION} VERSION_GREATER "10.4")
  # execinfo is found by this macro when cross compiling for Mac OS X 10.4
  # although it shouldn't be found for this platform
  CHECK_INCLUDE_FILES ("execinfo.h"                   ZORBA_HAVE_EXECINFO_H)
ENDIF (NOT APPLE OR ${CMAKE_SYSTEM_VERSION} VERSION_GREATER "10.4")
CHECK_INCLUDE_FILE_CXX ("FlexLexer.h"               ZORBA_HAVE_FLEXLEXER_H)
CHECK_INCLUDE_FILES ("uuid/uuid.h"                  ZORBA_HAVE_UUID_H)

CHECK_FUNCTION_EXISTS (strtof                     ZORBA_HAVE_STRTOF_FUNCTION)
CHECK_FUNCTION_EXISTS (strtoll                    ZORBA_HAVE_STRTOLL_FUNCTION)
CHECK_FUNCTION_EXISTS (_stricmp                   ZORBA_HAVE_STRICMP_FUNCTION)
CHECK_FUNCTION_EXISTS (strcat_s                   ZORBA_HAVE_STRCAT_S_FUNCTION)
CHECK_FUNCTION_EXISTS (strcpy_s                   ZORBA_HAVE_STRCPY_S_FUNCTION)

SET(CMAKE_REQUIRED_LIBRARIES pthread)  
CHECK_INCLUDE_FILES (pthread.h                    ZORBA_HAVE_PTHREAD_H)
CHECK_FUNCTION_EXISTS(pthread_spin_init           ZORBA_HAVE_PTHREAD_SPINLOCK)
CHECK_FUNCTION_EXISTS(pthread_mutex_init          ZORBA_HAVE_PTHREAD_MUTEX)
SET(CMAKE_REQUIRED_LIBRARIES)  

CHECK_INCLUDE_FILES ("sys/time.h;sys/resource.h"  ZORBA_HAVE_RUSAGE_INCLUDES)
CHECK_FUNCTION_EXISTS (getrusage                  ZORBA_HAVE_RUSAGE_FUNCTION)

CHECK_TYPE_SIZE("int32_t" ZORBA_HAVE_INT32_T) 
CHECK_TYPE_SIZE("uint32_t" ZORBA_HAVE_UINT32_T) 
CHECK_TYPE_SIZE("unsigned __int32" ZORBA_HAVE_MS_UINT32) 
CHECK_TYPE_SIZE("__int32" ZORBA_HAVE_MS_INT32) 
CHECK_TYPE_SIZE("int64_t" ZORBA_HAVE_INT64_T) 

CHECK_STRUCT_HAS_MEMBER("struct tm" tm_gmtoff time.h 	ZORBA_HAVE_STRUCT_TM_TM_GMTOFF)
CHECK_STRUCT_HAS_MEMBER("struct tm" __tm_gmtoff time.h 	ZORBA_HAVE_STRUCT_TM___TM_GMTOFF)

# C++ built-in type sizes
CHECK_TYPE_SIZE("double" ZORBA_SIZEOF_DOUBLE BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("float" ZORBA_SIZEOF_FLOAT BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("int" ZORBA_SIZEOF_INT BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("long" ZORBA_SIZEOF_LONG BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("long long" ZORBA_SIZEOF_LONG_LONG BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("void*" ZORBA_SIZEOF_POINTER BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("short" ZORBA_SIZEOF_SHORT BUILTIN_TYPES_ONLY) 
CHECK_TYPE_SIZE("size_t" ZORBA_SIZEOF_SIZE_T) 
SET(CMAKE_EXTRA_INCLUDE_FILES wchar.h)
CHECK_TYPE_SIZE("wchar_t" ZORBA_SIZEOF_WCHAR_T)
SET(CMAKE_EXTRA_INCLUDE_FILES)

# C++11 langauge features
CHECK_CXX_SOURCE_COMPILES(
  "int main() { int *p = nullptr; }" ZORBA_CXX_NULLPTR)
CHECK_CXX_SOURCE_COMPILES(
  "int main() { static_assert(1,\"\"); }" ZORBA_CXX_STATIC_ASSERT)

# C++11 standard library types
CHECK_CXX_SOURCE_COMPILES (
  "#include <type_traits>
  int main() { std::enable_if<true,int> x; }" ZORBA_HAVE_ENABLE_IF)
CHECK_CXX_SOURCE_COMPILES ("
  #include <memory>
  int main() { std::unique_ptr<int> p; }" ZORBA_HAVE_UNIQUE_PTR)
CHECK_CXX_SOURCE_COMPILES (
  "#include <unordered_map>
  int main() { std::unordered_map<int,int> m; }" ZORBA_HAVE_UNORDERED_MAP)
CHECK_CXX_SOURCE_COMPILES (
  "#include <unordered_set>
  int main() { std::unordered_set<int> s; }" ZORBA_HAVE_UNORDERED_SET)

################################################################################
# Various cmake macros

SET (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules ${CMAKE_MODULE_PATH})
IF (WIN32)
  # On Windows we use proxy modules that try to guess first the location
  # of the required third party libraries. This will search in order in:
  # 1. the path pointed by ZORBA_THIRD_PARTY_REQUIREMENTS
  # 2. the Program Files directory available on the users computer
  # 3. the PATH environment variable
  # The logic is implemented by the macros in the ProxyFindModule.cmake module.
  SET (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules/Windows ${CMAKE_MODULE_PATH})
  INCLUDE (ProxyFindModule)
ENDIF (WIN32)

################################################################################
# This section is used for batch script and Visual Studio Project file generation. 
# Each executable on Windows needs a bat script and in the Visual Studio build a \
# pre-generated Visual Studio Project file in order to set the PATH to the required DLLs.
# For this we use CACHE variables to gather all the information generated
# throughout the cmake execution (required DLLs, list of scripts to generate).
# The generation occurs only at the end of CMake execution (through the macro
# ZORBA_GENERATE_EXE_HELPERS_WIN32) when ALL the requirements have been found.
# E.g. zorba.exe needs in PATH all the DLLs of all external modules, etc.
IF (WIN32)
  # Clear all the variables because they are used only for this build.
  SET (ZORBA_REQUIRED_DLLS ""
    CACHE STRING "List of DLLs that must be installed" FORCE
  )
  SET (ZORBA_REQUIRED_DLL_PATHS ""
    CACHE STRING "List of paths executable require in order to find the required DLLs" FORCE
  )
  SET (ZORBA_EXE_SCRIPT_LIST ""
    CACHE STRING "List of Windows batch scripts to be generated, one for each executable" FORCE
  )
  IF (MSVC_IDE)
    SET (ZORBA_VC_PROJECT_FILE_LIST ""
      CACHE STRING "List of Visual Studio project files to be generated, one for each executable" FORCE
    )
  ENDIF(MSVC_IDE)

  # Moreover, mark it as advanced since the CMake GUI user should not see this.
  MARK_AS_ADVANCED (FORCE ZORBA_REQUIRED_DLLS)
  MARK_AS_ADVANCED (FORCE ZORBA_REQUIRED_DLL_PATHS)
  MARK_AS_ADVANCED (FORCE ZORBA_EXE_SCRIPT_LIST)
  IF (MSVC_IDE)
    MARK_AS_ADVANCED (FORCE ZORBA_VC_PROJECT_FILE_LIST)
  ENDIF(MSVC_IDE)
ENDIF (WIN32)
################################################################################


# ZorbaModule includes a number of generally-useful utility functions,
# including expected_failure().  Include it now (very early) to ensure
# the "expected_failure()" function is available to anyone who might
# add tests.
INCLUDE(ZorbaModule)

################################################################################
#                                                                              #
# Search for all the external libraries that any library or executable under   #
# this CMake depends on.                                                       #
#                                                                              #
################################################################################

FOREACH(path "${CMAKE_PREFIX_PATH}")
  IF(EXISTS "${path}")
    MESSAGE("path entry: ${path}")
  ELSE(EXISTS "${path}")
    # Ignore non-existing paths
    #MESSAGE("Non-existing path!: ${path}")
  ENDIF(EXISTS "${path}")
ENDFOREACH(path)

SET(requiredlibs)
SET(requiredlibs-store)

# Check for POSIX high-resolution timers - might be in "rt" library
CHECK_LIBRARY_EXISTS(rt clock_gettime ""  _clock_gettime_in_rt)
IF(_clock_gettime_in_rt)
  SET(requiredlibs ${requiredlibs} rt)
  SET(ZORBA_HAVE_CLOCKGETTIME_FUNCTION YES CACHE BOOL "Platform has clock_gettime (in librt)")
ELSE(_clock_gettime_in_rt)
  CHECK_FUNCTION_EXISTS(clock_gettime   ZORBA_HAVE_CLOCKGETTIME_FUNCTION)
ENDIF(_clock_gettime_in_rt)

#
# TcMalloc support
#
IF(ZORBA_USE_TCMALLOC)
  MESSAGE (STATUS "Looking for TcMalloc")

  FIND_PACKAGE(TcMalloc)
  IF(Tcmalloc_FOUND)
    SET(requiredlibs ${requiredlibs} ${Tcmalloc_LIBRARIES})
  ELSE(Tcmalloc_FOUND)
    MESSAGE(FATAL_ERROR "TcMalloc not found")
  ENDIF(Tcmalloc_FOUND)
ENDIF(ZORBA_USE_TCMALLOC)

#
# GooglePerfTools support
#
IF(ZORBA_WITH_CPU_PROFILE)
  MESSAGE(STATUS "Looking for GooglePerfTools")

  FIND_PACKAGE(GooglePerfTools)
  IF(GooglePerfTools_FOUND)
    SET(requiredlibs ${requiredlibs} ${GooglePerfTools_LIBRARIES})
  ELSE(GooglePerfTools_FOUND)
    MESSAGE(FATAL_ERROR "GooglePerfTools not found. Cannot build with CPU profiling")
  ENDIF(GooglePerfTools_FOUND)
ENDIF(ZORBA_WITH_CPU_PROFILE)

#
#  LibXml2
#
MESSAGE(STATUS "Looking for LibXml2")
FIND_PACKAGE(LibXml2)
IF(LIBXML2_FOUND)
  # Now check LibXml2's version
  FILE(STRINGS ${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h LIBXML2_VERSION_TMP REGEX 
       "^#define[ \t]+LIBXML_DOTTED_VERSION[ \t]+\".*\"")
  STRING(REGEX REPLACE "^#define[ \t]+LIBXML_DOTTED_VERSION[ \t]+\"(.*)\"" "\\1" 
         LIBXML2_VERSION ${LIBXML2_VERSION_TMP})
         
  IF(LIBXML2_VERSION VERSION_EQUAL "2.7.0" OR LIBXML2_VERSION VERSION_GREATER "2.7.0")
    MESSAGE(STATUS "Found LIBXML2 library -- " ${LIBXML2_LIBRARIES})
    INCLUDE_DIRECTORIES(${LIBXML2_INCLUDE_DIR})
    SET(requiredlibs-store ${requiredlibs-store} ${LIBXML2_LIBRARIES})
    SET(CXXFLAGS ${CXXFLAGS} ${LIBXML2_DEFINITONS})
  ELSE(LIBXML2_VERSION VERSION_EQUAL "2.7.0" OR LIBXML2_VERSION VERSION_GREATER "2.7.0")
    MESSAGE(FATAL_ERROR "The libxml2 library version 2.7.0 or higher is required in order to build Zorba.")
  ENDIF(LIBXML2_VERSION VERSION_EQUAL "2.7.0" OR LIBXML2_VERSION VERSION_GREATER "2.7.0")

ELSE(LIBXML2_FOUND)
  MESSAGE(FATAL_ERROR "The libxml2 library and headers are required in order to build simple store.")
ENDIF(LIBXML2_FOUND)
MESSAGE(STATUS "")

#
# Pthreads
#
IF(ZORBA_HAVE_PTHREAD_H AND NOT ZORBA_FOR_ONE_THREAD_ONLY)
  SET(requiredlibs ${requiredlibs} pthread)
ENDIF(ZORBA_HAVE_PTHREAD_H AND NOT ZORBA_FOR_ONE_THREAD_ONLY)

#
# Iconv
#
IF(WIN32)
  MESSAGE(STATUS "Looking for Iconv")
  FIND_PACKAGE(Iconv REQUIRED)

  IF(ICONV_FOUND)
    MESSAGE(STATUS "Found Iconv library -- " ${ICONV_LIBRARY})
    INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR})
    SET(requiredlibs ${requiredlibs} ${ICONV_LIBRARY})
  ELSE(ICONV_FOUND)
    MESSAGE(STATUS "The Iconv library was not found.")
  ENDIF(ICONV_FOUND)
  MESSAGE(STATUS "")
ENDIF(WIN32)

#
# ICU
#
MESSAGE(STATUS "Looking for ICU")
FIND_PACKAGE(ICU)
  
IF(ICU_FOUND AND ICU_I18N_FOUND AND ICU_DATA_FOUND)
  MESSAGE(STATUS "Found ICU library -- " ${ICU_LIBRARIES})

  INCLUDE_DIRECTORIES(${ICU_INCLUDE_DIRS})
  SET(requiredlibs ${requiredlibs} ${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES} ${ICU_DATA_LIBRARIES}) 
ELSE(ICU_FOUND AND ICU_I18N_FOUND AND ICU_DATA_FOUND)
  MESSAGE(FATAL_ERROR "The ICU library is required in order to build Zorba.")
ENDIF(ICU_FOUND AND ICU_I18N_FOUND AND ICU_DATA_FOUND)       
MESSAGE(STATUS "")

#
# Xerces-C part 1 - finding in default locations
#
MESSAGE (STATUS "Looking for Xerces-C")
FIND_PACKAGE (XercesC)
MESSAGE (STATUS "")

#
# shlwapi
#
IF(WIN32)
    SET(requiredlibs ${requiredlibs} "shlwapi")
ENDIF(WIN32)

#
# SWIG
#
IF (NOT ZORBA_SUPPRESS_SWIG)
  FIND_PACKAGE (SWIG)

  COMPARE_VERSION_STRINGS("${SWIG_VERSION}" "1.3.40" result)
  IF(result LESS 0)
    MESSAGE(WARNING "You need at least SWIG version 1.3.40")
  ENDIF(result LESS 0)


  IF (SWIG_FOUND)
    # this will be used both in swig and doc sub-directories

    #
    # PHP5
    #
    FIND_PACKAGE (PHP5)

    #
    # Python
    #
    FIND_PACKAGE (PythonInterp)
    FIND_PACKAGE (PythonLibs)
    
  ELSE (SWIG_FOUND)
    MESSAGE (STATUS "SWIG not found => no language bindings are generated.")  
  ENDIF (SWIG_FOUND)
ELSE (NOT ZORBA_SUPPRESS_SWIG)
  MESSAGE (STATUS "SWIG is disabled => no language bindings are generated.")  
ENDIF (NOT ZORBA_SUPPRESS_SWIG)


LIST(APPEND requiredlibs ${requiredlibs-store})

################################################################################
# Setup Compiler Options
# Note that this needs to be done after all packages are found because
# it sets variables based on whether packages have been found or not.
INCLUDE(CMakeConfiguration.txt)
INCLUDE(CMakeCompiler.txt)

#
#  LibXslt
#
IF(ZORBA_XQUERYX)
  MESSAGE(STATUS "Looking for LibXslt")
  FIND_PACKAGE(LibXslt)
  IF(LIBXSLT_FOUND)
    MESSAGE(STATUS "Found LIBXSLT library -- " ${LIBXSLT_LIBRARIES})

    INCLUDE_DIRECTORIES(${LIBXSLT_INCLUDE_DIR})
    SET(requiredlibs ${requiredlibs} ${LIBXSLT_LIBRARIES})
    SET(CXXFLAGS ${CXXFLAGS} ${LIBXSLT_DEFINITONS})
  ELSE(LIBXSLT_FOUND)
    MESSAGE(FATAL_ERROR "The libxslt library is required in order to build zorba with xqueryx capabilities.")
  ENDIF(LIBXSLT_FOUND)
  MESSAGE(STATUS "")
ENDIF(ZORBA_XQUERYX)


#
# Xerces-C part 2 - version verification
#
IF (NOT ZORBA_NO_XMLSCHEMA)
  # Find XercesC Version based on ${XERCESC_INCLUDE}
  FIND_PACKAGE(XercesCVersion)
  
  IF (XERCESC_FOUND)
    COMPARE_VERSION_STRINGS("${XERCESC_VERSION}" 2.8.0 result)

    IF (result LESS 0)
      MESSAGE(FATAL_ERROR "Xerces-C ${XERCESC_VERSION} not supported.
	Only 2.8.0, 3.0.1 and newer versions are supported
	or
	Use -D ZORBA_NO_XMLSCHEMA=ON option for building without XMLSchema support.")
    ELSE (result LESS 0)
      MESSAGE(STATUS "Found Xerces ${XERCESC_VERSION} -- " ${XERCESC_INCLUDE}  ${XERCESC_LIBRARY})
      INCLUDE_DIRECTORIES(${XERCESC_INCLUDE})
      SET(requiredlibs ${requiredlibs} ${XERCESC_LIBRARY})
    ENDIF (result LESS 0)
  ELSE  (XERCESC_FOUND)
    MESSAGE(FATAL_ERROR "Xerces-C not found.
	Make sure you have Xerces-C 2.8.0 or 3.0.1+ installed
	or
	Use -D ZORBA_NO_XMLSCHEMA=ON option for building without XMLSchema support.")    
  ENDIF (XERCESC_FOUND)
ELSE (NOT ZORBA_NO_XMLSCHEMA)
  MESSAGE(STATUS "ZORBA_NO_XMLSCHEMA: ${ZORBA_NO_XMLSCHEMA}"
    " - build without XMLSchema support")
ENDIF (NOT ZORBA_NO_XMLSCHEMA)

IF(WIN32)
  SET(requiredlibs ${requiredlibs} wsock32)
  
  # Clear those variables because this is used only for this build.
  # A cache variable is needed since this must be modified by subdirectories
  # and be visible at this level when ZORBA_GENERATE_EXE_HELPERS_WIN32 is called
  SET(ZORBA_EXE_SCRIPT_LIST "" CACHE STRING "List of bat scripts" FORCE)
  SET(ZORBA_VC_PROJECT_FILE_LIST "" CACHE STRING "List of VC project files" FORCE)
ENDIF(WIN32)

################################################################################
# Variables
SET(DEFINITIONS)

################################################################################
# Custom user-defined settings
INCLUDE ("CMakeUser.txt" OPTIONAL)


################################################################################
# Compiler setup

# add the include dir (i.e. the library) to the include path
INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_BINARY_DIR}/include)
INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/include)

# zorba versioning
SET(ZORBA_MAJOR_NUMBER "2")
SET(ZORBA_MINOR_NUMBER "9")
SET(ZORBA_PATCH_NUMBER "0")
SET(ZORBA_VERSION ${ZORBA_MAJOR_NUMBER}.${ZORBA_MINOR_NUMBER}.${ZORBA_PATCH_NUMBER})
MESSAGE(STATUS "Zorba version number: ${ZORBA_VERSION}")

SET(ZORBA_LIB_DIRNAME "lib" CACHE STRING
  "directory name for libs (may want to set to 'lib64' on 64-bit Linux platforms)")

# Directories for modules and schemas. "Core" are those which are
# shipped with Zorba and must be present for correct Zorba
# functionality. "Non-core" are developed independently of Zorba and
# have different version lifecycles.
#
# URI dir is for things resolved by URI - specifically modules (.xq
# files) and schemas (.xsd files) - which are platform-
# independent. Lib dir is for platform-specific files, specifically
# shared libraries for module external functions.
SET(ZORBA_NONCORE_URI_DIR "share/zorba/uris"
  CACHE STRING "Path (relative to installdir) to non-core modules/schemas")
MARK_AS_ADVANCED(ZORBA_NONCORE_URI_DIR)
SET(ZORBA_CORE_URI_DIR "${ZORBA_NONCORE_URI_DIR}/core/${ZORBA_VERSION}"
  CACHE STRING "Path (relative to installdir) to core modules/schemas")
MARK_AS_ADVANCED(ZORBA_CORE_URI_DIR)
SET(ZORBA_NONCORE_LIB_DIR "${ZORBA_LIB_DIRNAME}/zorba"
  CACHE STRING "Path (relative to installdir) to non-core libraries")
MARK_AS_ADVANCED(ZORBA_NONCORE_LIB_DIR)
SET(ZORBA_CORE_LIB_DIR "${ZORBA_NONCORE_LIB_DIR}/core/${ZORBA_VERSION}"
  CACHE STRING "Path (relative to installdir) to core libraries")
MARK_AS_ADVANCED(ZORBA_CORE_LIB_DIR)

# add compiler variables
ADD_DEFINITIONS(${DEFINITIONS})


################################################################################
# find all dylib files (i.e. transitive closure of the library files
# that are linked)
IF(APPLE)
  EXECUTE_PROCESS(
    COMMAND ${CMAKE_SOURCE_DIR}/scripts/find_macosx_libraries.sh ${ICU_LIBRARIES}
    OUTPUT_VARIABLE Zorba_ICUUC)
  EXECUTE_PROCESS(
    COMMAND ${CMAKE_SOURCE_DIR}/scripts/find_macosx_libraries.sh ${ICU_I18N_LIBRARIES}
    OUTPUT_VARIABLE Zorba_ICUI18N)
  EXECUTE_PROCESS(
    COMMAND ${CMAKE_SOURCE_DIR}/scripts/find_macosx_libraries.sh ${ICU_DATA_LIBRARIES}
    OUTPUT_VARIABLE Zorba_ICU_DATA)
  EXECUTE_PROCESS(
    COMMAND ${CMAKE_SOURCE_DIR}/scripts/find_macosx_libraries.sh ${XERCESC_LIBRARY}
    OUTPUT_VARIABLE Zorba_XERCESC)
  EXECUTE_PROCESS(
    COMMAND ${CMAKE_SOURCE_DIR}/scripts/find_macosx_libraries.sh ${LIBXML2_LIBRARIES}
    OUTPUT_VARIABLE Zorba_XML2)
ENDIF(APPLE)


SET (ZORBA_MODULES_DIR "${CMAKE_SOURCE_DIR}/../zorba_modules"
  CACHE PATH "Path to directory (outside of Zorba svn root) containing external modules to add to the build")

# Subdir of the installation directory for CMake files, relative to
# CMAKE_INSTALL_PREFIX.
IF (WIN32)
  SET(ZORBA_CMAKE_DIR "cmake" CACHE STRING
    "Relative path to CMake files in Zorba installation")
ELSE (WIN32)
  SET(ZORBA_CMAKE_DIR "share/cmake/zorba-${ZORBA_VERSION}" CACHE STRING
    "Relative path to CMake files in Zorba installation")
ENDIF (WIN32)

################################################################################
#                                                                              #
# Tell CMake to process the sub-directories                                    #
#                                                                              #
################################################################################

ADD_SUBDIRECTORY(scripts)
ADD_SUBDIRECTORY(cmake_modules)

# Subdirectory ordering: We need to include "bin" before "test" so
# that test/fots can add zorbacmd-based tests. We need to include
# "test" before "config" so ZorbaConfig knows about testdriver. We
# need to include "config" before "modules" so external modules will
# be able to find ZorbaConfig.cmake. We need to include "modules"
# before "include" so config.h can know eg. whether we found CURL. We
# need to include "modules" before "doc" so xqdoc can know what
# non-core modules are available.

ADD_SUBDIRECTORY(bin)

ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(config)
ADD_SUBDIRECTORY(schemas)
ADD_SUBDIRECTORY(modules)
ADD_SUBDIRECTORY(doc)
ADD_SUBDIRECTORY(include)

ADD_DEFINITIONS(-Dzorba_EXPORTS)
ADD_SUBDIRECTORY(src)
REMOVE_DEFINITIONS(-Dzorba_EXPORTS)

IF (SWIG_FOUND)
  ADD_SUBDIRECTORY (swig)
ENDIF (SWIG_FOUND)

# add the uninstall support
# QQQ move to config/
CONFIGURE_FILE(
    "${CMAKE_CURRENT_SOURCE_DIR}/CMakeUninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/CMakeUninstall.cmake"
    @ONLY)
ADD_CUSTOM_TARGET(uninstall
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/CMakeUninstall.cmake")

SET(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
INCLUDE(InstallRequiredSystemLibraries)

IF (NOT ZORBA_SKIP_CPACK)
  # QQQ move to config/ ?
  INCLUDE(CMakeCPack.cmake)
ENDIF (NOT ZORBA_SKIP_CPACK)

IF (APPLE AND UNIVERSAL)
  SET (CMAKE_OSX_ARCHITECTURES "ppc;i386" CACHE STRING "archs" FORCE)
  SET (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -v" )
  # The following is not the default when building universal
  INCLUDE_DIRECTORIES	(SYSTEM /usr/local/include)
  SET (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -v")
  MESSAGE ("Building Apple universal binaries")
ENDIF (APPLE AND UNIVERSAL)

IF (WIN32)
  ZORBA_GENERATE_EXE_HELPERS_WIN32 ()
ENDIF (WIN32)

# This must be done last! Create the one top-level target which
# depends on all generated (copied) module/schema files.
DONE_DECLARING_ZORBA_URIS()

# This dependency makes sure that the error and warning definition
# modules are generated before trying to install them as zorba modules.
# This dependency is zorba-only related, which is why it's added here
# and not within zorba-modules, which is for general use in other
# non-zorba modules.
ADD_DEPENDENCIES(check_core_uris gen_diag_modules)