cmExportInstallFileGenerator.cxx 17.8 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 "cmExportInstallFileGenerator.h"

14
15
#include "cmExportSet.h"
#include "cmExportSetMap.h"
16
#include "cmGeneratedFileStream.h"
17
18
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
19
20
#include "cmInstallExportGenerator.h"
#include "cmInstallTargetGenerator.h"
21
#include "cmTargetExport.h"
22
23
24
25

//----------------------------------------------------------------------------
cmExportInstallFileGenerator
::cmExportInstallFileGenerator(cmInstallExportGenerator* iegen):
26
  IEGen(iegen)
27
28
29
{
}

30
31
32
33
34
35
36
37
38
//----------------------------------------------------------------------------
std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
{
  std::string glob = this->FileBase;
  glob += "-*";
  glob += this->FileExt;
  return glob;
}

39
40
41
//----------------------------------------------------------------------------
bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
{
42
  std::vector<cmTargetExport*> allTargets;
43
44
45
46
47
48
49
  {
  std::string expectedTargets;
  std::string sep;
  for(std::vector<cmTargetExport*>::const_iterator
        tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
      tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
    {
Stephen Kelly's avatar
Stephen Kelly committed
50
    expectedTargets += sep + this->Namespace + (*tei)->Target->GetExportName();
51
    sep = " ";
52
    cmTargetExport * te = *tei;
53
54
    if(this->ExportedTargets.insert(te->Target).second)
      {
55
      allTargets.push_back(te);
56
57
58
59
      }
    else
      {
      cmOStringStream e;
60
      e << "install(EXPORT \""
61
62
        << this->IEGen->GetExportSet()->GetName()
        << "\" ...) " << "includes target \"" << te->Target->GetName()
63
64
65
66
        << "\" more than once in the export set.";
      cmSystemTools::Error(e.str().c_str());
      return false;
      }
67
68
    }

69
70
71
  this->GenerateExpectedTargetsCode(os, expectedTargets);
  }

72
73
74
75
76
  // Add code to compute the installation prefix relative to the
  // import file location.
  const char* installDest = this->IEGen->GetDestination();
  if(!cmSystemTools::FileIsFullPath(installDest))
    {
77
78
    std::string installPrefix =
      this->IEGen->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
79
80
81
82
83
84
85
86
87
    std::string absDest = installPrefix + "/" + installDest;
    std::string absDestS = absDest + "/";
    os << "# Compute the installation prefix relative to this file.\n"
       << "get_filename_component(_IMPORT_PREFIX"
       << " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
    if(strncmp(absDestS.c_str(), "/lib/", 5) == 0 ||
       strncmp(absDestS.c_str(), "/lib64/", 7) == 0 ||
       strncmp(absDestS.c_str(), "/usr/lib/", 9) == 0 ||
       strncmp(absDestS.c_str(), "/usr/lib64/", 11) == 0)
88
      {
89
90
91
92
93
94
95
96
97
98
99
      // Handle "/usr move" symlinks created by some Linux distros.
      os <<
        "# Use original install prefix when loaded through a\n"
        "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
        "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
        "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
        "if(_realCurr STREQUAL _realOrig)\n"
        "  set(_IMPORT_PREFIX \"" << absDest << "\")\n"
        "endif()\n"
        "unset(_realOrig)\n"
        "unset(_realCurr)\n";
100
      }
101
102
    std::string dest = installDest;
    while(!dest.empty())
103
      {
104
105
106
      os <<
        "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" PATH)\n";
      dest = cmSystemTools::GetFilenamePath(dest);
107
      }
108
    os << "\n";
109
110
111
112
113

    // Import location properties may reference this variable.
    this->ImportPrefix = "${_IMPORT_PREFIX}/";
    }

114
115
  std::vector<std::string> missingTargets;

116
  bool require2_8_12 = false;
117
  bool require2_8_13 = false;
118
  // Create all the imported targets.
119
  for(std::vector<cmTargetExport*>::const_iterator
120
121
122
        tei = allTargets.begin();
      tei != allTargets.end(); ++tei)
    {
123
    cmTarget* te = (*tei)->Target;
124
    this->GenerateImportTargetCode(os, te);
125
126
127

    ImportPropertyMap properties;

128
    this->PopulateIncludeDirectoriesInterface(*tei,
129
130
                                  cmGeneratorExpression::InstallInterface,
                                  properties, missingTargets);
131
132
133
    this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES",
                                  te,
                                  cmGeneratorExpression::InstallInterface,
134
135
136
137
138
                                  properties, missingTargets);
    this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
                                  te,
                                  cmGeneratorExpression::InstallInterface,
                                  properties, missingTargets);
