OSDN Git Service

Add MS7619SE
[uclinux-h8/uClinux-dist.git] / bin / mk-disttools-install
1 #!/bin/sh
2 #
3 # Convert a /usr/local toolchain to a /usr/local/uclinux-dist toolchain
4 # installer that is date stamped appropriately.
5 #
6 # David McCullough <ucdevel@gmail.com>
7 #
8
9 if [ $# -lt 3 -o $# -gt 4 -o ! -f "$1" ]
10 then
11         echo "usage: `basename $0` <tar-gz from / of usr local chain> <build-date> <cross> [prefix]"
12         echo "       for example:"
13         echo "       `basename $0` arm-linux-tools-20070808.tar.gz 20070808 arm-linux"
14         echo "       `basename $0` mips-linux-tools-20130305.tar.gz 20130305 mips-linux mips-unknown-linux-gnu"
15         echo ""
16         echo "cross  - new tools will be called cross-date-gcc"
17         echo "prefix - if existing prefix is different to cross, otherwise"
18         echo "         it defaults to cross"
19         exit 1
20 fi
21
22 ARCHIVE="$1"
23 DATE="$2"
24 CROSS="$3"
25 PREFIX="${4:-$CROSS}"
26 EXTRACT_DIR=/usr/local/uclinux-dist
27 LINK_DIR=/usr/local/bin
28 CLEAN_OLD=n
29
30 #
31 # helper to extract tar.gz from archive
32 #
33 # $1 = archive
34 #
35
36 cat_archive()
37 {
38         case "$1" in
39         *.sh)
40                 SKIP=`awk '/^__ARCHIVE_FOLLOWS__/ { print NR + 1; exit 0; }' "$1"`
41                 tail -n +$SKIP "$1"
42                 ;;
43         *)
44                 cat "$1"
45                 ;;
46         esac
47 }
48
49 #
50 # lets check the archive is pure ;-)
51 #
52
53 CHECK="/tmp/$$.check"
54 cat_archive "$ARCHIVE" | tar tvzf - > $CHECK
55 trap "rm -f $CHECK" 0
56
57 if egrep -v '(usr/local|usr/$)' $CHECK
58 then
59         echo "Found files outside usr/local !"
60         exit 1
61 fi
62 rm -f $CHECK
63 trap "" 0
64
65 #
66 # Now we generate a script to install this appropriately
67 #
68
69 INSTALL_FILE="$CROSS-tools-$DATE.sh"
70
71 cat <<!EOF > $INSTALL_FILE
72 #!/bin/sh
73
74 EXTRACT_DIR=$EXTRACT_DIR
75 LINK_DIR=$LINK_DIR
76 CLEAN_OLD=$CLEAN_OLD
77 CCACHE_OPTION=y
78
79 SCRIPT="\$0"
80 case "\${SCRIPT}" in
81 /*)
82         ;;
83 *)
84         if [ -f "\${SCRIPT}" ]
85         then
86                 SCRIPT="\`pwd\`/\${SCRIPT}"
87         else
88                 SCRIPT="\`which \${SCRIPT}\`"
89         fi
90         ;;
91 esac
92
93 cd /
94
95 if [ ! -f "\${SCRIPT}" ]
96 then
97         echo "Cannot find the location of the install script (\$SCRIPT) ?"
98         exit 1
99 fi
100
101 SKIP=\`awk '/^__ARCHIVE_FOLLOWS__/ { print NR + 1; exit 0; }' \${SCRIPT}\`
102
103 #
104 # find out where to put the buld of the archive
105 #
106 while :; do
107         echo -n "Where would you like these tools installed? [\$EXTRACT_DIR]: "
108         read t
109         case "\$t" in
110         "") break ;;
111         *)
112                 if [ -w \`basename \$t\` -o -w \$t ]; then
113                         EXTRACT_DIR=\$t
114                         break
115                 fi
116                 echo "Neither \`basename \$t\` or \$t are writable"
117                 ;;
118         esac
119 done
120
121 #
122 # and where we need to install the "compiler" executables for running
123 #
124 while :; do
125         echo -n "Install executable links into ? [\$LINK_DIR]: "
126         read t
127         case "\$t" in
128         "") break ;;
129         *)
130                 if [ -w \`basename \$t\` -o -w \$t ]; then
131                         LINK_DIR=\$t
132                         break
133                 fi
134                 echo "Neither \`basename \$t\` or \$t are writable"
135                 ;;
136         esac
137 done
138
139 #
140 # clean out old or possibly conflicting tools chains ?
141 #
142 while :; do
143         echo "***** Warning - removal may break other non SG packaged tool chains"
144         echo -n "Remove old or possible conflicting toolchains/files ? [\$CLEAN_OLD]: "
145         read t
146         case "\$t" in
147         "") break ;;
148         y|Y|[yY][eE][sS]) CLEAN_OLD=y; break ;;
149         n|N|[nN][oO])     CLEAN_OLD=n; break ;;
150         *) echo "Either Y or N will do :-)" ;;
151         esac
152 done
153
154 #
155 # if they have ccache install,  let them use it by default :-)
156 #
157 if [ -x "\`which ccache\`" ]; then
158         echo -n "You have ccache installed, always use it? [\$CCACHE_OPTION]: "
159         read t
160         case "\$t" in
161         "") break ;;
162         y|Y|[yY][eE][sS]) CCACHE_OPTION=y; break ;;
163         n|N|[nN][oO])     CCACHE_OPTION=n; break ;;
164         *) echo "Either Y or N will do :-)" ;;
165         esac
166
167 fi
168 if [ "\$CCACHE_OPTION" = y ]; then
169         CCACHE="\`which ccache\`"
170 else
171         CCACHE=""
172 fi
173
174 #
175 # Now clean up old conflicting tool chains and directories
176 #
177 if [ "\$CLEAN_OLD" = "y" ]; then
178         rm -rf "/usr/local/${CROSS}"
179         rm -rf "/usr/local/lib/gcc-lib/${CROSS}"
180         rm -rf "/usr/local/lib/gcc/${CROSS}"
181         rm -rf "/usr/local/libexec/gcc/${CROSS}"
182         rm -rf /usr/local/include/g++-v3
183         rm -rf /usr/local/include/c++
184         for i in /usr/local/bin/${CROSS}-*
185         do
186                 case "\$i" in
187                 /usr/local/bin/${CROSS}-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-*)
188                         echo "\$i is ok, leaving..."
189                         ;;
190                 *)
191                         echo "\$i is a conflict, removing..."
192                         rm -f "\$i"
193                         ;;
194                 esac
195         done
196 fi
197
198 #
199 # extract the toolchain and install the executables appropriately
200 #
201
202 [ -d \$EXTRACT_DIR ] || mkdir -p \$EXTRACT_DIR
203 if [ ! -d \$EXTRACT_DIR ]; then
204         echo "Cannot make directory \$EXTRACT_DIR"
205         exit 1
206 fi
207 rm -rf \$EXTRACT_DIR/${CROSS}-tools-${DATE}
208 if [ -d \$EXTRACT_DIR/${CROSS}-tools-${DATE} ]; then
209         echo "Cannot remove \$EXTRACT_DIR/${CROSS}-tools-${DATE}"
210         exit 1
211 fi
212 mkdir -p \$EXTRACT_DIR/${CROSS}-tools-${DATE}
213 if [ ! -d \$EXTRACT_DIR/${CROSS}-tools-${DATE} ]; then
214         echo "Cannot make directory \$EXTRACT_DIR/${CROSS}-tools-${DATE}"
215         exit 1
216 fi
217
218 (
219         cd \$EXTRACT_DIR/${CROSS}-tools-${DATE} || exit 1
220         tail -n +\${SKIP} \${SCRIPT} | gunzip | tar xf -
221 )
222
223 if [ ! -d \$EXTRACT_DIR/${CROSS}-tools-${DATE}/usr ]; then
224         "Extraction failed,  no usr directory created in target"
225         exit 1
226 fi
227
228 #
229 # move the binaries to a nicer location
230 #
231 for i in \$EXTRACT_DIR/${CROSS}-tools-${DATE}/usr/local/*
232 do
233         mv "\$i" \$EXTRACT_DIR/${CROSS}-tools-${DATE}/.
234 done
235 rm -rf \$EXTRACT_DIR/${CROSS}-tools-${DATE}/usr
236
237 #
238 # create the executables
239 #
240 for i in \$EXTRACT_DIR/${CROSS}-tools-${DATE}/bin/*
241 do
242         BASE="\`basename \$i\`"
243         BASE="\`echo \$BASE | sed 's/^${PREFIX}-//'\`"
244         rm -f \$LINK_DIR/${CROSS}-${DATE}-\$BASE
245         echo "#!/bin/sh" >> \$LINK_DIR/${CROSS}-${DATE}-\$BASE
246         if [ "\$CCACHE" ]; then
247                 case "\$BASE" in
248                 *gcc|*g++|*c++)
249                         echo "exec \$CCACHE \$i \\"\\\$@\\"" >> \
250                                 \$LINK_DIR/${CROSS}-${DATE}-\$BASE
251                         ;;
252                 esac
253         fi
254         # fallback to non cache in case ccache is removed
255         printf "exec \$i \\"\\\$@\\"" >> \$LINK_DIR/${CROSS}-${DATE}-\$BASE
256         chmod 755 \$LINK_DIR/${CROSS}-${DATE}-\$BASE
257 done
258
259 exit 0
260 __ARCHIVE_FOLLOWS__
261 !EOF
262
263 cat_archive "$ARCHIVE" >> $INSTALL_FILE
264 chmod +x $INSTALL_FILE
265