]> hackdaworld.org Git - hdw-linux/hdw-linux.git/blob - scripts/Build
fixed ftp location
[hdw-linux/hdw-linux.git] / scripts / Build
1 #! /bin/bash
2 #
3 # author: hackbard@hackdaworld.dyndns.org
4 #
5 # this script is used to build hdw-linux packages. 
6 #
7
8 hdw_status=""
9 package=""
10 no_src_del=""
11 priority=""
12 dir="*"
13
14 while [ "$1" ] ; do
15         case "$1" in 
16                 -stage)         hdw_status=$2   ; shift 2 ;;
17                 -package)       package=$2      ; shift 2 ;;
18                 -no_src_del)    no_src_del="1"  ; shift 1 ;;
19                 -priority)      priority=$2     ; shift 2 ;;
20                 -dir)           dir=$2          ; shift 2 ;;
21                 *)
22                                 echo
23                                 echo "Usage:"
24                                 echo
25                                 echo -e " ./scripts/Build \t-stage stagelevel"
26                                 echo -e "\t\t\t-package package-name"
27                                 echo
28                                 exit 1 ;;
29         esac
30 done
31
32 if [ -z "$package" ]; then
33         echo "no package specified, abort."
34         exit 1
35 fi
36
37 if [ -z "$hdw_status" ]; then
38         echo "stage level not specified. something went wrong ... hey,"
39         echo "this is a development version ;)"
40         exit 1
41 fi
42
43 if [ -z "$priority" ] ; then
44         echo "no priority specified, abort."
45         exit 1
46 fi
47 export priority
48
49 # load config andsubroutines
50 . ./scripts/Config -stage $hdw_status
51 . ./scripts/subroutines
52 # optimization
53 . ./scripts/optimization
54
55 # check for dietlibc
56 if [ "$hdw_status" != "1" -a "$hdw_use_dietlibc" = "1" ] ; then
57         export CC="diet-dyn $CC -nostdinc"
58         export CXX="diet-dyn $CXX -nostdinc"
59 fi
60
61 # okay, this is going to be fun - happy h4ck1n6
62 #
63
64 # defining build environment variables to build a package.
65 if [ "$dir" = "*" ] ; then
66         dirs="`ls ./packages/*/$package/$package`"
67         if [ "`echo $dirs | awk '{ print NF }'`" -gt "1" ] ; then
68                 echo "please specify a category using -dir, possibilities:"
69                 for dir in $dirs; do
70                         echo "-> $dir"
71                 done
72                 exit 1
73         else
74                 dir="`echo $dirs | awk -F/ '{ print $3 }'`"
75         fi
76 fi
77
78 if [ -f ./packages/$dir/$package/$package ] ; then
79 echo "found $package config file ..."
80         
81         # defining help variables
82         confdir="$hdw_home_dir/packages/$dir/$package"
83         archdir="$hdw_home_dir/download/$dir/$package"
84
85         # lets get some package info
86         pkgs=`cat $confdir/$package | grep '^#\ \[D\]' | \
87                 awk '{ print $3 }'`
88         pkg=`echo $pkgs | awk '{ print $1 }'` # to make sure there is only 1
89         ver=`cat $confdir/$package | grep '^#\ \[V\]' | \
90                 awk '{ print $3 }'`
91         patch=`cat $confdir/$package | grep '^#\ \[P\]' | \
92                 awk '{ print $3 }'`
93
94         # lets see whether we have the source
95         if [ ! -f ./download/$dir/$package/$pkg -a ! -z "$pkg" ]; then
96                 echo "sources for $package not found, trying to get them ..."
97                 ./scripts/Download -package $package
98         fi
99         if [ ! -f ./download/$dir/$package/$pkg -a ! -z "$pkg" ]; then
100                 echo "sources still not found, aborting ..."
101                 exit 1
102         fi
103
104         # compress format of source?
105         if [ ! -z "`echo \`detect_file_ending $pkg\` | grep 'bz2'`" ] ; then
106                 compress="bzip2"
107         elif [ ! -z "`echo \`detect_file_ending $pkg\` | grep 'tbz2'`"] ; then
108                 compress="bzip2"
109         else
110                 compress="gzip"
111         fi
112
113         #
114         # defining elementary build functions and variables
115         #
116         custmain="0"
117         autoextract="1"
118         srcdir="$package*"
119         skip=""; s_reason=""
120         make_conf=""; install_conf=""
121         
122         prefix=""; docdir=""
123         if [ "$hdw_status" -lt "2" ] ; then
124                 # use the symlink as prefix
125                 prefix="/${s1_prefix}"
126                 docdir="$prefix/share/doc/$package"
127                 install_conf="PREFIX=$prefix"
128         else
129                 if [ "$hdw_status" = "2" -o "$hdw_status" = "3" ] ; then
130                         prefix="$root/usr"; docdir="$root/usr/doc/$package"
131                         localstatedir="$root/var"
132                         sysconfdir="$root/etc"
133                 elif [ "$hdw_status" = "4" ] ; then
134                         prefix="$root/opt/$package"
135                         docdir="$root/opt/$package/doc"
136                         localstatedir="$root/var/opt/$package"
137                         sysconfdir="$root/etc/opt/$package"
138                 fi
139         fi
140         
141         if [ -z "$prefix" ] ; then
142                 echo "problem: no prefix found! aborting .."
143                 exit 1
144         fi
145
146         # defining confopt now
147         confopt="--prefix=$prefix --bindir=$prefix/bin"
148         confopt="$confopt --sbindir=$prefix/sbin"
149         confopt="$confopt --libdir=$prefix/lib"
150         confopt="$confopt --datadir=$prefix/share"
151         confopt="$confopt --infodir=$prefix/info"
152         confopt="$confopt --mandir=$prefix/man"
153         if [ "$hdw_status" -gt "1" ] ; then
154                 confopt="$confopt --localstatedir=$localstatedir"
155                 confopt="$confopt --sysconfdir=$sysconfdir"
156         fi
157         # architecture specific stuff
158         confopt="$confopt --build=$hdw_arch_build"
159         confopt="$confopt --host=$hdw_arch_host"
160         # crosscompiling
161         [ "$package" = "gcc" -o "$package" = "binutils" ] && \
162                 confopt="$confopt --target=$hdw_arch_target"
163
164         # buildfunctions
165         pre_install()   { 
166                 echo "hdw rules!" 
167                         }
168         post_install()  {
169                 echo "hdw rules!" 
170                         }
171         build_main ()   {
172                 echo
173                 echo "hdw-linux: \$confopt = $confopt"
174                 echo "hdw-linux: \$make_conf = $make_conf"
175                 echo "hdw-linux: \$install_conf = $install_conf"
176                 echo "lets do it ..."
177                 if [ -f ./configure ] ; then
178                         echo "run ./configure $confopt"
179                         ./configure $confopt
180                 fi
181                 echo ; echo "run make $make_conf"
182                 make $make_conf
183                 echo ; echo "run make $install_conf install"
184                 make $install_conf install
185                         }
186         
187         # flist find variables/command
188         flist_path="bin/ boot/ etc/ lib/ opt/ sbin/ usr/ var/"
189         [ "$hdw_status" -lt "2" ] && flist_path="${flist_path} ${s1_prefix}/"
190         flist_cmd="find \$flist_path \( -cnewer $root/.time_$package -o \
191                 -newer $root/.time_$package \) \( -not -type d -o \
192                 -type d -empty \) -print"
193         flist_pruned="var/adm|var/log|var/run|var/tmp|var/spool"
194         flist_pruned="${flist_pruned}|var/cache|var/lock"
195         
196         # reading config functions and variables
197         . $confdir/$package
198         
199         # lets begin building, in case we dont skip it!
200         if [ -z "$skip" ] ; then
201         
202         # setting umask, we dont reset it!
203         umask 0022
204
205         # run ldconfig to, maybe we installed some libs before!
206         if [ "$hdw_status" != "1" ] ; then
207                 echo "running ldconfig ..."
208                 ldconfig
209         fi
210         
211         # for creating the flist
212         #
213         cd $root/
214         touch .time_${package} && sleep 1
215         cd $hdw_home_dir
216
217         set -e
218         # use trap to catch errors in block
219         # we may kick out abort_when_package_build_failed lines
220         # can we use $_ instead off TRAP_ERR ?
221         {
222         trap 'abort_when_package_build_failed 1 $_' ERR
223
224         # automated build procedure
225         if [ "$custmain" != "0" ] ; then
226                 #build according packages custmain function
227                 if [ "$no_src_del" != "1" ] ; then
228                         rm -rf $root/src.$package
229                 fi
230                 mkdir -p $root/src.$package
231                 cd $root/src.$package
232                 if [ "$hdw_debug_buildenv" = "1" ] ; then
233                         echo 
234                         echo "Debug: (buildenvironment)"
235                         printenv
236                         echo
237                 fi
238                 custmain
239                 cd $hdw_home_dir
240                 rm -rf $root/src.$package
241         else    
242                 #now we go for automated building       
243                 # automated extract
244                 if [ "$no_src_del" != "1" ] ; then
245                         if [ "$autoextract" != "0" ] ; then
246                                 rm -rf $root/src.$package
247                                 mkdir -p $root/src.$package
248                                 tar --use-compress-program=$compress \
249                                         -xvf ./download/$dir/$package/$pkg \
250                                         -C $root/src.$package
251                         fi
252                 fi
253                 # change to directory and build
254                 cd $root/src.$package/$srcdir &&
255                 # patches from [P] tag
256                 if [ ! -z "$patch" ] ; then
257                         for every_single_patch in $patch; do
258                                 echo "aplying patch $every_single_patch ..."
259                                 patch -Nfp1 < $hdw_home_dir/download/$dir/$package/$every_single_patch
260                         done
261                         echo
262                 fi
263                 # patches from confdir
264                 # general patches
265                 for xtra_patch in `ls $confdir/*.patch 2> /dev/null`; do
266                         echo "applying patch $xtra_patch ..."
267                         patch -Nfp1 < $xtra_patch
268                 done
269                 # architecture dependent patches
270                 for xtra_patch in `ls $confdir/*.patch.${hdw_arch} 2> /dev/null`; do
271                         echo "applying patch $xtra_patch ..."
272                         patch -Nfp1 < $xtra_patch
273                 done
274                 # target specific patches
275                 for xtra_patch in `ls $confdir/*.patch.${hdw_target} 2> /dev/null`; do
276                         echo "applying patch $xtra_patch ..."
277                         patch -Nfp1 < $xtra_patch
278                 done
279                 # dietlibc patches
280                 if [ "$hdw_use_dietlibc" = "1" ] ; then
281                         for xtra_patch in `ls $confdir/*.patch.dietlibc 2> /dev/null`; do
282                                 echo "applying patch $xtra_patch ..."
283                                 patch -Nfp1 < $xtra_patch
284                         done
285                 fi
286                 # stage specific patches
287                 for xtra_patch in `ls $confdir/*.patch.${hdw_status} 2> /dev/null`; do
288                         echo "applying patch $xtra_patch ..."
289                         patch -Nfp1 < $xtra_patch
290                 done
291                 # patch section end ;)
292                 echo
293                 pre_install
294                 if [ "$hdw_debug_buildenv" = "1" ] ; then
295                         echo
296                         echo "Debug: (buildenvironment)"
297                         printenv
298                         echo
299                 fi
300                 build_main 
301                 post_install
302                 cd $hdw_home_dir 
303                 rm -rf $root/src.$package
304         fi
305         # init
306         # dirty! not create *.init for 00-init-conf, manually done.
307         if [ "$package" != "00-init-conf" ] ; then
308                 for my_init in `ls $confdir/*.init 2> /dev/null`; do
309                         create_init $my_init
310                 done
311         fi
312         # setup scripts
313         if [ "$hdw_status" != "1" ] ; then
314                 for my_setup in `ls $confdir/*.setup 2> /dev/null`; do
315                         create_setup_scripts $my_setup
316                 done
317         fi
318
319         }
320         # end of trap block
321
322         # let's see what we installed
323         #
324         cd $root/
325         eval $flist_cmd | grep -vE $flist_pruned > flist.$package
326
327         # let's create the flist
328         append=""
329         [ "$hdw_status" = "0" ] && append=".stage0"
330         [ "$hdw_status" = "1" ] && append=".stage1"
331         append="${append}-${priority}"
332         export package
333         export append
334         export hdw_status
335         export priority
336         awk '\
337         BEGIN   { package=ENVIRON["package"] ; append=ENVIRON["append"] ; \
338                   stage=ENVIRON["hdw_status"] ; priority=ENVIRON["priority"] } \
339                 { print package ": " $0 } \
340         END     { print package ": " "var/adm/logs/" stage "-" priority "-" \
341                   package ; \
342                   print package ": " "var/adm/flists/" package append }' \
343         $root/flist.$package > $root/var/adm/flists/${package}${append}
344         # manually add dependency files, calculated after flist
345         if [ "$hdw_status" -ge "3" ] ; then
346                 echo "${package}: var/adm/deps/build/$package" >> \
347                 $root/var/adm/flists/${package}${append}
348                 echo "${package}: var/adm/deps/run/$package" >> \
349                 $root/var/adm/flists/${package}${append}
350         fi
351
352         # build/runtime dependencies (just libs >= stage 3, 
353         # default is 'hardcoded')
354         # still in development !!
355         if [ "$hdw_status" -ge "3" ] ; then
356                 # build deps
357                 rm -f $root/var/adm/deps/build/$package
358                 echo -n "calculating build dependencies ... "
359                 alldeps=""
360                 for lib in `grep '\ -l[A-Za-z]' \
361                 $root/var/adm/logs/${hdw_status}-${priority}-$package.out`; do
362                         if [ -n "`echo $lib | grep '^-l'`" ] ; then
363                                 lib=${lib//-l/lib}
364                                 for dep in `grep "/$lib\." \
365                                 -r $root/var/adm/flists | \
366                                 awk -F: '{ print $2 }'`; do
367                                         add=1
368                                         for cmp in $alldeps; do
369                                                 [ "$cmp" = "$dep" ] && add=0
370                                         done
371                                         [ "$add" = "1" ] && \
372                                                 alldeps="$dep $alldeps"
373                                 done
374                         fi
375                 done
376                 for dep in $alldeps; do
377                         echo "$dep" >> $root/var/adm/deps/build/$package
378                 done
379                 touch $root/var/adm/deps/build/$package
380                 echo "done"
381                 # runtime deps
382                 rm -f $root/var/adm/deps/run/$package
383                 echo -n "calculating runtime dependencies ... "
384                 alldeps=""
385                 for binary in `grep 'bin\/' \
386                                 $root/var/adm/flists/${package}${append} | \
387                                 awk '{ print $2 }'`; do
388                         for rdep in `ldd $root/$binary 2>&1 | \
389                                         awk '{ print $3 }' | \
390                                         sed 's$/$$' | grep -v 'dynamic' | \
391                                         grep -v '^ldd'`; do
392                                 for rpkg in `grep $rdep -r \
393                                                 $root/var/adm/flists | \
394                                                 grep -v $s1_prefix | \
395                                                 awk -F: '{ print $2 }'`; do
396                                         add=1
397                                         for cmp in $alldeps; do
398                                                 [ "$cmp" = "$rpkg" ] && add=0
399                                         done
400                                         [ "$add" = "1" ] && \
401                                                 alldeps="$rpkg $alldeps"
402                                 done
403                         done
404                 done
405                 for library in `grep 'lib\/.*.so.*' \
406                                 $root/var/adm/flists/${package}${append} | \
407                                 awk '{ print $2 }'`; do
408                         for rdep in `ldd $root/$library 2>&1 | \
409                                         awk '{ print $3 }' | \
410                                         sed 's$/$$' | grep -v 'dynamic' | \
411                                         grep -v '^ldd'`; do
412                                 for rpkg in `grep $rdep -r \
413                                                 $root/var/adm/flists | \
414                                                 grep -v $s1_prefix | \
415                                                 awk -F: '{ print $2 }'`; do
416                                         add=1
417                                         for cmp in $alldeps; do
418                                                 [ "$cmp" = "$rpkg" ] && add=0
419                                         done
420                                         [ "$add" = "1" ] && \
421                                                 alldeps="$rpkg $alldeps"
422                                 done
423                         done
424                 done
425                 for dep in $alldeps; do
426                         echo "$dep" >> $root/var/adm/deps/run/$package
427                 done
428                 touch $root/var/adm/deps/run/$package
429                 echo "done"
430         fi
431         
432         # remove flist stuff 
433         rm .time_${package}
434         rm flist.$package
435         cd $hdw_home_dir
436
437         else 
438                 [ -z "$s_reason" ] && s_reason=unknown
439                 echo
440                 echo "FYI: $package build skipped, reason: $s_reason"
441         fi # (if [ -z "$skip" ])
442
443 else
444         echo "cannot find $package config file ..."
445         exit 1
446 fi