angstrom: prefer the git version of tslib
[openembedded.git] / classes / sanity.bbclass
blob2cf6c7f920f018bcc647d4dc9f613ecaae9a0f8b
2 # Sanity check the users setup for common misconfigurations
5 inherit qemu
7 def raise_sanity_error(msg):
8         import bb
9         bb.fatal(""" Openembedded's config sanity checker detected a potential misconfiguration.
10         Either fix the cause of this error or at your own risk disable the checker (see sanity.conf).
11         Following is the list of potential problems / advisories:
12         
13         %s""" % msg)
15 def check_conf_exists(fn, data):
16         bbpath = []
17         fn = bb.data.expand(fn, data)
18         vbbpath = bb.data.getVar("BBPATH", data)
19         if vbbpath:
20                 bbpath += vbbpath.split(":")
21         for p in bbpath:
22                 currname = os.path.join(bb.data.expand(p, data), fn)
23                 if os.access(currname, os.R_OK):
24                         return True
25         return False
27 def check_sanity(e):
28         from bb import note, error, data, __version__
30         try:
31                 from distutils.version import LooseVersion
32         except ImportError:
33                 def LooseVersion(v):
34                         bb.msg.warn(None, "sanity.bbclass can't compare versions without python-distutils")
35                         return 1
36         import commands
38         # Check the bitbake version meets minimum requirements
39         minversion = data.getVar('BB_MIN_VERSION', e.data , True)
40         if not minversion:
41                 # Hack: BB_MIN_VERSION hasn't been parsed yet so return 
42                 # and wait for the next call
43                 return
45         if 0 == os.getuid():
46                 raise_sanity_error("Do not use Bitbake as root.")
48         messages = ""
50         if (LooseVersion(__version__) < LooseVersion(minversion)):
51                 messages = messages + 'Bitbake version %s is required and version %s was found\n' % (minversion, __version__)
53         # Check TARGET_ARCH is set
54         if data.getVar('TARGET_ARCH', e.data, True) == 'INVALID':
55                 messages = messages + 'Please set TARGET_ARCH directly, or choose a MACHINE or DISTRO that does so.\n'
56         
57         # Check TARGET_OS is set
58         if data.getVar('TARGET_OS', e.data, True) == 'INVALID':
59                 messages = messages + 'Please set TARGET_OS directly, or choose a MACHINE or DISTRO that does so.\n'
61         assume_provided = data.getVar('ASSUME_PROVIDED', e.data , True).split()
62         # Check user doesn't have ASSUME_PROVIDED = instead of += in local.conf
63         if "diffstat-native" not in assume_provided:
64                 messages = messages + 'Please use ASSUME_PROVIDED +=, not ASSUME_PROVIDED = in your local.conf\n'
65         
66         # Check that the MACHINE is valid, if it is set
67         if data.getVar('MACHINE', e.data, True):
68                 if not check_conf_exists("conf/machine/${MACHINE}.conf", e.data):
69                         messages = messages + 'Please set a valid MACHINE in your local.conf\n'
70         
71         # Check that the DISTRO is valid
72         # need to take into account DISTRO renaming DISTRO
73         if not ( check_conf_exists("conf/distro/${DISTRO}.conf", e.data) or check_conf_exists("conf/distro/include/${DISTRO}.inc", e.data) ):
74                 messages = messages + "DISTRO '%s' not found. Please set a valid DISTRO in your local.conf\n" % data.getVar("DISTRO", e.data, True )
76         missing = ""
78         if not check_app_exists("${MAKE}", e.data):
79                 missing = missing + "GNU make,"
81         if not check_app_exists('${BUILD_PREFIX}gcc', e.data):
82                 missing = missing + "C Compiler (${BUILD_PREFIX}gcc),"
84         if not check_app_exists('${BUILD_PREFIX}g++', e.data):
85                 missing = missing + "C++ Compiler (${BUILD_PREFIX}g++),"
87         required_utilities = "patch diffstat texi2html makeinfo cvs svn bzip2 tar gzip gawk md5sum chrpath"
89         # If we'll be running qemu, perform some sanity checks
90         if data.getVar('ENABLE_BINARY_LOCALE_GENERATION', e.data, True):
91                 if "qemu-native" in assume_provided:
92                         required_utilities += " %s" % (qemu_target_binary(e.data))
94         for util in required_utilities.split():
95                 if not check_app_exists( util, e.data ):
96                         missing = missing + "%s," % util
98         if missing != "":
99                 missing = missing.rstrip(',')
100                 messages = messages + "Please install following missing utilities: %s\n" % missing
102         try:
103             if os.path.basename(os.readlink('/bin/sh')) == 'dash':
104                     messages = messages + "Using dash as /bin/sh causes various subtle build problems, please use bash instead.\n"
105         except:
106                 pass
108         omask = os.umask(022)
109         if omask & 0755:
110                 messages = messages + "Please use a umask which allows a+rx and u+rwx\n"
111         os.umask(omask)
113         oes_bb_conf = data.getVar( 'OES_BITBAKE_CONF', e.data, True )
114         if not oes_bb_conf:
115                 messages = messages + 'You do not include OpenEmbeddeds version of conf/bitbake.conf. This means your environment is misconfigured, in particular check BBPATH.\n'
117         #
118         # Check that TMPDIR hasn't changed location since the last time we were run
119         #
120         tmpdir = data.getVar('TMPDIR', e.data, True)
121         checkfile = os.path.join(tmpdir, "saved_tmpdir")
122         if os.path.exists(checkfile):
123                 f = file(checkfile, "r")
124                 oldpath = f.read().strip()
125                 if (oldpath != tmpdir):
126                         messages = messages + "Error, TMPDIR has changed location. You need to either move it back to %s or rebuild\n" % oldpath
127         else:
128                 import bb
129                 bb.mkdirhier(tmpdir)
130                 f = file(checkfile, "w")
131                 f.write(tmpdir)
132         f.close()
134         #
135         # Check the 'ABI' of TMPDIR
136         #
137         current_abi = data.getVar('OELAYOUT_ABI', e.data, True)
138         abifile = data.getVar('SANITY_ABIFILE', e.data, True)
139         if os.path.exists(abifile):
140                 f = file(abifile, "r")
141                 abi = f.read().strip()
142                 if not abi.isdigit():
143                         f = file(abifile, "w")
144                         f.write(current_abi)
145                 elif abi == "3" and current_abi == "4":
146                         import bb
147                         bb.note("Converting staging from layout version 2 to layout version 3")
148                         os.system(bb.data.expand("mv ${TMPDIR}/staging ${TMPDIR}/sysroots", e.data))
149                         os.system(bb.data.expand("ln -s sysroots ${TMPDIR}/staging", e.data))
150                         os.system(bb.data.expand("cd ${TMPDIR}/stamps; for i in */*do_populate_staging; do new=`echo $i | sed -e 's/do_populate_staging/do_populate_sysroot/'`; mv $i $new; done", e.data))
151                         f = file(abifile, "w")
152                         f.write(current_abi)
153                 elif abi == "5" and current_abi != "5":
154                         messages = messages + "Staging layout has changed. The cross directory has been deprecated and cross packages are now built under the native sysroot.\nThis requires a rebuild.\n"
155                 elif (abi != current_abi):
156                         # Code to convert from one ABI to another could go here if possible.
157                         messages = messages + "Error, TMPDIR has changed ABI (%s to %s) and you need to either rebuild, revert or adjust it at your own risk.\n" % (abi, current_abi)
158         else:
159                 f = file(abifile, "w")
160                 f.write(current_abi)
161         f.close()
163         #
164         # Check the Distro PR value didn't change
165         #
166         distro_pr = data.getVar('DISTRO_PR', e.data, True)
167         prfile = data.getVar('SANITY_PRFILE', e.data, True)
168         if os.path.exists(prfile):
169                 f = file(prfile, "r")
170                 pr = f.read().strip()
171                 if (pr != distro_pr):
172                         # Code to convert from one ABI to another could go here if possible.
173                         messages = messages + "Error, DISTRO_PR has changed (%s to %s) which means all packages need to rebuild. Please remove your TMPDIR so this can happen. For autobuilder setups you can avoid this by using a TMPDIR that include DISTRO_PR in the path.\n" % (pr, distro_pr)
174         else:
175                 f = file(prfile, "w")
176                 f.write(distro_pr)
177         f.close()
180         #
181         # Check there aren't duplicates in PACKAGE_ARCHS
182         #
183         archs = data.getVar('PACKAGE_ARCHS', e.data, True).split()
184         for arch in archs:
185                 if archs.count(arch) != 1:
186                         messages = messages + "Error, Your PACKAGE_ARCHS field contains duplicates. Perhaps you set PACKAGE_EXTRA_ARCHS twice accidently through some tune file?\n"
187                         break
189         #
190         # Check there isn't old persistent cache
191         #
192         cache = data.getVar('CACHE', e.data, True)
193         persistent_dir = data.getVar('PERSISTENT_DIR', e.data, True)
194         persistent_cache_filename = data.getVar('SANITY_PERSIST_DATA_FILE', e.data, True)
195         if cache != persistent_dir and os.path.exists(cache + '/' + persistent_cache_filename):
196                 messages = messages + "Error, persistent cache file '%s' exists in old location '%s', please migrate it to new location '%s' and merge them together if you have one for each MACHINE.\n" % (persistent_cache_filename, cache, persistent_dir)
198         if messages != "":
199                 raise_sanity_error(messages)
201 python check_sanity_eventhandler() {
202     if isinstance(e, bb.event.BuildStarted):
203         check_sanity(e)
205 addhandler check_sanity_eventhandler