Removed some unnecessary includes.
[wine] / dlls / opengl32 / make_opengl
1 #!/usr/bin/perl -w
2
3 # This script is called thus :
4 #
5 #   make_opengl path_to_spec_file opengl_version
6 #
7 #     - path_to_spec_file is the path to the directory where the OpenGL
8 #       spec files are located. These files are part of the OpenGL
9 #       sample implementation CVS tree and are located in
10 #       CVS_ROOT/projects/ogl-sample/main/doc/registry/specs.
11 #       You can find them on the web at the following URL :
12 #         http://oss.sgi.com/cgi-bin/cvsweb.cgi/projects/ogl-sample/main/doc/registry/specs/
13 #
14 #     - opengl_version is the OpenGL version emulated by the library
15 #       (can be 1.0 to 1.2).
16 #
17 # This script generates the three following files :
18 #
19 #     - opengl32.spec : the spec file giving all the exported functions
20 #       of the OpenGL32.DLL library. These functions are the one an
21 #       application can directly link to (and are all the functions
22 #       defined in the OpenGL core for the version defined by
23 #       'opengl_version').
24 #
25 #     - opengl_norm.c : this file contains the thunks for all OpenGL
26 #       functions that are defined in 'opengl32.spec'. The corresponding
27 #       functions NEED to be defined in Linux's libGL or the library
28 #       won't be able to be linked in.
29 #
30 #     - opengl_ext.c : in this file are stored thunks for ALL possible
31 #       OpenGL extensions (at least, all the extensions that are defined
32 #       in the OpenGL extension registry). Contrary to 'opengl_norm.c',
33 #       you do not need to have these extensions in your libGL to have
34 #       OpenGL work (as they are resolved at run-time using
35 #       glXGetProcAddressARB).
36 #
37
38 #
39 # Files to generate
40 #
41 $spec_file = "opengl32.spec";
42 $norm_file = "opengl_norm.c";
43 $ext_file  = "opengl_ext.c";
44
45 # Set to 0 for removing the ENTER / LEAVE GL calls
46 $gen_thread_safe = 1;
47 # Prefix used for the local variables
48 $ext_prefix = "func_";
49 # If set to 1, generate TRACEs for each OpenGL function
50 $gen_traces = 1;
51
52 #
53 # List of categories to put in the 'opengl_norm.c' file
54 #
55 %cat_1_0 = ( "display-list" => 1, 
56              "drawing" => 1, 
57              "drawing-control" => 1, 
58              "feedback" => 1, 
59              "framebuf" => 1, 
60              "misc" => 1, 
61              "modeling" => 1, 
62              "pixel-op" => 1, 
63              "pixel-rw" => 1, 
64              "state-req" => 1, 
65              "xform" => 1 );
66 %cat_1_1 = ( %cat_1_0, 
67              "1_1" => 1 );
68 %cat_1_2 = ( %cat_1_1, 
69              "VERSION_1_2" => 1, 
70              "ARB_multitexture" => 1 );
71
72 %norm_categories = ();
73
74 #
75 # This hash table gives the conversion between OpenGL types and what
76 # is used by the TRACE printfs
77 #
78 %debug_conv = 
79     ("GLbitfield" => "%d",
80      "GLboolean" => "%d",
81      "GLbyte" => "%d",
82      "GLclampd" => "%f",
83      "GLclampf" => "%f",
84      "GLdouble" => "%f",
85      "GLenum" => "%d",
86      "GLfloat" => "%f",
87      "GLint" => "%d",
88      "GLshort" => "%d",
89      "GLsizei" => "%d",
90      "GLstring" => "%s",
91      "GLubyte" => "%d",
92      "GLuint" => "%d",
93      "GLushort" => "%d",
94      "GLvoid" => "(void)",
95      "_GLfuncptr" => "%p");
96
97 #
98 # This hash table gives the conversion between OpenGL types and what
99 # is used in the .spec file
100 #
101 %arg_conv = 
102     ("GLbitfield" => [ "long", 4 ],
103      "GLboolean" => [ "long", 4 ],
104      "GLbyte" => [ "long", 4 ],
105      "GLclampd" => [ "double", 8 ],
106      "GLclampf" => [ "long", 4 ],
107      "GLdouble" => [ "double", 8 ],
108      "GLenum" => [ "long", 4 ],
109      "GLfloat" => [ "long", 4 ],
110      "GLint" => [ "long", 4 ],
111      "GLshort" => [ "long", 4 ],
112      "GLsizei" => [ "long", 4 ],
113      "GLstring" => [ "str", 4 ],
114      "GLubyte" => [ "long", 4 ],
115      "GLuint" => [ "long", 4 ],
116      "GLushort" => [ "long", 4 ],
117      "GLvoid" => [ "void", 4 ],
118      "_GLfuncptr" => [ "ptr", 4 ]);
119
120 #
121 # This functions generates the thunk for a given function.
122 #
123 sub GenerateThunk {
124     my ($func_ref, $comment, $prefix, $thread_safe) = @_;
125     my ($ret) = ("");
126     my ($call_arg) = ("");
127     my ($trace_arg) = ("");
128
129     # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
130     # Patrik says: Well I would be even happier if a (OPENGL.@) was added as well. Done. :-)
131     if ($comment eq 1) {
132         $ret = $ret . "/***********************************************************************\n";
133         $ret = $ret . " *              " . $func_ref->[0] . " (OPENGL.@)\n";
134         $ret = $ret . " */\n";
135     }
136     $ret = $ret . $func_ref->[1] . " WINAPI wine_" . $func_ref->[0] . "( ";
137     for ($i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
138         $type = $func_ref->[2]->[$i]->[0];
139         $name = $func_ref->[2]->[$i]->[1];
140         $ret = $ret . "$type $name";
141         $call_arg = $call_arg . "$name";
142         if ($type =~ /\*/) {
143             $trace_arg = $trace_arg . "%p";
144         } else {
145             $trace_arg = $trace_arg . $debug_conv{$type};
146         }
147         if ($i != $#{@{$func_ref->[2]}}) {
148             $ret = $ret . ", ";
149             $call_arg = $call_arg . ", ";
150             $trace_arg = $trace_arg . ", ";
151         } else {
152             $ret = $ret . " ";
153             $call_arg = $call_arg . " ";
154         }
155     }
156     $ret = $ret . ") {\n";
157     if ($func_ref->[1] ne "void") {
158         $ret = $ret . "  " . $func_ref->[1] . " ret_value;\n";
159     }
160     if ($gen_traces) {
161         $ret = $ret . "  TRACE(\"(" . $trace_arg . ")\\n\"";
162         if ($trace_arg ne "") {
163             $ret = $ret . ", " . $call_arg;
164         }
165         $ret = $ret . ");\n";
166     }
167     if ($thread_safe) {
168         $ret = $ret . "  ENTER_GL();\n";
169     }
170     $ret = $ret . "  ";
171     if ($func_ref->[1] ne "void") {
172         $ret = $ret . "ret_value = ";
173     }
174     $ret = $ret . $prefix . $func_ref->[0] . "( " . $call_arg . ");\n";
175     if ($thread_safe) {
176         $ret = $ret . "  LEAVE_GL();\n";
177     }
178     if ($func_ref->[1] ne "void") {
179         $ret = $ret . "  return ret_value;\n"
180     }
181     $ret = $ret . "}\n";
182
183     # Return this string....
184     $ret;
185 }
186
187 #
188 # Extract and checks the number of arguments
189 #
190 if ($#ARGV != 1) {
191     die "Usage : make_opengl OpenGL_registry_location OpenGL_version.\n";
192 }
193 $registry_path = shift @ARGV;
194 $version       = shift @ARGV;
195 if ($version eq "1.0") {
196     %norm_categories = %cat_1_0;
197 } elsif ($version eq "1.1") {
198     %norm_categories = %cat_1_1;
199 } elsif ($version eq "1.2") {
200     %norm_categories = %cat_1_2;
201 } else {
202     die "OpenGL version incorrect. Should be one of '1.0', '1.1' or '1.2'.\n";
203 }
204
205 #
206 # Open the registry files
207 #
208 open(TYPES,    $registry_path . "/gl.tm")   || die "Could not open 'gl.tm'. Please check your path the the registry files.\n";
209 open(REGISTRY, $registry_path . "/gl.spec") || die "Could not open 'gl.spec'. Please check your path the the registry files.\n";
210
211 #
212 # First, create a mapping between the pseudo types used in the spec file
213 # and OpenGL types using the 'gl.tm' file.
214 #
215 %pseudo_to_opengl = ();
216 while ($line = <TYPES>) {
217     ($pseudo, $opengl) = ($line =~ /(\w*),\*,\*,\s*(.*),\*,\*/);
218     $pseudo_to_opengl{$pseudo} = $opengl;
219 }
220 # This is to override the 'void' -> '*' bogus conversion
221 $pseudo_to_opengl{"void"} = "void";
222 # This is a bug in the spec file...
223 $pseudo_to_opengl{"FfdTargetSGIX"} = "GLint";
224 $pseudo_to_opengl{"FfdMaskSGIX"} = "GLint";
225 $pseudo_to_opengl{"IglooFunctionSelectSGIX"} = "GLint";
226 $pseudo_to_opengl{"IglooParameterSGIX"} = "GLint";
227
228 #
229 # Then, create the list of all OpenGL functions using the 'gl.spec'
230 # file. This will create two hash-tables, one with all the function
231 # whose category matches the one listed in '@norm_categories', the other
232 # with all other functions.
233 #
234 # An element of the hash table is a reference to an array with these
235 # elements :
236 #
237 #  - function name
238 #
239 #  - return type
240 #
241 #  - reference to an array giving the list of arguments (an empty array
242 #    for a 'void' function).
243 #
244 # The list of arguments is itself an array of reference to arrays. Each
245 # of these arrays represents the argument type and the argument name.
246 #
247 # An example :
248 #
249 # void glBitmap( GLsizei width, GLsizei height,
250 #                GLfloat xorig, GLfloat yorig,
251 #                GLfloat xmove, GLfloat ymove,
252 #                const GLubyte *bitmap );
253 #
254 # Would give something like that :
255 #
256 # [ "glBitmap",
257 #   "void",
258 #   [ [ "GLsizei", "width" ],
259 #     [ "GLsizei", "height" ],
260 #     [ "GLfloat", "xorig" ],
261 #     [ "GLfloat", "yorig" ],
262 #     [ "GLfloat", "xmove" ],
263 #     [ "GLfloat", "ymove" ],
264 #     [ "GLubyte *", "bitmap"] ] ];
265 #
266 %norm_functions = ();
267 %ext_functions  = ();
268
269 while ($line = <REGISTRY>) {
270     if ($line =~ /^\w*\(.*\)/) {
271         # Get the function name (NOTE: the 'gl' prefix needs to be added later)
272         ($funcname, $args) = ($line =~ /^(\w*)\((.*)\)/);
273         # and the argument names
274         @arg_names = split /\s*,\s*/, $args;
275         
276         # After get :
277         #  - the return type
278         #  - the argument types
279         #  - the category the function belongs
280         %arg_types = ();
281         $category = "";
282         $ret_type = "";
283         while (1) {
284             $line = <REGISTRY>;
285             unless (defined($line)) {
286                 last;
287             } elsif ($line =~ /^\s*$/) {
288                 if (($category eq "") || ($ret_type eq "")) {
289                     die "Missing 'category' line in function $funcname.\n";
290                 }
291                 last;
292             } elsif ($line =~ /\t*return\t*(\w*)/) {
293                 ($ret_type) = ($line =~ /\t*return\s*(\w*)/);
294                 $ret_type = $pseudo_to_opengl{$ret_type};
295                 unless (defined($ret_type)) {
296                     die "Unsupported return type in function $funcname\n";
297                 }
298             } elsif ($line =~ /^\t*category/) {
299                 ($category) = ($line =~ /^\t*category\s*([\w-]*)/);
300             } elsif ($line =~ /^\t*param/) {
301                 ($name, $base_type, $ext) = ($line =~ /\t*param\s*(\w*)\s*(\w*) (.*)/);
302                 $ptr = 0;
303                 unless (defined($name)) { 
304                     chomp $line;
305                     die "Broken spec file line $line in function $funcname\n";
306                 }
307
308                 if ($ext =~ /array/) {
309                     # This is a pointer
310                     $ptr = 1;
311                 } elsif ($ext =~ /value/) {
312                     # And this a 'normal' value
313                     $ptr = 0;
314                 } else {
315                     chomp $line;
316                     die "Unsupported type : $line in function $funcname\n";
317                 }
318                 # Get the 'real' type and append a '*' in case of a pointer
319                 $type = $pseudo_to_opengl{$base_type};
320                 unless (defined($type)) {
321                     chomp $line;
322                     die "Unsupported return type in function $funcname for type $base_type (line $line)\n";
323                 }
324                 if ($ptr) {
325                     $type = $type . "*";
326                 }
327                 
328                 $arg_types{$name} = $type;
329             }
330         }
331
332         # Now, build the argument reference
333         $arg_ref = [ ];
334         for ($i = 0; $i <= $#arg_names; $i++) {
335             unless (defined($arg_types{$arg_names[$i]})) {
336                 print "@arg_names\n";
337                 foreach (sort keys %arg_types) {
338                     print "$_ => $arg_types{$_}\n";
339                 }
340                 die "Undefined type for $arg_names[$i] in function $funcname\n";
341             }
342             
343             push @$arg_ref, [ $arg_types{$arg_names[$i]}, $arg_names[$i] ];
344         }
345         $func_ref = [ "gl" . $funcname, 
346                       $ret_type,
347                       $arg_ref ];
348         
349         # Now, put in one or the other hash table
350         if ($norm_categories{$category}) {
351             $norm_functions{$funcname} = $func_ref;
352         } else {
353             $ext_functions{$funcname} = $func_ref;
354         }
355     }
356 }
357
358 #
359 # Clean up the input files
360 #
361 close(TYPES);
362 close(REGISTRY);
363
364 #
365 # Now, generate the output files. First, the spec file.
366 #
367 open(SPEC, ">" . $spec_file);
368
369 print SPEC "
370 name opengl32
371 type win32
372
373 init OpenGL32_Init
374 import x11drv
375 import kernel32
376
377 debug_channels (opengl)
378
379 @  stdcall wglCreateContext(long) wglCreateContext
380 @  stdcall wglCreateLayerContext(long long) wglCreateLayerContext
381 @  stdcall wglCopyContext(long long long) wglCopyContext
382 @  stdcall wglDeleteContext(long) wglDeleteContext
383 @  stdcall wglDescribeLayerPlane(long long long long ptr) wglDescribeLayerPlane
384 @  stdcall wglGetCurrentContext() wglGetCurrentContext
385 @  stdcall wglGetCurrentDC() wglGetCurrentDC
386 @  stdcall wglGetLayerPaletteEntries(long long long long ptr) wglGetLayerPaletteEntries
387 @  stdcall wglGetProcAddress(str) wglGetProcAddress
388 @  stdcall wglMakeCurrent(long long) wglMakeCurrent
389 @  stdcall wglRealizeLayerPalette(long long long) wglRealizeLayerPalette
390 @  stdcall wglSetLayerPaletteEntries(long long long long ptr) wglSetLayerPaletteEntries
391 @  stdcall wglShareLists(long long) wglShareLists
392 @  stdcall wglSwapLayerBuffers(long long) wglSwapLayerBuffers
393 @  stdcall wglUseFontBitmapsA(long long long long) wglUseFontBitmapsA
394 @  stdcall wglUseFontOutlinesA(long long long long long long long ptr) wglUseFontOutlinesA
395 @  stub    glGetLevelParameterfv
396 @  stub    glGetLevelParameteriv
397 @  stub    wglUseFontBitmapsW
398 @  stub    wglUseFontOutlinesW
399 @  forward wglChoosePixelFormat GDI32.ChoosePixelFormat
400 @  forward wglDescribePixelFormat GDI32.DescribePixelFormat
401 @  forward wglGetPixelFormat GDI32.GetPixelFormat
402 @  forward wglSetPixelFormat GDI32.SetPixelFormat
403 @  forward wglSwapBuffers GDI32.SwapBuffers
404 ";
405
406 foreach (sort keys %norm_functions) {
407     $func_name = $norm_functions{$_}->[0];
408     print SPEC "@  stdcall $func_name( ";
409     for ($i = 0; $i <= $#{@{$norm_functions{$_}->[2]}}; $i++) {
410         $type = $norm_functions{$_}->[2]->[$i]->[0];
411         if ($type =~ /\*/) {
412             print SPEC "ptr ";
413         } elsif (defined($arg_conv{$type})) {
414             print SPEC "$@$arg_conv{$type}[0] ";
415         } else {
416             die "No convertion for GL type $type...\n";
417         }
418     }
419     print SPEC ") wine_$func_name\n";
420 }
421 close(SPEC);
422
423 #
424 # After the spec file, the opengl_norm.c file
425 #
426 open(NORM, ">" . $norm_file);
427 print NORM "
428 /* Auto-generated file... Do not edit ! */
429
430 #include \"config.h\"
431 #include \"wine_gl.h\"
432 #include \"debugtools.h\"
433
434 typedef const GLubyte * GLstring;
435
436 DEFAULT_DEBUG_CHANNEL(opengl);
437
438 ";
439 foreach (sort keys %norm_functions) {
440     $string = GenerateThunk($norm_functions{$_}, 1, "", $gen_thread_safe);
441
442     print NORM "$string\n";
443 }
444 close(NORM);
445
446 #
447 # Finally, more complex, the opengl_ext.c file
448 #
449 open(EXT, ">" . $ext_file);
450 print EXT "
451 /* Auto-generated file... Do not edit ! */
452
453 #include \"config.h\"
454 #include \"wine_gl.h\"
455 #include \"debugtools.h\"
456
457 typedef const GLubyte * GLstring;
458
459 #include \"opengl_ext.h\"
460
461 DEFAULT_DEBUG_CHANNEL(opengl);
462
463 ";
464
465 # First, generate the function pointers
466 foreach (sort keys %ext_functions) {
467     $func_ref = $ext_functions{$_};
468     print EXT $func_ref->[1] . " (*" . $ext_prefix . $func_ref->[0] . ")( ";
469     for ($i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
470         $type = $func_ref->[2]->[$i]->[0];
471         print EXT "$type";
472         if ($i != $#{@{$func_ref->[2]}}) {
473             print EXT ", ";
474         } else {
475             print EXT " ";
476         }
477     }
478     print EXT ") = (void *) 0xdeadbeef;\n";
479 }
480
481 # Then, the function prototypes
482 print EXT "\n\n/* The function prototypes */\n";
483 foreach (sort keys %ext_functions) {
484     $func_ref = $ext_functions{$_};
485     print EXT $func_ref->[1] . " WINAPI " . "wine_" . $func_ref->[0] . "( ";
486     for ($i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
487         $type = $func_ref->[2]->[$i]->[0];
488         print EXT "$type";
489         if ($i != $#{@{$func_ref->[2]}}) {
490             print EXT ", ";
491         } else {
492             print EXT " ";
493         }
494     }
495     print EXT ");\n";
496 }
497
498 # Then the table giving the string <-> function correspondance */
499 print EXT "\n\n/* The table giving the correspondance between names and functions */\n";
500 @tmp = keys %ext_functions;
501 print EXT "int extension_registry_size = " . ($#tmp + 1) . ";\n";
502 print EXT "OpenGL_extension extension_registry[" . ($#tmp + 1) . "] = {\n";
503 $i = 0;
504 foreach (sort keys %ext_functions) {
505     $func_ref = $ext_functions{$_};
506     print EXT "  { \"" . $func_ref->[0] . "\", (void *) wine_" . $func_ref->[0] . ", (void **) (&" . $ext_prefix . $func_ref->[0] . ") }";
507     if ($i != $#tmp) {
508         print EXT ",";
509     }
510     $i++;
511     print EXT "\n";
512 }
513 print EXT "};\n";
514
515 # And, finally, the thunks themselves....
516 print EXT "\n/* The thunks themselves....*/\n";
517 foreach (sort keys %ext_functions) {
518     $string = GenerateThunk($ext_functions{$_}, 0, $ext_prefix, $gen_thread_safe);
519
520     print EXT "$string\n";
521 }
522 close(EXT);