320ddad2e08e04407439d2c575d7a7fce4fd1dcf
[platform/upstream/cmake.git] / Modules / FindGLUT.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindGLUT
6 --------
7
8 Find OpenGL Utility Toolkit (GLUT) library and include files.
9
10 IMPORTED Targets
11 ^^^^^^^^^^^^^^^^
12
13 .. versionadded:: 3.1
14
15 This module defines the :prop_tgt:`IMPORTED` targets:
16
17 ``GLUT::GLUT``
18  Defined if the system has GLUT.
19
20 Result Variables
21 ^^^^^^^^^^^^^^^^
22
23 This module defines the following variables:
24
25 ``GLUT_FOUND``
26   True if ``glut`` was found.
27
28 ``GLUT_INCLUDE_DIRS``
29   .. versionadded:: 3.23
30
31   Where to find GL/glut.h, etc.
32
33 ``GLUT_LIBRARIES``
34   List of libraries for using ``glut``.
35
36 Cache Variables
37 ^^^^^^^^^^^^^^^
38
39 This module may set the following variables depending on platform.
40 These variables may optionally be set to help this module find the
41 correct files, but clients should not use these as results:
42
43 ``GLUT_INCLUDE_DIR``
44   The full path to the directory containing ``GL/glut.h``,
45   not including ``GL/``.
46
47 ``GLUT_glut_LIBRARY``
48   The full path to the glut library.
49
50 ``GLUT_Xmu_LIBRARY``
51   The full path to the Xmu library.
52
53 ``GLUT_Xi_LIBRARY``
54   The full path to the Xi Library.
55
56 Obsolete Variables
57 ^^^^^^^^^^^^^^^^^^
58
59 The following variables may also be provided, for backwards compatibility:
60
61 ``GLUT_INCLUDE_DIR``
62   This is one of above `Cache Variables`_, but prior to CMake 3.23 was
63   also a result variable.  Prefer to use ``GLUT_INCLUDE_DIRS`` instead
64   in CMake 3.23 and above.
65 #]=======================================================================]
66
67 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
68 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
69
70 function(_add_glut_target_simple)
71   if(TARGET GLUT::GLUT)
72     return()
73   endif()
74   add_library(GLUT::GLUT INTERFACE IMPORTED)
75   if(GLUT_INCLUDE_DIRS)
76     target_include_directories(GLUT::GLUT SYSTEM
77       INTERFACE "${GLUT_INCLUDE_DIRS}")
78   endif()
79   if(GLUT_LIBRARIES)
80     target_link_libraries(GLUT::GLUT INTERFACE ${GLUT_LIBRARIES})
81   endif()
82   if(GLUT_LIBRARY_DIRS)
83     target_link_directories(GLUT::GLUT INTERFACE ${GLUT_LIBRARY_DIRS})
84   endif()
85   if(GLUT_LDFLAGS)
86     target_link_options(GLUT::GLUT INTERFACE ${GLUT_LDFLAGS})
87   endif()
88   if(GLUT_CFLAGS)
89     separate_arguments(GLUT_CFLAGS_SPLIT UNIX_COMMAND "${GLUT_CFLAGS}")
90     target_compile_options(GLUT::GLUT INTERFACE ${GLUT_CFLAGS_SPLIT})
91   endif()
92
93   set_property(TARGET GLUT::GLUT APPEND PROPERTY
94     IMPORTED_LOCATION "${GLUT_glut_LIBRARY}")
95 endfunction()
96
97 find_package(PkgConfig QUIET)
98 if(PKG_CONFIG_FOUND)
99   # Tell pkg-config not to strip any -I flags to make sure GLUT_INCLUDE_DIRS
100   # will be defined.
101   if(DEFINED ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS})
102     set(_pkgconfig_allow_system_cflags_old "$ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}")
103   else()
104     unset(_pkgconfig_allow_system_cflags_old)
105   endif()
106   set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} 1)
107   pkg_check_modules(GLUT QUIET glut)
108   if(DEFINED _pkgconfig_allow_system_cflags_old)
109     set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} "${_pkgconfig_allow_system_cflags_old}")
110     unset(_pkgconfig_allow_system_cflags_old)
111   else()
112     unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS})
113   endif()
114   if(NOT GLUT_FOUND)
115     pkg_check_modules(GLUT QUIET freeglut)
116   endif()
117   if(GLUT_FOUND)
118     # GLUT_INCLUDE_DIRS is now the official result variable, but
119     # older versions of CMake only provided GLUT_INCLUDE_DIR.
120     set(GLUT_INCLUDE_DIR "${GLUT_INCLUDE_DIRS}")
121     _add_glut_target_simple()
122     FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLUT REQUIRED_VARS GLUT_FOUND)
123     return()
124   endif()
125 endif()
126
127 if(WIN32)
128   find_path( GLUT_INCLUDE_DIR NAMES GL/glut.h
129     PATHS  ${GLUT_ROOT_PATH}/include )
130   mark_as_advanced(GLUT_INCLUDE_DIR)
131   find_library( GLUT_glut_LIBRARY_RELEASE NAMES freeglut glut glut32
132     PATHS
133     ${OPENGL_LIBRARY_DIR}
134     ${GLUT_ROOT_PATH}/Release
135     )
136   find_library( GLUT_glut_LIBRARY_DEBUG NAMES freeglutd
137     PATHS
138     ${OPENGL_LIBRARY_DIR}
139     ${GLUT_ROOT_PATH}/Debug
140     )
141   mark_as_advanced(GLUT_glut_LIBRARY_RELEASE GLUT_glut_LIBRARY_DEBUG)
142   select_library_configurations(GLUT_glut)
143 elseif(APPLE)
144   find_path(GLUT_INCLUDE_DIR glut.h ${OPENGL_LIBRARY_DIR})
145   mark_as_advanced(GLUT_INCLUDE_DIR)
146   find_library(GLUT_glut_LIBRARY GLUT DOC "GLUT library for OSX")
147   find_library(GLUT_cocoa_LIBRARY Cocoa DOC "Cocoa framework for OSX")
148   mark_as_advanced(GLUT_glut_LIBRARY GLUT_cocoa_LIBRARY)
149
150   if(GLUT_cocoa_LIBRARY AND NOT TARGET GLUT::Cocoa)
151     add_library(GLUT::Cocoa UNKNOWN IMPORTED)
152     # Cocoa should always be a Framework, but we check to make sure.
153     if(GLUT_cocoa_LIBRARY MATCHES "/([^/]+)\\.framework$")
154       set(_glut_cocoa "${GLUT_cocoa_LIBRARY}/${CMAKE_MATCH_1}")
155       if(EXISTS "${_glut_cocoa}.tbd")
156         string(APPEND _glut_cocoa ".tbd")
157       endif()
158       set_target_properties(GLUT::Cocoa PROPERTIES
159         IMPORTED_LOCATION "${_glut_cocoa}")
160     else()
161       set_target_properties(GLUT::Cocoa PROPERTIES
162         IMPORTED_LOCATION "${GLUT_cocoa_LIBRARY}")
163     endif()
164   endif()
165 else()
166   if(BEOS)
167     set(_GLUT_INC_DIR /boot/develop/headers/os/opengl)
168     set(_GLUT_glut_LIB_DIR /boot/develop/lib/x86)
169   else()
170     find_library( GLUT_Xi_LIBRARY Xi
171       /usr/openwin/lib
172       )
173     mark_as_advanced(GLUT_Xi_LIBRARY)
174
175     find_library( GLUT_Xmu_LIBRARY Xmu
176       /usr/openwin/lib
177       )
178     mark_as_advanced(GLUT_Xmu_LIBRARY)
179
180     if(GLUT_Xi_LIBRARY AND NOT TARGET GLUT::Xi)
181       add_library(GLUT::Xi UNKNOWN IMPORTED)
182       set_target_properties(GLUT::Xi PROPERTIES
183         IMPORTED_LOCATION "${GLUT_Xi_LIBRARY}")
184     endif()
185
186     if(GLUT_Xmu_LIBRARY AND NOT TARGET GLUT::Xmu)
187       add_library(GLUT::Xmu UNKNOWN IMPORTED)
188       set_target_properties(GLUT::Xmu PROPERTIES
189         IMPORTED_LOCATION "${GLUT_Xmu_LIBRARY}")
190     endif()
191
192   endif ()
193
194   find_path( GLUT_INCLUDE_DIR GL/glut.h
195     /usr/include/GL
196     /usr/openwin/share/include
197     /usr/openwin/include
198     /opt/graphics/OpenGL/include
199     /opt/graphics/OpenGL/contrib/libglut
200     ${_GLUT_INC_DIR}
201     )
202   mark_as_advanced(GLUT_INCLUDE_DIR)
203
204   find_library( GLUT_glut_LIBRARY glut
205     /usr/openwin/lib
206     ${_GLUT_glut_LIB_DIR}
207     )
208   mark_as_advanced(GLUT_glut_LIBRARY)
209
210   unset(_GLUT_INC_DIR)
211   unset(_GLUT_glut_LIB_DIR)
212 endif()
213
214 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLUT REQUIRED_VARS GLUT_glut_LIBRARY GLUT_INCLUDE_DIR)
215
216 if (GLUT_FOUND)
217   # Is -lXi and -lXmu required on all platforms that have it?
218   # If not, we need some way to figure out what platform we are on.
219   set( GLUT_LIBRARIES
220     ${GLUT_glut_LIBRARY}
221     )
222   set(GLUT_INCLUDE_DIRS
223     ${GLUT_INCLUDE_DIR}
224     )
225   foreach(v GLUT_Xmu_LIBRARY GLUT_Xi_LIBRARY GLUT_cocoa_LIBRARY)
226     if(${v})
227       list(APPEND GLUT_LIBRARIES ${${v}})
228     endif()
229   endforeach()
230
231   if(NOT TARGET GLUT::GLUT)
232     add_library(GLUT::GLUT UNKNOWN IMPORTED)
233     set_target_properties(GLUT::GLUT PROPERTIES
234       INTERFACE_INCLUDE_DIRECTORIES "${GLUT_INCLUDE_DIRS}")
235     if(GLUT_glut_LIBRARY MATCHES "/([^/]+)\\.framework$")
236       set(_glut_glut "${GLUT_glut_LIBRARY}/${CMAKE_MATCH_1}")
237       if(EXISTS "${_glut_glut}.tbd")
238         string(APPEND _glut_glut ".tbd")
239       endif()
240       set_target_properties(GLUT::GLUT PROPERTIES
241         IMPORTED_LOCATION "${_glut_glut}")
242     else()
243       if(GLUT_glut_LIBRARY_RELEASE)
244         set_property(TARGET GLUT::GLUT APPEND PROPERTY
245           IMPORTED_CONFIGURATIONS RELEASE)
246         set_target_properties(GLUT::GLUT PROPERTIES
247           IMPORTED_LOCATION_RELEASE "${GLUT_glut_LIBRARY_RELEASE}")
248       endif()
249
250       if(GLUT_glut_LIBRARY_DEBUG)
251         set_property(TARGET GLUT::GLUT APPEND PROPERTY
252           IMPORTED_CONFIGURATIONS DEBUG)
253         set_target_properties(GLUT::GLUT PROPERTIES
254           IMPORTED_LOCATION_DEBUG "${GLUT_glut_LIBRARY_DEBUG}")
255       endif()
256
257       if(NOT GLUT_glut_LIBRARY_RELEASE AND NOT GLUT_glut_LIBRARY_DEBUG)
258         set_property(TARGET GLUT::GLUT APPEND PROPERTY
259           IMPORTED_LOCATION "${GLUT_glut_LIBRARY}")
260       endif()
261     endif()
262
263     if(TARGET GLUT::Xmu)
264       set_property(TARGET GLUT::GLUT APPEND
265         PROPERTY INTERFACE_LINK_LIBRARIES GLUT::Xmu)
266     endif()
267
268     if(TARGET GLUT::Xi)
269       set_property(TARGET GLUT::GLUT APPEND
270         PROPERTY INTERFACE_LINK_LIBRARIES GLUT::Xi)
271     endif()
272
273     if(TARGET GLUT::Cocoa)
274       set_property(TARGET GLUT::GLUT APPEND
275         PROPERTY INTERFACE_LINK_LIBRARIES GLUT::Cocoa)
276     endif()
277   endif()
278 endif()