src/dynapi/gendynapi.pl
changeset 8094 9efaae827924
child 8149 681eb46b8ac4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dynapi/gendynapi.pl	Mon Dec 09 16:03:18 2013 -0500
     1.3 @@ -0,0 +1,141 @@
     1.4 +#!/usr/bin/perl -w
     1.5 +
     1.6 +#  Simple DirectMedia Layer
     1.7 +#  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.8 +#
     1.9 +#  This software is provided 'as-is', without any express or implied
    1.10 +#  warranty.  In no event will the authors be held liable for any damages
    1.11 +#  arising from the use of this software.
    1.12 +#
    1.13 +#  Permission is granted to anyone to use this software for any purpose,
    1.14 +#  including commercial applications, and to alter it and redistribute it
    1.15 +#  freely, subject to the following restrictions:
    1.16 +#
    1.17 +#  1. The origin of this software must not be misrepresented; you must not
    1.18 +#     claim that you wrote the original software. If you use this software
    1.19 +#     in a product, an acknowledgment in the product documentation would be
    1.20 +#     appreciated but is not required.
    1.21 +#  2. Altered source versions must be plainly marked as such, and must not be
    1.22 +#     misrepresented as being the original software.
    1.23 +#  3. This notice may not be removed or altered from any source distribution.
    1.24 +
    1.25 +# WHAT IS THIS?
    1.26 +#  When you add a public API to SDL, please run this script, make sure the
    1.27 +#  output looks sane (hg diff, it adds to existing files), and commit it.
    1.28 +#  It keeps the dynamic API jump table operating correctly.
    1.29 +
    1.30 +# If you wanted this to be readable, you shouldn't have used perl.
    1.31 +
    1.32 +use warnings;
    1.33 +use strict;
    1.34 +use File::Basename;
    1.35 +
    1.36 +chdir(dirname(__FILE__) . '/../..');
    1.37 +my $sdl_dynapi_procs_h = "src/dynapi/SDL_dynapi_procs.h";
    1.38 +my $sdl_dynapi_overrides_h = "src/dynapi/SDL_dynapi_overrides.h";
    1.39 +
    1.40 +my %existing = ();
    1.41 +if (-f $sdl_dynapi_procs_h) {
    1.42 +    open(SDL_DYNAPI_PROCS_H, '<', $sdl_dynapi_procs_h) or die("Can't open $sdl_dynapi_procs_h: $!\n");
    1.43 +    while (<SDL_DYNAPI_PROCS_H>) {
    1.44 +        if (/\ASDL_DYNAPI_PROC\(.*?,(.*?),/) {
    1.45 +            $existing{$1} = 1;
    1.46 +        }
    1.47 +    }
    1.48 +    close(SDL_DYNAPI_PROCS_H)
    1.49 +}
    1.50 +
    1.51 +open(SDL_DYNAPI_PROCS_H, '>>', $sdl_dynapi_procs_h) or die("Can't open $sdl_dynapi_procs_h: $!\n");
    1.52 +open(SDL_DYNAPI_OVERRIDES_H, '>>', $sdl_dynapi_overrides_h) or die("Can't open $sdl_dynapi_overrides_h: $!\n");
    1.53 +
    1.54 +opendir(HEADERS, 'include') or die("Can't open include dir: $!\n");
    1.55 +while (readdir(HEADERS)) {
    1.56 +    next if not /\.h\Z/;
    1.57 +    my $header = "include/$_";
    1.58 +    open(HEADER, '<', $header) or die("Can't open $header: $!\n");
    1.59 +    while (<HEADER>) {
    1.60 +        chomp;
    1.61 +        next if not /\A\s*extern\s+DECLSPEC/;
    1.62 +        my $decl = "$_ ";
    1.63 +        if (not $decl =~ /\)\s*;/) {
    1.64 +            while (<HEADER>) {
    1.65 +                chomp;
    1.66 +                s/\A\s+//;
    1.67 +                s/\s+\Z//;
    1.68 +                $decl .= "$_ ";
    1.69 +                last if /\)\s*;/;
    1.70 +            }
    1.71 +        }
    1.72 +
    1.73 +        $decl =~ s/\s+\Z//;
    1.74 +        #print("DECL: [$decl]\n");
    1.75 +
    1.76 +        if ($decl =~ /\A\s*extern\s+DECLSPEC\s+(const\s+|)(unsigned\s+|)(.*?)\s*(\*?)\s*SDLCALL\s+(.*?)\s*\((.*?)\);/) {
    1.77 +            my $rc = "$1$2$3$4";
    1.78 +            my $fn = $5;
    1.79 +
    1.80 +            next if $existing{$fn};   # already slotted into the jump table.
    1.81 +
    1.82 +            my @params = split(',', $6);
    1.83 +
    1.84 +            #print("rc == '$rc', fn == '$fn', params == '$params'\n");
    1.85 +
    1.86 +            my $retstr = ($rc eq 'void') ? '' : 'return';
    1.87 +            my $paramstr = '(';
    1.88 +            my $argstr = '(';
    1.89 +            my $i = 0;
    1.90 +            foreach (@params) {
    1.91 +                my $str = $_;
    1.92 +                $str =~ s/\A\s+//;
    1.93 +                $str =~ s/\s+\Z//;
    1.94 +                #print("1PARAM: $str\n");
    1.95 +                if ($str eq 'void') {
    1.96 +                    $paramstr .= 'void';
    1.97 +                } elsif ($str eq '...') {
    1.98 +                    if ($i > 0) {
    1.99 +                        $paramstr .= ', ';
   1.100 +                    }
   1.101 +                    $paramstr .= $str;
   1.102 +                } elsif ($str =~ /\A\s*((const\s+|)(unsigned\s+|)([a-zA-Z0-9_]*)\s*([\*\s]*))\s*(.*?)\Z/) {
   1.103 +                    #print("PARSED: [$1], [$2], [$3], [$4], [$5]\n");
   1.104 +                    my $type = $1;
   1.105 +                    my $var = $6;
   1.106 +                    $type =~ s/\A\s+//;
   1.107 +                    $type =~ s/\s+\Z//;
   1.108 +                    $var =~ s/\A\s+//;
   1.109 +                    $var =~ s/\s+\Z//;
   1.110 +                    $type =~ s/\s*\*\Z/*/g;
   1.111 +                    $type =~ s/\s*(\*+)\Z/ $1/;
   1.112 +                    #print("SPLIT: ($type, $var)\n");
   1.113 +                    my $name = chr(ord('a') + $i);
   1.114 +                    if ($i > 0) {
   1.115 +                        $paramstr .= ', ';
   1.116 +                        $argstr .= ',';
   1.117 +                    }
   1.118 +                    my $spc = ($type =~ /\*\Z/) ? '' : ' ';
   1.119 +                    $paramstr .= "$type$spc$name";
   1.120 +                    $argstr .= "$name";
   1.121 +                }
   1.122 +                $i++;
   1.123 +            }
   1.124 +
   1.125 +            $paramstr = '(void' if ($i == 0);  # Just to make this consistent.
   1.126 +
   1.127 +            $paramstr .= ')';
   1.128 +            $argstr .= ')';
   1.129 +
   1.130 +            print("NEW: $decl\n");
   1.131 +            print SDL_DYNAPI_PROCS_H "SDL_DYNAPI_PROC($rc,$fn,$paramstr,$argstr,$retstr)\n";
   1.132 +            print SDL_DYNAPI_OVERRIDES_H "#define $fn ${fn}_REAL\n";
   1.133 +        } else {
   1.134 +            print("Failed to parse decl [$decl]!\n");
   1.135 +        }
   1.136 +    }
   1.137 +    close(HEADER);
   1.138 +}
   1.139 +closedir(HEADERS);
   1.140 +
   1.141 +close(SDL_DYNAPI_PROCS_H);
   1.142 +close(SDL_DYNAPI_OVERRIDES_H);
   1.143 +
   1.144 +# vi: set ts=4 sw=4 expandtab: