summaryrefslogtreecommitdiffstats
path: root/Source/QtDialog/QCMake.cxx
blob: 53519b425424dca41066bfcbd39ee269eaa58c1c (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

#include "QCMake.h"

#include <QCoreApplication>
#include <QDir>

#include "cmake.h"
#include "cmCacheManager.h"
#include "cmSystemTools.h"

QCMake::QCMake(QObject* p)
  : QObject(p)
{
  static int metaId = qRegisterMetaType<QCMakeCacheProperty>();
  static int metaIdList = qRegisterMetaType<QCMakeCachePropertyList>();
  
  QDir appDir(QCoreApplication::applicationDirPath());
#if defined(Q_OS_WIN)
  this->CMakeExecutable = appDir.filePath("cmake.exe");
#elif defined(Q_OS_MAC)
# error "need to implement for Mac OS X"
#else
  this->CMakeExecutable = appDir.filePath("cmake");
#endif
  // TODO: check for existence?

  cmSystemTools::DisableRunCommandOutput();
  cmSystemTools::SetRunCommandHideConsole(true);
  cmSystemTools::SetErrorCallback(QCMake::errorCallback, this);

  this->CMakeInstance = new cmake;
  this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
}

QCMake::~QCMake()
{
  delete this->CMakeInstance;
  //cmDynamicLoader::FlushCache();
}

void QCMake::loadCache(const QString& dir)
{
  this->setBinaryDirectory(dir);
}

void QCMake::setSourceDirectory(const QString& dir)
{
  this->SourceDirectory = dir;
  emit this->sourceDirChanged(dir);
}

void QCMake::setBinaryDirectory(const QString& dir)
{
  cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  this->BinaryDirectory = dir;
  this->CMakeInstance->GetCacheManager()->LoadCache(dir.toLocal8Bit().data());
  QCMakeCachePropertyList props = this->properties();
  emit this->propertiesChanged(props);
  cmCacheManager::CacheIterator itm = cachem->NewIterator();
  if ( itm.Find("CMAKE_HOME_DIRECTORY"))
    {
    setSourceDirectory(itm.GetValue());
    }
}


void QCMake::setGenerator(const QString& generator)
{
}

void QCMake::configure()
{
  this->CMakeInstance->SetHomeDirectory(this->SourceDirectory.toAscii().data());
  this->CMakeInstance->SetStartDirectory(this->SourceDirectory.toAscii().data());
  this->CMakeInstance->SetHomeOutputDirectory(this->BinaryDirectory.toAscii().data());
  this->CMakeInstance->SetStartOutputDirectory(this->BinaryDirectory.toAscii().data());
  this->CMakeInstance->SetGlobalGenerator(
    this->CMakeInstance->CreateGlobalGenerator("Unix Makefiles"));  // TODO
  this->CMakeInstance->SetCMakeCommand(this->CMakeExecutable.toAscii().data());
  this->CMakeInstance->LoadCache();

  cmSystemTools::ResetErrorOccuredFlag();

  int error = this->CMakeInstance->Configure();

  emit this->propertiesChanged(this->properties());
  emit this->configureDone(error);
}

void QCMake::generate()
{
  cmSystemTools::ResetErrorOccuredFlag();
  int error = this->CMakeInstance->Generate();
  emit this->generateDone(error);
}
  
void QCMake::setProperties(const QCMakeCachePropertyList& props)
{
  cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  cmCacheManager::CacheIterator it = cachem->NewIterator();
  foreach(QCMakeCacheProperty prop, props)
    {
    if ( it.Find(prop.Key.toAscii().data()) )
      {
      it.SetValue(prop.Value.toAscii().data());
      }
    }
}

QCMakeCachePropertyList QCMake::properties()
{
  QCMakeCachePropertyList ret;

  cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  for(cmCacheManager::CacheIterator i = cachem->NewIterator();
      !i.IsAtEnd(); i.Next())
    {

    if(i.GetType() == cmCacheManager::INTERNAL ||
       i.GetType() == cmCacheManager::STATIC)
      {
      continue;
      }

    QCMakeCacheProperty prop;
    prop.Key = i.GetName();
    prop.Help = i.GetProperty("HELPSTRING");
    prop.Value = i.GetValue();
    prop.Advanced = i.GetPropertyAsBool("ADVANCED");

    if(i.GetType() == cmCacheManager::BOOL)
      {
      prop.Type = QCMakeCacheProperty::BOOL;
      if(cmSystemTools::IsOn(prop.Value.toAscii().data()))
        {
        prop.Value = QString("ON");
        }
      else
        {
        prop.Value = QString("OFF");
        }
      }
    else if(i.GetType() == cmCacheManager::PATH)
      {
      prop.Type = QCMakeCacheProperty::PATH;
      }
    else if(i.GetType() == cmCacheManager::FILEPATH)
      {
      prop.Type = QCMakeCacheProperty::FILEPATH;
      }
    else if(i.GetType() == cmCacheManager::STRING)
      {
      prop.Type = QCMakeCacheProperty::STRING;
      }

    ret.append(prop);
    }

  return ret;
}
  
void QCMake::interrupt()
{
  cmSystemTools::SetFatalErrorOccured();
}

void QCMake::progressCallback(const char* msg, float percent, void* cd)
{
  QCMake* self = reinterpret_cast<QCMake*>(cd);
  emit self->progressChanged(msg, percent);
  QCoreApplication::processEvents();
}

void QCMake::errorCallback(const char* msg, const char* title, bool& stop, void* cd)
{
  QCMake* self = reinterpret_cast<QCMake*>(cd);
  emit self->error(title, msg, &stop);
}