Code::Blocks  SVN r11506
compiletargetbase.cpp
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  * $Revision: 10833 $
6  * $Id: compiletargetbase.cpp 10833 2016-04-17 21:31:25Z jenslody $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/compiletargetbase.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include <wx/filename.h>
14 
15  #include "compiletargetbase.h"
16  #include "compilerfactory.h"
17  #include "globals.h"
18  #include "logmanager.h" // Manager::Get()->GetLogManager()->DebugLog(F())
19 #endif
20 
21 #include "filefilters.h"
22 
24  : m_TargetType(ttExecutable),
25  m_RunHostApplicationInTerminal(false),
26  m_PrefixGenerationPolicy(tgfpPlatformDefault),
27  m_ExtensionGenerationPolicy(tgfpPlatformDefault)
28 {
29  //ctor
30  for (int i = 0; i < static_cast<int>(ortLast); ++i)
31  {
33  }
34 
35  // default "make" commands
36  m_MakeCommands[mcBuild] = _T("$make -f $makefile $target");
37  m_MakeCommands[mcCompileFile] = _T("$make -f $makefile $file");
38  m_MakeCommands[mcClean] = _T("$make -f $makefile clean$target");
39  m_MakeCommands[mcDistClean] = _T("$make -f $makefile distclean$target");
40  m_MakeCommands[mcAskRebuildNeeded] = _T("$make -q -f $makefile $target");
41 // m_MakeCommands[mcSilentBuild] = _T("$make -s -f $makefile $target");
42  m_MakeCommands[mcSilentBuild] = m_MakeCommands[mcBuild] + _T(" > $(CMD_NULL)");
43  m_MakeCommandsModified = false;
44 }
45 
47 {
48  //dtor
49 }
50 
53 {
54  m_PrefixGenerationPolicy = prefix;
55  m_ExtensionGenerationPolicy = extension;
56  SetModified(true);
57 }
58 
60  TargetFilenameGenerationPolicy& extensionOut) const
61 {
62  prefixOut = m_PrefixGenerationPolicy;
63  extensionOut = m_ExtensionGenerationPolicy;
64 }
65 
67 {
68  return m_Filename;
69 }
70 
72 {
73  return m_Title;
74 }
75 
77 {
78  if (m_Title == title)
79  return;
80  m_Title = title;
81  SetModified(true);
82 }
83 
85 {
86  if (filename.IsEmpty())
87  {
89  SetModified(true);
90  return;
91  }
92  else if (m_OutputFilename == filename)
93  return;
94  m_OutputFilename = UnixFilename(filename);
96  SetModified(true);
97 }
98 
100 {
101  if (filename.IsEmpty())
102  {
103  m_ImportLibraryFilename = _T("$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME)");
104  SetModified(true);
105  return;
106  }
107  else if (m_ImportLibraryFilename == filename)
108  return;
110 }
111 
113 {
114  if (filename.IsEmpty())
115  {
116  m_DefinitionFileFilename = _T("$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME)");
117  SetModified(true);
118  return;
119  }
120  else if (m_DefinitionFileFilename == filename)
121  return;
123 }
124 
126 {
127  if (m_WorkingDir == dirname)
128  return;
129  m_WorkingDir = UnixFilename(dirname);
130  SetModified(true);
131 }
132 
134 {
135  if (m_ObjectOutput == dirname)
136  return;
137  m_ObjectOutput = UnixFilename(dirname);
138  SetModified(true);
139 }
140 
142 {
143  if (m_DepsOutput == dirname)
144  return;
145  m_DepsOutput = UnixFilename(dirname);
146  SetModified(true);
147 }
148 
150 {
151  return m_OptionsRelation[type];
152 }
153 
155 {
156  if (m_OptionsRelation[type] == rel)
157  return;
158  m_OptionsRelation[type] = rel;
159  SetModified(true);
160 }
161 
163 {
165  return wxEmptyString;
168  return m_OutputFilename;
169 }
170 
172 {
173  wxString suggestion;
174  switch (m_TargetType)
175  {
176  case ttConsoleOnly: // fall through
177  case ttExecutable: suggestion = GetExecutableFilename(); break;
178  case ttDynamicLib: suggestion = GetDynamicLibFilename(); break;
179  case ttStaticLib: suggestion = GetStaticLibFilename(); break;
180  case ttNative: suggestion = GetNativeFilename(); break;
181  case ttCommandsOnly: // fall through
182  default:
183  suggestion.Clear();
184  break;
185  }
186  wxFileName fname(suggestion);
187  return UnixFilename(fname.GetFullName());
188 }
189 
191 {
193  return wxEmptyString;
194  wxString out;
195  if (m_WorkingDir.IsEmpty())
196  {
197  out = GetOutputFilename();
198  return wxFileName(out).GetPath(wxPATH_GET_VOLUME);
199  }
200  return m_WorkingDir;
201 }
202 
204 {
206  return wxEmptyString;
207  wxString out;
208  if (m_ObjectOutput.IsEmpty())
209  {
210  out = GetBasePath();
211  if (out.IsEmpty() || out.Matches(_T(".")))
212  return _T(".objs");
213  else
214  return out + wxFileName::GetPathSeparator() + _T(".objs");
215  }
216  return m_ObjectOutput;
217 }
218 
220 {
222  return wxEmptyString;
223  wxString out;
224  if (m_DepsOutput.IsEmpty())
225  {
226  out = GetBasePath();
227  if (out.IsEmpty() || out.Matches(_T(".")))
228  return _T(".deps");
229  else
230  return out + wxFileName::GetPathSeparator() + _T(".deps");
231  }
232  return m_DepsOutput;
233 }
234 
236 {
237  // nothing to do if no auto-generation
240  return;
241 
242  wxFileName fname(filename);
243  filename.Clear();
244  // path with volume and separator
245  filename << fname.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
246  // prefix + name + extension
247  switch (m_TargetType)
248  {
249  case ttConsoleOnly:
250  case ttExecutable:
251  {
253  {
254  filename << fname.GetName();
256  }
257  else
258  filename << fname.GetFullName();
259  break;
260  }
261  case ttDynamicLib:
262  {
264  {
265  wxString prefix = wxEmptyString;
266  // On linux, "lib" is the common prefix for this platform
267  if (platform::Linux)
268  prefix = wxT("lib");
269  // FIXME (Morten#5#): What about Mac (Windows is OK)?!
270 
271  // avoid adding the prefix, if there is no prefix, or already its there
272  if (!prefix.IsEmpty() && !fname.GetName().StartsWith(prefix))
273  filename << prefix;
274  }
276  filename << fname.GetName() << FileFilters::DYNAMICLIB_DOT_EXT;
277  else
278  filename << fname.GetFullName();
279  break;
280  }
281  case ttNative:
282  {
284  filename << fname.GetName() << FileFilters::NATIVE_DOT_EXT;
285  else
286  filename << fname.GetFullName();
287  break;
288  }
289  case ttStaticLib:
290  {
292  {
294  wxString prefix = compiler ? compiler->GetSwitches().libPrefix : _T("");
295  // avoid adding the prefix, if already there
296  if (!prefix.IsEmpty() && !fname.GetName().StartsWith(prefix))
297  filename << prefix;
298  }
300  {
302  wxString Ext = compiler ? compiler->GetSwitches().libExtension : FileFilters::STATICLIB_EXT;
303  filename << fname.GetName() << _T(".") << Ext;
304  }
305  else
306  filename << fname.GetFullName();
307  break;
308  }
309  case ttCommandsOnly: // fall through
310  default:
311  filename.Clear();
312  break;
313  }
314 
315 #ifdef command_line_generation
316  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GenerateTargetFilename got %s and returns: '%s'"), fname.GetFullPath().wx_str(), filename.wx_str()));
317 #endif
318 }
319 
321 {
323  return wxEmptyString;
324 
326  {
327  wxString out = m_Filename;
329  return out;
330  }
331 
332  wxFileName fname(m_Filename);
333 #ifdef __WXMSW__
335 #else
336  fname.SetExt(_T(""));
337 #endif
338  return fname.GetFullPath();
339 }
340 
342 {
344  return wxEmptyString;
345 
346  if (m_Filename.IsEmpty())
348 
350  {
351  wxString out = m_Filename;
353  return out;
354  }
355 
356  wxFileName fname(m_Filename);
357  fname.SetName(fname.GetName());
359  return fname.GetFullPath();
360 }
361 
363 {
365  return wxEmptyString;
366 
367  if (m_Filename.IsEmpty())
369 
371  {
372  wxString out = m_Filename;
374 #ifdef command_line_generation
375  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GetDynamicLibFilename [0] returns: '%s'"), out.wx_str()));
376 #endif
377  return out;
378  }
379 
380  wxFileName fname(m_Filename);
381  fname.SetName(fname.GetName());
383 
384 #ifdef command_line_generation
385  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GetDynamicLibFilename [1] returns: '%s'"), fname.GetFullPath().wx_str()));
386 #endif
387  return fname.GetFullPath();
388 }
389 
391 {
393  return wxEmptyString;
394 
396  m_ImportLibraryFilename = _T("$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME)");
397 
399 
400 #ifdef command_line_generation
401  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GetDynamicLibImportFilename returns: '%s'"), fname.GetFullPath().wx_str()));
402 #endif
403  return fname.GetFullPath();
404 }
405 
407 {
409  return wxEmptyString;
410 
412  m_DefinitionFileFilename = _T("$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME)");
413 
415 
416 #ifdef command_line_generation
417  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GetDynamicLibDefFilename returns: '%s'"), fname.GetFullPath().wx_str()));
418 #endif
419  return fname.GetFullPath();
420 }
421 
423 {
425  return wxEmptyString;
426 
427  if (m_Filename.IsEmpty())
429 
430  /* NOTE: There is no need to check for Generation policy for import library
431  if target type is ttDynamicLib. */
432  if ( (m_TargetType == ttStaticLib)
435  {
436  wxString out = m_Filename;
438 #ifdef command_line_generation
439  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GetStaticLibFilename [0] returns: '%s'"), out.wx_str()));
440 #endif
441  return out;
442  }
443 
444  wxFileName fname(m_Filename);
445 
446  wxString prefix = _T("lib");
449  if (compiler)
450  {
451  prefix = compiler->GetSwitches().libPrefix;
452  suffix = compiler->GetSwitches().libExtension;
453  }
454  if (!fname.GetName().StartsWith(prefix))
455  fname.SetName(prefix + fname.GetName());
456  fname.SetExt(suffix);
457 
458 #ifdef command_line_generation
459  Manager::Get()->GetLogManager()->DebugLog(F(_T("CompileTargetBase::GetStaticLibFilename [1] returns: '%s'"), fname.GetFullPath().wx_str()));
460 #endif
461  return fname.GetFullPath();
462 }
463 
465 {
466  if (m_Filename.IsEmpty())
467  return _T(".");
468 
469  wxFileName basePath(m_Filename);
470  wxString base = basePath.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
471  return !base.IsEmpty() ? base : _T(".");
472 }
473 
475 {
476  if (m_TargetType == pt)
477  return;
478 
479  m_TargetType = pt;
481  SetModified(true);
482 }
483 
485 {
486  return m_TargetType;
487 }
488 
490 {
491  return m_ExecutionParameters;
492 }
493 
495 {
496  if (m_ExecutionParameters == params)
497  return;
498 
499  m_ExecutionParameters = params;
500  SetModified(true);
501 }
502 
504 {
505  return m_HostApplication;
506 }
507 
509 {
510  if (m_HostApplication == app)
511  return;
512 
513  m_HostApplication = app;
514  SetModified(true);
515 }
516 
518 {
520 }
521 
523 {
524  if (m_RunHostApplicationInTerminal == in_terminal)
525  return;
526  m_RunHostApplicationInTerminal = in_terminal;
527  SetModified(true);
528 }
529 
531 {
532  if (id == m_CompilerId)
533  return;
534 
535  m_CompilerId = id;
536  SetModified(true);
537 }
538 
540 {
541  if (m_MakeCommands[cmd] == make)
542  return;
543 
544  m_MakeCommands[cmd] = make;
545  m_MakeCommandsModified = true;
546  SetModified(true);
547 }
wxString F(const wxChar *msg,...)
sprintf-like function
Definition: logmanager.h:20
virtual void SetOptionRelation(OptionsRelationType type, OptionsRelation rel)
Set the target&#39;s options relation for type to rel.
virtual void GetTargetFilenameGenerationPolicy(TargetFilenameGenerationPolicy &prefixOut, TargetFilenameGenerationPolicy &extensionOut) const
virtual void SetMakeCommandFor(MakeCommand cmd, const wxString &make)
Set the "make" command used for cmd.
bool Matches(const wxString &mask) const
TargetFilenameGenerationPolicy
A target&#39;s filename can either be auto-generated based on the running platform, or completely specifi...
virtual void SetTitle(const wxString &title)
Set the target&#39;s title.
virtual void SetObjectOutput(const wxString &dirname)
Set the target&#39;s objects output dir.
wxString m_MakeCommands[mcLast]
virtual wxString GetDepsOutput() const
Read the target&#39;s dependencies output dir.
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
virtual const wxString & GetExecutionParameters() const
Read the target&#39;s execution parameters.
MakeCommand
virtual const wxString & GetHostApplication() const
Read the target&#39;s host application.
void GenerateTargetFilename(wxString &filename) const
The option uses target options appended to parent options.
const DLLIMPORT wxString NATIVE_DOT_EXT
TargetFilenameGenerationPolicy m_PrefixGenerationPolicy
Target produces an executable.
static Compiler * GetCompiler(size_t index)
Generate filename based on running platform defaults.
#define _T(string)
wxString m_ImportLibraryFilename
wxString m_DefinitionFileFilename
virtual void SetTargetFilenameGenerationPolicy(TargetFilenameGenerationPolicy prefix, TargetFilenameGenerationPolicy extension)
A target&#39;s filename can either be auto-generated based on the running platform, or completely specifi...
const DLLIMPORT wxString DYNAMICLIB_DOT_EXT
virtual void SetExecutionParameters(const wxString &params)
Set the target&#39;s execution parameters to params.
wxString GetName() const
wxString m_ExecutionParameters
#define wxT(string)
TargetFilenameGenerationPolicy m_ExtensionGenerationPolicy
virtual void SetImportLibraryFilename(const wxString &filename)
Set the target&#39;s import library filename.
virtual wxString GetOutputFilename()
Read the target&#39;s output filename.
virtual void SetCompilerID(const wxString &id)
! Set the flag if the host app should be run in terminal
virtual wxString SuggestOutputFilename()
Suggest a filename based on the target&#39;s type.
virtual wxString GetStaticLibFilename()
Read the target&#39;s static library filename (produced if target type is ttStaticLib) ...
const DLLIMPORT wxString DYNAMICLIB_EXT
virtual bool GetRunHostApplicationInTerminal() const
Get the flag if the host app should be run in terminal.
virtual TargetType GetTargetType() const
Read the target&#39;s type.
virtual void SetDefinitionFileFilename(const wxString &filename)
Set the target&#39;s definition file filename.
virtual OptionsRelation GetOptionRelation(OptionsRelationType type) const
Read the target&#39;s options relation for type.
virtual void SetHostApplication(const wxString &app)
Set the target&#39;s host application to app.
DLLIMPORT wxString UnixFilename(const wxString &filename, wxPathFormat format=wxPATH_NATIVE)
Definition: globals.cpp:228
virtual const wxString & GetFilename() const
virtual wxString GetDynamicLibImportFilename()
Read the target&#39;s dynamic library import filename (produced if target type is ttDynamicLib) ...
virtual void SetOutputFilename(const wxString &filename)
Set the target&#39;s output filename.
virtual const wxString & GetTitle() const
Read the target&#39;s title.
Target produces a dynamic library.
OptionsRelation
Option&#39;s relation.
const DLLIMPORT wxString STATICLIB_EXT
virtual void SetModified(bool modified)
Target produces a native binary.
LogManager * GetLogManager() const
Definition: manager.cpp:439
virtual wxString GetBasePath() const
Read the target&#39;s base path, e.g. if GetFilename() returns "/usr/local/bin/xxx", base path will retur...
wxString libExtension
Definition: compiler.h:224
virtual wxString GetWorkingDir()
Read the target&#39;s working dir for execution (valid only for executable targets)
static wxUniChar GetPathSeparator(wxPathFormat format=wxPATH_NATIVE)
virtual void SetWorkingDir(const wxString &dirname)
Set the target&#39;s working dir on execution (valid only for executable targets)
const wxStringCharType * wx_str() const
~CompileTargetBase() override
OptionsRelation m_OptionsRelation[ortLast]
wxString wxEmptyString
virtual wxString GetDynamicLibFilename()
Read the target&#39;s dynamic library filename (produced if target type is ttDynamicLib) ...
const DLLIMPORT wxString NATIVE_EXT
No automatic generation; let the user specify the full filename.
virtual wxString GetExecutableFilename() const
Read the target&#39;s executable filename (produced if target type is ttExecutable)
Target produces a static library.
virtual void SetDepsOutput(const wxString &dirname)
Set the target&#39;s dependencies output dir.
const DLLIMPORT wxString EXECUTABLE_EXT
Abstract base class for compilers.
Definition: compiler.h:274
wxString libPrefix
Definition: compiler.h:223
bool IsEmpty() const
void Clear()
wxString GetPath(int flags=wxPATH_GET_VOLUME, wxPathFormat format=wxPATH_NATIVE) const
Target produces a console executable (without GUI) (distinction between ttExecutable and ttConsoleOnl...
wxString GetFullName() const
virtual wxString GetNativeFilename()
Read the target&#39;s native filename (produced if target type is ttNative)
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
virtual const CompilerSwitches & GetSwitches() const
Get the compiler&#39;s generic switches.
Definition: compiler.h:301
void SetExt(const wxString &ext)
bool StartsWith(const wxString &prefix, wxString *rest=NULL) const
virtual void SetTargetType(TargetType pt)
Set the target&#39;s type to pt.
void SetName(const wxString &name)
virtual wxString GetObjectOutput() const
Read the target&#39;s objects output dir.
const DLLIMPORT wxString EXECUTABLE_DOT_EXT
wxString GetFullPath(wxPathFormat format=wxPATH_NATIVE) const
TargetType
Enum to define the type of output the target produces.
virtual wxString GetDynamicLibDefFilename()
Read the target&#39;s dynamic library definition file filename (produced if target type is ttDynamicLib) ...
virtual void SetRunHostApplicationInTerminal(bool in_terminal)
OptionsRelationType
Enum that defines the option&#39;s relation types.
Target only runs commands in pre-build and/or post-build steps.