]> hackdaworld.org Git - hdw-linux/hdw-linux.git/blob - scripts/Build
bugfixed hdw-get
[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         confopt="$confopt --build=$hdw_arch_build"
154         confopt="$confopt --host=$hdw_arch_target"
155         # crosscompiling
156         [ "$hdw_crossbuild" != "0" ] && \
157                 confopt="$confopt --target=$hdw_arch_target"
158         if [ "$hdw_status" -gt "1" ] ; then
159                 confopt="$confopt --localstatedir=$localstatedir"
160                 confopt="$confopt --sysconfdir=$sysconfdir"
161         fi
162
163         # buildfunctions
164         pre_install()   { 
165                 echo "hdw rules!" 
166                         }
167         post_install()  {
168                 echo "hdw rules!" 
169                         }
170         build_main ()   {
171                 echo
172                 echo "hdw-linux: \$confopt = $confopt"
173                 echo "hdw-linux: \$make_conf = $make_conf"
174                 echo "hdw-linux: \$install_conf = $install_conf"
175                 echo "lets do it ..."
176                 if [ -f ./configure ] ; then
177                         echo "run ./configure $confopt"
178                         ./configure $confopt
179                 fi
180                 echo ; echo "run make $make_conf"
181                 make $make_conf
182                 echo ; echo "run make $install_conf install"
183                 make $install_conf install
184                         }
185         
186         # flist find variables/command
187         flist_path="bin/ boot/ etc/ lib/ opt/ sbin/ usr/ var/"
188         [ "$hdw_status" -lt "2" ] && flist_path="${flist_path} ${s1_prefix}/"
189         flist_cmd="find \$flist_path \( -cnewer $root/.time_$package -o \
190                 -newer $root/.time_$package \) \( -not -type d -o \
191                 -type d -empty \) -print"
192         flist_pruned="var/adm|var/log|var/run|var/tmp|var/spool"
193         flist_pruned="${flist_pruned}|var/cache|var/lock"
194         
195         # reading config functions and variables
196         . $confdir/$package
197         
198         # lets begin building, in case we dont skip it!
199         if [ -z "$skip" ] ; then
200         
201         # setting umask, we dont reset it!
202         umask 0022
203
204         # run ldconfig to, maybe we installed some libs before!
205         if [ "$hdw_status" != "1" ] ; then
206                 echo "running ldconfig ..."
207                 ldconfig
208         fi
209         
210         # for creating the flist
211         #
212         cd $root/
213         touch .time_${package} && sleep 1
214         cd $hdw_home_dir
215
216         set -e
217         # use trap to catch errors in block
218         # we may kick out abort_when_package_build_failed lines
219         # can we use $_ instead off TRAP_ERR ?
220         {
221         trap 'abort_when_package_build_failed 1 $_' ERR
222
223         # automated build procedure
224         if [ "$custmain" != "0" ] ; then
225                 #build according packages custmain function
226                 if [ "$no_src_del" != "1" ] ; then
227                         rm -rf $root/src.$package
228                 fi
229                 mkdir -p $root/src.$package
230                 cd $root/src.$package
231                 if [ "$hdw_debug_buildenv" = "1" ] ; then
232                         echo 
233                         echo "Debug: (buildenvironment)"
234                         printenv
235                         echo
236                 fi
237                 custmain
238                 cd $hdw_home_dir
239                 rm -rf $root/src.$package
240         else    
241                 #now we go for automated building       
242                 # automated extract
243                 if [ "$no_src_del" != "1" ] ; then
244                         if [ "$autoextract" != "0" ] ; then
245                                 rm -rf $root/src.$package
246                                 mkdir -p $root/src.$package
247                                 tar --use-compress-program=$compress \
248                                         -xvf ./download/$dir/$package/$pkg \
249                                         -C $root/src.$package
250                         fi
251                 fi
252                 # change to directory and build
253                 cd $root/src.$package/$srcdir &&
254                 # patches from [P] tag
255                 if [ ! -z "$patch" ] ; then
256                         for every_single_patch in $patch; do
257                                 echo "aplying patch $every_single_patch ..."
258                                 patch -Nfp1 < $hdw_home_dir/download/$dir/$package/$every_single_patch
259                         done
260                         echo
261                 fi
262                 # patches from confdir
263                 # general patches
264                 for xtra_patch in `ls $confdir/*.patch 2> /dev/null`; do
265                         echo "applying patch $xtra_patch ..."
266                         patch -Nfp1 < $xtra_patch
267                 done
268                 # architecture dependent patches
269                 for xtra_patch in `ls $confdir/*.patch.${hdw_arch} 2> /dev/null`; do
270                         echo "applying patch $xtra_patch ..."
271                         patch -Nfp1 < $xtra_patch
272                 done
273                 # target specific patches
274                 for xtra_patch in `ls $confdir/*.patch.${hdw_target} 2> /dev/null`; do
275                         echo "applying patch $xtra_patch ..."
276                         patch -Nfp1 < $xtra_patch
277                 done
278                 # dietlibc patches
279                 if [ "$hdw_use_dietlibc" = "1" ] ; then
280                         for xtra_patch in `ls $confdir/*.patch.dietlibc 2> /dev/null`; do
281                                 echo "applying patch $xtra_patch ..."
282                                 patch -Nfp1 < $xtra_patch
283                         done
284                 fi
285                 # stage specific patches
286                 for xtra_patch in `ls $confdir/*.patch.${hdw_status} 2> /dev/null`; do
287                         echo "applying patch $xtra_patch ..."
288                         patch -Nfp1 < $xtra_patch
289                 done
290                 # patch section end ;)
291                 echo
292                 pre_install
293                 if [ "$hdw_debug_buildenv" = "1" ] ; then
294                         echo
295                         echo "Debug: (buildenvironment)"
296                         printenv
297                         echo
298                 fi
299                 build_main 
300                 post_install
301                 cd $hdw_home_dir 
302                 rm -rf $root/src.$package
303         fi
304         # init
305         # dirty! not create *.init for 00-init-conf, manually done.
306         if [ "$package" != "00-init-conf" ] ; then
307                 for my_init in `ls $confdir/*.init 2> /dev/null`; do
308                         create_init $my_init
309                 done
310         fi
311         # setup scripts
312         if [ "$hdw_status" != "1" ] ; then
313                 for my_setup in `ls $confdir/*.setup 2> /dev/null`; do
314                         create_setup_scripts $my_setup
315                 done
316         fi
317
318         # build dependencies (just libs >= stage 3, default target hardcoded)
319         # still in development !!
320         if [ "$hdw_status" -ge "2" ] ; then
321                 rm -f $root/var/adm/deps/build/$package
322                 echo -n "calculating dependencies ... "
323                 alldeps=""
324                 for lib in `grep '\ -l[A-Za-z]' \
325                 $root/var/adm/logs/${hdw_status}-${priority}-$package.out`; do
326                         if [ -n "`echo $lib | grep '^-l'`" ] ; then
327                                 lib=${lib//-l/lib}
328                                 for dep in `grep "/$lib\." \
329                                 -r $root/var/adm/flists | \
330                                 awk -F: '{ print $2 }'`; do
331                                         add=1
332                                         for cmp in $alldeps; do
333                                                 [ "$cmp" = "$dep" ] && add=0
334                                         done
335                                         [ "$add" = "1" ] && \
336                                                 alldeps="$dep $alldeps"
337                                 done
338                         fi
339                 done
340                 for dep in $alldeps; do
341                         echo "$dep" >> $root/var/adm/deps/build/$package
342                 done
343                 echo "done"
344                 touch $root/var/adm/deps/build/$package
345         fi
346
347         }
348         # end of trap block
349
350         # let's see what we installed
351         #
352         cd $root/
353         eval $flist_cmd | grep -vE $flist_pruned > flist.$package
354
355         # let's create the flist
356         append=""
357         [ "$hdw_status" = "0" ] && append=".stage0"
358         [ "$hdw_status" = "1" ] && append=".stage1"
359         append="${append}-${priority}"
360         export package
361         export append
362         export hdw_status
363         export priority
364         awk '\
365         BEGIN   { package=ENVIRON["package"] ; append=ENVIRON["append"] ; \
366                   stage=ENVIRON["hdw_status"] ; priority=ENVIRON["priority"] } \
367                 { print package ": " $0 } \
368         END     { print package ": " "var/adm/logs/" stage "-" priority "-" \
369                   package ; \
370                   print package ": " "var/adm/flists/" package append }' \
371         $root/flist.$package > $root/var/adm/flists/${package}${append}
372         [ "$hdw_status" -ge "3" ] && \
373                 echo "${package}: var/adm/deps/build/$package" >> \
374                 $root/var/adm/flists/${package}${append}
375
376         # remove flist stuff 
377         rm .time_${package}
378         rm flist.$package
379         cd $hdw_home_dir
380
381         else 
382                 [ -z "$s_reason" ] && s_reason=unknown
383                 echo
384                 echo "FYI: $package build skipped, reason: $s_reason"
385         fi # (if [ -z "$skip" ])
386
387 else
388         echo "cannot find $package config file ..."
389         exit 1
390 fi