gendynapi.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/perl -w
  2. # Simple DirectMedia Layer
  3. # Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. #
  5. # This software is provided 'as-is', without any express or implied
  6. # warranty. In no event will the authors be held liable for any damages
  7. # arising from the use of this software.
  8. #
  9. # Permission is granted to anyone to use this software for any purpose,
  10. # including commercial applications, and to alter it and redistribute it
  11. # freely, subject to the following restrictions:
  12. #
  13. # 1. The origin of this software must not be misrepresented; you must not
  14. # claim that you wrote the original software. If you use this software
  15. # in a product, an acknowledgment in the product documentation would be
  16. # appreciated but is not required.
  17. # 2. Altered source versions must be plainly marked as such, and must not be
  18. # misrepresented as being the original software.
  19. # 3. This notice may not be removed or altered from any source distribution.
  20. # WHAT IS THIS?
  21. # When you add a public API to SDL, please run this script, make sure the
  22. # output looks sane (git diff, it adds to existing files), and commit it.
  23. # It keeps the dynamic API jump table operating correctly.
  24. # If you wanted this to be readable, you shouldn't have used perl.
  25. use warnings;
  26. use strict;
  27. use File::Basename;
  28. chdir(dirname(__FILE__) . '/../..');
  29. my $sdl_dynapi_procs_h = "src/dynapi/SDL_dynapi_procs.h";
  30. my $sdl_dynapi_overrides_h = "src/dynapi/SDL_dynapi_overrides.h";
  31. my %existing = ();
  32. if (-f $sdl_dynapi_procs_h) {
  33. open(SDL_DYNAPI_PROCS_H, '<', $sdl_dynapi_procs_h) or die("Can't open $sdl_dynapi_procs_h: $!\n");
  34. while (<SDL_DYNAPI_PROCS_H>) {
  35. if (/\ASDL_DYNAPI_PROC\(.*?,(.*?),/) {
  36. $existing{$1} = 1;
  37. }
  38. }
  39. close(SDL_DYNAPI_PROCS_H)
  40. }
  41. open(SDL_DYNAPI_PROCS_H, '>>', $sdl_dynapi_procs_h) or die("Can't open $sdl_dynapi_procs_h: $!\n");
  42. open(SDL_DYNAPI_OVERRIDES_H, '>>', $sdl_dynapi_overrides_h) or die("Can't open $sdl_dynapi_overrides_h: $!\n");
  43. opendir(HEADERS, 'include') or die("Can't open include dir: $!\n");
  44. while (my $d = readdir(HEADERS)) {
  45. next if not $d =~ /\.h\Z/;
  46. my $header = "include/$d";
  47. open(HEADER, '<', $header) or die("Can't open $header: $!\n");
  48. while (<HEADER>) {
  49. chomp;
  50. next if not /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/;
  51. my $decl = "$_ ";
  52. if (not $decl =~ /\)\s*;/) {
  53. while (<HEADER>) {
  54. chomp;
  55. s/\A\s+//;
  56. s/\s+\Z//;
  57. $decl .= "$_ ";
  58. last if /\)\s*;/;
  59. }
  60. }
  61. $decl =~ s/\s+\Z//;
  62. #print("DECL: [$decl]\n");
  63. if ($decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(const\s+|)(unsigned\s+|)(.*?)\s*(\*?)\s*SDLCALL\s+(.*?)\s*\((.*?)\);/) {
  64. my $rc = "$2$3$4$5";
  65. my $fn = $6;
  66. next if $existing{$fn}; # already slotted into the jump table.
  67. my @params = split(',', $7);
  68. #print("rc == '$rc', fn == '$fn', params == '$params'\n");
  69. my $retstr = ($rc eq 'void') ? '' : 'return';
  70. my $paramstr = '(';
  71. my $argstr = '(';
  72. my $i = 0;
  73. foreach (@params) {
  74. my $str = $_;
  75. $str =~ s/\A\s+//;
  76. $str =~ s/\s+\Z//;
  77. #print("1PARAM: $str\n");
  78. if ($str eq 'void') {
  79. $paramstr .= 'void';
  80. } elsif ($str eq '...') {
  81. if ($i > 0) {
  82. $paramstr .= ', ';
  83. }
  84. $paramstr .= $str;
  85. } elsif ($str =~ /\A\s*((const\s+|)(unsigned\s+|)([a-zA-Z0-9_]*)\s*([\*\s]*))\s*(.*?)\Z/) {
  86. #print("PARSED: [$1], [$2], [$3], [$4], [$5]\n");
  87. my $type = $1;
  88. my $var = $6;
  89. $type =~ s/\A\s+//;
  90. $type =~ s/\s+\Z//;
  91. $var =~ s/\A\s+//;
  92. $var =~ s/\s+\Z//;
  93. $type =~ s/\s*\*\Z/*/g;
  94. $type =~ s/\s*(\*+)\Z/ $1/;
  95. #print("SPLIT: ($type, $var)\n");
  96. my $var_array_suffix = "";
  97. # parse array suffix
  98. if ($var =~ /\A.*(\[.*\])\Z/) {
  99. #print("PARSED ARRAY SUFFIX: [$1] of '$var'\n");
  100. $var_array_suffix = $1;
  101. }
  102. my $name = chr(ord('a') + $i);
  103. if ($i > 0) {
  104. $paramstr .= ', ';
  105. $argstr .= ',';
  106. }
  107. my $spc = ($type =~ /\*\Z/) ? '' : ' ';
  108. $paramstr .= "$type$spc$name$var_array_suffix";
  109. $argstr .= "$name";
  110. }
  111. $i++;
  112. }
  113. $paramstr = '(void' if ($i == 0); # Just to make this consistent.
  114. $paramstr .= ')';
  115. $argstr .= ')';
  116. print("NEW: $decl\n");
  117. print SDL_DYNAPI_PROCS_H "SDL_DYNAPI_PROC($rc,$fn,$paramstr,$argstr,$retstr)\n";
  118. print SDL_DYNAPI_OVERRIDES_H "#define $fn ${fn}_REAL\n";
  119. } else {
  120. print("Failed to parse decl [$decl]!\n");
  121. }
  122. }
  123. close(HEADER);
  124. }
  125. closedir(HEADERS);
  126. close(SDL_DYNAPI_PROCS_H);
  127. close(SDL_DYNAPI_OVERRIDES_H);
  128. # vi: set ts=4 sw=4 expandtab: