-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_sage_wgl.pl
executable file
·162 lines (139 loc) · 5.48 KB
/
build_sage_wgl.pl
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
#! /usr/bin/perl -w
# This file generates three source files required to build sage
# This first part of this file extracts the extension names, the #defines
# and the function names. This process also creates a cutsom glext.h file
# for use in sage The remaining parts of the file write the header and
# code file.
# Filenames
$WGLEXT_FILE="wglext.h";
$WGLEXT_SAGE_FILE="sage/wglext_sage.h";
$SAGE_HEADER_FILE="sage/sage_wgl.h";
$SAGE_CODE_FILE="sage/sage_wgl.c";
$LICENSE_FILE="templates/license";
#regular expressions
$FUNCTION_REGEXP="[A-Za-z0-9 ]* WINAPI ([a-zA-Z0-9]*) .*";
$DEFINE_REGEXP="\#define (WGL_[A-Za-z0-9_]*) 1\$";
# Open glext.h for input
open (WGLEXT, "<$WGLEXT_FILE") or die "Cannot open $WGLEXT_FILE\n";
# Open our glext.h for output
open (WGLEXT_SAGE, ">$WGLEXT_SAGE_FILE") or die "Cannot open $WGLEXT_SAGE_FILE\n";
#flag checking whether we need to add an endif or not
$WAIT_FOR_ENDIF="false";
#loop through every line in glext.h
while (<WGLEXT>) {
print WGLEXT_SAGE $_; # print line back into out glext
# Grab function name
if ($_ =~ m|$FUNCTION_REGEXP|) {
($FUNCTION) = ($_ =~ m|$FUNCTION_REGEXP|); # Extract function name
$PFN_FUNCTION = "PFN".uc($FUNCTION)."PROC"; # make the typedef name
push(@FUNCTION_HEADER , "SAGEAPI $PFN_FUNCTION $FUNCTION;\n"); # declare function for header
push (@FUNCTION_CODE, "$PFN_FUNCTION $FUNCTION = NULL;\n"); # declaraion of function in c file
push (@FUNCTION_INIT," $FUNCTION = ($PFN_FUNCTION)SDL_GL_GetProcAddress(\"$FUNCTION\");\n"); # linkup function ptr
# Grab #defines
} elsif ($_ =~ m|$DEFINE_REGEXP|) {
($DEF) = ($_ =~ m|$DEFINE_REGEXP|); # extract #define name
push(@FUNCTION_HEADER, $_); # write into header
# the SAGE_$DEF is a check put into glext_sage.h to say that glext.h defined the function, so we need
# to link it up. If its defined elsewhere then SAGE_$DEF will not be defined and we can ignore it
push (@FUNCTION_HEADER, "#ifdef SAGE_$DEF\n");
push (@FUNCTION_CODE, "#ifdef SAGE_$DEF\n");
push (@FUNCTION_INIT, "#ifdef SAGE_$DEF\n");
if ($DEF =~ m|GL_VERSION|) { # ignore GL_VERSION for BOOLS def
} else {
push (@BOOLS, $DEF);
}
print WGLEXT_SAGE "#define SAGE_$DEF 1\n";
$WAIT_FOR_ENDIF="true"; # we now need to write a corresponding endif when its next found
# grab endif if required
} elsif ($_ =~ m|\#endif|) { # write the endif now we have it
if ($WAIT_FOR_ENDIF eq "true") {
push(@FUNCTION_HEADER, "#endif\n");
push(@FUNCTION_CODE, "#endif\n");
push(@FUNCTION_INIT, "#endif\n");
$WAIT_FOR_ENDIF="false";
}
}
}
#copy into two arrays - we use this twice (prob a better way to do this)
for (@BOOLS) {
push(@BOOLS_DEF, $_);
push(@BOOLS_ENUM, $_);
}
#Write header file
open (SAGE_HEADER, ">$SAGE_HEADER_FILE") or die "Can't open: $SAGE_HEADER_FILE\n";
print "Writing $SAGE_HEADER_FILE\n";
#Write autogen comment
print SAGE_HEADER "/* This file is autogenerated by sage_build_wgl.pl */\n";
#Write license header
open (LICENSE, "<$LICENSE_FILE") or die "Can't open: $LICENSE_FILE\n";
while(<LICENSE>) { print SAGE_HEADER $_; }
close LICENSE;
print SAGE_HEADER "\n";
# Write header check
print SAGE_HEADER "#ifndef SAGE_WGL_H\n";
print SAGE_HEADER "#define SAGE_WGL_H 1\n";
print SAGE_HEADER "\n";
# Write defines for use in extensions array
$INDEX = 0; # Set counter to zero
for (@BOOLS_ENUM) {
print SAGE_HEADER "#define ".uc($_)." $INDEX\n"; # write a #define
$INDEX = $INDEX + 1; # increment counter
}
# This allows use to determine the size of the extensions array
print SAGE_HEADER "#define SAGE_WGL_LAST_EXTENSION ".$INDEX."\n\n";
#include the SAGEAPI stuff
print SAGE_HEADER "#include \"sage/header.h\"\n";
#include GL for defs
# define __glext_h_ so we dont pull in the standard glext.h file when
# we include gl.h
print SAGE_HEADER "#include <GL/gl.h>\n";
print SAGE_HEADER "#define WIN32_LEAN_AND_MEAN 1\n";
print SAGE_HEADER "#include <windows.h>\n";
print SAGE_HEADER "#include <$WGLEXT_SAGE_FILE>\n\n";
#define the extensions array
print SAGE_HEADER "SAGEAPI int sage_wgl_ext[SAGE_WGL_LAST_EXTENSION];\n\n";
#print the function declarations and #if's generated from glext.h
for (@FUNCTION_HEADER) {
print SAGE_HEADER $_;
}
#add the init function prototype
print SAGE_HEADER "\nSAGEAPI void sage_wgl_init(void);\n\n";
#endif for header check
print SAGE_HEADER "#endif\n";
#close file handle
close SAGE_HEADER;
# Write the code file
#open code file for writing
open (SAGE_CODE, ">$SAGE_CODE_FILE") or die "Can't open: $SAGE_CODE_FILE\n";
print "Writing $SAGE_CODE_FILE\n";
#Write autogen comment
print SAGE_CODE "/* This file is autogenerated by sage_build_wgl.pl */\n";
#Write license header
open (LICENSE, "<$LICENSE_FILE") or die "Can't open: $LICENSE_FILE\n";
while(<LICENSE>) { print SAGE_CODE $_; }
close LICENSE;
print SAGE_CODE "\n";
#write the #includes
print SAGE_CODE "#include <$SAGE_HEADER_FILE>\n";
print SAGE_CODE "#include \"SDL.h\"\n";
print SAGE_CODE "#include <sage/utility.h>\n";
print SAGE_CODE "\n";
#start writing the function inits
for (@FUNCTION_CODE) {
print SAGE_CODE $_;
}
print SAGE_CODE "\n";
#start writing the init function
print SAGE_CODE "int sage_wgl_ext[SAGE_WGL_LAST_EXTENSION];\n\n";
print SAGE_CODE "void sage_wgl_init(void) {\n";
for (@FUNCTION_INIT) {
print SAGE_CODE $_;
}
# start writing the extension check code
for (@BOOLS_DEF) {
print SAGE_CODE " sage_wgl_ext[".uc($_)."] = isExtensionSupported(\"$_\");\n";
}
#write closing brace for function
print SAGE_CODE "}\n";
#close file handle
close SAGE_CODE;