summaryrefslogtreecommitdiffstats
path: root/Source/cmExportFileGenerator.cxx
blob: 2ecac8477fe4f67f782bce1a3d379ef2bc7eec85 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

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

  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.
============================================================================*/
#include "cmExportFileGenerator.h"

#include "cmExportSet.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmInstallExportGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
#include "cmTargetExport.h"
#include "cmVersion.h"
#include "cmComputeLinkInformation.h"

#include <cmsys/auto_ptr.hxx>

//----------------------------------------------------------------------------
cmExportFileGenerator::cmExportFileGenerator()
{
  this->AppendMode = false;
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::AddConfiguration(const char* config)
{
  this->Configurations.push_back(config);
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::SetExportFile(const char* mainFile)
{
  this->MainImportFile = mainFile;
  this->FileDir =
    cmSystemTools::GetFilenamePath(this->MainImportFile);
  this->FileBase =
    cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  this->FileExt =
    cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
}

//----------------------------------------------------------------------------
bool cmExportFileGenerator::GenerateImportFile()
{
  // Open the output file to generate it.
  cmsys::auto_ptr<std::ofstream> foutPtr;
  if(this->AppendMode)
    {
    // Open for append.
    cmsys::auto_ptr<std::ofstream>
      ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
    foutPtr = ap;
    }
  else
    {
    // Generate atomically and with copy-if-different.
    cmsys::auto_ptr<cmGeneratedFileStream>
      ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
    ap->SetCopyIfDifferent(true);
    foutPtr = ap;
    }
  if(!foutPtr.get() || !*foutPtr)
    {
    std::string se = cmSystemTools::GetLastSystemError();
    cmOStringStream e;
    e << "cannot write to file \"" << this->MainImportFile
      << "\": " << se;
    cmSystemTools::Error(e.str().c_str());
    return false;
    }
  std::ostream& os = *foutPtr;

  // Protect that file against use with older CMake versions.
  os << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
  os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
     << "   message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
     << "endif()\n";

  // Isolate the file policy level.
  // We use 2.6 here instead of the current version because newer
  // versions of CMake should be able to export files imported by 2.6
  // until the import format changes.
  os << "cmake_policy(PUSH)\n"
     << "cmake_policy(VERSION 2.6)\n";

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

  // Create all the imported targets.
  bool result = this->GenerateMainFile(os);

  // End with the import file footer.
  this->GenerateImportFooterCode(os);
  os << "cmake_policy(POP)\n";

  return result;
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
                                    const char* config,
                                    std::vector<std::string> &missingTargets)
{
  // Construct the property configuration suffix.
  std::string suffix = "_";
  if(config && *config)
    {
    suffix += cmSystemTools::UpperCase(config);
    }
  else
    {
    suffix += "NOCONFIG";
    }

  // Generate the per-config target information.
  this->GenerateImportTargetsConfig(os, config, suffix, missingTargets);
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
                                              cmTarget *target,
                                              ImportPropertyMap &properties)
{
  const char *input = target->GetProperty(propName);
  if (input)
    {
    properties[propName] = input;
    }
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
                      const char *outputName,
                      cmTarget *target,
                      cmGeneratorExpression::PreprocessContext preprocessRule,
                      ImportPropertyMap &properties,
                      std::vector<std::string> &missingTargets)
{
  const char *input = target->GetProperty(propName);
  if (input)
    {
    if (!*input)
      {
      // Set to empty
      properties[outputName] = "";
      return;
      }

    std::string prepro = cmGeneratorExpression::Preprocess(input,
                                                           preprocessRule);
    if (!prepro.empty())
      {
      this->ResolveTargetsInGeneratorExpressions(prepro, target,
                                                 missingTargets);
      properties[outputName] = prepro;
      }
    }
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
                      cmTarget *target,
                      cmGeneratorExpression::PreprocessContext preprocessRule,
                      ImportPropertyMap &properties,
                      std::vector<std::string> &missingTargets)
{
  this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
                            properties, missingTargets);
}


//----------------------------------------------------------------------------
void getPropertyContents(cmTarget *tgt, const char *prop,
         std::set<std::string> &ifaceProperties)
{
  const char *p = tgt->GetProperty(prop);
  if (!p)
    {
    return;
    }
  std::vector<std::string> content;
  cmSystemTools::ExpandListArgument(p, content);
  for (std::vector<std::string>::const_iterator ci = content.begin();
    ci != content.end(); ++ci)
    {
    ifaceProperties.insert(*ci);
    }
}

//----------------------------------------------------------------------------
void getCompatibleInterfaceProperties(cmTarget *target,
                                      std::set<std::string> &ifaceProperties,
                                      const char *config)
{
  cmComputeLinkInformation *info = target->GetLinkInformation(config);

  const cmComputeLinkInformation::ItemVector &deps = info->GetItems();

  for(cmComputeLinkInformation::ItemVector::const_iterator li =
      deps.begin();
      li != deps.end(); ++li)
    {
    if (!li->Target)
      {
      continue;
      }
    getPropertyContents(li->Target,
                        "COMPATIBLE_INTERFACE_BOOL",
                        ifaceProperties);
    getPropertyContents(li->Target,
                        "COMPATIBLE_INTERFACE_STRING",
                        ifaceProperties);
    }
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
                                cmTarget *target,
                                ImportPropertyMap &properties)
{
  this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
                                target, properties);
  this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
                                target, properties);

  std::set<std::string> ifaceProperties;

  getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);

  getCompatibleInterfaceProperties(target, ifaceProperties, 0);

  std::vector<std::string> configNames;
  target->GetMakefile()->GetConfigurations(configNames);

  for (std::vector<std::string>::const_iterator ci = configNames.begin();
    ci != configNames.end(); ++ci)
    {
    getCompatibleInterfaceProperties(target, ifaceProperties, ci->c_str());
    }

  for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
    it != ifaceProperties.end(); ++it)
    {
    this->PopulateInterfaceProperty(("INTERFACE_" + *it).c_str(),
                                    target, properties);
    }
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget *target,
                                        std::ostream& os,
                                        const ImportPropertyMap &properties)
{
  if (!properties.empty())
    {
    os << "if(NOT ${CMAKE_FIND_PACKAGE_NAME}_NO_INTERFACES)\n";
    std::string targetName = this->Namespace;
    targetName += target->GetName();
    os << "  set_target_properties(" << targetName << " PROPERTIES\n";
    for(ImportPropertyMap::const_iterator pi = properties.begin();
        pi != properties.end(); ++pi)
      {
      os << "    " << pi->first << " \"" << pi->second << "\"\n";
      }
    os << "  )\nendif()\n\n";
    }
}

