1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindFLTK2
6---------
7
8Find the native FLTK 2.0 includes and library
9
10The following settings are defined
11
12::
13
14  FLTK2_FLUID_EXECUTABLE, where to find the Fluid tool
15  FLTK2_WRAP_UI, This enables the FLTK2_WRAP_UI command
16  FLTK2_INCLUDE_DIR, where to find include files
17  FLTK2_LIBRARIES, list of fltk2 libraries
18  FLTK2_FOUND, Don't use FLTK2 if false.
19
20The following settings should not be used in general.
21
22::
23
24  FLTK2_BASE_LIBRARY   = the full path to fltk2.lib
25  FLTK2_GL_LIBRARY     = the full path to fltk2_gl.lib
26  FLTK2_IMAGES_LIBRARY = the full path to fltk2_images.lib
27#]=======================================================================]
28
29set (FLTK2_DIR $ENV{FLTK2_DIR} )
30
31#  Platform dependent libraries required by FLTK2
32if(WIN32)
33  if(NOT CYGWIN)
34    if(BORLAND)
35      set( FLTK2_PLATFORM_DEPENDENT_LIBS import32 )
36    else()
37      set( FLTK2_PLATFORM_DEPENDENT_LIBS wsock32 comctl32 )
38    endif()
39  endif()
40endif()
41
42if(UNIX)
43  include(${CMAKE_ROOT}/Modules/FindX11.cmake)
44  set( FLTK2_PLATFORM_DEPENDENT_LIBS ${X11_LIBRARIES} -lm)
45endif()
46
47if(APPLE)
48  set( FLTK2_PLATFORM_DEPENDENT_LIBS  "-framework Carbon -framework Cocoa -framework ApplicationServices -lz")
49endif()
50
51# If FLTK2_INCLUDE_DIR is already defined we assign its value to FLTK2_DIR
52if(FLTK2_INCLUDE_DIR)
53  set(FLTK2_DIR ${FLTK2_INCLUDE_DIR})
54else()
55  set(FLTK2_INCLUDE_DIR ${FLTK2_DIR})
56endif()
57
58
59# If FLTK2 has been built using CMake we try to find everything directly
60set(FLTK2_DIR_STRING "directory containing FLTK2Config.cmake.  This is either the root of the build tree, or PREFIX/lib/fltk for an installation.")
61
62# Search only if the location is not already known.
63if(NOT FLTK2_DIR)
64  # Get the system search path as a list.
65  file(TO_CMAKE_PATH "$ENV{PATH}" FLTK2_DIR_SEARCH2)
66
67  # Construct a set of paths relative to the system search path.
68  set(FLTK2_DIR_SEARCH "")
69  foreach(dir ${FLTK2_DIR_SEARCH2})
70    set(FLTK2_DIR_SEARCH ${FLTK2_DIR_SEARCH} "${dir}/../lib/fltk")
71  endforeach()
72  string(REPLACE "//" "/" FLTK2_DIR_SEARCH "${FLTK2_DIR_SEARCH}")
73
74  #
75  # Look for an installation or build tree.
76  #
77  find_path(FLTK2_DIR FLTK2Config.cmake
78    # Look for an environment variable FLTK2_DIR.
79    ENV FLTK2_DIR
80
81    # Look in places relative to the system executable search path.
82    ${FLTK2_DIR_SEARCH}
83
84    PATH_SUFFIXES
85    fltk2
86    fltk2/include
87    lib/fltk2
88    lib/fltk2/include
89
90    # Help the user find it if we cannot.
91    DOC "The ${FLTK2_DIR_STRING}"
92    )
93
94  if(NOT FLTK2_DIR)
95    find_path(FLTK2_DIR fltk/run.h ${FLTK2_INCLUDE_SEARCH_PATH})
96  endif()
97
98endif()
99
100
101# If FLTK2 was found, load the configuration file to get the rest of the
102# settings.
103if(FLTK2_DIR)
104
105  # Check if FLTK2 was built using CMake
106  if(EXISTS ${FLTK2_DIR}/FLTK2Config.cmake)
107    set(FLTK2_BUILT_WITH_CMAKE 1)
108  endif()
109
110  if(FLTK2_BUILT_WITH_CMAKE)
111    set(FLTK2_FOUND 1)
112    include(${FLTK2_DIR}/FLTK2Config.cmake)
113
114    # Fluid
115    if(FLUID_COMMAND)
116      set(FLTK2_FLUID_EXECUTABLE ${FLUID_COMMAND} CACHE FILEPATH "Fluid executable")
117    else()
118      find_program(FLTK2_FLUID_EXECUTABLE fluid2 PATHS
119        ${FLTK2_EXECUTABLE_DIRS}
120        ${FLTK2_EXECUTABLE_DIRS}/RelWithDebInfo
121        ${FLTK2_EXECUTABLE_DIRS}/Debug
122        ${FLTK2_EXECUTABLE_DIRS}/Release
123        NO_SYSTEM_PATH)
124    endif()
125
126    mark_as_advanced(FLTK2_FLUID_EXECUTABLE)
127    set( FLTK_FLUID_EXECUTABLE ${FLTK2_FLUID_EXECUTABLE} )
128
129
130
131
132    set(FLTK2_INCLUDE_DIR ${FLTK2_DIR})
133    link_directories(${FLTK2_LIBRARY_DIRS})
134
135    set(FLTK2_BASE_LIBRARY fltk2)
136    set(FLTK2_GL_LIBRARY fltk2_gl)
137    set(FLTK2_IMAGES_LIBRARY fltk2_images)
138
139    # Add the extra libraries
140    load_cache(${FLTK2_DIR}
141      READ_WITH_PREFIX
142      FL FLTK2_USE_SYSTEM_JPEG
143      FL FLTK2_USE_SYSTEM_PNG
144      FL FLTK2_USE_SYSTEM_ZLIB
145      )
146
147    set(FLTK2_IMAGES_LIBS "")
148    if(FLFLTK2_USE_SYSTEM_JPEG)
149      set(FLTK2_IMAGES_LIBS ${FLTK2_IMAGES_LIBS} fltk2_jpeg)
150    endif()
151    if(FLFLTK2_USE_SYSTEM_PNG)
152      set(FLTK2_IMAGES_LIBS ${FLTK2_IMAGES_LIBS} fltk2_png)
153    endif()
154    if(FLFLTK2_USE_SYSTEM_ZLIB)
155      set(FLTK2_IMAGES_LIBS ${FLTK2_IMAGES_LIBS} fltk2_zlib)
156    endif()
157    set(FLTK2_IMAGES_LIBS "${FLTK2_IMAGES_LIBS}" CACHE INTERNAL
158      "Extra libraries for fltk2_images library.")
159
160  else()
161
162    # if FLTK2 was not built using CMake
163    # Find fluid executable.
164    find_program(FLTK2_FLUID_EXECUTABLE fluid2 ${FLTK2_INCLUDE_DIR}/fluid)
165
166    # Use location of fluid to help find everything else.
167    set(FLTK2_INCLUDE_SEARCH_PATH "")
168    set(FLTK2_LIBRARY_SEARCH_PATH "")
169    if(FLTK2_FLUID_EXECUTABLE)
170      set( FLTK_FLUID_EXECUTABLE ${FLTK2_FLUID_EXECUTABLE} )
171      get_filename_component(FLTK2_BIN_DIR "${FLTK2_FLUID_EXECUTABLE}" PATH)
172      set(FLTK2_INCLUDE_SEARCH_PATH ${FLTK2_INCLUDE_SEARCH_PATH}
173        ${FLTK2_BIN_DIR}/../include ${FLTK2_BIN_DIR}/..)
174      set(FLTK2_LIBRARY_SEARCH_PATH ${FLTK2_LIBRARY_SEARCH_PATH}
175        ${FLTK2_BIN_DIR}/../lib)
176      set(FLTK2_WRAP_UI 1)
177    endif()
178
179    find_path(FLTK2_INCLUDE_DIR fltk/run.h ${FLTK2_INCLUDE_SEARCH_PATH} PATH_SUFFIXES fltk2 fltk2/include)
180
181    list(APPEND FLTK2_LIBRARY_SEARCH_PATH ${FLTK2_INCLUDE_DIR}/lib)
182
183    find_library(FLTK2_BASE_LIBRARY NAMES fltk2
184      PATHS ${FLTK2_LIBRARY_SEARCH_PATH} PATH_SUFFIXES fltk2 fltk2/lib)
185    find_library(FLTK2_GL_LIBRARY NAMES fltk2_gl
186      PATHS ${FLTK2_LIBRARY_SEARCH_PATH} PATH_SUFFIXES fltk2 fltk2/lib)
187    find_library(FLTK2_IMAGES_LIBRARY NAMES fltk2_images
188      PATHS ${FLTK2_LIBRARY_SEARCH_PATH} PATH_SUFFIXES fltk2 fltk2/lib)
189
190    # Find the extra libraries needed for the fltk_images library.
191    if(UNIX)
192      find_program(FLTK2_CONFIG_SCRIPT fltk2-config PATHS ${FLTK2_BIN_DIR})
193      if(FLTK2_CONFIG_SCRIPT)
194        exec_program(${FLTK2_CONFIG_SCRIPT} ARGS --use-images --ldflags
195          OUTPUT_VARIABLE FLTK2_IMAGES_LDFLAGS)
196        set(FLTK2_LIBS_EXTRACT_REGEX ".*-lfltk2_images (.*) -lfltk2.*")
197        if("${FLTK2_IMAGES_LDFLAGS}" MATCHES "${FLTK2_LIBS_EXTRACT_REGEX}")
198          string(REGEX REPLACE " +" ";" FLTK2_IMAGES_LIBS "${CMAKE_MATCH_1}")
199          # The EXEC_PROGRAM will not be inherited into subdirectories from
200          # the file that originally included this module.  Save the answer.
201          set(FLTK2_IMAGES_LIBS "${FLTK2_IMAGES_LIBS}" CACHE INTERNAL
202            "Extra libraries for fltk_images library.")
203        endif()
204      endif()
205    endif()
206
207  endif()
208endif()
209
210
211set(FLTK2_FOUND 1)
212foreach(var FLTK2_FLUID_EXECUTABLE FLTK2_INCLUDE_DIR
213    FLTK2_BASE_LIBRARY FLTK2_GL_LIBRARY
214    FLTK2_IMAGES_LIBRARY)
215  if(NOT ${var})
216    message( STATUS "${var} not found" )
217    set(FLTK2_FOUND 0)
218  endif()
219endforeach()
220
221
222if(FLTK2_FOUND)
223  set(FLTK2_LIBRARIES ${FLTK2_IMAGES_LIBRARY} ${FLTK2_IMAGES_LIBS} ${FLTK2_BASE_LIBRARY} ${FLTK2_GL_LIBRARY} )
224  if(APPLE)
225    set(FLTK2_LIBRARIES ${FLTK2_PLATFORM_DEPENDENT_LIBS} ${FLTK2_LIBRARIES})
226  else()
227    set(FLTK2_LIBRARIES ${FLTK2_LIBRARIES} ${FLTK2_PLATFORM_DEPENDENT_LIBS})
228  endif()
229
230  # The following deprecated settings are for compatibility with CMake 1.4
231  set (HAS_FLTK2 ${FLTK2_FOUND})
232  set (FLTK2_INCLUDE_PATH ${FLTK2_INCLUDE_DIR})
233  set (FLTK2_FLUID_EXE ${FLTK2_FLUID_EXECUTABLE})
234  set (FLTK2_LIBRARY ${FLTK2_LIBRARIES})
235else()
236  # make FIND_PACKAGE friendly
237  if(NOT FLTK2_FIND_QUIETLY)
238    if(FLTK2_FIND_REQUIRED)
239      message(FATAL_ERROR
240              "FLTK2 required, please specify its location with FLTK2_DIR.")
241    else()
242      message(STATUS "FLTK2 was not found.")
243    endif()
244  endif()
245endif()
246