cprover
ms_cl_mode.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Visual Studio CL Mode
4 
5 Author: CM Wintersteiger, 2006
6 
7 \*******************************************************************/
8 
11 
12 #include "ms_cl_mode.h"
13 
14 #ifdef _WIN32
15 #define EX_OK 0
16 #define EX_USAGE 64
17 #define EX_SOFTWARE 70
18 #else
19 #include <sysexits.h>
20 #endif
21 
22 #include <iostream>
23 
24 #include <util/config.h>
25 #include <util/file_util.h>
26 #include <util/get_base_name.h>
27 #include <util/message.h>
28 #include <util/prefix.h>
29 
30 #include "compile.h"
31 #include "ms_cl_version.h"
32 
33 static bool has_directory_suffix(const std::string &path)
34 {
35  // MS CL decides whether a parameter is a directory on the
36  // basis of the / or \\ suffix; it doesn't matter
37  // whether the directory actually exists.
38  return path.empty() ? false :
39  path.back()=='/' || path.back()=='\\';
40 }
41 
44 {
45  if(cmdline.isset('?') ||
46  cmdline.isset("help"))
47  {
48  help();
49  return EX_OK;
50  }
51 
52  compilet compiler(cmdline, message_handler, cmdline.isset("WX"));
53 
54  #if 0
55  bool act_as_ld=
56  has_prefix(base_name, "link") ||
57  has_prefix(base_name, "goto-link");
58  #endif
59 
60  const auto verbosity = messaget::eval_verbosity(
62 
63  ms_cl_versiont ms_cl_version;
64  ms_cl_version.get("cl.exe");
65 
67  log.debug() << "Visual Studio mode " << ms_cl_version << messaget::eom;
68 
69  // model validation
70  compiler.validate_goto_model = cmdline.isset("validate-goto-model");
71 
72  // get configuration
74 
75  if(ms_cl_version.target == ms_cl_versiont::targett::x86)
77  else if(ms_cl_version.target == ms_cl_versiont::targett::ARM)
79  else if(ms_cl_version.target == ms_cl_versiont::targett::x86)
81 
83  compiler.object_file_extension="obj";
84 
85  // determine actions to be undertaken
86 
87  if(cmdline.isset('E') || cmdline.isset('P'))
89  else if(cmdline.isset('c'))
91  else
93 
94  if(cmdline.isset("std"))
95  {
96  const std::string std_string = cmdline.get_value("std");
97 
98  if(
99  std_string == ":c++14" || std_string == "=c++14" ||
100  std_string == ":c++17" || std_string == "=c++17" ||
101  std_string == ":c++latest" || std_string == "=c++latest")
102  {
103  // we don't have any newer version at the moment
104  config.cpp.set_cpp14();
105  }
106  else if(std_string == ":c++11" || std_string == "=c++11")
107  {
108  // this isn't really a Visual Studio variant, we just do this for GCC
109  // command-line compatibility
110  config.cpp.set_cpp11();
111  }
112  else
113  {
114  log.warning() << "unknown language standard " << std_string
115  << messaget::eom;
116  }
117  }
118  else
119  config.cpp.set_cpp14();
120 
121  compiler.echo_file_name=true;
122 
123  if(cmdline.isset("Fo"))
124  {
125  std::string Fo_value = cmdline.get_value("Fo");
126 
127  // this could be a directory or a file name
128  if(has_directory_suffix(Fo_value))
129  {
130  compiler.output_directory_object = Fo_value;
131 
132  if(!is_directory(Fo_value))
133  log.warning() << "not a directory: " << Fo_value << messaget::eom;
134  }
135  else
136  compiler.output_file_object = Fo_value;
137  }
138 
139  if(
140  compiler.mode == compilet::COMPILE_ONLY &&
141  cmdline.args.size() > 1 &&
142  compiler.output_directory_object.empty())
143  {
144  log.error() << "output directory required for /c with multiple input files"
145  << messaget::eom;
146  return EX_USAGE;
147  }
148 
149  if(cmdline.isset("Fe"))
150  {
152 
153  // this could be a directory
154  if(
156  cmdline.args.size() >= 1)
157  {
158  if(!is_directory(compiler.output_file_executable))
159  {
160  log.warning() << "not a directory: " << compiler.output_file_executable
161  << messaget::eom;
162  }
163 
164  compiler.output_file_executable+=
165  get_base_name(cmdline.args[0], true) + ".exe";
166  }
167  }
168  else
169  {
170  // We need at least one argument.
171  // CL uses the first file name it gets!
172  if(cmdline.args.size()>=1)
173  compiler.output_file_executable=
174  get_base_name(cmdline.args[0], true)+".exe";
175  }
176 
177  if(cmdline.isset('J'))
179 
180  if(verbosity > messaget::M_STATISTICS)
181  {
182  std::list<std::string>::iterator it;
183 
184  std::cout << "Defines:\n";
185  for(it=config.ansi_c.defines.begin();
186  it!=config.ansi_c.defines.end();
187  it++)
188  {
189  std::cout << " " << (*it) << '\n';
190  }
191 
192  std::cout << "Undefines:\n";
193  for(it=config.ansi_c.undefines.begin();
194  it!=config.ansi_c.undefines.end();
195  it++)
196  {
197  std::cout << " " << (*it) << '\n';
198  }
199 
200  std::cout << "Preprocessor Options:\n";
201  for(it=config.ansi_c.preprocessor_options.begin();
203  it++)
204  {
205  std::cout << " " << (*it) << '\n';
206  }
207 
208  std::cout << "Include Paths:\n";
209  for(it=config.ansi_c.include_paths.begin();
210  it!=config.ansi_c.include_paths.end();
211  it++)
212  {
213  std::cout << " " << (*it) << '\n';
214  }
215 
216  std::cout << "Library Paths:\n";
217  for(it=compiler.library_paths.begin();
218  it!=compiler.library_paths.end();
219  it++)
220  {
221  std::cout << " " << (*it) << '\n';
222  }
223 
224  std::cout << "Output file (object): "
225  << compiler.output_file_object << '\n';
226  std::cout << "Output file (executable): "
227  << compiler.output_file_executable << '\n';
228  }
229 
230  // Parse input program, convert to goto program, write output
231  return compiler.doit() ? EX_USAGE : EX_OK;
232 }
233 
236 {
237  std::cout << "goto-cl understands the options of CL plus the following.\n\n";
238 }
std::string get_value(char option) const
Definition: cmdline.cpp:47
virtual bool isset(char option) const
Definition: cmdline.cpp:29
argst args
Definition: cmdline.h:143
bool echo_file_name
Definition: compile.h:30
@ PREPROCESS_ONLY
Definition: compile.h:33
@ COMPILE_LINK_EXECUTABLE
Definition: compile.h:38
@ COMPILE_ONLY
Definition: compile.h:34
std::string output_file_object
Definition: compile.h:50
std::string output_directory_object
Definition: compile.h:50
enum compilet::@4 mode
bool doit()
reads and source and object files, compiles and links them into goto program objects.
Definition: compile.cpp:58
std::string object_file_extension
Definition: compile.h:46
bool validate_goto_model
Definition: compile.h:31
std::list< std::string > library_paths
Definition: compile.h:41
std::string output_file_executable
Definition: compile.h:47
bool set(const cmdlinet &cmdline)
Definition: config.cpp:798
struct configt::cppt cpp
struct configt::ansi_ct ansi_c
const std::string base_name
Definition: goto_cc_mode.h:38
void help()
display command line help
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
static unsigned eval_verbosity(const std::string &user_input, const message_levelt default_verbosity, message_handlert &dest)
Parse a (user-)provided string as a verbosity level and set it as the verbosity of dest.
Definition: message.cpp:104
@ M_STATISTICS
Definition: message.h:171
@ M_ERROR
Definition: message.h:170
static eomt eom
Definition: message.h:297
ms_cl_cmdlinet & cmdline
Definition: ms_cl_mode.h:36
cl_message_handlert message_handler
Definition: ms_cl_mode.h:37
virtual int doit()
does it.
Definition: ms_cl_mode.cpp:43
virtual void help_mode()
display command line help
Definition: ms_cl_mode.cpp:235
void get(const std::string &executable)
enum ms_cl_versiont::targett target
Compile and link source and object files.
configt config
Definition: config.cpp:24
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
bool is_directory(const std::string &path)
Definition: file_util.cpp:187
std::string get_base_name(const std::string &in, bool strip_suffix)
cleans a filename from path and extension
static bool has_directory_suffix(const std::string &path)
Definition: ms_cl_mode.cpp:33
Visual Studio CL Mode.
std::list< std::string > include_paths
Definition: config.h:205
void set_32()
Definition: config.cpp:31
std::list< std::string > undefines
Definition: config.h:203
void set_64()
Definition: config.cpp:36
std::list< std::string > preprocessor_options
Definition: config.h:204
std::list< std::string > defines
Definition: config.h:202
bool char_is_unsigned
Definition: config.h:124
flavourt mode
Definition: config.h:196
void set_cpp14()
Definition: config.h:234
void set_cpp11()
Definition: config.h:233