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