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

  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"

cmInstallExportGenerator::cmInstallExportGenerator(const char* destination,
    const char* file_permissions,
    std::vector<std::string> const& configurations,
    const char* filename, const char* prefix, const char* tempOutputDir)
  :cmInstallGenerator(destination)
  ,FilePermissions(file_permissions)
  ,Configurations(configurations)
  ,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::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;
      }
    }

  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];
  snprintf(buf, 64, "%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";
    }

  // install rule for the file created above
  this->AddInstallRule(os, this->Destination.c_str(), cmTarget::INSTALL_FILES, 
                       this->ExportFilename.c_str(), false, 0, 
                       this->FilePermissions.c_str(), 0, this->Configurations, 
                       0, this->Filename.c_str(), 0);

}