merge of 'c167fd3cbb5184fbfe56dff8fbb57e988ce58f51'
[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.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
87     #call the modified create-env script
88     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
89            "--silent",
90            os.path.join(ice_dir,target_sys,'bin','gcc'),
91            os.path.join(ice_dir,target_sys,'bin','g++'),
92            os.path.join(ice_dir,target_sys,'bin','as'),
93            os.path.join(ice_dir,"ice",cross_name) ) )
94     return tar_file
95
96
97 def create_native_env(bb,d):
98     import tarfile, socket, time, os
99     ice_dir = bb.data.expand('${CROSS_DIR}', d)
100     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
101     distro  = bb.data.expand('${DISTRO}', d)
102     target_sys = bb.data.expand('${TARGET_SYS}',  d)
103     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
104     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
105     name    = socket.gethostname()
106
107     archive_name = "local-host-env" + "-" + name
108     tar_file = os.path.join(ice_dir, 'ice', archive_name + '.tar.gz')
109
110     try:
111         os.stat(tar_file)
112         # tar file already exists
113         return tar_file
114     except: 
115         try:
116             #os.makedirs(os.path.join(ice_dir))
117             os.makedirs(os.path.join(ice_dir,'ice'))
118         except:
119             # directory already exists, continue
120             pass
121
122     #check if user has specified a specific icecc-create-env script
123     #if not use the OE provided one
124     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
125     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
126            "--silent",
127            os.popen("%s gcc" % "which").read()[:-1],
128            os.popen("%s g++" % "which").read()[:-1],
129            os.popen("%s as" % "which").read()[:-1],
130            os.path.join(ice_dir,"ice",archive_name) ) )
131     return tar_file
132
133
134 def get_cross_kernel_cc(bb,d):
135     kernel_cc = bb.data.expand('${KERNEL_CC}', d)
136     kernel_cc = kernel_cc.replace('ccache', '')
137     kernel_cc = kernel_cc.strip()
138     return kernel_cc
139
140
141 def create_cross_kernel_env(bb,d):
142     import tarfile, socket, time, os
143     ice_dir = bb.data.expand('${CROSS_DIR}', d)
144     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
145     distro  = bb.data.expand('${DISTRO}', d)
146     target_sys = bb.data.expand('${TARGET_SYS}',  d)
147     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
148     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
149     name    = socket.gethostname()
150     kernel_cc = get_cross_kernel_cc(bb, d)
151
152     # Stupid check to determine if we have built a libc and a cross
153     # compiler.
154     try:
155         os.stat(os.path.join(ice_dir, 'bin', kernel_cc))
156     except: # no cross compiler built yet
157         bb.error('no cross compiler built yet')
158         return ""
159
160     VERSION = icc_determine_gcc_version( os.path.join(ice_dir,"bin",kernel_cc) )
161     cross_name = prefix + distro + "-" + target_sys + "-" + float + "-" + VERSION + "-" + name
162     tar_file = os.path.join(ice_dir, 'ice', cross_name + '.tar.gz')
163
164     try:
165         os.stat(tar_file)
166         # tar file already exists
167         return tar_file
168     except: 
169         try:
170             os.makedirs(os.path.join(ice_dir,'ice'))
171         except:
172             # directory already exists, continue
173             pass
174
175
176     #check if user has specified a specific icecc-create-env script
177     #if not use the OE provided one
178     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
179     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
180            "--silent",
181            os.path.join(ice_dir,'bin',kernel_cc),
182            os.path.join(ice_dir,target_sys,'bin','g++'),
183            os.path.join(ice_dir,target_sys,'bin','as'),
184            os.path.join(ice_dir,"ice",cross_name) ) )
185     return tar_file
186
187
188 def create_env(bb,d):
189
190         #return create_cross_kernel_env(bb,d) 
191
192         if bb.data.inherits_class("native", d):
193           return create_native_env(bb,d)
194         elif bb.data.inherits_class("kernel", d):
195           return create_cross_kernel_env(bb,d)
196         elif bb.data.inherits_class("cross", d):
197           return create_native_env(bb,d)
198         elif bb.data.inherits_class("sdk", d):
199           return create_native_env(bb,d)
200         else:  
201           return create_cross_env(bb,d)
202
203
204 def create_path(compilers, type, bb, d):
205     """
206     Create Symlinks for the icecc in the staging directory
207     """
208     import os
209
210     staging = os.path.join(bb.data.expand('${STAGING_DIR}', d), "ice", type)
211
212     #check if the icecc path is set by the user
213     icecc   = bb.data.getVar('ICECC_PATH', d) or os.popen("%s icecc" % "which").read()[:-1]
214
215     # Create the dir if necessary
216     try:
217         os.stat(staging)
218     except:
219         os.makedirs(staging)
220
221     for compiler in compilers:
222         gcc_path = os.path.join(staging, compiler)
223         try:
224             os.stat(gcc_path)
225         except:
226             os.symlink(icecc, gcc_path)
227
228     return staging + ":"
229
230
231 def use_icc_version(bb,d):
232       icecc_ver = "yes"
233       system_class_blacklist = [ "none" ] 
234
235       for black in system_class_blacklist:
236            if bb.data.inherits_class(black, d):
237               icecc_ver = "no"
238
239       user_class_blacklist =  bb.data.getVar('ICECC_USER_CLASS_BL', d) or "none"
240       user_class_blacklist = user_class_blacklist.split()
241
242       for black in user_class_blacklist:
243            if bb.data.inherits_class(black, d):
244               icecc_ver = "no"
245
246       return icecc_ver
247
248
249 def icc_path(bb,d):
250     package_tmp = bb.data.expand('${PN}', d)
251
252     #"system" package blacklist contains a list of packages that can not distribute compile tasks
253     #for one reason or the other
254     system_package_blacklist = [ "uclibc", "glibc-intermediate", "gcc", "qemu", "bind", "u-boot", "dhcp-forwarder", "enchant" ]
255
256     for black in system_package_blacklist:
257         if black in package_tmp:
258             bb.note(package_tmp, ' found in blacklist, disable icecc')
259             bb.data.setVar("PARALLEL_MAKE" , "", d) 
260             return ""
261
262     #user defined exclusion list
263     user_package_blacklist = bb.data.getVar('ICECC_USER_PACKAGE_BL', d) or ""
264     user_package_blacklist = user_package_blacklist.split()
265
266     for black in user_package_blacklist:
267         if black in package_tmp:
268             bb.data.setVar("PARALLEL_MAKE" , "", d) 
269             return ""
270
271     prefix = bb.data.expand('${HOST_PREFIX}', d)
272
273     if bb.data.inherits_class("cross", d):
274         return create_path( ["gcc", "g++"], "native", bb, d)
275
276     elif bb.data.inherits_class("native", d):
277         return create_path( ["gcc", "g++"], "native", bb, d)
278
279     elif bb.data.inherits_class("kernel", d):
280         return create_path( [get_cross_kernel_cc(bb,d), ], "cross-kernel", bb, d)
281
282     elif len(prefix) == 0:
283         return create_path( ["gcc", "g++"], "native", bb, d)
284
285     else:
286         return create_path( [prefix+"gcc", prefix+"g++"], "cross", bb, d)      
287
288
289 def icc_version(bb,d):
290     return create_env(bb,d)
291
292
293 def check_for_kernel(bb,d):     
294     if  bb.data.inherits_class("kernel", d):
295          return "yes"
296     return "no"
297
298
299 set_icecc_env() {
300     ICECC_PATH=${@icc_path(bb,d)}
301     if test x${ICECC_PATH} != x; then
302         export PATH=${ICECC_PATH}$PATH
303         export CCACHE_PATH=$PATH
304         #check if we are building a kernel and select gcc-cross-kernel
305         if [ "${@check_for_kernel(bb,d)}" = "yes" ]; then
306             export ICECC_CC="${@get_cross_kernel_cc(bb,d)}"
307             export ICECC_CXX="${HOST_PREFIX}g++"
308         else
309             export ICECC_CC="${HOST_PREFIX}gcc"
310             export ICECC_CXX="${HOST_PREFIX}g++"
311         fi
312
313         if [ "${@use_icc_version(bb,d)}" = "yes" ]; then
314             export ICECC_VERSION="${@icc_version(bb,d)}"
315         else
316             export -n ICECC_VERSION
317         fi
318         oenote "set the icecream environment variables: PATH=$PATH, CCACHE_PATH=$CCACHE_PATH, ICECC_CC=$ICECC_CC, ICECC_CXX=$ICECC_CXX, ICECC_VERSION=$ICECC_VERSION"
319     fi
320 }
321
322 do_configure_prepend() {
323     set_icecc_env
324 }
325
326 do_compile_prepend() {
327     set_icecc_env
328 }