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

  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 "cmExportCommand.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmGeneratedFileStream.h"
#include "cmake.h"

#include <cmsys/auto_ptr.hxx>

// cmExportCommand
bool cmExportCommand
::InitialPass(std::vector<std::string> const& args)
{
  if(args.size() < 2 )
    {
    this->SetError("called with too few arguments");
    return false;
    }

  std::string filename;
  std::string prefix;
  std::string exportName;
  std::vector<std::string> targets;
  bool append = false;
  if (!this->ParseArgs(args, filename, prefix, exportName, targets, append))
    {
    return false;
    }

  if ( !this->Makefile->CanIWriteThisFile(filename.c_str()) )
    {
    std::string e = "attempted to write a file: " + filename
      + " into a source directory.";
    this->SetError(e.c_str());
    cmSystemTools::SetFatalErrorOccured();
    return false;
    }

  if ((targets.empty()) || (filename.empty()))
    {
    return true;
    }

  // Use copy-if-different if not appending.
  cmsys::auto_ptr<std::ofstream> foutPtr;
  if(append)
    {
    cmsys::auto_ptr<std::ofstream> ap(
      new std::ofstream(filename.c_str(), std::ios::app));
    foutPtr = ap;
    }
  else
    {
    cmsys::auto_ptr<cmGeneratedFileStream> ap(
      new cmGeneratedFileStream(filename.c_str(), true));
    ap->SetCopyIfDifferent(true);
    foutPtr = ap;
    }
  std::ostream& fout = *foutPtr.get();

  if (!fout)
    {
    cmSystemTools::Error("Error Writing ", filename.c_str());
    cmSystemTools::ReportLastSystemError("");
    return true;
    }

  // the following code may move into an "export generator"
  // Compute the set of configurations.
  std::vector<std::string> configurationTypes;
  if(const char* types =
     this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
    {
    cmSystemTools::ExpandListArgument(types, configurationTypes);
    }
  if(configurationTypes.empty())
    {
    const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
    if (config!=0)
      {
      configurationTypes.push_back(config);
      }
    }

  for(std::vector<std::string>::const_iterator currentTarget = targets.begin();
      currentTarget != targets.end();
      ++currentTarget)
    {
    cmTarget* target = this->Makefile->GetLocalGenerator()->
             GetGlobalGenerator()->FindTarget(0, currentTarget->c_str(), true);
    if (target == 0)
      {
      std::string e = "detected unknown target: " + *currentTarget;
      this->SetError(e.c_str());
      cmSystemTools::SetFatalErrorOccured();
      return false;
      }
    }

  for(std::vector<std::string>::const_iterator currentTarget = targets.begin();
      currentTarget != targets.end();
      ++currentTarget)
    {
    // Look for a CMake target with the given name, which is an executable
    // and which can be run
    cmTarget* target = this->Makefile->GetLocalGenerator()->
             GetGlobalGenerator()->FindTarget(0, currentTarget->c_str(), true);
    if ((target != 0)
       && ((target->GetType() == cmTarget::EXECUTABLE)
        || (target->GetType() == cmTarget::STATIC_LIBRARY)
        || (target->GetType() == cmTarget::SHARED_LIBRARY)
        || (target->GetType() == cmTarget::MODULE_LIBRARY)))
      {
      switch (target->GetType())
        {
        case cmTarget::EXECUTABLE:
          fout << "ADD_EXECUTABLE(" << prefix.c_str() << currentTarget->c_str()
                                                              << " IMPORT )\n";
          break;
        case cmTarget::STATIC_LIBRARY:
          fout << "ADD_LIBRARY(" << prefix.c_str() << currentTarget->c_str()
                                                       << " STATIC IMPORT )\n";
          break;
        case cmTarget::SHARED_LIBRARY:
          fout << "ADD_LIBRARY(" << prefix.c_str() << currentTarget->c_str()
                                                       << " SHARED IMPORT )\n";
          break;
        case cmTarget::MODULE_LIBRARY:
          fout << "ADD_LIBRARY(" << prefix.c_str() << currentTarget->c_str()
                                                       << " MODULE IMPORT )\n";
          break;
        default:  // should never happen
          break;
        }

      fout << "SET_TARGET_PROPERTIES(" << prefix.c_str()
                                 << currentTarget->c_str() << " PROPERTIES \n"
        << "                      LOCATION " << target->GetLocation(0) << "\n";
      for(std::vector<std::string>::const_iterator
          currentConfig = configurationTypes.begin();
          currentConfig != configurationTypes.end();
          ++currentConfig)
        {
        if (!currentConfig->empty())
          {
          const char* loc = target->GetLocation(currentConfig->c_str());
          if (loc && *loc)
            {
            fout << "                      " << currentConfig->c_str()
                                                << "_LOCATION " << loc << "\n";
            }
          }
        }
      fout << "                     )\n\n";
      }
    }

  return true;
}

bool cmExportCommand::ParseArgs(const std::vector<std::string>& args,
                                std::string& filename,
                                std::string& prefix,
                                std::string& exportName,
                                std::vector<std::string>& targets,
                                bool& append) const
{
  bool doingFile = false;
  bool doingPrefix = false;
  bool doingTargets = false;
  bool doingName = true;
  for(std::vector<std::string>::const_iterator it = args.begin();
      it != args.end();
      ++it)
    {
    if (*it == "FILE")
      {
      doingFile = true;
      doingPrefix = false;
      doingName = false;
      doingTargets = false;
      }
    else if (*it == "PREFIX")
      {
      doingFile = false;
      doingPrefix = true;
      doingName = false;
      doingTargets = false;
      }
    else if (*it == "TARGETS")
      {
      doingFile = false;
      doingPrefix = false;
      doingName = false;
      doingTargets = true;
      }
    else if (*it == "APPEND")
      {
      append = true;
      doingFile = false;
      doingPrefix = false;
      doingName = false;
      doingTargets = false;
      }
    else if (doingFile)
      {
      filename = *it;
      doingFile = false;
      doingPrefix = false;
      doingName = false;
      doingTargets = false;
      }
    else if (doingPrefix)
      {
      prefix = *it;
      doingFile = false;
      doingPrefix = false;
      doingName = false;
      doingTargets = false;
      }
    else if (doingTargets)
      {
      targets.push_back(*it);
      }
    else if (doingName)
      {
      exportName = *it;
      doingFile = false;
      doingPrefix = false;
      doingName = false;
      doingTargets = false;
      }
    else
      {
      }
    }
  return true;
}