Resync.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmTarget.cxx
blobf68a5dacec411c586d35aefea97f97ac8a2fc049
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-13 20:35:39 $
7 Version: $Revision: 1.209 $
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.");
572 cm->DefineProperty
573 ("XCODE_ATTRIBUTE_<an-attribute>", cmProperty::TARGET,
574 "Set Xcode target attributes directly.",
575 "Tell the Xcode generator to set '<an-attribute>' to a given value "
576 "in the generated Xcode project. Ignored on other generators.");
578 cm->DefineProperty
579 ("GENERATOR_FILE_NAME", cmProperty::TARGET,
580 "Generator's file for this target.",
581 "An internal property used by some generators to record the name of "
582 "project or dsp file associated with this target.");
584 cm->DefineProperty
585 ("SOURCES", cmProperty::TARGET,
586 "Source names specified for a target.",
587 "Read-only list of sources specified for a target. "
588 "The names returned are suitable for passing to the "
589 "set_source_files_properties command.");
591 #if 0
592 cm->DefineProperty
593 ("OBJECT_FILES", cmProperty::TARGET,
594 "Used to get the resulting list of object files that make up a "
595 "target.",
596 "This can be used to put object files from one library "
597 "into another library. It is a read only property. It "
598 "converts the source list for the target into a list of full "
599 "paths to object names that will be produced by the target.");
600 #endif
602 #define CM_TARGET_FILE_TYPES_DOC \
603 "There are three kinds of target files that may be built: " \
604 "archive, library, and runtime. " \
605 "Executables are always treated as runtime targets. " \
606 "Static libraries are always treated as archive targets. " \
607 "Module libraries are always treated as library targets. " \
608 "For non-DLL platforms shared libraries are treated as library " \
609 "targets. " \
610 "For DLL platforms the DLL part of a shared library is treated as " \
611 "a runtime target and the corresponding import library is treated as " \
612 "an archive target. " \
613 "All Windows-based systems including Cygwin are DLL platforms."
615 cm->DefineProperty
616 ("ARCHIVE_OUTPUT_DIRECTORY", cmProperty::TARGET,
617 "Output directory in which to build ARCHIVE target files.",
618 "This property specifies the directory into which archive target files "
619 "should be built. "
620 CM_TARGET_FILE_TYPES_DOC " "
621 "This property is initialized by the value of the variable "
622 "CMAKE_ARCHIVE_OUTPUT_DIRECTORY if it is set when a target is created.");
623 cm->DefineProperty
624 ("LIBRARY_OUTPUT_DIRECTORY", cmProperty::TARGET,
625 "Output directory in which to build LIBRARY target files.",
626 "This property specifies the directory into which library target files "
627 "should be built. "
628 CM_TARGET_FILE_TYPES_DOC " "
629 "This property is initialized by the value of the variable "
630 "CMAKE_LIBRARY_OUTPUT_DIRECTORY if it is set when a target is created.");
631 cm->DefineProperty
632 ("RUNTIME_OUTPUT_DIRECTORY", cmProperty::TARGET,
633 "Output directory in which to build RUNTIME target files.",
634 "This property specifies the directory into which runtime target files "
635 "should be built. "
636 CM_TARGET_FILE_TYPES_DOC " "
637 "This property is initialized by the value of the variable "
638 "CMAKE_RUNTIME_OUTPUT_DIRECTORY if it is set when a target is created.");
640 // define some properties without documentation
641 cm->DefineProperty("DEBUG_OUTPUT_NAME", cmProperty::TARGET,0,0);
642 cm->DefineProperty("RELEASE_OUTPUT_NAME", cmProperty::TARGET,0,0);
645 void cmTarget::SetType(TargetType type, const char* name)
647 this->Name = name;
648 if(type == cmTarget::INSTALL_FILES ||
649 type == cmTarget::INSTALL_PROGRAMS ||
650 type == cmTarget::INSTALL_DIRECTORY)
652 this->Makefile->
653 IssueMessage(cmake::INTERNAL_ERROR,
654 "SetType called on cmTarget for INSTALL_FILES, "
655 "INSTALL_PROGRAMS, or INSTALL_DIRECTORY ");
656 return;
658 // only add dependency information for library targets
659 this->TargetTypeValue = type;
660 if(this->TargetTypeValue >= STATIC_LIBRARY
661 && this->TargetTypeValue <= MODULE_LIBRARY)
663 this->RecordDependencies = true;
665 else
667 this->RecordDependencies = false;
671 //----------------------------------------------------------------------------
672 void cmTarget::SetMakefile(cmMakefile* mf)
674 // Set our makefile.
675 this->Makefile = mf;
677 // set the cmake instance of the properties
678 this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
680 // Check whether this is a DLL platform.
681 this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
682 this->Makefile->IsOn("CYGWIN") ||
683 this->Makefile->IsOn("MINGW"));
685 // Setup default property values.
686 this->SetPropertyDefault("INSTALL_NAME_DIR", "");
687 this->SetPropertyDefault("INSTALL_RPATH", "");
688 this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
689 this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
690 this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
691 this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
692 this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
693 this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
694 this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", 0);
696 // Collect the set of configuration types.
697 std::vector<std::string> configNames;
698 if(const char* configurationTypes =
699 mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
701 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
703 else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
705 if(*buildType)
707 configNames.push_back(buildType);
711 // Setup per-configuration property default values.
712 for(std::vector<std::string>::iterator ci = configNames.begin();
713 ci != configNames.end(); ++ci)
715 // Initialize per-configuration name postfix property from the
716 // variable only for non-executable targets. This preserves
717 // compatibility with previous CMake versions in which executables
718 // did not support this variable. Projects may still specify the
719 // property directly. TODO: Make this depend on backwards
720 // compatibility setting.
721 if(this->TargetTypeValue != cmTarget::EXECUTABLE)
723 std::string property = cmSystemTools::UpperCase(*ci);
724 property += "_POSTFIX";
725 this->SetPropertyDefault(property.c_str(), 0);
729 // Save the backtrace of target construction.
730 this->Makefile->GetBacktrace(this->Internal->Backtrace);
732 // Record current policies for later use.
733 this->PolicyStatusCMP0003 =
734 this->Makefile->GetPolicyStatus(cmPolicies::CMP0003);
735 this->PolicyStatusCMP0004 =
736 this->Makefile->GetPolicyStatus(cmPolicies::CMP0004);
739 //----------------------------------------------------------------------------
740 cmListFileBacktrace const& cmTarget::GetBacktrace() const
742 return this->Internal->Backtrace;
745 //----------------------------------------------------------------------------
746 bool cmTarget::IsExecutableWithExports()
748 return (this->GetType() == cmTarget::EXECUTABLE &&
749 this->GetPropertyAsBool("ENABLE_EXPORTS"));
752 //----------------------------------------------------------------------------
753 bool cmTarget::IsFrameworkOnApple()
755 return (this->GetType() == cmTarget::SHARED_LIBRARY &&
756 this->Makefile->IsOn("APPLE") &&
757 this->GetPropertyAsBool("FRAMEWORK"));
760 //----------------------------------------------------------------------------
761 bool cmTarget::IsAppBundleOnApple()
763 return (this->GetType() == cmTarget::EXECUTABLE &&
764 this->Makefile->IsOn("APPLE") &&
765 this->GetPropertyAsBool("MACOSX_BUNDLE"));
768 //----------------------------------------------------------------------------
769 class cmTargetTraceDependencies
771 public:
772 cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile);
773 void Trace();
774 private:
775 cmTarget* Target;
776 cmMakefile* Makefile;
777 cmGlobalGenerator* GlobalGenerator;
778 std::queue<cmStdString> DependencyQueue;
779 std::set<cmStdString> DependenciesQueued;
780 std::set<cmSourceFile*> TargetSources;
782 void QueueOnce(std::string const& name);
783 void QueueOnce(std::vector<std::string> const& names);
784 void QueueDependencies(cmSourceFile* sf);
785 bool IsUtility(std::string const& dep);
786 void CheckCustomCommand(cmCustomCommand const& cc);
787 void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
790 //----------------------------------------------------------------------------
791 cmTargetTraceDependencies
792 ::cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile):
793 Target(target)
795 // Convenience.
796 this->Makefile = this->Target->GetMakefile();
797 this->GlobalGenerator =
798 this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
800 // Queue all the source files already specified for the target.
801 std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
802 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
803 si != sources.end(); ++si)
805 // Queue the source file itself in case it is generated.
806 this->QueueOnce((*si)->GetFullPath());
808 // Queue the dependencies of the source file in case they are
809 // generated.
810 this->QueueDependencies(*si);
812 // Track the sources already known to the target.
813 this->TargetSources.insert(*si);
816 // Queue the VS project file to check dependencies on the rule to
817 // generate it.
818 if(vsProjectFile)
820 this->QueueOnce(vsProjectFile);
823 // Queue pre-build, pre-link, and post-build rule dependencies.
824 this->CheckCustomCommands(this->Target->GetPreBuildCommands());
825 this->CheckCustomCommands(this->Target->GetPreLinkCommands());
826 this->CheckCustomCommands(this->Target->GetPostBuildCommands());
829 //----------------------------------------------------------------------------
830 void cmTargetTraceDependencies::Trace()
832 // Process one dependency at a time until the queue is empty.
833 while(!this->DependencyQueue.empty())
835 // Get the next dependency in from queue.
836 std::string dep = this->DependencyQueue.front();
837 this->DependencyQueue.pop();
839 // Check if we know how to generate this dependency.
840 if(cmSourceFile* sf =
841 this->Makefile->GetSourceFileWithOutput(dep.c_str()))
843 // Queue dependencies needed to generate this file.
844 this->QueueDependencies(sf);
846 // Make sure this file is in the target.
847 if(this->TargetSources.insert(sf).second)
849 this->Target->AddSourceFile(sf);
855 //----------------------------------------------------------------------------
856 void cmTargetTraceDependencies::QueueOnce(std::string const& name)
858 if(this->DependenciesQueued.insert(name).second)
860 this->DependencyQueue.push(name);
864 //----------------------------------------------------------------------------
865 void
866 cmTargetTraceDependencies::QueueOnce(std::vector<std::string> const& names)
868 for(std::vector<std::string>::const_iterator i = names.begin();
869 i != names.end(); ++i)
871 this->QueueOnce(*i);
875 //----------------------------------------------------------------------------
876 bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
878 // Dependencies on targets (utilities) are supposed to be named by
879 // just the target name. However for compatibility we support
880 // naming the output file generated by the target (assuming there is
881 // no output-name property which old code would not have set). In
882 // that case the target name will be the file basename of the
883 // dependency.
884 std::string util = cmSystemTools::GetFilenameName(dep);
885 if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
887 util = cmSystemTools::GetFilenameWithoutLastExtension(util);
890 // Check for a non-imported target with this name.
891 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, util.c_str()))
893 // If we find the target and the dep was given as a full path,
894 // then make sure it was not a full path to something else, and
895 // the fact that the name matched a target was just a coincidence.
896 if(cmSystemTools::FileIsFullPath(dep.c_str()))
898 // This is really only for compatibility so we do not need to
899 // worry about configuration names and output names.
900 std::string tLocation = t->GetLocation(0);
901 tLocation = cmSystemTools::GetFilenamePath(tLocation);
902 std::string depLocation = cmSystemTools::GetFilenamePath(dep);
903 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
904 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
905 if(depLocation == tLocation)
907 this->Target->AddUtility(util.c_str());
908 return true;
911 else
913 // The original name of the dependency was not a full path. It
914 // must name a target, so add the target-level dependency.
915 this->Target->AddUtility(util.c_str());
916 return true;
920 // The dependency does not name a target built in this project.
921 return false;
924 //----------------------------------------------------------------------------
925 void cmTargetTraceDependencies::QueueDependencies(cmSourceFile* sf)
927 // Queue dependency added explicitly by the user.
928 if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
930 std::vector<std::string> objDeps;
931 cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
932 this->QueueOnce(objDeps);
935 // Queue dependencies added programatically by commands.
936 this->QueueOnce(sf->GetDepends());
938 // Queue custom command dependencies.
939 if(cmCustomCommand const* cc = sf->GetCustomCommand())
941 this->CheckCustomCommand(*cc);
946 //----------------------------------------------------------------------------
947 void
948 cmTargetTraceDependencies
949 ::CheckCustomCommand(cmCustomCommand const& cc)
951 // Transform command names that reference targets built in this
952 // project to corresponding target-level dependencies.
953 for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
954 cit != cc.GetCommandLines().end(); ++cit)
956 std::string const& command = *cit->begin();
957 // Look for a non-imported target with this name.
958 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, command.c_str()))
960 if(t->GetType() == cmTarget::EXECUTABLE)
962 // The command refers to an executable target built in
963 // this project. Add the target-level dependency to make
964 // sure the executable is up to date before this custom
965 // command possibly runs.
966 this->Target->AddUtility(command.c_str());
971 // Queue the custom command dependencies.
972 std::vector<std::string> const& depends = cc.GetDepends();
973 for(std::vector<std::string>::const_iterator di = depends.begin();
974 di != depends.end(); ++di)
976 std::string const& dep = *di;
977 if(!this->IsUtility(dep))
979 // The dependency does not name a target and may be a file we
980 // know how to generate. Queue it.
981 this->QueueOnce(dep);
986 //----------------------------------------------------------------------------
987 void
988 cmTargetTraceDependencies
989 ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
991 for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
992 cli != commands.end(); ++cli)
994 this->CheckCustomCommand(*cli);
998 //----------------------------------------------------------------------------
999 void cmTarget::TraceDependencies(const char* vsProjectFile)
1001 // Use a helper object to trace the dependencies.
1002 cmTargetTraceDependencies tracer(this, vsProjectFile);
1003 tracer.Trace();
1006 //----------------------------------------------------------------------------
1007 void cmTarget::AddSources(std::vector<std::string> const& srcs)
1009 for(std::vector<std::string>::const_iterator i = srcs.begin();
1010 i != srcs.end(); ++i)
1012 this->AddSource(i->c_str());
1016 //----------------------------------------------------------------------------
1017 cmSourceFile* cmTarget::AddSource(const char* s)
1019 std::string src = s;
1021 // For backwards compatibility replace varibles in source names.
1022 // This should eventually be removed.
1023 this->Makefile->ExpandVariablesInString(src);
1025 cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
1026 this->AddSourceFile(sf);
1027 return sf;
1030 //----------------------------------------------------------------------------
1031 struct cmTarget::SourceFileFlags
1032 cmTarget::GetTargetSourceFileFlags(const cmSourceFile* sf)
1034 struct SourceFileFlags flags;
1035 this->ConstructSourceFileFlags();
1036 std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
1037 this->Internal->SourceFlagsMap.find(sf);
1038 if(si != this->Internal->SourceFlagsMap.end())
1040 flags = si->second;
1042 return flags;
1045 //----------------------------------------------------------------------------
1046 void cmTarget::ConstructSourceFileFlags()
1048 if(this->Internal->SourceFileFlagsConstructed)
1050 return;
1052 this->Internal->SourceFileFlagsConstructed = true;
1054 // Process public headers to mark the source files.
1055 if(const char* files = this->GetProperty("PUBLIC_HEADER"))
1057 std::vector<std::string> relFiles;
1058 cmSystemTools::ExpandListArgument(files, relFiles);
1059 for(std::vector<std::string>::iterator it = relFiles.begin();
1060 it != relFiles.end(); ++it)
1062 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1064 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1065 flags.MacFolder = "Headers";
1066 flags.Type = cmTarget::SourceFileTypePublicHeader;
1071 // Process private headers after public headers so that they take
1072 // precedence if a file is listed in both.
1073 if(const char* files = this->GetProperty("PRIVATE_HEADER"))
1075 std::vector<std::string> relFiles;
1076 cmSystemTools::ExpandListArgument(files, relFiles);
1077 for(std::vector<std::string>::iterator it = relFiles.begin();
1078 it != relFiles.end(); ++it)
1080 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1082 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1083 flags.MacFolder = "PrivateHeaders";
1084 flags.Type = cmTarget::SourceFileTypePrivateHeader;
1089 // Mark sources listed as resources.
1090 if(const char* files = this->GetProperty("RESOURCE"))
1092 std::vector<std::string> relFiles;
1093 cmSystemTools::ExpandListArgument(files, relFiles);
1094 for(std::vector<std::string>::iterator it = relFiles.begin();
1095 it != relFiles.end(); ++it)
1097 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1099 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1100 flags.MacFolder = "Resources";
1101 flags.Type = cmTarget::SourceFileTypeResource;
1106 // Handle the MACOSX_PACKAGE_LOCATION property on source files that
1107 // were not listed in one of the other lists.
1108 std::vector<cmSourceFile*> const& sources = this->GetSourceFiles();
1109 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
1110 si != sources.end(); ++si)
1112 cmSourceFile* sf = *si;
1113 if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
1115 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1116 if(flags.Type == cmTarget::SourceFileTypeNormal)
1118 flags.MacFolder = location;
1119 if(strcmp(location, "Resources") == 0)
1121 flags.Type = cmTarget::SourceFileTypeResource;
1123 else
1125 flags.Type = cmTarget::SourceFileTypeMacContent;
1132 //----------------------------------------------------------------------------
1133 void cmTarget::MergeLinkLibraries( cmMakefile& mf,
1134 const char *selfname,
1135 const LinkLibraryVectorType& libs )
1137 // Only add on libraries we haven't added on before.
1138 // Assumption: the global link libraries could only grow, never shrink
1139 LinkLibraryVectorType::const_iterator i = libs.begin();
1140 i += this->PrevLinkedLibraries.size();
1141 for( ; i != libs.end(); ++i )
1143 // We call this so that the dependencies get written to the cache
1144 this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
1146 this->PrevLinkedLibraries = libs;
1149 //----------------------------------------------------------------------------
1150 void cmTarget::AddLinkDirectory(const char* d)
1152 // Make sure we don't add unnecessary search directories.
1153 if(this->LinkDirectoriesEmmitted.insert(d).second)
1155 this->LinkDirectories.push_back(d);
1159 //----------------------------------------------------------------------------
1160 const std::vector<std::string>& cmTarget::GetLinkDirectories()
1162 return this->LinkDirectories;
1165 //----------------------------------------------------------------------------
1166 void cmTarget::ClearDependencyInformation( cmMakefile& mf,
1167 const char* target )
1169 // Clear the dependencies. The cache variable must exist iff we are
1170 // recording dependency information for this target.
1171 std::string depname = target;
1172 depname += "_LIB_DEPENDS";
1173 if (this->RecordDependencies)
1175 mf.AddCacheDefinition(depname.c_str(), "",
1176 "Dependencies for target", cmCacheManager::STATIC);
1178 else
1180 if (mf.GetDefinition( depname.c_str() ))
1182 std::string message = "Target ";
1183 message += target;
1184 message += " has dependency information when it shouldn't.\n";
1185 message += "Your cache is probably stale. Please remove the entry\n ";
1186 message += depname;
1187 message += "\nfrom the cache.";
1188 cmSystemTools::Error( message.c_str() );
1193 //----------------------------------------------------------------------------
1194 void cmTarget::AddLinkLibrary(const std::string& lib,
1195 LinkLibraryType llt)
1197 this->AddFramework(lib.c_str(), llt);
1198 cmTarget::LibraryID tmp;
1199 tmp.first = lib;
1200 tmp.second = llt;
1201 this->LinkLibraries.push_back(tmp);
1202 this->OriginalLinkLibraries.push_back(tmp);
1205 //----------------------------------------------------------------------------
1206 bool cmTarget::NameResolvesToFramework(const std::string& libname)
1208 return this->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
1209 NameResolvesToFramework(libname);
1212 //----------------------------------------------------------------------------
1213 bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
1215 (void)llt; // TODO: What is this?
1216 if(this->NameResolvesToFramework(libname.c_str()))
1218 std::string frameworkDir = libname;
1219 frameworkDir += "/../";
1220 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1221 std::vector<std::string>::iterator i =
1222 std::find(this->Frameworks.begin(),
1223 this->Frameworks.end(), frameworkDir);
1224 if(i == this->Frameworks.end())
1226 this->Frameworks.push_back(frameworkDir);
1228 return true;
1230 return false;
1233 //----------------------------------------------------------------------------
1234 void cmTarget::AddLinkLibrary(cmMakefile& mf,
1235 const char *target, const char* lib,
1236 LinkLibraryType llt)
1238 // Never add a self dependency, even if the user asks for it.
1239 if(strcmp( target, lib ) == 0)
1241 return;
1243 this->AddFramework(lib, llt);
1244 cmTarget::LibraryID tmp;
1245 tmp.first = lib;
1246 tmp.second = llt;
1247 this->LinkLibraries.push_back( tmp );
1248 this->OriginalLinkLibraries.push_back(tmp);
1250 // Add the explicit dependency information for this target. This is
1251 // simply a set of libraries separated by ";". There should always
1252 // be a trailing ";". These library names are not canonical, in that
1253 // they may be "-framework x", "-ly", "/path/libz.a", etc.
1254 // We shouldn't remove duplicates here because external libraries
1255 // may be purposefully duplicated to handle recursive dependencies,
1256 // and we removing one instance will break the link line. Duplicates
1257 // will be appropriately eliminated at emit time.
1258 if(this->RecordDependencies)
1260 std::string targetEntry = target;
1261 targetEntry += "_LIB_DEPENDS";
1262 std::string dependencies;
1263 const char* old_val = mf.GetDefinition( targetEntry.c_str() );
1264 if( old_val )
1266 dependencies += old_val;
1268 switch (llt)
1270 case cmTarget::GENERAL:
1271 dependencies += "general";
1272 break;
1273 case cmTarget::DEBUG:
1274 dependencies += "debug";
1275 break;
1276 case cmTarget::OPTIMIZED:
1277 dependencies += "optimized";
1278 break;
1280 dependencies += ";";
1281 dependencies += lib;
1282 dependencies += ";";
1283 mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
1284 "Dependencies for the target",
1285 cmCacheManager::STATIC );
1290 //----------------------------------------------------------------------------
1291 void
1292 cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
1294 // There are two key parts of the dependency analysis: (1)
1295 // determining the libraries in the link line, and (2) constructing
1296 // the dependency graph for those libraries.
1298 // The latter is done using the cache entries that record the
1299 // dependencies of each library.
1301 // The former is a more thorny issue, since it is not clear how to
1302 // determine if two libraries listed on the link line refer to the a
1303 // single library or not. For example, consider the link "libraries"
1304 // /usr/lib/libtiff.so -ltiff
1305 // Is this one library or two? The solution implemented here is the
1306 // simplest (and probably the only practical) one: two libraries are
1307 // the same if their "link strings" are identical. Thus, the two
1308 // libraries above are considered distinct. This also means that for
1309 // dependency analysis to be effective, the CMake user must specify
1310 // libraries build by his project without using any linker flags or
1311 // file extensions. That is,
1312 // LINK_LIBRARIES( One Two )
1313 // instead of
1314 // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
1315 // The former is probably what most users would do, but it never
1316 // hurts to document the assumptions. :-) Therefore, in the analysis
1317 // code, the "canonical name" of a library is simply its name as
1318 // given to a LINK_LIBRARIES command.
1320 // Also, we will leave the original link line intact; we will just add any
1321 // dependencies that were missing.
1323 // There is a problem with recursive external libraries
1324 // (i.e. libraries with no dependency information that are
1325 // recursively dependent). We must make sure that the we emit one of
1326 // the libraries twice to satisfy the recursion, but we shouldn't
1327 // emit it more times than necessary. In particular, we must make
1328 // sure that handling this improbable case doesn't cost us when
1329 // dealing with the common case of non-recursive libraries. The
1330 // solution is to assume that the recursion is satisfied at one node
1331 // of the dependency tree. To illustrate, assume libA and libB are
1332 // extrenal and mutually dependent. Suppose libX depends on
1333 // libA, and libY on libA and libX. Then
1334 // TARGET_LINK_LIBRARIES( Y X A B A )
1335 // TARGET_LINK_LIBRARIES( X A B A )
1336 // TARGET_LINK_LIBRARIES( Exec Y )
1337 // would result in "-lY -lX -lA -lB -lA". This is the correct way to
1338 // specify the dependencies, since the mutual dependency of A and B
1339 // is resolved *every time libA is specified*.
1341 // Something like
1342 // TARGET_LINK_LIBRARIES( Y X A B A )
1343 // TARGET_LINK_LIBRARIES( X A B )
1344 // TARGET_LINK_LIBRARIES( Exec Y )
1345 // would result in "-lY -lX -lA -lB", and the mutual dependency
1346 // information is lost. This is because in some case (Y), the mutual
1347 // dependency of A and B is listed, while in another other case (X),
1348 // it is not. Depending on which line actually emits A, the mutual
1349 // dependency may or may not be on the final link line. We can't
1350 // handle this pathalogical case cleanly without emitting extra
1351 // libraries for the normal cases. Besides, the dependency
1352 // information for X is wrong anyway: if we build an executable
1353 // depending on X alone, we would not have the mutual dependency on
1354 // A and B resolved.
1356 // IMPROVEMENTS:
1357 // -- The current algorithm will not always pick the "optimal" link line
1358 // when recursive dependencies are present. It will instead break the
1359 // cycles at an aribtrary point. The majority of projects won't have
1360 // cyclic dependencies, so this is probably not a big deal. Note that
1361 // the link line is always correct, just not necessary optimal.
1364 // Expand variables in link library names. This is for backwards
1365 // compatibility with very early CMake versions and should
1366 // eventually be removed. This code was moved here from the end of
1367 // old source list processing code which was called just before this
1368 // method.
1369 for(LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
1370 p != this->LinkLibraries.end(); ++p)
1372 this->Makefile->ExpandVariablesInString(p->first, true, true);
1376 typedef std::vector< std::string > LinkLine;
1378 // The dependency map.
1379 DependencyMap dep_map;
1381 // 1. Build the dependency graph
1383 for(LinkLibraryVectorType::reverse_iterator lib
1384 = this->LinkLibraries.rbegin();
1385 lib != this->LinkLibraries.rend(); ++lib)
1387 this->GatherDependencies( mf, *lib, dep_map);
1390 // 2. Remove any dependencies that are already satisfied in the original
1391 // link line.
1393 for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
1394 lib != this->LinkLibraries.end(); ++lib)
1396 for( LinkLibraryVectorType::iterator lib2 = lib;
1397 lib2 != this->LinkLibraries.end(); ++lib2)
1399 this->DeleteDependency( dep_map, *lib, *lib2);
1404 // 3. Create the new link line by simply emitting any dependencies that are
1405 // missing. Start from the back and keep adding.
1407 std::set<DependencyMap::key_type> done, visited;
1408 std::vector<DependencyMap::key_type> newLinkLibraries;
1409 for(LinkLibraryVectorType::reverse_iterator lib =
1410 this->LinkLibraries.rbegin();
1411 lib != this->LinkLibraries.rend(); ++lib)
1413 // skip zero size library entries, this may happen
1414 // if a variable expands to nothing.
1415 if (lib->first.size() != 0)
1417 this->Emit( *lib, dep_map, done, visited, newLinkLibraries );
1421 // 4. Add the new libraries to the link line.
1423 for( std::vector<DependencyMap::key_type>::reverse_iterator k =
1424 newLinkLibraries.rbegin();
1425 k != newLinkLibraries.rend(); ++k )
1427 // get the llt from the dep_map
1428 this->LinkLibraries.push_back( std::make_pair(k->first,k->second) );
1430 this->LinkLibrariesAnalyzed = true;
1433 //----------------------------------------------------------------------------
1434 void cmTarget::InsertDependency( DependencyMap& depMap,
1435 const LibraryID& lib,
1436 const LibraryID& dep)
1438 depMap[lib].push_back(dep);
1441 //----------------------------------------------------------------------------
1442 void cmTarget::DeleteDependency( DependencyMap& depMap,
1443 const LibraryID& lib,
1444 const LibraryID& dep)
1446 // Make sure there is an entry in the map for lib. If so, delete all
1447 // dependencies to dep. There may be repeated entries because of
1448 // external libraries that are specified multiple times.
1449 DependencyMap::iterator map_itr = depMap.find( lib );
1450 if( map_itr != depMap.end() )
1452 DependencyList& depList = map_itr->second;
1453 DependencyList::iterator itr;
1454 while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
1455 depList.end() )
1457 depList.erase( itr );
1462 //----------------------------------------------------------------------------
1463 void cmTarget::Emit(const LibraryID lib,
1464 const DependencyMap& dep_map,
1465 std::set<LibraryID>& emitted,
1466 std::set<LibraryID>& visited,
1467 DependencyList& link_line )
1469 // It's already been emitted
1470 if( emitted.find(lib) != emitted.end() )
1472 return;
1475 // Emit the dependencies only if this library node hasn't been
1476 // visited before. If it has, then we have a cycle. The recursion
1477 // that got us here should take care of everything.
1479 if( visited.insert(lib).second )
1481 if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
1483 const DependencyList& dep_on = dep_map.find( lib )->second;
1484 DependencyList::const_reverse_iterator i;
1486 // To cater for recursive external libraries, we must emit
1487 // duplicates on this link line *unless* they were emitted by
1488 // some other node, in which case we assume that the recursion
1489 // was resolved then. We making the simplifying assumption that
1490 // any duplicates on a single link line are on purpose, and must
1491 // be preserved.
1493 // This variable will keep track of the libraries that were
1494 // emitted directory from the current node, and not from a
1495 // recursive call. This way, if we come across a library that
1496 // has already been emitted, we repeat it iff it has been
1497 // emitted here.
1498 std::set<DependencyMap::key_type> emitted_here;
1499 for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
1501 if( emitted_here.find(*i) != emitted_here.end() )
1503 // a repeat. Must emit.
1504 emitted.insert(*i);
1505 link_line.push_back( *i );
1507 else
1509 // Emit only if no-one else has
1510 if( emitted.find(*i) == emitted.end() )
1512 // emit dependencies
1513 Emit( *i, dep_map, emitted, visited, link_line );
1514 // emit self
1515 emitted.insert(*i);
1516 emitted_here.insert(*i);
1517 link_line.push_back( *i );
1525 //----------------------------------------------------------------------------
1526 void cmTarget::GatherDependencies( const cmMakefile& mf,
1527 const LibraryID& lib,
1528 DependencyMap& dep_map)
1530 // If the library is already in the dependency map, then it has
1531 // already been fully processed.
1532 if( dep_map.find(lib) != dep_map.end() )
1534 return;
1537 const char* deps = mf.GetDefinition( (lib.first+"_LIB_DEPENDS").c_str() );
1538 if( deps && strcmp(deps,"") != 0 )
1540 // Make sure this library is in the map, even if it has an empty
1541 // set of dependencies. This distinguishes the case of explicitly
1542 // no dependencies with that of unspecified dependencies.
1543 dep_map[lib];
1545 // Parse the dependency information, which is a set of
1546 // type, library pairs separated by ";". There is always a trailing ";".
1547 cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
1548 std::string depline = deps;
1549 std::string::size_type start = 0;
1550 std::string::size_type end;
1551 end = depline.find( ";", start );
1552 while( end != std::string::npos )
1554 std::string l = depline.substr( start, end-start );
1555 if( l.size() != 0 )
1557 if (l == "debug")
1559 llt = cmTarget::DEBUG;
1561 else if (l == "optimized")
1563 llt = cmTarget::OPTIMIZED;
1565 else if (l == "general")
1567 llt = cmTarget::GENERAL;
1569 else
1571 LibraryID lib2(l,llt);
1572 this->InsertDependency( dep_map, lib, lib2);
1573 this->GatherDependencies( mf, lib2, dep_map);
1574 llt = cmTarget::GENERAL;
1577 start = end+1; // skip the ;
1578 end = depline.find( ";", start );
1580 // cannot depend on itself
1581 this->DeleteDependency( dep_map, lib, lib);
1585 //----------------------------------------------------------------------------
1586 void cmTarget::SetProperty(const char* prop, const char* value)
1588 if (!prop)
1590 return;
1592 if (!value)
1594 value = "NOTFOUND";
1597 this->Properties.SetProperty(prop, value, cmProperty::TARGET);
1599 // If imported information is being set, wipe out cached
1600 // information.
1601 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1603 this->ImportInfoMap.clear();
1607 //----------------------------------------------------------------------------
1608 void cmTarget::AppendProperty(const char* prop, const char* value)
1610 if (!prop)
1612 return;
1614 this->Properties.AppendProperty(prop, value, cmProperty::TARGET);
1616 // If imported information is being set, wipe out cached
1617 // information.
1618 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1620 this->ImportInfoMap.clear();
1624 //----------------------------------------------------------------------------
1625 void cmTarget::MarkAsImported()
1627 this->IsImportedTarget = true;
1630 //----------------------------------------------------------------------------
1631 const char* cmTarget::GetDirectory(const char* config, bool implib)
1633 if (this->IsImported())
1635 return this->ImportedGetDirectory(config, implib);
1637 else
1639 return this->NormalGetDirectory(config, implib);
1643 //----------------------------------------------------------------------------
1644 const char* cmTarget::ImportedGetDirectory(const char* config, bool implib)
1646 this->Directory =
1647 cmSystemTools::GetFilenamePath(
1648 this->ImportedGetFullPath(config, implib));
1649 return this->Directory.c_str();
1652 //----------------------------------------------------------------------------
1653 const char* cmTarget::NormalGetDirectory(const char* config, bool implib)
1655 if(config && *config)
1657 // Do not create the directory when config is given:
1658 this->Directory = this->GetAndCreateOutputDir(implib, true);
1659 // Add the configuration's subdirectory.
1660 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
1661 AppendDirectoryForConfig("/", config, "", this->Directory);
1662 return this->Directory.c_str();
1664 else
1666 return this->GetOutputDir(implib);
1670 //----------------------------------------------------------------------------
1671 const char* cmTarget::GetLocation(const char* config)
1673 if (this->IsImported())
1675 return this->ImportedGetLocation(config);
1677 else
1679 return this->NormalGetLocation(config);
1683 //----------------------------------------------------------------------------
1684 const char* cmTarget::ImportedGetLocation(const char* config)
1686 this->Location = this->ImportedGetFullPath(config, false);
1687 return this->Location.c_str();
1690 //----------------------------------------------------------------------------
1691 const char* cmTarget::NormalGetLocation(const char* config)
1693 this->Location = this->GetDirectory(config);
1694 if(!this->Location.empty())
1696 this->Location += "/";
1698 if(!config)
1700 // No specific configuration was given so it will not appear on
1701 // the result of GetDirectory. Add a name here to be replaced at
1702 // build time.
1703 const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
1704 if(cfgid && strcmp(cfgid, ".") != 0)
1706 this->Location += cfgid;
1707 this->Location += "/";
1710 this->Location += this->GetFullName(config, false);
1711 return this->Location.c_str();
1714 //----------------------------------------------------------------------------
1715 void cmTarget::GetTargetVersion(int& major, int& minor)
1717 // Set the default values.
1718 major = 0;
1719 minor = 0;
1721 // Look for a VERSION property.
1722 if(const char* version = this->GetProperty("VERSION"))
1724 // Try to parse the version number and store the results that were
1725 // successfully parsed.
1726 int parsed_major;
1727 int parsed_minor;
1728 switch(sscanf(version, "%d.%d", &parsed_major, &parsed_minor))
1730 case 2: minor = parsed_minor; // no break!
1731 case 1: major = parsed_major; // no break!
1732 default: break;
1737 //----------------------------------------------------------------------------
1738 const char *cmTarget::GetProperty(const char* prop)
1740 return this->GetProperty(prop, cmProperty::TARGET);
1743 //----------------------------------------------------------------------------
1744 void cmTarget::ComputeObjectFiles()
1746 if (this->IsImported())
1748 return;
1750 #if 0
1751 std::vector<std::string> dirs;
1752 this->Makefile->GetLocalGenerator()->
1753 GetTargetObjectFileDirectories(this,
1754 dirs);
1755 std::string objectFiles;
1756 std::string objExtensionLookup1 = "CMAKE_";
1757 std::string objExtensionLookup2 = "_OUTPUT_EXTENSION";
1759 for(std::vector<std::string>::iterator d = dirs.begin();
1760 d != dirs.end(); ++d)
1762 for(std::vector<cmSourceFile*>::iterator s = this->SourceFiles.begin();
1763 s != this->SourceFiles.end(); ++s)
1765 cmSourceFile* sf = *s;
1766 if(const char* lang = sf->GetLanguage())
1768 std::string lookupObj = objExtensionLookup1 + lang;
1769 lookupObj += objExtensionLookup2;
1770 const char* obj = this->Makefile->GetDefinition(lookupObj.c_str());
1771 if(obj)
1773 if(objectFiles.size())
1775 objectFiles += ";";
1777 std::string objFile = *d;
1778 objFile += "/";
1779 objFile += this->Makefile->GetLocalGenerator()->
1780 GetSourceObjectName(*sf);
1781 objFile += obj;
1782 objectFiles += objFile;
1787 this->SetProperty("OBJECT_FILES", objectFiles.c_str());
1788 #endif
1791 //----------------------------------------------------------------------------
1792 const char *cmTarget::GetProperty(const char* prop,
1793 cmProperty::ScopeType scope)
1795 if(!prop)
1797 return 0;
1800 // Watch for special "computed" properties that are dependent on
1801 // other properties or variables. Always recompute them.
1802 if(this->GetType() == cmTarget::EXECUTABLE ||
1803 this->GetType() == cmTarget::STATIC_LIBRARY ||
1804 this->GetType() == cmTarget::SHARED_LIBRARY ||
1805 this->GetType() == cmTarget::MODULE_LIBRARY)
1807 if(!this->IsImported() && strcmp(prop,"LOCATION") == 0)
1809 // Set the LOCATION property of the target. Note that this
1810 // cannot take into account the per-configuration name of the
1811 // target because the configuration type may not be known at
1812 // CMake time. It is now deprecated as described in the
1813 // documentation.
1814 this->SetProperty("LOCATION", this->GetLocation(0));
1817 // Support "LOCATION_<CONFIG>".
1818 if(strncmp(prop, "LOCATION_", 9) == 0)
1820 std::string configName = prop+9;
1821 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1823 else
1825 // Support "<CONFIG>_LOCATION" for compatiblity.
1826 int len = static_cast<int>(strlen(prop));
1827 if(len > 9 && strcmp(prop+len-9, "_LOCATION") == 0)
1829 std::string configName(prop, len-9);
1830 if(configName != "IMPORTED")
1832 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1838 if (strcmp(prop,"IMPORTED") == 0)
1840 return this->IsImported()?"TRUE":"FALSE";
1843 if(!strcmp(prop,"SOURCES"))
1845 cmOStringStream ss;
1846 const char* sep = "";
1847 for(std::vector<cmSourceFile*>::const_iterator
1848 i = this->SourceFiles.begin();
1849 i != this->SourceFiles.end(); ++i)
1851 // Separate from the previous list entries.
1852 ss << sep;
1853 sep = ";";
1855 // Construct what is known about this source file location.
1856 cmSourceFileLocation const& location = (*i)->GetLocation();
1857 std::string sname = location.GetDirectory();
1858 if(!sname.empty())
1860 sname += "/";
1862 sname += location.GetName();
1864 // Append this list entry.
1865 ss << sname;
1867 this->SetProperty("SOURCES", ss.str().c_str());
1870 // the type property returns what type the target is
1871 if (!strcmp(prop,"TYPE"))
1873 switch( this->GetType() )
1875 case cmTarget::STATIC_LIBRARY:
1876 return "STATIC_LIBRARY";
1877 // break; /* unreachable */
1878 case cmTarget::MODULE_LIBRARY:
1879 return "MODULE_LIBRARY";
1880 // break; /* unreachable */
1881 case cmTarget::SHARED_LIBRARY:
1882 return "SHARED_LIBRARY";
1883 // break; /* unreachable */
1884 case cmTarget::EXECUTABLE:
1885 return "EXECUTABLE";
1886 // break; /* unreachable */
1887 case cmTarget::UTILITY:
1888 return "UTILITY";
1889 // break; /* unreachable */
1890 case cmTarget::GLOBAL_TARGET:
1891 return "GLOBAL_TARGET";
1892 // break; /* unreachable */
1893 case cmTarget::INSTALL_FILES:
1894 return "INSTALL_FILES";
1895 // break; /* unreachable */
1896 case cmTarget::INSTALL_PROGRAMS:
1897 return "INSTALL_PROGRAMS";
1898 // break; /* unreachable */
1899 case cmTarget::INSTALL_DIRECTORY:
1900 return "INSTALL_DIRECTORY";
1901 // break; /* unreachable */
1903 return 0;
1905 bool chain = false;
1906 const char *retVal =
1907 this->Properties.GetPropertyValue(prop, scope, chain);
1908 if (chain)
1910 return this->Makefile->GetProperty(prop,scope);
1912 return retVal;
1915 //----------------------------------------------------------------------------
1916 bool cmTarget::GetPropertyAsBool(const char* prop)
1918 return cmSystemTools::IsOn(this->GetProperty(prop));
1921 //----------------------------------------------------------------------------
1922 const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
1924 if(this->GetProperty("HAS_CXX"))
1926 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
1928 const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
1929 if (linkerLang==0)
1931 // if the property has not yet been set, collect all languages in the
1932 // target and then find the language with the highest preference value
1933 std::set<cmStdString> languages;
1934 this->GetLanguages(languages);
1936 std::string linkerLangList; // only used for the error message
1937 int maxLinkerPref = 0;
1938 bool multiplePreferedLanguages = false;
1939 for(std::set<cmStdString>::const_iterator sit = languages.begin();
1940 sit != languages.end(); ++sit)
1942 int linkerPref = gg->GetLinkerPreference(sit->c_str());
1943 if ((linkerPref > maxLinkerPref) || (linkerLang==0))
1945 maxLinkerPref = linkerPref;
1946 linkerLang = sit->c_str();
1947 linkerLangList = *sit;
1948 multiplePreferedLanguages = false;
1950 else if (linkerPref == maxLinkerPref)
1952 linkerLangList += "; ";
1953 linkerLangList += *sit;
1954 multiplePreferedLanguages = true;
1958 if (linkerLang!=0)
1960 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", linkerLang);
1962 if (multiplePreferedLanguages)
1964 cmOStringStream err;
1965 err << "Error: Target " << this->Name << " contains multiple languages "
1966 << "with the highest linker preference (" << maxLinkerPref << "): "
1967 << linkerLangList << "\n"
1968 << "You must set the LINKER_LANGUAGE property for this target.";
1969 cmSystemTools::Error(err.str().c_str());
1972 return this->GetProperty("LINKER_LANGUAGE");
1975 //----------------------------------------------------------------------------
1976 const char* cmTarget::GetCreateRuleVariable()
1978 switch(this->GetType())
1980 case cmTarget::STATIC_LIBRARY:
1981 return "_CREATE_STATIC_LIBRARY";
1982 case cmTarget::SHARED_LIBRARY:
1983 return "_CREATE_SHARED_LIBRARY";
1984 case cmTarget::MODULE_LIBRARY:
1985 return "_CREATE_SHARED_MODULE";
1986 case cmTarget::EXECUTABLE:
1987 return "_LINK_EXECUTABLE";
1988 case cmTarget::UTILITY:
1989 case cmTarget::GLOBAL_TARGET:
1990 case cmTarget::INSTALL_FILES:
1991 case cmTarget::INSTALL_PROGRAMS:
1992 case cmTarget::INSTALL_DIRECTORY:
1993 break;
1995 return "";
1998 //----------------------------------------------------------------------------
1999 const char* cmTarget::GetSuffixVariableInternal(TargetType type,
2000 bool implib)
2002 switch(type)
2004 case cmTarget::STATIC_LIBRARY:
2005 return "CMAKE_STATIC_LIBRARY_SUFFIX";
2006 case cmTarget::SHARED_LIBRARY:
2007 return (implib
2008 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
2009 : "CMAKE_SHARED_LIBRARY_SUFFIX");
2010 case cmTarget::MODULE_LIBRARY:
2011 return (implib
2012 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
2013 : "CMAKE_SHARED_MODULE_SUFFIX");
2014 case cmTarget::EXECUTABLE:
2015 return (implib
2016 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
2017 : "CMAKE_EXECUTABLE_SUFFIX");
2018 case cmTarget::UTILITY:
2019 case cmTarget::GLOBAL_TARGET:
2020 case cmTarget::INSTALL_FILES:
2021 case cmTarget::INSTALL_PROGRAMS:
2022 case cmTarget::INSTALL_DIRECTORY:
2023 break;
2025 return "";
2029 //----------------------------------------------------------------------------
2030 const char* cmTarget::GetPrefixVariableInternal(TargetType type,
2031 bool implib)
2033 switch(type)
2035 case cmTarget::STATIC_LIBRARY:
2036 return "CMAKE_STATIC_LIBRARY_PREFIX";
2037 case cmTarget::SHARED_LIBRARY:
2038 return (implib
2039 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
2040 : "CMAKE_SHARED_LIBRARY_PREFIX");
2041 case cmTarget::MODULE_LIBRARY:
2042 return (implib
2043 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
2044 : "CMAKE_SHARED_MODULE_PREFIX");
2045 case cmTarget::EXECUTABLE:
2046 return (implib? "CMAKE_IMPORT_LIBRARY_PREFIX" : "");
2047 case cmTarget::UTILITY:
2048 case cmTarget::GLOBAL_TARGET:
2049 case cmTarget::INSTALL_FILES:
2050 case cmTarget::INSTALL_PROGRAMS:
2051 case cmTarget::INSTALL_DIRECTORY:
2052 break;
2054 return "";
2057 //----------------------------------------------------------------------------
2058 std::string cmTarget::GetPDBName(const char* config)
2060 std::string prefix;
2061 std::string base;
2062 std::string suffix;
2063 this->GetFullNameInternal(this->GetType(), config, false,
2064 prefix, base, suffix);
2065 return prefix+base+".pdb";
2068 //----------------------------------------------------------------------------
2069 std::string cmTarget::GetSOName(const char* config)
2071 if(this->IsImported())
2073 // Lookup the imported soname.
2074 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2076 if(info->NoSOName)
2078 // The imported library has no builtin soname so the name
2079 // searched at runtime will be just the filename.
2080 return cmSystemTools::GetFilenameName(info->Location);
2082 else
2084 // Use the soname given if any.
2085 return info->SOName;
2088 else
2090 return "";
2093 else
2095 // Compute the soname that will be built.
2096 std::string name;
2097 std::string soName;
2098 std::string realName;
2099 std::string impName;
2100 std::string pdbName;
2101 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2102 return soName;
2106 //----------------------------------------------------------------------------
2107 bool cmTarget::IsImportedSharedLibWithoutSOName(const char* config)
2109 if(this->IsImported() && this->GetType() == cmTarget::SHARED_LIBRARY)
2111 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2113 return info->NoSOName;
2116 return false;
2119 //----------------------------------------------------------------------------
2120 std::string cmTarget::NormalGetRealName(const char* config)
2122 // This should not be called for imported targets.
2123 // TODO: Split cmTarget into a class hierarchy to get compile-time
2124 // enforcement of the limited imported target API.
2125 if(this->IsImported())
2127 std::string msg = "NormalGetRealName called on imported target: ";
2128 msg += this->GetName();
2129 this->GetMakefile()->
2130 IssueMessage(cmake::INTERNAL_ERROR,
2131 msg.c_str());
2134 if(this->GetType() == cmTarget::EXECUTABLE)
2136 // Compute the real name that will be built.
2137 std::string name;
2138 std::string realName;
2139 std::string impName;
2140 std::string pdbName;
2141 this->GetExecutableNames(name, realName, impName, pdbName, config);
2142 return realName;
2144 else
2146 // Compute the real name that will be built.
2147 std::string name;
2148 std::string soName;
2149 std::string realName;
2150 std::string impName;
2151 std::string pdbName;
2152 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2153 return realName;
2157 //----------------------------------------------------------------------------
2158 std::string cmTarget::GetFullName(const char* config, bool implib)
2160 if(this->IsImported())
2162 return this->GetFullNameImported(config, implib);
2164 else
2166 return this->GetFullNameInternal(this->GetType(), config, implib);
2170 //----------------------------------------------------------------------------
2171 std::string cmTarget::GetFullNameImported(const char* config, bool implib)
2173 return cmSystemTools::GetFilenameName(
2174 this->ImportedGetFullPath(config, implib));
2177 //----------------------------------------------------------------------------
2178 void cmTarget::GetFullNameComponents(std::string& prefix, std::string& base,
2179 std::string& suffix, const char* config,
2180 bool implib)
2182 this->GetFullNameInternal(this->GetType(), config, implib,
2183 prefix, base, suffix);
2186 //----------------------------------------------------------------------------
2187 std::string cmTarget::GetFullPath(const char* config, bool implib,
2188 bool realname)
2190 if(this->IsImported())
2192 return this->ImportedGetFullPath(config, implib);
2194 else
2196 return this->NormalGetFullPath(config, implib, realname);
2200 //----------------------------------------------------------------------------
2201 std::string cmTarget::NormalGetFullPath(const char* config, bool implib,
2202 bool realname)
2204 // Start with the output directory for the target.
2205 std::string fpath = this->GetDirectory(config, implib);
2206 fpath += "/";
2208 // Add the full name of the target.
2209 if(implib)
2211 fpath += this->GetFullName(config, true);
2213 else if(realname)
2215 fpath += this->NormalGetRealName(config);
2217 else
2219 fpath += this->GetFullName(config, false);
2221 return fpath;
2224 //----------------------------------------------------------------------------
2225 std::string cmTarget::ImportedGetFullPath(const char* config, bool implib)
2227 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2229 if(implib)
2231 return info->ImportLibrary;
2233 else
2235 return info->Location;
2238 else
2240 std::string result = this->GetName();
2241 result += "-NOTFOUND";
2242 return result;
2246 //----------------------------------------------------------------------------
2247 std::string
2248 cmTarget::GetFullNameInternal(TargetType type, const char* config,
2249 bool implib)
2251 std::string prefix;
2252 std::string base;
2253 std::string suffix;
2254 this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
2255 return prefix+base+suffix;
2258 //----------------------------------------------------------------------------
2259 void cmTarget::GetFullNameInternal(TargetType type,
2260 const char* config,
2261 bool implib,
2262 std::string& outPrefix,
2263 std::string& outBase,
2264 std::string& outSuffix)
2266 // Use just the target name for non-main target types.
2267 if(type != cmTarget::STATIC_LIBRARY &&
2268 type != cmTarget::SHARED_LIBRARY &&
2269 type != cmTarget::MODULE_LIBRARY &&
2270 type != cmTarget::EXECUTABLE)
2272 outPrefix = "";
2273 outBase = this->GetName();
2274 outSuffix = "";
2275 return;
2278 // Return an empty name for the import library if this platform
2279 // does not support import libraries.
2280 if(implib &&
2281 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2283 outPrefix = "";
2284 outBase = "";
2285 outSuffix = "";
2286 return;
2289 // The implib option is only allowed for shared libraries, module
2290 // libraries, and executables.
2291 if(type != cmTarget::SHARED_LIBRARY &&
2292 type != cmTarget::MODULE_LIBRARY &&
2293 type != cmTarget::EXECUTABLE)
2295 implib = false;
2298 // Compute the full name for main target types.
2299 const char* targetPrefix = (implib
2300 ? this->GetProperty("IMPORT_PREFIX")
2301 : this->GetProperty("PREFIX"));
2302 const char* targetSuffix = (implib
2303 ? this->GetProperty("IMPORT_SUFFIX")
2304 : this->GetProperty("SUFFIX"));
2305 const char* configPostfix = 0;
2306 if(config && *config)
2308 std::string configProp = cmSystemTools::UpperCase(config);
2309 configProp += "_POSTFIX";
2310 configPostfix = this->GetProperty(configProp.c_str());
2312 const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
2313 const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
2314 const char* ll =
2315 this->GetLinkerLanguage(
2316 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2317 // first try language specific suffix
2318 if(ll)
2320 if(!targetSuffix && suffixVar && *suffixVar)
2322 std::string langSuff = suffixVar + std::string("_") + ll;
2323 targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
2325 if(!targetPrefix && prefixVar && *prefixVar)
2327 std::string langPrefix = prefixVar + std::string("_") + ll;
2328 targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
2332 // if there is no prefix on the target use the cmake definition
2333 if(!targetPrefix && prefixVar)
2335 targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
2337 // if there is no suffix on the target use the cmake definition
2338 if(!targetSuffix && suffixVar)
2340 targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
2343 // frameworks do not have a prefix or a suffix
2344 if(this->IsFrameworkOnApple())
2346 targetPrefix = 0;
2347 targetSuffix = 0;
2350 // Begin the final name with the prefix.
2351 outPrefix = targetPrefix?targetPrefix:"";
2353 // Append the target name or property-specified name.
2354 const char* outName = 0;
2355 if(config && *config)
2357 std::string configProp = cmSystemTools::UpperCase(config);
2358 configProp += "_OUTPUT_NAME";
2359 outName = this->GetProperty(configProp.c_str());
2361 if(!outName)
2363 outName = this->GetProperty("OUTPUT_NAME");
2365 if(outName)
2367 outBase = outName;
2369 else
2371 outBase = this->GetName();
2374 // Append the per-configuration postfix.
2375 outBase += configPostfix?configPostfix:"";
2377 // Name shared libraries with their version number on some platforms.
2378 if(const char* version = this->GetProperty("VERSION"))
2380 if(type == cmTarget::SHARED_LIBRARY && !implib &&
2381 this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION"))
2383 outBase += "-";
2384 outBase += version;
2388 // Append the suffix.
2389 outSuffix = targetSuffix?targetSuffix:"";
2392 //----------------------------------------------------------------------------
2393 void cmTarget::GetLibraryNames(std::string& name,
2394 std::string& soName,
2395 std::string& realName,
2396 std::string& impName,
2397 std::string& pdbName,
2398 const char* config)
2400 // Get the names based on the real type of the library.
2401 this->GetLibraryNamesInternal(name, soName, realName, impName, pdbName,
2402 this->GetType(), config);
2405 //----------------------------------------------------------------------------
2406 void cmTarget::GetLibraryCleanNames(std::string& staticName,
2407 std::string& sharedName,
2408 std::string& sharedSOName,
2409 std::string& sharedRealName,
2410 std::string& importName,
2411 std::string& pdbName,
2412 const char* config)
2414 // Get the name as if this were a static library.
2415 std::string soName;
2416 std::string realName;
2417 std::string impName;
2418 this->GetLibraryNamesInternal(staticName, soName, realName, impName,
2419 pdbName, cmTarget::STATIC_LIBRARY, config);
2421 // Get the names as if this were a shared library.
2422 if(this->GetType() == cmTarget::STATIC_LIBRARY)
2424 // Since the real type is static then the user either specified
2425 // STATIC or did not specify a type. In the former case the
2426 // shared library will never be present. In the latter case the
2427 // type will never be MODULE. Either way the only names that
2428 // might have to be cleaned are the shared library names.
2429 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2430 importName, pdbName,
2431 cmTarget::SHARED_LIBRARY, config);
2433 else
2435 // Use the name of the real type of the library (shared or module).
2436 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2437 importName, pdbName, this->GetType(),
2438 config);
2442 //----------------------------------------------------------------------------
2443 void cmTarget::GetLibraryNamesInternal(std::string& name,
2444 std::string& soName,
2445 std::string& realName,
2446 std::string& impName,
2447 std::string& pdbName,
2448 TargetType type,
2449 const char* config)
2451 // This should not be called for imported targets.
2452 // TODO: Split cmTarget into a class hierarchy to get compile-time
2453 // enforcement of the limited imported target API.
2454 if(this->IsImported())
2456 std::string msg = "GetLibraryNamesInternal called on imported target: ";
2457 msg += this->GetName();
2458 this->Makefile->IssueMessage(cmake::INTERNAL_ERROR,
2459 msg.c_str());
2460 return;
2463 // Construct the name of the soname flag variable for this language.
2464 const char* ll =
2465 this->GetLinkerLanguage(
2466 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2467 std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
2468 if(ll)
2470 sonameFlag += "_";
2471 sonameFlag += ll;
2473 sonameFlag += "_FLAG";
2475 // Check for library version properties.
2476 const char* version = this->GetProperty("VERSION");
2477 const char* soversion = this->GetProperty("SOVERSION");
2478 if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
2479 !this->Makefile->GetDefinition(sonameFlag.c_str()))
2481 // Versioning is supported only for shared libraries and modules,
2482 // and then only when the platform supports an soname flag.
2483 version = 0;
2484 soversion = 0;
2486 if(version && !soversion)
2488 // The soversion must be set if the library version is set. Use
2489 // the library version as the soversion.
2490 soversion = version;
2493 // Get the components of the library name.
2494 std::string prefix;
2495 std::string base;
2496 std::string suffix;
2497 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2499 // The library name.
2500 name = prefix+base+suffix;
2502 // The library's soname.
2503 #if defined(__APPLE__)
2504 soName = prefix+base;
2505 #else
2506 soName = name;
2507 #endif
2508 if(soversion)
2510 soName += ".";
2511 soName += soversion;
2513 #if defined(__APPLE__)
2514 soName += suffix;
2515 #endif
2517 // The library's real name on disk.
2518 #if defined(__APPLE__)
2519 realName = prefix+base;
2520 #else
2521 realName = name;
2522 #endif
2523 if(version)
2525 realName += ".";
2526 realName += version;
2528 else if(soversion)
2530 realName += ".";
2531 realName += soversion;
2533 #if defined(__APPLE__)
2534 realName += suffix;
2535 #endif
2537 // The import library name.
2538 if(type == cmTarget::SHARED_LIBRARY ||
2539 type == cmTarget::MODULE_LIBRARY)
2541 impName = this->GetFullNameInternal(type, config, true);
2543 else
2545 impName = "";
2548 // The program database file name.
2549 pdbName = prefix+base+".pdb";
2552 //----------------------------------------------------------------------------
2553 void cmTarget::GetExecutableNames(std::string& name,
2554 std::string& realName,
2555 std::string& impName,
2556 std::string& pdbName,
2557 const char* config)
2559 // Get the names based on the real type of the executable.
2560 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2561 this->GetType(), config);
2564 //----------------------------------------------------------------------------
2565 void cmTarget::GetExecutableCleanNames(std::string& name,
2566 std::string& realName,
2567 std::string& impName,
2568 std::string& pdbName,
2569 const char* config)
2571 // Get the name and versioned name of this executable.
2572 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2573 cmTarget::EXECUTABLE, config);
2576 //----------------------------------------------------------------------------
2577 void cmTarget::GetExecutableNamesInternal(std::string& name,
2578 std::string& realName,
2579 std::string& impName,
2580 std::string& pdbName,
2581 TargetType type,
2582 const char* config)
2584 // This should not be called for imported targets.
2585 // TODO: Split cmTarget into a class hierarchy to get compile-time
2586 // enforcement of the limited imported target API.
2587 if(this->IsImported())
2589 std::string msg = "GetExecutableNamesInternal called on imported target: ";
2590 msg += this->GetName();
2591 this->GetMakefile()->
2592 IssueMessage(cmake::INTERNAL_ERROR,
2593 msg.c_str());
2596 // This versioning is supported only for executables and then only
2597 // when the platform supports symbolic links.
2598 #if defined(_WIN32) && !defined(__CYGWIN__)
2599 const char* version = 0;
2600 #else
2601 // Check for executable version properties.
2602 const char* version = this->GetProperty("VERSION");
2603 if(type != cmTarget::EXECUTABLE || this->Makefile->IsOn("XCODE"))
2605 version = 0;
2607 #endif
2609 // Get the components of the executable name.
2610 std::string prefix;
2611 std::string base;
2612 std::string suffix;
2613 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2615 // The executable name.
2616 name = prefix+base+suffix;
2618 // The executable's real name on disk.
2619 #if defined(__CYGWIN__)
2620 realName = prefix+base;
2621 #else
2622 realName = name;
2623 #endif
2624 if(version)
2626 realName += "-";
2627 realName += version;
2629 #if defined(__CYGWIN__)
2630 realName += suffix;
2631 #endif
2633 // The import library name.
2634 impName = this->GetFullNameInternal(type, config, true);
2636 // The program database file name.
2637 pdbName = prefix+base+".pdb";
2640 //----------------------------------------------------------------------------
2641 void cmTarget::GenerateTargetManifest(const char* config)
2643 cmMakefile* mf = this->Makefile;
2644 cmLocalGenerator* lg = mf->GetLocalGenerator();
2645 cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2647 // Get the names.
2648 std::string name;
2649 std::string soName;
2650 std::string realName;
2651 std::string impName;
2652 std::string pdbName;
2653 if(this->GetType() == cmTarget::EXECUTABLE)
2655 this->GetExecutableNames(name, realName, impName, pdbName, config);
2657 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2658 this->GetType() == cmTarget::SHARED_LIBRARY ||
2659 this->GetType() == cmTarget::MODULE_LIBRARY)
2661 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2663 else
2665 return;
2668 // Get the directory.
2669 std::string dir = this->GetDirectory(config, false);
2671 // Add each name.
2672 std::string f;
2673 if(!name.empty())
2675 f = dir;
2676 f += "/";
2677 f += name;
2678 gg->AddToManifest(config? config:"", f);
2680 if(!soName.empty())
2682 f = dir;
2683 f += "/";
2684 f += soName;
2685 gg->AddToManifest(config? config:"", f);
2687 if(!realName.empty())
2689 f = dir;
2690 f += "/";
2691 f += realName;
2692 gg->AddToManifest(config? config:"", f);
2694 if(!pdbName.empty())
2696 f = dir;
2697 f += "/";
2698 f += pdbName;
2699 gg->AddToManifest(config? config:"", f);
2701 if(!impName.empty())
2703 f = this->GetDirectory(config, true);
2704 f += "/";
2705 f += impName;
2706 gg->AddToManifest(config? config:"", f);
2710 //----------------------------------------------------------------------------
2711 void cmTarget::SetPropertyDefault(const char* property,
2712 const char* default_value)
2714 // Compute the name of the variable holding the default value.
2715 std::string var = "CMAKE_";
2716 var += property;
2718 if(const char* value = this->Makefile->GetDefinition(var.c_str()))
2720 this->SetProperty(property, value);
2722 else if(default_value)
2724 this->SetProperty(property, default_value);
2728 //----------------------------------------------------------------------------
2729 bool cmTarget::HaveBuildTreeRPATH()
2731 return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
2732 !this->LinkLibraries.empty());
2735 //----------------------------------------------------------------------------
2736 bool cmTarget::HaveInstallTreeRPATH()
2738 const char* install_rpath = this->GetProperty("INSTALL_RPATH");
2739 return install_rpath && *install_rpath;
2742 //----------------------------------------------------------------------------
2743 bool cmTarget::NeedRelinkBeforeInstall()
2745 // Only executables and shared libraries can have an rpath and may
2746 // need relinking.
2747 if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
2748 this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
2749 this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
2751 return false;
2754 // If there is no install location this target will not be installed
2755 // and therefore does not need relinking.
2756 if(!this->GetHaveInstallRule())
2758 return false;
2761 // If skipping all rpaths completely then no relinking is needed.
2762 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
2764 return false;
2767 // If building with the install-tree rpath no relinking is needed.
2768 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2770 return false;
2773 // If chrpath is going to be used no relinking is needed.
2774 if(this->IsChrpathUsed())
2776 return false;
2779 // Check for rpath support on this platform.
2780 if(const char* ll = this->GetLinkerLanguage(
2781 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2783 std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2784 flagVar += ll;
2785 flagVar += "_FLAG";
2786 if(!this->Makefile->IsSet(flagVar.c_str()))
2788 // There is no rpath support on this platform so nothing needs
2789 // relinking.
2790 return false;
2793 else
2795 // No linker language is known. This error will be reported by
2796 // other code.
2797 return false;
2800 // If either a build or install tree rpath is set then the rpath
2801 // will likely change between the build tree and install tree and
2802 // this target must be relinked.
2803 return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
2806 //----------------------------------------------------------------------------
2807 std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
2809 // If building directly for installation then the build tree install_name
2810 // is the same as the install tree.
2811 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2813 return GetInstallNameDirForInstallTree(config);
2816 // Use the build tree directory for the target.
2817 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2818 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2819 !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
2821 std::string dir = this->GetDirectory(config);
2822 dir += "/";
2823 return dir;
2825 else
2827 return "";
2831 //----------------------------------------------------------------------------
2832 std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
2834 // Lookup the target property.
2835 const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
2836 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2837 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2838 install_name_dir && *install_name_dir)
2840 std::string dir = install_name_dir;
2841 dir += "/";
2842 return dir;
2844 else
2846 return "";
2850 //----------------------------------------------------------------------------
2851 const char* cmTarget::GetAndCreateOutputDir(bool implib, bool create)
2853 // The implib option is only allowed for shared libraries, module
2854 // libraries, and executables.
2855 if(this->GetType() != cmTarget::SHARED_LIBRARY &&
2856 this->GetType() != cmTarget::MODULE_LIBRARY &&
2857 this->GetType() != cmTarget::EXECUTABLE)
2859 implib = false;
2862 // Sanity check. Only generators on platforms supporting import
2863 // libraries should be asking for the import library output
2864 // directory.
2865 if(implib &&
2866 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2868 std::string msg = "GetAndCreateOutputDir, imlib set but there is no "
2869 "CMAKE_IMPORT_LIBRARY_SUFFIX for target: ";
2870 msg += this->GetName();
2871 this->GetMakefile()->
2872 IssueMessage(cmake::INTERNAL_ERROR,
2873 msg.c_str());
2875 if(implib && !this->DLLPlatform)
2877 std::string msg = "implib set for platform that does not "
2878 " support DLL's for target: ";
2879 msg += this->GetName();
2880 this->GetMakefile()->
2881 IssueMessage(cmake::INTERNAL_ERROR,
2882 msg.c_str());
2885 // Select whether we are constructing the directory for the main
2886 // target or the import library.
2887 std::string& out = implib? this->OutputDirImplib : this->OutputDir;
2889 if(out.empty())
2891 // Look for a target property defining the target output directory
2892 // based on the target type.
2893 const char* propertyName = 0;
2894 switch(this->GetType())
2896 case cmTarget::SHARED_LIBRARY:
2898 // For non-DLL platforms shared libraries are treated as
2899 // library targets. For DLL platforms the DLL part of a
2900 // shared library is treated as a runtime target and the
2901 // corresponding import library is treated as an archive
2902 // target.
2903 if(this->DLLPlatform)
2905 if(implib)
2907 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2909 else
2911 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2914 else
2916 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2918 } break;
2919 case cmTarget::STATIC_LIBRARY:
2921 // Static libraries are always treated as archive targets.
2922 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2923 } break;
2924 case cmTarget::MODULE_LIBRARY:
2926 // Module libraries are always treated as library targets.
2927 // Module import libraries are treated as archive targets.
2928 if(implib)
2930 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2932 else
2934 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2936 } break;
2937 case cmTarget::EXECUTABLE:
2939 // Executables are always treated as runtime targets.
2940 // Executable import libraries are treated as archive targets.
2941 if(implib)
2943 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2945 else
2947 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2949 } break;
2950 default: break;
2953 // Select an output directory.
2954 if(const char* outdir = this->GetProperty(propertyName))
2956 // Use the user-specified output directory.
2957 out = outdir;
2959 else if(this->GetType() == cmTarget::EXECUTABLE)
2961 // Lookup the output path for executables.
2962 out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
2964 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2965 this->GetType() == cmTarget::SHARED_LIBRARY ||
2966 this->GetType() == cmTarget::MODULE_LIBRARY)
2968 // Lookup the output path for libraries.
2969 out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
2971 if(out.empty())
2973 // Default to the current output directory.
2974 out = ".";
2976 // Convert the output path to a full path in case it is
2977 // specified as a relative path. Treat a relative path as
2978 // relative to the current output directory for this makefile.
2979 out =
2980 cmSystemTools::CollapseFullPath
2981 (out.c_str(), this->Makefile->GetStartOutputDirectory());
2983 // TODO: Make AppBundle and Framework directory computation in
2984 // target consistent. Why do we add the .framework part here for
2985 // frameworks but not the .app part for bundles? We should
2986 // probably not add it for either.
2987 if(this->IsFrameworkOnApple())
2989 out += "/";
2990 out += this->GetFullName(0, implib);
2991 out += ".framework";
2994 // Optionally make sure the output path exists on disk.
2995 if(create)
2997 if(!cmSystemTools::MakeDirectory(out.c_str()))
2999 cmSystemTools::Error("Error failed to create output directory: ",
3000 out.c_str());
3005 return out.c_str();
3008 //----------------------------------------------------------------------------
3009 const char* cmTarget::GetOutputDir(bool implib)
3011 return this->GetAndCreateOutputDir(implib, true);
3014 //----------------------------------------------------------------------------
3015 const char* cmTarget::GetExportMacro()
3017 // Define the symbol for targets that export symbols.
3018 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3019 this->GetType() == cmTarget::MODULE_LIBRARY ||
3020 this->IsExecutableWithExports())
3022 if(const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL"))
3024 this->ExportMacro = custom_export_name;
3026 else
3028 std::string in = this->GetName();
3029 in += "_EXPORTS";
3030 this->ExportMacro = cmSystemTools::MakeCindentifier(in.c_str());
3032 return this->ExportMacro.c_str();
3034 else
3036 return 0;
3040 //----------------------------------------------------------------------------
3041 void cmTarget::GetLanguages(std::set<cmStdString>& languages) const
3043 for(std::vector<cmSourceFile*>::const_iterator
3044 i = this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
3046 if(const char* lang = (*i)->GetLanguage())
3048 languages.insert(lang);
3053 //----------------------------------------------------------------------------
3054 bool cmTarget::IsChrpathUsed()
3056 #if defined(CMAKE_USE_ELF_PARSER)
3057 // Only certain target types have an rpath.
3058 if(!(this->GetType() == cmTarget::SHARED_LIBRARY ||
3059 this->GetType() == cmTarget::MODULE_LIBRARY ||
3060 this->GetType() == cmTarget::EXECUTABLE))
3062 return false;
3065 // If the target will not be installed we do not need to change its
3066 // rpath.
3067 if(!this->GetHaveInstallRule())
3069 return false;
3072 // Skip chrpath if skipping rpath altogether.
3073 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
3075 return false;
3078 // Skip chrpath if it does not need to be changed at install time.
3079 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
3081 return false;
3084 // Allow the user to disable builtin chrpath explicitly.
3085 if(this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH"))
3087 return false;
3090 // Enable if the rpath flag uses a separator and the target uses ELF
3091 // binaries.
3092 if(const char* ll = this->GetLinkerLanguage(
3093 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
3095 std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
3096 sepVar += ll;
3097 sepVar += "_FLAG_SEP";
3098 const char* sep = this->Makefile->GetDefinition(sepVar.c_str());
3099 if(sep && *sep)
3101 // TODO: Add ELF check to ABI detection and get rid of
3102 // CMAKE_EXECUTABLE_FORMAT.
3103 if(const char* fmt =
3104 this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT"))
3106 return strcmp(fmt, "ELF") == 0;
3110 #endif
3111 return false;
3114 //----------------------------------------------------------------------------
3115 cmTarget::ImportInfo const*
3116 cmTarget::GetImportInfo(const char* config)
3118 // There is no imported information for non-imported targets.
3119 if(!this->IsImported())
3121 return 0;
3124 // Lookup/compute/cache the import information for this
3125 // configuration.
3126 std::string config_upper;
3127 if(config && *config)
3129 config_upper = cmSystemTools::UpperCase(config);
3131 else
3133 config_upper = "NOCONFIG";
3135 ImportInfoMapType::const_iterator i =
3136 this->ImportInfoMap.find(config_upper);
3137 if(i == this->ImportInfoMap.end())
3139 ImportInfo info;
3140 this->ComputeImportInfo(config_upper, info);
3141 ImportInfoMapType::value_type entry(config_upper, info);
3142 i = this->ImportInfoMap.insert(entry).first;
3145 // If the location is empty then the target is not available for
3146 // this configuration.
3147 if(i->second.Location.empty())
3149 return 0;
3152 // Return the import information.
3153 return &i->second;
3156 //----------------------------------------------------------------------------
3157 void cmTarget::ComputeImportInfo(std::string const& desired_config,
3158 ImportInfo& info)
3160 // This method finds information about an imported target from its
3161 // properties. The "IMPORTED_" namespace is reserved for properties
3162 // defined by the project exporting the target.
3164 // Initialize members.
3165 info.NoSOName = false;
3167 // Track the configuration-specific property suffix.
3168 std::string suffix = "_";
3169 suffix += desired_config;
3171 // Look for a mapping from the current project's configuration to
3172 // the imported project's configuration.
3173 std::vector<std::string> mappedConfigs;
3175 std::string mapProp = "MAP_IMPORTED_CONFIG_";
3176 mapProp += desired_config;
3177 if(const char* mapValue = this->GetProperty(mapProp.c_str()))
3179 cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
3183 // If a mapping was found, check its configurations.
3184 const char* loc = 0;
3185 for(std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
3186 !loc && mci != mappedConfigs.end(); ++mci)
3188 // Look for this configuration.
3189 std::string mcUpper = cmSystemTools::UpperCase(mci->c_str());
3190 std::string locProp = "IMPORTED_LOCATION_";
3191 locProp += mcUpper;
3192 loc = this->GetProperty(locProp.c_str());
3194 // If it was found, use it for all properties below.
3195 if(loc)
3197 suffix = "_";
3198 suffix += mcUpper;
3202 // If we needed to find one of the mapped configurations but did not
3203 // then the target is not found. The project does not want any
3204 // other configuration.
3205 if(!mappedConfigs.empty() && !loc)
3207 return;
3210 // If we have not yet found it then there are no mapped
3211 // configurations. Look for an exact-match.
3212 if(!loc)
3214 std::string locProp = "IMPORTED_LOCATION";
3215 locProp += suffix;
3216 loc = this->GetProperty(locProp.c_str());
3219 // If we have not yet found it then there are no mapped
3220 // configurations and no exact match.
3221 if(!loc)
3223 // The suffix computed above is not useful.
3224 suffix = "";
3226 // Look for a configuration-less location. This may be set by
3227 // manually-written code.
3228 loc = this->GetProperty("IMPORTED_LOCATION");
3231 // If we have not yet found it then the project is willing to try
3232 // any available configuration.
3233 if(!loc)
3235 std::vector<std::string> availableConfigs;
3236 if(const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS"))
3238 cmSystemTools::ExpandListArgument(iconfigs, availableConfigs);
3240 for(std::vector<std::string>::const_iterator
3241 aci = availableConfigs.begin();
3242 !loc && aci != availableConfigs.end(); ++aci)
3244 suffix = "_";
3245 suffix += cmSystemTools::UpperCase(availableConfigs[0]);
3246 std::string locProp = "IMPORTED_LOCATION";
3247 locProp += suffix;
3248 loc = this->GetProperty(locProp.c_str());
3252 // If we have not yet found it then the target is not available.
3253 if(!loc)
3255 return;
3258 // A provided configuration has been chosen. Load the
3259 // configuration's properties.
3260 info.Location = loc;
3262 // Get the soname.
3263 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3265 std::string soProp = "IMPORTED_SONAME";
3266 soProp += suffix;
3267 if(const char* config_soname = this->GetProperty(soProp.c_str()))
3269 info.SOName = config_soname;
3271 else if(const char* soname = this->GetProperty("IMPORTED_SONAME"))
3273 info.SOName = soname;
3277 // Get the "no-soname" mark.
3278 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3280 std::string soProp = "IMPORTED_NO_SONAME";
3281 soProp += suffix;
3282 if(const char* config_no_soname = this->GetProperty(soProp.c_str()))
3284 info.NoSOName = cmSystemTools::IsOn(config_no_soname);
3286 else if(const char* no_soname = this->GetProperty("IMPORTED_NO_SONAME"))
3288 info.NoSOName = cmSystemTools::IsOn(no_soname);
3292 // Get the import library.
3293 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3294 this->IsExecutableWithExports())
3296 std::string impProp = "IMPORTED_IMPLIB";
3297 impProp += suffix;
3298 if(const char* config_implib = this->GetProperty(impProp.c_str()))
3300 info.ImportLibrary = config_implib;
3302 else if(const char* implib = this->GetProperty("IMPORTED_IMPLIB"))
3304 info.ImportLibrary = implib;
3308 // Get the link interface.
3310 std::string linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
3311 linkProp += suffix;
3312 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3314 cmSystemTools::ExpandListArgument(config_libs,
3315 info.LinkInterface.Libraries);
3317 else if(const char* libs =
3318 this->GetProperty("IMPORTED_LINK_INTERFACE_LIBRARIES"))
3320 cmSystemTools::ExpandListArgument(libs,
3321 info.LinkInterface.Libraries);
3325 // Get the link dependencies.
3327 std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
3328 linkProp += suffix;
3329 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3331 cmSystemTools::ExpandListArgument(config_libs,
3332 info.LinkInterface.SharedDeps);
3334 else if(const char* libs =
3335 this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES"))
3337 cmSystemTools::ExpandListArgument(libs, info.LinkInterface.SharedDeps);
3342 //----------------------------------------------------------------------------
3343 cmTargetLinkInterface const* cmTarget::GetLinkInterface(const char* config)
3345 // Imported targets have their own link interface.
3346 if(this->IsImported())
3348 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
3350 return &info->LinkInterface;
3352 return 0;
3355 // Link interfaces are supported only for shared libraries and
3356 // executables that export symbols.
3357 if((this->GetType() != cmTarget::SHARED_LIBRARY &&
3358 !this->IsExecutableWithExports()))
3360 return 0;
3363 // Lookup any existing link interface for this configuration.
3364 std::map<cmStdString, cmTargetLinkInterface*>::iterator
3365 i = this->LinkInterface.find(config?config:"");
3366 if(i == this->LinkInterface.end())
3368 // Compute the link interface for this configuration.
3369 cmTargetLinkInterface* iface = this->ComputeLinkInterface(config);
3371 // Store the information for this configuration.
3372 std::map<cmStdString, cmTargetLinkInterface*>::value_type
3373 entry(config?config:"", iface);
3374 i = this->LinkInterface.insert(entry).first;
3377 return i->second;
3380 //----------------------------------------------------------------------------
3381 cmTargetLinkInterface* cmTarget::ComputeLinkInterface(const char* config)
3383 // Construct the property name suffix for this configuration.
3384 std::string suffix = "_";
3385 if(config && *config)
3387 suffix += cmSystemTools::UpperCase(config);
3389 else
3391 suffix += "NOCONFIG";
3394 // Lookup the link interface libraries.
3395 const char* libs = 0;
3397 // Lookup the per-configuration property.
3398 std::string propName = "LINK_INTERFACE_LIBRARIES";
3399 propName += suffix;
3400 libs = this->GetProperty(propName.c_str());
3402 // If not set, try the generic property.
3403 if(!libs)
3405 libs = this->GetProperty("LINK_INTERFACE_LIBRARIES");
3409 // If still not set, there is no link interface.
3410 if(!libs)
3412 return 0;
3415 // Allocate the interface.
3416 cmTargetLinkInterface* iface = new cmTargetLinkInterface;
3417 if(!iface)
3419 return 0;
3422 // Expand the list of libraries in the interface.
3423 cmSystemTools::ExpandListArgument(libs, iface->Libraries);
3425 // Now we need to construct a list of shared library dependencies
3426 // not included in the interface.
3427 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3429 // Use a set to keep track of what libraries have been emitted to
3430 // either list.
3431 std::set<cmStdString> emitted;
3432 for(std::vector<std::string>::const_iterator
3433 li = iface->Libraries.begin();
3434 li != iface->Libraries.end(); ++li)
3436 emitted.insert(*li);
3439 // Compute which library configuration to link.
3440 cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
3441 if(config && cmSystemTools::UpperCase(config) == "DEBUG")
3443 linkType = cmTarget::DEBUG;
3446 // Construct the list of libs linked for this configuration.
3447 cmTarget::LinkLibraryVectorType const& llibs =
3448 this->GetOriginalLinkLibraries();
3449 for(cmTarget::LinkLibraryVectorType::const_iterator li = llibs.begin();
3450 li != llibs.end(); ++li)
3452 // Skip entries that will resolve to the target itself, are empty,
3453 // or are not meant for this configuration.
3454 if(li->first == this->GetName() || li->first.empty() ||
3455 !(li->second == cmTarget::GENERAL || li->second == linkType))
3457 continue;
3460 // Skip entries that have already been emitted into either list.
3461 if(!emitted.insert(li->first).second)
3463 continue;
3466 // Add this entry if it is a shared library.
3467 if(cmTarget* tgt = this->Makefile->FindTargetToUse(li->first.c_str()))
3469 if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
3471 iface->SharedDeps.push_back(li->first);
3474 else
3476 // TODO: Recognize shared library file names. Perhaps this
3477 // should be moved to cmComputeLinkInformation, but that creates
3478 // a chicken-and-egg problem since this list is needed for its
3479 // construction.
3484 // Return the completed interface.
3485 return iface;
3488 //----------------------------------------------------------------------------
3489 cmComputeLinkInformation*
3490 cmTarget::GetLinkInformation(const char* config)
3492 // Link information does not make sense for static libraries.
3493 assert(this->GetType() != cmTarget::STATIC_LIBRARY);
3495 // Lookup any existing information for this configuration.
3496 std::map<cmStdString, cmComputeLinkInformation*>::iterator
3497 i = this->LinkInformation.find(config?config:"");
3498 if(i == this->LinkInformation.end())
3500 // Compute information for this configuration.
3501 cmComputeLinkInformation* info =
3502 new cmComputeLinkInformation(this, config);
3503 if(!info || !info->Compute())
3505 delete info;
3506 info = 0;
3509 // Store the information for this configuration.
3510 std::map<cmStdString, cmComputeLinkInformation*>::value_type
3511 entry(config?config:"", info);
3512 i = this->LinkInformation.insert(entry).first;
3514 return i->second;
3517 //----------------------------------------------------------------------------
3518 cmTargetLinkInformationMap
3519 ::cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r): derived()
3521 // Ideally cmTarget instances should never be copied. However until
3522 // we can make a sweep to remove that, this copy constructor avoids
3523 // allowing the resources (LinkInformation) from getting copied. In
3524 // the worst case this will lead to extra cmComputeLinkInformation
3525 // instances. We also enforce in debug mode that the map be emptied
3526 // when copied.
3527 static_cast<void>(r);
3528 assert(r.empty());
3531 //----------------------------------------------------------------------------
3532 cmTargetLinkInformationMap::~cmTargetLinkInformationMap()
3534 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3536 delete i->second;
3540 //----------------------------------------------------------------------------
3541 cmTargetLinkInterfaceMap
3542 ::cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap const& r): derived()
3544 // Ideally cmTarget instances should never be copied. However until
3545 // we can make a sweep to remove that, this copy constructor avoids
3546 // allowing the resources (LinkInterface) from getting copied. In
3547 // the worst case this will lead to extra cmTargetLinkInterface
3548 // instances. We also enforce in debug mode that the map be emptied
3549 // when copied.
3550 static_cast<void>(r);
3551 assert(r.empty());
3554 //----------------------------------------------------------------------------
3555 cmTargetLinkInterfaceMap::~cmTargetLinkInterfaceMap()
3557 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3559 delete i->second;
3563 //----------------------------------------------------------------------------
3564 cmTargetInternalPointer::cmTargetInternalPointer()
3566 this->Pointer = new cmTargetInternals;
3569 //----------------------------------------------------------------------------
3570 cmTargetInternalPointer
3571 ::cmTargetInternalPointer(cmTargetInternalPointer const&)
3573 // Ideally cmTarget instances should never be copied. However until
3574 // we can make a sweep to remove that, this copy constructor avoids
3575 // allowing the resources (Internals) to be copied.
3576 this->Pointer = new cmTargetInternals;
3579 //----------------------------------------------------------------------------
3580 cmTargetInternalPointer::~cmTargetInternalPointer()
3582 delete this->Pointer;
3585 //----------------------------------------------------------------------------
3586 cmTargetInternalPointer&
3587 cmTargetInternalPointer::operator=(cmTargetInternalPointer const& r)
3589 if(this == &r) { return *this; } // avoid warning on HP about self check
3590 // Ideally cmTarget instances should never be copied. However until
3591 // we can make a sweep to remove that, this copy constructor avoids
3592 // allowing the resources (Internals) to be copied.
3593 cmTargetInternals* oldPointer = this->Pointer;
3594 this->Pointer = new cmTargetInternals;
3595 delete oldPointer;
3596 return *this;