Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmTarget.cxx
blobd008d10fdf9cc7ea441f5466c49ddca1190ea251
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.cxx,v $
5 Language: C++
6 Date: $Date: 2008-04-01 18:22:07 $
7 Version: $Revision: 1.212 $
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 "cmTarget.h"
18 #include "cmake.h"
19 #include "cmMakefile.h"
20 #include "cmSourceFile.h"
21 #include "cmLocalGenerator.h"
22 #include "cmGlobalGenerator.h"
23 #include "cmComputeLinkInformation.h"
24 #include "cmListFileCache.h"
25 #include <map>
26 #include <set>
27 #include <queue>
28 #include <stdlib.h> // required for atof
29 #include <assert.h>
30 const char* cmTarget::TargetTypeNames[] = {
31 "EXECUTABLE", "STATIC_LIBRARY",
32 "SHARED_LIBRARY", "MODULE_LIBRARY", "UTILITY", "GLOBAL_TARGET",
33 "INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY"
36 //----------------------------------------------------------------------------
37 class cmTargetInternals
39 public:
40 cmTargetInternals()
42 this->SourceFileFlagsConstructed = false;
44 typedef cmTarget::SourceFileFlags SourceFileFlags;
45 std::map<cmSourceFile const*, SourceFileFlags> SourceFlagsMap;
46 bool SourceFileFlagsConstructed;
48 // The backtrace when the target was created.
49 cmListFileBacktrace Backtrace;
52 //----------------------------------------------------------------------------
53 cmTarget::cmTarget()
55 this->Makefile = 0;
56 this->PolicyStatusCMP0003 = cmPolicies::WARN;
57 this->PolicyStatusCMP0004 = cmPolicies::WARN;
58 this->LinkLibrariesAnalyzed = false;
59 this->HaveInstallRule = false;
60 this->DLLPlatform = false;
61 this->IsImportedTarget = false;
64 //----------------------------------------------------------------------------
65 void cmTarget::DefineProperties(cmake *cm)
67 cm->DefineProperty
68 ("BUILD_WITH_INSTALL_RPATH", cmProperty::TARGET,
69 "Should build tree targets have install tree rpaths.",
70 "BUILD_WITH_INSTALL_RPATH is a boolean specifying whether to link "
71 "the target in the build tree with the INSTALL_RPATH. This takes "
72 "precedence over SKIP_BUILD_RPATH and avoids the need for relinking "
73 "before installation.");
75 cm->DefineProperty
76 ("CLEAN_DIRECT_OUTPUT", cmProperty::TARGET,
77 "Do not delete other variants of this target.",
78 "When a library is built CMake by default generates code to remove "
79 "any existing library using all possible names. This is needed "
80 "to support libraries that switch between STATIC and SHARED by "
81 "a user option. However when using OUTPUT_NAME to build a static "
82 "and shared library of the same name using different logical target "
83 "names the two targets will remove each other's files. This can be "
84 "prevented by setting the CLEAN_DIRECT_OUTPUT property to 1.");
86 cm->DefineProperty
87 ("COMPILE_FLAGS", cmProperty::TARGET,
88 "Additional flags to use when compiling this target's sources.",
89 "The COMPILE_FLAGS property sets additional compiler flags used "
90 "to build sources within the target. Use COMPILE_DEFINITIONS "
91 "to pass additional preprocessor definitions.");
93 cm->DefineProperty
94 ("COMPILE_DEFINITIONS", cmProperty::TARGET,
95 "Preprocessor definitions for compiling a target's sources.",
96 "The COMPILE_DEFINITIONS property may be set to a list of preprocessor "
97 "definitions using the syntax VAR or VAR=value. Function-style "
98 "definitions are not supported. CMake will automatically escape "
99 "the value correctly for the native build system (note that CMake "
100 "language syntax may require escapes to specify some values). "
101 "This property may be set on a per-configuration basis using the name "
102 "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
103 "(ex. \"COMPILE_DEFINITIONS_DEBUG\").\n"
104 "CMake will automatically drop some definitions that "
105 "are not supported by the native build tool. "
106 "The VS6 IDE does not support definitions with values "
107 "(but NMake does).\n"
108 "Dislaimer: Most native build tools have poor support for escaping "
109 "certain values. CMake has work-arounds for many cases but some "
110 "values may just not be possible to pass correctly. If a value "
111 "does not seem to be escaped correctly, do not attempt to "
112 "work-around the problem by adding escape sequences to the value. "
113 "Your work-around may break in a future version of CMake that "
114 "has improved escape support. Instead consider defining the macro "
115 "in a (configured) header file. Then report the limitation.");
117 cm->DefineProperty
118 ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::TARGET,
119 "Per-configuration preprocessor definitions on a target.",
120 "This is the configuration-specific version of COMPILE_DEFINITIONS.");
122 cm->DefineProperty
123 ("DEFINE_SYMBOL", cmProperty::TARGET,
124 "Define a symbol when compiling this target's sources.",
125 "DEFINE_SYMBOL sets the name of the preprocessor symbol defined when "
126 "compiling sources in a shared library. "
127 "If not set here then it is set to target_EXPORTS by default "
128 "(with some substitutions if the target is not a valid C "
129 "identifier). This is useful for headers to know whether they are "
130 "being included from inside their library our outside to properly "
131 "setup dllexport/dllimport decorations. ");
133 cm->DefineProperty
134 ("DEBUG_POSTFIX", cmProperty::TARGET,
135 "A postfix that will be applied to this target when build debug.",
136 "A property on a target that specifies a postfix to add to the "
137 "target name when built in debug mode. For example foo.dll "
138 "versus fooD.dll");
140 cm->DefineProperty
141 ("EchoString", cmProperty::TARGET,
142 "A message to be displayed when the target is built.",
143 "A message to display on some generators (such as makefiles) when "
144 "the target is built.");
146 cm->DefineProperty
147 ("FRAMEWORK", cmProperty::TARGET,
148 "This target is a framework on the Mac.",
149 "If a shared library target has this property set to true it will "
150 "be built as a framework when built on the mac. It will have the "
151 "directory structure required for a framework and will be suitable "
152 "to be used with the -framework option");
154 cm->DefineProperty
155 ("HAS_CXX", cmProperty::TARGET,
156 "Force a target to use the CXX linker.",
157 "Setting HAS_CXX on a target will force the target to use the "
158 "C++ linker (and C++ runtime libraries) for linking even if the "
159 "target has no C++ code in it.");
161 cm->DefineProperty
162 ("IMPORT_PREFIX", cmProperty::TARGET,
163 "What comes before the import library name.",
164 "Similar to the target property PREFIX, but used for import libraries "
165 "(typically corresponding to a DLL) instead of regular libraries. "
166 "A target property that can be set to override the prefix "
167 "(such as \"lib\") on an import library name.");
169 cm->DefineProperty
170 ("IMPORT_SUFFIX", cmProperty::TARGET,
171 "What comes after the import library name.",
172 "Similar to the target property SUFFIX, but used for import libraries "
173 "(typically corresponding to a DLL) instead of regular libraries. "
174 "A target property that can be set to override the suffix "
175 "(such as \".lib\") on an import library name.");
177 cm->DefineProperty
178 ("IMPORTED", cmProperty::TARGET,
179 "Read-only indication of whether a target is IMPORTED.",
180 "The boolean value of this property is true for targets created with "
181 "the IMPORTED option to add_executable or add_library. "
182 "It is false for targets built within the project.");
184 cm->DefineProperty
185 ("IMPORTED_CONFIGURATIONS", cmProperty::TARGET,
186 "Configurations provided for an IMPORTED target.",
187 "Lists configuration names available for an IMPORTED target. "
188 "The names correspond to configurations defined in the project from "
189 "which the target is imported. "
190 "If the importing project uses a different set of configurations "
191 "the names may be mapped using the MAP_IMPORTED_CONFIG_<CONFIG> "
192 "property. "
193 "Ignored for non-imported targets.");
195 cm->DefineProperty
196 ("IMPORTED_IMPLIB", cmProperty::TARGET,
197 "Full path to the import library for an IMPORTED target.",
198 "Specifies the location of the \".lib\" part of a windows DLL. "
199 "Ignored for non-imported targets.");
201 cm->DefineProperty
202 ("IMPORTED_IMPLIB_<CONFIG>", cmProperty::TARGET,
203 "Per-configuration version of IMPORTED_IMPLIB property.",
204 "This property is used when loading settings for the <CONFIG> "
205 "configuration of an imported target. "
206 "Configuration names correspond to those provided by the project "
207 "from which the target is imported.");
209 cm->DefineProperty
210 ("IMPORTED_LINK_DEPENDENT_LIBRARIES", cmProperty::TARGET,
211 "Dependent shared libraries of an imported shared library.",
212 "Shared libraries may be linked to other shared libraries as part "
213 "of their implementation. On some platforms the linker searches "
214 "for the dependent libraries of shared libraries they are including "
215 "in the link. This property lists "
216 "the dependent shared libraries of an imported library. The list "
217 "should be disjoint from the list of interface libraries in the "
218 "IMPORTED_LINK_INTERFACE_LIBRARIES property. On platforms requiring "
219 "dependent shared libraries to be found at link time CMake uses this "
220 "list to add appropriate files or paths to the link command line. "
221 "Ignored for non-imported targets.");
223 cm->DefineProperty
224 ("IMPORTED_LINK_DEPENDENT_LIBRARIES_<CONFIG>", cmProperty::TARGET,
225 "Per-configuration version of IMPORTED_LINK_DEPENDENT_LIBRARIES.",
226 "This property is used when loading settings for the <CONFIG> "
227 "configuration of an imported target. "
228 "Configuration names correspond to those provided by the project "
229 "from which the target is imported.");
231 cm->DefineProperty
232 ("IMPORTED_LINK_INTERFACE_LIBRARIES", cmProperty::TARGET,
233 "Transitive link interface of an IMPORTED target.",
234 "Lists libraries whose interface is included when an IMPORTED library "
235 "target is linked to another target. "
236 "The libraries will be included on the link line for the target. "
237 "Ignored for non-imported targets.");
239 cm->DefineProperty
240 ("IMPORTED_LINK_INTERFACE_LIBRARIES_<CONFIG>", cmProperty::TARGET,
241 "Per-configuration version of IMPORTED_LINK_INTERFACE_LIBRARIES.",
242 "This property is used when loading settings for the <CONFIG> "
243 "configuration of an imported target. "
244 "Configuration names correspond to those provided by the project "
245 "from which the target is imported.");
247 cm->DefineProperty
248 ("IMPORTED_LOCATION", cmProperty::TARGET,
249 "Full path to the main file on disk for an IMPORTED target.",
250 "Specifies the location of an IMPORTED target file on disk. "
251 "For executables this is the location of the executable file. "
252 "For bundles on OS X this is the location of the executable file "
253 "inside Contents/MacOS under the application bundle folder. "
254 "For static libraries and modules this is the location of the "
255 "library or module. "
256 "For shared libraries on non-DLL platforms this is the location of "
257 "the shared library. "
258 "For frameworks on OS X this is the location of the library file "
259 "symlink just inside the framework folder. "
260 "For DLLs this is the location of the \".dll\" part of the library. "
261 "Ignored for non-imported targets.");
263 cm->DefineProperty
264 ("IMPORTED_LOCATION_<CONFIG>", cmProperty::TARGET,
265 "Per-configuration version of IMPORTED_LOCATION property.",
266 "This property is used when loading settings for the <CONFIG> "
267 "configuration of an imported target. "
268 "Configuration names correspond to those provided by the project "
269 "from which the target is imported.");
271 cm->DefineProperty
272 ("IMPORTED_SONAME", cmProperty::TARGET,
273 "The \"soname\" of an IMPORTED target of shared library type.",
274 "Specifies the \"soname\" embedded in an imported shared library. "
275 "This is meaningful only on platforms supporting the feature. "
276 "Ignored for non-imported targets.");
278 cm->DefineProperty
279 ("IMPORTED_SONAME_<CONFIG>", cmProperty::TARGET,
280 "Per-configuration version of IMPORTED_SONAME property.",
281 "This property is used when loading settings for the <CONFIG> "
282 "configuration of an imported target. "
283 "Configuration names correspond to those provided by the project "
284 "from which the target is imported.");
286 cm->DefineProperty
287 ("EXCLUDE_FROM_ALL", cmProperty::TARGET,
288 "Exclude the target from the all target.",
289 "A property on a target that indicates if the target is excluded "
290 "from the default build target. If it is not, then with a Makefile "
291 "for example typing make will cause this target to be built. "
292 "The same concept applies to the default build of other generators. "
293 "Installing a target with EXCLUDE_FROM_ALL set to true has "
294 "undefined behavior.");
296 cm->DefineProperty
297 ("INSTALL_NAME_DIR", cmProperty::TARGET,
298 "Mac OSX directory name for installed targets.",
299 "INSTALL_NAME_DIR is a string specifying the "
300 "directory portion of the \"install_name\" field of shared libraries "
301 "on Mac OSX to use in the installed targets. ");
303 cm->DefineProperty
304 ("INSTALL_RPATH", cmProperty::TARGET,
305 "The rpath to use for installed targets.",
306 "A semicolon-separated list specifying the rpath "
307 "to use in installed targets (for platforms that support it).");
309 cm->DefineProperty
310 ("INSTALL_RPATH_USE_LINK_PATH", cmProperty::TARGET,
311 "Add paths to linker search and installed rpath.",
312 "INSTALL_RPATH_USE_LINK_PATH is a boolean that if set to true will "
313 "append directories in the linker search path and outside the "
314 "project to the INSTALL_RPATH. ");
316 cm->DefineProperty
317 ("LINK_FLAGS", cmProperty::TARGET,
318 "Additional flags to use when linking this target.",
319 "The LINK_FLAGS property can be used to add extra flags to the "
320 "link step of a target. LINK_FLAGS_<CONFIG> will add to the "
321 "configuration <CONFIG>, "
322 "for example, DEBUG, RELEASE, MINSIZEREL, RELWITHDEBINFO. ");
324 cm->DefineProperty
325 ("LINK_FLAGS_<CONFIG>", cmProperty::TARGET,
326 "Per-configuration linker flags for a target.",
327 "This is the configuration-specific version of LINK_FLAGS.");
329 cm->DefineProperty
330 ("LINK_SEARCH_END_STATIC", cmProperty::TARGET,
331 "End a link line such that static system libraries are used.",
332 "Some linkers support switches such as -Bstatic and -Bdynamic "
333 "to determine whether to use static or shared libraries for -lXXX "
334 "options. CMake uses these options to set the link type for "
335 "libraries whose full paths are not known or (in some cases) are in "
336 "implicit link directories for the platform. By default the "
337 "linker search type is left at -Bdynamic by the end of the library "
338 "list. This property switches the final linker search type to "
339 "-Bstatic.");
341 cm->DefineProperty
342 ("LINKER_LANGUAGE", cmProperty::TARGET,
343 "What tool to use for linking, based on language.",
344 "The LINKER_LANGUAGE property is used to change the tool "
345 "used to link an executable or shared library. The default is "
346 "set the language to match the files in the library. CXX and C "
347 "are common values for this property.");
349 cm->DefineProperty
350 ("LOCATION", cmProperty::TARGET,
351 "Deprecated. Use LOCATION_<CONFIG> or avoid altogether.",
352 "This property is provided for compatibility with CMake 2.4 and below. "
353 "It was meant to get the location of an executable target's output file "
354 "for use in add_custom_command. "
355 "In CMake 2.6 and above add_custom_command automatically recognizes a "
356 "target name in its COMMAND and DEPENDS options and computes the "
357 "target location. Therefore this property need not be used. "
358 "This property is not defined for IMPORTED targets because they "
359 "were not available in CMake 2.4 or below anyway.");
361 cm->DefineProperty
362 ("LOCATION_<CONFIG>", cmProperty::TARGET,
363 "Read-only property providing a target location on disk.",
364 "A read-only property that indicates where a target's main file is "
365 "located on disk for the configuration <CONFIG>. "
366 "The property is defined only for library and executable targets.");
368 cm->DefineProperty
369 ("LINK_INTERFACE_LIBRARIES", cmProperty::TARGET,
370 "List public interface libraries for a shared library or executable.",
371 "By default linking to a shared library target transitively "
372 "links to targets with which the library itself was linked. "
373 "For an executable with exports (see the ENABLE_EXPORTS property) "
374 "no default transitive link dependencies are used. "
375 "This property replaces the default transitive link dependencies with "
376 "an explict list. "
377 "When the target is linked into another target the libraries "
378 "listed (and recursively their link interface libraries) will be "
379 "provided to the other target also. "
380 "If the list is empty then no transitive link dependencies will be "
381 "incorporated when this target is linked into another target even if "
382 "the default set is non-empty.");
384 cm->DefineProperty
385 ("LINK_INTERFACE_LIBRARIES_<CONFIG>", cmProperty::TARGET,
386 "Per-configuration list of public interface libraries for a target.",
387 "This is the configuration-specific version of "
388 "LINK_INTERFACE_LIBRARIES.");
390 cm->DefineProperty
391 ("MAP_IMPORTED_CONFIG_<CONFIG>", cmProperty::TARGET,
392 "Map from project configuration to IMPORTED target's configuration.",
393 "List configurations of an imported target that may be used for "
394 "the current project's <CONFIG> configuration. "
395 "Targets imported from another project may not provide the same set "
396 "of configuration names available in the current project. "
397 "Setting this property tells CMake what imported configurations are "
398 "suitable for use when building the <CONFIG> configuration. "
399 "The first configuration in the list found to be provided by the "
400 "imported target is selected. If no matching configurations are "
401 "available the imported target is considered to be not found. "
402 "This property is ignored for non-imported targets.",
403 false /* TODO: make this chained */ );
405 cm->DefineProperty
406 ("OUTPUT_NAME", cmProperty::TARGET,
407 "Sets the real name of a target when it is built.",
408 "Sets the real name of a target when it is built and "
409 "can be used to help create two targets of the same name even though "
410 "CMake requires unique logical target names. There is also a "
411 "<CONFIG>_OUTPUT_NAME that can set the output name on a "
412 "per-configuration basis.");
414 cm->DefineProperty
415 ("PRE_INSTALL_SCRIPT", cmProperty::TARGET,
416 "Deprecated install support.",
417 "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
418 "old way to specify CMake scripts to run before and after "
419 "installing a target. They are used only when the old "
420 "INSTALL_TARGETS command is used to install the target. Use the "
421 "INSTALL command instead.");
423 cm->DefineProperty
424 ("PREFIX", cmProperty::TARGET,
425 "What comes before the library name.",
426 "A target property that can be set to override the prefix "
427 "(such as \"lib\") on a library name.");
429 cm->DefineProperty
430 ("POST_INSTALL_SCRIPT", cmProperty::TARGET,
431 "Deprecated install support.",
432 "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
433 "old way to specify CMake scripts to run before and after "
434 "installing a target. They are used only when the old "
435 "INSTALL_TARGETS command is used to install the target. Use the "
436 "INSTALL command instead.");
438 cm->DefineProperty
439 ("PRIVATE_HEADER", cmProperty::TARGET,
440 "Specify private header files in a FRAMEWORK shared library target.",
441 "Shared library targets marked with the FRAMEWORK property generate "
442 "frameworks on OS X and normal shared libraries on other platforms. "
443 "This property may be set to a list of header files to be placed "
444 "in the PrivateHeaders directory inside the framework folder. "
445 "On non-Apple platforms these headers may be installed using the "
446 "PRIVATE_HEADER option to the install(TARGETS) command.");
448 cm->DefineProperty
449 ("PUBLIC_HEADER", cmProperty::TARGET,
450 "Specify public header files in a FRAMEWORK shared library target.",
451 "Shared library targets marked with the FRAMEWORK property generate "
452 "frameworks on OS X and normal shared libraries on other platforms. "
453 "This property may be set to a list of header files to be placed "
454 "in the Headers directory inside the framework folder. "
455 "On non-Apple platforms these headers may be installed using the "
456 "PUBLIC_HEADER option to the install(TARGETS) command.");
458 cm->DefineProperty
459 ("RESOURCE", cmProperty::TARGET,
460 "Specify resource files in a FRAMEWORK shared library target.",
461 "Shared library targets marked with the FRAMEWORK property generate "
462 "frameworks on OS X and normal shared libraries on other platforms. "
463 "This property may be set to a list of files to be placed "
464 "in the Resources directory inside the framework folder. "
465 "On non-Apple platforms these files may be installed using the "
466 "RESOURCE option to the install(TARGETS) command.");
468 cm->DefineProperty
469 ("SKIP_BUILD_RPATH", cmProperty::TARGET,
470 "Should rpaths be used for the build tree.",
471 "SKIP_BUILD_RPATH is a boolean specifying whether to skip automatic "
472 "generation of an rpath allowing the target to run from the "
473 "build tree. ");
475 cm->DefineProperty
476 ("SOVERSION", cmProperty::TARGET,
477 "What version number is this target.",
478 "For shared libraries VERSION and SOVERSION can be used to specify "
479 "the build version and api version respectively. When building or "
480 "installing appropriate symlinks are created if the platform "
481 "supports symlinks and the linker supports so-names. "
482 "If only one of both is specified the missing is assumed to have "
483 "the same version number. "
484 "For shared libraries and executables on Windows the VERSION "
485 "attribute is parsed to extract a \"major.minor\" version number. "
486 "These numbers are used as the image version of the binary. ");
488 cm->DefineProperty
489 ("STATIC_LIBRARY_FLAGS", cmProperty::TARGET,
490 "Extra flags to use when linking static libraries.",
491 "Extra flags to use when linking a static library.");
493 cm->DefineProperty
494 ("SUFFIX", cmProperty::TARGET,
495 "What comes after the library name.",
496 "A target property that can be set to override the suffix "
497 "(such as \".so\") on a library name.");
499 cm->DefineProperty
500 ("VERSION", cmProperty::TARGET,
501 "What version number is this target.",
502 "For shared libraries VERSION and SOVERSION can be used to specify "
503 "the build version and api version respectively. When building or "
504 "installing appropriate symlinks are created if the platform "
505 "supports symlinks and the linker supports so-names. "
506 "If only one of both is specified the missing is assumed to have "
507 "the same version number. "
508 "For executables VERSION can be used to specify the build version. "
509 "When building or installing appropriate symlinks are created if "
510 "the platform supports symlinks. "
511 "For shared libraries and executables on Windows the VERSION "
512 "attribute is parsed to extract a \"major.minor\" version number. "
513 "These numbers are used as the image version of the binary. ");
516 cm->DefineProperty
517 ("WIN32_EXECUTABLE", cmProperty::TARGET,
518 "Build an executable with a WinMain entry point on windows.",
519 "When this property is set to true the executable when linked "
520 "on Windows will be created with a WinMain() entry point instead "
521 "of of just main()."
522 "This makes it a GUI executable instead of a console application. "
523 "See the CMAKE_MFC_FLAG variable documentation to configure use "
524 "of MFC for WinMain executables.");
526 cm->DefineProperty
527 ("MACOSX_BUNDLE", cmProperty::TARGET,
528 "Build an executable as an application bundle on Mac OS X.",
529 "When this property is set to true the executable when built "
530 "on Mac OS X will be created as an application bundle. "
531 "This makes it a GUI executable that can be launched from "
532 "the Finder.\n"
533 "The bundle Info.plist file is generated automatically. "
534 "The following target properties may be set to specify "
535 "its content:"
536 " MACOSX_BUNDLE_INFO_STRING\n"
537 " MACOSX_BUNDLE_ICON_FILE\n"
538 " MACOSX_BUNDLE_GUI_IDENTIFIER\n"
539 " MACOSX_BUNDLE_LONG_VERSION_STRING\n"
540 " MACOSX_BUNDLE_BUNDLE_NAME\n"
541 " MACOSX_BUNDLE_SHORT_VERSION_STRING\n"
542 " MACOSX_BUNDLE_BUNDLE_VERSION\n"
543 " MACOSX_BUNDLE_COPYRIGHT\n"
546 cm->DefineProperty
547 ("ENABLE_EXPORTS", cmProperty::TARGET,
548 "Specify whether an executable exports symbols for loadable modules.",
549 "Normally an executable does not export any symbols because it is "
550 "the final program. It is possible for an executable to export "
551 "symbols to be used by loadable modules. When this property is "
552 "set to true CMake will allow other targets to \"link\" to the "
553 "executable with the TARGET_LINK_LIBRARIES command. "
554 "On all platforms a target-level dependency on the executable is "
555 "created for targets that link to it. "
556 "For non-DLL platforms the link rule is simply ignored since "
557 "the dynamic loader will automatically bind symbols when the "
558 "module is loaded. "
559 "For DLL platforms an import library will be created for the "
560 "exported symbols and then used for linking. "
561 "All Windows-based systems including Cygwin are DLL platforms.");
563 cm->DefineProperty
564 ("Fortran_MODULE_DIRECTORY", cmProperty::TARGET,
565 "Specify output directory for Fortran modules provided by the target.",
566 "If the target contains Fortran source files that provide modules "
567 "and the compiler supports a module output directory this specifies "
568 "the directory in which the modules will be placed. "
569 "When this property is not set the modules will be placed in the "
570 "build directory corresponding to the target's source directory. "
571 "If the variable CMAKE_Fortran_MODULE_DIRECTORY is set when a target "
572 "is created its value is used to initialize this property.");
574 cm->DefineProperty
575 ("XCODE_ATTRIBUTE_<an-attribute>", cmProperty::TARGET,
576 "Set Xcode target attributes directly.",
577 "Tell the Xcode generator to set '<an-attribute>' to a given value "
578 "in the generated Xcode project. Ignored on other generators.");
580 cm->DefineProperty
581 ("GENERATOR_FILE_NAME", cmProperty::TARGET,
582 "Generator's file for this target.",
583 "An internal property used by some generators to record the name of "
584 "project or dsp file associated with this target.");
586 cm->DefineProperty
587 ("SOURCES", cmProperty::TARGET,
588 "Source names specified for a target.",
589 "Read-only list of sources specified for a target. "
590 "The names returned are suitable for passing to the "
591 "set_source_files_properties command.");
593 #if 0
594 cm->DefineProperty
595 ("OBJECT_FILES", cmProperty::TARGET,
596 "Used to get the resulting list of object files that make up a "
597 "target.",
598 "This can be used to put object files from one library "
599 "into another library. It is a read only property. It "
600 "converts the source list for the target into a list of full "
601 "paths to object names that will be produced by the target.");
602 #endif
604 #define CM_TARGET_FILE_TYPES_DOC \
605 "There are three kinds of target files that may be built: " \
606 "archive, library, and runtime. " \
607 "Executables are always treated as runtime targets. " \
608 "Static libraries are always treated as archive targets. " \
609 "Module libraries are always treated as library targets. " \
610 "For non-DLL platforms shared libraries are treated as library " \
611 "targets. " \
612 "For DLL platforms the DLL part of a shared library is treated as " \
613 "a runtime target and the corresponding import library is treated as " \
614 "an archive target. " \
615 "All Windows-based systems including Cygwin are DLL platforms."
617 cm->DefineProperty
618 ("ARCHIVE_OUTPUT_DIRECTORY", cmProperty::TARGET,
619 "Output directory in which to build ARCHIVE target files.",
620 "This property specifies the directory into which archive target files "
621 "should be built. "
622 CM_TARGET_FILE_TYPES_DOC " "
623 "This property is initialized by the value of the variable "
624 "CMAKE_ARCHIVE_OUTPUT_DIRECTORY if it is set when a target is created.");
625 cm->DefineProperty
626 ("LIBRARY_OUTPUT_DIRECTORY", cmProperty::TARGET,
627 "Output directory in which to build LIBRARY target files.",
628 "This property specifies the directory into which library target files "
629 "should be built. "
630 CM_TARGET_FILE_TYPES_DOC " "
631 "This property is initialized by the value of the variable "
632 "CMAKE_LIBRARY_OUTPUT_DIRECTORY if it is set when a target is created.");
633 cm->DefineProperty
634 ("RUNTIME_OUTPUT_DIRECTORY", cmProperty::TARGET,
635 "Output directory in which to build RUNTIME target files.",
636 "This property specifies the directory into which runtime target files "
637 "should be built. "
638 CM_TARGET_FILE_TYPES_DOC " "
639 "This property is initialized by the value of the variable "
640 "CMAKE_RUNTIME_OUTPUT_DIRECTORY if it is set when a target is created.");
642 // define some properties without documentation
643 cm->DefineProperty("DEBUG_OUTPUT_NAME", cmProperty::TARGET,0,0);
644 cm->DefineProperty("RELEASE_OUTPUT_NAME", cmProperty::TARGET,0,0);
647 void cmTarget::SetType(TargetType type, const char* name)
649 this->Name = name;
650 if(type == cmTarget::INSTALL_FILES ||
651 type == cmTarget::INSTALL_PROGRAMS ||
652 type == cmTarget::INSTALL_DIRECTORY)
654 this->Makefile->
655 IssueMessage(cmake::INTERNAL_ERROR,
656 "SetType called on cmTarget for INSTALL_FILES, "
657 "INSTALL_PROGRAMS, or INSTALL_DIRECTORY ");
658 return;
660 // only add dependency information for library targets
661 this->TargetTypeValue = type;
662 if(this->TargetTypeValue >= STATIC_LIBRARY
663 && this->TargetTypeValue <= MODULE_LIBRARY)
665 this->RecordDependencies = true;
667 else
669 this->RecordDependencies = false;
673 //----------------------------------------------------------------------------
674 void cmTarget::SetMakefile(cmMakefile* mf)
676 // Set our makefile.
677 this->Makefile = mf;
679 // set the cmake instance of the properties
680 this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
682 // Check whether this is a DLL platform.
683 this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
684 this->Makefile->IsOn("CYGWIN") ||
685 this->Makefile->IsOn("MINGW"));
687 // Setup default property values.
688 this->SetPropertyDefault("INSTALL_NAME_DIR", "");
689 this->SetPropertyDefault("INSTALL_RPATH", "");
690 this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
691 this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
692 this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
693 this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
694 this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
695 this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
696 this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", 0);
698 // Collect the set of configuration types.
699 std::vector<std::string> configNames;
700 if(const char* configurationTypes =
701 mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
703 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
705 else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
707 if(*buildType)
709 configNames.push_back(buildType);
713 // Setup per-configuration property default values.
714 for(std::vector<std::string>::iterator ci = configNames.begin();
715 ci != configNames.end(); ++ci)
717 // Initialize per-configuration name postfix property from the
718 // variable only for non-executable targets. This preserves
719 // compatibility with previous CMake versions in which executables
720 // did not support this variable. Projects may still specify the
721 // property directly. TODO: Make this depend on backwards
722 // compatibility setting.
723 if(this->TargetTypeValue != cmTarget::EXECUTABLE)
725 std::string property = cmSystemTools::UpperCase(*ci);
726 property += "_POSTFIX";
727 this->SetPropertyDefault(property.c_str(), 0);
731 // Save the backtrace of target construction.
732 this->Makefile->GetBacktrace(this->Internal->Backtrace);
734 // Record current policies for later use.
735 this->PolicyStatusCMP0003 =
736 this->Makefile->GetPolicyStatus(cmPolicies::CMP0003);
737 this->PolicyStatusCMP0004 =
738 this->Makefile->GetPolicyStatus(cmPolicies::CMP0004);
741 //----------------------------------------------------------------------------
742 cmListFileBacktrace const& cmTarget::GetBacktrace() const
744 return this->Internal->Backtrace;
747 //----------------------------------------------------------------------------
748 bool cmTarget::IsExecutableWithExports()
750 return (this->GetType() == cmTarget::EXECUTABLE &&
751 this->GetPropertyAsBool("ENABLE_EXPORTS"));
754 //----------------------------------------------------------------------------
755 bool cmTarget::IsFrameworkOnApple()
757 return (this->GetType() == cmTarget::SHARED_LIBRARY &&
758 this->Makefile->IsOn("APPLE") &&
759 this->GetPropertyAsBool("FRAMEWORK"));
762 //----------------------------------------------------------------------------
763 bool cmTarget::IsAppBundleOnApple()
765 return (this->GetType() == cmTarget::EXECUTABLE &&
766 this->Makefile->IsOn("APPLE") &&
767 this->GetPropertyAsBool("MACOSX_BUNDLE"));
770 //----------------------------------------------------------------------------
771 class cmTargetTraceDependencies
773 public:
774 cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile);
775 void Trace();
776 private:
777 cmTarget* Target;
778 cmMakefile* Makefile;
779 cmGlobalGenerator* GlobalGenerator;
780 std::queue<cmStdString> DependencyQueue;
781 std::set<cmStdString> DependenciesQueued;
782 std::set<cmSourceFile*> TargetSources;
784 void QueueOnce(std::string const& name);
785 void QueueOnce(std::vector<std::string> const& names);
786 void QueueDependencies(cmSourceFile* sf);
787 bool IsUtility(std::string const& dep);
788 void CheckCustomCommand(cmCustomCommand const& cc);
789 void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
792 //----------------------------------------------------------------------------
793 cmTargetTraceDependencies
794 ::cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile):
795 Target(target)
797 // Convenience.
798 this->Makefile = this->Target->GetMakefile();
799 this->GlobalGenerator =
800 this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
802 // Queue all the source files already specified for the target.
803 std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
804 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
805 si != sources.end(); ++si)
807 // Queue the source file itself in case it is generated.
808 this->QueueOnce((*si)->GetFullPath());
810 // Queue the dependencies of the source file in case they are
811 // generated.
812 this->QueueDependencies(*si);
814 // Track the sources already known to the target.
815 this->TargetSources.insert(*si);
818 // Queue the VS project file to check dependencies on the rule to
819 // generate it.
820 if(vsProjectFile)
822 this->QueueOnce(vsProjectFile);
825 // Queue pre-build, pre-link, and post-build rule dependencies.
826 this->CheckCustomCommands(this->Target->GetPreBuildCommands());
827 this->CheckCustomCommands(this->Target->GetPreLinkCommands());
828 this->CheckCustomCommands(this->Target->GetPostBuildCommands());
831 //----------------------------------------------------------------------------
832 void cmTargetTraceDependencies::Trace()
834 // Process one dependency at a time until the queue is empty.
835 while(!this->DependencyQueue.empty())
837 // Get the next dependency in from queue.
838 std::string dep = this->DependencyQueue.front();
839 this->DependencyQueue.pop();
841 // Check if we know how to generate this dependency.
842 if(cmSourceFile* sf =
843 this->Makefile->GetSourceFileWithOutput(dep.c_str()))
845 // Queue dependencies needed to generate this file.
846 this->QueueDependencies(sf);
848 // Make sure this file is in the target.
849 if(this->TargetSources.insert(sf).second)
851 this->Target->AddSourceFile(sf);
857 //----------------------------------------------------------------------------
858 void cmTargetTraceDependencies::QueueOnce(std::string const& name)
860 if(this->DependenciesQueued.insert(name).second)
862 this->DependencyQueue.push(name);
866 //----------------------------------------------------------------------------
867 void
868 cmTargetTraceDependencies::QueueOnce(std::vector<std::string> const& names)
870 for(std::vector<std::string>::const_iterator i = names.begin();
871 i != names.end(); ++i)
873 this->QueueOnce(*i);
877 //----------------------------------------------------------------------------
878 bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
880 // Dependencies on targets (utilities) are supposed to be named by
881 // just the target name. However for compatibility we support
882 // naming the output file generated by the target (assuming there is
883 // no output-name property which old code would not have set). In
884 // that case the target name will be the file basename of the
885 // dependency.
886 std::string util = cmSystemTools::GetFilenameName(dep);
887 if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
889 util = cmSystemTools::GetFilenameWithoutLastExtension(util);
892 // Check for a non-imported target with this name.
893 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, util.c_str()))
895 // If we find the target and the dep was given as a full path,
896 // then make sure it was not a full path to something else, and
897 // the fact that the name matched a target was just a coincidence.
898 if(cmSystemTools::FileIsFullPath(dep.c_str()))
900 // This is really only for compatibility so we do not need to
901 // worry about configuration names and output names.
902 std::string tLocation = t->GetLocation(0);
903 tLocation = cmSystemTools::GetFilenamePath(tLocation);
904 std::string depLocation = cmSystemTools::GetFilenamePath(dep);
905 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
906 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
907 if(depLocation == tLocation)
909 this->Target->AddUtility(util.c_str());
910 return true;
913 else
915 // The original name of the dependency was not a full path. It
916 // must name a target, so add the target-level dependency.
917 this->Target->AddUtility(util.c_str());
918 return true;
922 // The dependency does not name a target built in this project.
923 return false;
926 //----------------------------------------------------------------------------
927 void cmTargetTraceDependencies::QueueDependencies(cmSourceFile* sf)
929 // Queue dependency added explicitly by the user.
930 if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
932 std::vector<std::string> objDeps;
933 cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
934 this->QueueOnce(objDeps);
937 // Queue dependencies added programatically by commands.
938 this->QueueOnce(sf->GetDepends());
940 // Queue custom command dependencies.
941 if(cmCustomCommand const* cc = sf->GetCustomCommand())
943 this->CheckCustomCommand(*cc);
948 //----------------------------------------------------------------------------
949 void
950 cmTargetTraceDependencies
951 ::CheckCustomCommand(cmCustomCommand const& cc)
953 // Transform command names that reference targets built in this
954 // project to corresponding target-level dependencies.
955 for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
956 cit != cc.GetCommandLines().end(); ++cit)
958 std::string const& command = *cit->begin();
959 // Look for a non-imported target with this name.
960 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, command.c_str()))
962 if(t->GetType() == cmTarget::EXECUTABLE)
964 // The command refers to an executable target built in
965 // this project. Add the target-level dependency to make
966 // sure the executable is up to date before this custom
967 // command possibly runs.
968 this->Target->AddUtility(command.c_str());
973 // Queue the custom command dependencies.
974 std::vector<std::string> const& depends = cc.GetDepends();
975 for(std::vector<std::string>::const_iterator di = depends.begin();
976 di != depends.end(); ++di)
978 std::string const& dep = *di;
979 if(!this->IsUtility(dep))
981 // The dependency does not name a target and may be a file we
982 // know how to generate. Queue it.
983 this->QueueOnce(dep);
988 //----------------------------------------------------------------------------
989 void
990 cmTargetTraceDependencies
991 ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
993 for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
994 cli != commands.end(); ++cli)
996 this->CheckCustomCommand(*cli);
1000 //----------------------------------------------------------------------------
1001 void cmTarget::TraceDependencies(const char* vsProjectFile)
1003 // Use a helper object to trace the dependencies.
1004 cmTargetTraceDependencies tracer(this, vsProjectFile);
1005 tracer.Trace();
1008 //----------------------------------------------------------------------------
1009 void cmTarget::AddSources(std::vector<std::string> const& srcs)
1011 for(std::vector<std::string>::const_iterator i = srcs.begin();
1012 i != srcs.end(); ++i)
1014 this->AddSource(i->c_str());
1018 //----------------------------------------------------------------------------
1019 cmSourceFile* cmTarget::AddSource(const char* s)
1021 std::string src = s;
1023 // For backwards compatibility replace varibles in source names.
1024 // This should eventually be removed.
1025 this->Makefile->ExpandVariablesInString(src);
1027 cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
1028 this->AddSourceFile(sf);
1029 return sf;
1032 //----------------------------------------------------------------------------
1033 struct cmTarget::SourceFileFlags
1034 cmTarget::GetTargetSourceFileFlags(const cmSourceFile* sf)
1036 struct SourceFileFlags flags;
1037 this->ConstructSourceFileFlags();
1038 std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
1039 this->Internal->SourceFlagsMap.find(sf);
1040 if(si != this->Internal->SourceFlagsMap.end())
1042 flags = si->second;
1044 return flags;
1047 //----------------------------------------------------------------------------
1048 void cmTarget::ConstructSourceFileFlags()
1050 if(this->Internal->SourceFileFlagsConstructed)
1052 return;
1054 this->Internal->SourceFileFlagsConstructed = true;
1056 // Process public headers to mark the source files.
1057 if(const char* files = this->GetProperty("PUBLIC_HEADER"))
1059 std::vector<std::string> relFiles;
1060 cmSystemTools::ExpandListArgument(files, relFiles);
1061 for(std::vector<std::string>::iterator it = relFiles.begin();
1062 it != relFiles.end(); ++it)
1064 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1066 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1067 flags.MacFolder = "Headers";
1068 flags.Type = cmTarget::SourceFileTypePublicHeader;
1073 // Process private headers after public headers so that they take
1074 // precedence if a file is listed in both.
1075 if(const char* files = this->GetProperty("PRIVATE_HEADER"))
1077 std::vector<std::string> relFiles;
1078 cmSystemTools::ExpandListArgument(files, relFiles);
1079 for(std::vector<std::string>::iterator it = relFiles.begin();
1080 it != relFiles.end(); ++it)
1082 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1084 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1085 flags.MacFolder = "PrivateHeaders";
1086 flags.Type = cmTarget::SourceFileTypePrivateHeader;
1091 // Mark sources listed as resources.
1092 if(const char* files = this->GetProperty("RESOURCE"))
1094 std::vector<std::string> relFiles;
1095 cmSystemTools::ExpandListArgument(files, relFiles);
1096 for(std::vector<std::string>::iterator it = relFiles.begin();
1097 it != relFiles.end(); ++it)
1099 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1101 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1102 flags.MacFolder = "Resources";
1103 flags.Type = cmTarget::SourceFileTypeResource;
1108 // Handle the MACOSX_PACKAGE_LOCATION property on source files that
1109 // were not listed in one of the other lists.
1110 std::vector<cmSourceFile*> const& sources = this->GetSourceFiles();
1111 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
1112 si != sources.end(); ++si)
1114 cmSourceFile* sf = *si;
1115 if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
1117 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1118 if(flags.Type == cmTarget::SourceFileTypeNormal)
1120 flags.MacFolder = location;
1121 if(strcmp(location, "Resources") == 0)
1123 flags.Type = cmTarget::SourceFileTypeResource;
1125 else
1127 flags.Type = cmTarget::SourceFileTypeMacContent;
1134 //----------------------------------------------------------------------------
1135 void cmTarget::MergeLinkLibraries( cmMakefile& mf,
1136 const char *selfname,
1137 const LinkLibraryVectorType& libs )
1139 // Only add on libraries we haven't added on before.
1140 // Assumption: the global link libraries could only grow, never shrink
1141 LinkLibraryVectorType::const_iterator i = libs.begin();
1142 i += this->PrevLinkedLibraries.size();
1143 for( ; i != libs.end(); ++i )
1145 // We call this so that the dependencies get written to the cache
1146 this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
1148 this->PrevLinkedLibraries = libs;
1151 //----------------------------------------------------------------------------
1152 void cmTarget::AddLinkDirectory(const char* d)
1154 // Make sure we don't add unnecessary search directories.
1155 if(this->LinkDirectoriesEmmitted.insert(d).second)
1157 this->LinkDirectories.push_back(d);
1161 //----------------------------------------------------------------------------
1162 const std::vector<std::string>& cmTarget::GetLinkDirectories()
1164 return this->LinkDirectories;
1167 //----------------------------------------------------------------------------
1168 void cmTarget::ClearDependencyInformation( cmMakefile& mf,
1169 const char* target )
1171 // Clear the dependencies. The cache variable must exist iff we are
1172 // recording dependency information for this target.
1173 std::string depname = target;
1174 depname += "_LIB_DEPENDS";
1175 if (this->RecordDependencies)
1177 mf.AddCacheDefinition(depname.c_str(), "",
1178 "Dependencies for target", cmCacheManager::STATIC);
1180 else
1182 if (mf.GetDefinition( depname.c_str() ))
1184 std::string message = "Target ";
1185 message += target;
1186 message += " has dependency information when it shouldn't.\n";
1187 message += "Your cache is probably stale. Please remove the entry\n ";
1188 message += depname;
1189 message += "\nfrom the cache.";
1190 cmSystemTools::Error( message.c_str() );
1195 //----------------------------------------------------------------------------
1196 void cmTarget::AddLinkLibrary(const std::string& lib,
1197 LinkLibraryType llt)
1199 this->AddFramework(lib.c_str(), llt);
1200 cmTarget::LibraryID tmp;
1201 tmp.first = lib;
1202 tmp.second = llt;
1203 this->LinkLibraries.push_back(tmp);
1204 this->OriginalLinkLibraries.push_back(tmp);
1207 //----------------------------------------------------------------------------
1208 bool cmTarget::NameResolvesToFramework(const std::string& libname)
1210 return this->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
1211 NameResolvesToFramework(libname);
1214 //----------------------------------------------------------------------------
1215 bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
1217 (void)llt; // TODO: What is this?
1218 if(this->NameResolvesToFramework(libname.c_str()))
1220 std::string frameworkDir = libname;
1221 frameworkDir += "/../";
1222 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1223 std::vector<std::string>::iterator i =
1224 std::find(this->Frameworks.begin(),
1225 this->Frameworks.end(), frameworkDir);
1226 if(i == this->Frameworks.end())
1228 this->Frameworks.push_back(frameworkDir);
1230 return true;
1232 return false;
1235 //----------------------------------------------------------------------------
1236 void cmTarget::AddLinkLibrary(cmMakefile& mf,
1237 const char *target, const char* lib,
1238 LinkLibraryType llt)
1240 // Never add a self dependency, even if the user asks for it.
1241 if(strcmp( target, lib ) == 0)
1243 return;
1245 this->AddFramework(lib, llt);
1246 cmTarget::LibraryID tmp;
1247 tmp.first = lib;
1248 tmp.second = llt;
1249 this->LinkLibraries.push_back( tmp );
1250 this->OriginalLinkLibraries.push_back(tmp);
1252 // Add the explicit dependency information for this target. This is
1253 // simply a set of libraries separated by ";". There should always
1254 // be a trailing ";". These library names are not canonical, in that
1255 // they may be "-framework x", "-ly", "/path/libz.a", etc.
1256 // We shouldn't remove duplicates here because external libraries
1257 // may be purposefully duplicated to handle recursive dependencies,
1258 // and we removing one instance will break the link line. Duplicates
1259 // will be appropriately eliminated at emit time.
1260 if(this->RecordDependencies)
1262 std::string targetEntry = target;
1263 targetEntry += "_LIB_DEPENDS";
1264 std::string dependencies;
1265 const char* old_val = mf.GetDefinition( targetEntry.c_str() );
1266 if( old_val )
1268 dependencies += old_val;
1270 switch (llt)
1272 case cmTarget::GENERAL:
1273 dependencies += "general";
1274 break;
1275 case cmTarget::DEBUG:
1276 dependencies += "debug";
1277 break;
1278 case cmTarget::OPTIMIZED:
1279 dependencies += "optimized";
1280 break;
1282 dependencies += ";";
1283 dependencies += lib;
1284 dependencies += ";";
1285 mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
1286 "Dependencies for the target",
1287 cmCacheManager::STATIC );
1292 //----------------------------------------------------------------------------
1293 void
1294 cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
1296 // There are two key parts of the dependency analysis: (1)
1297 // determining the libraries in the link line, and (2) constructing
1298 // the dependency graph for those libraries.
1300 // The latter is done using the cache entries that record the
1301 // dependencies of each library.
1303 // The former is a more thorny issue, since it is not clear how to
1304 // determine if two libraries listed on the link line refer to the a
1305 // single library or not. For example, consider the link "libraries"
1306 // /usr/lib/libtiff.so -ltiff
1307 // Is this one library or two? The solution implemented here is the
1308 // simplest (and probably the only practical) one: two libraries are
1309 // the same if their "link strings" are identical. Thus, the two
1310 // libraries above are considered distinct. This also means that for
1311 // dependency analysis to be effective, the CMake user must specify
1312 // libraries build by his project without using any linker flags or
1313 // file extensions. That is,
1314 // LINK_LIBRARIES( One Two )
1315 // instead of
1316 // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
1317 // The former is probably what most users would do, but it never
1318 // hurts to document the assumptions. :-) Therefore, in the analysis
1319 // code, the "canonical name" of a library is simply its name as
1320 // given to a LINK_LIBRARIES command.
1322 // Also, we will leave the original link line intact; we will just add any
1323 // dependencies that were missing.
1325 // There is a problem with recursive external libraries
1326 // (i.e. libraries with no dependency information that are
1327 // recursively dependent). We must make sure that the we emit one of
1328 // the libraries twice to satisfy the recursion, but we shouldn't
1329 // emit it more times than necessary. In particular, we must make
1330 // sure that handling this improbable case doesn't cost us when
1331 // dealing with the common case of non-recursive libraries. The
1332 // solution is to assume that the recursion is satisfied at one node
1333 // of the dependency tree. To illustrate, assume libA and libB are
1334 // extrenal and mutually dependent. Suppose libX depends on
1335 // libA, and libY on libA and libX. Then
1336 // TARGET_LINK_LIBRARIES( Y X A B A )
1337 // TARGET_LINK_LIBRARIES( X A B A )
1338 // TARGET_LINK_LIBRARIES( Exec Y )
1339 // would result in "-lY -lX -lA -lB -lA". This is the correct way to
1340 // specify the dependencies, since the mutual dependency of A and B
1341 // is resolved *every time libA is specified*.
1343 // Something like
1344 // TARGET_LINK_LIBRARIES( Y X A B A )
1345 // TARGET_LINK_LIBRARIES( X A B )
1346 // TARGET_LINK_LIBRARIES( Exec Y )
1347 // would result in "-lY -lX -lA -lB", and the mutual dependency
1348 // information is lost. This is because in some case (Y), the mutual
1349 // dependency of A and B is listed, while in another other case (X),
1350 // it is not. Depending on which line actually emits A, the mutual
1351 // dependency may or may not be on the final link line. We can't
1352 // handle this pathalogical case cleanly without emitting extra
1353 // libraries for the normal cases. Besides, the dependency
1354 // information for X is wrong anyway: if we build an executable
1355 // depending on X alone, we would not have the mutual dependency on
1356 // A and B resolved.
1358 // IMPROVEMENTS:
1359 // -- The current algorithm will not always pick the "optimal" link line
1360 // when recursive dependencies are present. It will instead break the
1361 // cycles at an aribtrary point. The majority of projects won't have
1362 // cyclic dependencies, so this is probably not a big deal. Note that
1363 // the link line is always correct, just not necessary optimal.
1366 // Expand variables in link library names. This is for backwards
1367 // compatibility with very early CMake versions and should
1368 // eventually be removed. This code was moved here from the end of
1369 // old source list processing code which was called just before this
1370 // method.
1371 for(LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
1372 p != this->LinkLibraries.end(); ++p)
1374 this->Makefile->ExpandVariablesInString(p->first, true, true);
1378 typedef std::vector< std::string > LinkLine;
1380 // The dependency map.
1381 DependencyMap dep_map;
1383 // 1. Build the dependency graph
1385 for(LinkLibraryVectorType::reverse_iterator lib
1386 = this->LinkLibraries.rbegin();
1387 lib != this->LinkLibraries.rend(); ++lib)
1389 this->GatherDependencies( mf, *lib, dep_map);
1392 // 2. Remove any dependencies that are already satisfied in the original
1393 // link line.
1395 for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
1396 lib != this->LinkLibraries.end(); ++lib)
1398 for( LinkLibraryVectorType::iterator lib2 = lib;
1399 lib2 != this->LinkLibraries.end(); ++lib2)
1401 this->DeleteDependency( dep_map, *lib, *lib2);
1406 // 3. Create the new link line by simply emitting any dependencies that are
1407 // missing. Start from the back and keep adding.
1409 std::set<DependencyMap::key_type> done, visited;
1410 std::vector<DependencyMap::key_type> newLinkLibraries;
1411 for(LinkLibraryVectorType::reverse_iterator lib =
1412 this->LinkLibraries.rbegin();
1413 lib != this->LinkLibraries.rend(); ++lib)
1415 // skip zero size library entries, this may happen
1416 // if a variable expands to nothing.
1417 if (lib->first.size() != 0)
1419 this->Emit( *lib, dep_map, done, visited, newLinkLibraries );
1423 // 4. Add the new libraries to the link line.
1425 for( std::vector<DependencyMap::key_type>::reverse_iterator k =
1426 newLinkLibraries.rbegin();
1427 k != newLinkLibraries.rend(); ++k )
1429 // get the llt from the dep_map
1430 this->LinkLibraries.push_back( std::make_pair(k->first,k->second) );
1432 this->LinkLibrariesAnalyzed = true;
1435 //----------------------------------------------------------------------------
1436 void cmTarget::InsertDependency( DependencyMap& depMap,
1437 const LibraryID& lib,
1438 const LibraryID& dep)
1440 depMap[lib].push_back(dep);
1443 //----------------------------------------------------------------------------
1444 void cmTarget::DeleteDependency( DependencyMap& depMap,
1445 const LibraryID& lib,
1446 const LibraryID& dep)
1448 // Make sure there is an entry in the map for lib. If so, delete all
1449 // dependencies to dep. There may be repeated entries because of
1450 // external libraries that are specified multiple times.
1451 DependencyMap::iterator map_itr = depMap.find( lib );
1452 if( map_itr != depMap.end() )
1454 DependencyList& depList = map_itr->second;
1455 DependencyList::iterator itr;
1456 while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
1457 depList.end() )
1459 depList.erase( itr );
1464 //----------------------------------------------------------------------------
1465 void cmTarget::Emit(const LibraryID lib,
1466 const DependencyMap& dep_map,
1467 std::set<LibraryID>& emitted,
1468 std::set<LibraryID>& visited,
1469 DependencyList& link_line )
1471 // It's already been emitted
1472 if( emitted.find(lib) != emitted.end() )
1474 return;
1477 // Emit the dependencies only if this library node hasn't been
1478 // visited before. If it has, then we have a cycle. The recursion
1479 // that got us here should take care of everything.
1481 if( visited.insert(lib).second )
1483 if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
1485 const DependencyList& dep_on = dep_map.find( lib )->second;
1486 DependencyList::const_reverse_iterator i;
1488 // To cater for recursive external libraries, we must emit
1489 // duplicates on this link line *unless* they were emitted by
1490 // some other node, in which case we assume that the recursion
1491 // was resolved then. We making the simplifying assumption that
1492 // any duplicates on a single link line are on purpose, and must
1493 // be preserved.
1495 // This variable will keep track of the libraries that were
1496 // emitted directory from the current node, and not from a
1497 // recursive call. This way, if we come across a library that
1498 // has already been emitted, we repeat it iff it has been
1499 // emitted here.
1500 std::set<DependencyMap::key_type> emitted_here;
1501 for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
1503 if( emitted_here.find(*i) != emitted_here.end() )
1505 // a repeat. Must emit.
1506 emitted.insert(*i);
1507 link_line.push_back( *i );
1509 else
1511 // Emit only if no-one else has
1512 if( emitted.find(*i) == emitted.end() )
1514 // emit dependencies
1515 Emit( *i, dep_map, emitted, visited, link_line );
1516 // emit self
1517 emitted.insert(*i);
1518 emitted_here.insert(*i);
1519 link_line.push_back( *i );
1527 //----------------------------------------------------------------------------
1528 void cmTarget::GatherDependencies( const cmMakefile& mf,
1529 const LibraryID& lib,
1530 DependencyMap& dep_map)
1532 // If the library is already in the dependency map, then it has
1533 // already been fully processed.
1534 if( dep_map.find(lib) != dep_map.end() )
1536 return;
1539 const char* deps = mf.GetDefinition( (lib.first+"_LIB_DEPENDS").c_str() );
1540 if( deps && strcmp(deps,"") != 0 )
1542 // Make sure this library is in the map, even if it has an empty
1543 // set of dependencies. This distinguishes the case of explicitly
1544 // no dependencies with that of unspecified dependencies.
1545 dep_map[lib];
1547 // Parse the dependency information, which is a set of
1548 // type, library pairs separated by ";". There is always a trailing ";".
1549 cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
1550 std::string depline = deps;
1551 std::string::size_type start = 0;
1552 std::string::size_type end;
1553 end = depline.find( ";", start );
1554 while( end != std::string::npos )
1556 std::string l = depline.substr( start, end-start );
1557 if( l.size() != 0 )
1559 if (l == "debug")
1561 llt = cmTarget::DEBUG;
1563 else if (l == "optimized")
1565 llt = cmTarget::OPTIMIZED;
1567 else if (l == "general")
1569 llt = cmTarget::GENERAL;
1571 else
1573 LibraryID lib2(l,llt);
1574 this->InsertDependency( dep_map, lib, lib2);
1575 this->GatherDependencies( mf, lib2, dep_map);
1576 llt = cmTarget::GENERAL;
1579 start = end+1; // skip the ;
1580 end = depline.find( ";", start );
1582 // cannot depend on itself
1583 this->DeleteDependency( dep_map, lib, lib);
1587 //----------------------------------------------------------------------------
1588 void cmTarget::SetProperty(const char* prop, const char* value)
1590 if (!prop)
1592 return;
1595 this->Properties.SetProperty(prop, value, cmProperty::TARGET);
1597 // If imported information is being set, wipe out cached
1598 // information.
1599 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1601 this->ImportInfoMap.clear();
1605 //----------------------------------------------------------------------------
1606 void cmTarget::AppendProperty(const char* prop, const char* value)
1608 if (!prop)
1610 return;
1612 this->Properties.AppendProperty(prop, value, cmProperty::TARGET);
1614 // If imported information is being set, wipe out cached
1615 // information.
1616 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1618 this->ImportInfoMap.clear();
1622 //----------------------------------------------------------------------------
1623 void cmTarget::MarkAsImported()
1625 this->IsImportedTarget = true;
1628 //----------------------------------------------------------------------------
1629 const char* cmTarget::GetDirectory(const char* config, bool implib)
1631 if (this->IsImported())
1633 return this->ImportedGetDirectory(config, implib);
1635 else
1637 return this->NormalGetDirectory(config, implib);
1641 //----------------------------------------------------------------------------
1642 const char* cmTarget::ImportedGetDirectory(const char* config, bool implib)
1644 this->Directory =
1645 cmSystemTools::GetFilenamePath(
1646 this->ImportedGetFullPath(config, implib));
1647 return this->Directory.c_str();
1650 //----------------------------------------------------------------------------
1651 const char* cmTarget::NormalGetDirectory(const char* config, bool implib)
1653 if(config && *config)
1655 // Do not create the directory when config is given:
1656 this->Directory = this->GetAndCreateOutputDir(implib, true);
1657 // Add the configuration's subdirectory.
1658 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
1659 AppendDirectoryForConfig("/", config, "", this->Directory);
1660 return this->Directory.c_str();
1662 else
1664 return this->GetOutputDir(implib);
1668 //----------------------------------------------------------------------------
1669 const char* cmTarget::GetLocation(const char* config)
1671 if (this->IsImported())
1673 return this->ImportedGetLocation(config);
1675 else
1677 return this->NormalGetLocation(config);
1681 //----------------------------------------------------------------------------
1682 const char* cmTarget::ImportedGetLocation(const char* config)
1684 this->Location = this->ImportedGetFullPath(config, false);
1685 return this->Location.c_str();
1688 //----------------------------------------------------------------------------
1689 const char* cmTarget::NormalGetLocation(const char* config)
1691 this->Location = this->GetDirectory(config);
1692 if(!this->Location.empty())
1694 this->Location += "/";
1696 if(!config)
1698 // No specific configuration was given so it will not appear on
1699 // the result of GetDirectory. Add a name here to be replaced at
1700 // build time.
1701 const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
1702 if(cfgid && strcmp(cfgid, ".") != 0)
1704 this->Location += cfgid;
1705 this->Location += "/";
1708 this->Location += this->GetFullName(config, false);
1709 return this->Location.c_str();
1712 //----------------------------------------------------------------------------
1713 void cmTarget::GetTargetVersion(int& major, int& minor)
1715 // Set the default values.
1716 major = 0;
1717 minor = 0;
1719 // Look for a VERSION property.
1720 if(const char* version = this->GetProperty("VERSION"))
1722 // Try to parse the version number and store the results that were
1723 // successfully parsed.
1724 int parsed_major;
1725 int parsed_minor;
1726 switch(sscanf(version, "%d.%d", &parsed_major, &parsed_minor))
1728 case 2: minor = parsed_minor; // no break!
1729 case 1: major = parsed_major; // no break!
1730 default: break;
1735 //----------------------------------------------------------------------------
1736 const char *cmTarget::GetProperty(const char* prop)
1738 return this->GetProperty(prop, cmProperty::TARGET);
1741 //----------------------------------------------------------------------------
1742 void cmTarget::ComputeObjectFiles()
1744 if (this->IsImported())
1746 return;
1748 #if 0
1749 std::vector<std::string> dirs;
1750 this->Makefile->GetLocalGenerator()->
1751 GetTargetObjectFileDirectories(this,
1752 dirs);
1753 std::string objectFiles;
1754 std::string objExtensionLookup1 = "CMAKE_";
1755 std::string objExtensionLookup2 = "_OUTPUT_EXTENSION";
1757 for(std::vector<std::string>::iterator d = dirs.begin();
1758 d != dirs.end(); ++d)
1760 for(std::vector<cmSourceFile*>::iterator s = this->SourceFiles.begin();
1761 s != this->SourceFiles.end(); ++s)
1763 cmSourceFile* sf = *s;
1764 if(const char* lang = sf->GetLanguage())
1766 std::string lookupObj = objExtensionLookup1 + lang;
1767 lookupObj += objExtensionLookup2;
1768 const char* obj = this->Makefile->GetDefinition(lookupObj.c_str());
1769 if(obj)
1771 if(objectFiles.size())
1773 objectFiles += ";";
1775 std::string objFile = *d;
1776 objFile += "/";
1777 objFile += this->Makefile->GetLocalGenerator()->
1778 GetSourceObjectName(*sf);
1779 objFile += obj;
1780 objectFiles += objFile;
1785 this->SetProperty("OBJECT_FILES", objectFiles.c_str());
1786 #endif
1789 //----------------------------------------------------------------------------
1790 const char *cmTarget::GetProperty(const char* prop,
1791 cmProperty::ScopeType scope)
1793 if(!prop)
1795 return 0;
1798 // Watch for special "computed" properties that are dependent on
1799 // other properties or variables. Always recompute them.
1800 if(this->GetType() == cmTarget::EXECUTABLE ||
1801 this->GetType() == cmTarget::STATIC_LIBRARY ||
1802 this->GetType() == cmTarget::SHARED_LIBRARY ||
1803 this->GetType() == cmTarget::MODULE_LIBRARY)
1805 if(!this->IsImported() && strcmp(prop,"LOCATION") == 0)
1807 // Set the LOCATION property of the target. Note that this
1808 // cannot take into account the per-configuration name of the
1809 // target because the configuration type may not be known at
1810 // CMake time. It is now deprecated as described in the
1811 // documentation.
1812 this->SetProperty("LOCATION", this->GetLocation(0));
1815 // Support "LOCATION_<CONFIG>".
1816 if(strncmp(prop, "LOCATION_", 9) == 0)
1818 std::string configName = prop+9;
1819 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1821 else
1823 // Support "<CONFIG>_LOCATION" for compatiblity.
1824 int len = static_cast<int>(strlen(prop));
1825 if(len > 9 && strcmp(prop+len-9, "_LOCATION") == 0)
1827 std::string configName(prop, len-9);
1828 if(configName != "IMPORTED")
1830 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1836 if (strcmp(prop,"IMPORTED") == 0)
1838 return this->IsImported()?"TRUE":"FALSE";
1841 if(!strcmp(prop,"SOURCES"))
1843 cmOStringStream ss;
1844 const char* sep = "";
1845 for(std::vector<cmSourceFile*>::const_iterator
1846 i = this->SourceFiles.begin();
1847 i != this->SourceFiles.end(); ++i)
1849 // Separate from the previous list entries.
1850 ss << sep;
1851 sep = ";";
1853 // Construct what is known about this source file location.
1854 cmSourceFileLocation const& location = (*i)->GetLocation();
1855 std::string sname = location.GetDirectory();
1856 if(!sname.empty())
1858 sname += "/";
1860 sname += location.GetName();
1862 // Append this list entry.
1863 ss << sname;
1865 this->SetProperty("SOURCES", ss.str().c_str());
1868 // the type property returns what type the target is
1869 if (!strcmp(prop,"TYPE"))
1871 switch( this->GetType() )
1873 case cmTarget::STATIC_LIBRARY:
1874 return "STATIC_LIBRARY";
1875 // break; /* unreachable */
1876 case cmTarget::MODULE_LIBRARY:
1877 return "MODULE_LIBRARY";
1878 // break; /* unreachable */
1879 case cmTarget::SHARED_LIBRARY:
1880 return "SHARED_LIBRARY";
1881 // break; /* unreachable */
1882 case cmTarget::EXECUTABLE:
1883 return "EXECUTABLE";
1884 // break; /* unreachable */
1885 case cmTarget::UTILITY:
1886 return "UTILITY";
1887 // break; /* unreachable */
1888 case cmTarget::GLOBAL_TARGET:
1889 return "GLOBAL_TARGET";
1890 // break; /* unreachable */
1891 case cmTarget::INSTALL_FILES:
1892 return "INSTALL_FILES";
1893 // break; /* unreachable */
1894 case cmTarget::INSTALL_PROGRAMS:
1895 return "INSTALL_PROGRAMS";
1896 // break; /* unreachable */
1897 case cmTarget::INSTALL_DIRECTORY:
1898 return "INSTALL_DIRECTORY";
1899 // break; /* unreachable */
1901 return 0;
1903 bool chain = false;
1904 const char *retVal =
1905 this->Properties.GetPropertyValue(prop, scope, chain);
1906 if (chain)
1908 return this->Makefile->GetProperty(prop,scope);
1910 return retVal;
1913 //----------------------------------------------------------------------------
1914 bool cmTarget::GetPropertyAsBool(const char* prop)
1916 return cmSystemTools::IsOn(this->GetProperty(prop));
1919 //----------------------------------------------------------------------------
1920 const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
1922 if(this->GetProperty("HAS_CXX"))
1924 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
1926 const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
1927 if (linkerLang==0)
1929 // if the property has not yet been set, collect all languages in the
1930 // target and then find the language with the highest preference value
1931 std::set<cmStdString> languages;
1932 this->GetLanguages(languages);
1934 std::string linkerLangList; // only used for the error message
1935 int maxLinkerPref = 0;
1936 bool multiplePreferedLanguages = false;
1937 for(std::set<cmStdString>::const_iterator sit = languages.begin();
1938 sit != languages.end(); ++sit)
1940 int linkerPref = gg->GetLinkerPreference(sit->c_str());
1941 if ((linkerPref > maxLinkerPref) || (linkerLang==0))
1943 maxLinkerPref = linkerPref;
1944 linkerLang = sit->c_str();
1945 linkerLangList = *sit;
1946 multiplePreferedLanguages = false;
1948 else if (linkerPref == maxLinkerPref)
1950 linkerLangList += "; ";
1951 linkerLangList += *sit;
1952 multiplePreferedLanguages = true;
1956 if (linkerLang!=0)
1958 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", linkerLang);
1960 if (multiplePreferedLanguages)
1962 cmOStringStream err;
1963 err << "Error: Target " << this->Name << " contains multiple languages "
1964 << "with the highest linker preference (" << maxLinkerPref << "): "
1965 << linkerLangList << "\n"
1966 << "You must set the LINKER_LANGUAGE property for this target.";
1967 cmSystemTools::Error(err.str().c_str());
1970 return this->GetProperty("LINKER_LANGUAGE");
1973 //----------------------------------------------------------------------------
1974 const char* cmTarget::GetCreateRuleVariable()
1976 switch(this->GetType())
1978 case cmTarget::STATIC_LIBRARY:
1979 return "_CREATE_STATIC_LIBRARY";
1980 case cmTarget::SHARED_LIBRARY:
1981 return "_CREATE_SHARED_LIBRARY";
1982 case cmTarget::MODULE_LIBRARY:
1983 return "_CREATE_SHARED_MODULE";
1984 case cmTarget::EXECUTABLE:
1985 return "_LINK_EXECUTABLE";
1986 case cmTarget::UTILITY:
1987 case cmTarget::GLOBAL_TARGET:
1988 case cmTarget::INSTALL_FILES:
1989 case cmTarget::INSTALL_PROGRAMS:
1990 case cmTarget::INSTALL_DIRECTORY:
1991 break;
1993 return "";
1996 //----------------------------------------------------------------------------
1997 const char* cmTarget::GetSuffixVariableInternal(TargetType type,
1998 bool implib)
2000 switch(type)
2002 case cmTarget::STATIC_LIBRARY:
2003 return "CMAKE_STATIC_LIBRARY_SUFFIX";
2004 case cmTarget::SHARED_LIBRARY:
2005 return (implib
2006 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
2007 : "CMAKE_SHARED_LIBRARY_SUFFIX");
2008 case cmTarget::MODULE_LIBRARY:
2009 return (implib
2010 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
2011 : "CMAKE_SHARED_MODULE_SUFFIX");
2012 case cmTarget::EXECUTABLE:
2013 return (implib
2014 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
2015 : "CMAKE_EXECUTABLE_SUFFIX");
2016 case cmTarget::UTILITY:
2017 case cmTarget::GLOBAL_TARGET:
2018 case cmTarget::INSTALL_FILES:
2019 case cmTarget::INSTALL_PROGRAMS:
2020 case cmTarget::INSTALL_DIRECTORY:
2021 break;
2023 return "";
2027 //----------------------------------------------------------------------------
2028 const char* cmTarget::GetPrefixVariableInternal(TargetType type,
2029 bool implib)
2031 switch(type)
2033 case cmTarget::STATIC_LIBRARY:
2034 return "CMAKE_STATIC_LIBRARY_PREFIX";
2035 case cmTarget::SHARED_LIBRARY:
2036 return (implib
2037 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
2038 : "CMAKE_SHARED_LIBRARY_PREFIX");
2039 case cmTarget::MODULE_LIBRARY:
2040 return (implib
2041 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
2042 : "CMAKE_SHARED_MODULE_PREFIX");
2043 case cmTarget::EXECUTABLE:
2044 return (implib? "CMAKE_IMPORT_LIBRARY_PREFIX" : "");
2045 case cmTarget::UTILITY:
2046 case cmTarget::GLOBAL_TARGET:
2047 case cmTarget::INSTALL_FILES:
2048 case cmTarget::INSTALL_PROGRAMS:
2049 case cmTarget::INSTALL_DIRECTORY:
2050 break;
2052 return "";
2055 //----------------------------------------------------------------------------
2056 std::string cmTarget::GetPDBName(const char* config)
2058 std::string prefix;
2059 std::string base;
2060 std::string suffix;
2061 this->GetFullNameInternal(this->GetType(), config, false,
2062 prefix, base, suffix);
2063 return prefix+base+".pdb";
2066 //----------------------------------------------------------------------------
2067 std::string cmTarget::GetSOName(const char* config)
2069 if(this->IsImported())
2071 // Lookup the imported soname.
2072 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2074 if(info->NoSOName)
2076 // The imported library has no builtin soname so the name
2077 // searched at runtime will be just the filename.
2078 return cmSystemTools::GetFilenameName(info->Location);
2080 else
2082 // Use the soname given if any.
2083 return info->SOName;
2086 else
2088 return "";
2091 else
2093 // Compute the soname that will be built.
2094 std::string name;
2095 std::string soName;
2096 std::string realName;
2097 std::string impName;
2098 std::string pdbName;
2099 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2100 return soName;
2104 //----------------------------------------------------------------------------
2105 bool cmTarget::IsImportedSharedLibWithoutSOName(const char* config)
2107 if(this->IsImported() && this->GetType() == cmTarget::SHARED_LIBRARY)
2109 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2111 return info->NoSOName;
2114 return false;
2117 //----------------------------------------------------------------------------
2118 std::string cmTarget::NormalGetRealName(const char* config)
2120 // This should not be called for imported targets.
2121 // TODO: Split cmTarget into a class hierarchy to get compile-time
2122 // enforcement of the limited imported target API.
2123 if(this->IsImported())
2125 std::string msg = "NormalGetRealName called on imported target: ";
2126 msg += this->GetName();
2127 this->GetMakefile()->
2128 IssueMessage(cmake::INTERNAL_ERROR,
2129 msg.c_str());
2132 if(this->GetType() == cmTarget::EXECUTABLE)
2134 // Compute the real name that will be built.
2135 std::string name;
2136 std::string realName;
2137 std::string impName;
2138 std::string pdbName;
2139 this->GetExecutableNames(name, realName, impName, pdbName, config);
2140 return realName;
2142 else
2144 // Compute the real name that will be built.
2145 std::string name;
2146 std::string soName;
2147 std::string realName;
2148 std::string impName;
2149 std::string pdbName;
2150 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2151 return realName;
2155 //----------------------------------------------------------------------------
2156 std::string cmTarget::GetFullName(const char* config, bool implib)
2158 if(this->IsImported())
2160 return this->GetFullNameImported(config, implib);
2162 else
2164 return this->GetFullNameInternal(this->GetType(), config, implib);
2168 //----------------------------------------------------------------------------
2169 std::string cmTarget::GetFullNameImported(const char* config, bool implib)
2171 return cmSystemTools::GetFilenameName(
2172 this->ImportedGetFullPath(config, implib));
2175 //----------------------------------------------------------------------------
2176 void cmTarget::GetFullNameComponents(std::string& prefix, std::string& base,
2177 std::string& suffix, const char* config,
2178 bool implib)
2180 this->GetFullNameInternal(this->GetType(), config, implib,
2181 prefix, base, suffix);
2184 //----------------------------------------------------------------------------
2185 std::string cmTarget::GetFullPath(const char* config, bool implib,
2186 bool realname)
2188 if(this->IsImported())
2190 return this->ImportedGetFullPath(config, implib);
2192 else
2194 return this->NormalGetFullPath(config, implib, realname);
2198 //----------------------------------------------------------------------------
2199 std::string cmTarget::NormalGetFullPath(const char* config, bool implib,
2200 bool realname)
2202 // Start with the output directory for the target.
2203 std::string fpath = this->GetDirectory(config, implib);
2204 fpath += "/";
2206 // Add the full name of the target.
2207 if(implib)
2209 fpath += this->GetFullName(config, true);
2211 else if(realname)
2213 fpath += this->NormalGetRealName(config);
2215 else
2217 fpath += this->GetFullName(config, false);
2219 return fpath;
2222 //----------------------------------------------------------------------------
2223 std::string cmTarget::ImportedGetFullPath(const char* config, bool implib)
2225 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2227 if(implib)
2229 return info->ImportLibrary;
2231 else
2233 return info->Location;
2236 else
2238 std::string result = this->GetName();
2239 result += "-NOTFOUND";
2240 return result;
2244 //----------------------------------------------------------------------------
2245 std::string
2246 cmTarget::GetFullNameInternal(TargetType type, const char* config,
2247 bool implib)
2249 std::string prefix;
2250 std::string base;
2251 std::string suffix;
2252 this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
2253 return prefix+base+suffix;
2256 //----------------------------------------------------------------------------
2257 void cmTarget::GetFullNameInternal(TargetType type,
2258 const char* config,
2259 bool implib,
2260 std::string& outPrefix,
2261 std::string& outBase,
2262 std::string& outSuffix)
2264 // Use just the target name for non-main target types.
2265 if(type != cmTarget::STATIC_LIBRARY &&
2266 type != cmTarget::SHARED_LIBRARY &&
2267 type != cmTarget::MODULE_LIBRARY &&
2268 type != cmTarget::EXECUTABLE)
2270 outPrefix = "";
2271 outBase = this->GetName();
2272 outSuffix = "";
2273 return;
2276 // Return an empty name for the import library if this platform
2277 // does not support import libraries.
2278 if(implib &&
2279 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2281 outPrefix = "";
2282 outBase = "";
2283 outSuffix = "";
2284 return;
2287 // The implib option is only allowed for shared libraries, module
2288 // libraries, and executables.
2289 if(type != cmTarget::SHARED_LIBRARY &&
2290 type != cmTarget::MODULE_LIBRARY &&
2291 type != cmTarget::EXECUTABLE)
2293 implib = false;
2296 // Compute the full name for main target types.
2297 const char* targetPrefix = (implib
2298 ? this->GetProperty("IMPORT_PREFIX")
2299 : this->GetProperty("PREFIX"));
2300 const char* targetSuffix = (implib
2301 ? this->GetProperty("IMPORT_SUFFIX")
2302 : this->GetProperty("SUFFIX"));
2303 const char* configPostfix = 0;
2304 if(config && *config)
2306 std::string configProp = cmSystemTools::UpperCase(config);
2307 configProp += "_POSTFIX";
2308 configPostfix = this->GetProperty(configProp.c_str());
2310 const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
2311 const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
2312 const char* ll =
2313 this->GetLinkerLanguage(
2314 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2315 // first try language specific suffix
2316 if(ll)
2318 if(!targetSuffix && suffixVar && *suffixVar)
2320 std::string langSuff = suffixVar + std::string("_") + ll;
2321 targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
2323 if(!targetPrefix && prefixVar && *prefixVar)
2325 std::string langPrefix = prefixVar + std::string("_") + ll;
2326 targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
2330 // if there is no prefix on the target use the cmake definition
2331 if(!targetPrefix && prefixVar)
2333 targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
2335 // if there is no suffix on the target use the cmake definition
2336 if(!targetSuffix && suffixVar)
2338 targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
2341 // frameworks do not have a prefix or a suffix
2342 if(this->IsFrameworkOnApple())
2344 targetPrefix = 0;
2345 targetSuffix = 0;
2348 // Begin the final name with the prefix.
2349 outPrefix = targetPrefix?targetPrefix:"";
2351 // Append the target name or property-specified name.
2352 const char* outName = 0;
2353 if(config && *config)
2355 std::string configProp = cmSystemTools::UpperCase(config);
2356 configProp += "_OUTPUT_NAME";
2357 outName = this->GetProperty(configProp.c_str());
2359 if(!outName)
2361 outName = this->GetProperty("OUTPUT_NAME");
2363 if(outName)
2365 outBase = outName;
2367 else
2369 outBase = this->GetName();
2372 // Append the per-configuration postfix.
2373 outBase += configPostfix?configPostfix:"";
2375 // Name shared libraries with their version number on some platforms.
2376 if(const char* version = this->GetProperty("VERSION"))
2378 if(type == cmTarget::SHARED_LIBRARY && !implib &&
2379 this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION"))
2381 outBase += "-";
2382 outBase += version;
2386 // Append the suffix.
2387 outSuffix = targetSuffix?targetSuffix:"";
2390 //----------------------------------------------------------------------------
2391 void cmTarget::GetLibraryNames(std::string& name,
2392 std::string& soName,
2393 std::string& realName,
2394 std::string& impName,
2395 std::string& pdbName,
2396 const char* config)
2398 // Get the names based on the real type of the library.
2399 this->GetLibraryNamesInternal(name, soName, realName, impName, pdbName,
2400 this->GetType(), config);
2403 //----------------------------------------------------------------------------
2404 void cmTarget::GetLibraryCleanNames(std::string& staticName,
2405 std::string& sharedName,
2406 std::string& sharedSOName,
2407 std::string& sharedRealName,
2408 std::string& importName,
2409 std::string& pdbName,
2410 const char* config)
2412 // Get the name as if this were a static library.
2413 std::string soName;
2414 std::string realName;
2415 std::string impName;
2416 this->GetLibraryNamesInternal(staticName, soName, realName, impName,
2417 pdbName, cmTarget::STATIC_LIBRARY, config);
2419 // Get the names as if this were a shared library.
2420 if(this->GetType() == cmTarget::STATIC_LIBRARY)
2422 // Since the real type is static then the user either specified
2423 // STATIC or did not specify a type. In the former case the
2424 // shared library will never be present. In the latter case the
2425 // type will never be MODULE. Either way the only names that
2426 // might have to be cleaned are the shared library names.
2427 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2428 importName, pdbName,
2429 cmTarget::SHARED_LIBRARY, config);
2431 else
2433 // Use the name of the real type of the library (shared or module).
2434 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2435 importName, pdbName, this->GetType(),
2436 config);
2440 //----------------------------------------------------------------------------
2441 void cmTarget::GetLibraryNamesInternal(std::string& name,
2442 std::string& soName,
2443 std::string& realName,
2444 std::string& impName,
2445 std::string& pdbName,
2446 TargetType type,
2447 const char* config)
2449 // This should not be called for imported targets.
2450 // TODO: Split cmTarget into a class hierarchy to get compile-time
2451 // enforcement of the limited imported target API.
2452 if(this->IsImported())
2454 std::string msg = "GetLibraryNamesInternal called on imported target: ";
2455 msg += this->GetName();
2456 this->Makefile->IssueMessage(cmake::INTERNAL_ERROR,
2457 msg.c_str());
2458 return;
2461 // Construct the name of the soname flag variable for this language.
2462 const char* ll =
2463 this->GetLinkerLanguage(
2464 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2465 std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
2466 if(ll)
2468 sonameFlag += "_";
2469 sonameFlag += ll;
2471 sonameFlag += "_FLAG";
2473 // Check for library version properties.
2474 const char* version = this->GetProperty("VERSION");
2475 const char* soversion = this->GetProperty("SOVERSION");
2476 if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
2477 !this->Makefile->GetDefinition(sonameFlag.c_str()))
2479 // Versioning is supported only for shared libraries and modules,
2480 // and then only when the platform supports an soname flag.
2481 version = 0;
2482 soversion = 0;
2484 if(version && !soversion)
2486 // The soversion must be set if the library version is set. Use
2487 // the library version as the soversion.
2488 soversion = version;
2491 // Get the components of the library name.
2492 std::string prefix;
2493 std::string base;
2494 std::string suffix;
2495 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2497 // The library name.
2498 name = prefix+base+suffix;
2500 // The library's soname.
2501 #if defined(__APPLE__)
2502 soName = prefix+base;
2503 #else
2504 soName = name;
2505 #endif
2506 if(soversion)
2508 soName += ".";
2509 soName += soversion;
2511 #if defined(__APPLE__)
2512 soName += suffix;
2513 #endif
2515 // The library's real name on disk.
2516 #if defined(__APPLE__)
2517 realName = prefix+base;
2518 #else
2519 realName = name;
2520 #endif
2521 if(version)
2523 realName += ".";
2524 realName += version;
2526 else if(soversion)
2528 realName += ".";
2529 realName += soversion;
2531 #if defined(__APPLE__)
2532 realName += suffix;
2533 #endif
2535 // The import library name.
2536 if(type == cmTarget::SHARED_LIBRARY ||
2537 type == cmTarget::MODULE_LIBRARY)
2539 impName = this->GetFullNameInternal(type, config, true);
2541 else
2543 impName = "";
2546 // The program database file name.
2547 pdbName = prefix+base+".pdb";
2550 //----------------------------------------------------------------------------
2551 void cmTarget::GetExecutableNames(std::string& name,
2552 std::string& realName,
2553 std::string& impName,
2554 std::string& pdbName,
2555 const char* config)
2557 // Get the names based on the real type of the executable.
2558 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2559 this->GetType(), config);
2562 //----------------------------------------------------------------------------
2563 void cmTarget::GetExecutableCleanNames(std::string& name,
2564 std::string& realName,
2565 std::string& impName,
2566 std::string& pdbName,
2567 const char* config)
2569 // Get the name and versioned name of this executable.
2570 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2571 cmTarget::EXECUTABLE, config);
2574 //----------------------------------------------------------------------------
2575 void cmTarget::GetExecutableNamesInternal(std::string& name,
2576 std::string& realName,
2577 std::string& impName,
2578 std::string& pdbName,
2579 TargetType type,
2580 const char* config)
2582 // This should not be called for imported targets.
2583 // TODO: Split cmTarget into a class hierarchy to get compile-time
2584 // enforcement of the limited imported target API.
2585 if(this->IsImported())
2587 std::string msg =
2588 "GetExecutableNamesInternal called on imported target: ";
2589 msg += this->GetName();
2590 this->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR, msg.c_str());
2593 // This versioning is supported only for executables and then only
2594 // when the platform supports symbolic links.
2595 #if defined(_WIN32) && !defined(__CYGWIN__)
2596 const char* version = 0;
2597 #else
2598 // Check for executable version properties.
2599 const char* version = this->GetProperty("VERSION");
2600 if(type != cmTarget::EXECUTABLE || this->Makefile->IsOn("XCODE"))
2602 version = 0;
2604 #endif
2606 // Get the components of the executable name.
2607 std::string prefix;
2608 std::string base;
2609 std::string suffix;
2610 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2612 // The executable name.
2613 name = prefix+base+suffix;
2615 // The executable's real name on disk.
2616 #if defined(__CYGWIN__)
2617 realName = prefix+base;
2618 #else
2619 realName = name;
2620 #endif
2621 if(version)
2623 realName += "-";
2624 realName += version;
2626 #if defined(__CYGWIN__)
2627 realName += suffix;
2628 #endif
2630 // The import library name.
2631 impName = this->GetFullNameInternal(type, config, true);
2633 // The program database file name.
2634 pdbName = prefix+base+".pdb";
2637 //----------------------------------------------------------------------------
2638 void cmTarget::GenerateTargetManifest(const char* config)
2640 cmMakefile* mf = this->Makefile;
2641 cmLocalGenerator* lg = mf->GetLocalGenerator();
2642 cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2644 // Get the names.
2645 std::string name;
2646 std::string soName;
2647 std::string realName;
2648 std::string impName;
2649 std::string pdbName;
2650 if(this->GetType() == cmTarget::EXECUTABLE)
2652 this->GetExecutableNames(name, realName, impName, pdbName, config);
2654 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2655 this->GetType() == cmTarget::SHARED_LIBRARY ||
2656 this->GetType() == cmTarget::MODULE_LIBRARY)
2658 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2660 else
2662 return;
2665 // Get the directory.
2666 std::string dir = this->GetDirectory(config, false);
2668 // Add each name.
2669 std::string f;
2670 if(!name.empty())
2672 f = dir;
2673 f += "/";
2674 f += name;
2675 gg->AddToManifest(config? config:"", f);
2677 if(!soName.empty())
2679 f = dir;
2680 f += "/";
2681 f += soName;
2682 gg->AddToManifest(config? config:"", f);
2684 if(!realName.empty())
2686 f = dir;
2687 f += "/";
2688 f += realName;
2689 gg->AddToManifest(config? config:"", f);
2691 if(!pdbName.empty())
2693 f = dir;
2694 f += "/";
2695 f += pdbName;
2696 gg->AddToManifest(config? config:"", f);
2698 if(!impName.empty())
2700 f = this->GetDirectory(config, true);
2701 f += "/";
2702 f += impName;
2703 gg->AddToManifest(config? config:"", f);
2707 //----------------------------------------------------------------------------
2708 void cmTarget::SetPropertyDefault(const char* property,
2709 const char* default_value)
2711 // Compute the name of the variable holding the default value.
2712 std::string var = "CMAKE_";
2713 var += property;
2715 if(const char* value = this->Makefile->GetDefinition(var.c_str()))
2717 this->SetProperty(property, value);
2719 else if(default_value)
2721 this->SetProperty(property, default_value);
2725 //----------------------------------------------------------------------------
2726 bool cmTarget::HaveBuildTreeRPATH()
2728 return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
2729 !this->LinkLibraries.empty());
2732 //----------------------------------------------------------------------------
2733 bool cmTarget::HaveInstallTreeRPATH()
2735 const char* install_rpath = this->GetProperty("INSTALL_RPATH");
2736 return install_rpath && *install_rpath;
2739 //----------------------------------------------------------------------------
2740 bool cmTarget::NeedRelinkBeforeInstall()
2742 // Only executables and shared libraries can have an rpath and may
2743 // need relinking.
2744 if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
2745 this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
2746 this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
2748 return false;
2751 // If there is no install location this target will not be installed
2752 // and therefore does not need relinking.
2753 if(!this->GetHaveInstallRule())
2755 return false;
2758 // If skipping all rpaths completely then no relinking is needed.
2759 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
2761 return false;
2764 // If building with the install-tree rpath no relinking is needed.
2765 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2767 return false;
2770 // If chrpath is going to be used no relinking is needed.
2771 if(this->IsChrpathUsed())
2773 return false;
2776 // Check for rpath support on this platform.
2777 if(const char* ll = this->GetLinkerLanguage(
2778 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2780 std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2781 flagVar += ll;
2782 flagVar += "_FLAG";
2783 if(!this->Makefile->IsSet(flagVar.c_str()))
2785 // There is no rpath support on this platform so nothing needs
2786 // relinking.
2787 return false;
2790 else
2792 // No linker language is known. This error will be reported by
2793 // other code.
2794 return false;
2797 // If either a build or install tree rpath is set then the rpath
2798 // will likely change between the build tree and install tree and
2799 // this target must be relinked.
2800 return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
2803 //----------------------------------------------------------------------------
2804 std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
2806 // If building directly for installation then the build tree install_name
2807 // is the same as the install tree.
2808 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2810 return GetInstallNameDirForInstallTree(config);
2813 // Use the build tree directory for the target.
2814 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2815 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2816 !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
2818 std::string dir = this->GetDirectory(config);
2819 dir += "/";
2820 return dir;
2822 else
2824 return "";
2828 //----------------------------------------------------------------------------
2829 std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
2831 // Lookup the target property.
2832 const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
2833 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2834 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2835 install_name_dir && *install_name_dir)
2837 std::string dir = install_name_dir;
2838 dir += "/";
2839 return dir;
2841 else
2843 return "";
2847 //----------------------------------------------------------------------------
2848 const char* cmTarget::GetAndCreateOutputDir(bool implib, bool create)
2850 // The implib option is only allowed for shared libraries, module
2851 // libraries, and executables.
2852 if(this->GetType() != cmTarget::SHARED_LIBRARY &&
2853 this->GetType() != cmTarget::MODULE_LIBRARY &&
2854 this->GetType() != cmTarget::EXECUTABLE)
2856 implib = false;
2859 // Sanity check. Only generators on platforms supporting import
2860 // libraries should be asking for the import library output
2861 // directory.
2862 if(implib &&
2863 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2865 std::string msg = "GetAndCreateOutputDir, imlib set but there is no "
2866 "CMAKE_IMPORT_LIBRARY_SUFFIX for target: ";
2867 msg += this->GetName();
2868 this->GetMakefile()->
2869 IssueMessage(cmake::INTERNAL_ERROR,
2870 msg.c_str());
2872 if(implib && !this->DLLPlatform)
2874 std::string msg = "implib set for platform that does not "
2875 " support DLL's for target: ";
2876 msg += this->GetName();
2877 this->GetMakefile()->
2878 IssueMessage(cmake::INTERNAL_ERROR,
2879 msg.c_str());
2882 // Select whether we are constructing the directory for the main
2883 // target or the import library.
2884 std::string& out = implib? this->OutputDirImplib : this->OutputDir;
2886 if(out.empty())
2888 // Look for a target property defining the target output directory
2889 // based on the target type.
2890 const char* propertyName = 0;
2891 switch(this->GetType())
2893 case cmTarget::SHARED_LIBRARY:
2895 // For non-DLL platforms shared libraries are treated as
2896 // library targets. For DLL platforms the DLL part of a
2897 // shared library is treated as a runtime target and the
2898 // corresponding import library is treated as an archive
2899 // target.
2900 if(this->DLLPlatform)
2902 if(implib)
2904 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2906 else
2908 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2911 else
2913 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2915 } break;
2916 case cmTarget::STATIC_LIBRARY:
2918 // Static libraries are always treated as archive targets.
2919 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2920 } break;
2921 case cmTarget::MODULE_LIBRARY:
2923 // Module libraries are always treated as library targets.
2924 // Module import libraries are treated as archive targets.
2925 if(implib)
2927 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2929 else
2931 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2933 } break;
2934 case cmTarget::EXECUTABLE:
2936 // Executables are always treated as runtime targets.
2937 // Executable import libraries are treated as archive targets.
2938 if(implib)
2940 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2942 else
2944 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2946 } break;
2947 default: break;
2950 // Select an output directory.
2951 if(const char* outdir = this->GetProperty(propertyName))
2953 // Use the user-specified output directory.
2954 out = outdir;
2956 else if(this->GetType() == cmTarget::EXECUTABLE)
2958 // Lookup the output path for executables.
2959 out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
2961 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2962 this->GetType() == cmTarget::SHARED_LIBRARY ||
2963 this->GetType() == cmTarget::MODULE_LIBRARY)
2965 // Lookup the output path for libraries.
2966 out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
2968 if(out.empty())
2970 // Default to the current output directory.
2971 out = ".";
2973 // Convert the output path to a full path in case it is
2974 // specified as a relative path. Treat a relative path as
2975 // relative to the current output directory for this makefile.
2976 out =
2977 cmSystemTools::CollapseFullPath
2978 (out.c_str(), this->Makefile->GetStartOutputDirectory());
2980 // TODO: Make AppBundle and Framework directory computation in
2981 // target consistent. Why do we add the .framework part here for
2982 // frameworks but not the .app part for bundles? We should
2983 // probably not add it for either.
2984 if(this->IsFrameworkOnApple())
2986 out += "/";
2987 out += this->GetFullName(0, implib);
2988 out += ".framework";
2991 // Optionally make sure the output path exists on disk.
2992 if(create)
2994 if(!cmSystemTools::MakeDirectory(out.c_str()))
2996 cmSystemTools::Error("Error failed to create output directory: ",
2997 out.c_str());
3002 return out.c_str();
3005 //----------------------------------------------------------------------------
3006 const char* cmTarget::GetOutputDir(bool implib)
3008 return this->GetAndCreateOutputDir(implib, true);
3011 //----------------------------------------------------------------------------
3012 const char* cmTarget::GetExportMacro()
3014 // Define the symbol for targets that export symbols.
3015 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3016 this->GetType() == cmTarget::MODULE_LIBRARY ||
3017 this->IsExecutableWithExports())
3019 if(const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL"))
3021 this->ExportMacro = custom_export_name;
3023 else
3025 std::string in = this->GetName();
3026 in += "_EXPORTS";
3027 this->ExportMacro = cmSystemTools::MakeCindentifier(in.c_str());
3029 return this->ExportMacro.c_str();
3031 else
3033 return 0;
3037 //----------------------------------------------------------------------------
3038 void cmTarget::GetLanguages(std::set<cmStdString>& languages) const
3040 for(std::vector<cmSourceFile*>::const_iterator
3041 i = this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
3043 if(const char* lang = (*i)->GetLanguage())
3045 languages.insert(lang);
3050 //----------------------------------------------------------------------------
3051 bool cmTarget::IsChrpathUsed()
3053 #if defined(CMAKE_USE_ELF_PARSER)
3054 // Only certain target types have an rpath.
3055 if(!(this->GetType() == cmTarget::SHARED_LIBRARY ||
3056 this->GetType() == cmTarget::MODULE_LIBRARY ||
3057 this->GetType() == cmTarget::EXECUTABLE))
3059 return false;
3062 // If the target will not be installed we do not need to change its
3063 // rpath.
3064 if(!this->GetHaveInstallRule())
3066 return false;
3069 // Skip chrpath if skipping rpath altogether.
3070 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
3072 return false;
3075 // Skip chrpath if it does not need to be changed at install time.
3076 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
3078 return false;
3081 // Allow the user to disable builtin chrpath explicitly.
3082 if(this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH"))
3084 return false;
3087 // Enable if the rpath flag uses a separator and the target uses ELF
3088 // binaries.
3089 if(const char* ll = this->GetLinkerLanguage(
3090 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
3092 std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
3093 sepVar += ll;
3094 sepVar += "_FLAG_SEP";
3095 const char* sep = this->Makefile->GetDefinition(sepVar.c_str());
3096 if(sep && *sep)
3098 // TODO: Add ELF check to ABI detection and get rid of
3099 // CMAKE_EXECUTABLE_FORMAT.
3100 if(const char* fmt =
3101 this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT"))
3103 return strcmp(fmt, "ELF") == 0;
3107 #endif
3108 return false;
3111 //----------------------------------------------------------------------------
3112 cmTarget::ImportInfo const*
3113 cmTarget::GetImportInfo(const char* config)
3115 // There is no imported information for non-imported targets.
3116 if(!this->IsImported())
3118 return 0;
3121 // Lookup/compute/cache the import information for this
3122 // configuration.
3123 std::string config_upper;
3124 if(config && *config)
3126 config_upper = cmSystemTools::UpperCase(config);
3128 else
3130 config_upper = "NOCONFIG";
3132 ImportInfoMapType::const_iterator i =
3133 this->ImportInfoMap.find(config_upper);
3134 if(i == this->ImportInfoMap.end())
3136 ImportInfo info;
3137 this->ComputeImportInfo(config_upper, info);
3138 ImportInfoMapType::value_type entry(config_upper, info);
3139 i = this->ImportInfoMap.insert(entry).first;
3142 // If the location is empty then the target is not available for
3143 // this configuration.
3144 if(i->second.Location.empty())
3146 return 0;
3149 // Return the import information.
3150 return &i->second;
3153 //----------------------------------------------------------------------------
3154 void cmTarget::ComputeImportInfo(std::string const& desired_config,
3155 ImportInfo& info)
3157 // This method finds information about an imported target from its
3158 // properties. The "IMPORTED_" namespace is reserved for properties
3159 // defined by the project exporting the target.
3161 // Initialize members.
3162 info.NoSOName = false;
3164 // Track the configuration-specific property suffix.
3165 std::string suffix = "_";
3166 suffix += desired_config;
3168 // Look for a mapping from the current project's configuration to
3169 // the imported project's configuration.
3170 std::vector<std::string> mappedConfigs;
3172 std::string mapProp = "MAP_IMPORTED_CONFIG_";
3173 mapProp += desired_config;
3174 if(const char* mapValue = this->GetProperty(mapProp.c_str()))
3176 cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
3180 // If a mapping was found, check its configurations.
3181 const char* loc = 0;
3182 for(std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
3183 !loc && mci != mappedConfigs.end(); ++mci)
3185 // Look for this configuration.
3186 std::string mcUpper = cmSystemTools::UpperCase(mci->c_str());
3187 std::string locProp = "IMPORTED_LOCATION_";
3188 locProp += mcUpper;
3189 loc = this->GetProperty(locProp.c_str());
3191 // If it was found, use it for all properties below.
3192 if(loc)
3194 suffix = "_";
3195 suffix += mcUpper;
3199 // If we needed to find one of the mapped configurations but did not
3200 // then the target is not found. The project does not want any
3201 // other configuration.
3202 if(!mappedConfigs.empty() && !loc)
3204 return;
3207 // If we have not yet found it then there are no mapped
3208 // configurations. Look for an exact-match.
3209 if(!loc)
3211 std::string locProp = "IMPORTED_LOCATION";
3212 locProp += suffix;
3213 loc = this->GetProperty(locProp.c_str());
3216 // If we have not yet found it then there are no mapped
3217 // configurations and no exact match.
3218 if(!loc)
3220 // The suffix computed above is not useful.
3221 suffix = "";
3223 // Look for a configuration-less location. This may be set by
3224 // manually-written code.
3225 loc = this->GetProperty("IMPORTED_LOCATION");
3228 // If we have not yet found it then the project is willing to try
3229 // any available configuration.
3230 if(!loc)
3232 std::vector<std::string> availableConfigs;
3233 if(const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS"))
3235 cmSystemTools::ExpandListArgument(iconfigs, availableConfigs);
3237 for(std::vector<std::string>::const_iterator
3238 aci = availableConfigs.begin();
3239 !loc && aci != availableConfigs.end(); ++aci)
3241 suffix = "_";
3242 suffix += cmSystemTools::UpperCase(availableConfigs[0]);
3243 std::string locProp = "IMPORTED_LOCATION";
3244 locProp += suffix;
3245 loc = this->GetProperty(locProp.c_str());
3249 // If we have not yet found it then the target is not available.
3250 if(!loc)
3252 return;
3255 // A provided configuration has been chosen. Load the
3256 // configuration's properties.
3257 info.Location = loc;
3259 // Get the soname.
3260 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3262 std::string soProp = "IMPORTED_SONAME";
3263 soProp += suffix;
3264 if(const char* config_soname = this->GetProperty(soProp.c_str()))
3266 info.SOName = config_soname;
3268 else if(const char* soname = this->GetProperty("IMPORTED_SONAME"))
3270 info.SOName = soname;
3274 // Get the "no-soname" mark.
3275 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3277 std::string soProp = "IMPORTED_NO_SONAME";
3278 soProp += suffix;
3279 if(const char* config_no_soname = this->GetProperty(soProp.c_str()))
3281 info.NoSOName = cmSystemTools::IsOn(config_no_soname);
3283 else if(const char* no_soname = this->GetProperty("IMPORTED_NO_SONAME"))
3285 info.NoSOName = cmSystemTools::IsOn(no_soname);
3289 // Get the import library.
3290 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3291 this->IsExecutableWithExports())
3293 std::string impProp = "IMPORTED_IMPLIB";
3294 impProp += suffix;
3295 if(const char* config_implib = this->GetProperty(impProp.c_str()))
3297 info.ImportLibrary = config_implib;
3299 else if(const char* implib = this->GetProperty("IMPORTED_IMPLIB"))
3301 info.ImportLibrary = implib;
3305 // Get the link interface.
3307 std::string linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
3308 linkProp += suffix;
3309 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3311 cmSystemTools::ExpandListArgument(config_libs,
3312 info.LinkInterface.Libraries);
3314 else if(const char* libs =
3315 this->GetProperty("IMPORTED_LINK_INTERFACE_LIBRARIES"))
3317 cmSystemTools::ExpandListArgument(libs,
3318 info.LinkInterface.Libraries);
3322 // Get the link dependencies.
3324 std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
3325 linkProp += suffix;
3326 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3328 cmSystemTools::ExpandListArgument(config_libs,
3329 info.LinkInterface.SharedDeps);
3331 else if(const char* libs =
3332 this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES"))
3334 cmSystemTools::ExpandListArgument(libs, info.LinkInterface.SharedDeps);
3339 //----------------------------------------------------------------------------
3340 cmTargetLinkInterface const* cmTarget::GetLinkInterface(const char* config)
3342 // Imported targets have their own link interface.
3343 if(this->IsImported())
3345 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
3347 return &info->LinkInterface;
3349 return 0;
3352 // Link interfaces are supported only for shared libraries and
3353 // executables that export symbols.
3354 if((this->GetType() != cmTarget::SHARED_LIBRARY &&
3355 !this->IsExecutableWithExports()))
3357 return 0;
3360 // Lookup any existing link interface for this configuration.
3361 std::map<cmStdString, cmTargetLinkInterface*>::iterator
3362 i = this->LinkInterface.find(config?config:"");
3363 if(i == this->LinkInterface.end())
3365 // Compute the link interface for this configuration.
3366 cmTargetLinkInterface* iface = this->ComputeLinkInterface(config);
3368 // Store the information for this configuration.
3369 std::map<cmStdString, cmTargetLinkInterface*>::value_type
3370 entry(config?config:"", iface);
3371 i = this->LinkInterface.insert(entry).first;
3374 return i->second;
3377 //----------------------------------------------------------------------------
3378 cmTargetLinkInterface* cmTarget::ComputeLinkInterface(const char* config)
3380 // Construct the property name suffix for this configuration.
3381 std::string suffix = "_";
3382 if(config && *config)
3384 suffix += cmSystemTools::UpperCase(config);
3386 else
3388 suffix += "NOCONFIG";
3391 // Lookup the link interface libraries.
3392 const char* libs = 0;
3394 // Lookup the per-configuration property.
3395 std::string propName = "LINK_INTERFACE_LIBRARIES";
3396 propName += suffix;
3397 libs = this->GetProperty(propName.c_str());
3399 // If not set, try the generic property.
3400 if(!libs)
3402 libs = this->GetProperty("LINK_INTERFACE_LIBRARIES");
3406 // If still not set, there is no link interface.
3407 if(!libs)
3409 return 0;
3412 // Allocate the interface.
3413 cmTargetLinkInterface* iface = new cmTargetLinkInterface;
3414 if(!iface)
3416 return 0;
3419 // Expand the list of libraries in the interface.
3420 cmSystemTools::ExpandListArgument(libs, iface->Libraries);
3422 // Now we need to construct a list of shared library dependencies
3423 // not included in the interface.
3424 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3426 // Use a set to keep track of what libraries have been emitted to
3427 // either list.
3428 std::set<cmStdString> emitted;
3429 for(std::vector<std::string>::const_iterator
3430 li = iface->Libraries.begin();
3431 li != iface->Libraries.end(); ++li)
3433 emitted.insert(*li);
3436 // Compute which library configuration to link.
3437 cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
3438 if(config && cmSystemTools::UpperCase(config) == "DEBUG")
3440 linkType = cmTarget::DEBUG;
3443 // Construct the list of libs linked for this configuration.
3444 cmTarget::LinkLibraryVectorType const& llibs =
3445 this->GetOriginalLinkLibraries();
3446 for(cmTarget::LinkLibraryVectorType::const_iterator li = llibs.begin();
3447 li != llibs.end(); ++li)
3449 // Skip entries that will resolve to the target itself, are empty,
3450 // or are not meant for this configuration.
3451 if(li->first == this->GetName() || li->first.empty() ||
3452 !(li->second == cmTarget::GENERAL || li->second == linkType))
3454 continue;
3457 // Skip entries that have already been emitted into either list.
3458 if(!emitted.insert(li->first).second)
3460 continue;
3463 // Add this entry if it is a shared library.
3464 if(cmTarget* tgt = this->Makefile->FindTargetToUse(li->first.c_str()))
3466 if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
3468 iface->SharedDeps.push_back(li->first);
3471 else
3473 // TODO: Recognize shared library file names. Perhaps this
3474 // should be moved to cmComputeLinkInformation, but that creates
3475 // a chicken-and-egg problem since this list is needed for its
3476 // construction.
3481 // Return the completed interface.
3482 return iface;
3485 //----------------------------------------------------------------------------
3486 cmComputeLinkInformation*
3487 cmTarget::GetLinkInformation(const char* config)
3489 // Link information does not make sense for static libraries.
3490 assert(this->GetType() != cmTarget::STATIC_LIBRARY);
3492 // Lookup any existing information for this configuration.
3493 std::map<cmStdString, cmComputeLinkInformation*>::iterator
3494 i = this->LinkInformation.find(config?config:"");
3495 if(i == this->LinkInformation.end())
3497 // Compute information for this configuration.
3498 cmComputeLinkInformation* info =
3499 new cmComputeLinkInformation(this, config);
3500 if(!info || !info->Compute())
3502 delete info;
3503 info = 0;
3506 // Store the information for this configuration.
3507 std::map<cmStdString, cmComputeLinkInformation*>::value_type
3508 entry(config?config:"", info);
3509 i = this->LinkInformation.insert(entry).first;
3511 return i->second;
3514 //----------------------------------------------------------------------------
3515 cmTargetLinkInformationMap
3516 ::cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r): derived()
3518 // Ideally cmTarget instances should never be copied. However until
3519 // we can make a sweep to remove that, this copy constructor avoids
3520 // allowing the resources (LinkInformation) from getting copied. In
3521 // the worst case this will lead to extra cmComputeLinkInformation
3522 // instances. We also enforce in debug mode that the map be emptied
3523 // when copied.
3524 static_cast<void>(r);
3525 assert(r.empty());
3528 //----------------------------------------------------------------------------
3529 cmTargetLinkInformationMap::~cmTargetLinkInformationMap()
3531 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3533 delete i->second;
3537 //----------------------------------------------------------------------------
3538 cmTargetLinkInterfaceMap
3539 ::cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap const& r): derived()
3541 // Ideally cmTarget instances should never be copied. However until
3542 // we can make a sweep to remove that, this copy constructor avoids
3543 // allowing the resources (LinkInterface) from getting copied. In
3544 // the worst case this will lead to extra cmTargetLinkInterface
3545 // instances. We also enforce in debug mode that the map be emptied
3546 // when copied.
3547 static_cast<void>(r);
3548 assert(r.empty());
3551 //----------------------------------------------------------------------------
3552 cmTargetLinkInterfaceMap::~cmTargetLinkInterfaceMap()
3554 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3556 delete i->second;
3560 //----------------------------------------------------------------------------
3561 cmTargetInternalPointer::cmTargetInternalPointer()
3563 this->Pointer = new cmTargetInternals;
3566 //----------------------------------------------------------------------------
3567 cmTargetInternalPointer
3568 ::cmTargetInternalPointer(cmTargetInternalPointer const&)
3570 // Ideally cmTarget instances should never be copied. However until
3571 // we can make a sweep to remove that, this copy constructor avoids
3572 // allowing the resources (Internals) to be copied.
3573 this->Pointer = new cmTargetInternals;
3576 //----------------------------------------------------------------------------
3577 cmTargetInternalPointer::~cmTargetInternalPointer()
3579 delete this->Pointer;
3582 //----------------------------------------------------------------------------
3583 cmTargetInternalPointer&
3584 cmTargetInternalPointer::operator=(cmTargetInternalPointer const& r)
3586 if(this == &r) { return *this; } // avoid warning on HP about self check
3587 // Ideally cmTarget instances should never be copied. However until
3588 // we can make a sweep to remove that, this copy constructor avoids
3589 // allowing the resources (Internals) to be copied.
3590 cmTargetInternals* oldPointer = this->Pointer;
3591 this->Pointer = new cmTargetInternals;
3592 delete oldPointer;
3593 return *this;