Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmExportTryCompileFileGenerator.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2013 Stephen Kelly <steveire@gmail.com>
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12
13 #include "cmExportTryCompileFileGenerator.h"
14
15 #include "cmGeneratedFileStream.h"
16 #include "cmGeneratorExpressionDAGChecker.h"
17
18 //----------------------------------------------------------------------------
19 bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
20 {
21   std::set<cmTarget*> emitted;
22   std::set<cmTarget*> emittedDeps;
23   while(!this->Exports.empty())
24     {
25     cmTarget* te = this->Exports.back();
26     this->Exports.pop_back();
27     if (emitted.insert(te).second)
28       {
29       emittedDeps.insert(te);
30       this->GenerateImportTargetCode(os, te);
31
32       ImportPropertyMap properties;
33
34 #define FIND_TARGETS(PROPERTY) \
35       this->FindTargets(#PROPERTY, te, emittedDeps);
36
37       CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(FIND_TARGETS)
38
39       this->PopulateProperties(te, properties, emittedDeps);
40
41       this->GenerateInterfaceProperties(te, os, properties);
42       }
43     }
44   return true;
45 }
46
47 std::string cmExportTryCompileFileGenerator::FindTargets(const char *propName,
48                                                 cmTarget *tgt,
49                                                 std::set<cmTarget*> &emitted)
50 {
51   const char *prop = tgt->GetProperty(propName);
52   if(!prop)
53     {
54     return std::string();
55     }
56
57   cmListFileBacktrace lfbt;
58   cmGeneratorExpression ge(lfbt);
59
60   cmGeneratorExpressionDAGChecker dagChecker(lfbt,
61                                       tgt->GetName(),
62                                       propName, 0, 0);
63
64   cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
65
66   cmTarget dummyHead;
67   dummyHead.SetType(cmTarget::EXECUTABLE, "try_compile_dummy_exe");
68   dummyHead.SetMakefile(tgt->GetMakefile());
69
70   std::string result = cge->Evaluate(tgt->GetMakefile(), this->Config,
71                                      false, &dummyHead, tgt, &dagChecker);
72
73   const std::set<cmTarget*> &allTargets = cge->GetAllTargetsSeen();
74   for(std::set<cmTarget*>::const_iterator li = allTargets.begin();
75       li != allTargets.end(); ++li)
76     {
77     if(emitted.insert(*li).second)
78       {
79       this->Exports.push_back(*li);
80       }
81     }
82   return result;
83 }
84
85 //----------------------------------------------------------------------------
86 void
87 cmExportTryCompileFileGenerator::PopulateProperties(cmTarget* target,
88                                                 ImportPropertyMap& properties,
89                                                 std::set<cmTarget*> &emitted)
90 {
91   cmPropertyMap props = target->GetProperties();
92   for(cmPropertyMap::const_iterator i = props.begin(); i != props.end(); ++i)
93     {
94     properties[i->first] = i->second.GetValue();
95
96     if(i->first.find("IMPORTED_LINK_INTERFACE_LIBRARIES") == 0
97         || i->first.find("IMPORTED_LINK_DEPENDENT_LIBRARIES") == 0
98         || i->first.find("INTERFACE_LINK_LIBRARIES") == 0)
99       {
100       const std::string libs = i->second.GetValue();
101
102       std::string evalResult = this->FindTargets(i->first.c_str(),
103                                                  target, emitted);
104
105       std::vector<std::string> depends;
106       cmSystemTools::ExpandListArgument(evalResult, depends);
107       for(std::vector<std::string>::const_iterator li = depends.begin();
108           li != depends.end(); ++li)
109         {
110         cmTarget *tgt = target->GetMakefile()->FindTargetToUse(li->c_str());
111         if(tgt && emitted.insert(tgt).second)
112           {
113           this->Exports.push_back(tgt);
114           }
115         }
116       }
117     }
118 }
119 std::string
120 cmExportTryCompileFileGenerator::InstallNameDir(cmTarget* target,
121                                                 const std::string& config)
122 {
123   std::string install_name_dir;
124
125   cmMakefile* mf = target->GetMakefile();
126   if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
127     {
128     install_name_dir =
129       target->GetInstallNameDirForBuildTree(config.c_str());
130     }
131
132   return install_name_dir;
133 }