firefox: Perform a number of cleanups and fix consistency issues.
[openembedded.git] / classes / image.bbclass
1 inherit rootfs_${IMAGE_PKGTYPE}
2
3 LICENSE = "MIT"
4 PACKAGES = ""
5
6 #
7 # udev, devfsd, busybox-mdev (from busybox) or none
8 #
9 IMAGE_DEV_MANAGER ?= "${@base_contains("MACHINE_FEATURES", "kernel26",  "udev","",d)} "
10 #
11 # sysvinit, upstart
12 #
13 IMAGE_INIT_MANAGER ?= "sysvinit sysvinit-pidof"
14 IMAGE_INITSCRIPTS ?= "initscripts"
15 #
16 # tinylogin, getty
17 #
18 IMAGE_LOGIN_MANAGER ?= "tinylogin"
19
20 # set sane default for the SPLASH variable
21 SPLASH ?= ""
22
23 IMAGE_KEEPROOTFS ?= ""
24 IMAGE_KEEPROOTFS[doc] = "Set to non-empty to keep ${IMAGE_ROOTFS} around after image creation."
25
26 IMAGE_BOOT ?= "${IMAGE_INITSCRIPTS} \
27 ${IMAGE_DEV_MANAGER} \
28 ${IMAGE_INIT_MANAGER} \
29 ${IMAGE_LOGIN_MANAGER} "
30
31 RDEPENDS += "${IMAGE_INSTALL} ${IMAGE_BOOT}"
32
33 # "export IMAGE_BASENAME" not supported at this time
34 IMAGE_BASENAME[export] = "1"
35 export PACKAGE_INSTALL ?= "${IMAGE_INSTALL} ${IMAGE_BOOT}"
36
37 # We need to recursively follow RDEPENDS and RRECOMMENDS for images
38 do_rootfs[recrdeptask] += "do_deploy do_populate_staging"
39
40 # Images are generally built explicitly, do not need to be part of world.
41 EXCLUDE_FROM_WORLD = "1"
42
43 USE_DEVFS ?= "0"
44
45 PID = "${@os.getpid()}"
46
47 PACKAGE_ARCH = "${MACHINE_ARCH}"
48
49 do_rootfs[depends] += "makedevs-native:do_populate_staging fakeroot-native:do_populate_staging"
50
51 python () {
52     import bb
53
54     deps = bb.data.getVarFlag('do_rootfs', 'depends', d) or ""
55     for type in (bb.data.getVar('IMAGE_FSTYPES', d, True) or "").split():
56         for dep in ((bb.data.getVar('IMAGE_DEPENDS_%s' % type, d) or "").split() or []):
57             deps += " %s:do_populate_staging" % dep
58     for dep in (bb.data.getVar('EXTRA_IMAGEDEPENDS', d, True) or "").split():
59         deps += " %s:do_populate_staging" % dep
60     bb.data.setVarFlag('do_rootfs', 'depends', deps, d)
61
62     runtime_mapping_rename("PACKAGE_INSTALL", d)
63 }
64
65 #
66 # Get a list of files containing tables of devices to be created.
67 # * IMAGE_DEVICE_TABLE is the old name to an absolute path to a device table file
68 # * IMAGE_DEVICE_TABLES is a new name for a file, or list of files, searched
69 #   for in the BBPATH
70 # If neither are specified then the default name of files/device_table-minimal.txt
71 # is searched for in the BBPATH (same as the old version.)
72 #
73 def get_devtable_list(d):
74     import bb
75     devtable = bb.data.getVar('IMAGE_DEVICE_TABLE', d, 1)
76     if devtable != None:
77         return devtable
78     devtables = bb.data.getVar('IMAGE_DEVICE_TABLES', d, 1)
79     if devtables == None:
80         devtables = 'files/device_table-minimal.txt'
81     return " ".join([ bb.which(bb.data.getVar('BBPATH', d, 1), devtable)
82                       for devtable in devtables.split() ])
83
84 def get_imagecmds(d):
85     import bb
86     cmds = "\n"
87     old_overrides = bb.data.getVar('OVERRIDES', d, 0)
88     for type in bb.data.getVar('IMAGE_FSTYPES', d, True).split():
89         localdata = bb.data.createCopy(d)
90         bb.data.setVar('OVERRIDES', '%s:%s' % (type, old_overrides), localdata)
91         bb.data.update_data(localdata)
92         cmd  = "\t#Code for image type " + type + "\n"
93         cmd += "\t${IMAGE_CMD_" + type + "}\n"
94         cmd += "\tcd ${DEPLOY_DIR_IMAGE}/\n"
95         cmd += "\trm -f ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}." + type + "\n"
96         cmd += "\tln -s ${IMAGE_NAME}.rootfs." + type + " ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}." + type + "\n\n"
97         cmds += bb.data.expand(cmd, localdata)
98     return cmds
99
100 IMAGE_POSTPROCESS_COMMAND ?= ""
101 MACHINE_POSTPROCESS_COMMAND ?= ""
102 ROOTFS_POSTPROCESS_COMMAND ?= ""
103
104 # some default locales
105 IMAGE_LINGUAS ?= "de-de fr-fr en-gb"
106
107 LINGUAS_INSTALL = "${@" ".join(map(lambda s: "locale-base-%s" % s, bb.data.getVar('IMAGE_LINGUAS', d, 1).split()))}"
108
109 do_rootfs[nostamp] = "1"
110 do_rootfs[dirs] = "${TOPDIR}"
111 do_build[nostamp] = "1"
112
113 # Must call real_do_rootfs() from inside here, rather than as a separate
114 # task, so that we have a single fakeroot context for the whole process.
115 fakeroot do_rootfs () {
116         set -x
117         rm -rf ${IMAGE_ROOTFS}
118         mkdir -p ${IMAGE_ROOTFS}
119         mkdir -p ${DEPLOY_DIR_IMAGE}
120
121         if [ "${USE_DEVFS}" != "1" ]; then
122                 for devtable in ${@get_devtable_list(d)}; do
123                         makedevs -r ${IMAGE_ROOTFS} -D $devtable
124                 done
125         fi
126
127         rootfs_${IMAGE_PKGTYPE}_do_rootfs
128
129         insert_feed_uris
130
131         ${IMAGE_PREPROCESS_COMMAND}
132
133         ROOTFS_SIZE=`du -ks ${IMAGE_ROOTFS}|awk '{size = ${IMAGE_EXTRA_SPACE} + $1; print (size > ${IMAGE_ROOTFS_SIZE} ? size : ${IMAGE_ROOTFS_SIZE}) }'`
134         ${@get_imagecmds(d)}
135
136         ${IMAGE_POSTPROCESS_COMMAND}
137         
138         ${MACHINE_POSTPROCESS_COMMAND}
139         ${@['rm -rf ${IMAGE_ROOTFS}', ''][bool(d.getVar("IMAGE_KEEPROOTFS", 1))]}
140 }
141
142 do_deploy_to[nostamp] = "1"
143 do_deploy_to () {
144         # A standalone task to deploy built image to the location specified
145         # by DEPLOY_TO variable (likely passed via environment).
146         # Assumes ${IMAGE_FSTYPES} is a single value!
147         cp "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.${IMAGE_FSTYPES}" ${DEPLOY_TO}
148 }
149
150 insert_feed_uris () {
151         
152         echo "Building feeds for [${DISTRO}].."
153
154         for line in ${FEED_URIS}
155         do
156                 # strip leading and trailing spaces/tabs, then split into name and uri
157                 line_clean="`echo "$line"|sed 's/^[ \t]*//;s/[ \t]*$//'`"
158                 feed_name="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\1/p'`"
159                 feed_uri="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\2/p'`"
160                 
161                 echo "Added $feed_name feed with URL $feed_uri"
162                 
163                 # insert new feed-sources
164                 echo "src/gz $feed_name $feed_uri" >> ${IMAGE_ROOTFS}/etc/opkg/${feed_name}-feed.conf
165         done
166
167         # Allow to use package deploy directory contents as quick devel-testing
168         # feed. This creates individual feed configs for each arch subdir of those
169         # specified as compatible for the current machine.
170         # NOTE: Development-helper feature, NOT a full-fledged feed.
171         if [ -n "${FEED_DEPLOYDIR_BASE_URI}" ]; then
172             for arch in ${PACKAGE_ARCHS}
173             do
174                 echo "src/gz local-$arch ${FEED_DEPLOYDIR_BASE_URI}/$arch" >> ${IMAGE_ROOTFS}/etc/opkg/local-$arch-feed.conf
175             done
176         fi
177 }
178
179 log_check() {
180         set +x
181         for target in $*
182         do
183                 lf_path="${WORKDIR}/temp/log.do_$target.${PID}"
184                 
185                 echo "log_check: Using $lf_path as logfile"
186                 
187                 if test -e "$lf_path"
188                 then
189                         rootfs_${IMAGE_PKGTYPE}_log_check $target $lf_path
190                 else
191                         echo "Cannot find logfile [$lf_path]"
192                 fi
193                 echo "Logfile is clean"
194         done
195
196         set -x
197 }
198
199 # set '*' as the rootpassword so the images
200 # can decide if they want it or not
201
202 zap_root_password () {
203         sed 's%^root:[^:]*:%root:*:%' < ${IMAGE_ROOTFS}/etc/passwd >${IMAGE_ROOTFS}/etc/passwd.new
204         mv ${IMAGE_ROOTFS}/etc/passwd.new ${IMAGE_ROOTFS}/etc/passwd
205
206
207 create_etc_timestamp() {
208         date +%2m%2d%2H%2M%Y >${IMAGE_ROOTFS}/etc/timestamp
209 }
210
211 # Turn any symbolic /sbin/init link into a file
212 remove_init_link () {
213         if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
214                 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
215                 rm ${IMAGE_ROOTFS}/sbin/init
216                 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
217         fi
218 }
219
220 make_zimage_symlink_relative () {
221         if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
222                 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
223         fi
224 }
225
226 # Make login manager(s) enable automatic login.
227 # Useful for devices where we do not want to log in at all (e.g. phones)
228 set_image_autologin () {
229         sed -i 's%^AUTOLOGIN=\"false"%AUTOLOGIN="true"%g' ${IMAGE_ROOTFS}/etc/sysconfig/gpelogin
230 }
231
232 # Can be use to create /etc/timestamp during image construction to give a reasonably 
233 # sane default time setting
234 rootfs_update_timestamp () {
235         date "+%m%d%H%M%Y" >${IMAGE_ROOTFS}/etc/timestamp
236 }
237
238 # Install locales into image for every entry in IMAGE_LINGUAS
239 install_linguas() {
240 if [ -e ${IMAGE_ROOTFS}/usr/bin/opkg-cl ] ; then
241         OPKG="opkg-cl ${IPKG_ARGS}"
242
243         ${OPKG} update || true
244         ${OPKG} list_installed | awk '{print $1}' |sort | uniq > /tmp/installed-packages
245
246         for i in $(cat /tmp/installed-packages | grep -v locale) ; do
247                 for translation in ${IMAGE_LINGUAS}; do
248                         translation_split=$(echo ${translation} | awk -F '-' '{print $1}')
249                         echo ${i}-locale-${translation}
250                         echo ${i}-locale-${translation_split}
251                 done
252         done | sort | uniq > /tmp/wanted-locale-packages
253
254         ${OPKG} list | awk '{print $1}' |grep locale |sort | uniq > /tmp/available-locale-packages
255
256         cat /tmp/wanted-locale-packages /tmp/available-locale-packages | sort | uniq -d > /tmp/pending-locale-packages
257
258         cat /tmp/pending-locale-packages | xargs ${OPKG} -nodeps install
259         rm -f ${IMAGE_ROOTFS}${libdir}/opkg/lists/*
260
261     for i in ${IMAGE_ROOTFS}${libdir}/opkg/info/*.preinst; do
262         if [ -f $i ] && ! sh $i; then
263             opkg-cl ${IPKG_ARGS} flag unpacked `basename $i .preinst`
264         fi
265     done
266
267     for i in ${IMAGE_ROOTFS}${libdir}/opkg/info/*.postinst; do
268         if [ -f $i ] && ! sh $i configure; then
269             opkg-cl ${IPKG_ARGS} flag unpacked `basename $i .postinst`
270         fi
271     done
272
273 fi
274 }
275
276 # export the zap_root_password, create_etc_timestamp and remote_init_link
277 EXPORT_FUNCTIONS zap_root_password create_etc_timestamp remove_init_link do_rootfs make_zimage_symlink_relative set_image_autologin rootfs_update_timestamp install_linguas
278
279 addtask rootfs before do_build after do_install
280 addtask deploy_to after do_rootfs