-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl3w_gen.cmake
executable file
·294 lines (232 loc) · 6.68 KB
/
gl3w_gen.cmake
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
#!/usr/bin/cmake -P
# Usage:
# - cmake script mode: cmake -P gl3w_gen.cmake or ./gl3w_gen.cmake
# - from a cmake project: include(gl3w_gen) then gl3w_gen(OUTPUT_PATH)
# Pavel Rojtberg 2016
function(gl3w_gen OUTDIR)
set(UNLICENSE
"/*
This file was generated with gl3w_gen.cmake, part of glXXw
(hosted at https://github.com/paroj/glXXw-cmake)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/\n
")
file(MAKE_DIRECTORY ${OUTDIR}/include/GL)
file(MAKE_DIRECTORY ${OUTDIR}/src)
if(NOT EXISTS ${OUTDIR}/include/GL/glcorearb.h)
message(STATUS "Downloading glcorearb.h to include/GL...")
file(DOWNLOAD
https://www.khronos.org/registry/OpenGL/api/GL/glcorearb.h
${OUTDIR}/include/GL/glcorearb.h)
else()
message(STATUS "Reusing glcorearb.h from include/GL...")
endif()
message(STATUS "Parsing glcorearb.h header...")
file(STRINGS ${OUTDIR}/include/GL/glcorearb.h GLCOREARB)
foreach(LINE ${GLCOREARB})
string(REGEX MATCH "GLAPI.*APIENTRY[ ]+([a-zA-Z0-9_]+)" MATCHES ${LINE})
if(MATCHES)
list(APPEND PROCS ${CMAKE_MATCH_1})
endif()
endforeach()
list(SORT PROCS)
set(SPACES " ") # 55 spaces
macro(getproctype PROC)
string(TOUPPER ${PROC} P_T)
set(P_T "PFN${P_T}PROC")
endmacro()
macro(getproctype_aligned PROC)
getproctype(${PROC})
string(LENGTH ${P_T} LEN)
math(EXPR LEN "55 - ${LEN}")
string(SUBSTRING ${SPACES} 0 ${LEN} PAD)
set(P_T "${P_T}${PAD}")
endmacro()
macro(getprocsignature PROC)
string(SUBSTRING ${PROC} 2 -1 P_S)
set(P_S "gl3w${P_S}")
endmacro()
message(STATUS "Generating gl3w.h in include/GL...")
set(HDR_OUT ${OUTDIR}/include/GL/gl3w.h)
file(WRITE ${HDR_OUT} ${UNLICENSE})
file(APPEND ${HDR_OUT}
"#ifndef __gl3w_h_
#define __gl3w_h_
#include <GL/glcorearb.h>
#ifndef __gl_h_
#define __gl_h_
#endif
#ifdef __cplusplus
extern \"C\" {
#endif
typedef void (*GL3WglProc)(void);
typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc);
/* gl3w api */
int gl3wInit(void);
int gl3wInit2(GL3WGetProcAddressProc proc);
int gl3wIsSupported(int major, int minor);
GL3WglProc gl3wGetProcAddress(const char *proc);
/* OpenGL functions */
")
foreach(PROC ${PROCS})
getprocsignature(${PROC})
getproctype_aligned(${PROC})
file(APPEND ${HDR_OUT} "extern ${P_T} ${P_S};\n")
endforeach()
foreach(PROC ${PROCS})
string(SUBSTRING ${PROC} 2 -1 P_S)
string(LENGTH ${PROC} LEN)
math(EXPR LEN "48 - ${LEN}")
string(SUBSTRING ${SPACES} 0 ${LEN} PAD)
file(APPEND ${HDR_OUT} "#define ${PROC}${PAD} gl3w${P_S}\n")
endforeach()
file(APPEND ${HDR_OUT}
"
#ifdef __cplusplus
}
#endif
#endif
")
message(STATUS "Generating gl3w.c in src...")
set(SRC_OUT ${OUTDIR}/src/gl3w.c)
file(WRITE ${SRC_OUT} ${UNLICENSE})
file(APPEND ${SRC_OUT} "#include <GL/gl3w.h>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
static HMODULE libgl;
static PROC (__stdcall *wgl_get_proc_address)(LPCSTR);
static void open_libgl(void)
{
libgl = LoadLibraryA(\"opengl32.dll\");
*(void **)(&wgl_get_proc_address) = GetProcAddress(libgl, \"wglGetProcAddress\");
}
static void close_libgl(void)
{
FreeLibrary(libgl);
}
static GL3WglProc get_proc(const char *proc)
{
GL3WglProc res;
res = (GL3WglProc)wgl_get_proc_address(proc);
if (!res)
res = (GL3WglProc)GetProcAddress(libgl, proc);
return res;
}
#elif defined(__APPLE__) || defined(__APPLE_CC__)
#include <dlfcn.h>
static void *libgl;
static void open_libgl(void)
{
libgl = dlopen(\"/System/Library/Frameworks/OpenGL.framework/OpenGL\", RTLD_LAZY | RTLD_LOCAL);
}
static void close_libgl(void)
{
dlclose(libgl);
}
static GL3WglProc get_proc(const char *proc)
{
GL3WglProc res;
*(void **)(&res) = dlsym(libgl, proc);
return res;
}
#else
#include <dlfcn.h>
typedef GL3WglProc (*PFNGLXGETPROCADDRESSPROC)(const GLubyte *);
static void *libgl;
static PFNGLXGETPROCADDRESSPROC glx_get_proc_address;
static void open_libgl(void)
{
libgl = dlopen(\"libGL.so.1\", RTLD_LAZY | RTLD_LOCAL);
*(void **)(&glx_get_proc_address) = dlsym(libgl, \"glXGetProcAddressARB\");
}
static void close_libgl(void)
{
dlclose(libgl);
}
static GL3WglProc get_proc(const char *proc)
{
GL3WglProc res;
res = glx_get_proc_address((const GLubyte *)proc);
if (!res)
*(void **)(&res) = dlsym(libgl, proc);
return res;
}
#endif
static struct {
int major, minor;
} version;
static int parse_version(void)
{
if (!glGetIntegerv)
return -1;
glGetIntegerv(GL_MAJOR_VERSION, &version.major);
glGetIntegerv(GL_MINOR_VERSION, &version.minor);
if (version.major < 3)
return -1;
return 0;
}
static void load_procs(GL3WGetProcAddressProc proc);
int gl3wInit(void)
{
open_libgl();
load_procs(get_proc);
close_libgl();
return parse_version();
}
int gl3wInit2(GL3WGetProcAddressProc proc)
{
load_procs(proc);
return parse_version();
}
int gl3wIsSupported(int major, int minor)
{
if (major < 3)
return 0;
if (version.major == major)
return version.minor >= minor;
return version.major >= major;
}
GL3WglProc gl3wGetProcAddress(const char *proc)
{
return get_proc(proc);
}
")
foreach(PROC ${PROCS})
getprocsignature(${PROC})
getproctype_aligned(${PROC})
file(APPEND ${SRC_OUT} "${P_T} ${P_S};\n")
endforeach()
file(APPEND ${SRC_OUT} "
static void load_procs(GL3WGetProcAddressProc proc)
{\n")
foreach(PROC ${PROCS})
getprocsignature(${PROC})
getproctype(${PROC})
file(APPEND ${SRC_OUT} "\t${P_S} = (${P_T})proc(\"${PROC}\");\n")
endforeach()
file(APPEND ${SRC_OUT} "}\n")
endfunction()
if(NOT CMAKE_PROJECT_NAME)
gl3w_gen(".")
endif()