summaryrefslogtreecommitdiffstats
path: root/src/tools/list/compile_manager.cpp
blob: edf999c86e9f5c21f101db483a1575b66a6325c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *   Copyright (C) 2003-2004 Alain Gibaud <alain.gibaud@free.fr>           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "compile_manager.h"

#include <tqtimer.h>

#include "libgui/project.h"
#include "common/gui/misc_gui.h"
#include "compile_config.h"

Compile::Manager::Manager(TQObject *parent)
  : TQObject(parent, "compile_manager"), _base(0)
{}

void Compile::Manager::cleanFile(const PURL::Url &url)
{
  setupFile(Clean, TodoItem(url, false));
}

bool Compile::Manager::compileFile(const TodoItem &item)
{
  _label = i18n("Compiling file...");
  return setupFile(Clean | (Main::project() ? CompileOnly : Build), item);
}

bool Compile::Manager::setupFile(Operations op, const TodoItem &item)
{
  Log::Base::clear();
  clearAll();
  _operations = op;
  _type = NormalLinking;
  _todo.append(item);
  _action = Compiling;
  _wholeProject = false;
  TQTimer::singleShot(0, this, TQ_SLOT(start()));
  return true;
}

void Compile::Manager::cleanProject(LinkType type)
{
  setupProject(Clean, type);
}

bool Compile::Manager::buildProject(LinkType type)
{
  _label = i18n("Building project...");
  return setupProject(Clean | Build, type);
}

bool Compile::Manager::setupProject(Operations op, LinkType type)
{
  Log::Base::clear();
  clearAll();
  _operations = op;
  _type = type;
  PURL::UrlList files = Main::project()->absoluteFiles();
  if ( files.count()==0 ) {
    MessageBox::sorry(i18n("Cannot build empty project."), Log::Show);
    return false;
  }
  PURL::UrlList::const_iterator it;
  for (it=files.begin(); it!=files.end(); ++it)
    if ( (*it).data().group==PURL::Source ) _todo.append(TodoItem(*it, false));
  if ( _todo.count()>1 && Main::toolGroup().compileType()==Tool::SingleFile ) {
    MessageBox::sorry(i18n("The selected toolchain only supports single-file project."), Log::Show);
    return false;
  }
  _action = Compiling;
  _wholeProject = true;
  TQTimer::singleShot(0, this, TQ_SLOT(start()));
  return true;
}

void Compile::Manager::kill()
{
  if ( clearAll() ) Log::Base::log(Log::LineType::Error, i18n("*** Aborted ***"), Log::Delayed);
  emit failure();
}

bool Compile::Manager::clearAll()
{
  bool running = ( _base!=0 );
  delete _base;
  _base = 0;
  _todo.clear();
  return running;
}

void Compile::Manager::setup(Tool::Category category)
{
  delete _base;
  Compile::Data data(category, _items, Main::device(), Main::project(), _type);
  _base = Main::toolGroup().createCompileProcess(data, this);
}

bool Compile::Manager::setupCompile()
{
  PURL::FileType type = Main::toolGroup().implementationType(PURL::ToolType::Compiler);
  for (uint i=0; i<_items.count(); i++) {
    if ( _items[i].url.fileType()!=type ) {
      if ( _operations!=Clean ) {
        TQString e = PURL::extensions(type);
        MessageBox::detailedSorry(i18n("The selected toolchain (%1) cannot compile file. It only supports files with extensions: %2")
                                  .arg(Main::toolGroup().label()).arg(e), i18n("File: %1").arg(_items[i].url.pretty()), Log::Show);
        Log::Base::log(Log::LineType::Error, i18n("*** Aborted ***"), Log::Delayed);
        processFailed();
      }
      return false;
    }
  }
  if ( !compileOnly() && Main::toolGroup().needs(Main::project(), Tool::Category::Assembler) ) {
    PURL::FileType type = Main::toolGroup().implementationType(PURL::ToolType::Assembler);
    for (uint i=0; i<_items.count(); i++)
      _todo.append(TodoItem(_items[i].url.toFileType(type), true));
  }
  setup(Tool::Category::Compiler);
  return true;
}

bool Compile::Manager::setupAssemble()
{
  PURL::FileType type = Main::toolGroup().implementationType(PURL::ToolType::Assembler);
  for (uint i=0; i<_items.count(); i++) {
    if ( _items[i].url.fileType()!=type ) {
      if ( _operations!=Clean ) {
        if ( type==PURL::Nb_FileTypes ) type = Main::toolGroup().implementationType(PURL::ToolType::Compiler);
        TQString e = PURL::extensions(type);
        MessageBox::detailedSorry(i18n("The selected toolchain (%1) cannot assemble file. It only supports files with extensions: %2")
                                  .arg(Main::toolGroup().label()).arg(e), i18n("File: %1").arg(_items[i].url.pretty()), Log::Show);
        Log::Base::log(Log::LineType::Error, i18n("*** Aborted ***"), Log::Delayed);
        processFailed();
      }
      return false;
    }
  }
  setup(Tool::Category::Assembler);
  return true;
}

