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;
46 while ( s != args.end() )
48 std::string libType = *s;
49 if(libType == "STATIC")
52 type = cmTarget::STATIC_LIBRARY;
53 haveSpecifiedType = true;
55 else if(libType == "SHARED")
58 type = cmTarget::SHARED_LIBRARY;
59 haveSpecifiedType = true;
61 else if(libType == "MODULE")
64 type = cmTarget::MODULE_LIBRARY;
65 haveSpecifiedType = true;
67 else if(libType == "OBJECT")
70 type = cmTarget::OBJECT_LIBRARY;
71 haveSpecifiedType = true;
73 else if(libType == "UNKNOWN")
76 type = cmTarget::UNKNOWN_LIBRARY;
77 haveSpecifiedType = true;
79 else if(*s == "EXCLUDE_FROM_ALL")
82 excludeFromAll = true;
84 else if(*s == "IMPORTED")
89 else if(importTarget && *s == "GLOBAL")
100 /* ideally we should check whether for the linker language of the target
101 CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
102 STATIC. But at this point we know only the name of the target, but not
103 yet its linker language. */
104 if ((type != cmTarget::STATIC_LIBRARY) &&
105 (type != cmTarget::OBJECT_LIBRARY) &&
106 (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
107 "TARGET_SUPPORTS_SHARED_LIBS") == false))
111 "ADD_LIBRARY called with " <<
112 (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
113 " option but the target platform does not support dynamic linking. "
114 "Building a STATIC library instead. This may lead to problems.";
115 this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
116 type = cmTarget::STATIC_LIBRARY;
119 // Handle imported target creation.
122 // The IMPORTED signature requires a type to be specified explicitly.
123 if (!haveSpecifiedType)
125 this->SetError("called with IMPORTED argument but no library type.");
128 if(type == cmTarget::OBJECT_LIBRARY)
130 this->Makefile->IssueMessage(
132 "The OBJECT library type may not be used for IMPORTED libraries."
137 // Make sure the target does not already exist.
138 if(this->Makefile->FindTargetToUse(libName.c_str()))
141 e << "cannot create imported target \"" << libName
142 << "\" because another target with the same name already exists.";
143 this->SetError(e.str().c_str());
147 // Create the imported target.
148 this->Makefile->AddImportedTarget(libName.c_str(), type, importGlobal);
152 // A non-imported target may not have UNKNOWN type.
153 if(type == cmTarget::UNKNOWN_LIBRARY)
155 this->Makefile->IssueMessage(
157 "The UNKNOWN library type may be used only for IMPORTED libraries."
162 // Enforce name uniqueness.
165 if(!this->Makefile->EnforceUniqueName(libName, msg))
167 this->SetError(msg.c_str());
174 std::string msg = "You have called ADD_LIBRARY for library ";
176 msg += " without any source files. This typically indicates a problem ";
177 msg += "with your CMakeLists.txt file";
178 cmSystemTools::Message(msg.c_str() ,"Warning");
181 std::vector<std::string> srclists;
182 while (s != args.end())
184 srclists.push_back(*s);
188 this->Makefile->AddLibrary(libName.c_str(), type, srclists, excludeFromAll);