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