Skip to content

Instantly share code, notes, and snippets.

@emctague
Last active June 12, 2019 18:37
Show Gist options
  • Save emctague/115cd0102da6b042bb300bce79117579 to your computer and use it in GitHub Desktop.
Save emctague/115cd0102da6b042bb300bce79117579 to your computer and use it in GitHub Desktop.
Better FindGLFW.cmake
# Distributed under the OSI-approved BSD 3-Clause License.
# See https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindGLFW
-------
Finds GLFW3.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``GLFW::GLFW``
GLFW itself
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``GLFW_FOUND``
True if the system has GLFW.
``GLFW_VERSION``
The located version of GLFW, formatted as ``<MAJOR>.<MINOR>.<PATCH>``.
``GLFW_VERSION_MAJOR``
The major version of the located copy of GLFW3.
``GLFW_VERSION_MINOR``
The minor version of the located copy of GLFW3.
``GLFW_VERSION_PATCH``
The patch version of the located copy of GLFW3.
``GLFW_INCLUDE_DIRS``
Include directories needed to use GLFW3.
``GLFW_LIBRARIES``
Libraries needed to link to GLFW3.
``GLFW_DEFINITIONS``
Any other compiler definitions that ``pkg-config`` recommends, if necessary.
Cache Variables
^^^^^^^^^^^^^^^
The following cache variables may also be set:
``GLFW_INCLUDE_DIR``
The directory containing ``GLFW/glfw3.h``.
``GLFW_LIBRARY``
The path to the GLFW library.
#]=======================================================================]
find_package(PkgConfig)
pkg_check_modules(PC_GLFW QUIET glfw3)
find_path(GLFW_PRECOMP_DIR
NAMES
GLFW/glfw3.h
PATHS
${PROJECT_SOURCE_DIR}/
${PROJECT_SOURCE_DIR}/../
${GLFW_LOCATION}
$ENV{GLFW_LOCATION}
$ENV{PROGRAMFILES}/
$ENV{PROGRAMW6432}/
C:/glfw
${HOME}/glfw
/opt/glfw
PATH_SUFFIXES
include shared_external/glfw/include shared_external/GLFW/include shared_external/GLFW/include external/glfw/include GLFW/include glfw/include external/GLFW/include
DOC "GLFW precompiled binary path (not required)")
find_path(GLFW_INCLUDE_DIR
NAMES
GLFW/glfw3.h
PATHS
${GLFW_LOCATION}/include
$ENV{GLFW_LOCATION}/include
/usr/include
/usr/X11R6/include
/usr/include/X11
/usr/local/include
/sw/include
/opt/local/include
/opt/glfw/include
/opt/glfw3/include
/opt/include
/opt/graphics/OpenGL/include
/opt/graphics/OpenGL/contrib/libglfw
/usr/include/GL
/usr/openwin/share/include
/usr/openwin/include
${PROJECT_SOURCE_DIR}/external/glfw/include
${PROJECT_SOURCE_DIR}/../external/glfw/include
${PROJECT_SOURCE_DIR}/shared_external/glfw/include
${PROJECT_SOURCE_DIR}/../shared_external/glfw/include
${GLFW_PRECOMP_DIR}/include
${PC_GLFW_INCLUDE_DIRS}
NO_DEFAULT_PATH
DOC "The directory where GLFW/glfw3.h resides")
find_library(GLFW_LIBRARY
NAMES
glfw3 glfw
PATHS
${GLFW_PRECOMP_DIR}/lib-mingw-w64
${GLFW_PRECOMP_DIR}/lib-vc2012
${GLFW_PRECOMP_DIR}/lib-vc2013
${GLFW_PRECOMP_DIR}/lib-vc2015
${GLFW_PRECOMP_DIR}/lib
${GLFW_LOCATION}/lib
${GLFW_LOCATION}/lib/cocoa
$ENV{GLFW_LOCATION}/lib
$ENV{GLFW_LOCATION}/lib/cocoa
/usr/lib64
/usr/lib
/usr/local/lib64
/usr/X11R6/lib
/usr/local/lib/cocoa
/usr/local/lib
/sw/lib
/opt/local/lib
/opt/local/lib/cocoa
/opt/glfw/lib
/opt/glfw/lib/cocoa
/usr/lib/w32api
/opt/glfw3/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/i386-linux-gnu
${PC_GLFW_LIBRARY_DIRS}
NO_DEFAULT_PATH
DOC "GLFW library file")
if(GLFW_INCLUDE_DIR)
set(PATTERN "^#define GLFW_VERSION_.*$")
file(STRINGS "${GLFW_INCLUDE_DIR}/GLFW/glfw3.h" MATCH REGEX ${PATTERN})
string(REGEX MATCH "MAJOR[ \t\r]+([0-9+])" TMP "${MATCH}")
set(GLFW_VERSION_MAJOR ${CMAKE_MATCH_1})
string(REGEX MATCH "MINOR[ \t\r]+([0-9]+)" TMP "${MATCH}")
set(GLFW_VERSION_MINOR ${CMAKE_MATCH_1})
string(REGEX MATCH "REVISION[ \t\r]+([0-9]+)" TMP "${MATCH}")
set(GLFW_VERSION_PATCH ${CMAKE_MATCH_1})
unset(TMP)
set(GLFW_VERSION "${GLFW_VERSION_MAJOR}.${GLFW_VERSION_MINOR}.${GLFW_VERSION_PATCH}")
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW
FOUND_VAR GLFW_FOUND
REQUIRED_VARS
GLFW_LIBRARY
GLFW_INCLUDE_DIR
VERSION_VAR GLFW_VERSION
)
if(GLFW_FOUND)
# Important exported variables
set(GLFW_LIBRARIES ${GLFW_LIBRARY})
set(GLFW_INCLUDE_DIRS ${GLFW_INCLUDE_DIR})
set(GLFW_DEFINITIONS ${PC_GLFW_CFLAGS_OTHER})
# Alternative names
set(GLFW_VERSION_STRING ${GLFW_VERSION})
# Exported target
if(NOT TARGET GLFW::GLFW)
add_library(GLFW::GLFW UNKNOWN IMPORTED)
set_target_properties(GLFW::GLFW PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${GLFW_INCLUDE_DIR}"
IMPORTED_LOCATION "${GLFW_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_GLFW_CFLAGS_OTHER}"
)
endif()
endif()
# Hide internal variables
mark_as_advanced(GLFW_INCLUDE_DIR)
mark_as_advanced(GLFW_LIBRARY)
mark_as_advanced(GLFW_PRECOMP_DIR)
# Hide legacy/alternative names
mark_as_advanced(GLFW_VERSION_STRING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment