Code::Blocks  SVN r11506
compiler.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  */
5 
6 #ifndef COMPILER_H
7 #define COMPILER_H
8 
9 #include <vector>
10 #include <map>
11 
12 #include <wx/string.h>
13 #include <wx/filename.h>
14 #include <wx/dynarray.h>
15 #include <wx/regex.h>
16 #include "compileoptionsbase.h"
17 #include "compileroptions.h"
18 
20 class cbProject;
21 class ProjectBuildTarget;
22 class ProjectFile;
23 class wxXmlNode;
24 
25 /*
26  Macros used in command specs:
27  (keep in sync with `src/plugins/compilergcc/resources/advanced_compiler_options.xrc`)
28 
29  Compiler executable: $compiler
30  Resource compiler executable: $rescomp
31  Linker executable: $linker
32  Linker executable for static libs: $lib_linker
33  Compiler flags: $options
34  Resource compiler flags: $res_options
35  Linker flags: $link_options
36  Compiler include paths: $includes
37  Resource include paths: $res_includes
38  Linker include paths: $libdirs
39  Link libraries: $libs
40  Source file (full name): $file
41  Source file dir (no name, no ext.): $file_dir
42  Source file name (no path, no ext.): $file_name
43  Source file extension: $file_ext
44  Object file: $object
45  Dependency result: $dep_object
46  All *linkable* object files: $link_objects
47  All *linkable* flat object files: $link_flat_objects
48  All *linkable* resource object files: $link_resobjects
49  Executable output file (full name): $exe_output
50  Executable dir (no name, no ext): $exe_dir
51  Executable name (no path, no ext): $exe_name
52  Executable extension: $exe_ext
53  Static library output file: $static_output
54  Dynamic library output file: $exe_output
55  Dynamic library DEF output file: $def_output
56  Resources output file: $resource_output
57  Objects output dir: $objects_output_dir
58 
59  Usual special chars apply: \t, \n, etc.
60  The command output should be ready for inclusion
61  in a Makefile (properly tab aligned but *no* tabs at the start of the string)
62  The macro substitution happens in compiler's MakefileGenerator.
63 */
64 
67 {
68  cltNormal = 0,
72 };
73 
74 // regexes array declaration
76 {
78  : desc(_("Unknown")), lt(cltError), filename(0), line(0), regex(_T("")), regexCompiled(false)
79  {
80  memset(msg, 0, sizeof(msg));
81  }
83  : desc(rhs.desc), lt(rhs.lt), filename(rhs.filename), line(rhs.line), regex(rhs.regex), regexCompiled(false)
84  {
85  memcpy(msg, rhs.msg, sizeof(msg));
86  }
87  RegExStruct(const wxString& _desc,
88  CompilerLineType _lt,
89  const wxString& _regex,
90  int _msg,
91  int _filename = 0,
92  int _line = 0,
93  int _msg2 = 0,
94  int _msg3 = 0)
95  : desc(_desc), lt(_lt), filename(_filename), line(_line), regex(_regex), regexCompiled(false)
96  {
97  msg[0] = _msg;
98  msg[1] = _msg2;
99  msg[2] = _msg3;
100  }
102  {
103  desc=obj.desc;
104  lt=obj.lt;
105  regex=obj.regex;
106  regexCompiled=false;
107  filename=obj.filename;
108  line=obj.line;
109  memcpy(msg, obj.msg, sizeof(msg));
110 
111  return *this;
112  }
113 
114  bool operator!=(const RegExStruct& other)
115  {
116  return !(*this == other);
117  }
118  bool operator==(const RegExStruct& other)
119  {
120  return ( desc == other.desc
121  && lt == other.lt
122  && regex == other.regex
123  && msg[0] == other.msg[0]
124  && msg[1] == other.msg[1]
125  && msg[2] == other.msg[2]
126  && filename == other.filename
127  && line == other.line );
128  }
129 
130  wxString GetRegExString() const { return regex; }
131  void SetRegExString(const wxString &str)
132  {
133  if (regex != str)
134  {
135  regex = str;
136  regexCompiled = false;
137  }
138  }
139  bool HasRegEx() const { return !regex.empty(); }
140  const wxRegEx& GetRegEx() const { CompileRegEx(); return regexObject; }
141 
142  wxString desc; // title of this regex
143  CompilerLineType lt; // classify the line, if regex matches
144  int msg[3]; // up-to 3 sub-expression nr for warning/error message
145  int filename; // sub-expression nr for filename
146  int line; // sub-expression nr for line number
147  // if more than one sub-expressions are entered for msg,
148  // they are appended to each other, with one space in between.
149  // Appending takes place in the same order...
150 private:
151  void CompileRegEx() const
152  {
153  if (!regex.empty() && !regexCompiled)
154  {
156  regexCompiled=true;
157  }
158  }
159  wxString regex; // the regex to match
161  mutable bool regexCompiled;
162 };
163 typedef std::vector<RegExStruct> RegExArray;
164 
167 {
176 
178 };
179 
180 
181 
184 {
188 };
189 
191 {
194 };
195 
198 {
199  wxString C; // C compiler
200  wxString CPP; // C++ compiler
201  wxString LD; // dynamic libs linker
202  wxString LIB; // static libs linker
203  wxString WINDRES; // resource compiler
204  wxString MAKE; // make
205  wxString DBGconfig; // debugger config name = "debugger_settings_name:config_name"
206 };
207 
210 {
211  static const CompilerLoggingType defaultLogging = clogFull;
218  bool forceFwdSlashes; // force use forward slashes in file/path names (used by CompilerCommandGenerator)
219  bool forceLinkerUseQuotes; // use quotes for filenames in linker command line (needed or not)?
220  bool forceCompilerUseQuotes; // use quotes for filenames in compiler command line (needed or not)?
221  bool needDependencies; // true
225  bool linkerNeedsLibPrefix; // when adding a link library, linker needs prefix?
226  bool linkerNeedsLibExtension; // when adding a link library, linker needs extension?
227  bool linkerNeedsPathResolved; // linker does not support libDirs; C::B must resolve file paths
228  bool supportsPCH; // supports pre-compiled headers?
229  wxString PCHExtension; // pre-compiled headers extension
230  bool UseFlatObjects; // Use Flat object file names (no extra subdirs)?
231  bool UseFullSourcePaths; // This is mainly a workaround for the GDB debugger, apparently I doesn't deal
232  // well with relative paths, therefore for GCC it is better to specify the source
233  // full to the compiler in a full path notation, for all other compilers it is
234  // suggested to keep this switch at false
235  bool Use83Paths; // This is mainly a workaround for the resource compiler under Windows, apparently
236  // it doesn't deal well with spaces in the (include) path even if the path is quoted,
237  // therefore use 8.3 notation without spaces on Windows.
238  // However, this will apply to all include path's as other tools might have the
239  // same issue and it won't hurt to apply it to all include directories, if enabled.
243  int statusSuccess; // 0 - treat exit-codes >= 0 and <= statusSuccess as success (do not set negative!)
244 
245  CompilerSwitches(); // constructor initializing the members, specific compilers should overrule if needed
246 };
247 
250 {
251  // extensions string will be converted to array by GetArrayFromString using DEFAULT_ARRAY_SEP (;)
252  // as separator
253  CompilerTool(const wxString& command_in = wxEmptyString, const wxString& extensions_in = wxEmptyString, const wxString& generatedFiles_in = wxEmptyString)
254  : command(command_in), extensions(GetArrayFromString(extensions_in)), generatedFiles(GetArrayFromString(generatedFiles_in))
255  {}
257  : command(rhs.command), extensions(rhs.extensions), generatedFiles(rhs.generatedFiles)
258  {}
259  bool operator==(const CompilerTool& rhs) const { return command == rhs.command && extensions == rhs.extensions && generatedFiles == rhs.generatedFiles; }
260  bool operator!=(const CompilerTool& rhs) const { return !(*this == rhs); }
261 
265 };
266 
267 typedef std::vector<CompilerTool> CompilerToolsVector;
268 
275 {
276  public:
278  Compiler(const wxString& name, const wxString& ID, const wxString& parentID = wxEmptyString, int weight = 50);
279  ~Compiler() override;
280 
282  virtual bool IsValid();
283 
285  virtual CompilerLineType CheckForWarningsAndErrors(const wxString& line);
287  virtual wxString GetLastErrorFilename() { return m_ErrorFilename; }
289  virtual wxString GetLastErrorLine() { return m_ErrorLine; }
291  virtual wxString GetLastError() { return m_Error; }
293  virtual const wxString& GetName() const { return m_Name; }
295  virtual const wxString& GetMasterPath() const { return m_MasterPath; }
297  virtual const wxArrayString& GetExtraPaths() const { return m_ExtraPaths; }
299  virtual const CompilerPrograms& GetPrograms() const { return m_Programs; }
301  virtual const CompilerSwitches& GetSwitches() const { return m_Switches; }
303  virtual const CompilerOptions& GetOptions() const { return m_Options; }
308  virtual const wxString& GetCommand(CommandType ct, const wxString& fileExtension = wxEmptyString) const;
310  virtual const CompilerTool* GetCompilerTool(CommandType ct, const wxString& fileExtension = wxEmptyString) const;
312  virtual CompilerToolsVector& GetCommandToolsVector(CommandType ct) { return m_Commands[ct]; }
314  virtual const RegExArray& GetRegExArray(){ return m_RegExes; }
316  virtual void LoadDefaultRegExArray(bool globalPrecedence = false);
317 
319  virtual void SetName(const wxString& name){ m_Name = name; }
321  virtual void SetMasterPath(const wxString& path){ m_MasterPath = path; m_NeedValidityCheck = true; }
323  virtual void SetExtraPaths(const wxArrayString& paths){ m_ExtraPaths = paths; m_NeedValidityCheck = true; }
325  virtual void SetPrograms(const CompilerPrograms& programs){ m_Programs = programs; m_NeedValidityCheck = true; }
327  virtual void SetSwitches(const CompilerSwitches& switches){ m_Switches = switches; }
329  virtual void SetOptions(const CompilerOptions& options){ m_Options = options; }
331  virtual void SetRegExArray(const RegExArray& regexes) { m_RegExes = regexes; }
332 
333 
335  virtual void SaveSettings(const wxString& baseKey);
337  virtual void LoadSettings(const wxString& baseKey);
342  virtual void Reset();
346  virtual void ReloadOptions();
348  virtual AutoDetectResult AutoDetectInstallationDir() = 0;
349 
351  const wxString& GetID() const { return m_ID; }
353  const wxString& GetParentID() const { return m_ParentID; }
354 
356  static wxString CommandTypeDescriptions[ctCount];
357 
362  virtual void SetVersionString() { return; };
363 
365  const wxString GetVersionString() const { return m_VersionString; };
366 
370  virtual CompilerCommandGenerator* GetCommandGenerator(cbProject *project);
371 
372  void SetCOnlyFlags(const wxString& flags) { m_SortOptions[0] = flags; };
373  void SetCPPOnlyFlags(const wxString& flags) { m_SortOptions[1] = flags; };
374 
375  const wxString& GetCOnlyFlags() { return m_SortOptions[0]; };
376  const wxString& GetCPPOnlyFlags() { return m_SortOptions[1]; };
377 
379  bool WithMultiLineMsg() { return m_MultiLineMessages; };
380 
383  wxString MakeInvalidCompilerMessages() const;
384  protected:
385  friend class CompilerFactory;
386  Compiler(const Compiler& other); // copy ctor to copy everything but update m_ID
387 
389  virtual Compiler* CreateCopy() = 0;
390 
391  // purposely non-virtual
392  bool IsUniqueID(const wxString& ID){ return m_CompilerIDs.Index(ID) == wxNOT_FOUND; }
393  // converts, if needed, m_ID to something that is valid
394  void MakeValidID();
395 
396  // load options from the corresponding options_<name>.xml
397  void LoadDefaultOptions(const wxString& name, int recursion = 0);
398  // load array of regexes from the corresponding options_<name>.xml
399  void LoadRegExArray(const wxString& name, bool globalPrecedence = false, int recursion = 0);
400 
401  bool EvalXMLCondition(const wxXmlNode* node);
402  wxString GetExecName(const wxString& name);
403 
404  // keeps a copy of current settings (works only the first time it's called)
405  void MirrorCurrentSettings();
406 
407  // set the following members in your class
420  wxString m_SortOptions[2]; // m_SortOptions[0] == C-only flags; m_SortOptions[1] == C++-only flags
421 
422  int m_Weight; // lower means listed sooner (try to keep between 0 and 100)
423  bool m_MultiLineMessages; // true if the compiler writes multi-line error/warning messages
424  private:
426  wxString m_ParentID; // -1 for builtin compilers, the builtin compiler's ID to derive from for user compilers...
427  static wxArrayString m_CompilerIDs; // map to guarantee unique IDs
428  bool m_Valid; // 'valid' flag
429  bool m_NeedValidityCheck; // flag to re-check validity (raised when changing compiler paths)
430 
431  // "mirror" default settings for comparing when saving (to save only those that differ from defaults)
433  {
438 
439  // these are the CompileOptionsBase settings that each compiler keeps on a global level
449 
450  // below are the settings that the user is asked to revert to defaults (if defaults have changed)
455 
456  wxString SortOptions[2];
457  };
459  bool m_Mirrored; // flag to only mirror the settings once
460 };
461 
462 #endif // COMPILER_H
wxArrayString CmdsBefore
Definition: compiler.h:447
RegExArray m_RegExes
Definition: compiler.h:415
bool linkerNeedsLibExtension
Definition: compiler.h:226
virtual const RegExArray & GetRegExArray()
Get the array of regexes used in errors/warnings recognition.
Definition: compiler.h:314
DLLIMPORT wxArrayString GetArrayFromString(const wxString &text, const wxString &separator=DEFAULT_ARRAY_SEP, bool trimSpaces=true)
Definition: globals.cpp:134
bool operator!=(const CompilerTool &rhs) const
Definition: compiler.h:260
wxArrayString ExtraPaths
Definition: compiler.h:436
CompilerLoggingType logging
Definition: compiler.h:222
CompilerOptions Options
Definition: compiler.h:453
wxArrayString generatedFiles
the native language files this command generates that should be further compiled
Definition: compiler.h:264
wxString PCHExtension
Definition: compiler.h:229
CompilerPrograms Programs
Definition: compiler.h:437
virtual wxString GetLastError()
Returns warning/error actual string.
Definition: compiler.h:291
virtual const wxString & GetMasterPath() const
Get the compiler&#39;s master path (must contain "bin", "include" and "lib")
Definition: compiler.h:295
virtual void SetName(const wxString &name)
Set the compiler&#39;s name.
Definition: compiler.h:319
virtual void SetPrograms(const CompilerPrograms &programs)
Set the compiler&#39;s programs.
Definition: compiler.h:325
CompilerSwitches Switches
Definition: compiler.h:452
static wxArrayString m_CompilerIDs
Definition: compiler.h:427
CompilerLoggingType
Helper enum for type of compiler logging.
Definition: compiler.h:183
Generate command-lines needed to produce a build.
wxString genericSwitch
Definition: compiler.h:216
virtual const CompilerOptions & GetOptions() const
Get the compiler&#39;s options.
Definition: compiler.h:303
wxString libDirs
Definition: compiler.h:213
bool operator==(const RegExStruct &other)
Definition: compiler.h:118
bool m_Mirrored
Definition: compiler.h:459
wxString WINDRES
Definition: compiler.h:203
wxString m_Error
Definition: compiler.h:418
bool regexCompiled
Definition: compiler.h:161
Link console executable command, e.g. "$linker $libdirs -o $exe_output $link_objects $libs"...
Definition: compiler.h:172
bool m_MultiLineMessages
Definition: compiler.h:423
wxArrayString extensions
file extensions for which the command will be invoked (no leading dot)
Definition: compiler.h:263
RegExStruct()
Definition: compiler.h:77
Generate dependencies command.
Definition: compiler.h:169
wxString m_ErrorFilename
Definition: compiler.h:416
wxString CPP
Definition: compiler.h:200
wxArrayString CmdsAfter
Definition: compiler.h:448
#define _T(string)
int filename
Definition: compiler.h:145
const wxString & GetCPPOnlyFlags()
Definition: compiler.h:376
Compile object command, e.g. "$compiler $options $includes -c $file -o $object".
Definition: compiler.h:168
void SetRegExString(const wxString &str)
Definition: compiler.h:131
CompilerPrograms m_Programs
Definition: compiler.h:412
void SetCPPOnlyFlags(const wxString &flags)
Definition: compiler.h:373
bool UseFullSourcePaths
Definition: compiler.h:231
MirrorSettings m_Mirror
Definition: compiler.h:458
CompilerLineType lt
Definition: compiler.h:143
virtual wxString GetLastErrorLine()
Returns warning/error line number (as a string).
Definition: compiler.h:289
#define wxNOT_FOUND
Struct to keep programs.
Definition: compiler.h:197
Represents a file in a Code::Blocks project.
Definition: projectfile.h:39
bool empty() const
wxString m_Name
Definition: compiler.h:408
AutoDetectResult
Definition: compiler.h:190
wxString defines
Definition: compiler.h:215
bool operator==(const CompilerTool &rhs) const
Definition: compiler.h:259
wxArrayString LinkLibs
Definition: compiler.h:446
wxString LD
Definition: compiler.h:201
CompilerLineType
Enum categorizing compiler&#39;s output line as warning/error/info/normal.
Definition: compiler.h:66
bool forceFwdSlashes
Definition: compiler.h:218
#define DLLIMPORT
Definition: settings.h:16
wxUSE_UNICODE_dependent wxChar
std::vector< RegExStruct > RegExArray
Definition: compiler.h:163
const wxString & GetID() const
Get this compiler&#39;s unique ID.
Definition: compiler.h:351
virtual void SetRegExArray(const RegExArray &regexes)
Set the array of regexes used in errors/warnings recognition.
Definition: compiler.h:331
wxString m_ErrorLine
Definition: compiler.h:417
Represents a Code::Blocks project.
Definition: cbproject.h:96
RegExStruct(const RegExStruct &rhs)
Definition: compiler.h:82
const wxString & GetCOnlyFlags()
Definition: compiler.h:375
std::vector< CompilerTool > CompilerToolsVector
Definition: compiler.h:267
wxArrayString LibDirs
Definition: compiler.h:445
Link dynamic (dll) lib command, e.g. "$linker -shared -Wl,--output-def=$def_output -Wl...
Definition: compiler.h:173
wxString m_VersionString
Definition: compiler.h:419
Struct for compiler/linker commands.
Definition: compiler.h:249
wxArrayString ResIncludeDirs
Definition: compiler.h:444
wxString objectExtension
Definition: compiler.h:217
wxString MAKE
Definition: compiler.h:204
bool m_NeedValidityCheck
Definition: compiler.h:429
wxChar includeDirSeparator
Definition: compiler.h:240
virtual const CompilerPrograms & GetPrograms() const
Get the compiler&#39;s programs.
Definition: compiler.h:299
virtual const wxArrayString & GetExtraPaths() const
Get the compiler&#39;s extra paths.
Definition: compiler.h:297
RegExStruct & operator=(const RegExStruct &obj)
Definition: compiler.h:101
wxArrayString m_ExtraPaths
Definition: compiler.h:410
wxString libExtension
Definition: compiler.h:224
wxChar objectSeparator
Definition: compiler.h:242
wxString linkLibs
Definition: compiler.h:214
This is a base class for all classes needing compilation parameters.
wxString DBGconfig
Definition: compiler.h:205
Link executable command, e.g. "$linker $libdirs -o $exe_output $link_objects $libs -mwindows"...
Definition: compiler.h:171
wxString includeDirs
Definition: compiler.h:212
wxString wxEmptyString
virtual void SetExtraPaths(const wxArrayString &paths)
Set the compiler&#39;s extra paths.
Definition: compiler.h:323
CompilerTool(const CompilerTool &rhs)
Definition: compiler.h:256
wxString command
command to execute
Definition: compiler.h:262
const wxString GetVersionString() const
Get the compiler version string.
Definition: compiler.h:365
const wxString & _(const wxString &string)
wxArrayString LinkerOptions
Definition: compiler.h:442
wxArrayString ResourceCompilerOptions
Definition: compiler.h:441
bool HasRegEx() const
Definition: compiler.h:139
wxString m_ParentID
Definition: compiler.h:426
virtual void SetOptions(const CompilerOptions &options)
Set the compiler&#39;s options.
Definition: compiler.h:329
bool operator!=(const RegExStruct &other)
Definition: compiler.h:114
CommandType
Helper enum to retrieve compiler commands.
Definition: compiler.h:166
CompilerOptions m_Options
Definition: compiler.h:414
Definition: id.h:15
bool WithMultiLineMsg()
Do compiler writes multi-line messages?
Definition: compiler.h:379
Compile Win32 resources command, e.g. "$rescomp -i $file -J rc -o $resource_output -O coff $includes"...
Definition: compiler.h:170
Abstract base class for compilers.
Definition: compiler.h:274
wxRegEx regexObject
Definition: compiler.h:160
wxString libPrefix
Definition: compiler.h:223
bool forceLinkerUseQuotes
Definition: compiler.h:219
wxString C
Definition: compiler.h:199
virtual const wxString & GetName() const
Get the compiler&#39;s name.
Definition: compiler.h:293
const wxString & GetParentID() const
Get this compiler&#39;s parent&#39;s unique ID.
Definition: compiler.h:353
virtual const CompilerSwitches & GetSwitches() const
Get the compiler&#39;s generic switches.
Definition: compiler.h:301
wxString m_MasterPath
Definition: compiler.h:409
wxChar libDirSeparator
Definition: compiler.h:241
virtual CompilerToolsVector & GetCommandToolsVector(CommandType ct)
Get a command tool vector based on CommandType (used by advanced compiler dialog) ...
Definition: compiler.h:312
wxArrayString IncludeDirs
Definition: compiler.h:443
virtual wxString GetLastErrorFilename()
Returns warning/error filename.
Definition: compiler.h:287
wxString regex
Definition: compiler.h:159
wxString GetRegExString() const
Definition: compiler.h:130
bool linkerNeedsLibPrefix
Definition: compiler.h:225
Represents a Code::Blocks project build target.
bool Compile(const wxString &pattern, int flags=wxRE_DEFAULT)
wxString LIB
Definition: compiler.h:202
Link native binary command.
Definition: compiler.h:175
virtual void SetSwitches(const CompilerSwitches &switches)
Set the compiler&#39;s generic switches.
Definition: compiler.h:327
Link static lib command, e.g. "ar -r $output $link_objects\n\tranlib $static_output".
Definition: compiler.h:174
int m_Weight
Definition: compiler.h:422
wxArrayString CompilerOptions_
Definition: compiler.h:440
bool m_Valid
Definition: compiler.h:428
const wxRegEx & GetRegEx() const
Definition: compiler.h:140
wxString desc
Definition: compiler.h:142
RegExStruct(const wxString &_desc, CompilerLineType _lt, const wxString &_regex, int _msg, int _filename=0, int _line=0, int _msg2=0, int _msg3=0)
Definition: compiler.h:87
static const wxString FilePathWithSpaces
Definition: compiler.h:277
virtual void SetVersionString()
Set the compiler version string.
Definition: compiler.h:362
bool forceCompilerUseQuotes
Definition: compiler.h:220
Struct to keep switches.
Definition: compiler.h:209
Do NOT use.
Definition: compiler.h:177
bool IsUniqueID(const wxString &ID)
Definition: compiler.h:392
void CompileRegEx() const
Definition: compiler.h:151
wxString m_ID
Definition: compiler.h:425
bool needDependencies
Definition: compiler.h:221
CompilerTool(const wxString &command_in=wxEmptyString, const wxString &extensions_in=wxEmptyString, const wxString &generatedFiles_in=wxEmptyString)
Definition: compiler.h:253
virtual void SetMasterPath(const wxString &path)
Set the compiler&#39;s master path (must contain "bin", "include" and "lib")
Definition: compiler.h:321
void SetCOnlyFlags(const wxString &flags)
Definition: compiler.h:372
bool linkerNeedsPathResolved
Definition: compiler.h:227
CompilerSwitches m_Switches
Definition: compiler.h:413
int msg[3]
Definition: compiler.h:144
bool UseFlatObjects
Definition: compiler.h:230