cmLocalVisualStudioGenerator.cxx 7.08 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "cmLocalVisualStudioGenerator.h"
4

5
#include "cmCustomCommandGenerator.h"
6
#include "cmGeneratorTarget.h"
7
#include "cmGlobalGenerator.h"
8 9 10
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmSystemTools.h"
11
#include "windows.h"
12

13 14
cmLocalVisualStudioGenerator::cmLocalVisualStudioGenerator(
  cmGlobalGenerator* gg, cmMakefile* mf)
15
  : cmLocalGenerator(gg, mf)
16 17 18 19 20 21 22
{
}

cmLocalVisualStudioGenerator::~cmLocalVisualStudioGenerator()
{
}

23 24 25 26 27 28 29 30
cmGlobalVisualStudioGenerator::VSVersion
cmLocalVisualStudioGenerator::GetVersion() const
{
  cmGlobalVisualStudioGenerator* gg =
    static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  return gg->GetVersion();
}

31
void cmLocalVisualStudioGenerator::ComputeObjectFilenames(
32 33
  std::map<cmSourceFile const*, std::string>& mapping,
  cmGeneratorTarget const* gt)
34
{
35
  std::string dir_max = this->ComputeLongestObjectDirectory(gt);
36 37 38 39 40

  // Count the number of object files with each name.  Note that
  // windows file names are not case sensitive.
  std::map<std::string, int> counts;

41 42 43
  for (std::map<cmSourceFile const*, std::string>::iterator si =
         mapping.begin();
       si != mapping.end(); ++si) {
44 45 46
    cmSourceFile const* sf = si->first;
    std::string objectNameLower = cmSystemTools::LowerCase(
      cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
47
    objectNameLower += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
48
    counts[objectNameLower] += 1;
49
  }
50 51 52 53

  // For all source files producing duplicate names we need unique
  // object name computation.

54 55 56
  for (std::map<cmSourceFile const*, std::string>::iterator si =
         mapping.begin();
       si != mapping.end(); ++si) {
57 58 59
    cmSourceFile const* sf = si->first;
    std::string objectName =
      cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
60
    objectName += this->GlobalGenerator->GetLanguageOutputExtension(*sf);
61
    if (counts[cmSystemTools::LowerCase(objectName)] > 1) {
62 63 64
      const_cast<cmGeneratorTarget*>(gt)->AddExplicitObjectName(sf);
      objectName = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
    }
65 66
    si->second = objectName;
  }
67 68
}

69
CM_AUTO_PTR<cmCustomCommand>
70
cmLocalVisualStudioGenerator::MaybeCreateImplibDir(cmGeneratorTarget* target,
71
                                                   const std::string& config,
72
                                                   bool isFortran)
73
{
74
  CM_AUTO_PTR<cmCustomCommand> pcc;
75 76 77

  // If an executable exports symbols then VS wants to create an
  // import library but forgets to create the output directory.
78
  // The Intel Fortran plugin always forgets to the directory.
79 80
  if (target->GetType() != cmStateEnums::EXECUTABLE &&
      !(isFortran && target->GetType() == cmStateEnums::SHARED_LIBRARY)) {
81 82
    return pcc;
  }
83 84
  std::string outDir = target->GetDirectory(config, false);
  std::string impDir = target->GetDirectory(config, true);
85 86 87
  if (impDir == outDir) {
    return pcc;
  }
88 89 90

  // Add a pre-build event to create the directory.
  cmCustomCommandLine command;
91
  command.push_back(cmSystemTools::GetCMakeCommand());
92 93 94 95
  command.push_back("-E");
  command.push_back("make_directory");
  command.push_back(impDir);
  std::vector<std::string> no_output;
96
  std::vector<std::string> no_byproducts;
97 98 99
  std::vector<std::string> no_depends;
  cmCustomCommandLines commands;
  commands.push_back(command);
100 101
  pcc.reset(new cmCustomCommand(0, no_output, no_byproducts, no_depends,
                                commands, 0, 0));
102 103 104 105 106
  pcc->SetEscapeOldStyle(false);
  pcc->SetEscapeAllowMakeVars(true);
  return pcc;
}

107
const char* cmLocalVisualStudioGenerator::ReportErrorLabel() const
108
{
109
  return ":VCReportError";
110 111
}

112
const char* cmLocalVisualStudioGenerator::GetReportErrorLabel() const
113
{
114
  return this->ReportErrorLabel();
115 116
}

117 118
std::string cmLocalVisualStudioGenerator::ConstructScript(
  cmCustomCommandGenerator const& ccg, const std::string& newline_text)
119
{
120
  bool useLocal = this->CustomCommandUseLocal();
121
  std::string workingDirectory = ccg.GetWorkingDirectory();
122

123
  // Avoid leading or trailing newlines.
124
  std::string newline = "";
125

126
  // Line to check for error between commands.
127
  std::string check_error = newline_text;
128
  if (useLocal) {
129
    check_error += "if %errorlevel% neq 0 goto :cmEnd";
130
  } else {
131 132
    check_error += "if errorlevel 1 goto ";
    check_error += this->GetReportErrorLabel();
133
  }
134

135 136
  // Store the script in a string.
  std::string script;
137 138

  // Open a local context.
139
  if (useLocal) {
140 141 142
    script += newline;
    newline = newline_text;
    script += "setlocal";
143
  }
144

145
  if (!workingDirectory.empty()) {
146
    // Change the working directory.
147
    script += newline;
148
    newline = newline_text;
149
    script += "cd ";
150 151
    script += this->ConvertToOutputFormat(
      cmSystemTools::CollapseFullPath(workingDirectory), SHELL);
152
    script += check_error;
153 154

    // Change the working drive.
155
    if (workingDirectory.size() > 1 && workingDirectory[1] == ':') {
156
      script += newline;
157
      newline = newline_text;
158 159
      script += workingDirectory[0];
      script += workingDirectory[1];
160
      script += check_error;
161
    }
162
  }
163

164 165
  // for visual studio IDE add extra stuff to the PATH
  // if CMAKE_MSVCIDE_RUN_PATH is set.
166
  if (this->Makefile->GetDefinition("MSVC_IDE")) {
167 168
    const char* extraPath =
      this->Makefile->GetDefinition("CMAKE_MSVCIDE_RUN_PATH");
169
    if (extraPath) {
170
      script += newline;
171
      newline = newline_text;
172 173 174 175
      script += "set PATH=";
      script += extraPath;
      script += ";%PATH%";
    }
176
  }
177

178
  // Write each command on a single line.
179
  for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
180 181
    // Start a new line.
    script += newline;
182
    newline = newline_text;
183

184 185
    // Add this command line.
    std::string cmd = ccg.GetCommand(c);
186 187 188 189 190

    // Use "call " before any invocations of .bat or .cmd files
    // invoked as custom commands.
    //
    std::string suffix;
191 192 193
    if (cmd.size() > 4) {
      suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
      if (suffix == ".bat" || suffix == ".cmd") {
194 195
        script += "call ";
      }
196
    }
197

198
    if (workingDirectory.empty()) {
199 200 201
      script += this->ConvertToOutputFormat(
        this->ConvertToRelativePath(this->GetCurrentBinaryDirectory(), cmd),
        cmOutputConverter::SHELL);
202
    } else {
203
      script += this->ConvertToOutputFormat(cmd.c_str(), SHELL);
204
    }
205
    ccg.AppendArguments(c, script);
206 207 208 209 210

    // After each custom command, check for an error result.
    // If there was an error, jump to the VCReportError label,
    // skipping the run of any subsequent commands in this
    // sequence.
211
    script += check_error;
212
  }
213

214
  // Close the local context.
215
  if (useLocal) {
216 217 218
    script += newline;
    script += ":cmEnd";
    script += newline;
219 220 221 222 223 224 225
    script += "endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone";
    script += newline;
    script += ":cmErrorLevel";
    script += newline;
    script += "exit /b %1";
    script += newline;
    script += ":cmDone";
226
    script += newline;
227
    script += "if %errorlevel% neq 0 goto ";
228
    script += this->GetReportErrorLabel();
229
  }
230

231 232
  return script;
}