]> git.mxchange.org Git - simgear.git/blob - CMakeLists.txt
6aa1768c0ca63ea41a8cf6853ab4b3bb1a53f6c3
[simgear.git] / CMakeLists.txt
1 cmake_minimum_required (VERSION 2.6.4)
2
3 if(COMMAND cmake_policy)
4    if(POLICY CMP0054)
5        cmake_policy(SET CMP0054 NEW)
6    endif()
7    if(POLICY CMP0042)
8        cmake_policy(SET CMP0042 NEW)
9    endif()
10 endif()
11
12 include (CheckFunctionExists)
13 include (CheckIncludeFile)
14 include (CheckLibraryExists)
15 include (CheckCXXSourceCompiles)
16 include (CheckCXXCompilerFlag)
17
18 project(SimGear)
19
20 # read 'version' file into a variable (stripping any newlines or spaces)
21 file(READ version versionFile)
22 string(STRIP ${versionFile} SIMGEAR_VERSION)
23
24 set(FIND_LIBRARY_USE_LIB64_PATHS ON)
25
26 # use simgear version also as the SO version (if building SOs)
27 SET(SIMGEAR_SOVERSION ${SIMGEAR_VERSION})
28
29 # Warning when build is not an out-of-source build.
30 string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" InSourceBuild)
31 if(InSourceBuild)
32     message(WARNING  "Avoid building inside the source tree!")
33     message(WARNING  "Create a separate build directory instead (i.e. 'sgbuild') and call CMake from there: ")
34     message(WARNING  "  mkdir ../sgbuild && cd ../sgbuild && cmake ${CMAKE_SOURCE_DIR}")
35 endif(InSourceBuild)
36
37 #packaging
38 SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/COPYING")
39 SET(CPACK_RESOURCE_FILE_README "${PROJECT_SOURCE_DIR}/README")
40 SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Simulation support libraries for FlightGear and related projects")
41 SET(CPACK_PACKAGE_VENDOR "The FlightGear project")
42 SET(CPACK_GENERATOR "TBZ2")
43 SET(CPACK_INSTALL_CMAKE_PROJECTS  ${CMAKE_CURRENT_BINARY_DIR};SimGear;ALL;/)
44
45
46 # split version string into components, note CMAKE_MATCH_0 is the entire regexp match
47 string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" CPACK_PACKAGE_VERSION ${SIMGEAR_VERSION} )
48 set(CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
49 set(CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
50 set(CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
51
52 message(STATUS "version is ${CPACK_PACKAGE_VERSION_MAJOR} dot ${CPACK_PACKAGE_VERSION_MINOR} dot ${CPACK_PACKAGE_VERSION_PATCH}")
53
54 set(CPACK_SOURCE_GENERATOR TBZ2)
55 set(CPACK_SOURCE_PACKAGE_FILE_NAME "simgear-${SIMGEAR_VERSION}" CACHE INTERNAL "tarball basename")
56 set(CPACK_SOURCE_IGNORE_FILES
57   "^${PROJECT_SOURCE_DIR}/.git;\\\\.gitignore;Makefile.am;~$;${CPACK_SOURCE_IGNORE_FILES}")
58
59 message(STATUS "ignoring: ${CPACK_SOURCE_IGNORE_FILES}")
60
61 include (CPack)
62
63 # We have some custom .cmake scripts not in the official distribution.
64 set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_PATH}")
65
66 # Change the default build type to something fast
67 if(NOT CMAKE_BUILD_TYPE)
68   set(CMAKE_BUILD_TYPE Release CACHE STRING
69       "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
70       FORCE)
71 endif(NOT CMAKE_BUILD_TYPE)
72
73 # Determine name of library installation directory, i.e. "lib" vs "lib64", which
74 # differs between all Debian-based vs all other Linux distros.
75 # See cmake bug #11964, http://cmake.org/gitweb?p=cmake.git;a=commit;h=126c993d
76 # GNUInstallDirs requires CMake >= 2.8.5, use own file for older cmake
77 if(${CMAKE_VERSION} VERSION_GREATER 2.8.4)
78   include(GNUInstallDirs)
79 else(${CMAKE_VERSION} VERSION_GREATER 2.8.4)
80   include(OldGNUInstallDirs)
81 endif(${CMAKE_VERSION} VERSION_GREATER 2.8.4)
82 message(STATUS "Library installation directory: ${CMAKE_INSTALL_LIBDIR}")
83
84 #####################################################################################
85 # Configure library search paths
86 #####################################################################################
87
88 if(NOT "${CMAKE_LIBRARY_ARCHITECTURE}" STREQUAL "")
89     # Workaround for Ubuntu/Debian which introduced the "multiarch" library
90     # directory structure, which is unsupported by CMake < 2.8.10, so we need to
91     # add paths manually
92     # see http://www.cmake.org/Bug/view.php?id=12049 and
93     # http://www.cmake.org/Bug/view.php?id=12037
94     list(APPEND ADDITIONAL_LIBRARY_PATHS
95          /usr/local/lib/${CMAKE_LIBRARY_ARCHITECTURE}
96          /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}
97          /lib/${CMAKE_LIBRARY_ARCHITECTURE})
98     message(STATUS "additional library directories: ${ADDITIONAL_LIBRARY_PATHS}")
99 endif()
100
101 #####################################################################################
102
103 if (NOT MSVC)
104 option(SIMGEAR_SHARED   "Set to ON to build SimGear as a shared library/framework" OFF)
105 option(SYSTEM_EXPAT     "Set to ON to build SimGear using the system libExpat" OFF)
106 else()
107 # Building SimGear DLLs is currently not supported for MSVC.
108 set(SIMGEAR_SHARED OFF)
109 # Using a system expat is currently not supported for MSVC - it would require shared simgear (DLL).
110 set(SYSTEM_EXPAT OFF)
111 endif()
112
113 option(SIMGEAR_HEADLESS "Set to ON to build SimGear without GUI/graphics support" OFF)
114 option(ENABLE_RTI       "Set to ON to build SimGear with RTI support" OFF)
115 option(ENABLE_TESTS     "Set to OFF to disable building SimGear's test applications" ON)
116 option(ENABLE_SOUND     "Set to OFF to disable building SimGear's sound support" ON)
117 option(ENABLE_PKGUTIL   "Set to ON to build the sg_pkgutil application (default)" ON)
118
119 if (MSVC)
120   GET_FILENAME_COMPONENT(PARENT_DIR ${PROJECT_BINARY_DIR} PATH)
121   if (CMAKE_CL_64)
122     SET(TEST_3RDPARTY_DIR "${PARENT_DIR}/3rdparty.x64")
123   else (CMAKE_CL_64)
124     SET(TEST_3RDPARTY_DIR "${PARENT_DIR}/3rdparty")
125   endif (CMAKE_CL_64)
126   if (EXISTS ${TEST_3RDPARTY_DIR})
127     set(MSVC_3RDPARTY_ROOT ${PARENT_DIR} CACHE PATH "Location where the third-party dependencies are extracted")
128   else (EXISTS ${TEST_3RDPARTY_DIR})
129     set(MSVC_3RDPARTY_ROOT NOT_FOUND CACHE PATH "Location where the third-party dependencies are extracted")
130   endif (EXISTS ${TEST_3RDPARTY_DIR})
131 else (MSVC)
132   set(MSVC_3RDPARTY_ROOT NOT_FOUND CACHE PATH "Location where the third-party dependencies are extracted")
133 endif (MSVC)
134
135 if (MSVC AND MSVC_3RDPARTY_ROOT)
136   message(STATUS "3rdparty files located in ${MSVC_3RDPARTY_ROOT}")
137   set( OSG_MSVC "msvc" )
138   if (${MSVC_VERSION} EQUAL 1700)
139       set( OSG_MSVC ${OSG_MSVC}110 )
140   elseif (${MSVC_VERSION} EQUAL 1600)
141       set( OSG_MSVC ${OSG_MSVC}100 )
142   else (${MSVC_VERSION} EQUAL 1700)
143       set( OSG_MSVC ${OSG_MSVC}90 )
144   endif (${MSVC_VERSION} EQUAL 1700)
145   if (CMAKE_CL_64)
146       set( OSG_MSVC ${OSG_MSVC}-64 )
147       set( MSVC_3RDPARTY_DIR 3rdParty.x64 )
148   else (CMAKE_CL_64)
149       set( MSVC_3RDPARTY_DIR 3rdParty )
150   endif (CMAKE_CL_64)
151
152   set (CMAKE_LIBRARY_PATH ${MSVC_3RDPARTY_ROOT}/${MSVC_3RDPARTY_DIR}/lib ${MSVC_3RDPARTY_ROOT}/install/${OSG_MSVC}/OpenScenegraph/lib ${MSVC_3RDPARTY_ROOT}/install/${OSG_MSVC}/OpenRTI/lib )
153   set (CMAKE_INCLUDE_PATH ${MSVC_3RDPARTY_ROOT}/${MSVC_3RDPARTY_DIR}/include ${MSVC_3RDPARTY_ROOT}/install/${OSG_MSVC}/OpenScenegraph/include ${MSVC_3RDPARTY_ROOT}/install/${OSG_MSVC}/OpenRTI/include)
154   find_path(BOOST_ROOT boost/version.hpp
155                         ${MSVC_3RDPARTY_ROOT}/boost
156                         ${MSVC_3RDPARTY_ROOT}/boost_1_52_0
157                         ${MSVC_3RDPARTY_ROOT}/boost_1_51_0
158                         ${MSVC_3RDPARTY_ROOT}/boost_1_50_0
159                         ${MSVC_3RDPARTY_ROOT}/boost_1_49_0
160                         ${MSVC_3RDPARTY_ROOT}/boost_1_48_0
161                         ${MSVC_3RDPARTY_ROOT}/boost_1_47_0
162                         ${MSVC_3RDPARTY_ROOT}/boost_1_46_1
163                         ${MSVC_3RDPARTY_ROOT}/boost_1_46_0
164                         ${MSVC_3RDPARTY_ROOT}/boost_1_45_0
165                         ${MSVC_3RDPARTY_ROOT}/boost_1_44_0
166                         )
167   # set (BOOST_ROOT ${MSVC_3RDPARTY_ROOT}/boost_1_44_0)
168   message(STATUS "BOOST_ROOT is ${BOOST_ROOT}")
169   set (OPENAL_INCLUDE_DIR ${MSVC_3RDPARTY_ROOT}/${MSVC_3RDPARTY_DIR}/include)
170   set (OPENAL_LIBRARY_DIR ${MSVC_3RDPARTY_ROOT}/${MSVC_3RDPARTY_DIR}/lib)
171 endif (MSVC AND MSVC_3RDPARTY_ROOT)
172
173 if(APPLE)
174   find_library(COCOA_LIBRARY Cocoa)
175   
176   # using 10.7 because boost requires libc++ and 10.6 doesn't include it
177   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.7")
178 endif()
179
180 if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR
181         ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
182     find_package(Threads REQUIRED)
183 endif()
184     
185 # Somehow this only works if included before searching for Boost...
186 include(BoostTestTargets)
187
188 find_package(Boost REQUIRED)
189 set (BOOST_CXX_FLAGS "-DBOOST_BIMAP_DISABLE_SERIALIZATION")
190
191 if(SIMGEAR_HEADLESS)
192     message(STATUS "SimGear mode: HEADLESS")
193     set(ENABLE_SOUND 0)
194 else()
195     message(STATUS "SimGear mode: NORMAL")
196     find_package(OpenGL REQUIRED)
197
198     if (ENABLE_SOUND)
199         find_package(OpenAL REQUIRED)
200         message(STATUS "Sound support: ENABLED")
201     endif(ENABLE_SOUND)
202
203     find_package(OpenSceneGraph 3.2.0 REQUIRED osgText osgSim osgDB osgParticle osgGA osgViewer osgUtil)
204 endif(SIMGEAR_HEADLESS)
205
206 find_package(ZLIB REQUIRED)
207
208 if (SYSTEM_EXPAT)
209     message(STATUS "Requested to use system Expat library, forcing SIMGEAR_SHARED to true")
210     set(SIMGEAR_SHARED ON)
211     find_package(EXPAT REQUIRED)
212
213 else()
214     message(STATUS "Using built-in expat code")
215     # XML_STATIC is important to avoid sg_expat_external.h
216     # declaring symbols as declspec(import)
217     add_definitions(-DHAVE_EXPAT_CONFIG_H -DXML_STATIC)
218     set(EXPAT_INCLUDE_DIRS
219             ${PROJECT_SOURCE_DIR}/3rdparty/expat
220             ${PROJECT_BINARY_DIR}/3rdparty/expat)
221 endif(SYSTEM_EXPAT)
222
223 include_directories(${EXPAT_INCLUDE_DIRS})
224
225 check_include_file(inttypes.h HAVE_INTTYPES_H)
226 check_include_file(sys/time.h HAVE_SYS_TIME_H)
227 check_include_file(sys/timeb.h HAVE_SYS_TIMEB_H)
228 check_include_file(unistd.h HAVE_UNISTD_H)
229 check_include_file(windows.h HAVE_WINDOWS_H)
230
231 if(HAVE_INTTYPES_H)
232   # ShivaVG needs inttypes.h
233   add_definitions(-DHAVE_INTTYPES_H)
234 endif()
235
236 if(ENABLE_RTI)
237     # See if we have any rti library variant installed
238     message(STATUS "RTI: ENABLED")
239     find_package(RTI)
240 else()
241     message(STATUS "RTI: DISABLED")
242 endif(ENABLE_RTI)
243
244 check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
245 check_function_exists(ftime HAVE_FTIME)
246 check_function_exists(timegm HAVE_TIMEGM)
247 check_function_exists(rint HAVE_RINT)
248 check_function_exists(mkdtemp HAVE_MKDTEMP)
249 check_function_exists(bcopy HAVE_BCOPY)
250 check_function_exists(mmap HAVE_MMAP)
251
252 if(HAVE_UNISTD_H)
253     set(CMAKE_REQUIRED_INCLUDES ${CMAKE_INCLUDE_PATH})
254     check_cxx_source_compiles(
255        "#include <unistd.h>
256         #if !defined(_POSIX_TIMERS) || (0 >= _POSIX_TIMERS)
257             #error clock_gettime is not supported
258         #endif
259
260         int main() { return 0; }
261         "
262         HAVE_CLOCK_GETTIME)
263 endif(HAVE_UNISTD_H)
264
265 set(RT_LIBRARY "")
266 if(HAVE_CLOCK_GETTIME)
267     check_library_exists(rt clock_gettime "" HAVE_RT)
268     if(HAVE_RT)
269         set(RT_LIBRARY rt)
270     endif(HAVE_RT)
271 endif(HAVE_CLOCK_GETTIME)
272
273 set(DL_LIBRARY "")
274 check_cxx_source_compiles(
275     "#include <dlfcn.h>
276     int main(void) {
277         return 0;
278     }
279     "
280     HAVE_DLFCN_H)
281
282 if(HAVE_DLFCN_H)
283     check_library_exists(dl dlerror "" HAVE_DL)
284     if(HAVE_DL)
285         set(DL_LIBRARY "dl")
286     endif()
287 endif()
288
289 SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually 'd' on windows")
290 SET(CMAKE_RELEASE_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows")
291 SET(CMAKE_RELWITHDEBINFO_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows")
292 SET(CMAKE_MINSIZEREL_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows")
293
294 # isnan might not be real symbol, so can't check using function_exists
295 check_cxx_source_compiles(
296     "#include <cmath>
297     int main() { return isnan(0.0);} "
298     HAVE_ISNAN)
299
300 check_cxx_source_compiles(
301     "#include <cmath>
302     int main() { return std::isnan(0.0);} "
303     HAVE_STD_ISNAN)
304
305 if(CMAKE_COMPILER_IS_GNUCXX)
306     set(WARNING_FLAGS_CXX "-Wall")
307     set(WARNING_FLAGS_C   "-Wall")
308
309     # certain GCC versions don't provide the atomic builds, and hence
310     # require is to provide them in SGAtomic.cxx
311     set(CMAKE_REQUIRED_INCLUDES ${CMAKE_INCLUDE_PATH})
312     check_cxx_source_compiles(
313        "int main() { unsigned mValue; return __sync_add_and_fetch(&mValue, 1); }"
314         GCC_ATOMIC_BUILTINS_FOUND)
315 endif(CMAKE_COMPILER_IS_GNUCXX)
316
317 if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
318     # Boost redeclares class members
319     set(WARNING_FLAGS_CXX "-Wall -Wno-overloaded-virtual -Wno-redeclared-class-member")
320     set(WARNING_FLAGS_C   "-Wall")
321     set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
322     set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++11")
323     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
324 endif()
325
326 if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
327 # boost goes haywire wrt static asserts
328     check_cxx_compiler_flag(-Wno-unused-local-typedefs HAS_NOWARN_UNUSED_TYPEDEFS)
329     if(HAS_NOWARN_UNUSED_TYPEDEFS)
330         set(WARNING_FLAGS_CXX " ${WARNING_FLAGS_CXX} -Wno-unused-local-typedefs")
331     endif()
332 endif()
333
334 if(WIN32)
335
336     if(MINGW)
337         add_definitions(-D_WIN32_WINNT=0x501)
338     endif()
339
340     if(MSVC)
341         # turn off various warnings
342         # foreach(warning 4244 4251 4267 4275 4290 4786 4305 4996)
343         #     SET(WARNING_FLAGS "${WARNING_FLAGS} /wd${warning}")
344         # endforeach(warning)
345
346         set(MSVC_FLAGS "-DWIN32 -DNOMINMAX -D_USE_MATH_DEFINES -D_CRT_SECURE_NO_WARNINGS -D__CRT_NONSTDC_NO_WARNINGS /wd4996 /wd4250 -Dstrdup=_strdup")
347         if (${MSVC_VERSION} GREATER 1599)
348             set( MSVC_LD_FLAGS "/FORCE:MULTIPLE" )
349         endif (${MSVC_VERSION} GREATER 1599)
350     endif(MSVC)
351
352     # assumed on Windows
353     set(HAVE_GETLOCALTIME 1)
354
355     set( WINSOCK_LIBRARY "ws2_32.lib" )
356     set( RT_LIBRARY "winmm" )
357 endif(WIN32)
358
359 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS_C} ${MSVC_FLAGS}")
360 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS_CXX} ${MSVC_FLAGS} ${BOOST_CXX_FLAGS}")
361 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MSVC_LD_FLAGS}")
362
363 # use BEFORE to ensure local directories are used first,
364 # ahead of system-installed libs
365 include_directories(BEFORE ${PROJECT_SOURCE_DIR})
366 include_directories(BEFORE ${PROJECT_SOURCE_DIR}/simgear/canvas/ShivaVG/include)
367 include_directories(BEFORE ${PROJECT_BINARY_DIR}/simgear)
368
369 include_directories(${OPENSCENEGRAPH_INCLUDE_DIRS}
370     ${Boost_INCLUDE_DIRS}
371     ${ZLIB_INCLUDE_DIR}
372     ${OPENAL_INCLUDE_DIR}
373 )
374
375 add_definitions(-DHAVE_CONFIG_H)
376
377 # configure a header file to pass some of the CMake settings
378 # to the source code
379 configure_file (
380   "${PROJECT_SOURCE_DIR}/simgear/simgear_config_cmake.h.in"
381   "${PROJECT_BINARY_DIR}/simgear/simgear_config.h"
382   )
383
384 if(ENABLE_TESTS)
385     # enable CTest / make test target
386     message(STATUS "Tests: ENABLED")
387
388     include (Dart)
389     enable_testing()
390 else()
391     message(STATUS "Tests: DISABLED")
392 endif(ENABLE_TESTS)
393
394 # always set TEST_LIBS as it is also used by other tools/applications
395 set(TEST_LIBS_INTERNAL_CORE
396     ${CMAKE_THREAD_LIBS_INIT}
397     ${ZLIB_LIBRARY}
398     ${WINSOCK_LIBRARY}
399     ${RT_LIBRARY}
400     ${DL_LIBRARY}
401     ${COCOA_LIBRARY})
402 set(TEST_LIBS SimGearCore ${TEST_LIBS_INTERNAL_CORE})
403
404 if(NOT SIMGEAR_HEADLESS)
405     set(TEST_LIBS SimGearScene ${OPENGL_LIBRARIES} ${TEST_LIBS})
406 endif()
407
408 install (FILES ${PROJECT_BINARY_DIR}/simgear/simgear_config.h  DESTINATION include/simgear/)
409
410 include_directories(3rdparty/utf8/source)
411
412 add_subdirectory(3rdparty)
413 add_subdirectory(simgear)
414
415 #-----------------------------------------------------------------------------
416 ### uninstall target
417 #-----------------------------------------------------------------------------
418 CONFIGURE_FILE(
419   "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/cmake_uninstall.cmake.in"
420   "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
421   IMMEDIATE @ONLY)
422 ADD_CUSTOM_TARGET(uninstall
423   "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
424
425