139
140
141
142
    this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS",
                                  te,
                                  cmGeneratorExpression::InstallInterface,
                                  properties, missingTargets);
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    const bool newCMP0022Behavior =
                              te->GetPolicyStatusCMP0022() != cmPolicies::WARN
                           && te->GetPolicyStatusCMP0022() != cmPolicies::OLD;
    if (newCMP0022Behavior)
      {
      if (this->PopulateInterfaceLinkLibrariesProperty(te,
                                    cmGeneratorExpression::InstallInterface,
                                    properties, missingTargets)
          && !this->ExportOld)
        {
        require2_8_12 = true;
        }
      }
157
158
159
160
    if (te->GetType() == cmTarget::INTERFACE_LIBRARY)
      {
      require2_8_13 = true;
      }
161
162
    this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
                                  te, properties);
163
    this->PopulateCompatibleInterfaceProperties(te, properties);
164
165

    this->GenerateInterfaceProperties(te, os, properties);
166
167
    }

168
169
170
171
172
  if (require2_8_13)
    {
    this->GenerateRequiredCMakeVersion(os, "2.8.12.20131007");
    }
  else if (require2_8_12)
173
    {
Brad King's avatar
Brad King committed
174
    this->GenerateRequiredCMakeVersion(os, "2.8.12");
175
    }
176

177
178
  // Now load per-configuration properties for them.
  os << "# Load information for each installed configuration.\n"
179
180
     << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
     << "file(GLOB CONFIG_FILES \"${_DIR}/"
181
     << this->GetConfigImportFileGlob() << "\")\n"
182
183
184
     << "foreach(f ${CONFIG_FILES})\n"
     << "  include(${f})\n"
     << "endforeach()\n"
185
186
     << "\n";

187
188
189
190
191
192
193
  // Cleanup the import prefix variable.
  if(!this->ImportPrefix.empty())
    {
    os << "# Cleanup temporary variables.\n"
       << "set(_IMPORT_PREFIX)\n"
       << "\n";
    }
194
195
  this->GenerateImportedFileCheckLoop(os);

196
197
198
199
200
201
  // Generate an import file for each configuration.
  bool result = true;
  for(std::vector<std::string>::const_iterator
        ci = this->Configurations.begin();
      ci != this->Configurations.end(); ++ci)
    {
202
    if(!this->GenerateImportFileConfig(ci->c_str(), missingTargets))
203
204
205
206
      {
      result = false;
      }
    }
207
208
209

  this->GenerateMissingTargetsCheckCode(os, missingTargets);

210
211
212
  return result;
}

