|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use File::Glob 'bsd_glob'; |
| 7 | + |
| 8 | +#original |
| 9 | +sub read_file { |
| 10 | + my $f = shift; |
| 11 | + open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!"; |
| 12 | + binmode $fh; |
| 13 | + return do { local $/; <$fh> }; |
| 14 | +} |
| 15 | +#original |
| 16 | +sub write_file { |
| 17 | + my ($f, $data) = @_; |
| 18 | + die "FATAL: write_file() no data" unless defined $data; |
| 19 | + open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!"; |
| 20 | + binmode $fh; |
| 21 | + print $fh $data or die "FATAL: write_file() cannot write to '$f': $!"; |
| 22 | + close $fh or die "FATAL: write_file() cannot close '$f': $!"; |
| 23 | + return; |
| 24 | +} |
| 25 | +#original |
| 26 | +sub patch_file { |
| 27 | + my ($content, @variables) = @_; |
| 28 | + for my $v (@variables) { |
| 29 | + if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) { |
| 30 | + my $name = $1; |
| 31 | + $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s; |
| 32 | + } |
| 33 | + else { |
| 34 | + die "patch_file failed: " . substr($v, 0, 30) . ".."; |
| 35 | + } |
| 36 | + } |
| 37 | + return $content; |
| 38 | +} |
| 39 | +#original |
| 40 | +sub prepare_variable { |
| 41 | + my ($varname, @list) = @_; |
| 42 | + my $output = "$varname="; |
| 43 | + my $len = length($output); |
| 44 | + foreach my $obj (sort @list) { |
| 45 | + $len = $len + length $obj; |
| 46 | + $obj =~ s/\*/\$/; |
| 47 | + if ($len > 100) { |
| 48 | + $output .= "\\\n"; |
| 49 | + $len = length $obj; |
| 50 | + } |
| 51 | + $output .= $obj . ' '; |
| 52 | + } |
| 53 | + $output =~ s/ $//; |
| 54 | + return $output; |
| 55 | +} |
| 56 | + |
| 57 | +#changed from original |
| 58 | +sub process_makefiles { |
| 59 | + my (@o) = @_; |
| 60 | + |
| 61 | + |
| 62 | + my $var_o = prepare_variable("OBJECTS", @o); |
| 63 | + (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg; |
| 64 | + |
| 65 | + # TODO: update MSVC project files |
| 66 | + |
| 67 | + # update OBJECTS |
| 68 | + for my $m (qw/ makefile makefile.shared makefile_include.mk makefile.msvc makefile.unix makefile.mingw /) { |
| 69 | + my $old = read_file($m); |
| 70 | + my $new = $m eq 'makefile.msvc' ? patch_file($old, $var_obj) |
| 71 | + : patch_file($old, $var_o); |
| 72 | + if ($old ne $new) { |
| 73 | + write_file($m, $new); |
| 74 | + warn "changed: $m\n"; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +# changed from original but mainly just stripped down to what's needed |
| 80 | +sub gather_dependencies |
| 81 | +{ |
| 82 | + my %depmap; |
| 83 | + foreach my $filename (glob 'bn*.c') { |
| 84 | + open(my $src, '<', $filename) or die "Can't open source file!\n"; |
| 85 | + read $src, my $content, -s $src; |
| 86 | + close $src; |
| 87 | + |
| 88 | + $content =~ s{/\*.*?\*/}{}gs; |
| 89 | + foreach my $line (split /\n/, $content) { |
| 90 | + while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()|(?<=\()mp\_[a-z_0-9]*(?=,)/g) { |
| 91 | + my $a = $&; |
| 92 | + next if $a eq "mp_err"; |
| 93 | + $a = 'bn_' . $a . '.o'; |
| 94 | + push @{$depmap{$filename}}, $a; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return %depmap; |
| 99 | +} |
| 100 | + |
| 101 | +#new |
| 102 | +sub uniq { |
| 103 | + my %seen; |
| 104 | + grep !$seen{$_}++, @_; |
| 105 | +} |
| 106 | + |
| 107 | +#new |
| 108 | +sub make_it_so{ |
| 109 | + # TODO: get rid of hardcoded paths |
| 110 | + my $config = "make_config.conf"; |
| 111 | + open(my $src, '<', $config) or die "Can't open config!\n"; |
| 112 | + read $src, my $content, -s $src; |
| 113 | + close $src; |
| 114 | + |
| 115 | + my %depmap = gather_dependencies(); |
| 116 | + my @all_deps; |
| 117 | + |
| 118 | + # read line by line |
| 119 | + foreach my $line (split /\n/, $content) { |
| 120 | + chomp $line; |
| 121 | + next if $line =~ /^#/; |
| 122 | + $line = 'bn_' . $line . '.c'; |
| 123 | + #check %depmap for the dependencies of the entry in that line |
| 124 | + exists $depmap{$line} or die "\"$line\" does not exist.\n"; |
| 125 | + #add those dependencies to the list of all dependecies |
| 126 | + push @all_deps, @{$depmap{$line}}; |
| 127 | + } |
| 128 | + # unique the list of all dependencies |
| 129 | + my @uniq = uniq(@all_deps); |
| 130 | + # write the makefiles |
| 131 | + process_makefiles(@uniq); |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +make_it_so(); |
| 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 | + |
0 commit comments