FindGLFW3.cmake 4.21 KB
Newer Older
Bastian Krayer's avatar
Bastian Krayer committed
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
#
# Try to find GLFW3 library and include path.
# Once done this will define
#
# GLFW3_FOUND
# GLFW3_INCLUDE_PATH
# GLFW3_LIBRARIES
#

IF(DEFINED ENV{GLFW3_ROOT})
	file(TO_CMAKE_PATH $ENV{GLFW3_ROOT} GLFW3_ROOT_ENV)
ENDIF()

# For older cmake versions use root variable as additional search directory explicitly
IF(${CMAKE_VERSION} VERSION_LESS_EQUAL "3.12.0") 
	SET(GLFW_ADDITIONAL_SEARCH_DIRS ${GLFW3_ROOT_ENV})
ENDIF()


if (WIN32)

	IF (MINGW)

		SET(GLFW_SEARCH_PATH_SUFFIX mingw)
		SET(GLFW_LIB_NAME libglfw3)
   
	ELSEIF (MSVC)
	
		# MSVC toolset suffix
		# Checks for version
		IF(MSVC_TOOLSET_VERSION EQUAL 140 OR MSVC_VERSION EQUAL 1900)
		
			SET(GLFW_SEARCH_PATH_SUFFIX "msvc140")
		ELSEIF(MSVC_TOOLSET_VERSION EQUAL 141 OR 
		(MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS_EQUAL 1919) )
			SET(GLFW_SEARCH_PATH_SUFFIX "msvc141")
		ELSE()
			SET(GLFW_SEARCH_PATH_SUFFIX "msvc141") # good luck
		ENDIF()

		SET(GLFW_LIB_NAME glfw3)
		
		

	ENDIF()
	
	find_library(GLFW3_LIBRARY_RELEASE 
		NAMES ${GLFW_LIB_NAME}
		PATHS
		${GLFW_ADDITIONAL_SEARCH_DIRS}
		PATH_SUFFIXES 
		${GLFW_SEARCH_PATH_SUFFIX}
		${GLFW_SEARCH_PATH_SUFFIX}/lib
	)
	find_library(GLFW3_LIBRARY_DEBUG 
		NAMES "${GLFW_LIB_NAME}d"
		PATHS
		${GLFW_ADDITIONAL_SEARCH_DIRS}
		PATH_SUFFIXES 
		${GLFW_SEARCH_PATH_SUFFIX}
		${GLFW_SEARCH_PATH_SUFFIX}/lib
	) 

	FIND_PATH(GLFW3_INCLUDE_DIR GLFW/glfw3.h
		PATHS
		${GLFW_ADDITIONAL_SEARCH_DIRS}
		PATH_SUFFIXES ${GLFW_SEARCH_PATH_SUFFIX}/include
    )
	
	include(SelectLibraryConfigurations)
	select_library_configurations(GLFW3)


	if(NOT GLFW3_LIBRARY_RELEASE)
        set(GLFW3_LIBRARY_RELEASE "GLFW3_LIBRARY_RELEASE-NOTFOUND" CACHE FILEPATH "Path to a library.")
    endif()
    if(NOT GLFW3_LIBRARY_DEBUG)
        set(GLFW3_LIBRARY_DEBUG "GLFW3_LIBRARY_DEBUG-NOTFOUND" CACHE FILEPATH "Path to a library.")
    endif()
	
    get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
    if( GLFW3_LIBRARY_DEBUG AND GLFW3_LIBRARY_RELEASE AND
           NOT GLFW3_LIBRARY_DEBUG STREQUAL GLFW3_LIBRARY_RELEASE AND
           ( _isMultiConfig OR CMAKE_BUILD_TYPE ) )
        # if the generator is multi-config or if CMAKE_BUILD_TYPE is set for
        # single-config generators, set optimized and debug libraries
        set( GLFW3_INTERFACE_LIBRARY "" )
        foreach( _libname IN LISTS GLFW3_LIBRARY_RELEASE )
           list( APPEND GLFW3_INTERFACE_LIBRARY $<$<CONFIG:RelWithDebInfo>:${_libname}>  )
           list( APPEND GLFW3_INTERFACE_LIBRARY $<$<CONFIG:Release>:${_libname}>  )
           list( APPEND GLFW3_INTERFACE_LIBRARY $<$<CONFIG:MinSizeRel>:${_libname}>  )
            #list( APPEND GLFW3_LIBRARY ${_libname} )
        endforeach()
        foreach( _libname IN LISTS GLFW3_LIBRARY_DEBUG )
            list( APPEND GLFW3_INTERFACE_LIBRARY $<$<CONFIG:Debug>:${_libname}> )
        endforeach()
    elseif( GLFW3_LIBRARY_RELEASE )
        set( GLFW3_INTERFACE_LIBRARY ${GLFW3_LIBRARY_RELEASE} )
    elseif( GLFW3_LIBRARY_DEBUG )
        set( GLFW3_INTERFACE_LIBRARY ${GLFW3_LIBRARY_DEBUG} )
    else()
        set( GLFW3_INTERFACE_LIBRARY "GLFW3_LIBRARY-NOTFOUND")
    endif()
	
	
	
ELSEIF(APPLE)

    FIND_PATH(GLFW3_INCLUDE_DIR GLFW/glfw3.h DOC "Path to GLFW include directory."
	HINTS ${GLFW3_ROOT_ENV}/include
	PATHS /usr/include /usr/local/include /opt/local/include
    )
    
    FIND_LIBRARY( GLFW3_LIBRARY
        NAMES libglfw3.a glfw libglfw3.dylib
        PATHS $ENV{GLFW3_ROOT_ENV}/build/src /usr/lib /usr/local/lib /opt/local/lib
    )

ELSE()
	FIND_PATH(GLFW3_INCLUDE_DIR GLFW/glfw3.h
		PATHS
		${GLFW_ADDITIONAL_SEARCH_DIRS})
	FIND_LIBRARY(GLFW3_LIBRARY
	PATHS
		${GLFW_ADDITIONAL_SEARCH_DIRS}
        NAMES glfw3 glfw
	PATH_SUFFIXES dynamic) 
ENDIF ()



include(FindPackageHandleStandardArgs)
	find_package_handle_standard_args(GLFW3
                                  REQUIRED_VARS GLFW3_INCLUDE_DIR GLFW3_LIBRARY)
set(GLFW3_INCLUDE_DIRS ${GLFW3_INCLUDE_DIR})


if(GLFW3_FOUND AND NOT TARGET GLFW3::glfw3)
    add_library(GLFW3::glfw3 INTERFACE IMPORTED)
	
	IF(NOT GLFW3_INTERFACE_LIBRARY)
		SET(GLFW3_INTERFACE_LIBRARY ${GLFW3_LIBRARIES})
	ENDIF()
    set_target_properties(GLFW3::glfw3 PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${GLFW3_INCLUDE_DIR}"
		INTERFACE_LINK_LIBRARIES "${GLFW3_INTERFACE_LIBRARY}"
    )
endif()