File: FindIgnOGRE2.cmake

package info (click to toggle)
ignition-cmake 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,132 kB
  • sloc: xml: 35,671; python: 3,768; javascript: 2,308; sh: 172; ansic: 109; cpp: 64; makefile: 7
file content (446 lines) | stat: -rw-r--r-- 15,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#===============================================================================
# Copyright (C) 2018 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
########################################

#--------------------------------------
# Find OGRE2 headers and libraries
#
# Usage of this module as follows:
#
#     ign_find_package(IgnOGRE2)
#
# Variables defined by this module:
#
#  OGRE2_FOUND              System has OGRE libs/headers
#  OGRE2_LIBRARIES          The OGRE libraries
#  OGRE2_INCLUDE_DIRS       The location of OGRE headers
#  OGRE2_VERSION            Full OGRE version in the form of MAJOR.MINOR.PATCH
#  OGRE2_VERSION_MAJOR      OGRE major version
#  OGRE2_VERSION_MINOR      OGRE minor version
#  OGRE2_VERSION_PATCH      OGRE patch version
#  OGRE2_RESOURCE_PATH      Path to ogre plugins directory
#  IgnOGRE2::IgnOGRE2       Imported target for OGRE2
#
# On Windows, we assume that all the OGRE* defines are passed in manually
# to CMake.
#
# Supports finding the following OGRE2 components: HlmsPbs, HlmsUnlit, Overlay
#
# Example usage:
#
#     ign_find_package(IgnOGRE2
#                      VERSION 2.2.0
#                      COMPONENTS HlmsPbs HlmsUnlit Overlay)

# sanity check
if (${IgnOGRE2_FIND_VERSION_MAJOR})
  if (${IgnOGRE2_FIND_VERSION_MAJOR} VERSION_LESS "2")
    set (OGRE2_FOUND false)
    return()
  endif()
endif()

message(STATUS "-- Finding OGRE 2.${IgnOGRE2_FIND_VERSION_MINOR}")
set(OGRE2_INSTALL_PATH "OGRE-2.${IgnOGRE2_FIND_VERSION_MINOR}")

macro(append_library VAR LIB)
  if(EXISTS "${LIB}")
    list(APPEND ${VAR} ${LIB})
  endif()
endmacro()

# filter all ocurrences of LIBRARY_STR with the form of: debug;<path>;optimized;<path>
# based on CMAKE_BUILD_TYPE
macro(select_lib_by_build_type LIBRARY_STR OUTPUT_VAR)
  foreach(library ${LIBRARY_STR})
    if(library STREQUAL optimized)
      set(conf optimized)
    elseif(library STREQUAL debug)
      set(conf debug)
    else()
      if(conf STREQUAL optimized)
        append_library(LIB_RELEASE ${library})
        set(conf)
      elseif(conf STREQUAL debug)
        append_library(LIB_DEBUG ${library})
        set(conf)
      else()
        # assume library without debug/optimized prefix
        append_library(LIB_RELEASE ${library})
        append_library(LIB_DEBUG ${library})
      endif()
    endif()
  endforeach()

  if(LIB_DEBUG AND CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(${OUTPUT_VAR} "${LIB_DEBUG}")
  elseif(LIB_RELEASE)
    set(${OUTPUT_VAR} "${LIB_RELEASE}")
  endif()
endmacro()

# this macro and the version parsing logic below is taken from the
# FindOGRE.cmake file distributed by ogre
macro(get_preprocessor_entry CONTENTS KEYWORD VARIABLE)
  string(REGEX MATCH
    "# *define +${KEYWORD} +((\"([^\n]*)\")|([^ \n]*))"
    PREPROC_TEMP_VAR
    ${${CONTENTS}}
  )
  if (CMAKE_MATCH_3)
    set(${VARIABLE} ${CMAKE_MATCH_3})
  else ()
    set(${VARIABLE} ${CMAKE_MATCH_4})
  endif ()
endmacro()

if (NOT WIN32)
  set(PKG_CONFIG_PATH_ORIGINAL $ENV{PKG_CONFIG_PATH})

  # Note: OGRE2 installed from debs is named OGRE-2.2 while the version
  # installed from source does not have the 2.2 suffix
  # look for OGRE2 installed from debs
  ign_pkg_check_modules_quiet(OGRE2 ${OGRE2_INSTALL_PATH} NO_CMAKE_ENVIRONMENT_PATH QUIET)

  if (OGRE2_FOUND)
    set(IGN_PKG_NAME ${OGRE2_INSTALL_PATH})
  else()
    # look for OGRE2 installed from source
    set(PKG_CONFIG_PATH_TMP ${PKG_CONFIG_PATH_ORIGINAL})
    execute_process(COMMAND pkg-config --variable pc_path pkg-config
                    OUTPUT_VARIABLE _pkgconfig_invoke_result
                    RESULT_VARIABLE _pkgconfig_failed)
    if(_pkgconfig_failed)
      IGN_BUILD_WARNING ("Failed to get pkg-config search paths")
    elseif (NOT _pkgconfig_invoke_result STREQUAL "")
      set (PKG_CONFIG_PATH_TMP "${PKG_CONFIG_PATH_TMP}:${_pkgconfig_invoke_result}")
    endif()

    # check and see if there are any paths at all
    if ("${PKG_CONFIG_PATH_TMP}" STREQUAL "")
      message("No valid pkg-config search paths found")
      return()
    endif()

    string(REPLACE ":" ";" PKG_CONFIG_PATH_TMP ${PKG_CONFIG_PATH_TMP})

    # loop through pkg config paths and find an ogre version that is >= 2.0.0
    foreach(pkg_path ${PKG_CONFIG_PATH_TMP})
      set(ENV{PKG_CONFIG_PATH} ${pkg_path})
      pkg_check_modules(OGRE2 "OGRE" NO_CMAKE_ENVIRONMENT_PATH QUIET)
      if (OGRE2_FOUND)
        if (${OGRE2_VERSION} VERSION_LESS 2.0.0)
          set (OGRE2_FOUND false)
        else ()
          # pkg_check_modules does not provide complete path to libraries
          # So update variable to point to full path
          set(OGRE2_LIBRARY_NAME ${OGRE2_LIBRARIES})
          find_library(OGRE2_LIBRARY NAMES ${OGRE2_LIBRARY_NAME}
                                     HINTS ${OGRE2_LIBRARY_DIRS} NO_DEFAULT_PATH)
          if ("${OGRE2_LIBRARY}" STREQUAL "OGRE2_LIBRARY-NOTFOUND")
            set(OGRE2_FOUND false)
            continue()
          else()
            set(OGRE2_LIBRARIES ${OGRE2_LIBRARY})
          endif()
          set(IGN_PKG_NAME "OGRE")
          break()
        endif()
      endif()
    endforeach()
  endif()

  if (NOT OGRE2_FOUND)
    return()
  endif()

  # use pkg-config to find ogre plugin path
  # do it here before resetting the pkg-config paths
  execute_process(COMMAND pkg-config --variable=plugindir ${IGN_PKG_NAME}
                  OUTPUT_VARIABLE _pkgconfig_invoke_result
                  RESULT_VARIABLE _pkgconfig_failed)
  if(_pkgconfig_failed)
    IGN_BUILD_WARNING ("Failed to find OGRE's plugin directory. The build will succeed, but there will likely be run-time errors.")
  else()
    set(OGRE2_PLUGINDIR ${_pkgconfig_invoke_result})
  endif()

  # reset pkg config path
  set(ENV{PKG_CONFIG_PATH} ${PKG_CONFIG_PATH_ORIGINAL})

  # verify ogre header can be found in the include path
  find_path(OGRE2_INCLUDE
    NAMES Ogre.h
    PATHS ${OGRE2_INCLUDE_DIRS}
    NO_DEFAULT_PATH
  )

  if(NOT OGRE2_INCLUDE)
    set(OGRE2_FOUND false)
    return()
  endif()

  # manually search and append the the RenderSystem/GL3Plus path to
  # OGRE2_INCLUDE_DIRS so OGRE GL headers can be found
  foreach (dir ${OGRE2_INCLUDE_DIRS})
    get_filename_component(dir_name "${dir}" NAME)
    if ("${dir_name}" STREQUAL ${IGN_PKG_NAME})
      set(dir_include "${dir}/RenderSystems/GL3Plus")
    else()
      set(dir_include "${dir}")
    endif()
    list(APPEND OGRE2_INCLUDE_DIRS ${dir_include})
  endforeach()

  file(READ ${OGRE2_INCLUDE}/OgrePrerequisites.h OGRE_TEMP_VERSION_CONTENT)
  get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_MAJOR OGRE2_VERSION_MAJOR)
  get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_MINOR OGRE2_VERSION_MINOR)
  get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_PATCH OGRE2_VERSION_PATCH)
  get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_NAME OGRE2_VERSION_NAME)
  set(OGRE2_VERSION "${OGRE2_VERSION_MAJOR}.${OGRE2_VERSION_MINOR}.${OGRE2_VERSION_PATCH}")

  # find ogre components
  include(IgnImportTarget)
  foreach(component ${IgnOGRE2_FIND_COMPONENTS})
    find_library(OGRE2-${component}
      NAMES
        "Ogre${component}_d.${OGRE2_VERSION}"
        "Ogre${component}_d"
        "Ogre${component}.${OGRE2_VERSION}"
        "Ogre${component}"
      HINTS ${OGRE2_LIBRARY_DIRS})
    if (NOT "OGRE2-${component}" STREQUAL "OGRE2-${component}-NOTFOUND")

      # create a new target for each component
      set(component_TARGET_NAME "IgnOGRE2-${component}::IgnOGRE2-${component}")
      set(component_INCLUDE_DIRS ${OGRE2_INCLUDE_DIRS})

      # append the Hlms/Common include dir if it exists.
      string(FIND ${component} "Hlms" HLMS_POS)
      if(${HLMS_POS} GREATER -1)
        foreach (dir ${OGRE2_INCLUDE_DIRS})
          get_filename_component(dir_name "${dir}" NAME)
          if ("${dir_name}" STREQUAL ${IGN_PKG_NAME})
            set(dir_include "${dir}/Hlms/Common")
            if (EXISTS ${dir_include})
              list(APPEND component_INCLUDE_DIRS ${dir_include})
            endif()
          endif()
        endforeach()
      endif()

      set(component_LIBRARY_DIRS ${OGRE2_LIBRARY_DIRS})
      set(component_LIBRARIES ${OGRE2-${component}})
      ign_import_target(${component} TARGET_NAME ${component_TARGET_NAME}
          LIB_VAR component_LIBRARIES
          INCLUDE_VAR component_INCLUDE_DIRS)

      # add it to the list of ogre libraries
      list(APPEND OGRE2_LIBRARIES ${component_TARGET_NAME})

    elseif(IgnOGRE2_FIND_REQUIRED_${component})
      set(OGRE2_FOUND false)
    endif()
  endforeach()

  if ("${OGRE2_PLUGINDIR}" STREQUAL "")
    # set path to find ogre plugins
    # keep variable naming consistent with ogre 1
    # TODO currently using harded paths based on dir structure in ubuntu
    foreach(resource_path ${OGRE2_LIBRARY_DIRS})
      list(APPEND OGRE2_RESOURCE_PATH "${resource_path}/OGRE")
    endforeach()
  else()
    set(OGRE2_RESOURCE_PATH ${OGRE2_PLUGINDIR})
    # Seems that OGRE2_PLUGINDIR can end in a newline, which will cause problems
    # when we pass it to the compiler later.
    string(REPLACE "\n" "" OGRE2_RESOURCE_PATH ${OGRE2_RESOURCE_PATH})
  endif()

  # We need to manually specify the pkgconfig entry (and type of entry),
  # because ign_pkg_check_modules does not work for it.
  include(IgnPkgConfig)
  ign_pkg_config_library_entry(IgnOGRE2 OgreMain)

