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
|
# This project defines a `glad_add_library` function that will create a glad library.
# The created library will automatically generate the glad sources.
# Consumers can link to the the library.
#
# configuration variables:
# GLAD_SOURCES_DIR: path to the sources of glad (=python module)
#
#
# glad_add_library(<TARGET> [SHARED|STATIC|MODULE|INTERFACE] [EXCLUDE_FROM_ALL] [MERGE] [QUIET] [LOCATION <PATH>]
# [LANGUAGE <LANG>] [API <API1> [<API2> ...]] [EXTENSIONS [<EXT1> [<EXT2> ...]]])
# - <TARGET>
# Name of the TARGET
# - SHARED|STATIC|MODULE|INTERFACE
# Type of the library, if none is specified, default BUILD_SHARED_LIBS behavior is honored
# - EXCLUDE_FROM_ALL
# Exclude building the library from the all target
# - MERGE
# Merge multiple APIs of the same specitifation into one file.
# - QUIET
# Disable logging
# - LOCATION <PATH>
# Set the location where the generated glad should be saved.
# - LANGUAGE <LANG>
# Language of the generated glad sources.
# - API <API1> [<API2> ...]]
# Apis to include in the generated glad library.
# - EXTENSIONS [<EXT1> [<EXT2> ...]]
# Extensions to include in the generated glad library. Pass NONE to add no extensions whatsoever.
#
# examples:
# - create a shared glad library of the core profile of opengl 3.3, having all extensions:
# ```
# glad_add_library(glad_gl_core_33 SHARED API gl:core=3.3)
# ```
# - create a module glad library of the compatibility profile of opengl 1.0, having only the GL_EXT_COMPRESSION_s3tc extensionsion
# ```
# glad_add_library(glad_gl_compat_10 MODULE API gl:compatibility=1.0 EXTENSIONS GL_EXT_COMPRESSION_s3tc)
# ```
# - create a static glad library with the vulkan=1.1
cmake_minimum_required(VERSION 3.2)
project(glad C)
find_package(PythonInterp REQUIRED)
set(GLAD_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE STRING "Directory containing glad generator CMakeLists.txt")
set(GLAD_SOURCES_DIR "${GLAD_CMAKE_DIR}/../" CACHE STRING "Directory containing glad sources (python modules), used as working directory")
mark_as_advanced(GLAD_CMAKE_DIR)
# Extract specification, profile and version from a string
# examples:
# gl:core=3.3 => SPEC=gl PROFILE=core VERSION=3.3
# gl:compatibility=4.0 => SPEC=gl PROFILE=compatibility VERSION=4.0
# vulkan=1.1 => SPEC=vulkan PROFILE="" VERSION=1.1
function(__glad_extract_spec_profile_version SPEC PROFILE VERSION STRING)
string(REPLACE "=" ";" SPEC_PROFILE_VERSION_LIST "${STRING}")
list(LENGTH SPEC_PROFILE_VERSION_LIST SPV_LENGTH)
if(SPV_LENGTH LESS 2)
message(FATAL_ERROR "${SPEC} is an invalid SPEC")
endif()
list(GET SPEC_PROFILE_VERSION_LIST 0 SPEC_PROFILE_STR)
list(GET SPEC_PROFILE_VERSION_LIST 1 VERSION_STR)
string(REPLACE ":" ";" SPEC_PROFILE_LIST "${SPEC_PROFILE_STR}")
list(LENGTH SPEC_PROFILE_LIST SP_LENGTH)
if(SP_LENGTH LESS 2)
list(GET SPEC_PROFILE_LIST 0 SPEC_STR)
set(PROFILE_STR "")
else()
list(GET SPEC_PROFILE_LIST 0 SPEC_STR)
list(GET SPEC_PROFILE_LIST 1 PROFILE_STR)
endif()
set("${SPEC}" "${SPEC_STR}" PARENT_SCOPE)
set("${PROFILE}" "${PROFILE_STR}" PARENT_SCOPE)
set("${VERSION}" "${VERSION_STR}" PARENT_SCOPE)
endfunction()
# Calculate the argument and generated files for the "c" subparser for glad
function(__glad_c_library CARGS CFILES)
cmake_parse_arguments(GGC "ALIAS;DEBUG;HEADERONLY;LOADER;MX;MXGLOBAL;ON_DEMAND" "" "API" ${ARGN})
if(NOT GGC_API)
message(FATAL_ERROR "Need API")
endif()
set(GGC_FILES "")
foreach(API ${GGC_API})
__glad_extract_spec_profile_version(SPEC PROFILE VERSION "${API}")
if(SPEC STREQUAL "egl")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/EGL/eglplatform.h"
"${GLAD_DIR}/include/KHR/khrplatform.h"
"${GLAD_DIR}/include/glad/egl.h"
"${GLAD_DIR}/src/egl.c"
)
elseif(SPEC STREQUAL "vulkan")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/vk_platform.h"
"${GLAD_DIR}/include/glad/vulkan.h"
"${GLAD_DIR}/src/vulkan.c"
)
elseif(SPEC STREQUAL "gl")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/KHR/khrplatform.h"
"${GLAD_DIR}/include/glad/gl.h"
"${GLAD_DIR}/src/gl.c"
)
elseif(SPEC STREQUAL "gles1")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/KHR/khrplatform.h"
"${GLAD_DIR}/include/glad/gles1.h"
"${GLAD_DIR}/src/gles1.c"
)
elseif(SPEC STREQUAL "gles2")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/KHR/khrplatform.h"
"${GLAD_DIR}/include/glad/gles2.h"
"${GLAD_DIR}/src/gles2.c"
)
elseif(SPEC STREQUAL "glsc2")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/KHR/khrplatform.h"
"${GLAD_DIR}/include/glad/glsc2.h"
"${GLAD_DIR}/src/glsc2.c"
)
elseif(SPEC STREQUAL "wgl")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/glad/wgl.h"
"${GLAD_DIR}/src/wgl.c"
)
elseif(SPEC STREQUAL "glx")
list(APPEND GGC_FILES
"${GLAD_DIR}/include/glad/glx.h"
"${GLAD_DIR}/src/glx.c"
)
else()
message(FATAL_ERROR "Unknown SPEC: '${SPEC}'")
endif()
endforeach()
set(GGC_ARGS "")
if(GGC_ALIAS)
list(APPEND GGC_ARGS "--alias")
endif()
if(GGC_DEBUG)
list(APPEND GGC_ARGS "--debug")
endif()
if(GGC_HEADERONLY)
list(APPEND GGC_ARGS "--header-only")
endif()
if(GGC_LOADER)
list(APPEND GGC_ARGS "--loader")
endif()
if(GGC_MX)
list(APPEND GGC_ARGS "--mx")
endif()
if(GGC_MXGLOBAL)
list(APPEND GGC_ARGS "--mx-global")
endif()
if(GGC_ON_DEMAND)
list(APPEND GGC_ARGS "--on-demand")
endif()
set("${CARGS}" "${GGC_ARGS}" PARENT_SCOPE)
set("${CFILES}" "${GGC_FILES}" PARENT_SCOPE)
endfunction()
# Create a glad library named "${TARGET}"
function(glad_add_library TARGET)
message(STATUS "Glad Library \'${TARGET}\'")
cmake_parse_arguments(GG "MERGE;QUIET;REPRODUCIBLE;STATIC;SHARED;MODULE;INTERFACE;EXCLUDE_FROM_ALL" "LOCATION;LANGUAGE" "API;EXTENSIONS" ${ARGN})
if(NOT GG_LOCATION)
set(GG_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/gladsources/${TARGET}")
endif()
set(GLAD_DIR "${GG_LOCATION}")
if(NOT IS_DIRECTORY "${GLAD_DIR}")
file(MAKE_DIRECTORY "${GLAD_DIRECTORY}")
endif()
set(GLAD_ARGS --out-path "${GLAD_DIR}")
if(NOT GG_API)
message(FATAL_ERROR "Need API")
endif()
string(REPLACE ";" "," GLAD_API "${GG_API}")
list(APPEND GLAD_ARGS --api "${GLAD_API}")
if(GG_EXTENSIONS)
list(FIND GG_EXTENSIONS NONE GG_EXT_NONE)
if(GG_EXT_NONE GREATER -1)
set(GLAD_EXTENSIONS " ")
else()
list(REMOVE_DUPLICATES GG_EXTENSIONS)
list(JOIN GG_EXTENSIONS "," GLAD_EXTENSIONS)
endif()
list(APPEND GLAD_ARGS --extensions "${GLAD_EXTENSIONS}")
endif()
if(GG_QUIET)
list(APPEND GLAD_ARGS --quiet)
endif()
if(GG_MERGE)
list(APPEND GLAD_ARGS --merge)
endif()
if(GG_REPRODUCIBLE)
list(APPEND GLAD_ARGS --reproducible)
endif()
set(GLAD_LANGUAGE "c")
if(GG_LANGUAGE)
string(TOLOWER "${GG_LANGUAGE}" "${GLAD_LANGUAGE}")
endif()
if(GLAD_LANGUAGE STREQUAL "c")
__glad_c_library(LANG_ARGS GLAD_FILES ${GG_UNPARSED_ARGUMENTS} API ${GG_API})
else()
message(FATAL_ERROR "Unknown LANGUAGE")
endif()
list(APPEND GLAD_ARGS ${GLAD_LANGUAGE} ${LANG_ARGS})
string(REPLACE "${GLAD_DIR}" GLAD_DIRECTORY GLAD_ARGS_UNIVERSAL "${GLAD_ARGS}")
set(GLAD_ARGS_PATH "${GLAD_DIR}/args.txt")
# add make custom target
add_custom_command(
OUTPUT ${GLAD_FILES} ${GLAD_ARGS_PATH}
COMMAND echo Cleaning ${GLAD_DIR}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${GLAD_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory ${GLAD_DIR}
COMMAND echo Generating with args ${GLAD_ARGS}
COMMAND ${PYTHON_EXECUTABLE} -m glad ${GLAD_ARGS}
COMMAND echo Writing ${GLAD_ARGS_PATH}
COMMAND echo ${GLAD_ARGS} > ${GLAD_ARGS_PATH}
WORKING_DIRECTORY ${GLAD_SOURCES_DIR}
COMMENT "${TARGET}-generate"
USES_TERMINAL
)
set(GLAD_ADD_LIBRARY_ARGS "")
if(GG_SHARED)
list(APPEND GLAD_ADD_LIBRARY_ARGS SHARED)
elseif(GG_STATIC)
list(APPEND GLAD_ADD_LIBRARY_ARGS STATIC)
elseif(GG_MODULE)
list(APPEND GLAD_ADD_LIBRARY_ARGS MODULE)
elseif(GG_INTERFACE)
list(APPEND GLAD_ADD_LIBRARY_ARGS INTERFACE)
endif()
if(GG_EXCLUDE_FROM_ALL)
list(APPEND GLAD_ADD_LIBRARY_ARGS EXCLUDE_FROM_ALL)
endif()
add_library("${TARGET}" ${GLAD_ADD_LIBRARY_ARGS}
${GLAD_FILES}
)
target_include_directories("${TARGET}"
PUBLIC
"${GLAD_DIR}/include"
)
target_link_libraries("${TARGET}"
PUBLIC
${CMAKE_DL_LIBS}
)
if(GG_SHARED)
target_compile_definitions("${TARGET}" PUBLIC GLAD_API_CALL_EXPORT)
set_target_properties("${TARGET}"
PROPERTIES
DEFINE_SYMBOL "GLAD_API_CALL_EXPORT_BUILD"
)
endif()
endfunction()
|