Detect sinf/cosf/sqrtf presence with CMake (instead of relying on __cpluscplus)
[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_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
101         LIST(APPEND FREEGLUT_SRCS
102             ${CMAKE_BINARY_DIR}/freeglutdll.def # generated below from src/freeglutdll.def.in
103         )
104     ENDIF()
105
106 ELSEIF(ANDROID)
107     LIST(APPEND FREEGLUT_SRCS
108         src/android/native_app_glue/android_native_app_glue.c
109         src/android/native_app_glue/android_native_app_glue.h
110         src/android/fg_internal_android.h
111         src/android/fg_cursor_android.c
112         src/android/fg_ext_android.c
113         src/android/fg_gamemode_android.c
114         src/android/fg_init_android.c
115         src/android/fg_input_devices_android.c
116         src/android/fg_joystick_android.c
117         src/android/fg_main_android.c
118         src/android/fg_main_android.h
119         src/android/fg_runtime_android.c
120         src/android/fg_spaceball_android.c
121         src/android/fg_state_android.c
122         src/android/fg_structure_android.c
123         src/android/fg_window_android.c
124     )
125 ELSE()
126     LIST(APPEND FREEGLUT_SRCS
127         src/x11/fg_cursor_x11.c
128         src/x11/fg_ext_x11.c
129         src/x11/fg_gamemode_x11.c
130         src/x11/fg_glutfont_definitions_x11.c
131         src/x11/fg_init_x11.c
132         src/x11/fg_internal_x11.h
133         src/x11/fg_input_devices_x11.c
134         src/x11/fg_joystick_x11.c
135         src/x11/fg_main_x11.c
136         src/x11/fg_menu_x11.c
137         src/x11/fg_spaceball_x11.c
138         src/x11/fg_state_x11.c
139         src/x11/fg_structure_x11.c
140         src/x11/fg_window_x11.c
141         src/x11/fg_xinput_x11.c
142     )
143     IF(NOT(FREEGLUT_GLES2 OR FREEGLUT_GLES1))
144         LIST(APPEND FREEGLUT_SRCS
145             src/x11/fg_internal_x11_glx.h
146             src/x11/fg_display_x11_glx.c
147             src/x11/fg_state_x11_glx.c
148             src/x11/fg_state_x11_glx.h
149             src/x11/fg_window_x11_glx.c
150             src/x11/fg_window_x11_glx.h
151         )
152     ENDIF()
153 ENDIF()
154 IF(FREEGLUT_GLES2 OR FREEGLUT_GLES1)
155     LIST(APPEND FREEGLUT_SRCS
156         src/egl/fg_internal_egl.h
157         src/egl/fg_display_egl.c
158         src/egl/fg_ext_egl.c
159         src/egl/fg_init_egl.c
160         src/egl/fg_init_egl.h
161         src/egl/fg_state_egl.c
162         src/egl/fg_state_egl.h
163         src/egl/fg_structure_egl.c
164         src/egl/fg_structure_egl.h
165         src/egl/fg_window_egl.c
166         src/egl/fg_window_egl.h
167     )
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(CMAKE_COMPILER_IS_GNUCC)
199   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
200   IF(!ANDROID)
201     # not setting -ansi as EGL/KHR headers doesn't support it
202     SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ansi")
203   ENDIF()
204 ENDIF(CMAKE_COMPILER_IS_GNUCC)
205
206 IF(UNIX AND NOT ANDROID)
207     FIND_PACKAGE(X11 REQUIRED)
208     LIST(APPEND LIBS ${X11_LIBRARIES})
209     IF(X11_Xrandr_FOUND)
210         SET(HAVE_X11_EXTENSIONS_XRANDR_H TRUE)
211         LIST(APPEND LIBS ${X11_Xrandr_LIB})
212     ENDIF()
213     IF(X11_xf86vmode_FOUND)
214         SET(HAVE_X11_EXTENSIONS_XF86VMODE_H TRUE)
215         # Work-around http://www.cmake.org/Bug/bug_view_page.php?bug_id=6976
216         IF(NOT "${X11_Xxf86vm_LIB}")
217             SET(X11_Xxf86vm_LIB "Xxf86vm")
218         ENDIF()
219         LIST(APPEND LIBS ${X11_Xxf86vm_LIB})
220     ENDIF()
221 ENDIF()
222 IF(ANDROID)
223     # -landroid for ANativeWindow
224     # -llog for native Android logging
225     LIST(APPEND LIBS android log)
226 ENDIF()
227
228 INCLUDE(CheckIncludeFiles)
229 INCLUDE(CheckFunctionExists)
230 INCLUDE(CheckTypeSize)
231 CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
232 CHECK_INCLUDE_FILES(unistd.h    HAVE_UNISTD_H)
233 CHECK_INCLUDE_FILES(sys/time.h  HAVE_SYS_TIME_H)
234 CHECK_INCLUDE_FILES(stdbool.h   HAVE_STDBOOL_H)
235 CHECK_INCLUDE_FILES(sys/param.h HAVE_SYS_PARAM_H)
236 CHECK_INCLUDE_FILES(sys/ioctl.h HAVE_SYS_IOCTL_H)
237 CHECK_INCLUDE_FILES(fcntl.h     HAVE_FCNTL_H)
238 CHECK_INCLUDE_FILES(errno.h     HAVE_ERRNO_H)
239 CHECK_INCLUDE_FILES(usbhid.h    HAVE_USBHID_H)
240 CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
241 CHECK_FUNCTION_EXISTS(vfprintf  HAVE_VFPRINTF)
242 CHECK_FUNCTION_EXISTS(_doprnt   HAVE_DOPRNT)
243 CHECK_FUNCTION_EXISTS(XParseGeometry   HAVE_XPARSEGEOMETRY)
244 IF (NOT HAVE_XPARSEGEOMETRY)
245    LIST(APPEND FREEGLUT_SRCS
246         src/util/xparsegeometry_repl.c
247         src/util/xparsegeometry_repl.h)
248    SET(NEED_XPARSEGEOMETRY_IMPL TRUE)
249 ENDIF()
250 # decide on suitable type for internal time keeping, 64-bit if possible
251 CHECK_INCLUDE_FILES(stdint.h    HAVE_STDINT_H)
252 CHECK_INCLUDE_FILES(inttypes.h  HAVE_INTTYPES_H)
253 IF (NOT (HAVE_STDINT_H OR HAVE_INTTYPES_H))
254     IF (MSVC)
255         # Some old Microsoft VC don't support unsigned long long, but all we
256         # care about support unsigned __int64, so test for presence of that
257         # type
258         CHECK_TYPE_SIZE("unsigned __int64" U__INT64 BUILTIN_TYPES_ONLY)
259     ELSEIF()
260         CHECK_TYPE_SIZE("unsigned long long" ULONG_LONG BUILTIN_TYPES_ONLY)
261     ENDIF()
262 ENDIF()
263
264 # Check for sinf/cosf/sqrtf
265 # CHECK_FUNCTION_EXISTS doesn't work because check requires <math.h>
266 #CHECK_FUNCTION_EXISTS(sinf      HAVE_SINF)
267 #CHECK_FUNCTION_EXISTS(cosf      HAVE_COSF)
268 #CHECK_FUNCTION_EXISTS(sqrtf     HAVE_SQRTF)
269 INCLUDE(CheckCSourceCompiles)
270 CHECK_C_SOURCE_COMPILES("
271     #include <math.h>
272     int main(){sinf(0); return 0;}
273     " HAVE_SINF
274 )
275 CHECK_C_SOURCE_COMPILES("
276     #include <math.h>
277     int main(){cosf(0); return 0;}
278     " HAVE_COSF
279 )
280 CHECK_C_SOURCE_COMPILES("
281     #include <math.h>
282     int main(){sqrtf(0); return 0;}
283     " HAVE_SQRTF
284 )
285
286 # The generated config.h is placed in the project's build directory, just to
287 # ensure that all CMake-generated files are kept away from the main source tree.
288 # As a result, the build directory must to be added to the include path list.
289 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h)
290 INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src)
291 ADD_DEFINITIONS(-DHAVE_CONFIG_H)
292 IF(WIN32)
293     # we also have to generate freeglut.rc, which contains the version
294     # number
295     CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/freeglut.rc.in ${CMAKE_BINARY_DIR}/freeglut.rc)
296     IF (MSVC AND NOT CMAKE_CL_64)
297         # .def file only for 32bit Windows builds with Visual Studio
298         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/src/freeglutdll.def.in ${CMAKE_BINARY_DIR}/freeglutdll.def)
299     ENDIF()
300 ENDIF()
301
302 IF(BUILD_SHARED_LIBS)
303     ADD_LIBRARY(freeglut SHARED ${FREEGLUT_SRCS})
304 ENDIF()
305 IF(BUILD_STATIC_LIBS)
306     ADD_LIBRARY(freeglut_static STATIC ${FREEGLUT_SRCS})
307 ENDIF()
308
309
310 IF(WIN32)
311     LIST(APPEND LIBS winmm)
312     IF(BUILD_SHARED_LIBS)
313         SET_TARGET_PROPERTIES(freeglut PROPERTIES COMPILE_FLAGS -DFREEGLUT_EXPORTS)
314     ENDIF()
315     IF(BUILD_STATIC_LIBS)
316         SET_TARGET_PROPERTIES(freeglut_static PROPERTIES COMPILE_FLAGS -DFREEGLUT_STATIC)
317         # need to set machine:x64 for linker, at least for VC10, and
318         # doesn't hurt for older compilers:
319         # http://public.kitware.com/Bug/view.php?id=11240#c22768
320         IF (CMAKE_CL_64)
321             SET_TARGET_PROPERTIES(freeglut_static PROPERTIES STATIC_LIBRARY_FLAGS "/machine:x64")
322         ENDIF()
323     ENDIF()
324 ELSE()
325     # on UNIX we need to make sure:
326     # - all shared libraries must have a soname/version, see :
327     #   http://sourceware.org/autobook/autobook/autobook_91.html#SEC91
328     #   http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
329     #   Current: -version-info 12:0:9 -> 3.9.0
330     # - the output library should be named glut so it'll be linkable with -lglut
331     # - the shared library should link to the dependency libraries so that the user
332     #   won't have to link them explicitly (they shouldn't have to know that we depend
333     #   on Xrandr or Xxf86vm)
334     IF(FREEGLUT_GLES2)
335       SET(LIBNAME freeglut-gles2)
336     ELSEIF(FREEGLUT_GLES1)
337       SET(LIBNAME freeglut-gles1)
338     ELSE()
339       SET(LIBNAME glut)
340     ENDIF()
341
342     SET_TARGET_PROPERTIES(freeglut PROPERTIES VERSION 3.9.0 SOVERSION 3 OUTPUT_NAME ${LIBNAME})
343     SET_TARGET_PROPERTIES(freeglut_static PROPERTIES OUTPUT_NAME ${LIBNAME})
344     IF(ANDROID)
345         # Not in CMake toolchain file, because the toolchain
346         # file is called several times and generally doesn't
347         # seem to be meant for modifying CFLAGS:
348         # '-mandroid' is not mandatory but doesn't hurt
349         # '-O0 -gstabs+' helps the currently buggy GDB port
350         # Too late to manipulate ENV: SET(ENV{CFLAGS} "$ENV{CFLAGS} -mandroid")
351         # Not using _INIT variables, they seem to be used internally only
352         SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mandroid")
353         SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -gstabs+")
354     ENDIF()
355 ENDIF()
356 IF(BUILD_SHARED_LIBS)
357     TARGET_LINK_LIBRARIES(freeglut ${LIBS})
358 ENDIF()
359 IF(BUILD_STATIC_LIBS)
360     TARGET_LINK_LIBRARIES(freeglut_static ${LIBS})
361 ENDIF()
362
363 IF(BUILD_SHARED_LIBS)
364     INSTALL(TARGETS freeglut DESTINATION lib)
365 ENDIF()
366 IF(BUILD_STATIC_LIBS)
367     INSTALL(TARGETS freeglut_static DESTINATION lib)
368 ENDIF()
369 INSTALL(FILES ${FREEGLUT_HEADERS} DESTINATION include/GL)
370
371
372
373 # Optionally build demos, on by default.
374 option( FREEGLUT_BUILD_DEMOS "Build FreeGLUT demos." ON )
375
376 SET(DEMO_LIBS ${OPENGL_glu_LIBRARY} ${LIBS})
377 # lib m for math, not needed on windows
378 IF (NOT WIN32)
379     LIST(APPEND DEMO_LIBS m)
380 ENDIF()
381
382 MACRO(ADD_DEMO name)
383     IF( FREEGLUT_BUILD_DEMOS )
384         IF(BUILD_SHARED_LIBS)
385             ADD_EXECUTABLE(${name} ${ARGN})
386             TARGET_LINK_LIBRARIES(${name} ${DEMO_LIBS} freeglut)
387         ENDIF()
388         IF(BUILD_STATIC_LIBS)
389             ADD_EXECUTABLE(${name}_static ${ARGN})
390             TARGET_LINK_LIBRARIES(${name}_static ${DEMO_LIBS} freeglut_static)
391             SET_TARGET_PROPERTIES(${name}_static PROPERTIES COMPILE_FLAGS -DFREEGLUT_STATIC)
392         ENDIF()
393     ENDIF()
394 ENDMACRO()
395
396 ADD_DEMO(CallbackMaker   progs/demos/CallbackMaker/CallbackMaker.c)
397 ADD_DEMO(Fractals        progs/demos/Fractals/fractals.c)
398 ADD_DEMO(Fractals_random progs/demos/Fractals_random/fractals_random.c)
399 ADD_DEMO(Lorenz          progs/demos/Lorenz/lorenz.c)
400 ADD_DEMO(One             progs/demos/One/one.c)
401 ADD_DEMO(Resizer         progs/demos/Resizer/Resizer.cpp)
402 ADD_DEMO(shapes          progs/demos/shapes/shapes.c)
403 ADD_DEMO(smooth_opengl3  progs/demos/smooth_opengl3/smooth_opengl3.c)
404 ADD_DEMO(spaceball       progs/demos/spaceball/spaceball.c
405                          progs/demos/spaceball/vmath.c
406                          progs/demos/spaceball/vmath.h)
407 ADD_DEMO(subwin          progs/demos/subwin/subwin.c)
408 ADD_DEMO(timer           progs/demos/timer/timer.c)
409
410 # pkg-config support, to install at $(libdir)/pkgconfig
411 # Define static build dependencies
412 IF(WIN32)
413   SET(PC_LIBS_PRIVATE "-lopengl32 -lwinmm -lgdi32 -lm")
414 ELSEIF(FREEGLUT_GLES2)
415   IF(ANDROID)
416     SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv2 -lEGL -lm")
417   ELSE()
418     SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv2 -lEGL -lm")
419   ENDIF()
420 ELSEIF(FREEGLUT_GLES1)
421   IF(ANDROID)
422     SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv1_CM -lEGL -lm")
423   ELSE()
424     SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv1_CM -lEGL -lm")
425   ENDIF()
426 ELSE()
427   SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGL -lm")
428 ENDIF()
429 # Client applications need to define FreeGLUT GLES version to
430 # bootstrap headers inclusion in freeglut_std.h:
431 SET(PC_LIBNAME "glut")
432 SET(PC_FILENAME "freeglut.pc")
433 IF(FREEGLUT_GLES2)
434   SET(PC_CFLAGS "-DFREEGLUT_GLES2")
435   SET(PC_LIBNAME "freeglut-gles2")
436   SET(PC_FILENAME "freeglut-gles2.pc")
437 ELSEIF(FREEGLUT_GLES1)
438   SET(PC_CFLAGS "-DFREEGLUT_GLES1")
439   SET(PC_LIBNAME "freeglut-gles1")
440   SET(PC_FILENAME "freeglut-gles1.pc")
441 ENDIF()
442 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/freeglut.pc.in ${CMAKE_BINARY_DIR}/freeglut.pc @ONLY)
443 INSTALL(FILES ${CMAKE_BINARY_DIR}/freeglut.pc DESTINATION share/pkgconfig/ RENAME ${PC_FILENAME})
444 # TODO: change the library and .pc name when building for GLES,
445 # e.g. -lglut-GLES2