now only setting FREEGLUT_EXPORTS and FREEGLUT_STATIC for the shared and
[freeglut] / CMakeLists.txt
1 PROJECT(freeglut)
2 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
3
4 SET(FREEGLUT_HEADERS
5         include/GL/freeglut.h
6         include/GL/freeglut_ext.h
7         include/GL/freeglut_std.h
8         include/GL/glut.h
9 )
10 SET(FREEGLUT_SRCS
11         ${FREEGLUT_HEADERS}
12         src/fg_callbacks.c
13         src/fg_cursor.c
14         src/fg_display.c
15         src/fg_ext.c
16         src/fg_font_data.c
17         src/fg_gamemode.c
18         src/fg_init.c
19         src/fg_internal.h
20         src/fg_input_devices.c
21         src/fg_joystick.c
22         src/fg_main.c
23         src/fg_misc.c
24         src/fg_overlay.c
25         src/fg_spaceball.c
26         src/fg_state.c
27         src/fg_stroke_mono_roman.c
28         src/fg_stroke_roman.c
29         src/fg_structure.c
30         src/fg_videoresize.c
31         src/fg_window.c
32 )
33 # Android port requires adding a version of these compatible with
34 # OpenGL ES (TODO):
35 IF(NOT ANDROID)
36     LIST(APPEND FREEGLUT_SRCS
37         src/fg_font.c
38         src/fg_geometry.c
39         src/fg_menu.c
40         src/fg_teapot.c
41 )
42 ENDIF()
43
44 IF(WIN32)
45         LIST(APPEND FREEGLUT_SRCS
46                 src/mswin/fg_cursor_mswin.c
47                 src/mswin/fg_display_mswin.c
48                 src/mswin/fg_ext_mswin.c
49                 src/mswin/fg_gamemode_mswin.c
50                 src/mswin/fg_init_mswin.c
51                 src/mswin/fg_internal_mswin.h
52                 src/mswin/fg_input_devices_mswin.c
53                 src/mswin/fg_joystick_mswin.c
54                 src/mswin/fg_main_mswin.c
55                 src/mswin/fg_menu_mswin.c
56                 src/mswin/fg_spaceball_mswin.c
57                 src/mswin/fg_state_mswin.c
58                 src/mswin/fg_structure_mswin.c
59                 src/mswin/fg_window_mswin.c
60 )
61 ELSEIF(ANDROID)
62     LIST(APPEND FREEGLUT_SRCS
63         src/egl/fg_internal_egl.h
64         src/egl/fg_display_egl.c
65         src/egl/fg_init_egl.c
66         src/egl/fg_structure_egl.c
67         src/egl/fg_window_egl.c
68         src/android/native_app_glue/android_native_app_glue.c
69         src/android/native_app_glue/android_native_app_glue.h
70         src/android/fg_runtime_android.c
71         src/android/fg_gamemode_android.c
72         src/android/fg_input_devices_android.c
73         src/android/fg_joystick_android.c
74         src/android/fg_main_android.c
75         src/android/fg_spaceball_android.c
76         src/android/fg_state_android.c
77         src/android/fg_window_android.c
78         src/android/opengles_stubs.c
79         src/android/fg_internal_android.h
80 )
81 ELSE()
82         LIST(APPEND FREEGLUT_SRCS
83                 src/x11/fg_cursor_x11.c
84                 src/x11/fg_display_x11.c
85                 src/x11/fg_ext_x11.c
86                 src/x11/fg_gamemode_x11.c
87                 src/x11/fg_glutfont_definitions_x11.c
88                 src/x11/fg_init_x11.c
89                 src/x11/fg_internal_x11.h
90                 src/x11/fg_input_devices_x11.c
91                 src/x11/fg_joystick_x11.c
92                 src/x11/fg_main_x11.c
93                 src/x11/fg_menu_x11.c
94                 src/x11/fg_spaceball_x11.c
95                 src/x11/fg_state_x11.c
96                 src/x11/fg_structure_x11.c
97                 src/x11/fg_window_x11.c
98                 src/x11/fg_xinput_x11.c
99 )
100
101
102 ENDIF()
103
104 # Neatly organize all of the output files in the build directory
105 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
106 SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
107
108 # BUILD_SHARED_LIBS is already a standard CMake variable, but we need to
109 # re-declare it here so it will show up in the GUI.
110 OPTION(BUILD_SHARED_LIBS "Build FreeGLUT shared library." ON)
111 OPTION(BUILD_STATIC_LIBS "Build FreeGLUT static library." ON)
112
113 # OpenGL ES support
114 OPTION(FREEGLUT_GLES1 "Use OpenGL ES 1.x (requires EGL)" OFF)
115 OPTION(FREEGLUT_GLES2 "Use OpenGL ES 2.x (requires EGL) (overrides BUILD_GLES1)" OFF)
116
117 IF(FREEGLUT_GLES2)
118   ADD_DEFINITIONS(-DGLESv2)
119   LIST(APPEND LIBS GLESv2 EGL)
120 ELSEIF(FREEGLUT_GLES1)
121   ADD_DEFINITIONS(-DGLESv1)
122   LIST(APPEND LIBS GLESv1 EGL)
123 ELSE()
124   FIND_PACKAGE(OpenGL REQUIRED)
125   LIST(APPEND LIBS ${OPENGL_gl_LIBRARY})
126   INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
127 ENDIF()
128
129 # lib m for math, not needed on windows
130 IF (NOT WIN32)
131     LIST(APPEND LIBS m)
132 ENDIF()
133
134 IF(WIN32)
135         MESSAGE(WARNING "Insecure CRT warnings hidden (might want to fix these)")
136         ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
137 ENDIF()
138
139 if(UNIX AND NOT ANDROID)
140         FIND_PACKAGE(X11 REQUIRED)
141         LIST(APPEND LIBS ${X11_LIBRARIES})
142         IF(X11_Xrandr_FOUND)
143                 SET(HAVE_X11_EXTENSIONS_XRANDR_H TRUE)
144                 LIST(APPEND LIBS ${X11_Xrandr_LIB})
145         ENDIF()
146         IF(X11_xf86vmode_FOUND)
147                 SET(HAVE_X11_EXTENSIONS_XF86VMODE_H TRUE)
148                 # Work-around http://www.cmake.org/Bug/bug_view_page.php?bug_id=6976
149                 IF(NOT "${X11_Xxf86vm_LIB}")
150                         SET(X11_Xxf86vm_LIB "Xxf86vm")
151                 ENDIF()
152                 LIST(APPEND LIBS ${X11_Xxf86vm_LIB})
153         ENDIF()
154 ENDIF()
155
156 INCLUDE(CheckIncludeFiles)
157 INCLUDE(CheckFunctionExists)
158 INCLUDE(CheckTypeSize)
159 CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
160 CHECK_INCLUDE_FILES(unistd.h    HAVE_UNISTD_H)
161 CHECK_INCLUDE_FILES(sys/time.h  HAVE_SYS_TIME_H)
162 CHECK_INCLUDE_FILES(stdbool.h   HAVE_STDBOOL_H)
163 CHECK_INCLUDE_FILES(sys/param.h HAVE_SYS_PARAM_H)
164 CHECK_INCLUDE_FILES(sys/ioctl.h HAVE_SYS_IOCTL_H)
165 CHECK_INCLUDE_FILES(fcntl.h     HAVE_FCNTL_H)
166 CHECK_INCLUDE_FILES(errno.h     HAVE_ERRNO_H)
167 CHECK_INCLUDE_FILES(usbhid.h    HAVE_USBHID_H)
168 CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
169 CHECK_FUNCTION_EXISTS(vfprintf  HAVE_VFPRINTF)
170 CHECK_FUNCTION_EXISTS(_doprnt   HAVE_DOPRNT)
171 CHECK_FUNCTION_EXISTS(XParseGeometry   HAVE_XPARSEGEOMETRY)
172 IF (NOT HAVE_XPARSEGEOMETRY)
173    LIST(APPEND FREEGLUT_SRCS
174                 src/util/xparsegeometry_repl.c
175                 src/util/xparsegeometry_repl.h)
176    SET(NEED_XPARSEGEOMETRY_IMPL TRUE)
177 ENDIF()
178 # decide on suitable type for internal time keeping, 64-bit if possible
179 CHECK_INCLUDE_FILES(stdint.h    HAVE_STDINT_H)
180 CHECK_INCLUDE_FILES(inttypes.h  HAVE_INTTYPES_H)
181 IF (NOT (HAVE_STDINT_H OR HAVE_INTTYPES_H))
182     IF (MSVC)
183         # Some old Microsoft VC don't support unsigned long long, but all we
184         # care about support unsigned __int64, so test for presence of that
185         # type
186         CHECK_TYPE_SIZE("unsigned __int64" U__INT64 BUILTIN_TYPES_ONLY)
187     ELSEIF()
188         CHECK_TYPE_SIZE("unsigned long long" ULONG_LONG BUILTIN_TYPES_ONLY)
189     ENDIF()
190 ENDIF()
191
192
193 # The generated config.h is placed in the project's build directory, just to
194 # ensure that all CMake-generated files are kept away from the main source tree.
195 # As a result, the build directory must to be added to the include path list.
196 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h)
197 INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src)
198 ADD_DEFINITIONS(-DHAVE_CONFIG_H)
199
200 # by default, at least on UNIX, we want to build both
201 IF(BUILD_SHARED_LIBS)
202         ADD_LIBRARY(freeglut SHARED ${FREEGLUT_SRCS})
203 ENDIF()
204 IF(BUILD_STATIC_LIBS)
205         ADD_LIBRARY(freeglut_static STATIC ${FREEGLUT_SRCS})
206 ENDIF()
207
208
209 IF(WIN32)
210         LIST(APPEND LIBS winmm)
211         IF(BUILD_SHARED_LIBS)
212                 LIST(APPEND FREEGLUT_SRCS freeglut.rc)
213         IF (NOT CMAKE_CL_64)
214             # .def file only for 32bit Windows builds
215             LIST(APPEND FREEGLUT_SRCS src/Common/freeglutdll.def)
216         ENDIF()
217                 SET_TARGET_PROPERTIES(freeglut PROPERTIES COMPILE_FLAGS -DFREEGLUT_EXPORTS)
218         ENDIF()
219     IF(BUILD_STATIC_LIBS)
220         SET_TARGET_PROPERTIES(freeglut_static PROPERTIES COMPILE_FLAGS -DFREEGLUT_STATIC)
221         ENDIF()
222 ELSE()
223         # on UNIX we need to make sure:
224         # - all shared libraries must have a soname/version, see :
225         #   http://sourceware.org/autobook/autobook/autobook_91.html#SEC91
226         #   http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
227         #   Current: -version-info 12:0:9 -> 3.9.0
228         # - the output library should be named glut so it'll be linkable with -lglut
229         # - the shared library should link to the dependency libraries so that the user
230         #   won't have to link them explicitly (they shouldn't have to know that we depend
231         #   on Xrandr or Xxf86vm)
232         SET_TARGET_PROPERTIES(freeglut PROPERTIES VERSION 3.9.0 SOVERSION 3 OUTPUT_NAME glut)
233         SET_TARGET_PROPERTIES(freeglut_static PROPERTIES OUTPUT_NAME glut)
234         IF(ANDROID)
235                 # Not in CMake toolchain file, because the toolchain
236                 # file is called several times and generally doesn't
237                 # seem to be meant for it:
238                 # '-mandroid' is not mandatory but doesn't hurt
239                 # '-O0 -gstabs+' helps the currently buggy GDB port
240                 # '-DANDROID' is the Android build system convention
241                 # Too late to manipulate ENV: SET(ENV{CFLAGS} "$ENV{CFLAGS} -mandroid")
242                 # Not using _INIT variables, they seem to be used internally only
243                 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mandroid")
244                 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -gstabs+")
245                 ADD_DEFINITIONS(-DANDROID)
246         ENDIF()
247 ENDIF()
248 IF(BUILD_SHARED_LIBS)
249     TARGET_LINK_LIBRARIES(freeglut ${LIBS})
250 ENDIF()
251 IF(BUILD_STATIC_LIBS)
252     TARGET_LINK_LIBRARIES(freeglut_static ${LIBS})
253 ENDIF()
254
255 IF(BUILD_SHARED_LIBS)
256         INSTALL(TARGETS freeglut DESTINATION lib)
257 ENDIF()
258 IF(BUILD_STATIC_LIBS)
259         INSTALL(TARGETS freeglut_static DESTINATION lib)
260 ENDIF()
261 INSTALL(FILES ${FREEGLUT_HEADERS} DESTINATION include/GL)
262
263
264
265 # Optionally build demos, on by default.
266 option( FREEGLUT_BUILD_DEMOS "Build FreeGLUT demos." ON )
267
268 SET(DEMO_LIBS ${OPENGL_glu_LIBRARY} ${LIBS})
269 # lib m for math, not needed on windows
270 IF (NOT WIN32)
271     LIST(APPEND DEMO_LIBS m)
272 ENDIF()
273
274 MACRO(ADD_DEMO name)
275     IF( FREEGLUT_BUILD_DEMOS )
276         IF(BUILD_SHARED_LIBS)
277             ADD_EXECUTABLE(${name} ${ARGN})
278             TARGET_LINK_LIBRARIES(${name} ${DEMO_LIBS} freeglut)
279         ENDIF()
280         IF(BUILD_STATIC_LIBS)
281             ADD_EXECUTABLE(${name}_static ${ARGN})
282             TARGET_LINK_LIBRARIES(${name}_static ${DEMO_LIBS} freeglut_static)
283             SET_TARGET_PROPERTIES(${name}_static PROPERTIES COMPILE_FLAGS -DFREEGLUT_STATIC)
284         ENDIF()
285     ENDIF()
286 ENDMACRO()
287
288 ADD_DEMO(CallbackMaker   progs/demos/CallbackMaker/CallbackMaker.c)
289 ADD_DEMO(Fractals        progs/demos/Fractals/fractals.c)
290 ADD_DEMO(Fractals_random progs/demos/Fractals_random/fractals_random.c)
291 ADD_DEMO(Lorenz          progs/demos/Lorenz/lorenz.c)
292 ADD_DEMO(One             progs/demos/One/one.c)
293 ADD_DEMO(Resizer         progs/demos/Resizer/Resizer.cpp)
294 ADD_DEMO(shapes          progs/demos/shapes/shapes.c)
295 ADD_DEMO(smooth_opengl3  progs/demos/smooth_opengl3/smooth_opengl3.c)
296 ADD_DEMO(spaceball       progs/demos/spaceball/spaceball.c
297                          progs/demos/spaceball/vmath.c
298                          progs/demos/spaceball/vmath.h)
299 ADD_DEMO(subwin          progs/demos/subwin/subwin.c)
300 ADD_DEMO(timer           progs/demos/timer/timer.c)