1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmAddLibraryCommand.cxx,v $
6 <<<<<<< cmAddLibraryCommand.cxx
7 Date: $Date: 2008/02/11 22:33:46 $
8 Version: $Revision: 1.36 $
10 Date: $Date: 2008-08-18 15:39:22 $
11 Version: $Revision: 1.37 $
14 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
15 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
17 This software is distributed WITHOUT ANY WARRANTY; without even
18 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 PURPOSE. See the above copyright notices for more information.
21 =========================================================================*/
22 #include "cmAddLibraryCommand.h"
27 bool cmAddLibraryCommand
28 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
32 this->SetError("called with incorrect number of arguments");
35 // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
36 // otherwise it defaults to static library.
37 cmTarget::TargetType type
= cmTarget::SHARED_LIBRARY
;
38 if (cmSystemTools::IsOff(this->Makefile
->GetDefinition("BUILD_SHARED_LIBS")))
40 type
= cmTarget::STATIC_LIBRARY
;
42 bool excludeFromAll
= false;
43 bool importTarget
= false;
45 std::vector
<std::string
>::const_iterator s
= args
.begin();
47 std::string libName
= *s
;
51 // If the second argument is "SHARED" or "STATIC", then it controls
52 // the type of library. Otherwise, it is treated as a source or
53 // source list name. There may be two keyword arguments, check for them
54 bool haveSpecifiedType
= false;
55 while ( s
!= args
.end() )
57 std::string libType
= *s
;
58 if(libType
== "STATIC")
61 type
= cmTarget::STATIC_LIBRARY
;
62 haveSpecifiedType
= true;
64 else if(libType
== "SHARED")
67 type
= cmTarget::SHARED_LIBRARY
;
68 haveSpecifiedType
= true;
70 else if(libType
== "MODULE")
73 type
= cmTarget::MODULE_LIBRARY
;
74 haveSpecifiedType
= true;
76 else if(libType
== "UNKNOWN")
79 type
= cmTarget::UNKNOWN_LIBRARY
;
80 haveSpecifiedType
= true;
82 else if(*s
== "EXCLUDE_FROM_ALL")
85 excludeFromAll
= true;
87 else if(*s
== "IMPORTED")
98 /* ideally we should check whether for the linker language of the target
99 CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
100 STATIC. But at this point we know only the name of the target, but not
101 yet its linker language. */
102 if ((type
!= cmTarget::STATIC_LIBRARY
) &&
103 (this->Makefile
->GetCMakeInstance()->GetPropertyAsBool(
104 "TARGET_SUPPORTS_SHARED_LIBS") == false))
106 std::string msg
= "ADD_LIBRARY for library ";
108 msg
+= " is used with the ";
109 msg
+= type
==cmTarget::SHARED_LIBRARY
? "SHARED" : "MODULE";
110 msg
+= " option, but the target platform supports only STATIC libraries. "
111 "Building it STATIC instead. This may lead to problems.";
112 cmSystemTools::Message(msg
.c_str() ,"Warning");
113 type
= cmTarget::STATIC_LIBRARY
;
116 // The IMPORTED signature requires a type to be specified explicitly.
117 if(importTarget
&& !haveSpecifiedType
)
119 this->SetError("called with IMPORTED argument but no library type.");
123 // Handle imported target creation.
126 // Make sure the target does not already exist.
127 if(this->Makefile
->FindTargetToUse(libName
.c_str()))
130 e
<< "cannot create imported target \"" << libName
131 << "\" because another target with the same name already exists.";
132 this->SetError(e
.str().c_str());
136 // Create the imported target.
137 this->Makefile
->AddImportedTarget(libName
.c_str(), type
);
141 // A non-imported target may not have UNKNOWN type.
142 if(type
== cmTarget::UNKNOWN_LIBRARY
)
144 this->Makefile
->IssueMessage(
146 "The UNKNOWN library type may be used only for IMPORTED libraries."
151 // Enforce name uniqueness.
154 if(!this->Makefile
->EnforceUniqueName(libName
, msg
))
156 this->SetError(msg
.c_str());
163 std::string msg
= "You have called ADD_LIBRARY for library ";
165 msg
+= " without any source files. This typically indicates a problem ";
166 msg
+= "with your CMakeLists.txt file";
167 cmSystemTools::Message(msg
.c_str() ,"Warning");
170 std::vector
<std::string
> srclists
;
171 while (s
!= args
.end())
173 srclists
.push_back(*s
);
177 this->Makefile
->AddLibrary(libName
.c_str(), type
, srclists
,