1#!/bin/bash
2# Copyright (c) 2012 The Native Client Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Script assumed to be run in native_client/
7if [[ "x${OSTYPE}" = "xcygwin" ]]; then
8  cd "$(cygpath "${PWD}")"
9fi
10if [[ ${PWD} != */native_client ]]; then
11  echo "ERROR: must be run in native_client!"
12  exit 1
13fi
14
15if [[ $# -ne 3 ]]; then
16  echo "USAGE: $0 version_file win/mac/linux glibc/newlib"
17  exit 2
18fi
19
20if [[ "${0:0:1}" = "/" ]]; then
21  declare -r scriptname="$0"
22else
23  declare -r scriptname="$PWD/$0"
24fi
25
26cd tools/BACKPORTS
27
28if [[ "$((sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") 2>/dev/null)" = "$(cat "$1.lastver")" ]]; then
29  # Everything is already done
30  exit 0
31fi
32
33set -x
34set -e
35set -u
36
37declare -r GIT_BASE_URL=https://chromium.googlesource.com/native_client
38
39rm -f "$$.error"
40
41# Checkout toolchain sources from git repo.  We'll need them later when we'll
42# patch sources toolchain before calling build script.
43for dirname in binutils gcc gdb glibc linux-headers-for-nacl newlib ; do
44  repo="${dirname}"
45  if [[ "$repo" != "linux-headers-for-nacl" ]]; then
46    repo="nacl-${repo}"
47  fi
48  if [[ -d "$dirname" ]]; then (
49    cd "$dirname"
50    git pull --all ||
51    (cd .. &&
52     rm -rf "$dirname")
53  ) fi
54  if [[ ! -d "$dirname" ]]; then (
55    git clone "${GIT_BASE_URL}/${repo}.git" "$dirname"
56  ) fi || touch $$.error &
57done
58
59wait
60
61# If we were unable to checkout some sources then it's time to stop.
62if [[ -e "$$.error" ]]; then
63  rm -f "$$.error"
64  # Errors are reported by git above already
65  exit 1
66fi
67
68# Here we'll checkout correct versions of all the sources for all supported
69# ppapi versions.
70while read name id comment ; do
71  case "$name" in
72    binutils | gcc | glibc | newlib | '' | \#*)
73      # Skip this line
74      ;;
75    *)
76      if [[ "$((sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") 2>/dev/null)" = "$(cat "$1.$name.lastver")" ]]; then
77	# Everything is already done
78	continue
79      fi
80      # First step is to use glient to sync sources.
81      if [[ -d "$name" ]]; then
82	cd "$name"
83	if [[ "$2" = "win" ]]; then
84	  # "gclient.bat revert" automatically calls "runhooks"… ⇒ it fails…
85	  # "gclient runhooks" downloads toolchain then calls gyp… ⇒ it fails…
86	  # "glient.bat runhooks" sees toolchain and simply calls gyp ⇒ success!
87	  # Additional fun: error codes are lost somewhere in gclient.bat
88	  ( gclient.bat revert || true
89	    gclient runhooks --force || true
90	    gclient.bat runhooks --force || true
91	  ) < /dev/null
92	else
93	  gclient revert < /dev/null
94	fi
95      else
96	mkdir "$name"
97	cd "$name"
98	if [[ "$name" == ppapi17 ]]; then
99	  cat >.gclient <<-END
100	solutions = [
101	  { "name"        : "native_client",
102	    "url"         : "http://src.chromium.org/native_client/trunk/src/native_client@$id",
103	    "deps_file"   : "DEPS",
104	    "managed"     : True,
105	    "custom_deps" : {
106	      "third_party/jsoncpp/source/include": "http://svn.code.sf.net/p/jsoncpp/code/trunk/jsoncpp/include@246",
107	      "third_party/jsoncpp/source/src/lib_json": "http://svn.code.sf.net/p/jsoncpp/code/trunk/jsoncpp/src/lib_json@246",
108	    },
109	    "safesync_url": "",
110	  },
111	]
112	END
113	elif [[ "$name" == ppapi1[89] ]] || [[ "$name" == ppapi2[0-6] ]]; then
114	  cat >.gclient <<-END
115	solutions = [
116	  { "name"        : "native_client",
117	    "url"         : "http://src.chromium.org/native_client/trunk/src/native_client@$id",
118	    "deps_file"   : "DEPS",
119	    "managed"     : True,
120	    "custom_deps" : {
121	      "third_party/jsoncpp/source/include": "http://svn.code.sf.net/p/jsoncpp/code/trunk/jsoncpp/include@248",
122	      "third_party/jsoncpp/source/src/lib_json": "http://svn.code.sf.net/p/jsoncpp/code/trunk/jsoncpp/src/lib_json@248",
123	    },
124	    "safesync_url": "",
125	  },
126	]
127	END
128	else
129	  cat >.gclient <<-END
130	solutions = [
131	  { "name"        : "native_client",
132	    "url"         : "http://src.chromium.org/native_client/trunk/src/native_client@$id",
133	    "deps_file"   : "DEPS",
134	    "managed"     : True,
135	    "custom_deps" : {
136	    },
137	    "safesync_url": "",
138	  },
139	]
140	END
141	fi
142	if [[ "$2" = "win" ]]; then
143	  # "gclient.bat revert" automatically calls "runhooks"… ⇒ it fails…
144	  # "gclient runhooks" downloads toolchain then calls gyp… ⇒ it fails…
145	  # "glient.bat runhooks" sees toolchain and simply calls gyp ⇒ success!
146	  # Additional fun: error codes are lost somewhere in gclient.bat
147	  ( gclient.bat sync || true
148	    gclient runhooks --force || true
149	    gclient.bat runhooks --force || true
150	  ) < /dev/null
151	else
152	  gclient sync < /dev/null
153	fi
154      fi
155      # Now we need to change versions to officialy mark binaries.  We don't
156      # show git revision because we combine different branches here.
157      patch -p0 <<-END
158	--- native_client/buildbot/buildbot_standard.py
159	+++ native_client/buildbot/buildbot_standard.py
160	@@ -148 +148,2 @@
161	-  with Step('cleanup_temp', status):
162	+  if False:
163	+   with Step('cleanup_temp', status):
164	--- native_client/tools/glibc_download.sh
165	+++ native_client/tools/glibc_download.sh
166	@@ -42 +42 @@
167	-  for ((j=glibc_revision+1;j<glibc_revision+revisions_count;j++)); do
168	+  for ((j=\${glibc_revision%~*}+1;j<\${glibc_revision%~*}+revisions_count;j++)); do
169	--- native_client/tools/glibc_revision.sh
170	+++ native_client/tools/glibc_revision.sh
171	@@ -13 +13 @@
172	-for i in REVISIONS glibc_revision.sh Makefile ; do
173	+for i in ../../../VERSIONS ../../../build_backports.sh REVISIONS glibc_revision.sh Makefile ; do
174	@@ -20 +20 @@
175	-echo "\$REVISION"
176	+echo "\$REVISION~$name"
177	END
178      if [[ "$name" = ppapi1? ]] || [[ "$name" = ppapi2[0-6] ]]; then
179	patch -p0 <<-END
180	--- native_client/tools/Makefile
181	+++ native_client/tools/Makefile
182	@@ -202,2 +202,4 @@
183	-  CFLAGS="-m\$(HOST_TOOLCHAIN_BITS) \$(CFLAGS)" \\
184	-  CXXFLAGS="-m\$(HOST_TOOLCHAIN_BITS) \$(CXXFLAGS)" \\
185	+  CC="\$(GCC_CC)" \\
186	+  CXX="\$(GCC_CXX)" \\
187	+  CFLAGS="\$(CFLAGS)" \\
188	+  CXXFLAGS="\$(CXXFLAGS)" \\
189	@@ -363 +365,7 @@
190	+ifeq (\$(PLATFORM), mac)
191	+GCC_CC = clang -m\$(HOST_TOOLCHAIN_BITS) -fgnu89-inline
192	+GCC_CXX = clang++ -m\$(HOST_TOOLCHAIN_BITS)
193	+else
194	 GCC_CC = gcc -m\$(HOST_TOOLCHAIN_BITS)
195	+GCC_CXX = g++ -m\$(HOST_TOOLCHAIN_BITS)
196	+endif
197	END
198      fi
199      if [[ "$name" = ppapi1? ]] || [[ "$name" = ppapi2[0-7] ]]; then
200	patch -p0 <<-END
201	--- native_client/tools/glibc_download.sh
202	+++ native_client/tools/glibc_download.sh
203	@@ -20 +20 @@
204	-declare -r glibc_url_prefix=http://gsdview.appspot.com/nativeclient-archive2/between_builders/x86_glibc/r
205	+declare -r glibc_url_prefix=http://storage.googleapis.com/nativeclient-archive2/between_builders/x86_glibc/r
206	END
207      fi
208      declare rev="$(native_client/tools/glibc_revision.sh)"
209      if [[ "$name" = ppapi14 ]]; then
210	patch -p0 <<-END
211	--- native_client/tools/Makefile
212	+++ native_client/tools/Makefile
213	@@ -237 +237 @@
214	-	sed -e s'|cloog_LDADD = \$(LDADD)|cloog_LDADD = \$(LDADD) -lstdc++ -lm |' \\
215	+	sed -e s'|LIBS = @LIBS@|LIBS = @LIBS@ -lstdc++ -lm |' \\
216	@@ -315,2 +315,2 @@
217	-	  CC="gcc" \\
218	-	  CFLAGS="-m\$(HOST_TOOLCHAIN_BITS) -O2 \$(CFLAGS)" \\
219	+	  CC="\$(GCC_CC)" \\
220	+	  CFLAGS="\$(CFLAGS)" \\
221	@@ -847 +847 @@
222	-	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1-\\2-\\3+'\` (Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/binutils ; LC_ALL=C git rev-parse HEAD\`)\\"\\n" |\\
223	+	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`cd ../../.. ; LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1-\\2-\\3+'\` (Native Client r$rev)\\"\\n" |\\
224	@@ -849,2 +849,3 @@
225	-	LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1-\\2-\\3+' > SRC/gcc/gcc/DATESTAMP
226	-	echo "Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/gcc ; LC_ALL=C git rev-parse HEAD\`" > SRC/gcc/gcc/DEV-PHASE
227	+	( cd ../../.. ; LC_ALL=C svn info ) | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1-\\2-\\3+' > SRC/gcc/gcc/DATESTAMP
228	+	echo "Native Client r$rev" > SRC/gcc/gcc/DEV-PHASE
229	+	cp -aiv SRC/gdb/gdb/version.in SRC/gdb/gdb/version.inT
230	@@ -854 +855 @@
231	-	+\`cat SRC/gdb/gdb/version.in\` \`LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1-\\2-\\3+'\` (Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/gdb ; LC_ALL=C git rev-parse HEAD\`)\\n" |\\
232	+	+\`cat SRC/gdb/gdb/version.in\` \`cd ../../.. ; LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1-\\2-\\3+'\` (Native Client r$rev)\\n" |\\
233	END
234      elif [[ "$name" = ppapi15 ]]; then
235	patch -p0 <<-END
236	--- native_client/tools/Makefile
237	+++ native_client/tools/Makefile
238	@@ -237 +237 @@
239	-	sed -e s'|cloog_LDADD = \$(LDADD)|cloog_LDADD = \$(LDADD) -lstdc++ -lm |' \\
240	+	sed -e s'|LIBS = @LIBS@|LIBS = @LIBS@ -lstdc++ -lm |' \\
241	@@ -315,2 +315,2 @@
242	-	  CC="gcc" \\
243	-	  CFLAGS="-m\$(HOST_TOOLCHAIN_BITS) -O2 \$(CFLAGS)" \\
244	+	  CC="\$(GCC_CC)" \\
245	+	  CFLAGS="\$(CFLAGS)" \\
246	@@ -847 +847 @@
247	-	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/binutils ; LC_ALL=C git rev-parse HEAD\`)\\"\\n" |\\
248	+	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`cd ../../.. ; LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r$rev)\\"\\n" |\\
249	@@ -849,2 +849,3 @@
250	-	LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+' > SRC/gcc/gcc/DATESTAMP
251	-	echo "Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/gcc ; LC_ALL=C git rev-parse HEAD\`" > SRC/gcc/gcc/DEV-PHASE
252	+	( cd ../../.. ; LC_ALL=C svn info ) | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+' > SRC/gcc/gcc/DATESTAMP
253	+	echo "Native Client r$rev" > SRC/gcc/gcc/DEV-PHASE
254	+	cp -aiv SRC/gdb/gdb/version.in SRC/gdb/gdb/version.inT
255	@@ -854 +855 @@
256	-	+\`cat SRC/gdb/gdb/version.in\` \`LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/gdb ; LC_ALL=C git rev-parse HEAD\`)\\n" |\\
257	+	+\`cat SRC/gdb/gdb/version.in\` \`cd ../../.. LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r$rev)\\n" |\\
258	END
259      elif [[ "$name" = ppapi1? ]] || [[ "$name" = ppapi2[01] ]]; then
260	patch -p0 <<-END
261	--- native_client/tools/Makefile
262	+++ native_client/tools/Makefile
263	@@ -700,5 +700,5 @@
264	-BINUTILS_PATCHNAME := naclbinutils-\$(BINUTILS_VERSION)-r\$(shell svnversion | tr : _)
265	-GCC_PATCHNAME := naclgcc-\$(GCC_VERSION)-r\$(shell svnversion | tr : _)
266	-#GDB_PATCHNAME := naclgdb-\$(GDB_VERSION)-r\$(shell svnversion | tr : _)
267	-GLIBC_PATCHNAME := naclglibc-\$(GLIBC_VERSION)-r\$(shell svnversion | tr : _)
268	-NEWLIB_PATCHNAME := naclnewlib-\$(NEWLIB_VERSION)-r\$(shell svnversion | tr : _)
269	+BINUTILS_PATCHNAME := naclbinutils-\$(BINUTILS_VERSION)-r\$(shell ./glibc_revision.sh)
270	+GCC_PATCHNAME := naclgcc-\$(GCC_VERSION)-r\$(shell ./glibc_revision.sh)
271	+#GDB_PATCHNAME := naclgdb-\$(GDB_VERSION)-r\$(shell ./glibc_revision.sh)
272	+GLIBC_PATCHNAME := naclglibc-\$(GLIBC_VERSION)-r\$(shell ./glibc_revision.sh)
273	+NEWLIB_PATCHNAME := naclnewlib-\$(NEWLIB_VERSION)-r\$(shell ./glibc_revision.sh)
274	@@ -847 +847 @@
275	-	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/binutils ; LC_ALL=C git rev-parse HEAD\`)\\"\\n" |\\
276	+	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`cd ../../.. ; LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r$rev)\\"\\n" |\\
277	@@ -849,2 +849,3 @@
278	-	LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+' > SRC/gcc/gcc/DATESTAMP
279	-	echo "Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/gcc ; LC_ALL=C git rev-parse HEAD\`" > SRC/gcc/gcc/DEV-PHASE
280	+	( cd ../../.. ; LC_ALL=C svn info ) | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+' > SRC/gcc/gcc/DATESTAMP
281	+	echo "Native Client r$rev" > SRC/gcc/gcc/DEV-PHASE
282	+	cp -aiv SRC/gdb/gdb/version.in SRC/gdb/gdb/version.inT
283	@@ -854 +855 @@
284	-	+\`cat SRC/gdb/gdb/version.in\` \`LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r\`LC_ALL=C svnversion\`, Git Commit \`cd SRC/gdb ; LC_ALL=C git rev-parse HEAD\`)\\n" |\\
285	+	+\`cat SRC/gdb/gdb/version.in\` \`cd ../../.. LC_ALL=C svn info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r$rev)\\n" |\\
286	END
287      else
288	patch -p0 <<-END
289	--- native_client/tools/Makefile
290	+++ native_client/tools/Makefile
291	@@ -700,5 +700,5 @@
292	-BINUTILS_PATCHNAME := naclbinutils-\$(BINUTILS_VERSION)-r\$(shell \$(SVNVERSION) | tr : _)
293	-GCC_PATCHNAME := naclgcc-\$(GCC_VERSION)-r\$(shell \$(SVNVERSION) | tr : _)
294	-#GDB_PATCHNAME := naclgdb-\$(GDB_VERSION)-r\$(shell \$(SVNVERSION) | tr : _)
295	-GLIBC_PATCHNAME := naclglibc-\$(GLIBC_VERSION)-r\$(shell \$(SVNVERSION) | tr : _)
296	-NEWLIB_PATCHNAME := naclnewlib-\$(NEWLIB_VERSION)-r\$(shell \$(SVNVERSION) | tr : _)
297	+BINUTILS_PATCHNAME := naclbinutils-\$(BINUTILS_VERSION)-r\$(shell ./glibc_revision.sh)
298	+GCC_PATCHNAME := naclgcc-\$(GCC_VERSION)-r\$(shell ./glibc_revision.sh)
299	+#GDB_PATCHNAME := naclgdb-\$(GDB_VERSION)-r\$(shell ./glibc_revision.sh)
300	+GLIBC_PATCHNAME := naclglibc-\$(GLIBC_VERSION)-r\$(shell ./glibc_revision.sh)
301	+NEWLIB_PATCHNAME := naclnewlib-\$(NEWLIB_VERSION)-r\$(shell ./glibc_revision.sh)
302	@@ -847 +847 @@
303	-	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`LC_ALL=C \$(SVN) info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r\`LC_ALL=C \$(SVNVERSION)\`, Git Commit \`cd SRC/binutils ; LC_ALL=C git rev-parse HEAD\`)\\"\\n" |\\
304	+	+#define BFD_VERSION_STRING  @bfd_version_package@ @bfd_version_string@ \\" \`cd ../../.. ; LC_ALL=C \$(SVN) info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r$rev)\\"\\n" |\\
305	@@ -849,2 +849,3 @@
306	-	LC_ALL=C \$(SVN) info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+' > SRC/gcc/gcc/DATESTAMP
307	-	echo "Native Client r\`LC_ALL=C \$(SVNVERSION)\`, Git Commit \`cd SRC/gcc ; LC_ALL=C git rev-parse HEAD\`" > SRC/gcc/gcc/DEV-PHASE
308	+	( cd ../../.. ; LC_ALL=C \$(SVN) info ) | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+' > SRC/gcc/gcc/DATESTAMP
309	+	echo "Native Client r$rev" > SRC/gcc/gcc/DEV-PHASE
310	+	cp -aiv SRC/gdb/gdb/version.in SRC/gdb/gdb/version.inT
311	@@ -854 +855 @@
312	-	+\`cat SRC/gdb/gdb/version.in\` \`LC_ALL=C \$(SVN) info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r\`LC_ALL=C \$(SVNVERSION)\`, Git Commit \`cd SRC/gdb ; LC_ALL=C git rev-parse HEAD\`)\\n" |\\
313	+	+\`cat SRC/gdb/gdb/version.in\` \`cd ../../.. LC_ALL=C \$(SVN) info | grep 'Last Changed Date' | sed -e s'+Last Changed Date: \\(....\\)-\\(..\\)-\\(..\\).*+\\1\\2\\3+'\` (Native Client r$rev)\\n" |\\
314	END
315      fi
316      # The buildbot has become strict in enforcing tag names.
317      # We used to emit STEP_SUCCESS, but it never got added to the annotator.
318      # Dropping the tag in old versions.
319      if [[ "$name" = ppapi1[0-9] ]] || [[ "$name" = ppapi2[0-9] ]] || \
320         [[ "$name" = ppapi30 ]]; then
321          patch -p0 <<-END
322	--- native_client/buildbot/buildbot_lib.py
323	+++ native_client/buildbot/buildbot_lib.py
324	@@ -378,7 +378,6 @@ class Step(object):
325	         raise StopBuild()
326	     else:
327	       self.status.ReportPass(self.name)
328	-      print '@@@STEP_SUCCESS@@@'
329
330	     # Suppress any exception that occurred.
331	     return True
332	END
333      fi
334      if [[ "$name" = ppapi19 ]] || [[ "$name" = ppapi20 ]]; then
335	  patch -p0 <<-END
336	--- native_client/tools/Makefile
337	+++ native_client/tools/Makefile
338	@@ -154,2 +154,2 @@
339	-NEWLIB_VERSION = 1.18.0
340	-NACL_NEWLIB_GIT_BASE = 65e6baefeb2874011001c2f843cf3083e771b62f
341	+NEWLIB_VERSION = 1.20.0
342	+NACL_NEWLIB_GIT_BASE = 151b2c72fb87849bbc6e3ef569718c6344eed2e6
343	END
344      fi
345      patch -p0 <<-END
346	--- native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
347	+++ native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
348	@@ -27 +27 @@
349	-rm -rf scons-out tools/SRC/* tools/BUILD/* tools/out tools/toolchain \\
350	+rm -rf scons-out tools/BUILD/* tools/out tools/toolchain \\
351	--- native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
352	+++ native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
353	@@ -90 +90 @@
354	-    make glibc-check
355	+    true make glibc-check
356	--- native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
357	+++ native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
358	@@ -109 +109 @@
359	-  rev="\$(tools/glibc_revision.sh)"
360	+  rev="$rev"
361	--- native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
362	+++ native_client/buildbot/buildbot_lucid64-glibc-makefile.sh
363	@@ -164 +164 @@
364	-    make glibc-check
365	+    true make glibc-check
366	--- native_client/buildbot/buildbot_mac-glibc-makefile.sh
367	+++ native_client/buildbot/buildbot_mac-glibc-makefile.sh
368	@@ -28 +28 @@
369	-rm -rf scons-out tools/SRC/* tools/BUILD/* tools/out/* tools/toolchain \\
370	+rm -rf scons-out tools/BUILD/* tools/out/* tools/toolchain \\
371	--- native_client/buildbot/buildbot_windows-glibc-makefile.sh
372	+++ native_client/buildbot/buildbot_windows-glibc-makefile.sh
373	@@ -40 +40 @@
374	-rm -rf scons-out tools/SRC/* tools/BUILD/* tools/out tools/toolchain \\
375	+rm -rf scons-out tools/BUILD/* tools/out tools/toolchain \\
376	--- native_client/buildbot/gsutil.sh
377	+++ native_client/buildbot/gsutil.sh
378	@@ -29 +29 @@
379	-  gsutil="\${SCRIPT_DIR_ABS}/../../../../../scripts/slave/gsutil.bat"
380	+  gsutil="\${SCRIPT_DIR_ABS}/../../../../../../../../../scripts/slave/gsutil.bat"
381	END
382      mv native_client/buildbot/buildbot_windows-glibc-makefile.bat \
383	native_client/buildbot/buildbot_windows-glibc-makefile.bat.orig
384      sed -e s'/  ..\\..\\..\\..\\scripts\\slave\\gsutil/  ..\\..\\..\\..\\..\\..\\..\\..\\scripts\\slave\\gsutil/' \
385	< native_client/buildbot/buildbot_windows-glibc-makefile.bat.orig \
386	> native_client/buildbot/buildbot_windows-glibc-makefile.bat
387      rm native_client/buildbot/buildbot_windows-glibc-makefile.bat.orig
388      if [[ "$name" = ppapi14 ]]; then
389	patch -p0 <<-END
390	--- native_client/buildbot/buildbot_toolchain.sh
391	+++ native_client/buildbot/buildbot_toolchain.sh
392	@@ -36 +36 @@
393	-rm -rf ../scons-out sdk-out sdk ../toolchain SRC/* BUILD/*
394	+rm -rf ../scons-out sdk-out sdk ../toolchain BUILD/*
395	@@ -61 +61 @@
396	-      \${GS_BASE}/latest/naclsdk_\${PLATFORM}_x86.tgz
397	+      \${GS_BASE}/latest~"$name"/naclsdk_\${PLATFORM}_x86.tgz
398	END
399      elif [[ "$name" = ppapi1[5-7] ]]; then
400	patch -p0 <<-END
401	--- native_client/buildbot/buildbot_toolchain.sh
402	+++ native_client/buildbot/buildbot_toolchain.sh
403	@@ -36 +36 @@
404	-rm -rf ../scons-out sdk-out sdk ../toolchain SRC/* BUILD/*
405	+rm -rf ../scons-out sdk-out sdk ../toolchain BUILD/*
406	@@ -69 +69 @@
407	-    for destrevision in \${BUILDBOT_GOT_REVISION} latest ; do
408	+    for destrevision in \${BUILDBOT_GOT_REVISION} latest~"$name" ; do
409	@@ -73 +73 @@
410	-          \${GS_BASE}/\${destrevision}/naclsdk_\${PLATFORM}_x86.\${suffix}
411	+          \${GS_BASE}/"\${destrevision}"/naclsdk_\${PLATFORM}_x86.\${suffix}
412	END
413      else
414	patch -p0 <<-END
415	--- native_client/buildbot/buildbot_toolchain.sh
416	+++ native_client/buildbot/buildbot_toolchain.sh
417	@@ -36 +36 @@
418	-rm -rf ../scons-out sdk-out sdk ../toolchain/*_newlib SRC/* BUILD/*
419	+rm -rf ../scons-out sdk-out sdk ../toolchain/*_newlib BUILD/*
420	@@ -69 +69 @@
421	-    for destrevision in \${BUILDBOT_GOT_REVISION} latest ; do
422	+    for destrevision in \${BUILDBOT_GOT_REVISION} latest~"$name" ; do
423	@@ -73 +73 @@
424	-          \${GS_BASE}/\${destrevision}/naclsdk_\${PLATFORM}_x86.\${suffix}
425	+          \${GS_BASE}/"\${destrevision}"/naclsdk_\${PLATFORM}_x86.\${suffix}
426	END
427      fi
428      # Patch sources and build the toolchains.
429      if [[ "$name" != "ppapi14" ]] || [[ "$3" != glibc ]]; then
430	make -C native_client/tools clean
431	rm -rf native_client/tools/SRC/*
432	for i in binutils gcc gdb glibc linux-headers-for-nacl newlib ; do (
433	  if [[ "$name" != "ppapi14" ]] || [[ "$i" != glibc ]]; then
434	    rm -rf native_client/tools/SRC/"$i"
435	    git clone ../"$i" native_client/tools/SRC/"$i"
436	    cd native_client/tools/SRC/"$i"
437	    . ../../REVISIONS
438	    declare varname="NACL_$(echo "$i" | LC_ALL=C tr a-z A-Z)_COMMIT"
439	    if [[ "$varname" = "NACL_LINUX-HEADERS-FOR-NACL_COMMIT" ]]; then
440	      . ../../../../../../REVISIONS
441	      git checkout "$LINUX_HEADERS_FOR_NACL_COMMIT"
442	    else
443	      git checkout "${!varname}"
444	    fi
445	    cd ../../../../..
446	    ( while read n id comment && [[ "$n" != "$name" ]]; do
447		: # Nothing
448	      done
449	      cd "$name/native_client/tools/SRC/$i"
450	      while read tag id comment ; do
451		if [[ "$i" = "$tag" ]]; then
452		  if [[ "$name" = ppapi1[4-8] ]] && [[ "$i" = "newlib" ]] &&
453		     [[ "$id" = "4353bc00936874bb78aa3ba21c648b4f4c3f946b" ]]; then
454		    # Ignore error
455		    git diff "$id"{^..,} | patch -p1 ||
456		    ( rejfiles="$(find -name '*.rej')"
457		      if [[ "$rejfiles" != "./newlib/libc/include/machine/setjmp.h.rej" ]]; then
458			touch "../../../../../$$.error" "../../../../../$$.error.$name"
459		      else
460			rm ./newlib/libc/include/machine/setjmp.h.rej
461		      fi
462		    )
463		  elif [[ "$name" = ppapi1[4-7] ]] &&
464		       [[ "$id" = "f96a3cbfb8777e1e47471b357929b8a1e3340a23" ]]; then
465		    patch -p0 <<-END
466			--- gcc/config/i386/nacl.h
467			+++ gcc/config/i386/nacl.h
468			@@ -269,3 +269,6 @@
469			 #define DWARF2_ADDR_SIZE \\
470			     (TARGET_NACL ? (TARGET_64BIT ? 8 : 4) : \\
471			                    (POINTER_SIZE / BITS_PER_UNIT))
472			+
473			+/* Profile counters are not available under Native Client. */
474			+#define NO_PROFILE_COUNTERS 1
475			END
476		  elif [[ "$name" = ppapi1[5-9] || "$name" == ppapi2[0-7] ]] &&
477		       [[ "$id" = "2324fd9e11f551e367cbe714ff49a4df3309396e" ]]; then
478		    for ldscript in elf{,64}_nacl.x{,.static,s}; do
479		      patch -p0 <<-END
480			--- nacl/dyn-link/ldscripts/$ldscript
481			+++ nacl/dyn-link/ldscripts/$ldscript
482			@@ -50 +50,6 @@
483			-  .note.gnu.build-id : { *(.note.gnu.build-id) } :seg_rodata
484			+  .note.gnu.build-id :
485			+  {
486			+    PROVIDE_HIDDEN (__note_gnu_build_id_start = .);
487			+    *(.note.gnu.build-id)
488			+    PROVIDE_HIDDEN (__note_gnu_build_id_end = .);
489			+  } :seg_rodata
490			END
491		    done
492		  elif [[ "$name" != ppapi1[5-8] ]] ||
493		       [[ "$id" != "8ec02f0e5af28bd478ce262f04d156e4ef09c4d9" ]]; then
494		    git diff "$id"{^..,} | patch -p1 ||
495		      touch "../../../../../$$.error" "../../../../../$$.error.$name"
496		  fi
497		fi
498	      done
499	    ) < "$1"
500	  fi
501	) done
502	if [[ "$name" == ppapi1[45] ]]; then
503	  patch -p0 <<-END
504	--- native_client/tools/Makefile
505	+++ native_client/tools/Makefile
506	@@ -747,6 +747,7 @@
507	 	  CC="\$\${CC}" \\
508	 	  LDFLAGS="-s" \\
509	 	  ../../SRC/gdb/configure \\
510	+	    --disable-werror \\
511	 	    --prefix=\$(SDKROOT) \\
512	 	    \$\${BUILD} \\
513	 	    --target=nacl
514	END
515	elif [[ "$name" == ppapi1[6-9] ]]; then
516	  patch -p0 <<-END
517	--- native_client/tools/Makefile
518	+++ native_client/tools/Makefile
519	@@ -747,6 +747,7 @@
520	 	  CC="\$\${CC}" \\
521	 	  LDFLAGS="-s" \\
522	 	  ../../SRC/gdb/configure \\
523	+	    --disable-werror \\
524	 	    --prefix=\$(PREFIX) \\
525	 	    \$\${BUILD} \\
526	 	    --target=nacl
527	END
528	else
529	  patch -p0 <<-END
530	--- native_client/tools/SRC/gdb/gdb/doc/Makefile.in
531	+++ native_client/tools/SRC/gdb/gdb/doc/Makefile.in
532	@@ -306 +306 @@
533	-	echo "@set GDBVN \`sed q \$(srcdir)/../version.in\`" > ./GDBvn.new
534	+	echo "@set GDBVN \`sed q \$(srcdir)/../version.inT\`" > ./GDBvn.new
535	END
536	fi
537	declare url_prefix=http://storage.googleapis.com/nativeclient-archive2
538	if [[ "$3" = "glibc" ]]; then
539	  declare url=$url_prefix/x86_toolchain/r"$rev"/toolchain_"$2"_x86.tar.gz
540	else
541	  declare url=$url_prefix/toolchain/"$rev"/naclsdk_"$2"_x86.tgz
542	fi
543	if [[ ! -e "../$$.error.$name" ]]; then
544	  # If toolchain is already available then another try will not change anything
545	  curl --fail --location --url "$url" -o /dev/null &&
546	  (cd .. ; sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") >"../$1.$name.lastver" 2>/dev/null &&
547	  rm -rf "../$name" ||
548	  (
549	    cd native_client
550	    export BUILD_COMPATIBLE_TOOLCHAINS=no
551	    export BUILDBOT_GOT_REVISION="$rev"
552	    if [[ "$2" = "win" ]]; then (
553	      # Use extended globbing (cygwin should always have it).
554	      shopt -s extglob
555	      # Filter out cygwin python (everything under /usr or /bin, or *cygwin*).
556	      export PATH=${PATH/#\/bin*([^:])/}
557	      export PATH=${PATH//:\/bin*([^:])/}
558	      export PATH=${PATH/#\/usr*([^:])/}
559	      export PATH=${PATH//:\/usr*([^:])/}
560	      export PATH=${PATH/#*([^:])cygwin*([^:])/}
561	      export PATH=${PATH//:*([^:])cygwin*([^:])/}
562	      python_slave buildbot/buildbot_selector.py
563	      (cd ../.. ; sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") >"../../$1.$name.lastver" 2>/dev/null
564	      rm -rf "../../$name"
565	    ) else
566	      # PPAPI14 to PPAPI30 were designed to be built on Ubuntu Lucid,
567	      # not on Ubuntu Precise
568	      if [[ "$name" == ppapi[12]? ]] || [[ "$name" == ppapi30 ]]; then
569		BUILDBOT_BUILDERNAME="${BUILDBOT_BUILDERNAME/precise64/lucid64}"
570	      fi
571	      python buildbot/buildbot_selector.py
572	      (cd ../.. ; sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") >"../../$1.$name.lastver" 2>/dev/null
573	      rm -rf "../../$name"
574	    fi
575	  )
576	  cd ..
577	fi
578      else
579	(cd .. ; sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") >"../$1.$name.lastver" 2>/dev/null &&
580	rm -rf "../$name"
581	cd ..
582      fi
583    ;;
584  esac
585done < "$1"
586
587(sha1sum "$scriptname" "$1" || shasum "$scriptname" "$1") > "$1.lastver" 2>/dev/null
588