bitbake/lib/bb/shell.py:
[bitbake.git] / bin / bbimage
1 #!/usr/bin/env python
2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 #
5 # Copyright (C) 2003  Chris Larson
6 #
7 # This program is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License along with
17 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 # Place, Suite 330, Boston, MA 02111-1307 USA. 
19
20 import sys, os
21 sys.path.insert(0,os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
22 import bb
23 from bb import *
24
25 __version__ = 1.1
26 type = "jffs2"
27 cfg_bb = data.init()
28 cfg_oespawn = data.init()
29
30 bb.msg.set_debug_level(0)
31
32 def usage():
33     print "Usage: bbimage [options ...]"
34     print "Creates an image for a target device from a root filesystem,"
35     print "obeying configuration parameters from the BitBake"
36     print "configuration files, thereby easing handling of deviceisms."
37     print ""
38     print "  %s\t\t%s" % ("-r [arg], --root [arg]", "root directory (default=${IMAGE_ROOTFS})")
39     print "  %s\t\t%s" % ("-t [arg], --type [arg]", "image type (jffs2[default], cramfs)")
40     print "  %s\t\t%s" % ("-n [arg], --name [arg]", "image name (override IMAGE_NAME variable)")
41     print "  %s\t\t%s" % ("-v, --version", "output version information and exit")
42     sys.exit(0)
43
44 def version():
45     print "BitBake Build Tool Core version %s" % bb.__version__
46     print "BBImage version %s" % __version__
47
48 def emit_bb(d, base_d = {}):
49     for v in d.keys():
50         if d[v] != base_d[v]:
51             data.emit_var(v, d)
52
53 def getopthash(l):
54     h = {}
55     for (opt, val) in l:
56         h[opt] = val
57     return h
58
59 import getopt
60 try:
61     (opts, args) = getopt.getopt(sys.argv[1:], 'vr:t:e:n:', [ 'version', 'root=', 'type=', 'bbfile=', 'name=' ])
62 except getopt.GetoptError:
63     usage()
64
65 # handle opts
66 opthash = getopthash(opts)
67
68 if '--version' in opthash or '-v' in opthash:
69     version()
70     sys.exit(0)
71
72 try:
73     cfg_bb = parse.handle(os.path.join('conf', 'bitbake.conf'), cfg_bb)
74 except IOError:
75     fatal("Unable to open bitbake.conf")
76
77 # sanity check
78 if cfg_bb is None:
79     fatal("Unable to open/parse %s" % os.path.join('conf', 'bitbake.conf'))
80     usage(1)
81
82 rootfs = None
83 extra_files = []
84
85 if '--root' in opthash:
86     rootfs = opthash['--root']
87 if '-r' in opthash:
88     rootfs = opthash['-r']
89
90 if '--type' in opthash:
91     type = opthash['--type']
92 if '-t' in opthash:
93     type = opthash['-t']
94
95 if '--bbfile' in opthash:
96     extra_files.append(opthash['--bbfile'])
97 if '-e' in opthash:
98     extra_files.append(opthash['-e'])
99
100 for f in extra_files:
101     try:
102         cfg_bb = parse.handle(f, cfg_bb)
103     except IOError:
104         print "unable to open %s" % f
105
106 if not rootfs:
107     rootfs = data.getVar('IMAGE_ROOTFS', cfg_bb, 1)
108
109 if not rootfs:
110     bb.fatal("IMAGE_ROOTFS not defined")
111
112 data.setVar('IMAGE_ROOTFS', rootfs, cfg_bb)
113
114 from copy import copy, deepcopy
115 localdata = data.createCopy(cfg_bb)
116
117 overrides = data.getVar('OVERRIDES', localdata)
118 if not overrides:
119     bb.fatal("OVERRIDES not defined.")
120 data.setVar('OVERRIDES', '%s:%s' % (overrides, type), localdata)
121 data.update_data(localdata)
122 data.setVar('OVERRIDES', overrides, localdata)
123
124 if '-n' in opthash:
125     data.setVar('IMAGE_NAME', opthash['-n'], localdata)
126 if '--name' in opthash:
127     data.setVar('IMAGE_NAME', opthash['--name'], localdata)
128
129 topdir = data.getVar('TOPDIR', localdata, 1) or os.getcwd()
130
131 cmd = data.getVar('IMAGE_CMD', localdata, 1)
132 if not cmd:
133     bb.fatal("IMAGE_CMD not defined")
134
135 outdir = data.getVar('DEPLOY_DIR_IMAGE', localdata, 1)
136 if not outdir:
137     bb.fatal('DEPLOY_DIR_IMAGE not defined')
138 mkdirhier(outdir)
139
140 #depends = data.getVar('IMAGE_DEPENDS', localdata, 1) or ""
141 #if depends:
142 #       bb.note("Spawning bbmake to satisfy dependencies: %s" % depends)
143 #       ret = os.system('bbmake %s' % depends)
144 #       if ret != 0:
145 #           bb.error("executing bbmake to satisfy dependencies")
146
147 bb.note("Executing %s" % cmd)
148 data.setVar('image_cmd', cmd, localdata)
149 data.setVarFlag('image_cmd', 'func', 1, localdata)
150 try:
151     bb.build.exec_func('image_cmd', localdata)
152 except bb.build.FuncFailed:
153     sys.exit(1)
154 #ret = os.system(cmd)
155 #sys.exit(ret)