cmExportBuildFileGenerator.cxx 11.6 KB
Newer Older
1 2 3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5 6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8 9 10 11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12 13
#include "cmExportBuildFileGenerator.h"

14 15
#include "cmLocalGenerator.h"
#include "cmGlobalGenerator.h"
16 17
#include "cmExportSet.h"
#include "cmTargetExport.h"
18 19 20

//----------------------------------------------------------------------------
cmExportBuildFileGenerator::cmExportBuildFileGenerator()
21
  : Backtrace(NULL)
22
{
23
  this->Makefile = 0;
24
  this->ExportSet = 0;
25 26
}

27 28 29
//----------------------------------------------------------------------------
bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
{
30 31 32
  {
  std::string expectedTargets;
  std::string sep;
33 34
  std::vector<std::string> targets;
  this->GetTargets(targets);
35
  for(std::vector<std::string>::const_iterator
36 37
        tei = targets.begin();
      tei != targets.end(); ++tei)
38
    {
39
    cmTarget *te = this->Makefile->FindTargetToUse(*tei);
40
    expectedTargets += sep + this->Namespace + te->GetExportName();
41
    sep = " ";
42 43
    if(this->ExportedTargets.insert(te).second)
      {
44
      this->Exports.push_back(te);
45 46 47
      }
    else
      {
48 49 50
      cmOStringStream e;
      e << "given target \"" << te->GetName() << "\" more than once.";
      this->Makefile->GetCMakeInstance()
Stephen Kelly's avatar
Stephen Kelly committed
51
          ->IssueMessage(cmake::FATAL_ERROR, e.str(), this->Backtrace);
52 53
      return false;
      }
54 55
    if (te->GetType() == cmTarget::INTERFACE_LIBRARY)
      {
Brad King's avatar
Brad King committed
56
      this->GenerateRequiredCMakeVersion(os, "3.0.0");
57
      }
58 59
    }

60 61 62
  this->GenerateExpectedTargetsCode(os, expectedTargets);
  }

63 64
  std::vector<std::string> missingTargets;

65 66
  // Create all the imported targets.
  for(std::vector<cmTarget*>::const_iterator
67 68
        tei = this->Exports.begin();
      tei != this->Exports.end(); ++tei)
69 70
    {
    cmTarget* te = *tei;
71 72 73 74 75 76 77 78 79 80
    if (te->GetProperty("INTERFACE_SOURCES"))
      {
      cmOStringStream e;
      e << "Target \""
        << te->GetName()
        << "\" has a populated INTERFACE_SOURCES property.  This is not "
          "currently supported.";
      cmSystemTools::Error(e.str().c_str());
      return false;
      }
81
    this->GenerateImportTargetCode(os, te);
82

83 84
    te->AppendBuildInterfaceIncludes();

85 86 87 88 89 90 91 92
    ImportPropertyMap properties;

    this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", te,
                                    cmGeneratorExpression::BuildInterface,
                                    properties, missingTargets);
    this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", te,
                                    cmGeneratorExpression::BuildInterface,
                                    properties, missingTargets);
93 94 95
    this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", te,
                                    cmGeneratorExpression::BuildInterface,
                                    properties, missingTargets);
96 97
    this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", te,
                                    cmGeneratorExpression::BuildInterface,
98 99 100
                                    properties, missingTargets);
    this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", te,
                                    cmGeneratorExpression::BuildInterface,
101
                                    properties, missingTargets);
102 103
    this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
                                  te, properties);
104 105 106 107 108 109 110 111 112
    const bool newCMP0022Behavior =
                              te->GetPolicyStatusCMP0022() != cmPolicies::WARN
                           && te->GetPolicyStatusCMP0022() != cmPolicies::OLD;
    if (newCMP0022Behavior)
      {
      this->PopulateInterfaceLinkLibrariesProperty(te,
                                    cmGeneratorExpression::BuildInterface,
                                    properties, missingTargets);
      }
