|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use warnings; |
| 4 | + |
| 5 | +sub trim |
| 6 | +{ |
| 7 | + my $trim = $_[0]; |
| 8 | + $trim =~ s/^\s+|\s+$// ; |
| 9 | + return $trim; |
| 10 | +} |
| 11 | + |
| 12 | +# Predeclarations |
| 13 | +sub output; |
| 14 | + |
| 15 | +# Parameters |
| 16 | +my $name = $ARGV[0]; |
| 17 | + |
| 18 | +# Read in from stdin |
| 19 | +my @spec = <STDIN>; |
| 20 | + |
| 21 | +# Prefix for symbols |
| 22 | +my $prefix = ""; |
| 23 | +if (defined $ARGV[0] and $ARGV[0] ne "") |
| 24 | +{ |
| 25 | + $prefix = $ARGV[0]; |
| 26 | +} |
| 27 | + |
| 28 | +# Output variables |
| 29 | +my $symbolExterns = ""; |
| 30 | +my $symbolEntries = ""; |
| 31 | + |
| 32 | +foreach my $line (@spec) |
| 33 | +{ |
| 34 | + # Ignore empty lines |
| 35 | + if ($line =~ /^\s*$/) |
| 36 | + { |
| 37 | + next; |
| 38 | + } |
| 39 | + |
| 40 | + # Ignore comment lines |
| 41 | + if ($line =~ /^\s*#/) |
| 42 | + { |
| 43 | + next; |
| 44 | + } |
| 45 | + |
| 46 | + # Ignore lines defining modules (we only care about symbols) |
| 47 | + if (substr($line, 0, 1) ne "\t") |
| 48 | + { |
| 49 | + next; |
| 50 | + } |
| 51 | + |
| 52 | + # Skip the tab character |
| 53 | + substr($line, 0, 1) = ""; |
| 54 | + trim($line); |
| 55 | + |
| 56 | + # Handle optional symbols |
| 57 | + my $symbol = undef; |
| 58 | + my @words = split('\s+', $line); |
| 59 | + if (substr($line, 0, 1) eq "?") |
| 60 | + { |
| 61 | + $symbol = $words[1]; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + $symbol = $words[0]; |
| 66 | + } |
| 67 | + |
| 68 | + # Remove any trailing colons |
| 69 | + $symbol =~ s/:$// ; |
| 70 | + |
| 71 | + $symbolExterns .= "extern \"C\" void *$symbol;\n"; |
| 72 | + $symbolEntries .= " { \"$symbol\", $symbol },\n"; |
| 73 | +} |
| 74 | + |
| 75 | +print STDOUT $symbolExterns; |
| 76 | +output "struct LibExport { const char *name; void *address; };"; |
| 77 | +output "struct LibInfo { const char **name; struct LibExport *exports; };"; |
| 78 | +output "static const char *__libexternalname = \"$name\";"; |
| 79 | +output "static struct LibExport __libexports[] ="; |
| 80 | +output "{"; |
| 81 | +print STDOUT $symbolEntries; |
| 82 | +output " { 0, 0 }"; |
| 83 | +output "};"; |
| 84 | +output "static struct LibInfo __libinfo="; |
| 85 | +output "{"; |
| 86 | +output " &__libexternalname,"; |
| 87 | +output " __libexports"; |
| 88 | +output "};"; |
| 89 | +output "LibInfo *__libinfoptr_$name = &__libinfo;"; |
| 90 | + |
| 91 | +sub output |
| 92 | +{ |
| 93 | + my $line = $_[0]; |
| 94 | + if (@_ != 1) |
| 95 | + { |
| 96 | + $line = ""; |
| 97 | + } |
| 98 | + |
| 99 | + print STDOUT "$line\n"; |
| 100 | +} |
0 commit comments