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
6declare NSIS=0 verboase=0
7
8while getopts "nv" flag ; do
9  case "$flag" in
10    n) NSIS=1 ;;
11    v) verbose=1 ;;
12    ?) cat <<END
13  Usage: $0 [-n] [-v]
14  Flags:
15    -n: run NSIS to create actual installer
16    -v: be more verbose when processing data
17    Place CygWin decription file (setup.ini) in current directory, run script.
18    You'll get:
19      1. Downloaded binary files in subdirectory "packages"
20      2. Downloaded source files in subdirectory "packages.src"
21      3. Unpacked files in subdirectory "packages.unpacked"
22      4. Setup log files in subdirectory "setup"
23      5. Ready to use file make_installer.nsi for NSIS installer
24   setup.ini is here: http://mirrors.kernel.org/sourceware/cygwin/setup.ini
25   It's not downloaded by script to keep it hermetic.
26
27    NSIS file format is described here: http://nsis.sourceforge.net/Docs
28END
29      exit 1;;
30  esac
31done
32
33declare CygWin=0
34if [[ "`uname -s`" != "Darwin" ]] ; then
35  if [[ "`uname -o`" = "Cygwin" ]] ; then
36    CygWin=1
37    declare need_restart=0
38    if ((BASH_VERSINFO[0]<4)) ; then
39      need_restart=1
40    fi
41    if ((NSIS)) && ! [ -x NSIS/makensis.exe ] && ! [ -x /bin/7z ] ; then
42      need_restart=1
43    fi
44  fi
45fi
46
47# Can only happen on CygWin - we don't autoinstall tools on other platforms
48if ((need_restart)) ; then
49  if ! [ -x "$PWD/hermetic_cygwin/bin/7z" ] && ! [ -x "$PWD/hermetic_cygwin/bin/7z.exe" ] ; then
50    wget https://storage.googleapis.com/nativeclient-mirror/nacl/cygwin_mirror/hermetic_cygwin_1_7_9-0_1.exe -O cygwin_mini_setup.exe
51    chmod a+x cygwin_mini_setup.exe
52    "`cygpath $COMSPEC`" /C start /WAIT ".\\cygwin_mini_setup" /CYGPORT /S "/D=`cygpath -w $PWD/hermetic_cygwin`"
53  fi
54  exec "`cygpath $COMSPEC`" /C "`cygpath -w $PWD/hermetic_cygwin/bin/bash`" "`cygpath -w $0`" "$@"
55fi
56
57if ((BASH_VERSINFO[0]<4)) ; then
58  echo "You need Bash4 to use this script" >&2
59  exit 1
60fi
61
62if ((NSIS)) && ((CygWin)) && ! [ -d NSIS ] ; then
63  7z -oNSIS x ../../../third_party/NSIS/nsis-2.46-Unicode-setup.exe
64  ln -sfn NSIS AccessControl
65  7z x ../../../third_party/NSIS/AccessControl.zip
66  rm AccessControl
67  mkdir -p NSIS/Contrib/Graphics/{Checks,Header,Icons,Wizard}
68  for dirname in Checks Header Icons Wizard ; do
69    mv NSIS/\$_OUTDIR/$dirname/* NSIS/Contrib/Graphics/$dirname
70  done
71  rmdir NSIS/\$_OUTDIR/{Checks,Header,Icons,Wizard,}
72  mkdir "NSIS/Docs/Modern UI/images"
73  ln "NSIS/Docs/Modern UI 2/images"/* "NSIS/Docs/Modern UI/images"
74  mv NSIS/\$PLUGINSDIR/modern-header.bmp NSIS/Contrib/Graphics/Header/nsis.bmp
75  mv NSIS/\$PLUGINSDIR/modern-wizard.bmp NSIS/Contrib/Graphics/Wizard/nsis.bmp
76  mv NSIS/\$PLUGINSDIR/*.dll NSIS/Plugins
77  rmdir NSIS/\$PLUGINSDIR
78  chmod a+x NSIS/{,Bin,Contrib/UIs}/*.exe
79  mkdir -p "MkLink/nsis"
80  cp -aiv "NSIS/Examples/Plugin/nsis/"* "Mklink/nsis"
81  cp -aiv "MkLink/Release Unicode/MkLink.dll" "NSIS/Plugins"
82fi
83
84declare -A description packages
85. "${0/.sh/.conf}"
86. "`dirname \"$0\"`"/make_installer.inc
87
88CYGWIN_VERSION=1.7.24-1.0
89
90mkdir -p packages{,.src,.unpacked} setup
91
92parse_setup_ini
93fix_setup_inf_info
94download_package_dependences bash 0
95reqpackages=()
96sectionin=()
97allinstpackages=()
98allinstalledpackages=()
99rm setup/*.lst.gz
100download_package "Base" "`seq -s ' ' \"$((${#packages[@]}+3))\"`"
101download_addon_packages 2
102if ((include_all_packages)) ; then
103  download_all_packages 1
104else
105  for pkgname in "${!sectionin[@]}" ; do
106    sectionin["$pkgname"]=" 1${sectionin[$pkgname]}"
107  done
108  for pkgname in "${!seed[@]}" ; do
109    seed["$pkgname"]=" 1${seed[$pkgname]}"
110  done
111fi
112fill_required_packages
113fill_filetype_info
114
115(
116  cat <<END
117RequestExecutionLevel user
118SetCompressor $compressor
119SetCompressorDictSize 128
120Name "Hermetic CygWin"
121OutFile hermetic_cygwin_${CYGWIN_VERSION//./_}.exe
122END
123  declare_nsis_variables
124  cat <<END
125
126InstallDir "c:\\cygwin"
127
128!include "MUI2.nsh"
129!include "Sections.nsh"
130
131!define MUI_HEADERIMAGE
132!define MUI_WELCOMEFINISHPAGE_BITMAP "\${NSISDIR}\\Contrib\\Graphics\\Wizard\\win.bmp"
133
134!define MUI_WELCOMEPAGE_TITLE "Welcome to Hermetic CygWin"
135!define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the installation of Hermetic CygWin $CYGWIN_VERSION.\$\\r\$\\n\$\\r\$\\nHermetic CygWin is simple and reliable way to install pre-defined version of CygWin.\$\\r\$\\n\$\\r\$\\n\$_CLICK"
136
137!define MUI_COMPONENTSPAGE_SMALLDESC
138
139!insertmacro MUI_PAGE_WELCOME
140!insertmacro MUI_PAGE_COMPONENTS
141!insertmacro MUI_PAGE_DIRECTORY
142!insertmacro MUI_PAGE_INSTFILES
143
144!define MUI_FINISHPAGE_LINK "Visit the Native Client site for news, FAQs and support"
145!define MUI_FINISHPAGE_LINK_LOCATION "http://code.google.com/chrome/nativeclient"
146
147!insertmacro MUI_PAGE_FINISH
148
149!insertmacro MUI_LANGUAGE "English"
150
151Section "" sec_Preinstall
152  SectionIn `seq -s ' ' $((${#packages[@]}+3))`
153  Push \$R0
154  CreateDirectory "\$INSTDIR"
155  ; Owner can do anything
156  AccessControlW::GrantOnFile "\$INSTDIR" "(S-1-3-0)" "FullAccess"
157  ; Group can read
158  AccessControlW::GrantOnFile "\$INSTDIR" "(S-1-3-1)" "Traverse + GenericRead"
159  ; "Everyone" can read too
160  AccessControlW::GrantOnFile "\$INSTDIR" "(S-1-1-0)" "Traverse + GenericRead"
161  CreateDirectory "\$INSTDIR\\etc"
162  CreateDirectory "\$INSTDIR\\etc\\setup"
163  FileOpen \$R0 \$INSTDIR\\postinstall.sh w
164  FileWrite \$R0 'export PATH=/usr/local/bin:/usr/bin:/bin\$\nexport CYGWIN="\$\$CYGWIN nodosfilewarning"\$\n'
165  FileClose \$R0
166  FileOpen \$R0 \$INSTDIR\\etc\\setup\\installed.log w
167  FileWrite \$R0 "INSTALLED.DB 2\$\n"
168  FileClose \$R0
169  Pop \$R0
170SectionEnd
171END
172  generate_section_list
173  cat <<END
174Section "" sec_PostInstall
175  SectionIn `seq -s ' ' $((${#packages[@]}+3))`
176  Push \$R0
177  Push \$R1
178  FileOpen \$R0 \$INSTDIR\\postinstall.sh a
179  FileSeek \$R0 0 END
180  FileWrite \$R0 "/bin/sort /etc/setup/installed.log -o /etc/setup/installed.db\$\nrm /etc/setup/installed.log\$\n"
181  FileClose \$R0
182  SetOutPath \$INSTDIR
183  nsExec::ExecToLog '"bin\\bash" -c ./postinstall.sh'
184  nsExec::ExecToLog '"bin\dash" -c "bin/rebaseall -v"'
185  Delete \$INSTDIR\\postinstall.sh
186  FileOpen \$R0 \$INSTDIR\\Cygwin.bat w
187  StrCpy \$R1 \$INSTDIR 1
188  FileWrite \$R0 "@echo off\$\r\$\n\$\r\$\n\$R1:$\r\$\nchdir \$INSTDIR\\bin\$\r\$\nbash --login -i$\r\$\n"
189  FileClose \$R0
190  Pop \$R1
191  Pop \$R0
192SectionEnd
193END
194  generate_init_function 2
195  generate_onselchange_function
196) > make_hermetic_cygwin.nsi
197# Replace symlinks with hardlinks for python and gawk
198if ! patch --no-backup-if-mismatch <<END
199--- make_hermetic_cygwin.nsi
200+++ make_hermetic_cygwin.nsi
201@@ -2295,2 +2295,2 @@
202   MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\gawk.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\gawk-4.1.0.exe"
203-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\awk.exe" "gawk.exe"
204+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\awk.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\gawk-4.1.0.exe"
205@@ -5370,6 +5370,7 @@
206   CreateDirectory "\$INSTDIR\\usr"
207   CreateDirectory "\$INSTDIR\\bin"
208   CreateDirectory "\$INSTDIR\\bin-unrebased"
209+  CreateDirectory "\$INSTDIR\\dev"
210   CreateDirectory "\$INSTDIR\\usr\\share"
211   CreateDirectory "\$INSTDIR\\usr\\share\\doc"
212   CreateDirectory "\$INSTDIR\\usr\\share\\doc\\bash"
213@@ -21338,3 +21338,4 @@
214-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python.exe" "python2.7.exe"
215-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python-config" "python2.7-config"
216-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}lib\\libpython2.7.dll.a" "python2.7\\config\\libpython2.7.dll.a"
217+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python2.7.exe"
218+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python2.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python2.7.exe"
219+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python-config" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python2.7-config"
220+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}lib\\libpython2.7.dll.a" "\$INSTDIR\\${CYGWIN_PREFIX}lib\\python2.7\\config\\libpython2.7.dll.a"
221@@ -34953,6 +34953,10 @@
222-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\pydoc3" "pydoc3.2"
223-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.exe" "python3.2m.exe"
224-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3-config" "python3.2m-config"
225-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.2.exe" "python3.2m.exe"
226-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}lib\\libpython3.2m.dll.a" "python3.2\\config-3.2m\\libpython3.2m.dll.a"
227-  MkLink::SoftF "\$INSTDIR\\${CYGWIN_PREFIX}lib\\pkgconfig\\python3.pc" "python-3.2.pc"
228+  IntCmp \$PKV_python 1 L1_NoInstallPythonHardlink +1 L1_NoInstallPythonHardlink
229+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.2m.exe"
230+L1_NoInstallPythonHardlink:
231+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\pydoc3" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\pydoc3.2"
232+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.2m.exe"
233+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3-config" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.2m-config"
234+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.2.exe" "\$INSTDIR\\${CYGWIN_PREFIX}bin\\python3.2m.exe"
235+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}lib\\libpython3.2m.dll.a" "\$INSTDIR\\${CYGWIN_PREFIX}lib\\python3.2\\config-3.2m\\libpython3.2m.dll.a"
236+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}lib\\pkgconfig\\python.pc" "\$INSTDIR\\${CYGWIN_PREFIX}lib\\pkgconfig\\python-3.2.pc"
237+  MkLink::Hard "\$INSTDIR\\${CYGWIN_PREFIX}lib\\pkgconfig\\python3.pc" "\$INSTDIR\\${CYGWIN_PREFIX}lib\\pkgconfig\\python-3.2.pc"
238END
239  then
240    exit 1
241fi
242if ((NSIS)) ; then
243  if [ -e NSIS/makensis.exe ] ; then
244    NSIS/makensis.exe /V2 make_hermetic_cygwin.nsi
245  else
246    makensis /V2 make_hermetic_cygwin.nsi
247  fi
248fi
249