summaryrefslogtreecommitdiffstats
path: root/Source/cmInstallExportGenerator.cxx
blob: e0ff82ccfc47d15665b9f07a27462532e0ea6cbd (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
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include <stdio.h>

#include "cmInstallTargetGenerator.h"
#include "cmGeneratedFileStream.h"
#include "cmTarget.h"

#include "cmInstallExportGenerator.h"
#include "cmInstallFilesGenerator.h"

cmInstallExportGenerator::cmInstallExportGenerator(
  const char* destination,
  const char* file_permissions,
  std::vector<std::string> const& configurations,
  const char* component,
  const char* filename, const char* prefix, const char* tempOutputDir)
  :cmInstallGenerator(destination, configurations, component)
  ,FilePermissions(file_permissions)
  ,Filename(filename)
  ,Prefix(prefix)
  ,TempOutputDir(tempOutputDir)
{
}

/* Helper function which adds the install locations from the generator
to the properties for this target.
*/
bool cmInstallExportGenerator::AddInstallLocations(cmTargetWithProperties* twp,
                                           cmInstallTargetGenerator* generator,
                                           const char* prefix)
{
  if (generator == 0) // nothing to do
    {
    return true;
    }

  if (prefix == 0)
    {
    prefix = "";
    }

  const std::vector<std::string>& configs = generator->GetConfigurations();
  if (configs.empty())
    {
    std::string propertyName = prefix;
    propertyName += "LOCATION";
    // check that this property doesn't exist yet and add it then
    if (twp->Properties.find(propertyName.c_str()) == twp->Properties.end())
      {
      std::string destinationFilename = generator->GetDestination();
      destinationFilename += "/";
      destinationFilename += generator->GetInstallFilename(0);
      twp->Properties[propertyName.c_str()] = destinationFilename;
      }
    else
      {
      return false;
      }
    }
  else
    {
    for(std::vector<std::string>::const_iterator configIt = configs.begin();
        configIt != configs.end();
        ++configIt)
      {
      std::string propertyName = configIt->c_str();
      propertyName +=  "_";
      propertyName += prefix;
      propertyName += "LOCATION";
      // check that this property doesn't exist yet and add it then
      if (twp->Properties.find(propertyName.c_str()) == twp->Properties.end())
        {
        std::string destinationFilename = generator->GetDestination();
        destinationFilename += "/";
        destinationFilename +=generator->GetInstallFilename(configIt->c_str());
        twp->Properties[propertyName.c_str()] = destinationFilename;
        }
      else
        {
        return false;
        }
      }
    }
  return true;
}


bool cmInstallExportGenerator::AddInstallLocations(cmTargetWithProperties* twp,
                                           cmInstallFilesGenerator* generator,
                                           const char* propertyName)
{
  if (generator == 0) // nothing to do
    {
    return true;
    }

  if ((propertyName == 0) || (*propertyName == '\0'))
    {
    return false;
    }

  // check that this property doesn't exist yet and add it then
  if (twp->Properties.find(propertyName) == twp->Properties.end())
    {
    twp->Properties[propertyName] = generator->GetDestination();
    return true;
    }

  return false;
}


bool cmInstallExportGenerator::SetExportSet(const char* name,
                                       const std::vector<cmTargetExport*>* set)
{
  if ((name == 0) || (*name == 0) || (set==0))
    {
    return false;
    }

  this->Name = name;

  /* iterate over all targets in the set.
  If a cmTargetWithProperties with the same name already exists in this 
  generator, add the new properties to it. If the property already exists, 
  fail with an error.
  If no cmTargetWithProperties exists, create a new one.
  */
  for(std::vector<cmTargetExport*>::const_iterator it=set->begin();
      it != set->end();
      ++it)
    {
    std::string targetName = (*it)->Target->GetName();

    cmTargetWithProperties* targetWithProps = 0;
    for(unsigned int i=0; i<this->Targets.size(); i++)
      {
      if (targetName == this->Targets[i]->Target->GetName())
        {
        targetWithProps = this->Targets[i];
        }
      }

    if (targetWithProps == 0)
      {
      targetWithProps = new cmTargetWithProperties((*it)->Target);
      this->Targets.push_back(targetWithProps);
      }

    if (this->AddInstallLocations(targetWithProps, (*it)->ArchiveGenerator, 
                                  "ARCHIVE_") == false)
      {
      return false;
      }
    if (this->AddInstallLocations(targetWithProps, (*it)->RuntimeGenerator, 
                                  "") == false)
      {
      return false;
      }
    if (this->AddInstallLocations(targetWithProps, (*it)->LibraryGenerator, 
                                  "LIBRARY_") == false)
      {
      return false;
      }
    if (this->AddInstallLocations(targetWithProps, (*it)->HeaderGenerator, 
                                  "PUBLIC_HEADER_LOCATION") == false)
      {
      return false;
      }
    }

  return true;
}

void cmInstallExportGenerator::GenerateScript(std::ostream& os)
{
  // for the case that somebody exports the same set with the same file name 
  // to different locations make the temp filename unique
  char buf[64];
  sprintf(buf, "%p", this);
  this->ExportFilename = this->TempOutputDir;
  this->ExportFilename += "/";
  this->ExportFilename += this->Filename;
  this->ExportFilename += ".";
  this->ExportFilename += buf;

  cmGeneratedFileStream exportFileStream(this->ExportFilename.c_str());
  if(!exportFileStream)
    {
    return;
    }

  /* for every target add the IMPORT statements and set the properties
    of the target.  */
  for(std::vector<cmTargetWithProperties*>::const_iterator 
      targetIt = this->Targets.begin();
      targetIt != this->Targets.end();
      ++targetIt)
    {
      switch ((*targetIt)->Target->GetType())
        {
        case cmTarget::EXECUTABLE:
          exportFileStream << "ADD_EXECUTABLE(" << this->Prefix.c_str()
                           << (*targetIt)->Target->GetName()
                           << " IMPORT )\n";
          break;
        case cmTarget::STATIC_LIBRARY:
          exportFileStream << "ADD_LIBRARY(" << this->Prefix.c_str() 
                           << (*targetIt)->Target->GetName() 
                           << " STATIC IMPORT )\n";
          break;
        case cmTarget::SHARED_LIBRARY:
          exportFileStream << "ADD_LIBRARY(" << this->Prefix.c_str() 
                           << (*targetIt)->Target->GetName()
                           << " SHARED IMPORT )\n";
          break;
        case cmTarget::MODULE_LIBRARY:
          exportFileStream << "ADD_LIBRARY(" << this->Prefix.c_str() 
                           << (*targetIt)->Target->GetName()
                           << " MODULE IMPORT )\n";
          break;
        default:  // should never happen
          break;
        }

      exportFileStream << "SET_TARGET_PROPERTIES ( " << this->Prefix.c_str() 
                       << (*targetIt)->Target->GetName() << " PROPERTIES \n";

      for (std::map<std::string, std::string>::const_iterator
           propIt = (*targetIt)->Properties.begin();
           propIt != (*targetIt)->Properties.end();
           ++propIt)
        {
        exportFileStream << "                     " << propIt->first 
                         << " \"" << propIt->second << "\"\n";
        }
      exportFileStream << "                      )\n\n";
    }

  // Perform the main install script generation.
  this->cmInstallGenerator::GenerateScript(os);
}

//----------------------------------------------------------------------------
void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
                                                     Indent const& indent)
{
  // install rule for the file created above
  std::vector<std::string> exportFile;
  exportFile.push_back(this->ExportFilename);
  this->AddInstallRule(os, this->Destination.c_str(), cmTarget::INSTALL_FILES,
                       exportFile, false, 0,
                       this->FilePermissions.c_str(),
                       0, this->Filename.c_str(), 0, indent);
}