//----------------------------------------------------------------------------
bool
cmExportFileGenerator::AddTargetNamespace(std::string &input,
                                    cmTarget* target,
                                    std::vector<std::string> &missingTargets)
{
  cmMakefile *mf = target->GetMakefile();

  cmTarget *tgt = mf->FindTargetToUse(input.c_str());
  if (!tgt)
    {
    return false;
    }

  if(tgt->IsImported())
    {
    return true;
    }
  if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
    {
    input = this->Namespace + input;
    }
  else
    {
    std::string namespacedTarget;
    this->HandleMissingTarget(namespacedTarget, missingTargets,
                              mf, target, tgt);
    if (!namespacedTarget.empty())
      {
      input = namespacedTarget;
      }
    }
  return true;
}

//----------------------------------------------------------------------------
static bool isGeneratorExpression(const std::string &lib)
{
  const std::string::size_type openpos = lib.find("$<");
  return (openpos != std::string::npos)
      && (lib.find(">", openpos) != std::string::npos);
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
                                    std::string &input,
                                    cmTarget* target,
                                    std::vector<std::string> &missingTargets,
                                    FreeTargetsReplace replace)
{
  if (replace == NoReplaceFreeTargets)
    {
    this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
    return;
    }
  std::vector<std::string> parts;
  cmGeneratorExpression::Split(input, parts);

  std::string sep;
  input = "";
  for(std::vector<std::string>::iterator li = parts.begin();
      li != parts.end(); ++li)
    {
    if (!isGeneratorExpression(*li))
      {
      this->AddTargetNamespace(*li, target, missingTargets);
      }
    else
      {
      this->ResolveTargetsInGeneratorExpression(
                                    *li,
                                    target,
                                    missingTargets);
      }
    input += sep + *li;
    sep = ";";
    }
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
                                    std::string &input,
                                    cmTarget* target,
                                    std::vector<std::string> &missingTargets)
{
  std::string::size_type pos = 0;
  std::string::size_type lastPos = pos;

  cmMakefile *mf = target->GetMakefile();

  while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
    {
    std::string::size_type nameStartPos = pos +
                                            sizeof("$<TARGET_PROPERTY:") - 1;
    std::string::size_type closePos = input.find(">", nameStartPos);
    std::string::size_type commaPos = input.find(",", nameStartPos);
    std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
    if (commaPos == input.npos // Implied 'this' target
        || closePos == input.npos // Imcomplete expression.
        || closePos < commaPos // Implied 'this' target
        || nextOpenPos < commaPos) // Non-literal
      {
      lastPos = nameStartPos;
      continue;
      }

    std::string targetName = input.substr(nameStartPos,
                                                commaPos - nameStartPos);

    if (this->AddTargetNamespace(targetName, target, missingTargets))
      {
      input.replace(nameStartPos, commaPos - nameStartPos, targetName);
      }
    lastPos = nameStartPos + targetName.size() + 1;
    }

  std::string errorString;
  pos = 0;
  lastPos = pos;
  while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
    {
    std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
    std::string::size_type endPos = input.find(">", nameStartPos);
    if (endPos == input.npos)
      {
      errorString = "$<TARGET_NAME:...> expression incomplete";
      break;
      }
    std::string targetName = input.substr(nameStartPos,
                                                endPos - nameStartPos);
    if(targetName.find("$<") != input.npos)
      {
      errorString = "$<TARGET_NAME:...> requires its parameter to be a "
                    "literal.";
      break;
      }
    if (!this->AddTargetNamespace(targetName, target, missingTargets))
      {
      errorString = "$<TARGET_NAME:...> requires its parameter to be a "
                    "reachable target.";
      break;
      }
    input.replace(pos, endPos - pos + 1, targetName);
    lastPos = endPos;
    }

  this->ReplaceInstallPrefix(input);

  if (!errorString.empty())
    {
    mf->IssueMessage(cmake::FATAL_ERROR, errorString);
    }
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
{
  // Do nothing
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator
::SetImportLinkInterface(const char* config, std::string const& suffix,
                    cmGeneratorExpression::PreprocessContext preprocessRule,
                    cmTarget* target, ImportPropertyMap& properties,
                    std::vector<std::string>& missingTargets)
{
  // Add the transitive link dependencies for this configuration.
  cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
                                                                  target);
  if (!iface)
    {
    return;
    }

  if (iface->ImplementationIsInterface)
    {
    this->SetImportLinkProperty(suffix, target,
                                "IMPORTED_LINK_INTERFACE_LIBRARIES",
                                iface->Libraries, properties, missingTargets);
    return;
    }

  const char *propContent;

  if (const char *prop_suffixed = target->GetProperty(
                    ("LINK_INTERFACE_LIBRARIES" + suffix).c_str()))
    {
    propContent = prop_suffixed;
    }
  else if (const char *prop = target->GetProperty(
                    "LINK_INTERFACE_LIBRARIES"))
    {
    propContent = prop;
    }
  else
    {
    return;
    }

  if (!*propContent)
    {
    properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
    return;
    }

  std::string prepro = cmGeneratorExpression::Preprocess(propContent,
                                                         preprocessRule);
  if (!prepro.empty())
    {
    this->ResolveTargetsInGeneratorExpressions(prepro, target,
                                               missingTargets,
                                               ReplaceFreeTargets);
    properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
    }
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator
::SetImportDetailProperties(const char* config, std::string const& suffix,
                            cmTarget* target, ImportPropertyMap& properties,
                            std::vector<std::string>& missingTargets
                           )
{
  // Get the makefile in which to lookup target information.
  cmMakefile* mf = target->GetMakefile();

  // Add the soname for unix shared libraries.
  if(target->GetType() == cmTarget::SHARED_LIBRARY ||
     target->GetType() == cmTarget::MODULE_LIBRARY)
    {
    // Check whether this is a DLL platform.
    bool dll_platform =
      (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
    if(!dll_platform)
      {
      std::string prop;
      std::string value;
      if(target->HasSOName(config))
        {
        prop = "IMPORTED_SONAME";
        value = target->GetSOName(config);
        }
      else
        {
        prop = "IMPORTED_NO_SONAME";
        value = "TRUE";
        }
      prop += suffix;
      properties[prop] = value;
      }
    }

  // Add the transitive link dependencies for this configuration.
  if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
                                                                     target))
    {
    this->SetImportLinkProperty(suffix, target,
                                "IMPORTED_LINK_INTERFACE_LANGUAGES",
                                iface->Languages, properties, missingTargets);

    this->SetImportLinkProperty(suffix, target,
                                "IMPORTED_LINK_DEPENDENT_LIBRARIES",
                                iface->SharedDeps, properties, missingTargets);
    if(iface->Multiplicity > 0)
      {
      std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
      prop += suffix;
      cmOStringStream m;
      m << iface->Multiplicity;
      properties[prop] = m.str();
      }
    }
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator
::SetImportLinkProperty(std::string const& suffix,
                        cmTarget* target,
                        const char* propName,
                        std::vector<std::string> const& libs,
                        ImportPropertyMap& properties,
                        std::vector<std::string>& missingTargets
                       )
{
  // Skip the property if there are no libraries.
  if(libs.empty())
    {
    return;
    }

  // Construct the property value.
  std::string link_libs;
  const char* sep = "";
  for(std::vector<std::string>::const_iterator li = libs.begin();
      li != libs.end(); ++li)
    {
    // Separate this from the previous entry.
    link_libs += sep;
    sep = ";";

    std::string temp = *li;
    this->AddTargetNamespace(temp, target, missingTargets);
    link_libs += temp;
    }

  // Store the property.
  std::string prop = propName;
  prop += suffix;
  properties[prop] = link_libs;
}


//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
                                                     const char* config)
{
  os << "#----------------------------------------------------------------\n"
     << "# Generated CMake target import file";
  if(config)
    {
    os << " for configuration \"" << config << "\".\n";
    }
  else
    {
    os << ".\n";
    }
  os << "#----------------------------------------------------------------\n"
     << "\n";
  this->GenerateImportVersionCode(os);
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
{
  os << "# Commands beyond this point should not need to know the version.\n"
     << "set(CMAKE_IMPORT_FILE_VERSION)\n";
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
{
  // Store an import file format version.  This will let us change the
  // format later while still allowing old import files to work.
  os << "# Commands may need to know the format version.\n"
     << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
     << "\n";
}

//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
                                            const std::string &expectedTargets)
{
  os << "set(_targetsDefined)\n"
        "set(_targetsNotDefined)\n"
        "set(_expectedTargets)\n"
        "foreach(_expectedTarget " << expectedTargets << ")\n"
        "  list(APPEND _expectedTargets ${_expectedTarget})\n"
        "  if(NOT TARGET ${_expectedTarget})\n"
        "    list(APPEND _targetsNotDefined ${_expectedTarget})\n"
        "  endif()\n"
        "  if(TARGET ${_expectedTarget})\n"
        "    list(APPEND _targetsDefined ${_expectedTarget})\n"
        "  endif()\n"
        "endforeach()\n"
        "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
        "  set(CMAKE_IMPORT_FILE_VERSION)\n"
        "  cmake_policy(POP)\n"
        "  return()\n"
        "endif()\n"
        "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
        "  message(FATAL_ERROR \"Some (but not all) targets in this export "
        "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
        "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
        "endif()\n"
        "unset(_targetsDefined)\n"
        "unset(_targetsNotDefined)\n"
        "unset(_expectedTargets)\n"
        "\n\n";
}
//----------------------------------------------------------------------------
void
cmExportFileGenerator
::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
{
  // Construct the imported target name.
  std::string targetName = this->Namespace;
  targetName += target->GetName();

  // Create the imported target.
  os << "# Create imported target " << targetName << "\n";
  switch(target->GetType())
    {
    case cmTarget::EXECUTABLE:
      os << "add_executable(" << targetName << " IMPORTED)\n";
      break;
    case cmTarget::STATIC_LIBRARY:
      os << "add_library(" << targetName << " STATIC IMPORTED)\n";
      break;
    case cmTarget::SHARED_LIBRARY:
      os << "add_library(" << targetName << " SHARED IMPORTED)\n";
      break;
    case cmTarget::MODULE_LIBRARY:
      os << "add_library(" << targetName << " MODULE IMPORTED)\n";
      break;
    default:  // should never happen
      break;
    }

  // Mark the imported executable if it has exports.
  if(target->IsExecutableWithExports())
    {
    os << "set_property(TARGET " << targetName
       << " PROPERTY ENABLE_EXPORTS 1)\n";
    }

  // Mark the imported library if it is a framework.
  if(target->IsFrameworkOnApple())
    {
    os << "set_property(TARGET " << targetName
       << " PROPERTY FRAMEWORK 1)\n";
    }

  // Mark the imported executable if it is an application bundle.
  if(target->IsAppBundleOnApple())
    {
    os << "set_property(TARGET " << targetName
       << " PROPERTY MACOSX_BUNDLE 1)\n";
    }

  if (target->IsCFBundleOnApple())
    {
    os << "set_property(TARGET " << targetName
       << " PROPERTY BUNDLE 1)\n";
    }
  os << "\n";
}

//----------------------------------------------------------------------------
void
cmExportFileGenerator
::GenerateImportPropertyCode(std::ostream& os, const char* config,
                             cmTarget* target,
                             ImportPropertyMap const& properties)
{
  // Construct the imported target name.
  std::string targetName = this->Namespace;
  targetName += target->GetName();

  // Set the import properties.
  os << "# Import target \"" << targetName << "\" for configuration \""
     << config << "\"\n";
  os << "set_property(TARGET " << targetName
     << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  if(config && *config)
    {
    os << cmSystemTools::UpperCase(config);
    }
  else
    {
    os << "NOCONFIG";
    }
  os << ")\n";
  os << "set_target_properties(" << targetName << " PROPERTIES\n";
  for(ImportPropertyMap::const_iterator pi = properties.begin();
      pi != properties.end(); ++pi)
    {
    os << "  " << pi->first << " \"" << pi->second << "\"\n";
    }
  os << "  )\n"
     << "\n";
}


//----------------------------------------------------------------------------
void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
                                const std::vector<std::string>& missingTargets)
{
  if (missingTargets.empty())
    {
    return;
    }
  os << "# Make sure the targets which have been exported in some other \n"
        "# export set exist.\n";
  std::set<std::string> emitted;
  for(unsigned int i=0; i<missingTargets.size(); ++i)
    {
    if (emitted.insert(missingTargets[i]).second)
      {
      os << "if(NOT TARGET \"" << missingTargets[i] << "\" )\n"
        << "  if(CMAKE_FIND_PACKAGE_NAME)\n"
        << "    set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
        << "    set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
        << "\"Required imported target \\\"" << missingTargets[i]
        << "\\\" not found ! \")\n"
        << "  else()\n"
        << "    message(FATAL_ERROR \"Required imported target \\\""
        << missingTargets[i] << "\\\" not found ! \")\n"
        << "  endif()\n"
        << "endif()\n";
      }
    }
  os << "\n";
}


//----------------------------------------------------------------------------
void
cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
{
  // Add code which verifies at cmake time that the file which is being
  // imported actually exists on disk. This should in theory always be theory
  // case, but still when packages are split into normal and development
  // packages this might get broken (e.g. the Config.cmake could be part of
  // the non-development package, something similar happened to me without
  // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  // but the development package was not installed.).
  os << "# Loop over all imported files and verify that they actually exist\n"
        "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
        "  foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
        "    if(NOT EXISTS \"${file}\" )\n"
        "      message(FATAL_ERROR \"The imported target \\\"${target}\\\""
        " references the file\n"
        "   \\\"${file}\\\"\n"
        "but this file does not exist.  Possible reasons include:\n"
        "* The file was deleted, renamed, or moved to another location.\n"
        "* An install or uninstall procedure did not complete successfully.\n"
        "* The installation package was faulty and contained\n"
        "   \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
        "but not all the files it references.\n"
        "\")\n"
        "    endif()\n"
        "  endforeach()\n"
        "  unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
        "endforeach()\n"
        "unset(_IMPORT_CHECK_TARGETS)\n"
        "\n";
}


//----------------------------------------------------------------------------
void
cmExportFileGenerator
::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
                                 ImportPropertyMap const& properties,
                                const std::set<std::string>& importedLocations)
{
  // Construct the imported target name.
  std::string targetName = this->Namespace;
  targetName += target->GetName();

  os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
        "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";

  for(std::set<std::string>::const_iterator li = importedLocations.begin();
      li != importedLocations.end();
      ++li)
    {
    ImportPropertyMap::const_iterator pi = properties.find(*li);
    if (pi != properties.end())
      {
      os << "\"" << pi->second << "\" ";
      }
    }

  os << ")\n\n";
}