113
    this->PopulateCompatibleInterfaceProperties(te, properties);
114 115

    this->GenerateInterfaceProperties(te, os, properties);
116 117
    }

118 119 120 121 122
  // Generate import file content for each configuration.
  for(std::vector<std::string>::const_iterator
        ci = this->Configurations.begin();
      ci != this->Configurations.end(); ++ci)
    {
Stephen Kelly's avatar
Stephen Kelly committed
123
    this->GenerateImportConfig(os, *ci, missingTargets);
124 125
    }

126 127
  this->GenerateMissingTargetsCheckCode(os, missingTargets);

128 129 130 131 132 133 134
  return true;
}

//----------------------------------------------------------------------------
void
cmExportBuildFileGenerator
::GenerateImportTargetsConfig(std::ostream& os,
135 136 137
                              const std::string& config,
                              std::string const& suffix,
                              std::vector<std::string> &missingTargets)
138 139
{
  for(std::vector<cmTarget*>::const_iterator
140 141
        tei = this->Exports.begin();
      tei != this->Exports.end(); ++tei)
142 143 144 145
    {
    // Collect import properties for this target.
    cmTarget* target = *tei;
    ImportPropertyMap properties;
146 147 148 149 150

    if (target->GetType() != cmTarget::INTERFACE_LIBRARY)
      {
      this->SetImportLocationProperty(config, suffix, target, properties);
      }
151 152 153
    if(!properties.empty())
      {
      // Get the rest of the target details.
154 155 156 157 158 159 160 161
      if (target->GetType() != cmTarget::INTERFACE_LIBRARY)
        {
        this->SetImportDetailProperties(config, suffix,
                                        target, properties, missingTargets);
        this->SetImportLinkInterface(config, suffix,
                                    cmGeneratorExpression::BuildInterface,
                                    target, properties, missingTargets);
        }
162 163

      // TOOD: PUBLIC_HEADER_LOCATION
164 165
      // This should wait until the build feature propagation stuff
      // is done.  Then this can be a propagated include directory.
166 167 168 169 170 171 172 173 174
      // this->GenerateImportProperty(config, te->HeaderGenerator,
      //                              properties);

      // Generate code in the export file.
      this->GenerateImportPropertyCode(os, config, target, properties);
      }
    }
}

175 176 177 178 179 180
//----------------------------------------------------------------------------
void cmExportBuildFileGenerator::SetExportSet(cmExportSet *exportSet)
{
  this->ExportSet = exportSet;
}

