1# CMake build for xerces-c
2#
3# Written by Roger Leigh <rleigh@codelibre.net>
4#
5# Licensed to the Apache Software Foundation (ASF) under one or more
6# contributor license agreements.  See the NOTICE file distributed with
7# this work for additional information regarding copyright ownership.
8# The ASF licenses this file to You under the Apache License, Version 2.0
9# (the "License"); you may not use this file except in compliance with
10# the License.  You may obtain a copy of the License at
11#
12#      http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19
20# mutexmgr selection
21
22set(xerces_thread_default ON)
23find_package(Threads)
24if(NOT TARGET Threads::Threads)
25  set(xerces_thread_default ON)
26endif()
27
28option(threads "Threading support" ${xerces_thread_default})
29
30include(CheckCXXSourceCompiles)
31
32function(thread_test outvar)
33  set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
34
35  check_cxx_source_compiles(
36"#include <thread>
37#include <mutex>
38#include <iostream>
39
40namespace
41{
42
43  std::mutex m1;
44  std::recursive_mutex m2;
45
46  void
47  threadmain()
48  {
49    std::lock_guard<std::mutex> lock1(m1);
50    std::lock_guard<std::recursive_mutex> lock2(m2);
51    std::cout << \"In thread\" << std::endl;
52  }
53
54}
55
56int main() {
57  std::thread foo(threadmain);
58  foo.join();
59
60  return 0;
61}"
62${outvar})
63
64  set(${outvar} ${${outvar}} PARENT_SCOPE)
65endfunction(thread_test)
66
67if(threads)
68  set(THREADS_PREFER_PTHREAD_FLAG ON)
69  add_definitions(-D_THREAD_SAFE=1)
70  find_package(Threads)
71
72  thread_test(XERCES_HAVE_STD_THREAD)
73  if(XERCES_HAVE_STD_THREAD)
74    list(APPEND mutexmgrs standard)
75  endif()
76
77  if(TARGET Threads::Threads)
78    if(WIN32)
79      list(APPEND mutexmgrs windows)
80    else()
81      list(APPEND mutexmgrs posix)
82      set(HAVE_PTHREAD 1)
83    endif()
84  endif()
85else()
86  set(mutexmgr nothreads)
87endif(threads)
88list(APPEND mutexmgrs nothreads)
89
90string(REPLACE ";" "|" mutexmgr_help "${mutexmgrs}")
91list(GET mutexmgrs 0 xerces_mutexmgr_default)
92set(mutex-manager "${xerces_mutexmgr_default}" CACHE STRING "Mutex manager (${mutexmgr_help})")
93set(mutexmgr "${mutex-manager}")
94
95list(FIND mutexmgrs "${mutexmgr}" mutexmgr_found)
96if(mutexmgr_found EQUAL -1)
97  message(FATAL_ERROR "${mutexmgr} mutexmgr unavailable")
98endif()
99
100set(XERCES_USE_MUTEXMGR_STD 0)
101set(XERCES_USE_MUTEXMGR_POSIX 0)
102set(XERCES_USE_MUTEXMGR_WINDOWS 0)
103set(XERCES_USE_MUTEXMGR_NOTHREAD 0)
104if(mutexmgr STREQUAL "standard")
105  set(XERCES_USE_MUTEXMGR_STD 1)
106elseif(mutexmgr STREQUAL "posix")
107  set(XERCES_USE_MUTEXMGR_POSIX 1)
108elseif(mutexmgr STREQUAL "windows")
109  set(XERCES_USE_MUTEXMGR_WINDOWS 1)
110elseif(mutexmgr STREQUAL "nothreads")
111  set(XERCES_USE_MUTEXMGR_NOTHREAD 1)
112else()
113  message(FATAL_ERROR "Invalid mutex manager: \"${mutexmgr}\"")
114endif()
115