Merge commit 'origin/org.openembedded.dev' into org.openembedded.dev
[openembedded.git] / classes / icecc.bbclass
1 # IceCream distributed compiling support
2 #
3 # Stages directories with symlinks from gcc/g++ to icecc, for both
4 # native and cross compilers. Depending on each configure or compile,
5 # the directories are added at the head of the PATH list and ICECC_CXX
6 # and ICEC_CC are set.
7 #
8 # For the cross compiler, creates a tar.gz of our toolchain and sets
9 # ICECC_VERSION accordingly.
10 #
11 #The class now handles all 3 different compile 'stages' (i.e native ,cross-kernel and target) creating the
12 #necessary enviroment tar.gz file to be used by the remote machines.
13 #It also supports meta-toolchain generation
14 #
15 #If ICECC_PATH is not set in local.conf then the class will try to locate it using 'which'
16 #but nothing is sure ;)
17 #
18 #If ICECC_ENV_EXEC is set in local.conf should point to the icecc-create-env script provided by the user
19 #or the default one provided by icecc-create-env.bb  will be used
20 #(NOTE that this is a modified version of the script need it and *not the one that comes with icecc*
21 #
22 #User can specify if specific packages or packages belonging to class should not use icecc to distribute
23 #compile jobs to remote machines, but handled localy, by defining ICECC_USER_CLASS_BL and ICECC_PACKAGE_BL
24 #with the appropriate values in local.conf
25 #########################################################################################
26 #Error checking is kept to minimum so double check any parameters you pass to the class
27 ###########################################################################################
28
29
30 def icc_determine_gcc_version(gcc):
31     """
32     Hack to determine the version of GCC
33
34     'i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5363)'
35     """
36     import os
37     return os.popen("%s --version" % gcc ).readline().split()[2]
38
39
40 def create_cross_env(bb,d):
41     """
42     Create a tar.bz2 of the current toolchain
43     """
44     # Constin native-native compilation no environment needed if
45     # host prefix is empty (let us duplicate the query for ease)
46     prefix = bb.data.expand('${HOST_PREFIX}', d)
47     if len(prefix) == 0:
48         return ""
49
50     import tarfile, socket, time, os
51     ice_dir = bb.data.expand('${CROSS_DIR}', d)
52     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
53     distro  = bb.data.expand('${DISTRO}', d)
54     target_sys = bb.data.expand('${TARGET_SYS}',  d)
55     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
56     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
57     name    = socket.gethostname()
58
59     # Stupid check to determine if we have built a libc and a cross
60     # compiler.
61     try:
62         os.stat(os.path.join(ice_dir, target_sys, 'lib', 'libstdc++.so'))
63         os.stat(os.path.join(ice_dir, target_sys, 'bin', 'g++'))
64     except: # no cross compiler built yet
65         bb.error('no cross compiler built yet?')
66         return ""
67
68     VERSION = icc_determine_gcc_version( os.path.join(ice_dir,target_sys,"bin","g++") )
69     cross_name = prefix + distro + "-" + target_sys + "-" + float + "-" + VERSION + "-" + name
70     tar_file = os.path.join(ice_dir, 'ice', cross_name + '.tar.gz')
71
72     try:
73         os.stat(tar_file)
74         # tar file already exists
75         return tar_file
76     except: 
77         try:
78             os.makedirs(os.path.join(ice_dir,'ice'))
79         except:
80             # directory already exists, continue
81             pass
82
83
84     #check if user has specified a specific icecc-create-env script
85     #if not use the OE provided one
86     cr_env_script = bb.data.expand('${ICECC_ENV_EXEC}',  d)
87     if cr_env_script == "${ICECC_ENV_EXEC}":
88         cr_env_script = bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
89     #call the modified create-env script
90     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
91            "--silent",
92            os.path.join(ice_dir, 'bin', "%s-gcc" % target_sys),
93            os.path.join(ice_dir, 'bin', "%s-g++" % target_sys),
94            os.path.join(ice_dir, 'bin', "%s-as" % target_sys),
95            os.path.join(ice_dir,"ice",cross_name) ) )
96     return tar_file
97
98
99 def create_native_env(bb,d):
100     import tarfile, socket, time, os
101     ice_dir = bb.data.expand('${CROSS_DIR}', d)
102     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
103     distro  = bb.data.expand('${DISTRO}', d)
104     target_sys = bb.data.expand('${TARGET_SYS}',  d)
105     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
106     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
107     name    = socket.gethostname()
108
109     archive_name = "local-host-env" + "-" + name
110     tar_file = os.path.join(ice_dir, 'ice', archive_name + '.tar.gz')
111
112     try:
113         os.stat(tar_file)
114         # tar file already exists
115         return tar_file
116     except: 
117         try:
118             #os.makedirs(os.path.join(ice_dir))
119             os.makedirs(os.path.join(ice_dir,'ice'))
120         except:
121             # directory already exists, continue
122             pass
123
124     #check if user has specified a specific icecc-create-env script
125     #if not use the OE provided one
126     cr_env_script = bb.data.expand('${ICECC_ENV_EXEC}',  d)
127     if cr_env_script == "${ICECC_ENV_EXEC}":
128         cr_env_script = bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
129     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
130            "--silent",
131            os.popen("%s gcc" % "which").read()[:-1],
132            os.popen("%s g++" % "which").read()[:-1],
133            os.popen("%s as" % "which").read()[:-1],
134            os.path.join(ice_dir,"ice",archive_name) ) )
135     return tar_file
136
137
138 def get_cross_kernel_cc(bb,d):
139     kernel_cc = bb.data.expand('${KERNEL_CC}', d)
140     kernel_cc = kernel_cc.replace('ccache', '')
141     kernel_cc = kernel_cc.strip()
142     return kernel_cc
143
144
145 def create_cross_kernel_env(bb,d):
146     import tarfile, socket, time, os
147     ice_dir = bb.data.expand('${CROSS_DIR}', d)
148     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
149     distro  = bb.data.expand('${DISTRO}', d)
150     target_sys = bb.data.expand('${TARGET_SYS}',  d)
151     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
152     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
153     name    = socket.gethostname()
154     kernel_cc = get_cross_kernel_cc(bb, d)
155
156     # Stupid check to determine if we have built a libc and a cross
157     # compiler.
158     try:
159         os.stat(os.path.join(ice_dir, 'bin', kernel_cc))
160     except: # no cross compiler built yet
161         bb.error('no kernel cross compiler built yet')
162         return ""
163
164     VERSION = icc_determine_gcc_version( os.path.join(ice_dir,"bin",kernel_cc) )
165     cross_name = prefix + distro + "-" + target_sys + "-" + float + "-" + VERSION + "-" + name
166     tar_file = os.path.join(ice_dir, 'ice', cross_name + '.tar.gz')
167
168     try:
169         os.stat(tar_file)
170         # tar file already exists
171         return tar_file
172     except: 
173         try:
174             os.makedirs(os.path.join(ice_dir,'ice'))
175         except:
176             # directory already exists, continue
177             pass
178
179
180     #check if user has specified a specific icecc-create-env script
181     #if not use the OE provided one
182     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
183     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
184            "--silent",
185            os.path.join(ice_dir,'bin',kernel_cc),
186            os.path.join(ice_dir,target_sys,'bin','g++'),
187            os.path.join(ice_dir,target_sys,'bin','as'),
188            os.path.join(ice_dir,"ice",cross_name) ) )
189     return tar_file
190
191
192 def create_env(bb,d):
193
194         #return create_cross_kernel_env(bb,d) 
195
196         if bb.data.inherits_class("native", d):
197           return create_native_env(bb,d)
198         elif bb.data.inherits_class("kernel", d):
199           return create_cross_kernel_env(bb,d)
200         elif bb.data.inherits_class("cross", d):
201           return create_native_env(bb,d)
202         elif bb.data.inherits_class("sdk", d):
203           return create_native_env(bb,d)
204         else:  
205           return create_cross_env(bb,d)
206
207
208 def create_path(compilers, type, bb, d):
209     """
210     Create Symlinks for the icecc in the staging directory
211     """
212     import os
213
214     staging = os.path.join(bb.data.expand('${STAGING_DIR}', d), "ice", type)
215
216     #check if the icecc path is set by the user
217     icecc   = bb.data.getVar('ICECC_PATH', d) or os.popen("%s icecc" % "which").read()[:-1]
218
219     # Create the dir if necessary
220     try:
221         os.stat(staging)
222     except:
223         os.makedirs(staging)
224
225     for compiler in compilers:
226         gcc_path = os.path.join(staging, compiler)
227         try:
228             os.stat(gcc_path)
229         except:
230             os.symlink(icecc, gcc_path)
231
232     return staging + ":"
233
234
235 def use_icc_version(bb,d):
236       icecc_ver = "yes"
237       system_class_blacklist = [ "none" ] 
238
239       for black in system_class_blacklist:
240            if bb.data.inherits_class(black, d):
241               icecc_ver = "no"
242
243       user_class_blacklist =  bb.data.getVar('ICECC_USER_CLASS_BL', d) or "none"
244       user_class_blacklist = user_class_blacklist.split()
245
246       for black in user_class_blacklist:
247            if bb.data.inherits_class(black, d):
248               icecc_ver = "no"
249
250       return icecc_ver
251
252
253 def icc_path(bb,d):
254     package_tmp = bb.data.expand('${PN}', d)
255
256     #"system" package blacklist contains a list of packages that can not distribute compile tasks
257     #for one reason or the other
258     system_package_blacklist = [ "uclibc", "glibc", "gcc", "qemu", "bind", "u-boot", "dhcp-forwarder", "enchant", "connman" ]
259     user_package_blacklist = (bb.data.getVar('ICECC_USER_PACKAGE_BL', d) or "").split()
260     package_blacklist = system_package_blacklist + user_package_blacklist
261
262     for black in package_blacklist:
263         if black in package_tmp:
264             bb.note(package_tmp, ' found in blacklist, disable icecc')
265             bb.data.setVar("PARALLEL_MAKE" , "", d) 
266             return ""
267
268     prefix = bb.data.expand('${HOST_PREFIX}', d)
269
270     if bb.data.inherits_class("cross", d):
271         return create_path( ["gcc", "g++"], "native", bb, d)
272
273     elif bb.data.inherits_class("native", d):
274         return create_path( ["gcc", "g++"], "native", bb, d)
275
276     elif bb.data.inherits_class("kernel", d):
277         return create_path( [get_cross_kernel_cc(bb,d), ], "cross-kernel", bb, d)
278
279     elif len(prefix) == 0:
280         return create_path( ["gcc", "g++"], "native", bb, d)
281
282     else:
283         return create_path( [prefix+"gcc", prefix+"g++"], "cross", bb, d)      
284
285
286 def icc_version(bb,d):
287     return create_env(bb,d)
288
289
290 def check_for_kernel(bb,d):     
291     if  bb.data.inherits_class("kernel", d):
292          return "yes"
293     return "no"
294
295
296 set_icecc_env() {
297     ICE_PATH=${@icc_path(bb,d)}
298     if test x${ICE_PATH} != x; then
299         export PATH=${ICE_PATH}$PATH
300         export CCACHE_PATH=$PATH
301         #check if we are building a kernel and select gcc-cross-kernel
302         if [ "${@check_for_kernel(bb,d)}" = "yes" ]; then
303             export ICECC_CC="${@get_cross_kernel_cc(bb,d)}"
304             export ICECC_CXX="${HOST_PREFIX}g++"
305         else
306             export ICECC_CC="${HOST_PREFIX}gcc"
307             export ICECC_CXX="${HOST_PREFIX}g++"
308         fi
309
310         if [ "${@use_icc_version(bb,d)}" = "yes" ]; then
311             export ICECC_VERSION="${@icc_version(bb,d)}"
312         else
313             export -n ICECC_VERSION
314         fi
315         oenote "set the icecream environment variables: PATH=$PATH, CCACHE_PATH=$CCACHE_PATH, ICECC_CC=$ICECC_CC, ICECC_CXX=$ICECC_CXX, ICECC_VERSION=$ICECC_VERSION"
316     fi
317 }
318
319 do_configure_prepend() {
320     set_icecc_env
321 }
322
323 do_compile_prepend() {
324     set_icecc_env
325 }