Stephen Kelly's avatar
Stephen Kelly committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//----------------------------------------------------------------------------
void
cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string &input)
{
  std::string::size_type pos = 0;
  std::string::size_type lastPos = pos;

  while((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos)
    {
    std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
    input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
    lastPos = endPos;
    }
}

228
229
//----------------------------------------------------------------------------
bool
230
231
cmExportInstallFileGenerator::GenerateImportFileConfig(const char* config,
                                    std::vector<std::string> &missingTargets)
232
233
{
  // Skip configurations not enabled for this export.
234
  if(!this->IEGen->InstallsForConfig(config))
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
    {
    return true;
    }

  // Construct the name of the file to generate.
  std::string fileName = this->FileDir;
  fileName += "/";
  fileName += this->FileBase;
  fileName += "-";
  if(config && *config)
    {
    fileName += cmSystemTools::LowerCase(config);
    }
  else
    {
    fileName += "noconfig";
    }
  fileName += this->FileExt;

  // Open the output file to generate it.
  cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  if(!exportFileStream)
    {
    std::string se = cmSystemTools::GetLastSystemError();
    cmOStringStream e;
    e << "cannot write to file \"" << fileName.c_str()
      << "\": " << se;
    cmSystemTools::Error(e.str().c_str());
    return false;
    }
  std::ostream& os = exportFileStream;

  // Start with the import file header.
  this->GenerateImportHeaderCode(os, config);

  // Generate the per-config target information.
271
  this->GenerateImportConfig(os, config, missingTargets);
272
273
274
275
276
277
278
279
280
281
282
283
284
285

  // End with the import file footer.
  this->GenerateImportFooterCode(os);

  // Record this per-config import file.
  this->ConfigImportFiles[config] = fileName;

  return true;
}

//----------------------------------------------------------------------------
void
cmExportInstallFileGenerator
::GenerateImportTargetsConfig(std::ostream& os,
286
287
                              const char* config, std::string const& suffix,
                              std::vector<std::string> &missingTargets)
288
289
{
  // Add each target in the set to the export.
290
  for(std::vector<cmTargetExport*>::const_iterator
291
292
        tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
      tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
293
294
    {
    // Collect import properties for this target.
295
    cmTargetExport const* te = *tei;
296
    ImportPropertyMap properties;
297
    std::set<std::string> importedLocations;
298
299
300
301
302
303
304
305
    if (!properties.empty()
        && te->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
      {
      this->GenerateImportPropertyCode(os, config, te->Target, properties);
      this->GenerateImportedFileChecksCode(os, te->Target, properties,
                                           importedLocations);
      continue;
      }
306
307
308
309
    this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
                                    properties, importedLocations);
    this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
                                    properties, importedLocations);
310
    this->SetImportLocationProperty(config, suffix,
311
312
313
314
315
316
                                    te->RuntimeGenerator, properties,
                                    importedLocations);
    this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
                                    properties, importedLocations);
    this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
                                    properties, importedLocations);
317
318
319
320
321
322
323

    // If any file location was set for the target add it to the
    // import file.
    if(!properties.empty())
      {
      // Get the rest of the target details.
      this->SetImportDetailProperties(config, suffix,
324
                                      te->Target, properties, missingTargets);
325

326
327
328
329
      this->SetImportLinkInterface(config, suffix,
                                   cmGeneratorExpression::InstallInterface,
                                   te->Target, properties, missingTargets);

330
      // TOOD: PUBLIC_HEADER_LOCATION
331
332
      // This should wait until the build feature propagation stuff
      // is done.  Then this can be a propagated include directory.
333
334
335
336
337
      // this->GenerateImportProperty(config, te->HeaderGenerator,
      //                              properties);

      // Generate code in the export file.
      this->GenerateImportPropertyCode(os, config, te->Target, properties);
338
339
      this->GenerateImportedFileChecksCode(os, te->Target, properties,
                                           importedLocations);
340
341
342
343
344
345
346
347
348
      }
    }
}

//----------------------------------------------------------------------------
void
cmExportInstallFileGenerator
::SetImportLocationProperty(const char* config, std::string const& suffix,
                            cmInstallTargetGenerator* itgen,
349
350
351
                            ImportPropertyMap& properties,
                            std::set<std::string>& importedLocations
                           )
352
353
354
355
356
357
358
{
  // Skip rules that do not match this configuration.
  if(!(itgen && itgen->InstallsForConfig(config)))
    {
    return;
    }

359
360
  // Get the target to be installed.
  cmTarget* target = itgen->GetTarget();
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376

  // Construct the installed location of the target.
  std::string dest = itgen->GetDestination();
  std::string value;
  if(!cmSystemTools::FileIsFullPath(dest.c_str()))
    {
    // The target is installed relative to the installation prefix.
    if(this->ImportPrefix.empty())
      {
      this->ComplainAboutImportPrefix(itgen);
      }
    value = this->ImportPrefix;
    }
  value += dest;
  value += "/";

377
  if(itgen->IsImportLibrary())
378
    {
379
380
381
382
383
384
385
386
387
388
    // Construct the property name.
    std::string prop = "IMPORTED_IMPLIB";
    prop += suffix;

    // Append the installed file name.
    value += itgen->GetInstallFilename(target, config,
                                       cmInstallTargetGenerator::NameImplib);

    // Store the property.
    properties[prop] = value;
389
    importedLocations.insert(prop);
390
    }
391
  else
392
    {
393
394
395
    // Construct the property name.
    std::string prop = "IMPORTED_LOCATION";
    prop += suffix;
396

397
    // Append the installed file name.
398
    if(target->IsAppBundleOnApple())
399
400
401
402
403
404
405
406
407
408
409
410
411
      {
      value += itgen->GetInstallFilename(target, config);
      value += ".app/Contents/MacOS/";
      value += itgen->GetInstallFilename(target, config);
      }
    else
      {
      value += itgen->GetInstallFilename(target, config,
                                         cmInstallTargetGenerator::NameReal);
      }

    // Store the property.
    properties[prop] = value;
412
    importedLocations.insert(prop);
413
    }
414
415
}

