File: autocode.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (196 lines) | stat: -rw-r--r-- 7,609 bytes parent folder | download | duplicates (3)
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
function autocode(overwrite, glheaderpath, openal)

% AUTOCODE.M  Generate MATLAB-OpenGL or OpenAL interface code from OpenGL or OpenAL header files
%
% Usage: autocode(overwrite, glheaderpath, openal).
%
% glheaderpath is ignored in the current script for OpenGL! Instead the
% files in the headers/ subdirectory and the file ./GL/glew.h are used
% to parse OpenGL function definitions. Our own copy of glew.h gives us
% the OpenGL 1.0 and OpenGL 1.1 base definitions. headers/glext_edit.h
% gives us the definitions of OpenGL 1.2 and later, as well as OpenGL
% extensions. header/glu_edit.h and headers/glut_edit.h provide GLU and
% GLUT function definitions.
%
% glheaderpath used to be the path to the OpenGL gl.h and glu.h header files.
%
% If overwrite is == 1, then existing M-Files are overwritten, else (and
% this is the default) existing files are not touched.
%
% If openal is == 1, then OpenAL code is generated, instead of OpenGL code.
%
% 18-Dec-05 -- created (RFM)
% 05-Mar-06 -- added option to spec. glheaderpath (MK)
% 30-May-06 -- added option 'overwrite' to not overwrite M-Files. (MK)
%              added additional parsing code for headers/glext_edit.h
% 06-Feb-07 -- added support for OpenAL (MK)
% 24-Mar-11 -- Silence mlint warnings. (MK)
% 01-Apr-12 -- Adapt to parsing of current glext_edit.h from OpenGL registry. (MK)
% 28-Sep-14 -- Make sure to not add same function multiple times. (MK)
% 28-Sep-14 -- Use our own copy of glu.h, as Apple ones is screwed up by now. (MK)
% 29-Sep-14 -- Cross-Check each entry point for glew.h support before adding it. (MK)
clc;

if nargin < 1 || isempty(overwrite)
    overwrite = 0;
end;
overwrite %#ok<NOPRT>

if nargin < 3
	openal = 0;
end

% Alternate path to header files specified?
if nargin < 2 || isempty(glheaderpath)
    if IsOSX
        glheaderpath = '/System/Library/Frameworks/OpenGL.framework/Headers';
		if openal
			glheaderpath = '/System/Library/Frameworks/OpenAL.framework/Headers';
		end
    end;
    if IsLinux
        glheaderpath = '/usr/include/GL';
		if openal
			glheaderpath = '/usr/include/AL';
		end
    end;
    if IsWin
        error('autocode.m not yet supported on M$-Windows.');
    end;
end;

if openal
	fprintf('Parsing OpenAL and ALC header files in %s ...\n',glheaderpath);
else
	fprintf('Parsing OpenGL and GLU header files in sub-directories GL/ and headers/\n');
	%fprintf('Parsing OpenGL and GLU header files in %s ...\n',glheaderpath);
end

% delete unprotected wrapper files
% ( protected = contains string "---protected---" somewhere in the file )
if overwrite > 0
    ! find ../wrap/*.m -not -exec grep -q -e ---protected--- {} \; -print0 | xargs -0 -n 1 rm
end;

% make file with list of OpenGL functions
tmplistfile='/tmp/mogl_listfile.txt';

if openal
	unix(sprintf('grep al[A-Z]   %s/al.h        | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*extern[[:space:]]*//'' | sed -E ''s/;[[:space:]]*$//'' >  %s',glheaderpath, tmplistfile));
	unix(sprintf('grep alu[A-Z]  %s/alc.h       | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*extern[[:space:]]*//'' | sed -E ''s/;[[:space:]]*$//'' >> %s',glheaderpath, tmplistfile));

	% initialize and open C file
	cfile='al_auto.c';
	if unix(sprintf('cat al_auto_init.c | sed ''s/DATE/%s/'' > %s',datestr(now,1),cfile))~=0,
		error(sprintf('unable to initialize ''%s''',cfile)); %#ok<SPERR>
	end
else
	%unix(sprintf('grep gl[A-Z]   %s/gl.h        | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*extern[[:space:]]*//'' | sed -E ''s/;[[:space:]]*$//'' >  %s',glheaderpath, tmplistfile));
	unix(sprintf('grep gl[A-Z]   GL/glew.h      | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*extern[[:space:]]*//'' | sed -E ''s/;[[:space:]]*$//'' >  %s',tmplistfile));
	unix(sprintf('grep glu[A-Z]  headers/glu_edit.h | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*extern[[:space:]]*//'' | sed -E ''s/;[[:space:]]*$//'' >> %s',tmplistfile));
	unix(sprintf('grep glut[A-Z] headers/glut_edit.h | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*extern[[:space:]]*//'' | sed -E ''s/;[[:space:]]*$//'' >> %s',tmplistfile));
	unix(sprintf('grep gl[A-Z]   headers/glext_edit.h | grep -v ProcPtr | grep -v \\#define | sed -E ''s/^[[:space:]]*GLAPI[[:space:]]*//''  | sed -E ''s/[[:space:]]*APIENTRY*//'' | sed -E ''s/;[[:space:]]*$//'' >>  %s',tmplistfile));
    
	% initialize and open C file
	cfile='gl_auto.c';
	if unix(sprintf('cat gl_auto_init.c | sed ''s/DATE/%s/'' > %s',datestr(now,1),cfile))~=0,
		error(sprintf('unable to initialize ''%s''',cfile)); %#ok<SPERR>
	end
end

listfid=fopen(tmplistfile,'r');
cfid=fopen(cfile,'a');

% initialize list of entries in command map
cmdmap={};
knownfuncs = '';

% Read the full content of glew.h into glewfuncs:
glewfid = fopen('glew.h', 'rt');
glewfuncs = char(fread(glewfid)); %#ok<FREAD>
fclose(glewfid);

% Make it digestable by strfind():
glewfuncs = transpose(squeeze(glewfuncs));

% step through list of OpenGL functions
while(~feof(listfid)),

    % parse the next function declaration
    funcp=cparse(fgetl(listfid), openal);

    % skip unsupported/unparseable functions:
    if isempty(funcp)
        fprintf(1,'Rejected: [unparseable/unsupported, see line above]\n');
        continue;
    end

    % skip functions in autono.txt
    if filecontains('autono.txt',funcp.fname),
        fprintf(1,'Rejected: %s ... [in autono.txt]\n',funcp.fname);
        continue;
    end

    % Skip functions already added in the past:
    if ~isempty(strfind(knownfuncs, [' ' funcp.fname]))
        fprintf(1,'Rejected: %s ... [redundant definition]\n',funcp.fname);
        continue;
    end
    
    % skip functions that use double indirection in an input argument
    if doubleindirect(funcp),
        % add function name to autono.txt
        unix(sprintf('cat "%s  %% uses double indirection (added to autono.txt by autocode.m) >> autono.txt',funcp.fname));
        fprintf(1,'Rejected: %s ... [pointer double indirection]\n',funcp.fname);
        continue;
    end
    
    % Cross-Check with glew.h glxew.h etc. to make sure our current GLEW
    % version supports this entry point. Exceptions are gluXXX and glutXXX
    % functions:
    if isempty(strfind(glewfuncs, [' ' funcp.fname ' '])) && isempty(strfind(funcp.fname, 'glu'))
        fprintf(1,'Rejected: %s ... [entry point unsupported by GLEW]\n',funcp.fname);
        continue;
    end

    % function meets criteria, so show function name
    fprintf(1,'Found: %s ...',funcp.fname);

    % extract information needed to construct M-file wrapper and
    % C interface function (input arguments, return arguments, etc.)
    [M,C]=mcinfo(funcp);

    if (overwrite > 0) || (exist([ funcp.fname '.m'], 'file')~= 2)
        % (re)create M-file wrapper
        fprintf(1, '(re)creating M-File wrapper.\n');
        mwrite(funcp,M,openal);
    else
        fprintf(1, '\n');
    end
    
    % create C interface function
	cwrite(cfid,funcp,C);
	
	% add line to command map (to be written to C file later)
	cmdmap{end+1}=sprintf('{ %-35s%-35s }',[ '"' funcp.fname '",' ],C.interfacename); %#ok<AGROW>
	
    % add function name to list of already known functions:
    knownfuncs = [knownfuncs ' ' funcp.fname];  %#ok<AGROW>
end

% sort and write command map
cmdmap=sort(cmdmap);
fprintf(cfid,'int gl_auto_map_count=%d;\n',numel(cmdmap));
fprintf(cfid,'cmdhandler gl_auto_map[] = {\n');
fprintf(cfid,'%s,\n',cmdmap{1:end-1});
fprintf(cfid,'%s};\n',cmdmap{end});

% close files
fclose(cfid);
fclose(listfid);

% clean up
delete('/tmp/mogl_*');
delete('/tmp/moal_*');

return