pymtt
 All Classes Namespaces Files Functions Variables Groups
Compilers.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2016-2018 Intel, Inc. All rights reserved.
4 # $COPYRIGHT$
5 #
6 # Additional copyrights may follow
7 #
8 # $HEADER$
9 #
10 
11 from __future__ import print_function
12 import os
13 from BaseMTTUtility import *
14 
15 ## @addtogroup Utilities
16 # @{
17 # @section Compilers
18 # Identify the type and version of compilers in-use
19 # @}
21  def __init__(self):
22  BaseMTTUtility.__init__(self)
23  self.options = {}
24  return
25 
26  def print_name(self):
27  return "Compilers"
28 
29  def print_options(self, testDef, prefix):
30  lines = testDef.printOptions(self.options)
31  for line in lines:
32  print(prefix + line)
33  return
34 
35  def execute(self, log, testDef):
36  # GNU is probably the most common, so check that one as soon as
37  # possible. Intel pretends to be GNU, so need to check Intel
38  # before checking for GNU.
39 
40  # Intel
41  if self.check_c_if(testDef, "defined(__INTEL_COMPILER) || defined(__ICC)", "icc"):
42  compiler = "intel"
43  status, vsn = self.check_version("icc", "--version", testDef)
44  # Pathscale
45  elif self.check_c_ifdef(testDef, "__PATHSCALE__", "pgcc"):
46  compiler = "pathscale"
47  status, vsn = self.check_version("pgcc", "-V", testDef)
48  # GNU
49  elif self.check_c_ifdef(testDef, "__GNUC__", "gcc"):
50  compiler = "gnu"
51  status, vsn = self.check_version("gcc", "--version", testDef)
52  # Borland Turbo C
53  elif self.check_c_ifdef(testDef, "__TURBOC__", "tcc"):
54  compiler = "borland"
55  status, vsn = self.check_version("tcc", "--version", testDef)
56  # Borland C++
57  elif self.check_c_ifdef(testDef, "__BORLANDC__", "cpp"):
58  compiler = "borland"
59  status, vsn = self.check_version("cpp", "--version", testDef)
60  # Compaq C/C++
61  elif self.check_c_ifdef(testDef, "__COMO__", "cc"):
62  compiler = "comeau"
63  status, vsn = self.check_version("cc", "--version", testDef)
64  elif self.check_c_if(testDef, "defined(__DECC) || defined(VAXC) || defined(__VAXC)", "cc"):
65  compiler = "compaq"
66  status, vsn = self.check_version("cc", "--version", testDef)
67  elif self.check_c_if(testDef, "defined(__osf__) || defined(__LANGUAGE_C__)", "cc"):
68  compiler = "compaq"
69  status, vsn = self.check_version("cc", "--version", testDef)
70  elif self.check_c_ifdef(testDef, "__DECCXX", "cc"):
71  compiler = "compaq"
72  status, vsn = self.check_version("cc", "--version", testDef)
73  # Cray C/C++
74  elif self.check_c_ifdef(testDef, "_CRAYC", "cc"):
75  compiler = "cray"
76  status, vsn = self.check_version("cc", "--version", testDef)
77  # Diab C/C++
78  elif self.check_c_ifdef(testDef, "__DCC", "cc"):
79  compiler = "diab"
80  status, vsn = self.check_version("cc", "--version", testDef)
81  # Digital Mars
82  elif self.check_c_if(testDef, "defined(__DMC__) || defined(__SC__) || defined(__ZTC__)", "cc"):
83  compiler = "digital mars"
84  status, vsn = self.check_version("cc", "--version", testDef)
85  # HP ANSI C / aC++
86  elif self.check_c_if(testDef, "defined(__HP_cc) || defined(__HP_aCC)", "cc"):
87  compiler = "hp"
88  status, vsn = self.check_version("cc", "--version", testDef)
89  # IBM XL C/C++
90  elif self.check_c_if(testDef, "defined(__xlC__) || defined(__IBMC__) || defined(__IBMCPP__)", "cc"):
91  compiler = "ibm"
92  status, vsn = self.check_version("cc", "--version", testDef)
93  # IBM XL C/C++
94  elif self.check_c_if(testDef, "defined(_AIX) && defined(__GNUC__)", "cc"):
95  compiler = "ibm"
96  status, vsn = self.check_version("cc", "--version", testDef)
97  # KAI C++ (rest in peace)
98  elif self.check_c_ifdef(testDef, "__KCC", "cc"):
99  compiler = "kai"
100  status, vsn = self.check_version("cc", "--version", testDef)
101  # LCC
102  elif self.check_c_ifdef(testDef, "__LCC__", "cc"):
103  compiler = "lcc"
104  status, vsn = self.check_version("cc", "--version", testDef)
105  # MetaWare High C/C++
106  elif self.check_c_ifdef(testDef, "__HIGHC__", "cc"):
107  compiler = "metaware high"
108  status, vsn = self.check_version("cc", "--version", testDef)
109  # Metrowerks Codewarrior
110  elif self.check_c_ifdef(testDef, "__MWERKS__", "cc"):
111  compiler = "metrowerks"
112  status, vsn = self.check_version("cc", "--version", testDef)
113  # MIPSpro (SGI)
114  elif self.check_c_if(testDef, "defined(sgi) || defined(__sgi)", "cc"):
115  compiler = "sgi"
116  status, vsn = self.check_version("cc", "--version", testDef)
117  # MPW C++
118  elif self.check_c_if(testDef, "defined(__MRC__) || defined(MPW_C) || defined(MPW_CPLUS)", "cpp"):
119  compiler = "mpw"
120  status, vsn = self.check_version("cc", "--version", testDef)
121  # Microsoft
122  # (Always use C compiler when checking for Microsoft, as
123  # Visual C++ doesn't recognize .cc as a C++ file.)
124  elif self.check_c_if(testDef, "defined(_MSC_VER) || defined(__MSC_VER)", "cc"):
125  compiler = "microsoft"
126  status, vsn = self.check_version("cc", "--version", testDef)
127  # Norcroft C
128  elif self.check_c_ifdef(testDef, "__CC_NORCROFT", "cc"):
129  compiler = "norcroft"
130  status, vsn = self.check_version("cc", "--version", testDef)
131  # Pelles C
132  elif self.check_c_ifdef(testDef, "__POCC__", "cc"):
133  compiler = "pelles"
134  status, vsn = self.check_version("cc", "--version", testDef)
135  # Portland Group
136  elif self.check_c_ifdef(testDef, "__PGI", "cc"):
137  compiler = "pgi"
138  status, vsn = self.check_version("cc", "--version", testDef)
139  # SAS/C
140  elif self.check_c_if(testDef, "defined(SASC) || defined(__SASC) || defined(__SASC__)", "cc"):
141  compiler = "sas"
142  status, vsn = self.check_version("cc", "--version", testDef)
143  # Sun Workshop C/C++
144  elif self.check_c_if(testDef, "defined(__SUNPRO_C) || defined(__SUNPRO_CC)", "cc"):
145  compiler = "sun"
146  status, vsn = self.check_version("cc", "--version", testDef)
147  # TenDRA C/C++
148  elif self.check_c_ifdef(testDef, "__TenDRA__", "cc"):
149  compiler = "tendra"
150  status, vsn = self.check_version("cc", "--version", testDef)
151  # Tiny C
152  elif self.check_c_ifdef(testDef, "__TINYC__", "cc"):
153  compiler = "tiny"
154  status, vsn = self.check_version("cc", "--version", testDef)
155  # USL C
156  elif self.check_c_ifdef(testDef, "__USLC__", "cc"):
157  compiler = "usl"
158  status, vsn = self.check_version("cc", "--version", testDef)
159  # Watcom C++
160  elif self.check_c_ifdef(testDef, "__WATCOMC__", "cpp"):
161  compiler = "watcom"
162  status, vsn = self.check_version("cpp", "--version", testDef)
163 
164  else:
165  vsn = [""]
166  status = 1;
167  compiler = "None"
168  version = "Unknown"
169  # record the result
170  log['status'] = status
171  log['compiler'] = compiler
172  log['version'] = vsn[0][:64]
173  return
174 
175  def check_compile(self, testDef, macro, c_code, compiler):
176  # write out a little test program
177  fh = open("spastic.c", 'w')
178  for ln in c_code:
179  print(ln, file=fh)
180  fh.close()
181 
182  # Attempt to compile it
183  mycmdargs = [compiler, "-c", "spastic.c"]
184  status, stdout, stderr, _ = testDef.execmd.execute(None, mycmdargs, testDef)
185 
186  # cleanup the test
187  os.remove("spastic.c")
188  if os.path.exists("spastic.o"):
189  os.remove("spastic.o")
190 
191  if 0 == status:
192  return True
193  return False
194 
195  def check_c_ifdef(self, testDef, macro, compiler):
196 
197  c_code = ["/*", "* This program is automatically generated by compiler.py",
198  "* of MPI Testing Tool (MTT). Any changes you make here may",
199  "* get lost!", "*",
200  "* Copyrights and licenses of this file are the same as for the MTT.",
201  "*/", "#ifndef " + macro, "#error", "choke me", "#endif"]
202 
203  return self.check_compile(testDef, macro, c_code, compiler);
204 
205  def check_c_if(self, testDef, macro, compiler):
206 
207  c_code = ["/*", "* This program is automatically generated by compiler.py",
208  "* of MPI Testing Tool (MTT). Any changes you make here may",
209  "* get lost!", "*",
210  "* Copyrights and licenses of this file are the same as for the MTT.",
211  "*/", "#if !( " + macro + " )", "#error", "choke me", "#endif"]
212 
213  return self.check_compile(testDef, macro, c_code, compiler);
214 
215  def check_version(self, compiler, version, testDef):
216  # try the universal version option
217  mycmdargs = [compiler, version]
218  status, stdout, stderr, _ = testDef.execmd.execute(None, mycmdargs, testDef)
219  return status, stdout
220