merge of '1df6d041b2c60bf99f158bc629911c2bd43cb76b'
[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 #
14 #If ICECC_PATH is not set in local.conf then the class will try to locate it using 'which'
15 #but nothing is sure ;)
16 #
17 #If ICECC_ENV_EXEC is set in local.conf should point to the icecc-create-env script provided by the user
18 #or the default one provided by icecc-create-env.bb  will be used
19 #(NOTE that this is a modified version of the script need it and *not the one that comes with icecc*
20 #
21 #User can specify if specific packages or packages belonging to class should not use icecc to distribute
22 #compile jobs to remote machines, but handled localy, by defining ICECC_USER_CLASS_BL and ICECC_PACKAGE_BL
23 #with the appropriate values in local.conf
24 #########################################################################################
25 #Error checking is kept to minimum so double check any parameters you pass to the class
26 ###########################################################################################
27
28
29 def icc_determine_gcc_version(gcc):
30     """
31     Hack to determine the version of GCC
32
33     'i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5363)'
34     """
35     import os
36     return os.popen("%s --version" % gcc ).readline().split()[2]
37
38 def create_cross_env(bb,d):
39     """
40     Create a tar.bz2 of the current toolchain
41     """
42
43     # Constin native-native compilation no environment needed if
44     # host prefix is empty (let us duplicate the query for ease)
45     prefix = bb.data.expand('${HOST_PREFIX}', d)
46     if len(prefix) == 0:
47         return ""
48
49     import tarfile, socket, time, os
50     ice_dir = bb.data.expand('${CROSS_DIR}', d)
51     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
52     distro  = bb.data.expand('${DISTRO}', d)
53     target_sys = bb.data.expand('${TARGET_SYS}',  d)
54     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
55     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
56     name    = socket.gethostname()
57   
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', 'libc.so'))
63         os.stat(os.path.join(ice_dir, target_sys, 'bin', 'g++'))
64     except: # no cross compiler built yet
65         return ""
66
67     VERSION = icc_determine_gcc_version( os.path.join(ice_dir,target_sys,"bin","g++") )
68     cross_name = prefix + distro + "-" + target_sys + "-" + float + "-" + VERSION + "-" + name
69     tar_file = os.path.join(ice_dir, 'ice', cross_name + '.tar.gz')
70
71     try:
72         os.stat(tar_file)
73         # tar file already exists
74         return tar_file
75     except: 
76         try:
77             os.makedirs(os.path.join(ice_dir,'ice'))
78         except:
79             # directory already exists, continue
80             pass
81
82
83     #check if user has specified a specific icecc-create-env script
84     #if not use the OE provided one
85     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
86     #call the modified create-env script
87     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
88            "--silent",
89            os.path.join(ice_dir,target_sys,'bin','gcc'),
90            os.path.join(ice_dir,target_sys,'bin','g++'),
91            os.path.join(ice_dir,target_sys,'bin','as'),
92            os.path.join(ice_dir,"ice",cross_name) ) )
93     return tar_file
94
95
96 def create_native_env(bb,d):
97
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     
108     archive_name = "local-host-env" + "-" + name
109     tar_file = os.path.join(ice_dir, 'ice', archive_name + '.tar.gz')
110
111     try:
112         os.stat(tar_file)
113         # tar file already exists
114         return tar_file
115     except: 
116         try:
117             #os.makedirs(os.path.join(ice_dir))
118             os.makedirs(os.path.join(ice_dir,'ice'))
119         except:
120             # directory already exists, continue
121             pass
122
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.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
127     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
128            "--silent",
129            os.popen("%s gcc" % "which").read()[:-1],
130            os.popen("%s g++" % "which").read()[:-1],
131            os.popen("%s as" % "which").read()[:-1],
132            os.path.join(ice_dir,"ice",archive_name) ) )
133     return tar_file
134
135
136
137 def create_cross_kernel_env(bb,d):
138
139     import tarfile, socket, time, os
140     ice_dir = bb.data.expand('${CROSS_DIR}', d)
141     prefix  = bb.data.expand('${HOST_PREFIX}' , d)
142     distro  = bb.data.expand('${DISTRO}', d)
143     target_sys = bb.data.expand('${TARGET_SYS}',  d)
144     target_prefix = bb.data.expand('${TARGET_PREFIX}',  d)
145     float   = bb.data.getVar('TARGET_FPU', d) or "hard"
146     name    = socket.gethostname()
147     kernel_cc = bb.data.expand('${KERNEL_CC}', d)
148     kernel_cc = kernel_cc[:-1]
149   
150
151     # Stupid check to determine if we have built a libc and a cross
152     # compiler.
153     try:
154        os.stat(os.path.join(ice_dir, 'bin', kernel_cc))
155     except: # no cross compiler built yet
156         return ""
157
158     VERSION = icc_determine_gcc_version( os.path.join(ice_dir,"bin",kernel_cc) )
159     cross_name = prefix + distro + "-" + target_sys + "-" + float + "-" + VERSION + "-" + name
160     tar_file = os.path.join(ice_dir, 'ice', cross_name + '.tar.gz')
161
162     try:
163         os.stat(tar_file)
164         # tar file already exists
165         return tar_file
166     except: 
167         try:
168             os.makedirs(os.path.join(ice_dir,'ice'))
169         except:
170             # directory already exists, continue
171             pass
172
173
174     #check if user has specified a specific icecc-create-env script
175     #if not use the OE provided one
176     cr_env_script = bb.data.getVar('ICECC_ENV_EXEC',  d) or  bb.data.expand('${STAGING_DIR}', d)+"/ice/icecc-create-env"
177     result=os.popen("%s %s %s %s %s %s" %(cr_env_script,
178            "--silent",
179            os.path.join(ice_dir,'bin',kernel_cc),
180            os.path.join(ice_dir,target_sys,'bin','g++'),
181            os.path.join(ice_dir,target_sys,'bin','as'),
182            os.path.join(ice_dir,"ice",cross_name) ) )
183     return tar_file
184
185
186 def create_env(bb,d):
187
188         #return create_cross_kernel_env(bb,d) 
189         if bb.data.inherits_class("native", d):
190           return create_native_env(bb,d)
191         elif bb.data.inherits_class("kernel", d):
192           return create_cross_kernel_env(bb,d)
193         elif bb.data.inherits_class("cross", d):
194           return create_native_env(bb,d)
195         else:  
196           return create_cross_env(bb,d)
197         
198        
199 def create_path(compilers, type, bb, d):
200     """
201     Create Symlinks for the icecc in the staging directory
202     """
203     import os
204
205     staging = os.path.join(bb.data.expand('${STAGING_DIR}', d), "ice", type)
206
207     #check if the icecc path is set by the user
208     icecc   = bb.data.getVar('ICECC_PATH', d) or os.popen("%s icecc" % "which").read()[:-1]
209
210     
211     # Create the dir if necessary
212     try:
213         os.stat(staging)
214     except:
215         os.makedirs(staging)
216
217     for compiler in compilers:
218         gcc_path = os.path.join(staging, compiler)
219         try:
220             os.stat(gcc_path)
221         except:
222             os.symlink(icecc, gcc_path)
223
224     return staging + ":"
225
226
227
228
229
230 def use_icc_version(bb,d):
231
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
240       user_class_blacklist =  bb.data.getVar('ICECC_USER_CLASS_BL', d) or "none"
241       user_class_blacklist = user_class_blacklist.split()
242       
243       for black in user_class_blacklist:
244            if bb.data.inherits_class(black, d):
245               icecc_ver = "no"
246  
247       return icecc_ver
248
249
250
251 def icc_path(bb,d,compile):
252     package_tmp = bb.data.expand('${PN}', d)
253
254     #"system" package blacklist contains a list of packages that can not distribute compile tasks
255     #for one reason or the other
256     system_package_blacklist = [ "ulibc", "glibc", "qemu" ]
257
258     for black in system_package_blacklist:
259       if black in package_tmp:
260          return ""
261
262     #user defined exclusion list
263     user_package_blacklist = bb.data.getVar('ICECC_USER_PACKAGE_BL', d) or "none"   
264     user_package_blacklist = user_package_blacklist.split()
265
266     for black in user_package_blacklist:
267       if black in package_tmp:
268          return ""
269
270
271     prefix = bb.data.expand('${HOST_PREFIX}', d)
272
273             
274     if compile and bb.data.inherits_class("cross", d):
275        return create_path( ["gcc", "g++"], "native", bb, d)
276
277     elif compile and bb.data.inherits_class("native", d):
278          return create_path( ["gcc", "g++"], "native", bb, d)
279
280     elif compile and bb.data.inherits_class("kernel", d):
281          #kernel_cc = bb.data.expand('${KERNEL_CC}', d) 
282           return create_path( [get_cross_kernel_ver(bb,d), "foo"], "cross-kernel", bb, d)
283
284     elif not compile or len(prefix) == 0:
285            return create_path( ["gcc", "g++"], "native", bb, d)
286
287     else:
288            return create_path( [prefix+"gcc", prefix+"g++"], "cross", bb, d)      
289
290
291
292
293 def icc_version(bb,d):
294     return create_env(bb,d)
295
296 def check_for_kernel(bb,d):     
297      if  bb.data.inherits_class("kernel", d):
298        return "yes"
299
300        return "no"
301
302
303 def get_cross_kernel_ver(bb,d):
304
305        return  bb.data.expand('${KERNEL_CC}', d).strip() or "gcc"
306  
307 # set the icecream environment variables
308 do_configure_prepend() {
309     export PATH=${@icc_path(bb,d,False)}$PATH
310     export ICECC_CC="gcc"
311     export ICECC_CXX="g++"
312 }
313
314 do_compile_prepend() {
315
316     export PATH=${@icc_path(bb,d,True)}$PATH
317
318  #check if we are building a kernel and select gcc-cross-kernel
319  if [ "${@check_for_kernel(bb,d)}" = "yes" ]; then
320     export ICECC_CC="${@get_cross_kernel_ver(bb,d)}"
321     export ICECC_CXX="${HOST_PREFIX}g++"
322  else
323     export ICECC_CC="${HOST_PREFIX}gcc"
324     export ICECC_CXX="${HOST_PREFIX}g++"
325  fi
326
327     if [ "${@use_icc_version(bb,d)}" = "yes" ]; then
328         export ICECC_VERSION="${@icc_version(bb,d)}"
329     else
330         export ICECC_VERSION="NONE"
331     fi
332 }
333