]> git.mxchange.org Git - flightgear.git/blob - CMakeModules/FindSimGear.cmake
2764fa7eb651d71c538d4eb590065608b0a31d01
[flightgear.git] / CMakeModules / FindSimGear.cmake
1 # Locate SimGear
2 # This module defines
3
4 # SIMGEAR_CORE_LIBRARIES, a list of the core static libraries
5 # SIMGEAR_LIBRARIES, a list of all the static libraries (core + scene)
6 # SIMGEAR_FOUND, if false, do not try to link to SimGear
7 # SIMGEAR_INCLUDE_DIR, where to find the headers
8 #
9 # $SIMGEAR_DIR is an environment variable that would
10 # correspond to the ./configure --prefix=$SIMGEAR_DIR
11 # used in building SimGear.
12 #
13 # Created by James Turner. This was influenced by the FindOpenAL.cmake module.
14
15 #=============================================================================
16 # Copyright 2005-2009 Kitware, Inc.
17 #
18 # Distributed under the OSI-approved BSD License (the "License");
19 # see accompanying file Copyright.txt for details.
20 #
21 # This software is distributed WITHOUT ANY WARRANTY; without even the
22 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 # See the License for more information.
24 #=============================================================================
25 # (To distributed this file outside of CMake, substitute the full
26 #  License text for the above reference.)
27
28 include(SelectLibraryConfigurations)
29
30 macro(find_sg_library libName varName libs)
31     set(libVarName "${varName}_LIBRARY")
32     # do not cache the library check
33     unset(${libVarName}_DEBUG CACHE)
34     unset(${libVarName}_RELEASE CACHE)
35
36     FIND_LIBRARY(${libVarName}_DEBUG
37       NAMES ${libName}${CMAKE_DEBUG_POSTFIX}
38       HINTS $ENV{SIMGEAR_DIR}
39       PATH_SUFFIXES ${CMAKE_INSTALL_LIBDIR} libs64 libs libs/Win32 libs/Win64
40       PATHS
41       /usr/local
42       /usr
43       /opt
44     )
45     FIND_LIBRARY(${libVarName}_RELEASE
46       NAMES ${libName}${CMAKE_RELEASE_POSTFIX}
47       HINTS $ENV{SIMGEAR_DIR}
48       PATH_SUFFIXES ${CMAKE_INSTALL_LIBDIR} libs64 libs libs/Win32 libs/Win64
49       PATHS
50       /usr/local
51       /usr
52       /opt
53     )
54     
55    # message(STATUS "before: Simgear ${${libVarName}_RELEASE} ")
56   #  message(STATUS "before: Simgear ${${libVarName}_DEBUG} ")
57     
58     select_library_configurations( ${varName} )
59
60   #  message(STATUS "after:Simgear ${${libVarName}_RELEASE} ")
61   #  message(STATUS "after:Simgear ${${libVarName}_DEBUG} ")
62
63     set(componentLibRelease ${${libVarName}_RELEASE})
64   #  message(STATUS "Simgear ${libVarName}_RELEASE ${componentLibRelease}")
65     set(componentLibDebug ${${libVarName}_DEBUG})
66    # message(STATUS "Simgear ${libVarName}_DEBUG ${componentLibDebug}")
67     
68     if (NOT ${libVarName}_DEBUG)
69         if (NOT ${libVarName}_RELEASE)
70             #message(STATUS "found ${componentLib}")
71             list(APPEND ${libs} ${componentLibRelease})
72         endif()
73     else()
74         list(APPEND ${libs} optimized ${componentLibRelease} debug ${componentLibDebug})
75     endif()
76 endmacro()
77
78 macro(find_sg_component comp libs)
79     set(compLib "sg${comp}")
80     string(TOUPPER "SIMGEAR_${comp}" libVar)
81     
82     find_sg_library(${compLib} ${libVar} ${libs})
83 endmacro()
84
85 FIND_PATH(SIMGEAR_INCLUDE_DIR simgear/math/SGMath.hxx
86   HINTS $ENV{SIMGEAR_DIR}
87   PATH_SUFFIXES include
88   PATHS
89   ~/Library/Frameworks
90   /Library/Frameworks
91   /usr/local
92   /usr
93   /opt
94 )
95
96 # make sure the simgear include directory exists
97 if (NOT SIMGEAR_INCLUDE_DIR)
98     message(FATAL_ERROR "Cannot find SimGear includes! (Forgot 'make install' for SimGear?) "
99             "Compile & INSTALL SimGear before configuring FlightGear. "
100             "When using non-standard locations, use 'SIMGEAR_DIR' to configure the SimGear location.")
101 endif()
102
103 message(STATUS "SimGear include directory: ${SIMGEAR_INCLUDE_DIR}")
104
105 # make sure the simgear/version.h header exists
106 if (NOT EXISTS ${SIMGEAR_INCLUDE_DIR}/simgear/version.h)
107     message(FATAL_ERROR "Found SimGear, but it does not contain a simgear/version.h include! "
108             "SimGear installation is incomplete.")
109 endif()
110
111 # read the simgear version header file, get the version
112 file(READ ${SIMGEAR_INCLUDE_DIR}/simgear/version.h sgVersionFile)
113 string(STRIP ${sgVersionFile} SIMGEAR_DEFINE)
114 string(REPLACE "#define SIMGEAR_VERSION " "" SIMGEAR_VERSION ${SIMGEAR_DEFINE})
115 message(STATUS "found SimGear version: ${SIMGEAR_VERSION} (needed ${SimGear_FIND_VERSION})")
116
117 if(NOT ${SIMGEAR_VERSION} EQUAL ${SimGear_FIND_VERSION})
118     message(FATAL_ERROR "You have installed a mismatching SimGear version ${SIMGEAR_VERSION} "
119             "instead of ${SimGear_FIND_VERSION} as required by FlightGear. "
120             "When using multiple SimGear installations, please use 'SIMGEAR_DIR' "
121             "to select the SimGear library location to be used.")
122 endif()
123
124 # dependent packages
125 find_package(ZLIB REQUIRED)
126 find_package(Threads REQUIRED)
127
128 if(SIMGEAR_SHARED)
129     message(STATUS "looking for shared Simgear libraries")
130
131     find_sg_library(SimGearCore SIMGEAR_CORE SIMGEAR_CORE_LIBRARIES)
132     find_sg_library(SimGearScene SIMGEAR_SCENE SIMGEAR_LIBRARIES)
133
134  
135     list(APPEND SIMGEAR_LIBRARIES ${SIMGEAR_CORE_LIBRARIES})
136     set(SIMGEAR_CORE_LIBRARY_DEPENDENCIES "")
137     set(SIMGEAR_SCENE_LIBRARY_DEPENDENCIES "")
138     
139    # message(STATUS "core lib ${SIMGEAR_CORE_LIBRARIES}")
140   #  message(STATUS "all libs ${SIMGEAR_LIBRARIES}")
141 else(SIMGEAR_SHARED)
142
143     set(SIMGEAR_LIBRARIES "") # clear value
144     set(SIMGEAR_CORE_LIBRARIES "") # clear value
145     message(STATUS "looking for static SimGear libraries")
146     
147   # note the order here affects the order Simgear libraries are
148   # linked in, and hence ability to link when using a traditional
149   # linker such as GNU ld on Linux
150     set(comps
151         environment
152         nasal
153         tsync
154         bucket
155         route
156         io
157         serial
158         math
159         props
160         structure
161         timing
162         xml
163         misc
164         threads
165         debug
166         magvar
167     )
168
169     set(scene_comps
170         ephem
171         sky
172         material
173         tgdb
174         model
175         screen
176         bvh
177         util
178         sound)
179             
180     foreach(component ${comps})
181         find_sg_component(${component} SIMGEAR_CORE_LIBRARIES)
182     endforeach()
183
184     foreach(component ${scene_comps})
185         find_sg_component(${component} SIMGEAR_LIBRARIES)
186     endforeach()
187
188     # again link order matters - scene libraries depend on core ones
189     list(APPEND SIMGEAR_LIBRARIES ${SIMGEAR_CORE_LIBRARIES})
190
191     #message(STATUS "all libs ${SIMGEAR_LIBRARIES}")
192     
193     set(SIMGEAR_CORE_LIBRARY_DEPENDENCIES
194         ${CMAKE_THREAD_LIBS_INIT}
195         ${ZLIB_LIBRARY})
196
197     set(SIMGEAR_SCENE_LIBRARY_DEPENDENCIES 
198         ${ALUT_LIBRARY} 
199         ${OPENAL_LIBRARY}
200         ${LIBSVN_LIBRARIES})
201
202     if(WIN32)
203         list(APPEND SIMGEAR_CORE_LIBRARY_DEPENDENCIES ws2_32.lib)
204     endif(WIN32)
205
206     if(NOT MSVC)
207         # basic timing routines on non windows systems, may be also cygwin?!
208         check_library_exists(rt clock_gettime "" have_rt)
209         if(have_rt)
210             list(APPEND SIMGEAR_CORE_LIBRARY_DEPENDENCIES rt)
211         endif(have_rt)
212     endif(NOT MSVC)
213 endif(SIMGEAR_SHARED)
214
215 if((NOT SIMGEAR_CORE_LIBRARIES)OR(NOT SIMGEAR_LIBRARIES))
216     message(FATAL_ERROR "Cannot find SimGear libraries! (Forgot 'make install' for SimGear?) "
217             "Compile & INSTALL SimGear before configuring FlightGear. "
218             "When using non-standard locations, use 'SIMGEAR_DIR' to configure the SimGear location.")
219 else()
220     message(STATUS "found SimGear libraries")
221 endif()
222
223 # now we've found SimGear, try test-compiling using its includes
224 include(CheckCXXSourceRuns)
225
226 SET(CMAKE_REQUIRED_INCLUDES ${SIMGEAR_INCLUDE_DIR})
227
228 # clear cache, run a fresh compile test every time
229 unset(SIMGEAR_COMPILE_TEST CACHE)
230
231 # disable OSG dependencies for test-compiling
232 set(CMAKE_REQUIRED_DEFINITIONS "-DNO_OPENSCENEGRAPH_INTERFACE")
233 check_cxx_source_runs(
234     "#include <cstdio>
235     #include \"simgear/version.h\"
236     #include \"simgear/math/SGMath.hxx\"
237
238     #define xstr(s) str(s)
239     #define str(s) #s
240
241     #define MIN_MAJOR ${SimGear_FIND_VERSION_MAJOR}
242     #define MIN_MINOR ${SimGear_FIND_VERSION_MINOR}
243     #define MIN_MICRO ${SimGear_FIND_VERSION_PATCH}
244
245     int main() {
246         int major, minor, micro;
247
248         /* printf(%d.%d.%d or greater, , MIN_MAJOR, MIN_MINOR, MIN_MICRO); */
249         printf(\"found %s ... \", xstr(SIMGEAR_VERSION));
250
251         sscanf( xstr(SIMGEAR_VERSION), \"%d.%d.%d\", &major, &minor, &micro );
252
253         if ( (major != MIN_MAJOR) ||
254              (minor != MIN_MINOR) ||
255              (micro != MIN_MICRO) ) {
256          return -1;
257         }
258
259         return 0;
260     }
261     "
262     SIMGEAR_COMPILE_TEST)
263
264 if(NOT SIMGEAR_COMPILE_TEST)
265     message(FATAL_ERROR "Oops, you have installed SimGear includes, however test compiling failed. "
266             "Try removing 'CMakeCache.txt' and reconfigure with 'cmake'.")
267 endif()
268 unset(CMAKE_REQUIRED_DEFINITIONS)
269
270 include(FindPackageHandleStandardArgs)
271 FIND_PACKAGE_HANDLE_STANDARD_ARGS(SimGear DEFAULT_MSG
272      SIMGEAR_LIBRARIES SIMGEAR_CORE_LIBRARIES SIMGEAR_INCLUDE_DIR SIMGEAR_COMPILE_TEST)
273