3 Tool-specific initialization for the Intel C/C++ compiler.
4 Supports Linux and Windows compilers, v7 and up.
6 There normally shouldn't be any need to import this module directly.
7 It will usually be imported through the generic SCons.Tool.Tool()
15 # Permission is hereby granted, free of charge, to any person obtaining
16 # a copy of this software and associated documentation files (the
17 # "Software"), to deal in the Software without restriction, including
18 # without limitation the rights to use, copy, modify, merge, publish,
19 # distribute, sublicense, and/or sell copies of the Software, and to
20 # permit persons to whom the Software is furnished to do so, subject to
21 # the following conditions:
23 # The above copyright notice and this permission notice shall be included
24 # in all copies or substantial portions of the Software.
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
27 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
28 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
37 import math, sys, os.path, glob, string, re
39 is_windows = sys.platform == 'win32'
40 is_win64 = is_windows and (os.environ['PROCESSOR_ARCHITECTURE'] == 'AMD64' or
41 ('PROCESSOR_ARCHITEW6432' in os.environ and
42 os.environ['PROCESSOR_ARCHITEW6432'] == 'AMD64'))
43 is_linux = sys.platform == 'linux2'
44 is_mac = sys.platform == 'darwin'
47 import SCons.Tool.msvc
55 # Exceptions for this tool
56 class IntelCError(SCons.Errors.InternalError):
58 class MissingRegistryError(IntelCError): # missing registry entry
60 class MissingDirError(IntelCError): # dir not found
62 class NoRegistryModuleError(IntelCError): # can't read registry at all
66 """Return a sequence containing only one copy of each unique element from input sequence s.
67 Does not preserve order.
68 Input sequence must be hashable (i.e. must be usable as a dictionary key)."""
74 def linux_ver_normalize(vstr):
75 """Normalize a Linux compiler version number.
76 Intel changed from "80" to "9.0" in 2005, so we assume if the number
77 is greater than 60 it's an old-style number and otherwise new-style.
78 Always returns an old-style float like 80 or 90 for compatibility with Windows.
80 # Check for version number like 9.1.026: return 91.026
81 m = re.match(r'([0-9]+)\.([0-9]+)\.([0-9]+)', vstr)
83 vmaj,vmin,build = m.groups()
84 return float(vmaj) * 10 + float(vmin) + float(build) / 1000.;
90 if f < 60: return f * 10.0
94 """Check for valid ABI (application binary interface) name,
95 and map into canonical one"""
99 # valid_abis maps input name to canonical name
101 valid_abis = {'ia32' : 'ia32',
107 valid_abis = {'ia32' : 'ia32',
113 valid_abis = {'ia32' : 'ia32',
118 abi = valid_abis[abi]
120 raise SCons.Errors.UserError("Intel compiler: Invalid ABI %s, valid values are %s"% \
121 (abi, valid_abis.keys()))
125 """Compare strings as floats,
126 but Intel changed Linux naming convention at 9.0"""
127 return cmp(linux_ver_normalize(b), linux_ver_normalize(a))
129 def get_version_from_list(v, vlist):
130 """See if we can match v (string) in vlist (list of strings)
131 Linux has to match in a fuzzy way."""
133 # Simple case, just find it in the list
134 if v in vlist: return v
137 # Fuzzy match: normalize version number first, but still return
138 # original non-normalized form.
141 if math.fabs(linux_ver_normalize(vi) - linux_ver_normalize(v)) < fuzz:
146 def get_intel_registry_value(valuename, version=None, abi=None):
148 Return a value from the Intel compiler registry tree. (Windows only)
152 K = 'Software\\Wow6432Node\\Intel\\Compilers\\C++\\' + version + '\\'+abi.upper()
154 K = 'Software\\Intel\\Compilers\\C++\\' + version + '\\'+abi.upper()
156 k = SCons.Util.RegOpenKeyEx(SCons.Util.HKEY_LOCAL_MACHINE, K)
157 except SCons.Util.RegError:
158 raise MissingRegistryError("%s was not found in the registry, for Intel compiler version %s, abi='%s'"%(K, version,abi))
162 v = SCons.Util.RegQueryValueEx(k, valuename)[0]
163 return v # or v.encode('iso-8859-1', 'replace') to remove unicode?
164 except SCons.Util.RegError:
165 raise MissingRegistryError("%s\\%s was not found in the registry."%(K, valuename))
168 def get_all_compiler_versions():
169 """Returns a sorted list of strings, like "70" or "80" or "9.0"
170 with most recent compiler version first.
175 keyname = 'Software\\WoW6432Node\\Intel\\Compilers\\C++'
177 keyname = 'Software\\Intel\\Compilers\\C++'
179 k = SCons.Util.RegOpenKeyEx(SCons.Util.HKEY_LOCAL_MACHINE,
187 subkey = SCons.Util.RegEnumKey(k, i) # raises EnvironmentError
188 # Check that this refers to an existing dir.
189 # This is not 100% perfect but should catch common
190 # installation issues like when the compiler was installed
191 # and then the install directory deleted or moved (rather
192 # than uninstalling properly), so the registry values
195 for try_abi in ('IA32', 'IA32e', 'IA64', 'EM64T'):
197 d = get_intel_registry_value('ProductDir', subkey, try_abi)
198 except MissingRegistryError:
199 continue # not found in reg, keep going
200 if os.path.exists(d): ok = True
202 versions.append(subkey)
205 # Registry points to nonexistent dir. Ignore this
207 value = get_intel_registry_value('ProductDir', subkey, 'IA32')
208 except MissingRegistryError, e:
210 # Registry key is left dangling (potentially
211 # after uninstalling).
214 "scons: *** Ignoring the registry key for the Intel compiler version %s.\n" \
215 "scons: *** It seems that the compiler was uninstalled and that the registry\n" \
216 "scons: *** was not cleaned up properly.\n" % subkey
218 print "scons: *** Ignoring "+str(value)
221 except EnvironmentError:
225 for d in glob.glob('/opt/intel_cc_*'):
226 # Typical dir here is /opt/intel_cc_80.
227 m = re.search(r'cc_(.*)$', d)
229 versions.append(m.group(1))
230 for d in glob.glob('/opt/intel/cc*/*'):
231 # Typical dir here is /opt/intel/cc/9.0 for IA32,
232 # /opt/intel/cce/9.0 for EMT64 (AMD64)
233 m = re.search(r'([0-9.]+)$', d)
235 versions.append(m.group(1))
237 for d in glob.glob('/opt/intel/cc*/*'):
238 # Typical dir here is /opt/intel/cc/9.0 for IA32,
239 # /opt/intel/cce/9.0 for EMT64 (AMD64)
240 m = re.search(r'([0-9.]+)$', d)
242 versions.append(m.group(1))
243 return sorted(uniquify(versions)) # remove dups
245 def get_intel_compiler_top(version, abi):
247 Return the main path to the top-level dir of the Intel compiler,
248 using the given version.
249 The compiler will be in <top>/bin/icl.exe (icc on linux),
250 the include dir is <top>/include, etc.
254 if not SCons.Util.can_read_reg:
255 raise NoRegistryModuleError("No Windows registry module was found")
256 top = get_intel_registry_value('ProductDir', version, abi)
257 # pre-11, icl was in Bin. 11 and later, it's in Bin/<abi> apparently.
258 if not os.path.exists(os.path.join(top, "Bin", "icl.exe")) \
259 and not os.path.exists(os.path.join(top, "Bin", abi, "icl.exe")):
260 raise MissingDirError("Can't find Intel compiler in %s"%(top))
261 elif is_mac or is_linux:
262 # first dir is new (>=9.0) style, second is old (8.0) style.
263 dirs=('/opt/intel/cc/%s', '/opt/intel_cc_%s')
265 dirs=('/opt/intel/cce/%s',) # 'e' stands for 'em64t', aka x86_64 aka amd64
268 if os.path.exists(os.path.join(d%version, "bin", "icc")):
272 raise MissingDirError("Can't find version %s Intel compiler in %s (abi='%s')"%(version,top, abi))
276 def generate(env, version=None, abi=None, topdir=None, verbose=0):
277 """Add Builders and construction variables for Intel C/C++ compiler
280 version: (string) compiler version to use, like "80"
281 abi: (string) 'win32' or whatever Itanium version wants
282 topdir: (string) compiler top dir, like
283 "c:\Program Files\Intel\Compiler70"
284 If topdir is used, version and abi are ignored.
285 verbose: (int) if >0, prints compiler version used.
287 if not (is_mac or is_linux or is_windows):
288 # can't handle this platform
292 SCons.Tool.msvc.generate(env)
294 SCons.Tool.gcc.generate(env)
296 SCons.Tool.gcc.generate(env)
298 # if version is unspecified, use latest
299 vlist = get_all_compiler_versions()
304 # User may have specified '90' but we need to get actual dirname '9.0'.
305 # get_version_from_list does that mapping.
306 v = get_version_from_list(version, vlist)
308 raise SCons.Errors.UserError("Invalid Intel compiler version %s: "%version + \
309 "installed versions are %s"%(', '.join(vlist)))
312 # if abi is unspecified, use ia32
313 # alternatives are ia64 for Itanium, or amd64 or em64t or x86_64 (all synonyms here)
316 if is_mac or is_linux:
317 # Check if we are on 64-bit linux, default to 64 then.
318 uname_m = os.uname()[4]
319 if uname_m == 'x86_64':
329 if version and not topdir:
331 topdir = get_intel_compiler_top(version, abi)
332 except (SCons.Util.RegError, IntelCError):
336 # Normally this is an error, but it might not be if the compiler is
337 # on $PATH and the user is importing their env.
338 class ICLTopDirWarning(SCons.Warnings.Warning):
340 if (is_mac or is_linux) and not env.Detect('icc') or \
341 is_windows and not env.Detect('icl'):
343 SCons.Warnings.enableWarningClass(ICLTopDirWarning)
344 SCons.Warnings.warn(ICLTopDirWarning,
345 "Failed to find Intel compiler for version='%s', abi='%s'"%
346 (str(version), str(abi)))
348 # should be cleaned up to say what this other version is
349 # since in this case we have some other Intel compiler installed
350 SCons.Warnings.enableWarningClass(ICLTopDirWarning)
351 SCons.Warnings.warn(ICLTopDirWarning,
352 "Can't find Intel compiler top dir for version='%s', abi='%s'"%
353 (str(version), str(abi)))
357 print "Intel C compiler: using version %s (%g), abi %s, in '%s'"%\
358 (repr(version), linux_ver_normalize(version),abi,topdir)
360 # Show the actual compiler version by running the compiler.
361 os.system('%s/bin/icc --version'%topdir)
363 # Show the actual compiler version by running the compiler.
364 os.system('%s/bin/icc --version'%topdir)
366 env['INTEL_C_COMPILER_TOP'] = topdir
368 paths={'INCLUDE' : 'include',
371 'LD_LIBRARY_PATH' : 'lib'}
372 for p in paths.keys():
373 env.PrependENVPath(p, os.path.join(topdir, paths[p]))
375 paths={'INCLUDE' : 'include',
378 'LD_LIBRARY_PATH' : 'lib'}
379 for p in paths.keys():
380 env.PrependENVPath(p, os.path.join(topdir, paths[p]))
382 # env key reg valname default subdir of top
383 paths=(('INCLUDE', 'IncludeDir', 'Include'),
384 ('LIB' , 'LibDir', 'Lib'),
385 ('PATH' , 'BinDir', 'Bin'))
386 # We are supposed to ignore version if topdir is set, so set
387 # it to the emptry string if it's not already set.
390 # Each path has a registry entry, use that or default to subdir
393 path=get_intel_registry_value(p[1], version, abi)
394 # These paths may have $(ICInstallDir)
395 # which needs to be substituted with the topdir.
396 path=path.replace('$(ICInstallDir)', topdir + os.sep)
398 # Couldn't get it from registry: use default subdir of topdir
399 env.PrependENVPath(p[0], os.path.join(topdir, p[2]))
401 env.PrependENVPath(p[0], path.split(os.pathsep))
402 # print "ICL %s: %s, final=%s"%(p[0], path, str(env['ENV'][p[0]]))
407 env['LINK'] = 'xilink'
411 # Don't reset LINK here;
412 # use smart_link which should already be here from link.py.
415 env['LD'] = 'xild' # not used by default
417 # This is not the exact (detailed) compiler version,
418 # just the major version as determined above or specified
419 # by the user. It is a float like 80 or 90, in normalized form for Linux
420 # (i.e. even for Linux 9.0 compiler, still returns 90 rather than 9.0)
422 env['INTEL_C_COMPILER_VERSION']=linux_ver_normalize(version)
425 # Look for license file dir
426 # in system environment, registry, and default location.
427 envlicdir = os.environ.get("INTEL_LICENSE_FILE", '')
428 K = ('SOFTWARE\Intel\Licenses')
430 k = SCons.Util.RegOpenKeyEx(SCons.Util.HKEY_LOCAL_MACHINE, K)
431 reglicdir = SCons.Util.RegQueryValueEx(k, "w_cpp")[0]
432 except (AttributeError, SCons.Util.RegError):
434 defaultlicdir = r'C:\Program Files\Common Files\Intel\Licenses'
437 for ld in [envlicdir, reglicdir]:
438 # If the string contains an '@', then assume it's a network
439 # license (port@system) and good by definition.
440 if ld and (ld.find('@') != -1 or os.path.exists(ld)):
444 licdir = defaultlicdir
445 if not os.path.exists(licdir):
446 class ICLLicenseDirWarning(SCons.Warnings.Warning):
448 SCons.Warnings.enableWarningClass(ICLLicenseDirWarning)
449 SCons.Warnings.warn(ICLLicenseDirWarning,
450 "Intel license dir was not found."
451 " Tried using the INTEL_LICENSE_FILE environment variable (%s), the registry (%s) and the default path (%s)."
452 " Using the default path as a last resort."
453 % (envlicdir, reglicdir, defaultlicdir))
454 env['ENV']['INTEL_LICENSE_FILE'] = licdir
457 if not (is_mac or is_linux or is_windows):
458 # can't handle this platform
462 versions = get_all_compiler_versions()
463 except (SCons.Util.RegError, IntelCError):
465 detected = versions is not None and len(versions) > 0
467 # try env.Detect, maybe that will work
469 return env.Detect('icl')
471 return env.Detect('icc')
473 return env.Detect('icc')
480 # indent-tabs-mode:nil
482 # vim: set expandtab tabstop=4 shiftwidth=4: