1#!/usr/bin/perl -w
2
3# Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4# Copyright (C) 2009 Google Inc. All rights reserved.
5# Copyright (C) 2011 Brent Fulgham. All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
11# 1.  Redistributions of source code must retain the above copyright
12#     notice, this list of conditions and the following disclaimer. 
13# 2.  Redistributions in binary form must reproduce the above copyright
14#     notice, this list of conditions and the following disclaimer in the
15#     documentation and/or other materials provided with the distribution. 
16# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17#     its contributors may be used to endorse or promote products derived
18#     from this software without specific prior written permission. 
19#
20# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31# Update script for WebKit Open Source Project.
32
33use strict;
34use FindBin;
35use lib $FindBin::Bin;
36use File::Basename;
37use File::Path;
38use File::Spec;
39use Getopt::Long;
40use VCSUtils;
41use webkitdirs;
42
43sub runSvnUpdate();
44sub runGitUpdate();
45
46# Handle options
47my $quiet = '';
48my $showHelp;
49my $useGYP = 0;
50
51determineIsChromium();
52
53determineIsWinCairo();
54
55chdirWebKit();
56
57my $getOptionsResult = GetOptions(
58    'h|help'  => \$showHelp,
59    'q|quiet' => \$quiet,
60    'gyp' => \$useGYP,
61); 
62
63if (!$getOptionsResult || $showHelp) {
64    print STDERR <<__END__;
65Usage: @{[ basename($0) ]} [options]
66  --chromium  also update dependencies of the chromium port
67  -h|--help   show the help message
68  -q|--quiet  pass -q to svn update for quiet updates
69  --gyp       generate project files from gyp after update
70  --wincairo  also update dependencies of the WinCairo port
71__END__
72    exit 1;
73}
74
75my $startTime = time();
76
77my @svnOptions = ();
78push @svnOptions, '-q' if $quiet;
79
80# Don't prompt when using svn-1.6 or newer.
81push @svnOptions, qw(--accept postpone) if isSVNVersion16OrNewer();
82
83print "Updating OpenSource\n" unless $quiet;
84runSvnUpdate() if isSVN();
85runGitUpdate() if isGit();
86
87if (-d "../Internal") {
88    chdir("../Internal");
89    print "Updating Internal\n" unless $quiet;
90    runSvnUpdate() if isSVN();
91    runGitUpdate() if isGit();
92} elsif (isChromium()) {
93    # Workaround for https://bugs.webkit.org/show_bug.cgi?id=38926
94    # We should remove the following "if" block when we find a right fix.
95    if ((isCygwin() || isWindows()) && (stat("WebKit/chromium/features.gypi"))[9] >= $startTime) {
96        print "features.gypi has been updated. Cleaning the build directories.\n";
97        rmtree(["WebKit/chromium/Debug", "WebKit/chromium/Release"]);
98    }
99
100    system("perl", "Tools/Scripts/update-webkit-chromium") == 0 or die $!;
101} elsif (isAppleWinWebKit()) {
102    system("perl", "Tools/Scripts/update-webkit-auxiliary-libs") == 0 or die;
103    if (isWinCairo()) {
104        system("perl", "Tools/Scripts/update-webkit-wincairo-libs") == 0 or die;
105    }
106}
107
108setupAppleWinEnv() if isAppleWinWebKit();
109
110if ($useGYP) {
111    print "Generating Project Files\n";
112    system("perl", "Tools/Scripts/generate-project-files") == 0 or die "Failed to run generate-project-files";
113}
114
115exit 0;
116
117sub runSvnUpdate()
118{
119    open UPDATE, "-|", "svn", "update", @svnOptions or die;
120    my @conflictedChangeLogs;
121    while (my $line = <UPDATE>) {
122        print $line;
123        $line =~ m/^C\s+(.+?)[\r\n]*$/;
124        if ($1) {
125          my $filename = normalizePath($1);
126          push @conflictedChangeLogs, $filename if basename($filename) eq "ChangeLog";
127        }
128    }
129    close UPDATE or die;
130
131    if (@conflictedChangeLogs) {
132        print "Attempting to merge conflicted ChangeLogs.\n";
133        my $resolveChangeLogsPath = File::Spec->catfile(dirname($0), "resolve-ChangeLogs");
134        (system($resolveChangeLogsPath, "--no-warnings", @conflictedChangeLogs) == 0)
135            or die "Could not open resolve-ChangeLogs script: $!.\n";
136    }
137}
138
139sub runGitUpdate()
140{
141    # Doing a git fetch first allows setups with svn-remote.svn.fetch = trunk:refs/remotes/origin/master
142    # to perform the rebase much much faster.
143    system("git", "fetch") == 0 or die;
144    if (isGitSVN()) {
145        system("git", "svn", "rebase") == 0 or die;
146    } else {
147        # This will die if branch.$BRANCHNAME.merge isn't set, which is
148        # almost certainly what we want.
149        system("git", "pull") == 0 or die;
150    }
151}
152