summaryrefslogtreecommitdiffstats
path: root/src/windows/build/copyfiles.pl
blob: 79b6e156baae1aab5730ee80fd4f6bd5f62fd825 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!perl -w


#use strict;

use XML::Simple;
use Data::Dumper;

sub copyFiles {
    local ($xml, $config)   = @_;
    local @odr              = $config->{Config};
    local @files            = $xml->{Files};
    # Check for includes:

    if (exists $xml->{Files}->{Include}->{path}) {
        my $includepath    = $xml->{Files}->{Include}->{path};
        print "Info -- Including files from $includepath\n";
        my $savedDir = `cd`;
        $savedDir =~ s/\n//g;
        chdir $originalDir;                         ## Includes are relative to where we were invoked.

        print "Info -- chdir to ".`cd`."\n"         if ($verbose);
        my $tmp    = new XML::Simple;
        my $includeXML = $tmp->XMLin($includepath);
        chdir $savedDir;
        print "Info -- chdir to ".`cd`."\n"         if ($verbose);

        local $i = 0;
        while ($includeXML->{File}[$i]) {           ## Copy File entries from includeXML.

            $files[0]->{File}[++$#{$files[0]->{File}}] = $includeXML->{File}[$i];        

            $i++;
            }
        delete $files->{Include};
        }
    ##++ Set up path substitution variables for use inside the copy loop:

    # A path can contain a variable part, which will be handled here.  If the variable part is 

    # the Always or BuildDependent tag, then the variable will be changed to the 

    # build-type-dependent PathFragment.

    # If the variable part is the IgnoreTag, then the file will not be copied.

    # If the variable part is %filestem%, it will be replaced with Config->FileStem->name.

    my ($PathFragment, $BuildDependentTag, $IgnoreTag, $FileStemFragment, $fromRoot, $toRoot); 
    my $bPathTags    = (exists $xml->{Config}->{DebugArea}) && (exists $xml->{Config}->{ReleaseArea});
    my $bFileStem    = (exists $xml->{Config}->{FileStem});
    
    if ($odr->{debug}->{def}) {   ## Debug build tags:

        $PathFragment       = $xml->{Config}->{DebugArea}->{value};
        $BuildDependentTag  = $xml->{Config}->{DebugTag}->{value};
        $IgnoreTag          = $xml->{Config}->{ReleaseTag}->{value};
        }
    else {                                  ## Release build tags:

        $PathFragment       = $xml->{Config}->{ReleaseArea}->{value};
        $BuildDependentTag  = $xml->{Config}->{ReleaseTag}->{value};
        $IgnoreTag          = $xml->{Config}->{DebugTag}->{value};
        }            
    my $AlwaysTag           = $xml->{Config}->{AlwaysTag}->{value};
    $FileStemFragment       = $xml->{Config}->{FileStem}->{name};    
    $fromRoot               = $xml->{Config}->{From}->{root};    
    $toRoot                 = $xml->{Config}->{To}->{root};    
    ##-- Set up path substitution variables for use inside the copy loop.

    # For each file in the file list:

    #  Substitute any variable parts of the path name.

    #  Handle wildcards

    #  Copy


    local $i    = 0;
    my $bOldDot = 1;
    my $bDot    = 0;
    while ($files[0]->{File}[$i]) {

        my ($name, $newname, $from, $to, $file);
        $file   = $files[0]->{File}->[$i];
        $name   = $file->{name};
        if (exists $file->{newname})    {$newname = $file->{newname};}
        else                            {$newname = $name;}
        if ($name && (! exists $file->{ignore})) {      ## Ignore or process this entry?

            $from   = "$fromRoot\\$file->{from}\\$name";
            $to     = "$toRoot\\$file->{to}\\$newname";
            # Copy this file?  Check for ignore tag [debug-only in release mode or vice versa].

            if ( $bPathTags || $bFileStem || (index($from.$to, $IgnoreTag) <0) ) {  
                if ($bPathTags) {                                   ## Apply PathTag substitutions:

                    $from   =~ s/$AlwaysTag/$PathFragment/g;
                    $to     =~ s/$AlwaysTag/$PathFragment/g;
                    $from   =~ s/$BuildDependentTag/$PathFragment/g;
                    $to     =~ s/$BuildDependentTag/$PathFragment/g;
                    }
                if ($bFileStem) {                                   ## FileStem substitution?

                    $from   =~ s/%filestem%/$FileStemFragment/g;
                    $to     =~ s/%filestem%/$FileStemFragment/g;
                    }        
                # %-DEBUG% substitution:

                local $DebugFragment    = ($odr->{debug}->{def}) ? "-DEBUG" : "";
                $from       =~ s/%\-DEBUG%/$DebugFragment/g;
                $to         =~ s/%\-DEBUG%/$DebugFragment/g;
                $to         =~ s/\*.*//;                ## Truncate to path before any wildcard


                my $bCopyOK     = 1;
                my $fromcheck   = $from;
                my $bRequired   = ! (exists $file->{notrequired});
                if ($name =~ /\*/) {                    ## Wildcard case

                    $fromcheck =~ s/\*.*//;
                    if ($bRequired && (! -d $fromcheck)) {
                        if ($bDot) {print "\n";}
                        die "Fatal -- Can't find $fromcheck";
                        }
                    $bCopyOK = !system("echo D | xcopy /D /F /Y /S  $from $to > a.tmp 2>NUL");
                    }
                else {                                  ## Specific file case

                    if ($bRequired && (! -e $fromcheck)) {
                        if ($bDot) {print "\n";}
                        die "Fatal -- Can't find $fromcheck";
                        }
                    $bCopyOK    = !system("echo F | xcopy /D /F /Y $from $to > a.tmp 2>NUL");
                    }

                if ($bCopyOK) {                         ## xcopy OK - show progress

                    # To show progress when files aren't copied, print a string of dots.

                    open(MYINPUTFILE, "<a.tmp");
                    my(@lines) = <MYINPUTFILE>;
                    foreach $line (@lines) { 
                        $bDot = ($line =~ /^0/);
                        }
                    close(MYINPUTFILE);
                    if (!$bDot && $bOldDot) {print "\n";}
                    if ($bDot) {print "."; STDOUT->flush;}
                    else {print "$from copied to $to\n";}
                    $bOldDot = $bDot;
                    }
                else {                                  ## xcopy failed

                    if (!exists $file->{notrequired}) {
                        if ($bDot) {print "\n";}
                        die "Fatal -- Copy of $from to $to failed";
                        }
                    }                                   ## End xcopy succeed or fail

                }                                       ## End not dummy entry nor ignored

            }
        $i++;
        }
    if ($bDot) {print "\n";}
    }

return 1;