1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddLibraryCommand.cxx,v $
6 Date: $Date: 2008-08-18 15:39:22 $
7 Version: $Revision: 1.37 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmAddLibraryCommand.h"
22 bool cmAddLibraryCommand
23 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
27 this->SetError("called with incorrect number of arguments");
30 // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
31 // otherwise it defaults to static library.
32 cmTarget::TargetType type
= cmTarget::SHARED_LIBRARY
;
33 if (cmSystemTools::IsOff(this->Makefile
->GetDefinition("BUILD_SHARED_LIBS")))
35 type
= cmTarget::STATIC_LIBRARY
;
37 bool excludeFromAll
= false;
38 bool importTarget
= false;
40 std::vector
<std::string
>::const_iterator s
= args
.begin();
42 std::string libName
= *s
;
46 // If the second argument is "SHARED" or "STATIC", then it controls
47 // the type of library. Otherwise, it is treated as a source or
48 // source list name. There may be two keyword arguments, check for them
49 bool haveSpecifiedType
= false;
50 while ( s
!= args
.end() )
52 std::string libType
= *s
;
53 if(libType
== "STATIC")
56 type
= cmTarget::STATIC_LIBRARY
;
57 haveSpecifiedType
= true;
59 else if(libType
== "SHARED")
62 type
= cmTarget::SHARED_LIBRARY
;
63 haveSpecifiedType
= true;
65 else if(libType
== "MODULE")
68 type
= cmTarget::MODULE_LIBRARY
;
69 haveSpecifiedType
= true;
71 else if(libType
== "UNKNOWN")
74 type
= cmTarget::UNKNOWN_LIBRARY
;
75 haveSpecifiedType
= true;
77 else if(*s
== "EXCLUDE_FROM_ALL")
80 excludeFromAll
= true;
82 else if(*s
== "IMPORTED")
93 /* ideally we should check whether for the linker language of the target
94 CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
95 STATIC. But at this point we know only the name of the target, but not
96 yet its linker language. */
97 if ((type
!= cmTarget::STATIC_LIBRARY
) &&
98 (this->Makefile
->GetCMakeInstance()->GetPropertyAsBool(
99 "TARGET_SUPPORTS_SHARED_LIBS") == false))
101 std::string msg
= "ADD_LIBRARY for library ";
103 msg
+= " is used with the ";
104 msg
+= type
==cmTarget::SHARED_LIBRARY
? "SHARED" : "MODULE";
105 msg
+= " option, but the target platform supports only STATIC libraries. "
106 "Building it STATIC instead. This may lead to problems.";
107 cmSystemTools::Message(msg
.c_str() ,"Warning");
108 type
= cmTarget::STATIC_LIBRARY
;
111 // The IMPORTED signature requires a type to be specified explicitly.
112 if(importTarget
&& !haveSpecifiedType
)
114 this->SetError("called with IMPORTED argument but no library type.");
118 // Handle imported target creation.
121 // Make sure the target does not already exist.
122 if(this->Makefile
->FindTargetToUse(libName
.c_str()))
125 e
<< "cannot create imported target \"" << libName
126 << "\" because another target with the same name already exists.";
127 this->SetError(e
.str().c_str());
131 // Create the imported target.
132 this->Makefile
->AddImportedTarget(libName
.c_str(), type
);
136 // A non-imported target may not have UNKNOWN type.
137 if(type
== cmTarget::UNKNOWN_LIBRARY
)
139 this->Makefile
->IssueMessage(
141 "The UNKNOWN library type may be used only for IMPORTED libraries."
146 // Enforce name uniqueness.
149 if(!this->Makefile
->EnforceUniqueName(libName
, msg
))
151 this->SetError(msg
.c_str());
158 std::string msg
= "You have called ADD_LIBRARY for library ";
160 msg
+= " without any source files. This typically indicates a problem ";
161 msg
+= "with your CMakeLists.txt file";
162 cmSystemTools::Message(msg
.c_str() ,"Warning");
165 std::vector
<std::string
> srclists
;
166 while (s
!= args
.end())
168 srclists
.push_back(*s
);
172 this->Makefile
->AddLibrary(libName
.c_str(), type
, srclists
,