propagate from branch 'org.openembedded.dev' (head a6b798a43c05aef43ed650ab880f3edd38...
[vuplus_openembedded] / 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 def create_cross_env(bb,d):
40     """
41     Create a tar.bz2 of the current toolchain
42     """
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
60     # Stupid check to determine if we have built a libc and a cross
61     # compiler.
62     try:
63         os.stat(os.path.join(ice_dir, target_sys, 'lib', 'libc.so'))
64         os.stat(os.path.join(ice_dir, target_sys, 'bin', 'g++'))
65     except: # 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
99     import tarfile, socket, time, os
100     ice_dir = bb.data.expand('${CROSS_DIR}', d)
101     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
102     distro  = bb.data.expand('${DISTRO}', d)
103     target_sys = bb.data.expand('${TARGET_SYS}',  d)
104     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
105     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
106     name    = socket.gethostname()
107   
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
125     #check if user has specified a specific icecc-create-env script
126     #if not use the OE provided one
127     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
128     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
129            "--silent",
130            os.popen("%s gcc" % "which").read()[:-1],
131            os.popen("%s g++" % "which").read()[:-1],
132            os.popen("%s as" % "which").read()[:-1],
133            os.path.join(ice_dir,"ice",archive_name) ) )
134     return tar_file
135
136
137
138 def create_cross_kernel_env(bb,d):
139
140     import tarfile, socket, time, os
141     ice_dir = bb.data.expand('${CROSS_DIR}', d)
142     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
143     distro  = bb.data.expand('${DISTRO}', d)
144     target_sys = bb.data.expand('${TARGET_SYS}',  d)
145     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
146     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
147     name    = socket.gethostname()
148     kernel_cc = bb.data.expand('${KERNEL_CC}', d)
149     kernel_cc = kernel_cc[:-1]
150   
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         return ""
158
159     VERSION = icc_determine_gcc_version( os.path.join(ice_dir,"bin",kernel_cc) )
160     cross_name = prefix + distro + "-" + target_sys + "-" + float + "-" + VERSION + "-" + name
161     tar_file = os.path.join(ice_dir, 'ice', cross_name + '.tar.gz')
162
163     try:
164         os.stat(tar_file)
165         # tar file already exists
166         return tar_file
167     except: 
168         try:
169             os.makedirs(os.path.join(ice_dir,'ice'))
170         except:
171             # directory already exists, continue
172             pass
173
174
175     #check if user has specified a specific icecc-create-env script
176     #if not use the OE provided one
177     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
178     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
179            "--silent",
180            os.path.join(ice_dir,'bin',kernel_cc),
181            os.path.join(ice_dir,target_sys,'bin','g++'),
182            os.path.join(ice_dir,target_sys,'bin','as'),
183            os.path.join(ice_dir,"ice",cross_name) ) )
184     return tar_file
185
186
187 def create_env(bb,d):
188
189         #return create_cross_kernel_env(bb,d) 
190
191         if bb.data.inherits_class("native", d):
192           return create_native_env(bb,d)
193         elif bb.data.inherits_class("kernel", d):
194           return create_cross_kernel_env(bb,d)
195         elif bb.data.inherits_class("cross", d):
196           return create_native_env(bb,d)
197         elif bb.data.inherits_class("sdk", d):
198           return create_native_env(bb,d)
199         else:  
200           return create_cross_env(bb,d)
201         
202        
203 def create_path(compilers, type, bb, d):
204     """
205     Create Symlinks for the icecc in the staging directory
206     """
207     import os
208
209     staging = os.path.join(bb.data.expand('${STAGING_DIR}', d), "ice", type)
210
211     #check if the icecc path is set by the user
212     icecc   = bb.data.getVar('ICECC_PATH', d) or os.popen("%s icecc" % "which").read()[:-1]
213
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
232
233
234 def use_icc_version(bb,d):
235
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
244       user_class_blacklist =  bb.data.getVar('ICECC_USER_CLASS_BL', d) or "none"
245       user_class_blacklist = user_class_blacklist.split()
246       
247       for black in user_class_blacklist:
248            if bb.data.inherits_class(black, d):
249               icecc_ver = "no"
250  
251       return icecc_ver
252
253
254
255 def icc_path(bb,d,compile):
256     package_tmp = bb.data.expand('${PN}', d)
257
258     #"system" package blacklist contains a list of packages that can not distribute compile tasks
259     #for one reason or the other
260     system_package_blacklist = [ "uclibc", "glibc-intermediate", "gcc", "qemu", "bind", "u-boot", "dhcp-forwarder", "enchant" ]
261
262     for black in system_package_blacklist:
263       if black in package_tmp:
264          bb.data.setVar("PARALLEL_MAKE" , "", d) 
265          return ""
266
267     #user defined exclusion list
268     user_package_blacklist = bb.data.getVar('ICECC_USER_PACKAGE_BL', d) or "none"   
269     user_package_blacklist = user_package_blacklist.split()
270
271     for black in user_package_blacklist:
272       if black in package_tmp:
273          bb.data.setVar("PARALLEL_MAKE" , "", d) 
274          return ""
275
276
277     prefix = bb.data.expand('${HOST_PREFIX}', d)
278
279             
280     if compile and bb.data.inherits_class("cross", d):
281        return create_path( ["gcc", "g++"], "native", bb, d)
282
283     elif compile and bb.data.inherits_class("native", d):
284          return create_path( ["gcc", "g++"], "native", bb, d)
285
286     elif compile and bb.data.inherits_class("kernel", d):
287           return create_path( [get_cross_kernel_ver(bb,d), "foo"], "cross-kernel", bb, d)
288
289     elif not compile or len(prefix) == 0:
290            return create_path( ["gcc", "g++"], "native", bb, d)
291
292     else:
293            return create_path( [prefix+"gcc", prefix+"g++"], "cross", bb, d)      
294
295
296
297
298 def icc_version(bb,d):
299     return create_env(bb,d)
300
301 def check_for_kernel(bb,d):     
302      if  bb.data.inherits_class("kernel", d):
303        return "yes"
304
305        return "no"
306
307
308 def get_cross_kernel_ver(bb,d):
309
310        return  bb.data.expand('${KERNEL_CC}', d).strip() or "gcc"
311
312 # set the icecream environment variables
313 do_configure_prepend() {
314     export PATH=${@icc_path(bb,d,False)}$PATH
315     export ICECC_CC="gcc"
316     export ICECC_CXX="g++"
317 }
318
319 do_compile_prepend() {
320
321     export PATH=${@icc_path(bb,d,True)}$PATH
322  
323  #check if we are building a kernel and select gcc-cross-kernel
324  if [ "${@check_for_kernel(bb,d)}" = "yes" ]; then
325     export ICECC_CC="${@get_cross_kernel_ver(bb,d)}"
326     export ICECC_CXX="${HOST_PREFIX}g++"
327  else
328     export ICECC_CC="${HOST_PREFIX}gcc"
329     export ICECC_CXX="${HOST_PREFIX}g++"
330  fi
331
332     if [ "${@use_icc_version(bb,d)}" = "yes" ]; then
333         export ICECC_VERSION="${@icc_version(bb,d)}"
334     else
335         export ICECC_VERSION="NONE"
336     fi
337 }
338