181 182 183
//----------------------------------------------------------------------------
void
cmExportBuildFileGenerator
184 185
::SetImportLocationProperty(const std::string& config,
                            std::string const& suffix,
186 187 188 189 190 191 192 193 194
                            cmTarget* target, ImportPropertyMap& properties)
{
  // Get the makefile in which to lookup target information.
  cmMakefile* mf = target->GetMakefile();

  // Add the main target file.
  {
  std::string prop = "IMPORTED_LOCATION";
  prop += suffix;
195
  std::string value;
196
  if(target->IsAppBundleOnApple())
197
    {
198 199 200 201 202 203
    value = target->GetFullPath(config, false);
    }
  else
    {
    value = target->GetFullPath(config, false, true);
    }
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  properties[prop] = value;
  }

  // Check whether this is a DLL platform.
  bool dll_platform =
    (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));

  // Add the import library for windows DLLs.
  if(dll_platform &&
     (target->GetType() == cmTarget::SHARED_LIBRARY ||
      target->IsExecutableWithExports()) &&
     mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
    {
    std::string prop = "IMPORTED_IMPLIB";
    prop += suffix;
    std::string value = target->GetFullPath(config, true);
220 221
    target->GetImplibGNUtoMS(value, value,
                             "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
222 223 224 225
    properties[prop] = value;
    }
}

226 227 228
//----------------------------------------------------------------------------
void
cmExportBuildFileGenerator::HandleMissingTarget(
229 230
  std::string& link_libs, std::vector<std::string>& missingTargets,
  cmMakefile* mf, cmTarget* depender, cmTarget* dependee)
231 232 233 234
{
  // The target is not in the export.
  if(!this->AppendMode)
    {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    const std::string name = dependee->GetName();
    std::vector<std::string> namespaces = this->FindNamespaces(mf, name);

    int targetOccurrences = (int)namespaces.size();
    if (targetOccurrences == 1)
      {
      std::string missingTarget = namespaces[0];

      missingTarget += dependee->GetExportName();
      link_libs += missingTarget;
      missingTargets.push_back(missingTarget);
      return;
      }
    else
      {
      // We are not appending, so all exported targets should be
      // known here.  This is probably user-error.
      this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
      }
254 255 256 257
    }
  // Assume the target will be exported by another command.
  // Append it with the export namespace.
  link_libs += this->Namespace;
Stephen Kelly's avatar
Stephen Kelly committed
258
  link_libs += dependee->GetExportName();
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
//----------------------------------------------------------------------------
void cmExportBuildFileGenerator
::GetTargets(std::vector<std::string> &targets) const
{
  if (this->ExportSet)
    {
    for(std::vector<cmTargetExport*>::const_iterator
          tei = this->ExportSet->GetTargetExports()->begin();
          tei != this->ExportSet->GetTargetExports()->end(); ++tei)
      {
      targets.push_back((*tei)->Target->GetName());
      }
    return;
    }
  targets = this->Targets;
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
//----------------------------------------------------------------------------
std::vector<std::string>
cmExportBuildFileGenerator
::FindNamespaces(cmMakefile* mf, const std::string& name)
{
  std::vector<std::string> namespaces;
  cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();

  std::map<std::string, cmExportBuildFileGenerator*>& exportSets
                                                  = gg->GetBuildExportSets();

  for(std::map<std::string, cmExportBuildFileGenerator*>::const_iterator
      expIt = exportSets.begin(); expIt != exportSets.end(); ++expIt)
    {
    const cmExportBuildFileGenerator* exportSet = expIt->second;
293 294
    std::vector<std::string> targets;
    exportSet->GetTargets(targets);
295 296 297 298 299 300 301
    if (std::find(targets.begin(), targets.end(), name) != targets.end())
      {
      namespaces.push_back(exportSet->GetNamespace());
      }
    }

  return namespaces;
302 303
}

304 305 306
//----------------------------------------------------------------------------
void
cmExportBuildFileGenerator
307
::ComplainAboutMissingTarget(cmTarget* depender,
308 309
                             cmTarget* dependee,
                             int occurrences)
310
{
311
  if(cmSystemTools::GetErrorOccuredFlag())
312 313 314 315
    {
    return;
    }

316
  cmOStringStream e;
317
  e << "export called with target \"" << depender->GetName()
318 319 320 321 322 323 324 325 326 327 328
    << "\" which requires target \"" << dependee->GetName() << "\" ";
  if (occurrences == 0)
    {
    e << "that is not in the export set.\n";
    }
  else
    {
    e << "that is not in this export set, but " << occurrences
    << " times in others.\n";
    }
  e << "If the required target is not easy to reference in this call, "
329
    << "consider using the APPEND option with multiple separate calls.";
330 331

  this->Makefile->GetCMakeInstance()
Stephen Kelly's avatar
Stephen Kelly committed
332
      ->IssueMessage(cmake::FATAL_ERROR, e.str(), this->Backtrace);
333
}
334 335 336 337 338 339 340 341 342 343 344

std::string
cmExportBuildFileGenerator::InstallNameDir(cmTarget* target,
                                           const std::string& config)
{
  std::string install_name_dir;

  cmMakefile* mf = target->GetMakefile();
  if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
    {
    install_name_dir =
Stephen Kelly's avatar
Stephen Kelly committed
345
      target->GetInstallNameDirForBuildTree(config);
346 347 348 349
    }

  return install_name_dir;
}