aboutsummaryrefslogtreecommitdiff
path: root/cil32-crosstool.sh
blob: 73b53eb30d9c11da885e0ce6c4c7b4bf71c17758 (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
#!/bin/bash


INSTALL_BINUTILS=yes
INSTALL_GCC=yes

TARGET=cil32

BUILD_DIR=${BUILD_DIR-`pwd`}
SOURCE_DIR=${SOURCE_DIR-`dirname $0`}
PREFIX=${PREFIX-`pwd`/newbuild}
LANGUAGES=c

_help()
{
    echo "Options:"
    echo "         -src        : dir with the sources (default ${SOURCE_DIR})"
    echo "         -build      : dir where to the the build (default ${BUILD_DIR})"
    echo "         -prefix     : dir where to install (default ${PREFIX})"
    echo "         -binutils   : install of binutils (default)"
    echo "         -nobinutils : skip install of binutils"
    echo "         -gcc        : gcc build and installation (default)"
    echo "         -nogcc      : skip gcc build and installation"
    echo "         -cpp        : build also C++ compiler"
    echo "         -help       : this message"
}

# Create directories
_prepare_dirs()
{
    mkdir -p ${PREFIX}
    mkdir -p ${PREFIX}/bin
    mkdir -p ${PREFIX}/include
    mkdir -p ${PREFIX}/lib
    # SYSROOT must be = ${PREFIX}/${TARGET}
    mkdir -p ${SYSROOT}
    mkdir -p ${SYSROOT}/bin
    ( cd ${SYSROOT} ; ln -fs ../include . ) 
    ( cd ${SYSROOT} ; ln -fs ../lib . )  
}

# Install Binutils
_binutils()
{
    CIL_AS=${CIL_AS-`which ilasm`}
    CIL_LD=${CIL_LD-`which ilalink`}

    echo "Tools:"
    echo "  CIL ASSEMBLER: " ${CIL_AS}
    echo "  CIL LINKER:    " ${CIL_LD}

    echo ""
    echo "Installing Bin Utils"

    cat > ${SYSROOT}/bin/tool_not_exist <<_EOF_
#!/bin/sh
echo "Tool " \`basename \$0\` " not provided"
exit 1
_EOF_
    chmod 755 ${SYSROOT}/bin/tool_not_exist

    cat > ${SYSROOT}/bin/as <<_EOF_
#!/bin/sh

${CIL_AS} "\$@" 
_EOF_
    chmod 755 ${SYSROOT}/bin/as

    cat > ${SYSROOT}/bin/ld <<_EOF_
#!/bin/sh

PARAM=

for i in "\$@" ; do
    case "\$i" in
        -lm)
         ;;
        /tmp*)
            PARAM="\${PARAM} /\${i}";
         ;;
        /exchange*)
            PARAM="\${PARAM} /\${i}";
         ;;
        *)
            PARAM="\${PARAM} \${i}";
         ;;
    esac
done

${CIL_LD} \$PARAM

_EOF_
    chmod 755 ${SYSROOT}/bin/ld

    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/addr2line
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/ar
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/nm
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/objcopy
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/objdump
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/ranlib
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/readelf
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/size
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/strings
    cp ${SYSROOT}/bin/tool_not_exist ${SYSROOT}/bin/strip


    cp ${SYSROOT}/bin/addr2line ${PREFIX}/bin/${TARGET}-addr2line
    cp ${SYSROOT}/bin/ar        ${PREFIX}/bin/${TARGET}-ar
    cp ${SYSROOT}/bin/as        ${PREFIX}/bin/${TARGET}-as
    cp ${SYSROOT}/bin/ld        ${PREFIX}/bin/${TARGET}-ld
    cp ${SYSROOT}/bin/nm        ${PREFIX}/bin/${TARGET}-nm
    cp ${SYSROOT}/bin/objcopy   ${PREFIX}/bin/${TARGET}-objcopy
    cp ${SYSROOT}/bin/objdump   ${PREFIX}/bin/${TARGET}-objdump
    cp ${SYSROOT}/bin/ranlib    ${PREFIX}/bin/${TARGET}-ranlib
    cp ${SYSROOT}/bin/readelf   ${PREFIX}/bin/${TARGET}-readelf
    cp ${SYSROOT}/bin/size      ${PREFIX}/bin/${TARGET}-size
    cp ${SYSROOT}/bin/strings   ${PREFIX}/bin/${TARGET}-strings
    cp ${SYSROOT}/bin/strip     ${PREFIX}/bin/${TARGET}-strip
    
    echo ""
    echo "Installing vm wrappers"

    cat > ${PREFIX}/bin/${TARGET}-ilrun <<_EOF_
#!/bin/sh

basedir=\`dirname \$0\`
basedir=\`cd \$basedir/.. ; pwd\`

ilrun -L \$basedir/lib "\$@"
_EOF_
    chmod 755 ${PREFIX}/bin/${TARGET}-ilrun

    cat > ${PREFIX}/bin/${TARGET}-mono <<_EOF_
#!/bin/sh

basedir=\`dirname \$0\`
basedir=\`cd \$basedir/.. ; pwd\`

MONO_PATH=\$basedir/lib mono "\$@" 
_EOF_
    chmod 755 ${PREFIX}/bin/${TARGET}-mono
}

# Configure, build and install gcc

_gcc()
{
    echo ""
    echo "Configuring GCC"

    mkdir -p ${BUILD_DIR}/build-gcc
    cd ${BUILD_DIR}/build-gcc

    ${SOURCE_DIR}/configure --target=${TARGET} \
                            --enable-languages=${LANGUAGES} \
                            --disable-bootstrap \
                            --prefix=${PREFIX} \
                            --with-local-prefix=${SYSROOT}

    echo ""
    echo "Building GCC"
    make all

    echo ""
    echo "Installing GCC"
    make install
}

while [ $# -gt 0 ] ; do

if    [ "x$1" == x-help ] ; then
    _help ;
    exit 0
elif  [ "x$1" == x-cpp ] ; then
    LANGUAGES=c,c++
elif  [ "x$1" == x-nobinutils ] ; then
    INSTALL_BINUTILS=no
elif  [ "x$1" == x-nogcc ] ; then
    INSTALL_GCC=no
elif  [ "x$1" == x-binutils ] ; then
    INSTALL_BINUTILS=yes
elif  [ "x$1" == x-gcc ] ; then
    INSTALL_GCC=yes
elif  [ "x$1" == x-src ] ; then
    SOURCE_DIR=$2
    shift 1
elif  [ "x$1" == x-build ] ; then
    BUILD_DIR=$2
    shift 1
elif  [ "x$1" == x-prefix ] ; then
    PREFIX=$2
    shift 1
else
    echo "Unrecognized option: " $1
    _help ;
    exit 1
fi

shift 1
done

SOURCE_DIR=`cd $SOURCE_DIR; pwd`
SYSROOT=${PREFIX}/${TARGET}

echo "DIRECTORIES:"
echo "  BUILD_DIR:  " ${BUILD_DIR}
echo "  SOURCE_DIR: " ${SOURCE_DIR}
echo "  SYSROOT:    " ${SYSROOT}
echo "  PREFIX:     " ${PREFIX}

PATH="${PREFIX}/bin:${PATH}"
export PATH

_prepare_dirs

if test "x${INSTALL_BINUTILS}" == "xyes"
then
    _binutils
fi


if test "x${INSTALL_GCC}" == "xyes"
then
    _gcc
fi