bool Compile::Manager::setupLink()
{
  _action = Linking;
  Tool::Category category = (Main::project() && Main::project()->outputType()==Tool::OutputType::Library ? Tool::Category::Librarian : Tool::Category::Linker);
  if ( !Main::toolGroup().needs(Main::project(), category) ) {
    start();
    return false;
  }
  setup(category);
  return true;
}

bool Compile::Manager::setupBinToHex()
{
  _action = BinToHex;
  if ( !Main::toolGroup().needs(Main::project(), Tool::Category::BinToHex) ) {
    start();
    return false;
  }
  setup(Tool::Category::BinToHex);
  return true;
}

bool Compile::Manager::prepareAction()
{
  switch (_action) {
    case Compiling:
      if ( _todo.count()!=0 ) {
        _items.clear();
        if ( Main::toolGroup().compileType()==Tool::AllFiles ) {
          _items = _todo;
          _todo.clear();
        } else {
          _items.append(_todo[0]);
          _todo.remove(_todo.begin());
        }
        PURL::SourceFamily family = _items[0].url.data().sourceFamily;
        if ( family.data().toolType==PURL::ToolType::Compiler && Main::toolGroup().base(Tool::Category::Compiler) )
          return setupCompile();
        return setupAssemble();
      }
      if ( !compileOnly() ) return setupLink();
      break;
    case Linking: return setupBinToHex();
    case BinToHex: break;
  }
  if ( !(_operations & Clean) ) {
    Log::Base::log(Log::LineType::Information, i18n("*** Success ***"), Log::Delayed);
    emit success();
  } else if ( _operations!=Clean ) {
    _operations &= ~Clean;
    if (_wholeProject) setupProject(_operations, _type);
    else setupFile(_operations, _items[0]);
  }
  return false;
}

void Compile::Manager::start()
{
  if ( Main::toolGroup().isCustom() ) {
    executeCustomCommands();
    return;
  }
  delete _base;
  _base = 0;
  if ( !prepareAction() ) return;
  if ( !_base->check() ) {
    processFailed();
    return;
  }
  if ( _operations & Clean ) {
    _base->files(0).onlyExistingFiles().cleanGenerated();
    TQTimer::singleShot(0, this, TQ_SLOT(start()));
    return;
  }
  if ( !_base->start() ) {
    Log::Base::log(Log::LineType::Error, i18n("Failed to execute command: check toolchain configuration."), Log::Delayed);
    processFailed();
  }
}

void Compile::Manager::log(Log::LineType type, const TQString &message, const TQString &filepath, uint line)
{
  if ( type==Log::LineType::Error ) setError(message);
  static_cast<LogWidget *>(view())->appendLine(type, message, filepath, line);
}

void Compile::Manager::log(Log::DebugLevel level, const TQString &message, const TQString &filepath, uint line)
{
  static_cast<LogWidget *>(view())->appendLine(level, message, filepath, line);
}

void Compile::Manager::processDone()
{
  if ( hasError() ) {
    processFailed();
    return;
  }
  if ( Main::toolGroup().isCustom() ) {
    _customCommandIndex++;
    startCustomCommand();
  } else {
    FileData::List list = _base->files(0).onlyExistingFiles();
    FileData::List::iterator it;
    for (it=list.begin(); it!=list.end(); ++it) emit updateFile(*it);
    TQTimer::singleShot(0, this, TQ_SLOT(start()));
  }
}

void Compile::Manager::processFailed()
{
  clearAll();
  emit failure();
}

void Compile::Manager::startCustomCommand()
{
  delete _base;
  _base = 0;
  TQStringList commands = Compile::Config::customCommands(Main::project());
  if ( _customCommandIndex==commands.count() ) {
    Log::Base::log(Log::LineType::Information, i18n("*** Success ***"), Log::Delayed);
    emit success();
    return;
  }
  TQString command = commands[_customCommandIndex];
  _base = new CustomProcess(command);
  Compile::Data data(Tool::Category::Nb_Types, _todo, Main::device(), Main::project(), _type);
  _base->init(data, this);
  if ( !_base->start() ) {
    Log::Base::log(Log::LineType::Error, i18n("Failed to execute custom command #%1.").arg(_customCommandIndex+1), Log::Delayed);
    processFailed();
  }
}

void Compile::Manager::executeCustomCommands()
{
  _customCommandIndex = 0;
  if ( Compile::Config::customCommands(Main::project()).isEmpty() ) {
    MessageBox::sorry(i18n("No custom commands specified."), Log::Show);
    emit failure();
    return;
  }
  log(Log::LineType::Information, i18n("Executing custom commands..."));
  startCustomCommand();
}