fix to smooth demo to work with GL 3.3 and higher, thanks Neil Mannall
[freeglut] / CMakeLists.txt
1 PROJECT(freeglut)
2 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
3
4 # NOTE: On Windows and Cygwin, the dll's are placed in the
5 # CMAKE_RUNTIME_OUTPUT_DIRECTORY, while their corresponding import
6 # libraries end up in CMAKE_ARCHIVE_OUTPUT_DIRECTORY. On other
7 # platforms, such as Linux, the shared libraries are put in
8 # CMAKE_ARCHIVE_OUTPUT_DIRECTORY instead.
9 # Static libraries end up in CMAKE_ARCHIVE_OUTPUT_DIRECTORY on all
10 # platforms.
11 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
12 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
13
14 # setup version numbers
15 # TODO: Update these for each release!
16 set(VERSION_MAJOR 3)
17 set(VERSION_MINOR 0)
18 set(VERSION_PATCH 0)
19
20
21 # BUILD_SHARED_LIBS is already a standard CMake variable, but we need to
22 # re-declare it here so it will show up in the GUI.
23 # by default, we want to build both
24 OPTION(BUILD_SHARED_LIBS "Build FreeGLUT shared library." ON)
25 OPTION(BUILD_STATIC_LIBS "Build FreeGLUT static library." ON)
26
27 # OpenGL ES support
28 OPTION(FREEGLUT_GLES1 "Use OpenGL ES 1.x (requires EGL)" OFF)
29 OPTION(FREEGLUT_GLES2 "Use OpenGL ES 2.x (requires EGL) (overrides BUILD_GLES1)" OFF)
30
31
32 SET(FREEGLUT_HEADERS
33     include/GL/freeglut.h
34     include/GL/freeglut_ext.h
35     include/GL/freeglut_std.h
36     include/GL/glut.h
37 )
38 SET(FREEGLUT_SRCS
39     ${FREEGLUT_HEADERS}
40     src/fg_callbacks.c
41     src/fg_cursor.c
42     src/fg_display.c
43     src/fg_ext.c
44     src/fg_font_data.c
45     src/fg_gamemode.c
46     src/fg_gl2.c
47     src/fg_gl2.h
48     src/fg_init.c
49     src/fg_init.h
50     src/fg_internal.h
51     src/fg_input_devices.c
52     src/fg_joystick.c
53     src/fg_main.c
54     src/fg_main.h
55     src/fg_misc.c
56     src/fg_overlay.c
57     src/fg_spaceball.c
58     src/fg_state.c
59     src/fg_stroke_mono_roman.c
60     src/fg_stroke_roman.c
61     src/fg_structure.c
62     src/fg_videoresize.c
63     src/fg_window.c
64 )
65 # TODO: OpenGL ES requires a compatible version of these files:
66 IF(NOT FREEGLUT_GLES2 AND NOT FREEGLUT_GLES1)
67     LIST(APPEND FREEGLUT_SRCS
68         src/fg_font.c
69         src/fg_menu.c
70         src/fg_teapot.c
71         src/fg_teapot_data.h
72         src/fg_geometry.c
73     )
74 ELSE()
75     LIST(APPEND FREEGLUT_SRCS
76         src/fg_geometry.c
77         src/gles_stubs.c
78     )
79 ENDIF()
80
81 IF(WIN32)
82     LIST(APPEND FREEGLUT_SRCS
83         src/mswin/fg_cursor_mswin.c
84         src/mswin/fg_display_mswin.c
85         src/mswin/fg_ext_mswin.c
86         src/mswin/fg_gamemode_mswin.c
87         src/mswin/fg_init_mswin.c
88         src/mswin/fg_internal_mswin.h
89         src/mswin/fg_input_devices_mswin.c
90         src/mswin/fg_joystick_mswin.c
91         src/mswin/fg_main_mswin.c
92         src/mswin/fg_menu_mswin.c
93         src/mswin/fg_spaceball_mswin.c
94         src/mswin/fg_state_mswin.c
95         src/mswin/fg_structure_mswin.c
96         src/mswin/fg_window_mswin.c
97         ${CMAKE_BINARY_DIR}/freeglut.rc # generated below from freeglut.rc.in
98     )
99     IF (MSVC AND NOT CMAKE_CL_64)
100         # .def file only for 32bit Windows builds (TODO: MSVC only right
101         # now, needed for any other Windows platform?)
102         LIST(APPEND FREEGLUT_SRCS
103             ${CMAKE_BINARY_DIR}/freeglutdll.def # generated below from src/freeglutdll.def.in
104         )
105     ENDIF()
106
107 ELSEIF(ANDROID)
108     LIST(APPEND FREEGLUT_SRCS
109         src/android/native_app_glue/android_native_app_glue.c
110         src/android/native_app_glue/android_native_app_glue.h
111         src/android/fg_internal_android.h
112         src/android/fg_cursor_android.c
113         src/android/fg_ext_android.c
114         src/android/fg_gamemode_android.c
115         src/android/fg_init_android.c
116         src/android/fg_input_devices_android.c
117         src/android/fg_joystick_android.c
118         src/android/fg_main_android.c
119         src/android/fg_main_android.h
120         src/android/fg_runtime_android.c
121         src/android/fg_spaceball_android.c
122         src/android/fg_state_android.c
123         src/android/fg_structure_android.c
124         src/android/fg_window_android.c
125     )
126 ELSE()
127     LIST(APPEND FREEGLUT_SRCS
128         src/x11/fg_cursor_x11.c
129         src/x11/fg_ext_x11.c
130         src/x11/fg_gamemode_x11.c
131         src/x11/fg_glutfont_definitions_x11.c
132         src/x11/fg_init_x11.c
133         src/x11/fg_internal_x11.h
134         src/x11/fg_input_devices_x11.c
135         src/x11/fg_joystick_x11.c
136         src/x11/fg_main_x11.c
137         src/x11/fg_menu_x11.c
138         src/x11/fg_spaceball_x11.c
139         src/x11/fg_state_x11.c
140         src/x11/fg_structure_x11.c
141         src/x11/fg_window_x11.c
142         src/x11/fg_xinput_x11.c
143     )
144     IF(NOT(FREEGLUT_GLES2 OR FREEGLUT_GLES1))
145         LIST(APPEND FREEGLUT_SRCS
146             src/x11/fg_internal_x11_glx.h
147             src/x11/fg_display_x11_glx.c
148             src/x11/fg_state_x11_glx.c
149             src/x11/fg_state_x11_glx.h
150             src/x11/fg_window_x11_glx.c
151             src/x11/fg_window_x11_glx.h
152         )
153     ENDIF()
154 ENDIF()
155 IF(FREEGLUT_GLES2 OR FREEGLUT_GLES1)
156     LIST(APPEND FREEGLUT_SRCS
157         src/egl/fg_internal_egl.h
158         src/egl/fg_display_egl.c
159         src/egl/fg_ext_egl.c
160         src/egl/fg_init_egl.c
161         src/egl/fg_init_egl.h
162         src/egl/fg_state_egl.c
163         src/egl/fg_state_egl.h
164         src/egl/fg_structure_egl.c
165         src/egl/fg_structure_egl.h
166         src/egl/fg_window_egl.c
167         src/egl/fg_window_egl.h
168     )
169 ENDIF()
170
171 # For OpenGL ES (GLES):
172 # - compile with -DFREEGLUT_GLES1 and -DFREEGLUT_GLES2 to cleanly
173 #   bootstrap headers inclusion in freeglut_std.h; these constants
174 #   also need to be defined in client applications (e.g. through
175 #   pkg-config), but use GLES constants directly for all other needs
176 # - define GLES version-specific library
177 IF(FREEGLUT_GLES2)
178   ADD_DEFINITIONS(-DFREEGLUT_GLES2)
179   LIST(APPEND LIBS GLESv2 EGL)
180 ELSEIF(FREEGLUT_GLES1)
181   ADD_DEFINITIONS(-DFREEGLUT_GLES1)
182   LIST(APPEND LIBS GLESv1_CM EGL)
183 ELSE()
184   FIND_PACKAGE(OpenGL REQUIRED)
185   LIST(APPEND LIBS ${OPENGL_gl_LIBRARY})
186   INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
187 ENDIF()
188
189 # lib m for math, not needed on windows
190 IF (NOT WIN32)
191     # For compilation:
192     LIST(APPEND LIBS m)
193     # For CHECK_FUNCTION_EXISTS:
194     LIST(APPEND CMAKE_REQUIRED_LIBRARIES m)
195 ENDIF()
196
197 IF(WIN32)
198     # hide insecure CRT warnings, common practice
199     ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
200     IF(MSVC)
201         SET( CMAKE_DEBUG_POSTFIX "d" )
202     ENDIF(MSVC)
203     
204 ENDIF()
205
206 IF(CMAKE_COMPILER_IS_GNUCC)
207   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
208   IF(!ANDROID)
209     # not setting -ansi as EGL/KHR headers doesn't support it
210     SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ansi")
211   ENDIF()
212 ENDIF(CMAKE_COMPILER_IS_GNUCC)
213
214 INCLUDE(CheckIncludeFiles)
215 IF(UNIX AND NOT ANDROID)
216     FIND_PACKAGE(X11 REQUIRED)
217     LIST(APPEND LIBS ${X11_LIBRARIES})
218     IF(X11_Xrandr_FOUND)
219         SET(HAVE_X11_EXTENSIONS_XRANDR_H TRUE)
220         LIST(APPEND LIBS ${X11_Xrandr_LIB})
221     ENDIF()
222     IF(X11_xf86vmode_FOUND)
223         SET(HAVE_X11_EXTENSIONS_XF86VMODE_H TRUE)
224         # Work-around http://www.cmake.org/Bug/bug_view_page.php?bug_id=6976
225         IF(NOT "${X11_Xxf86vm_LIB}")
226             SET(X11_Xxf86vm_LIB "Xxf86vm")
227         ENDIF()
228         LIST(APPEND LIBS ${X11_Xxf86vm_LIB})
229     ENDIF()
230     IF(X11_Xinput_FOUND)
231         # Needed for multi-touch:
232         CHECK_INCLUDE_FILES("${X11_Xinput_INCLUDE_PATH}/X11/extensions/XInput2.h" HAVE_X11_EXTENSIONS_XINPUT2_H)
233         LIST(APPEND LIBS ${X11_Xinput_LIB})
234     ENDIF()
235 ENDIF()
236 IF(ANDROID)
237     # -landroid for ANativeWindow
238     # -llog for native Android logging
239     LIST(APPEND LIBS android log)
240 ENDIF()
241
242 INCLUDE(CheckFunctionExists)
243 INCLUDE(CheckTypeSize)
244 CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
245 CHECK_INCLUDE_FILES(unistd.h    HAVE_UNISTD_H)
246 CHECK_INCLUDE_FILES(sys/time.h  HAVE_SYS_TIME_H)
247 CHECK_INCLUDE_FILES(stdbool.h   HAVE_STDBOOL_H)
248 CHECK_INCLUDE_FILES(sys/param.h HAVE_SYS_PARAM_H)
249 CHECK_INCLUDE_FILES(sys/ioctl.h HAVE_SYS_IOCTL_H)
250 CHECK_INCLUDE_FILES(fcntl.h     HAVE_FCNTL_H)
251 CHECK_INCLUDE_FILES(errno.h     HAVE_ERRNO_H)
252 CHECK_INCLUDE_FILES(usbhid.h    HAVE_USBHID_H)
253 CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
254 CHECK_FUNCTION_EXISTS(vfprintf  HAVE_VFPRINTF)
255 CHECK_FUNCTION_EXISTS(_doprnt   HAVE_DOPRNT)
256 CHECK_FUNCTION_EXISTS(XParseGeometry   HAVE_XPARSEGEOMETRY)
257 IF (NOT HAVE_XPARSEGEOMETRY)
258    LIST(APPEND FREEGLUT_SRCS
259         src/util/xparsegeometry_repl.c
260         src/util/xparsegeometry_repl.h)
261    SET(NEED_XPARSEGEOMETRY_IMPL TRUE)
262 ENDIF()
263 # decide on suitable type for internal time keeping, 64-bit if possible
264 CHECK_INCLUDE_FILES(stdint.h    HAVE_STDINT_H)
265 CHECK_INCLUDE_FILES(inttypes.h  HAVE_INTTYPES_H)
266 IF (NOT (HAVE_STDINT_H OR HAVE_INTTYPES_H))
267     IF (MSVC)
268         # Some old Microsoft VC releases don't support unsigned long
269         # long, but all we care about is support for unsigned __int64 on
270         # MSVC, so test for presence of that type
271         CHECK_TYPE_SIZE("unsigned __int64" U__INT64 BUILTIN_TYPES_ONLY)
272     ELSEIF()
273         CHECK_TYPE_SIZE("unsigned long long" ULONG_LONG BUILTIN_TYPES_ONLY)
274     ENDIF()
275 ENDIF()
276
277 # The generated config.h is placed in the project's build directory, just to
278 # ensure that all CMake-generated files are kept away from the main source tree.
279 # As a result, the build directory must to be added to the include path list.
280 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h)
281 INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src)
282 ADD_DEFINITIONS(-DHAVE_CONFIG_H)
283 IF(WIN32)
284     # we also have to generate freeglut.rc, which contains the version
285     # number
286     CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/freeglut.rc.in ${CMAKE_BINARY_DIR}/freeglut.rc)
287     IF (MSVC AND NOT CMAKE_CL_64)
288         # .def file only for 32bit Windows builds with Visual Studio
289         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/src/freeglutdll.def.in ${CMAKE_BINARY_DIR}/freeglutdll.def)
290     ENDIF()
291 ENDIF()
292
293 IF(BUILD_SHARED_LIBS)
294     ADD_LIBRARY(freeglut SHARED ${FREEGLUT_SRCS})
295 ENDIF()
296 IF(BUILD_STATIC_LIBS)
297     ADD_LIBRARY(freeglut_static STATIC ${FREEGLUT_SRCS})
298 ENDIF()
299
300
301 IF(WIN32)
302     LIST(APPEND LIBS winmm)
303     IF(BUILD_SHARED_LIBS)
304         SET_TARGET_PROPERTIES(freeglut PROPERTIES COMPILE_FLAGS -DFREEGLUT_EXPORTS)
305     ENDIF()
306     IF(BUILD_STATIC_LIBS)
307         SET_TARGET_PROPERTIES(freeglut_static PROPERTIES COMPILE_FLAGS -DFREEGLUT_STATIC)
308         # need to set machine:x64 for linker, at least for VC10, and
309         # doesn't hurt for older compilers:
310         # http://public.kitware.com/Bug/view.php?id=11240#c22768
311         IF (CMAKE_CL_64)
312             SET_TARGET_PROPERTIES(freeglut_static PROPERTIES STATIC_LIBRARY_FLAGS "/machine:x64")
313         ENDIF()
314     ENDIF()
315 ELSE()
316     # on UNIX we need to make sure:
317     # - all shared libraries must have a soname/version, see :
318     #   http://sourceware.org/autobook/autobook/autobook_91.html#SEC91
319     #   http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
320     #   Current: -version-info 12:0:9 -> 3.9.0
321     #   Note: most platforms now prefer the latter major.minor.revision form
322     #   (e.g. FreeBSD, cf. http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8765),
323     #   or special-cased FreeGLUT long ago (e.g. .so.4 on OpenBSD), so
324     #   the lack of support for current:revision:age in CMake should
325     #   not be a problem.
326     # - the output library should be named glut so it'll be linkable with -lglut
327     # - the shared library should link to the dependency libraries so that the user
328     #   won't have to link them explicitly (they shouldn't have to know that we depend
329     #   on Xrandr or Xxf86vm)
330     IF(FREEGLUT_GLES2)
331       SET(LIBNAME freeglut-gles2)
332     ELSEIF(FREEGLUT_GLES1)
333       SET(LIBNAME freeglut-gles1)
334     ELSE()
335       SET(LIBNAME glut)
336     ENDIF()
337
338     SET_TARGET_PROPERTIES(freeglut PROPERTIES VERSION 3.9.0 SOVERSION 3 OUTPUT_NAME ${LIBNAME})
339     SET_TARGET_PROPERTIES(freeglut_static PROPERTIES OUTPUT_NAME ${LIBNAME})
340     IF(ANDROID)
341         # Not in CMake toolchain file, because the toolchain
342         # file is called several times and generally doesn't
343         # seem to be meant for modifying CFLAGS:
344         # '-mandroid' is not mandatory but doesn't hurt
345         # '-O0 -gstabs+' helps the currently buggy GDB port
346         # Too late to manipulate ENV: SET(ENV{CFLAGS} "$ENV{CFLAGS} -mandroid")
347         # Not using _INIT variables, they seem to be used internally only
348         SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mandroid")
349         SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -gstabs+")
350     ENDIF()
351 ENDIF()
352 IF(BUILD_SHARED_LIBS)
353     TARGET_LINK_LIBRARIES(freeglut ${LIBS})
354 ENDIF()
355 IF(BUILD_STATIC_LIBS)
356     TARGET_LINK_LIBRARIES(freeglut_static ${LIBS})
357 ENDIF()
358
359 IF(BUILD_SHARED_LIBS)
360     INSTALL(TARGETS freeglut DESTINATION lib)
361 ENDIF()
362 IF(BUILD_STATIC_LIBS)
363     INSTALL(TARGETS freeglut_static DESTINATION lib)
364 ENDIF()
365 INSTALL(FILES ${FREEGLUT_HEADERS} DESTINATION include/GL)
366
367
368
369 # Optionally build demos, on by default.
370 option( FREEGLUT_BUILD_DEMOS "Build FreeGLUT demos." ON )
371
372 SET(DEMO_LIBS ${OPENGL_glu_LIBRARY} ${LIBS})
373 # lib m for math, not needed on windows
374 IF (NOT WIN32)
375     LIST(APPEND DEMO_LIBS m)
376 ENDIF()
377
378 MACRO(ADD_DEMO name)
379     IF( FREEGLUT_BUILD_DEMOS )
380         IF(BUILD_SHARED_LIBS)
381             ADD_EXECUTABLE(${name} ${ARGN})
382             TARGET_LINK_LIBRARIES(${name} ${DEMO_LIBS} freeglut)
383             IF(WIN32 AND MSVC)
384                 SET_TARGET_PROPERTIES(${name} PROPERTIES DEBUG_POSTFIX d)
385             ENDIF()
386         ENDIF()
387         IF(BUILD_STATIC_LIBS)
388             ADD_EXECUTABLE(${name}_static ${ARGN})
389             TARGET_LINK_LIBRARIES(${name}_static ${DEMO_LIBS} freeglut_static)
390             SET_TARGET_PROPERTIES(${name}_static PROPERTIES COMPILE_FLAGS -DFREEGLUT_STATIC)
391             IF(WIN32 AND MSVC)
392                 SET_TARGET_PROPERTIES(${name}_static PROPERTIES DEBUG_POSTFIX d)
393             ENDIF()
394         ENDIF()
395     ENDIF()
396 ENDMACRO()
397
398 ADD_DEMO(CallbackMaker   progs/demos/CallbackMaker/CallbackMaker.c)
399 ADD_DEMO(Fractals        progs/demos/Fractals/fractals.c)
400 ADD_DEMO(Fractals_random progs/demos/Fractals_random/fractals_random.c)
401 ADD_DEMO(Lorenz          progs/demos/Lorenz/lorenz.c)
402 ADD_DEMO(One             progs/demos/One/one.c)
403 ADD_DEMO(Resizer         progs/demos/Resizer/Resizer.cpp)
404 ADD_DEMO(multi-touch     progs/demos/multi-touch/multi-touch.c)
405 ADD_DEMO(shapes          progs/demos/shapes/shapes.c
406                          progs/demos/shapes/glmatrix.h
407                          progs/demos/shapes/glmatrix.c)
408 ADD_DEMO(smooth_opengl3  progs/demos/smooth_opengl3/smooth_opengl3.c)
409 IF(UNIX)
410     ADD_DEMO(spaceball       progs/demos/spaceball/spaceball.c
411                              progs/demos/spaceball/vmath.c
412                              progs/demos/spaceball/vmath.h)
413 ENDIF()
414 ADD_DEMO(subwin          progs/demos/subwin/subwin.c)
415 ADD_DEMO(timer           progs/demos/timer/timer.c)
416
417
418
419 # pkg-config support, to install at $(libdir)/pkgconfig
420 # Define static build dependencies
421 IF(WIN32)
422   SET(PC_LIBS_PRIVATE "-lopengl32 -lwinmm -lgdi32 -lm")
423 ELSEIF(FREEGLUT_GLES2)
424   IF(ANDROID)
425     SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv2 -lEGL -lm")
426   ELSE()
427     SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv2 -lEGL -lm")
428   ENDIF()
429 ELSEIF(FREEGLUT_GLES1)
430   IF(ANDROID)
431     SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv1_CM -lEGL -lm")
432   ELSE()
433     SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv1_CM -lEGL -lm")
434   ENDIF()
435 ELSE()
436   SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGL -lm")
437 ENDIF()
438 # Client applications need to define FreeGLUT GLES version to
439 # bootstrap headers inclusion in freeglut_std.h:
440 SET(PC_LIBNAME "glut")
441 SET(PC_FILENAME "freeglut.pc")
442 IF(FREEGLUT_GLES2)
443   SET(PC_CFLAGS "-DFREEGLUT_GLES2")
444   SET(PC_LIBNAME "freeglut-gles2")
445   SET(PC_FILENAME "freeglut-gles2.pc")
446 ELSEIF(FREEGLUT_GLES1)
447   SET(PC_CFLAGS "-DFREEGLUT_GLES1")
448   SET(PC_LIBNAME "freeglut-gles1")
449   SET(PC_FILENAME "freeglut-gles1.pc")
450 ENDIF()
451 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/freeglut.pc.in ${CMAKE_BINARY_DIR}/freeglut.pc @ONLY)
452 INSTALL(FILES ${CMAKE_BINARY_DIR}/freeglut.pc DESTINATION share/pkgconfig/ RENAME ${PC_FILENAME})
453 # TODO: change the library and .pc name when building for GLES,
454 # e.g. -lglut-GLES2