summaryrefslogtreecommitdiffhomepage
path: root/booster.pl
blob: bd4c51edcb4c810e9f797c7dce680fefcb8959bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl -w

use strict;
use File::Path qw(make_path);
use File::Basename;
use Getopt::Long;

if (@ARGV == 0) {
    exit 0;
}

my @files;

for my $path (@ARGV) {
    if (-f $path) {
        push @files, $path;
    } elsif (-d $path) {
        find_files($path, \@files);
    } else {
        warn "Skipping '$path': not a file or directory\n";
    }
}

make_path("boostified/include/boost/tproc");

for my $file (@files) {
    process_file($file);
}

sub find_files {
    my ($dir, $list) = @_;
    opendir(my $dh, $dir) or die "Cannot open directory $dir: $!\n";
    while (my $entry = readdir($dh)) {
        next if $entry eq '.' || $entry eq '..';
        my $full = "$dir/$entry";
        if (-d $full) {
            find_files($full, $list);
        } elsif (-f $full && $full =~ /\.(cpp|inl|hpp|h|cc|hh|cxx|hxx)$/i) {
            push @$list, [ $entry, $full, dirname($full) ];
        }
    }
    closedir($dh);
}

sub process_file {
    my $info = shift;
    my ($name, $fullpath, $srcdir) = @$info;

    open(my $fh, '<', $fullpath) or die "Cannot read $fullpath: $!\n";
    my $content = do { local $/; <$fh> };
    close($fh);

    my $original = $content;

    $content =~ s/\bocl::/boost::/g;
    $content =~ s/\bocl\//boost\//g;
    $content =~ s/\bOCL_/BOOST_/g;

    if ($content ne $original) {
        my $reldir = substr($srcdir, length($ARGV[0]) + 1);
        my $outdir = "boostified/include/boost/$reldir";
        make_path($outdir) unless -d $outdir;

        open(my $out, '>', "$outdir/$name") or die "Cannot write $outdir/$name: $!\n";
        print $out $content;
        close($out);
    }
}