416
417
418
419
420
421
//----------------------------------------------------------------------------
void
cmExportInstallFileGenerator::HandleMissingTarget(
  std::string& link_libs, std::vector<std::string>& missingTargets,
  cmMakefile* mf, cmTarget* depender, cmTarget* dependee)
{
Stephen Kelly's avatar
Stephen Kelly committed
422
  const std::string name = dependee->GetName();
423
424
425
426
427
  std::vector<std::string> namespaces = this->FindNamespaces(mf, name);
  int targetOccurrences = (int)namespaces.size();
  if (targetOccurrences == 1)
    {
    std::string missingTarget = namespaces[0];
Stephen Kelly's avatar
Stephen Kelly committed
428
429

    missingTarget += dependee->GetExportName();
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    link_libs += missingTarget;
    missingTargets.push_back(missingTarget);
    }
  else
    {
    // We are not appending, so all exported targets should be
    // known here.  This is probably user-error.
    this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
    }
}

//----------------------------------------------------------------------------
std::vector<std::string>
cmExportInstallFileGenerator
::FindNamespaces(cmMakefile* mf, const std::string& name)
{
  std::vector<std::string> namespaces;
  cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
  const cmExportSetMap& exportSets = gg->GetExportSets();

  for(cmExportSetMap::const_iterator expIt = exportSets.begin();
      expIt != exportSets.end();
      ++expIt)
    {
    const cmExportSet* exportSet = expIt->second;
    std::vector<cmTargetExport*> const* targets =
                                                 exportSet->GetTargetExports();

    bool containsTarget = false;
    for(unsigned int i=0; i<targets->size(); i++)
      {
      if (name == (*targets)[i]->Target->GetName())
        {
        containsTarget = true;
        break;
        }
      }

    if (containsTarget)
      {
      std::vector<cmInstallExportGenerator const*> const* installs =
                                                 exportSet->GetInstallations();
      for(unsigned int i=0; i<installs->size(); i++)
        {
        namespaces.push_back((*installs)[i]->GetNamespace());
        }
      }
    }

  return namespaces;
}


483
484
485
486
487
//----------------------------------------------------------------------------
void
cmExportInstallFileGenerator
::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
{
488
  const char* installDest = this->IEGen->GetDestination();
489
  cmOStringStream e;
490
  e << "install(EXPORT \""
491
492
    << this->IEGen->GetExportSet()->GetName()
    << "\") given absolute "
493
494
495
496
497
498
499
500
501
502
    << "DESTINATION \"" << installDest << "\" but the export "
    << "references an installation of target \""
    << itgen->GetTarget()->GetName() << "\" which has relative "
    << "DESTINATION \"" << itgen->GetDestination() << "\".";
  cmSystemTools::Error(e.str().c_str());
}

//----------------------------------------------------------------------------
void
cmExportInstallFileGenerator
503
504
505
::ComplainAboutMissingTarget(cmTarget* depender,
                             cmTarget* dependee,
                             int occurrences)
506
507
{
  cmOStringStream e;
508
  e << "install(EXPORT \""
509
510
    << this->IEGen->GetExportSet()->GetName()
    << "\" ...) "
511
    << "includes target \"" << depender->GetName()
512
513
514
515
516
517
518
519
520
521
    << "\" which requires target \"" << dependee->GetName() << "\" ";
  if (occurrences == 0)
    {
    e << "that is not in the export set.";
    }
  else
    {
    e << "that is not in this export set, but " << occurrences
    << " times in others.";
    }
522
  cmSystemTools::Error(e.str().c_str());
523
}
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539

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

  cmMakefile* mf = target->GetMakefile();
  if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
    {
    install_name_dir =
      target->GetInstallNameDirForInstallTree();
    }

  return install_name_dir;
}