else() #WIN32

  set(OGRE2_FOUND TRUE)
  set(OGRE_LIBRARIES "")
  set(OGRE2_VERSION "")
  set(OGRE2_VERSION_MAJOR "")
  set(OGRE2_VERSION_MINOR "")
  set(OGRE2_RESOURCE_PATH "")

  set(OGRE2_SEARCH_VER "OGRE-${IgnOGRE2_FIND_VERSION_MAJOR}.${IgnOGRE2_FIND_VERSION_MINOR}")
  set(OGRE2_PATHS "")
  set(OGRE2_INC_PATHS "")
  foreach(_rootPath ${VCPKG_CMAKE_FIND_ROOT_PATH})
      list(APPEND OGRE2_PATHS "${_rootPath}/lib/${OGRE2_SEARCH_VER}/")
      list(APPEND OGRE2_PATHS "${_rootPath}/lib/${OGRE2_SEARCH_VER}/manual-link/")
      list(APPEND OGRE2_INC_PATHS "${_rootPath}/include/${OGRE2_SEARCH_VER}")
  endforeach()

  find_library(OGRE2_LIBRARY
    NAMES "OgreMain"
    HINTS ${OGRE2_PATHS}
    NO_DEFAULT_PATH)

  find_path(OGRE2_INCLUDE
    NAMES "Ogre.h"
    HINTS ${OGRE2_INC_PATHS})

  if("${OGRE2_LIBRARY}" STREQUAL "OGRE2_LIBRARY-NOTFOUND")
    set(OGRE2_FOUND false)
  else()
    set(OGRE2_LIBRARIES ${OGRE2_LIBRARY})
  endif()

  if(NOT OGRE2_INCLUDE)
    set(OGRE2_FOUND false)
  endif()

  if (OGRE2_FOUND)
    set(OGRE2_INCLUDE_DIRS ${OGRE2_INCLUDE})
    set(OGRE2_LIBRARY_DIRS ${OGRE2_PATHS})

    file(READ ${OGRE2_INCLUDE}/OgrePrerequisites.h OGRE_TEMP_VERSION_CONTENT)
    get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_MAJOR OGRE2_VERSION_MAJOR)
    get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_MINOR OGRE2_VERSION_MINOR)
    get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_PATCH OGRE2_VERSION_PATCH)
    get_preprocessor_entry(OGRE_TEMP_VERSION_CONTENT OGRE_VERSION_NAME OGRE2_VERSION_NAME)
    set(OGRE2_VERSION "${OGRE2_VERSION_MAJOR}.${OGRE2_VERSION_MINOR}.${OGRE2_VERSION_PATCH}")
    set(OGRE_TEMP_VERSION_CONTENT "")

    macro(ogre_find_component COMPONENT HEADER PATH_HINTS)
      set(PREFIX OGRE2_${COMPONENT})
      find_path(${PREFIX}_INCLUDE_DIR
          NAMES ${HEADER}
          HINTS ${OGRE2_INCLUDE_DIRS}
          PATH_SUFFIXES
              ${PATH_HINTS} ${COMPONENT} ${OGRE2_SEARCH_VER}/${COMPONENT})

      find_library(${PREFIX}_LIBRARY
          NAMES
              "Ogre${COMPONENT}"
              "Ogre${COMPONENT}_d"
          HINTS
              ${OGRE2_LIBRARY_DIRS}
          NO_DEFAULT_PATH)

      if (NOT ${PREFIX}_FOUND)
        if (${PREFIX}_INCLUDE_DIR AND ${PREFIX}_LIBRARY)
          set(${PREFIX}_FOUND TRUE)
          set(${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIR})
          set(${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARY})
          message(STATUS "Found ${PREFIX}: ${${PREFIX}_LIBRARIES}")
        endif()
      endif()
  endmacro()

  macro(ogre_find_plugin PLUGIN HEADER)
    set(PREFIX OGRE2_${PLUGIN})
    string(REPLACE "RenderSystem_" "" PLUGIN_TEMP ${PLUGIN})
    string(REPLACE "Plugin_" "" PLUGIN_NAME ${PLUGIN_TEMP})
      # header files for plugins are not usually needed, but find them anyway if they are present
    set(OGRE2_PLUGIN_PATH_SUFFIXES
      PlugIns
      PlugIns/${PLUGIN_NAME}
      Plugins
      Plugins/${PLUGIN_NAME}
      ${PLUGIN}
      RenderSystems
      RenderSystems/${PLUGIN_NAME}
      ${ARGN})
    find_path(
      ${PREFIX}_INCLUDE_DIR
      NAMES
        ${HEADER}
      HINTS
        ${OGRE2_INCLUDE_DIRS} ${OGRE_PREFIX_SOURCE}
      PATH_SUFFIXES
        ${OGRE2_PLUGIN_PATH_SUFFIXES})
    find_library(${PREFIX}_LIBRARY
      NAMES ${PLUGIN}
      HINTS  ${OGRE2_LIBRARY_DIRS}
      PATH_SUFFIXES "" opt "${OGRE2_SEARCH_VER}" "${OGRE2_SEARCH_VER}/opt")

    if (NOT ${PREFIX}_FOUND)
      if (${PREFIX}_INCLUDE_DIR AND ${PREFIX}_LIBRARY)
          set(${PREFIX}_FOUND TRUE)
          set(${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIR})
          set(${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARY})
          message(STATUS "Found ${PREFIX}: ${${PREFIX}_LIBRARIES}")
      endif()
    endif()
  endmacro()

  ogre_find_component(Overlay OgreOverlaySystem.h "Overlay")
  ogre_find_component(HlmsPbs OgreHlmsPbs.h Hlms/Pbs/)
  ogre_find_component(HlmsUnlit OgreHlmsUnlit.h Hlms/Unlit)

  ogre_find_plugin(Plugin_ParticleFX OgreParticleFXPrerequisites.h PlugIns/ParticleFX/include)
  ogre_find_plugin(RenderSystem_GL3Plus OgreGL3PlusRenderSystem.h RenderSystems/GL3Plus/include)
  ogre_find_plugin(RenderSystem_Direct3D11 OgreD3D11RenderSystem.h RenderSystems/Direct3D11/include)

  foreach(component ${IgnOGRE2_FIND_COMPONENTS})
    set(PREFIX OGRE2_${component})
    if(${PREFIX}_FOUND)
      set(component_TARGET_NAME "IgnOGRE2-${component}::IgnOGRE2-${component}")
      set(component_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS})
      # append the Hlms/Common include dir if it exists.
      string(FIND ${component} "Hlms" HLMS_POS)
      if(${HLMS_POS} GREATER -1)
        foreach (dir ${OGRE2_INCLUDE_DIRS})
          get_filename_component(dir_name "${dir}" NAME)
          if ("${dir_name}" STREQUAL "OGRE-${OGRE2_VERSION_MAJOR}.${OGRE2_VERSION_MINOR}")
            set(dir_include "${dir}/Hlms/Common")
            if (EXISTS ${dir_include})
              list(APPEND component_INCLUDE_DIRS ${dir_include})
            endif()
          endif()
        endforeach()
      endif()

      set(component_LIBRARIES ${${PREFIX}_LIBRARIES})

      ign_import_target(${component}
        TARGET_NAME ${component_TARGET_NAME}
        LIB_VAR component_LIBRARIES
        INCLUDE_VAR component_INCLUDE_DIRS
      )
      list(APPEND OGRE2_LIBRARIES ${component_TARGET_NAME})
    endif()
  endforeach()

  set(OGRE2_PLUGINS_VCPKG Plugin_ParticleFX RenderSystem_GL3Plus RenderSystem_Direct3D11)
  foreach(PLUGIN ${OGRE2_PLUGINS_VCPKG})
    if(OGRE2_${PLUGIN}_FOUND)
      list(APPEND OGRE2_INCLUDE_DIRS ${OGRE2_${PLUGIN}_INCLUDE_DIRS})
    endif()
  endforeach()
  endif()
endif()

set(IgnOGRE2_FOUND false)
# create OGRE2 target
if (OGRE2_FOUND)
  set(IgnOGRE2_FOUND true)

  ign_import_target(IgnOGRE2
    TARGET_NAME IgnOGRE2::IgnOGRE2
    LIB_VAR OGRE2_LIBRARIES
    INCLUDE_VAR OGRE2_INCLUDE_DIRS)
endif()