aboutsummaryrefslogtreecommitdiffhomepage
path: root/dev/make-sdk-win32.sh
blob: 0112324d4dd4a5e41e1480a2cef592aab90ea89c (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash

# To run this script install cygwin by running setup-x86.exe from cygwin.com
# Install the standard packages plus wget.  Then run this script.

# Sworn by Enrico Tassi <enrico.tassi@inra.fr>
# Modified to support other directories and almost support spaces in paths
# by Jason Gross <jgross@mit.edu>
# License: Expat/MIT http://opensource.org/licenses/MIT

# This script reads the following environment variables:
# VERBOSE     - set to non-empty to have wget/this script be more verbose, for debugging purposes
# BASE        - set to non-empty to give a different location for the zip file, e.g., if /cygdrive/c is full or doesn't exist

set -e
if [ ! -z "$VERBOSE" ]
then
  set -x
fi

# Resources
ocaml=ocaml-4.01.0-i686-mingw64-installer3.exe
glib=base-windows-0.18.1.1.13.356@BUILD_ec06e9.txz
gtk=base-gtk-2.24.18.1.58@BUILD_594ca8.txz
lablgtk=lablgtk-2.18.0.tar.gz
camlp5=camlp5-6.11.tgz
nsis=nsis-2.46-setup.exe

ocaml_URL='http://yquem.inria.fr/~protzenk/caml-installer/'$ocaml
lablgtk_URL='https://forge.ocamlcore.org/frs/download.php/1261/'$lablgtk
glib_URL='http://dl.arirux.de/5/binaries32/'$glib
gtk_URL='http://dl.arirux.de/5/binaries32/'$gtk
camlp5_URL='http://pauillac.inria.fr/~ddr/camlp5/distrib/src/'$camlp5
nsis_URL='http://netcologne.dl.sourceforge.net/project/nsis/NSIS%202/2.46/'$nsis

cygwin=setup-${HOSTTYPE/i6/x}.exe
cygwin_URL='http://cygwin.com/'$cygwin
cygwin_PKGS=p7zip,zip,sed,make,mingw64-i686-gcc-g++,mingw64-i686-gcc-core,mingw64-i686-gcc,patch,rlwrap,libreadline6,diffutils

has_spaces() {
    test -z "$2"
}
# utilities
# http://www.dependencywalker.com/depends22_x86.zip

# The SDK itself
REVISION=85-1
# support for working on computers that don't have a C: drive
if [ -z "$BASE" ]
then
  TRUE_BASE=/cygdrive/c
else
  # get absolute path
  TRUE_BASE="$(readlink -f "$BASE")"
fi
BASE="$TRUE_BASE/CoqSDK-$REVISION"

if [ -z "$VERBOSE" ]
then
  WGET_ARGS="-N -q"
else
  WGET_ARGS="-N"
fi

# Windows has a version of FIND in C:/Windows/system32/find, and we don't want to use that
if [ -x /usr/bin/find ]
then
  FIND=/usr/bin/find
else
  echo "WARNING: /usr/bin/find does not exist.  Falling back on:"
  which find
  FIND=find
fi

WGET_ARGS="-N -q"

if [ "$(has_spaces $BASE; echo $?)" -ne 0 ]; then
  echo "ERROR: The current base directory ($BASE) has spaces."
  echo "ERROR: building lablgtk would fail."
  exit 1
fi

cyg_install() {
  if [ ! -e "$cygwin" ]; then wget $WGET_ARGS "$cygwin_URL"; fi
  chmod a+x "$cygwin"
  cygstart -w "$cygwin" -q -P $@
}

sanity_check() {
  echo "Check: wget."
  (which wget) || \
	  (echo "Please install wget using the cygwin installer and retry.";\
	   exit 1)
  echo "Check: 7z, gcc.  If it fails wait for cygwin to complete and retry"
  (which 7z && which i686-w64-mingw32-gcc) || \
	  (echo "Some cygwin package is not installed.";\
	   echo "Please wait for cygwin to finish and retry.";\
	   cyg_install $cygwin_PKGS;\
	   exit 1)
}

install_base() {
  echo "Setting up $BASE"
  rm -rf "$BASE"
  mkdir -p "$BASE"
}

make_environ() {
  echo "Setting up $BASE/environ"
  pushd "$BASE" >/dev/null
  cat > environ <<- EOF
	cyg_install() {
	  if [ ! -e "$cygwin" ]; then wget $WGET_ARGS "$cygwin_URL"; fi
	  chmod a+x "$cygwin"
	  cygstart -w "$cygwin" -q -P \$@
	}
	# Sanity checks: is the mingw64-i686-gcc package installed?
	(which i686-w64-mingw32-gcc && which make && which sed) || \\
		(echo "Some cygwin package is not installed.";\\
	         echo "Please wait for cygwin to finish and retry.";\\
	 	 cyg_install $cygwin_PKGS;\\
		 exit 1) || exit 1

	export BASE="\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
	export PATH="\$BASE/bin:\$PATH"
	export OCAMLLIB="\$(cygpath -m "\$BASE")/lib"
	export OCAMLFIND_CONF="\$(cygpath -m "\$BASE")/etc/findlib.conf"
	sed s"|@BASE@|\$(cygpath -m "\$BASE")|g" "\$BASE/lib/ld.conf.in" \\
	        > "\$BASE/lib/ld.conf"
	sed s"|@BASE@|\$(cygpath -m "\$BASE")|g" "\$BASE/lib/topfind.in" \\
	        > "\$BASE/lib/topfind"
	sed s"|@BASE@|\$(cygpath -m "\$BASE")|g" "\$BASE/etc/findlib.conf.in" \\
	        > "\$BASE/etc/findlib.conf"
	echo "Good.  You can now build Coq and Coqide from cygwin."
	EOF
  popd >/dev/null
}

download() {
  echo "Downloading some software:"
  if [ ! -e "$ocaml" ]; then
    echo "- downloading OCaml..."
    wget $WGET_ARGS "$ocaml_URL"
  fi
  chmod a+x "$ocaml"
  if [ ! -e "$lablgtk" ]; then
    echo "- downloading lablgtk..."
    wget $WGET_ARGS --no-check-certificate "$lablgtk_URL"
  fi
  if [ ! -e "$gtk" ]; then
    echo "- downloading gtk..."
    wget $WGET_ARGS "$gtk_URL"
  fi
  if [ ! -e "$glib" ]; then
    echo "- downloading glib..."
    wget $WGET_ARGS "$glib_URL"
  fi
  if [ ! -e "$camlp5" ]; then
    echo "- downloading camlp5..."
    wget $WGET_ARGS "$camlp5_URL"
  fi
  if [ ! -e "$nsis" ]; then
    echo "- downloading nsis..."
    wget $WGET_ARGS "$nsis_URL"
  fi
}

cleanup() {
  rm -rf tmp build
}

install_gtk() {
  echo "Installing $glib"
  tar -xJf "$glib" -C "$BASE"
  echo "Installing $gtk"
  tar -xJf "$gtk" -C "$BASE"
}

install_ocaml() {
  echo "Installing $ocaml"
  mkdir -p tmp
  7z -otmp x "$ocaml" >/dev/null
  cp -r tmp/\$_OUTDIR/bin "$BASE/"
  cp -r tmp/bin "$BASE/"
  cp -r tmp/\$_OUTDIR/lib "$BASE/"
  cp -r tmp/lib "$BASE/"
  cp -r tmp/\$_OUTDIR/etc "$BASE/"
  "$FIND" "$BASE" -name '*.dll' -o -name '*.exe' | tr '\n' '\0' \
	  | xargs -0 chmod a+x
  mv "$BASE/lib/topfind" "$BASE/lib/topfind.in"
  sed -i 's|@SITELIB@|@BASE@/lib/site-lib|g' "$BASE/lib/topfind.in"
  cat > "$BASE/lib/ld.conf.in" <<- EOF
	@BASE@/lib
	@BASE@/lib/stublibs
	EOF
  cat > "$BASE/etc/findlib.conf.in" <<- EOF
	destdir="@BASE@/lib/site-lib"
	path="@BASE@/lib/site-lib"
	stdlib="@BASE@/lib"
	ldconf="@BASE@/lib/ld.conf"
	ocamlc="ocamlc.opt"
	ocamlopt="ocamlopt.opt"
	ocamldep="ocamldep.opt"
	ocamldoc="ocamldoc.opt"
	EOF
  cp "$BASE/lib/topdirs.cmi" "$BASE/lib/compiler-libs/"
}

build_install_lablgtk() {
  echo "Building $lablgtk"
  mkdir -p build
  tar -xzf "$lablgtk" -C build
  cd build/lablgtk-*
  patch -p1 <<EOT
--- lablgtk-2.18.0/src/glib.mli	2013-10-01 01:31:50.000000000 -0700
+++ lablgtk-2.18.0.new/src/glib.mli	2013-12-06 11:57:34.203675200 -0800
@@ -75,6 +75,7 @@
   type condition = [ \`ERR | \`HUP | \`IN | \`NVAL | \`OUT | \`PRI]
   type id
   val channel_of_descr : Unix.file_descr -> channel
+  val channel_of_descr_socket : Unix.file_descr -> channel
   val add_watch :
     cond:condition list -> callback:(condition list -> bool) -> ?prio:int -> channel -> id
   val remove : id -> unit
--- lablgtk-2.18.0/src/glib.ml	2013-10-01 01:31:50.000000000 -0700
+++ lablgtk-2.18.0.new/src/glib.ml	2013-12-06 11:57:53.070804800 -0800
@@ -72,6 +72,8 @@
   type id
   external channel_of_descr : Unix.file_descr -> channel
     = "ml_g_io_channel_unix_new"
+  external channel_of_descr_socket : Unix.file_descr -> channel
+    = "ml_g_io_channel_unix_new_socket"
   external remove : id -> unit = "ml_g_source_remove"
   external add_watch :
     cond:condition list -> callback:(condition list -> bool) -> ?prio:int -> channel -> id
--- lablgtk-2.18.0/src/ml_glib.c	2013-10-01 01:31:50.000000000 -0700
+++ lablgtk-2.18.0.new/src/ml_glib.c	2013-12-10 02:03:33.940371800 -0800
@@ -25,6 +25,8 @@
 #include <string.h>
 #include <locale.h>
 #ifdef _WIN32
+/* to kill a #warning: include winsock2.h before windows.h */
+#include <winsock2.h>
 #include "win32.h"
 #include <wtypes.h>
 #include <io.h>
@@ -38,6 +40,11 @@
 #include <caml/callback.h>
 #include <caml/threads.h>

+#ifdef _WIN32
+/* for Socket_val */
+#include <caml/unixsupport.h>
+#endif
+
 #include "wrappers.h"
 #include "ml_glib.h"
 #include "glib_tags.h"
@@ -325,14 +332,23 @@

 #ifndef _WIN32
 ML_1 (g_io_channel_unix_new, Int_val, Val_GIOChannel_noref)
+CAMLprim value ml_g_io_channel_unix_new_socket (value arg1) {
+  return Val_GIOChannel_noref (g_io_channel_unix_new (Int_val (arg1)));
+}

 #else
 CAMLprim value ml_g_io_channel_unix_new(value wh)
 {
   return Val_GIOChannel_noref
-    (g_io_channel_unix_new
+    (g_io_channel_win32_new_fd
      (_open_osfhandle((long)*(HANDLE*)Data_custom_val(wh), O_BINARY)));
 }
+
+CAMLprim value ml_g_io_channel_unix_new_socket(value wh)
+{
+  return Val_GIOChannel_noref
+    (g_io_channel_win32_new_socket(Socket_val(wh)));
+}
 #endif

 static gboolean ml_g_io_channel_watch(GIOChannel *s, GIOCondition c,
EOT
  #sed -i s'/$PKG_CONFIG/"$PKG_CONFIG"/g' configure
  #sed -i s'/""$PKG_CONFIG""/"$PKG_CONFIG"/g' configure
  ./configure --disable-gtktest --prefix="$(cygpath -m "$BASE")" \
	  >log-configure 2>&1
  sed -i 's?\\?/?g' config.make
  make >log-make 2>&1
  make opt >>log-make 2>&1
  #echo "Testing $lablgtk"
  #cd src
  #./lablgtk2 ../examples/calc.ml
  #./lablgtk2 -all ../examples/calc.ml
  #cd ..
  echo "Installing $lablgtk"
  make install >>log-make 2>&1
  cd ../..
}

build_install_camlp5() {
  echo "Building $camlp5"
  mkdir -p build
  tar -xzf "$camlp5" -C build
  cd build/camlp5-*
  ./configure >log-configure 2>&1
  sed -i 's/EXT_OBJ=.obj/EXT_OBJ=.o/' config/Makefile
  sed -i 's/EXT_LIB=.lib/EXT_LIB=.a/' config/Makefile
  make world.opt >log-make 2>&1
  echo "Installing $camlp5"
  make install >>log-make 2>&1
  cd ../..
}

install_nsis() {
  echo "Installing $nsis"
  rm -rf tmp
  mkdir -p tmp
  7z -otmp x $nsis >/dev/null
  mkdir "$BASE/NSIS"
  cp -r tmp/\$_OUTDIR/* "$BASE/NSIS"
  rm -rf tmp/\$_OUTDIR
  rm -rf tmp/\$PLUGINSDIR
  cp -r tmp/* "$BASE/NSIS"
}

zip_sdk() {
  echo "Generating CoqSDK-${REVISION}.zip"
  here="`pwd`"
  cd "$BASE/.."
  rm -f "$here/CoqSDK-${REVISION}.zip"
  zip -q -r "$here/CoqSDK-${REVISION}.zip" "$(basename "$BASE")"
  cd "$here"
}

diet_sdk() {
  rm -rf "$BASE"/+*
  rm -rf "$BASE"/bin/camlp4*
  rm -rf "$BASE"/lib/camlp4/
  rm -rf "$BASE"/lib/site-lib/camlp4/
}

victory(){
  echo "Output file: CoqSDK-${REVISION}.zip "\
	  "(`du -sh CoqSDK-${REVISION}.zip | cut -f 1`)"
  echo "Usage: unpack and source the environ file at its root"
}

if [ -z "$1" ]; then
  sanity_check
  download
  cleanup
  install_base
  install_nsis
  install_ocaml
  install_gtk
  make_environ
  . "$BASE/environ"
  build_install_lablgtk
  build_install_camlp5
  diet_sdk
  make_environ
  zip_sdk
  cleanup
  victory
else
  # just one step
  $1
fi