Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / bin / add-cross
1 #!/usr/bin/env bash
2
3 set -e
4
5 # This script adds a new crosscompiler target for MLton.
6 #
7 # It takes four arguments.
8 #
9 # 1. <crossTarget>, which is what MLton would pass via the -b flag to the GCC
10 # cross-compiler tools. You don't need to have installed these tools in order
11 # to run this script, since it uses ssh and the native gcc on the target.
12 # Examples of $crossTarget are i386-pc-cygwin and sparc-sun-solaris.
13 #
14 # 2. <crossArch> specifies the target architecture.
15 #
16 # 3. <crossOS> specifies the target OS.
17 #
18 # 4. <machine> specifies a remote machine of the target type. This script
19 # will ssh to $machine to compile the runtime and to compile and run the
20 # program that will print the values of all the constants that the MLton
21 # basis library needs.
22 #
23 # Here are some example uses of this script.
24 #
25 # add-cross i386-pc-cygwin x86 cygwin cygwin
26 # add-cross sparc-sun-solaris sparc solaris blade
27 #
28 # (Here cygwin happens to be the name of my Cygwin machine and blade
29 # happens to be the name of my Sparc machine.)
30 #
31 # You also may need to set $libDir, which determines where the
32 # cross-compiler target will be installed.
33
34 die () {
35 echo >&2 "$1"
36 exit 1
37 }
38
39 usage () {
40 die "usage: $name <crossTarget> <crossArch> <crossOS> <machine>"
41 }
42
43 case "$#" in
44 4)
45 crossTarget="$1"
46 crossArch="$2"
47 crossOS="$3"
48 machine="$4"
49 ;;
50 *)
51 usage
52 ;;
53 esac
54
55 name=`basename "$0"`
56 original=`pwd`
57 dir=`dirname "$0"`
58 src=`cd "$dir/.." && pwd`
59
60 PATH="$src"/bin:$PATH
61
62 # libDir is the mlton lib directory where you would like the
63 # cross-compiler information to be installed. If you have installed
64 # from the rpms, this will usually be /usr/lib/mlton. You must have
65 # write permission there.
66
67 lib="$src/build/lib/mlton"
68
69 # You shouldn't need to change anything below this line.
70
71 rm -rf "$lib/targets/$crossTarget"
72 mkdir -p "$lib/targets/$crossTarget" || die "Cannot write to $lib."
73
74 tmp='/tmp/mlton-add-cross'
75
76 ( cd "$src" &&
77 mmake TARGET=$crossTarget TARGET_ARCH=$crossArch TARGET_OS=$crossOS \
78 dirs )
79
80 ssh $machine "rm -rf $tmp && mkdir $tmp"
81
82 echo "Copying files."
83 ( cd "$src" && tar cf - --exclude '*.o' --exclude '*.a' Makefile basis-library bin include runtime ) |
84 ssh $machine "cd $tmp && tar xf - &&
85 if [ ! $crossArch == \`./bin/host-arch\` ]; then echo $machine is \`./bin/host-arch\`, not $crossArch; exit 1; fi &&
86 if [ ! $crossOS == \`./bin/host-os\` ]; then echo $machine is \`./bin/host-os\`, not $crossOS; exit 1; fi"
87
88 echo "Making runtime on $machine."
89 ssh $machine "cd $tmp && ./bin/mmake CPPFLAGS=\"$CPPFLAGS\" LDFLAGS=\"$LDFLAGS\" COMPILE_FAST=yes OMIT_BYTECODE=yes clean dirs runtime"
90
91 ssh $machine "cd $tmp/build/lib/targets/self && tar cf - ." |
92 ( cd "$lib/targets/$crossTarget" && tar xf - )
93 ( cd "$src" &&
94 mmake TARGET=$crossTarget TARGET_ARCH=$crossArch TARGET_OS=$crossOS \
95 mlbpathmap )
96
97 case "$crossOS" in
98 mingw)
99 suf='.exe'
100 ;;
101 *)
102 suf=''
103 ;;
104 esac
105
106 # Copied from mlton-script
107 case "$crossArch" in
108 amd64)
109 archOpts='-m64'
110 ;;
111 hppa)
112 archOpts=''
113 ;;
114 ia64)
115 archOpts='-mlp64'
116 ;;
117 sparc)
118 archOpts='-m32'
119 ;;
120 x86)
121 archOpts=''
122 ;;
123 esac
124
125 case "$crossOS" in
126 aix)
127 osOpts='-maix64'
128 ;;
129 cygwin)
130 osOpts=''
131 ;;
132 darwin)
133 osOpts='-I/usr/local/include -I/opt/local/include -I/sw/include -L/usr/local/lib -L/opt/local/lib -L/sw/lib'
134 ;;
135 freebsd)
136 osOpts='-I/usr/local/include -L/usr/local/lib/'
137 ;;
138 hurd)
139 osOpts=''
140 ;;
141 hpux)
142 osOpts=''
143 ;;
144 linux)
145 osOpts=''
146 ;;
147 mingw)
148 libs='-lws2_32 -lkernel32 -lpsapi -lnetapi32 -lwinmm'
149 ;;
150 netbsd)
151 osOpts='-I/usr/pkg/include -Wl,-R/usr/pkg/lib -L/usr/pkg/lib/'
152 ;;
153 openbsd)
154 osOpts='-I/usr/local/include -L/usr/local/lib/'
155 ;;
156 solaris)
157 osOpts='-lnsl -lsocket -lrt'
158 ;;
159 esac
160
161 exe='print-constants'
162 echo "Compiling and running print-constants on $machine."
163 "$src/build/bin/mlton" -target $crossTarget -build-constants true |
164 ssh $machine "cd $tmp/runtime &&
165 cat >$exe.c &&
166 gcc $archOpts $osOpts $CPPFLAGS -I. -o $exe $exe.c libmlton.a libgdtoa.a $LDFLAGS -lgmp -lm"
167 ssh $machine "$tmp/runtime/$exe$suf" >"$lib/targets/$crossTarget/constants"
168 ssh $machine "rm -rf $tmp"