Code::Blocks  SVN r11506
compilerOWgenerator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 8649 $
6  * $Id: compilerOWgenerator.cpp 8649 2012-12-12 19:18:18Z mortenmacfly $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/plugins/compilergcc/compilerOWgenerator.cpp $
8  */
9 
10 #include <sdk.h>
11 #ifndef CB_PRECOMP
12 # include "compiler.h"
13 # include "cbproject.h"
14 # include "projectbuildtarget.h"
15 # include "logmanager.h"
16 # include "macrosmanager.h"
17 #endif
18 #include "compileoptionsbase.h"
19 #include "compilerOWgenerator.h"
20 
21 
23 {
24  //ctor
26 }
27 
29 {
30  //dtor
31 }
32 
34 {
35  wxArrayString LibDirs = compiler->GetLibDirs();
36  if (LibDirs.IsEmpty())
37  return wxEmptyString;
38  wxString Result = compiler->GetSwitches().libDirs + _T(" ");
39  if (target)
40  {
41  wxString tmp, targetStr, projectStr;
42  // First prepare the target
43  const wxArrayString targetArr = target->GetLibDirs();
44  for (size_t i = 0; i < targetArr.GetCount(); ++i)
45  {
46  tmp = targetArr[i];
47  Manager::Get()->GetMacrosManager()->ReplaceMacros(tmp, target);
48  targetStr << tmp << _T(";");
49  }
50  // Now for project
51  const wxArrayString projectArr = target->GetParentProject()->GetLibDirs();
52  for (size_t i = 0; i < projectArr.GetCount(); ++i)
53  {
54  tmp = projectArr[i];
55  Manager::Get()->GetMacrosManager()->ReplaceMacros(tmp, target);
56  projectStr << tmp << _T(";");
57  }
58  // Decide order and arrange it
59  Result << GetOrderedOptions(target, ortLibDirs, projectStr, targetStr);
60  }
61  // Finally add the compiler options
62  const wxArrayString compilerArr = compiler->GetLibDirs();
63  wxString tmp, compilerStr;
64  for (size_t i = 0; i < compilerArr.GetCount(); ++i)
65  {
66  tmp = compilerArr[i];
67  Manager::Get()->GetMacrosManager()->ReplaceMacros(tmp, target);
68  compilerStr << tmp << _T(";");
69  }
70  // Now append it
71  Result << compilerStr;
72  // Remove last ';' char
73  Result = Result.Trim(true);
74  if (Result.Right(1).IsSameAs(_T(';')))
75  Result = Result.RemoveLast();
76  return Result;
77 }
78 
80 {
81  wxString Temp, LinkerOptions, Result;
82  wxArrayString ComLinkerOptions, OtherLinkerOptions, LinkerOptionsArr;
83  int i, j, Count;
84 
85  for (j = 0; j < 3; ++j)
86  {
87  LinkerOptions = wxEmptyString;
88  if (j == 0 && target)
89  {
90  ComLinkerOptions = target->GetCompilerOptions();
91  OtherLinkerOptions = target->GetLinkerOptions();
92  }
93  else if (j == 1 && target)
94  {
95  ComLinkerOptions = target->GetParentProject()->GetCompilerOptions();
96  OtherLinkerOptions = target->GetParentProject()->GetLinkerOptions();
97  }
98  else if (j == 2)
99  {
100  ComLinkerOptions = compiler->GetCompilerOptions();
101  OtherLinkerOptions = compiler->GetLinkerOptions();
102  }
103  if (!ComLinkerOptions.IsEmpty())
104  {
105  Count = ComLinkerOptions.GetCount();
106  for (i = 0; i < Count; ++i)
107  {
108  Temp = ComLinkerOptions[i];
109 
110  // Replace any macros
111  Manager::Get()->GetMacrosManager()->ReplaceMacros(Temp, target);
112 
113 // TODO (Biplab#5#): Move the linker options parsing code to a different function
114  //Let's not scan all the options unnecessarily
115  if (Temp.Matches(_T("-b*")))
116  {
117  Temp = MapTargetType(Temp, target->GetTargetType());
118  if (!Temp.IsEmpty() && LinkerOptions.Find(_T("system")) == wxNOT_FOUND)
119  LinkerOptions += Temp;
120  }
121  // TODO: Map and Set All Debug Flags
122  else if (Temp.Matches(_T("-d*")) && Temp.Length() <= 4)
123  {
124  LinkerOptions = LinkerOptions + MapDebugOptions(Temp);
125  }
126  // Debugger Type: -hw (Watcom), -hd (Dwarf), -hc (CodeView)
127  else if (Temp.Matches(_T("-h?")))
128  {
129  MapDebuggerOptions(Temp);
130  }
131  else if (Temp.StartsWith(_T("-l=")))
132  {
133  Temp = Temp.AfterFirst(_T('='));
134  if (LinkerOptions.Find(_T("system")) == wxNOT_FOUND && !Temp.IsEmpty())
135  LinkerOptions += _T("system ") + Temp + _T(" ");
136  }
137  else if (Temp.StartsWith(_T("-fm")))
138  {
139  LinkerOptions += _T("option map");
140  int pos = Temp.Find(_T('='));
141  if (pos != wxNOT_FOUND)
142  LinkerOptions += Temp.Mid(pos);
143  LinkerOptions.Append(_T(" "));
144  }
145  else if (Temp.StartsWith(_T("-k")))
146  {
147  LinkerOptions += _T("option stack=") + Temp.Mid(2) + _T(" ");
148  }
149  else if (Temp.StartsWith(_T("@")))
150  {
151  LinkerOptions += Temp + _T(" ");
152  }
153  }
154  }
155  /* Following code will allow user to add any valid linker option
156  * in target's linker option section.
157  */
158  if (!OtherLinkerOptions.IsEmpty())
159  {
160  Count = OtherLinkerOptions.GetCount();
161  for (i = 0; i < Count; ++i)
162  {
163  Temp = OtherLinkerOptions[i];
164  /* Let's make a small check. It should not start with - or / */
165  if ((Temp[0] != _T('-')) && (Temp[0] != _T('/')))
166  LinkerOptions = LinkerOptions + Temp + _T(" ");
167  }
168  }
169  // Finally add it to an array
170  LinkerOptionsArr.Add(LinkerOptions);
171  }
172  // Arrange them in specified order
173  if (target)
174  Result = GetOrderedOptions(target, ortLinkerOptions, LinkerOptionsArr[1], LinkerOptionsArr[0]);
175  // Now append compiler level options
176  Result << LinkerOptionsArr[2];
177 
178  return Result;
179 }
180 
182 {
183  wxString Result;
184  wxString targetStr, projectStr, compilerStr;
186 
187  if (target)
188  {
189  // Start with target first
190  Libs = target->GetLinkLibs();
191  for (size_t i = 0; i < Libs.GetCount(); ++i)
192  targetStr << Libs[i] + _T(",");
193  // Next process project
194  Libs = target->GetParentProject()->GetLinkLibs();
195  for (size_t i = 0; i < Libs.GetCount(); ++i)
196  projectStr << Libs[i] + _T(",");
197  // Set them in proper order
198  if (!targetStr.IsEmpty() || !projectStr.IsEmpty())
199  Result << GetOrderedOptions(target, ortLinkerOptions, projectStr, targetStr);
200  }
201  // Now prepare compiler libraries, if any
202  Libs = compiler->GetLinkLibs();
203  for (size_t i = 0; i < Libs.GetCount(); ++i)
204  compilerStr << Libs[i] << _T(",");
205  // Append it to result
206  Result << compilerStr;
207  // Now trim trailing spaces, if any, and the ',' at the end
208  Result = Result.Trim(true);
209  if (Result.Right(1).IsSameAs(_T(',')))
210  Result = Result.RemoveLast();
211 
212  if (!Result.IsEmpty())
213  Result.Prepend(_T("library "));
214  return Result;
215 }
216 
218 {
219  if (Opt.IsSameAs(_T("-bt=nt")) || Opt.IsSameAs(_T("-bcl=nt")))
220  {
221  if (target_type == ttExecutable || target_type == ttStaticLib) // Win32 Executable
222  return _T("system nt_win ");
223  else if (target_type == ttConsoleOnly) // Console
224  return _T("system nt ");
225  else if (target_type == ttDynamicLib) // DLL
226  return _T("system nt_dll ");
227  else
228  return _T("system nt_win ref '_WinMain@16' "); // Default to Win32 executables
229  }
230  else if (Opt.IsSameAs(_T("-bt=linux")) || Opt.IsSameAs(_T("-bcl=linux")))
231  {
232  /* The support is experimental. Need proper manual to improve it. */
233  return _T("system linux ");
234  }
235  return wxEmptyString;
236 }
237 
238 /* The following function will be expanded later
239  to incorporate detailed debug options
240 */
242 {
243  if (Opt.IsSameAs(_T("-d0"))) // No Debug
244  {
245  return wxEmptyString;
246  }
247  if (Opt.IsSameAs(_T("-d1")))
248  {
249  return wxString(_T("debug ") + m_DebuggerType + _T("lines "));
250  }
251  if (Opt.IsSameAs(_T("-d2")) || Opt.IsSameAs(_T("-d3")))
252  {
253  return wxString(_T("debug ") + m_DebuggerType + _T("all "));
254  }
255  // Nothing Matched
256  return wxEmptyString;
257 }
258 
260 {
261  if (Opt.IsSameAs(_T("-hw")))
262  {
263  m_DebuggerType = _T("watcom ");
264  }
265  else if (Opt.IsSameAs(_T("-hd")))
266  {
267  m_DebuggerType = _T("dwarf ");
268  }
269  else if (Opt.IsSameAs(_T("-hc")))
270  {
271  m_DebuggerType = _T("codeview ");
272  }
273  else
274  {
276  }
277 }
bool Matches(const wxString &mask) const
wxString MapDebugOptions(const wxString &Opt)
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
wxString libDirs
Definition: compiler.h:213
Linker include dir option.
std::map< wxString, RefCountedLib > Libs
Target produces an executable.
size_t Length() const
virtual const wxArrayString & GetCompilerOptions() const
#define _T(string)
virtual wxString GetOrderedOptions(const ProjectBuildTarget *target, OptionsRelationType rel, const wxString &project_options, const wxString &target_options)
Arrange order of options.
wxString AfterFirst(wxUniChar ch) const
#define wxNOT_FOUND
virtual cbProject * GetParentProject()
virtual TargetType GetTargetType() const
Read the target&#39;s type.
virtual const wxArrayString & GetLinkerOptions() const
wxString & RemoveLast(size_t n=1)
virtual const wxArrayString & GetLinkLibs() const
Target produces a dynamic library.
virtual wxString SetupLinkerOptions(Compiler *compiler, ProjectBuildTarget *target)
Setup linker flags for build target.
void MapDebuggerOptions(const wxString &Opt)
bool IsSameAs(const wxString &s, bool caseSensitive=true) const
virtual wxString SetupLibrariesDirs(Compiler *compiler, ProjectBuildTarget *target)
Setup linker include dirs for build target.
bool IsEmpty() const
wxString wxEmptyString
wxString Right(size_t count) const
MacrosManager * GetMacrosManager() const
Definition: manager.cpp:454
wxString & Trim(bool fromRight=true)
void ReplaceMacros(wxString &buffer, ProjectBuildTarget *target=nullptr, bool subrequest=false)
Target produces a static library.
Abstract base class for compilers.
Definition: compiler.h:274
wxString & Append(const char *psz)
bool IsEmpty() const
virtual const wxArrayString & GetLibDirs() const
Target produces a console executable (without GUI) (distinction between ttExecutable and ttConsoleOnl...
virtual const CompilerSwitches & GetSwitches() const
Get the compiler&#39;s generic switches.
Definition: compiler.h:301
wxString & Prepend(const wxString &str)
size_t Add(const wxString &str, size_t copies=1)
bool StartsWith(const wxString &prefix, wxString *rest=NULL) const
Represents a Code::Blocks project build target.
Linker option.
size_t GetCount() const
int Find(wxUniChar ch, bool fromEnd=false) const
virtual wxString SetupLinkLibraries(Compiler *compiler, ProjectBuildTarget *target)
Setup link libraries for build target.
wxString MapTargetType(const wxString &Opt, int target_type)
wxString Mid(size_t first, size_t nCount=wxString::npos) const