1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
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 #include "cmAddLibraryCommand.h"
17 bool cmAddLibraryCommand
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
22 this->SetError("called with incorrect number of arguments");
25 // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
26 // otherwise it defaults to static library.
27 cmTarget::TargetType type = cmTarget::SHARED_LIBRARY;
28 if (cmSystemTools::IsOff(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
30 type = cmTarget::STATIC_LIBRARY;
32 bool excludeFromAll = false;
33 bool importTarget = false;
34 bool importGlobal = false;
36 std::vector<std::string>::const_iterator s = args.begin();
38 std::string libName = *s;
42 // If the second argument is "SHARED" or "STATIC", then it controls
43 // the type of library. Otherwise, it is treated as a source or
44 // source list name. There may be two keyword arguments, check for them
45 bool haveSpecifiedType = false;
47 while ( s != args.end() )
49 std::string libType = *s;
50 if(libType == "STATIC")
53 type = cmTarget::STATIC_LIBRARY;
54 haveSpecifiedType = true;
56 else if(libType == "SHARED")
59 type = cmTarget::SHARED_LIBRARY;
60 haveSpecifiedType = true;
62 else if(libType == "MODULE")
65 type = cmTarget::MODULE_LIBRARY;
66 haveSpecifiedType = true;
68 else if(libType == "OBJECT")
71 type = cmTarget::OBJECT_LIBRARY;
72 haveSpecifiedType = true;
74 else if(libType == "UNKNOWN")
77 type = cmTarget::UNKNOWN_LIBRARY;
78 haveSpecifiedType = true;
80 else if(libType == "ALIAS")
85 else if(*s == "EXCLUDE_FROM_ALL")
88 excludeFromAll = true;
90 else if(*s == "IMPORTED")
95 else if(importTarget && *s == "GLOBAL")
107 if(!cmGeneratorExpression::IsValidTargetName(libName.c_str()))
109 this->SetError(("Invalid name for ALIAS: " + libName).c_str());
114 this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
117 if(importTarget || importGlobal)
119 this->SetError("IMPORTED with ALIAS is not allowed.");
125 e << "ALIAS requires exactly one target argument.";
126 this->SetError(e.str().c_str());
130 const char *aliasedName = s->c_str();
131 if(this->Makefile->IsAlias(aliasedName))
134 e << "cannot create ALIAS target \"" << libName
135 << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
136 this->SetError(e.str().c_str());
139 cmTarget *aliasedTarget =
140 this->Makefile->FindTargetToUse(aliasedName, true);
144 e << "cannot create ALIAS target \"" << libName
145 << "\" because target \"" << aliasedName << "\" does not already "
147 this->SetError(e.str().c_str());
150 cmTarget::TargetType aliasedType = aliasedTarget->GetType();
151 if(aliasedType != cmTarget::SHARED_LIBRARY
152 && aliasedType != cmTarget::STATIC_LIBRARY
153 && aliasedType != cmTarget::MODULE_LIBRARY
154 && aliasedType != cmTarget::OBJECT_LIBRARY)
157 e << "cannot create ALIAS target \"" << libName
158 << "\" because target \"" << aliasedName << "\" is not a library.";
159 this->SetError(e.str().c_str());
162 if(aliasedTarget->IsImported())
165 e << "cannot create ALIAS target \"" << libName
166 << "\" because target \"" << aliasedName << "\" is IMPORTED.";
167 this->SetError(e.str().c_str());
170 this->Makefile->AddAlias(libName.c_str(), aliasedTarget);
174 if(importTarget && excludeFromAll)
176 this->SetError("excludeFromAll with IMPORTED target makes no sense.");
180 /* ideally we should check whether for the linker language of the target
181 CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
182 STATIC. But at this point we know only the name of the target, but not
183 yet its linker language. */
184 if ((type != cmTarget::STATIC_LIBRARY) &&
185 (type != cmTarget::OBJECT_LIBRARY) &&
186 (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
187 "TARGET_SUPPORTS_SHARED_LIBS") == false))
191 "ADD_LIBRARY called with " <<
192 (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
193 " option but the target platform does not support dynamic linking. "
194 "Building a STATIC library instead. This may lead to problems.";
195 this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
196 type = cmTarget::STATIC_LIBRARY;
199 // Handle imported target creation.
202 // The IMPORTED signature requires a type to be specified explicitly.
203 if (!haveSpecifiedType)
205 this->SetError("called with IMPORTED argument but no library type.");
208 if(type == cmTarget::OBJECT_LIBRARY)
210 this->Makefile->IssueMessage(
212 "The OBJECT library type may not be used for IMPORTED libraries."
217 // Make sure the target does not already exist.
218 if(this->Makefile->FindTargetToUse(libName.c_str()))
221 e << "cannot create imported target \"" << libName
222 << "\" because another target with the same name already exists.";
223 this->SetError(e.str().c_str());
227 // Create the imported target.
228 this->Makefile->AddImportedTarget(libName.c_str(), type, importGlobal);
232 // A non-imported target may not have UNKNOWN type.
233 if(type == cmTarget::UNKNOWN_LIBRARY)
235 this->Makefile->IssueMessage(
237 "The UNKNOWN library type may be used only for IMPORTED libraries."
242 // Enforce name uniqueness.
245 if(!this->Makefile->EnforceUniqueName(libName, msg))
247 this->SetError(msg.c_str());
254 std::string msg = "You have called ADD_LIBRARY for library ";
256 msg += " without any source files. This typically indicates a problem ";
257 msg += "with your CMakeLists.txt file";
258 cmSystemTools::Message(msg.c_str() ,"Warning");
261 std::vector<std::string> srclists;
262 while (s != args.end())
264 srclists.push_back(*s);
268 this->Makefile->AddLibrary(libName.c_str(), type, srclists, excludeFromAll);