BUG: fix some bad changes in progress calc
[cmake.git] / Source / cmTarget.cxx
blob7aba56e3a9448d354a0411fe06886bcf7b92df20
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-04 18:51:27 $
7 Version: $Revision: 1.205 $
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 <map>
25 #include <set>
26 #include <queue>
27 #include <stdlib.h> // required for atof
28 #include <assert.h>
29 const char* cmTarget::TargetTypeNames[] = {
30 "EXECUTABLE", "STATIC_LIBRARY",
31 "SHARED_LIBRARY", "MODULE_LIBRARY", "UTILITY", "GLOBAL_TARGET",
32 "INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY"
35 //----------------------------------------------------------------------------
36 class cmTargetInternals
38 public:
39 cmTargetInternals()
41 this->SourceFileFlagsConstructed = false;
43 typedef cmTarget::SourceFileFlags SourceFileFlags;
44 std::map<cmSourceFile const*, SourceFileFlags> SourceFlagsMap;
45 bool SourceFileFlagsConstructed;
48 //----------------------------------------------------------------------------
49 cmTarget::cmTarget()
51 this->Makefile = 0;
52 this->LinkLibrariesAnalyzed = false;
53 this->HaveInstallRule = false;
54 this->DLLPlatform = false;
55 this->IsImportedTarget = false;
58 //----------------------------------------------------------------------------
59 void cmTarget::DefineProperties(cmake *cm)
61 cm->DefineProperty
62 ("BUILD_WITH_INSTALL_RPATH", cmProperty::TARGET,
63 "Should build tree targets have install tree rpaths.",
64 "BUILD_WITH_INSTALL_RPATH is a boolean specifying whether to link "
65 "the target in the build tree with the INSTALL_RPATH. This takes "
66 "precedence over SKIP_BUILD_RPATH and avoids the need for relinking "
67 "before installation.");
69 cm->DefineProperty
70 ("CLEAN_DIRECT_OUTPUT", cmProperty::TARGET,
71 "Do not delete other variants of this target.",
72 "When a library is built CMake by default generates code to remove "
73 "any existing library using all possible names. This is needed "
74 "to support libraries that switch between STATIC and SHARED by "
75 "a user option. However when using OUTPUT_NAME to build a static "
76 "and shared library of the same name using different logical target "
77 "names the two targets will remove each other's files. This can be "
78 "prevented by setting the CLEAN_DIRECT_OUTPUT property to 1.");
80 cm->DefineProperty
81 ("COMPILE_FLAGS", cmProperty::TARGET,
82 "Additional flags to use when compiling this target's sources.",
83 "The COMPILE_FLAGS property sets additional compiler flags used "
84 "to build sources within the target. Use COMPILE_DEFINITIONS "
85 "to pass additional preprocessor definitions.");
87 cm->DefineProperty
88 ("COMPILE_DEFINITIONS", cmProperty::TARGET,
89 "Preprocessor definitions for compiling a target's sources.",
90 "The COMPILE_DEFINITIONS property may be set to a list of preprocessor "
91 "definitions using the syntax VAR or VAR=value. Function-style "
92 "definitions are not supported. CMake will automatically escape "
93 "the value correctly for the native build system (note that CMake "
94 "language syntax may require escapes to specify some values). "
95 "This property may be set on a per-configuration basis using the name "
96 "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
97 "(ex. \"COMPILE_DEFINITIONS_DEBUG\").\n"
98 "CMake will automatically drop some definitions that "
99 "are not supported by the native build tool. "
100 "The VS6 IDE does not support definitions with values "
101 "(but NMake does).\n"
102 "Dislaimer: Most native build tools have poor support for escaping "
103 "certain values. CMake has work-arounds for many cases but some "
104 "values may just not be possible to pass correctly. If a value "
105 "does not seem to be escaped correctly, do not attempt to "
106 "work-around the problem by adding escape sequences to the value. "
107 "Your work-around may break in a future version of CMake that "
108 "has improved escape support. Instead consider defining the macro "
109 "in a (configured) header file. Then report the limitation.");
111 cm->DefineProperty
112 ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::TARGET,
113 "Per-configuration preprocessor definitions on a target.",
114 "This is the configuration-specific version of COMPILE_DEFINITIONS.");
116 cm->DefineProperty
117 ("DEFINE_SYMBOL", cmProperty::TARGET,
118 "Define a symbol when compiling this target's sources.",
119 "DEFINE_SYMBOL sets the name of the preprocessor symbol defined when "
120 "compiling sources in a shared library. "
121 "If not set here then it is set to target_EXPORTS by default "
122 "(with some substitutions if the target is not a valid C "
123 "identifier). This is useful for headers to know whether they are "
124 "being included from inside their library our outside to properly "
125 "setup dllexport/dllimport decorations. ");
127 cm->DefineProperty
128 ("DEBUG_POSTFIX", cmProperty::TARGET,
129 "A postfix that will be applied to this target when build debug.",
130 "A property on a target that specifies a postfix to add to the "
131 "target name when built in debug mode. For example foo.dll "
132 "versus fooD.dll");
134 cm->DefineProperty
135 ("EchoString", cmProperty::TARGET,
136 "A message to be displayed when the target is built.",
137 "A message to display on some generators (such as makefiles) when "
138 "the target is built.");
140 cm->DefineProperty
141 ("FRAMEWORK", cmProperty::TARGET,
142 "This target is a framework on the Mac.",
143 "If a shared library target has this property set to true it will "
144 "be built as a framework when built on the mac. It will have the "
145 "directory structure required for a framework and will be suitable "
146 "to be used with the -framework option");
148 cm->DefineProperty
149 ("HAS_CXX", cmProperty::TARGET,
150 "Force a target to use the CXX linker.",
151 "Setting HAS_CXX on a target will force the target to use the "
152 "C++ linker (and C++ runtime libraries) for linking even if the "
153 "target has no C++ code in it.");
155 cm->DefineProperty
156 ("IMPORT_PREFIX", cmProperty::TARGET,
157 "What comes before the import library name.",
158 "Similar to the target property PREFIX, but used for import libraries "
159 "(typically corresponding to a DLL) instead of regular libraries. "
160 "A target property that can be set to override the prefix "
161 "(such as \"lib\") on an import library name.");
163 cm->DefineProperty
164 ("IMPORT_SUFFIX", cmProperty::TARGET,
165 "What comes after the import library name.",
166 "Similar to the target property SUFFIX, but used for import libraries "
167 "(typically corresponding to a DLL) instead of regular libraries. "
168 "A target property that can be set to override the suffix "
169 "(such as \".lib\") on an import library name.");
171 cm->DefineProperty
172 ("IMPORTED", cmProperty::TARGET,
173 "Read-only indication of whether a target is IMPORTED.",
174 "The boolean value of this property is true for targets created with "
175 "the IMPORTED option to add_executable or add_library. "
176 "It is false for targets built within the project.");
178 cm->DefineProperty
179 ("IMPORTED_CONFIGURATIONS", cmProperty::TARGET,
180 "Configurations provided for an IMPORTED target.",
181 "Lists configuration names available for an IMPORTED target. "
182 "The names correspond to configurations defined in the project from "
183 "which the target is imported. "
184 "If the importing project uses a different set of configurations "
185 "the names may be mapped using the MAP_IMPORTED_CONFIG_<CONFIG> "
186 "property. "
187 "Ignored for non-imported targets.");
189 cm->DefineProperty
190 ("IMPORTED_IMPLIB", cmProperty::TARGET,
191 "Full path to the import library for an IMPORTED target.",
192 "Specifies the location of the \".lib\" part of a windows DLL. "
193 "Ignored for non-imported targets.");
195 cm->DefineProperty
196 ("IMPORTED_IMPLIB_<CONFIG>", cmProperty::TARGET,
197 "Per-configuration version of IMPORTED_IMPLIB property.",
198 "This property is used when loading settings for the <CONFIG> "
199 "configuration of an imported target. "
200 "Configuration names correspond to those provided by the project "
201 "from which the target is imported.");
203 cm->DefineProperty
204 ("IMPORTED_LINK_DEPENDENT_LIBRARIES", cmProperty::TARGET,
205 "Dependent shared libraries of an imported shared library.",
206 "Shared libraries may be linked to other shared libraries as part "
207 "of their implementation. On some platforms the linker searches "
208 "for the dependent libraries of shared libraries they are including "
209 "in the link. This property lists "
210 "the dependent shared libraries of an imported library. The list "
211 "should be disjoint from the list of interface libraries in the "
212 "IMPORTED_LINK_INTERFACE_LIBRARIES property. On platforms requiring "
213 "dependent shared libraries to be found at link time CMake uses this "
214 "list to add appropriate files or paths to the link command line. "
215 "Ignored for non-imported targets.");
217 cm->DefineProperty
218 ("IMPORTED_LINK_DEPENDENT_LIBRARIES_<CONFIG>", cmProperty::TARGET,
219 "Per-configuration version of IMPORTED_LINK_DEPENDENT_LIBRARIES.",
220 "This property is used when loading settings for the <CONFIG> "
221 "configuration of an imported target. "
222 "Configuration names correspond to those provided by the project "
223 "from which the target is imported.");
225 cm->DefineProperty
226 ("IMPORTED_LINK_INTERFACE_LIBRARIES", cmProperty::TARGET,
227 "Transitive link interface of an IMPORTED target.",
228 "Lists libraries whose interface is included when an IMPORTED library "
229 "target is linked to another target. "
230 "The libraries will be included on the link line for the target. "
231 "Ignored for non-imported targets.");
233 cm->DefineProperty
234 ("IMPORTED_LINK_INTERFACE_LIBRARIES_<CONFIG>", cmProperty::TARGET,
235 "Per-configuration version of IMPORTED_LINK_INTERFACE_LIBRARIES.",
236 "This property is used when loading settings for the <CONFIG> "
237 "configuration of an imported target. "
238 "Configuration names correspond to those provided by the project "
239 "from which the target is imported.");
241 cm->DefineProperty
242 ("IMPORTED_LOCATION", cmProperty::TARGET,
243 "Full path to the main file on disk for an IMPORTED target.",
244 "Specifies the location of an IMPORTED target file on disk. "
245 "For executables this is the location of the executable file. "
246 "For bundles on OS X this is the location of the executable file "
247 "inside Contents/MacOS under the application bundle folder. "
248 "For static libraries and modules this is the location of the "
249 "library or module. "
250 "For shared libraries on non-DLL platforms this is the location of "
251 "the shared library. "
252 "For frameworks on OS X this is the location of the library file "
253 "symlink just inside the framework folder. "
254 "For DLLs this is the location of the \".dll\" part of the library. "
255 "Ignored for non-imported targets.");
257 cm->DefineProperty
258 ("IMPORTED_LOCATION_<CONFIG>", cmProperty::TARGET,
259 "Per-configuration version of IMPORTED_LOCATION property.",
260 "This property is used when loading settings for the <CONFIG> "
261 "configuration of an imported target. "
262 "Configuration names correspond to those provided by the project "
263 "from which the target is imported.");
265 cm->DefineProperty
266 ("IMPORTED_SONAME", cmProperty::TARGET,
267 "The \"soname\" of an IMPORTED target of shared library type.",
268 "Specifies the \"soname\" embedded in an imported shared library. "
269 "This is meaningful only on platforms supporting the feature. "
270 "Ignored for non-imported targets.");
272 cm->DefineProperty
273 ("IMPORTED_SONAME_<CONFIG>", cmProperty::TARGET,
274 "Per-configuration version of IMPORTED_SONAME property.",
275 "This property is used when loading settings for the <CONFIG> "
276 "configuration of an imported target. "
277 "Configuration names correspond to those provided by the project "
278 "from which the target is imported.");
280 cm->DefineProperty
281 ("EXCLUDE_FROM_ALL", cmProperty::TARGET,
282 "Exclude the target from the all target.",
283 "A property on a target that indicates if the target is excluded "
284 "from the default build target. If it is not, then with a Makefile "
285 "for example typing make will cause this target to be built. "
286 "The same concept applies to the default build of other generators. "
287 "Installing a target with EXCLUDE_FROM_ALL set to true has "
288 "undefined behavior.");
290 cm->DefineProperty
291 ("INSTALL_NAME_DIR", cmProperty::TARGET,
292 "Mac OSX directory name for installed targets.",
293 "INSTALL_NAME_DIR is a string specifying the "
294 "directory portion of the \"install_name\" field of shared libraries "
295 "on Mac OSX to use in the installed targets. ");
297 cm->DefineProperty
298 ("INSTALL_RPATH", cmProperty::TARGET,
299 "The rpath to use for installed targets.",
300 "A semicolon-separated list specifying the rpath "
301 "to use in installed targets (for platforms that support it).");
303 cm->DefineProperty
304 ("INSTALL_RPATH_USE_LINK_PATH", cmProperty::TARGET,
305 "Add paths to linker search and installed rpath.",
306 "INSTALL_RPATH_USE_LINK_PATH is a boolean that if set to true will "
307 "append directories in the linker search path and outside the "
308 "project to the INSTALL_RPATH. ");
310 cm->DefineProperty
311 ("LINK_FLAGS", cmProperty::TARGET,
312 "Additional flags to use when linking this target.",
313 "The LINK_FLAGS property can be used to add extra flags to the "
314 "link step of a target. LINK_FLAGS_<CONFIG> will add to the "
315 "configuration <CONFIG>, "
316 "for example, DEBUG, RELEASE, MINSIZEREL, RELWITHDEBINFO. ");
318 cm->DefineProperty
319 ("LINK_FLAGS_<CONFIG>", cmProperty::TARGET,
320 "Per-configuration linker flags for a target.",
321 "This is the configuration-specific version of LINK_FLAGS.");
323 cm->DefineProperty
324 ("LINK_SEARCH_END_STATIC", cmProperty::TARGET,
325 "End a link line such that static system libraries are used.",
326 "Some linkers support switches such as -Bstatic and -Bdynamic "
327 "to determine whether to use static or shared libraries for -lXXX "
328 "options. CMake uses these options to set the link type for "
329 "libraries whose full paths are not known or (in some cases) are in "
330 "implicit link directories for the platform. By default the "
331 "linker search type is left at -Bdynamic by the end of the library "
332 "list. This property switches the final linker search type to "
333 "-Bstatic.");
335 cm->DefineProperty
336 ("LINKER_LANGUAGE", cmProperty::TARGET,
337 "What tool to use for linking, based on language.",
338 "The LINKER_LANGUAGE property is used to change the tool "
339 "used to link an executable or shared library. The default is "
340 "set the language to match the files in the library. CXX and C "
341 "are common values for this property.");
343 cm->DefineProperty
344 ("LOCATION", cmProperty::TARGET,
345 "Deprecated. Use LOCATION_<CONFIG> or avoid altogether.",
346 "This property is provided for compatibility with CMake 2.4 and below. "
347 "It was meant to get the location of an executable target's output file "
348 "for use in add_custom_command. "
349 "In CMake 2.6 and above add_custom_command automatically recognizes a "
350 "target name in its COMMAND and DEPENDS options and computes the "
351 "target location. Therefore this property need not be used. "
352 "This property is not defined for IMPORTED targets because they "
353 "were not available in CMake 2.4 or below anyway.");
355 cm->DefineProperty
356 ("LOCATION_<CONFIG>", cmProperty::TARGET,
357 "Read-only property providing a target location on disk.",
358 "A read-only property that indicates where a target's main file is "
359 "located on disk for the configuration <CONFIG>. "
360 "The property is defined only for library and executable targets.");
362 cm->DefineProperty
363 ("LINK_INTERFACE_LIBRARIES", cmProperty::TARGET,
364 "List public interface libraries for a shared library or executable.",
365 "By default linking to a shared library target transitively "
366 "links to targets with which the library itself was linked. "
367 "For an executable with exports (see the ENABLE_EXPORTS property) "
368 "no default transitive link dependencies are used. "
369 "This property replaces the default transitive link dependencies with "
370 "an explict list. "
371 "When the target is linked into another target the libraries "
372 "listed (and recursively their link interface libraries) will be "
373 "provided to the other target also. "
374 "If the list is empty then no transitive link dependencies will be "
375 "incorporated when this target is linked into another target even if "
376 "the default set is non-empty.");
378 cm->DefineProperty
379 ("LINK_INTERFACE_LIBRARIES_<CONFIG>", cmProperty::TARGET,
380 "Per-configuration list of public interface libraries for a target.",
381 "This is the configuration-specific version of "
382 "LINK_INTERFACE_LIBRARIES.");
384 cm->DefineProperty
385 ("MAP_IMPORTED_CONFIG_<CONFIG>", cmProperty::TARGET,
386 "Map from project configuration to IMPORTED target's configuration.",
387 "List configurations of an imported target that may be used for "
388 "the current project's <CONFIG> configuration. "
389 "Targets imported from another project may not provide the same set "
390 "of configuration names available in the current project. "
391 "Setting this property tells CMake what imported configurations are "
392 "suitable for use when building the <CONFIG> configuration. "
393 "The first configuration in the list found to be provided by the "
394 "imported target is selected. If no matching configurations are "
395 "available the imported target is considered to be not found. "
396 "This property is ignored for non-imported targets.",
397 false /* TODO: make this chained */ );
399 cm->DefineProperty
400 ("OUTPUT_NAME", cmProperty::TARGET,
401 "Sets the real name of a target when it is built.",
402 "Sets the real name of a target when it is built and "
403 "can be used to help create two targets of the same name even though "
404 "CMake requires unique logical target names. There is also a "
405 "<CONFIG>_OUTPUT_NAME that can set the output name on a "
406 "per-configuration basis.");
408 cm->DefineProperty
409 ("PRE_INSTALL_SCRIPT", cmProperty::TARGET,
410 "Deprecated install support.",
411 "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
412 "old way to specify CMake scripts to run before and after "
413 "installing a target. They are used only when the old "
414 "INSTALL_TARGETS command is used to install the target. Use the "
415 "INSTALL command instead.");
417 cm->DefineProperty
418 ("PREFIX", cmProperty::TARGET,
419 "What comes before the library name.",
420 "A target property that can be set to override the prefix "
421 "(such as \"lib\") on a library name.");
423 cm->DefineProperty
424 ("POST_INSTALL_SCRIPT", cmProperty::TARGET,
425 "Deprecated install support.",
426 "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
427 "old way to specify CMake scripts to run before and after "
428 "installing a target. They are used only when the old "
429 "INSTALL_TARGETS command is used to install the target. Use the "
430 "INSTALL command instead.");
432 cm->DefineProperty
433 ("PRIVATE_HEADER", cmProperty::TARGET,
434 "Specify private header files in a FRAMEWORK shared library target.",
435 "Shared library targets marked with the FRAMEWORK property generate "
436 "frameworks on OS X and normal shared libraries on other platforms. "
437 "This property may be set to a list of header files to be placed "
438 "in the PrivateHeaders directory inside the framework folder. "
439 "On non-Apple platforms these headers may be installed using the "
440 "PRIVATE_HEADER option to the install(TARGETS) command.");
442 cm->DefineProperty
443 ("PUBLIC_HEADER", cmProperty::TARGET,
444 "Specify public header files in a FRAMEWORK shared library target.",
445 "Shared library targets marked with the FRAMEWORK property generate "
446 "frameworks on OS X and normal shared libraries on other platforms. "
447 "This property may be set to a list of header files to be placed "
448 "in the Headers directory inside the framework folder. "
449 "On non-Apple platforms these headers may be installed using the "
450 "PUBLIC_HEADER option to the install(TARGETS) command.");
452 cm->DefineProperty
453 ("RESOURCE", cmProperty::TARGET,
454 "Specify resource files in a FRAMEWORK shared library target.",
455 "Shared library targets marked with the FRAMEWORK property generate "
456 "frameworks on OS X and normal shared libraries on other platforms. "
457 "This property may be set to a list of files to be placed "
458 "in the Resources directory inside the framework folder. "
459 "On non-Apple platforms these files may be installed using the "
460 "RESOURCE option to the install(TARGETS) command.");
462 cm->DefineProperty
463 ("SKIP_BUILD_RPATH", cmProperty::TARGET,
464 "Should rpaths be used for the build tree.",
465 "SKIP_BUILD_RPATH is a boolean specifying whether to skip automatic "
466 "generation of an rpath allowing the target to run from the "
467 "build tree. ");
469 cm->DefineProperty
470 ("SOVERSION", cmProperty::TARGET,
471 "What version number is this target.",
472 "For shared libraries VERSION and SOVERSION can be used to specify "
473 "the build version and api version respectively. When building or "
474 "installing appropriate symlinks are created if the platform "
475 "supports symlinks and the linker supports so-names. "
476 "If only one of both is specified the missing is assumed to have "
477 "the same version number. "
478 "For shared libraries and executables on Windows the VERSION "
479 "attribute is parsed to extract a \"major.minor\" version number. "
480 "These numbers are used as the image version of the binary. ");
482 cm->DefineProperty
483 ("STATIC_LIBRARY_FLAGS", cmProperty::TARGET,
484 "Extra flags to use when linking static libraries.",
485 "Extra flags to use when linking a static library.");
487 cm->DefineProperty
488 ("SUFFIX", cmProperty::TARGET,
489 "What comes after the library name.",
490 "A target property that can be set to override the suffix "
491 "(such as \".so\") on a library name.");
493 cm->DefineProperty
494 ("VERSION", cmProperty::TARGET,
495 "What version number is this target.",
496 "For shared libraries VERSION and SOVERSION can be used to specify "
497 "the build version and api version respectively. When building or "
498 "installing appropriate symlinks are created if the platform "
499 "supports symlinks and the linker supports so-names. "
500 "If only one of both is specified the missing is assumed to have "
501 "the same version number. "
502 "For executables VERSION can be used to specify the build version. "
503 "When building or installing appropriate symlinks are created if "
504 "the platform supports symlinks. "
505 "For shared libraries and executables on Windows the VERSION "
506 "attribute is parsed to extract a \"major.minor\" version number. "
507 "These numbers are used as the image version of the binary. ");
510 cm->DefineProperty
511 ("WIN32_EXECUTABLE", cmProperty::TARGET,
512 "Build an executable with a WinMain entry point on windows.",
513 "When this property is set to true the executable when linked "
514 "on Windows will be created with a WinMain() entry point instead "
515 "of of just main()."
516 "This makes it a GUI executable instead of a console application. "
517 "See the CMAKE_MFC_FLAG variable documentation to configure use "
518 "of MFC for WinMain executables.");
520 cm->DefineProperty
521 ("MACOSX_BUNDLE", cmProperty::TARGET,
522 "Build an executable as an application bundle on Mac OS X.",
523 "When this property is set to true the executable when built "
524 "on Mac OS X will be created as an application bundle. "
525 "This makes it a GUI executable that can be launched from "
526 "the Finder.\n"
527 "The bundle Info.plist file is generated automatically. "
528 "The following target properties may be set to specify "
529 "its content:"
530 " MACOSX_BUNDLE_INFO_STRING\n"
531 " MACOSX_BUNDLE_ICON_FILE\n"
532 " MACOSX_BUNDLE_GUI_IDENTIFIER\n"
533 " MACOSX_BUNDLE_LONG_VERSION_STRING\n"
534 " MACOSX_BUNDLE_BUNDLE_NAME\n"
535 " MACOSX_BUNDLE_SHORT_VERSION_STRING\n"
536 " MACOSX_BUNDLE_BUNDLE_VERSION\n"
537 " MACOSX_BUNDLE_COPYRIGHT\n"
540 cm->DefineProperty
541 ("ENABLE_EXPORTS", cmProperty::TARGET,
542 "Specify whether an executable exports symbols for loadable modules.",
543 "Normally an executable does not export any symbols because it is "
544 "the final program. It is possible for an executable to export "
545 "symbols to be used by loadable modules. When this property is "
546 "set to true CMake will allow other targets to \"link\" to the "
547 "executable with the TARGET_LINK_LIBRARIES command. "
548 "On all platforms a target-level dependency on the executable is "
549 "created for targets that link to it. "
550 "For non-DLL platforms the link rule is simply ignored since "
551 "the dynamic loader will automatically bind symbols when the "
552 "module is loaded. "
553 "For DLL platforms an import library will be created for the "
554 "exported symbols and then used for linking. "
555 "All Windows-based systems including Cygwin are DLL platforms.");
557 cm->DefineProperty
558 ("Fortran_MODULE_DIRECTORY", cmProperty::TARGET,
559 "Specify output directory for Fortran modules provided by the target.",
560 "If the target contains Fortran source files that provide modules "
561 "and the compiler supports a module output directory this specifies "
562 "the directory in which the modules will be placed. "
563 "When this property is not set the modules will be placed in the "
564 "build directory corresponding to the target's source directory.");
566 cm->DefineProperty
567 ("XCODE_ATTRIBUTE_<an-attribute>", cmProperty::TARGET,
568 "Set Xcode target attributes directly.",
569 "Tell the Xcode generator to set '<an-attribute>' to a given value "
570 "in the generated Xcode project. Ignored on other generators.");
572 cm->DefineProperty
573 ("GENERATOR_FILE_NAME", cmProperty::TARGET,
574 "Generator's file for this target.",
575 "An internal property used by some generators to record the name of "
576 "project or dsp file associated with this target.");
578 cm->DefineProperty
579 ("SOURCES", cmProperty::TARGET,
580 "Source names specified for a target.",
581 "Read-only list of sources specified for a target. "
582 "The names returned are suitable for passing to the "
583 "set_source_files_properties command.");
585 #if 0
586 cm->DefineProperty
587 ("OBJECT_FILES", cmProperty::TARGET,
588 "Used to get the resulting list of object files that make up a "
589 "target.",
590 "This can be used to put object files from one library "
591 "into another library. It is a read only property. It "
592 "converts the source list for the target into a list of full "
593 "paths to object names that will be produced by the target.");
594 #endif
596 #define CM_TARGET_FILE_TYPES_DOC \
597 "There are three kinds of target files that may be built: " \
598 "archive, library, and runtime. " \
599 "Executables are always treated as runtime targets. " \
600 "Static libraries are always treated as archive targets. " \
601 "Module libraries are always treated as library targets. " \
602 "For non-DLL platforms shared libraries are treated as library " \
603 "targets. " \
604 "For DLL platforms the DLL part of a shared library is treated as " \
605 "a runtime target and the corresponding import library is treated as " \
606 "an archive target. " \
607 "All Windows-based systems including Cygwin are DLL platforms."
609 cm->DefineProperty
610 ("ARCHIVE_OUTPUT_DIRECTORY", cmProperty::TARGET,
611 "Output directory in which to build ARCHIVE target files.",
612 "This property specifies the directory into which archive target files "
613 "should be built. "
614 CM_TARGET_FILE_TYPES_DOC " "
615 "This property is initialized by the value of the variable "
616 "CMAKE_ARCHIVE_OUTPUT_DIRECTORY if it is set when a target is created.");
617 cm->DefineProperty
618 ("LIBRARY_OUTPUT_DIRECTORY", cmProperty::TARGET,
619 "Output directory in which to build LIBRARY target files.",
620 "This property specifies the directory into which library target files "
621 "should be built. "
622 CM_TARGET_FILE_TYPES_DOC " "
623 "This property is initialized by the value of the variable "
624 "CMAKE_LIBRARY_OUTPUT_DIRECTORY if it is set when a target is created.");
625 cm->DefineProperty
626 ("RUNTIME_OUTPUT_DIRECTORY", cmProperty::TARGET,
627 "Output directory in which to build RUNTIME target files.",
628 "This property specifies the directory into which runtime target files "
629 "should be built. "
630 CM_TARGET_FILE_TYPES_DOC " "
631 "This property is initialized by the value of the variable "
632 "CMAKE_RUNTIME_OUTPUT_DIRECTORY if it is set when a target is created.");
634 // define some properties without documentation
635 cm->DefineProperty("DEBUG_OUTPUT_NAME", cmProperty::TARGET,0,0);
636 cm->DefineProperty("RELEASE_OUTPUT_NAME", cmProperty::TARGET,0,0);
639 void cmTarget::SetType(TargetType type, const char* name)
641 this->Name = name;
642 if(type == cmTarget::INSTALL_FILES ||
643 type == cmTarget::INSTALL_PROGRAMS ||
644 type == cmTarget::INSTALL_DIRECTORY)
646 abort();
648 // only add dependency information for library targets
649 this->TargetTypeValue = type;
650 if(this->TargetTypeValue >= STATIC_LIBRARY
651 && this->TargetTypeValue <= MODULE_LIBRARY)
653 this->RecordDependencies = true;
655 else
657 this->RecordDependencies = false;
661 //----------------------------------------------------------------------------
662 void cmTarget::SetMakefile(cmMakefile* mf)
664 // Set our makefile.
665 this->Makefile = mf;
667 // set the cmake instance of the properties
668 this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
670 // Check whether this is a DLL platform.
671 this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
672 this->Makefile->IsOn("CYGWIN") ||
673 this->Makefile->IsOn("MINGW"));
675 // Setup default property values.
676 this->SetPropertyDefault("INSTALL_NAME_DIR", "");
677 this->SetPropertyDefault("INSTALL_RPATH", "");
678 this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
679 this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
680 this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
681 this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
682 this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
683 this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
684 this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", 0);
686 // Collect the set of configuration types.
687 std::vector<std::string> configNames;
688 if(const char* configurationTypes =
689 mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
691 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
693 else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
695 if(*buildType)
697 configNames.push_back(buildType);
701 // Setup per-configuration property default values.
702 for(std::vector<std::string>::iterator ci = configNames.begin();
703 ci != configNames.end(); ++ci)
705 // Initialize per-configuration name postfix property from the
706 // variable only for non-executable targets. This preserves
707 // compatibility with previous CMake versions in which executables
708 // did not support this variable. Projects may still specify the
709 // property directly. TODO: Make this depend on backwards
710 // compatibility setting.
711 if(this->TargetTypeValue != cmTarget::EXECUTABLE)
713 std::string property = cmSystemTools::UpperCase(*ci);
714 property += "_POSTFIX";
715 this->SetPropertyDefault(property.c_str(), 0);
720 //----------------------------------------------------------------------------
721 bool cmTarget::IsExecutableWithExports()
723 return (this->GetType() == cmTarget::EXECUTABLE &&
724 this->GetPropertyAsBool("ENABLE_EXPORTS"));
727 //----------------------------------------------------------------------------
728 bool cmTarget::IsFrameworkOnApple()
730 return (this->GetType() == cmTarget::SHARED_LIBRARY &&
731 this->Makefile->IsOn("APPLE") &&
732 this->GetPropertyAsBool("FRAMEWORK"));
735 //----------------------------------------------------------------------------
736 bool cmTarget::IsAppBundleOnApple()
738 return (this->GetType() == cmTarget::EXECUTABLE &&
739 this->Makefile->IsOn("APPLE") &&
740 this->GetPropertyAsBool("MACOSX_BUNDLE"));
743 //----------------------------------------------------------------------------
744 class cmTargetTraceDependencies
746 public:
747 cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile);
748 void Trace();
749 private:
750 cmTarget* Target;
751 cmMakefile* Makefile;
752 cmGlobalGenerator* GlobalGenerator;
753 std::queue<cmStdString> DependencyQueue;
754 std::set<cmStdString> DependenciesQueued;
755 std::set<cmSourceFile*> TargetSources;
757 void QueueOnce(std::string const& name);
758 void QueueOnce(std::vector<std::string> const& names);
759 void QueueDependencies(cmSourceFile* sf);
760 bool IsUtility(std::string const& dep);
761 void CheckCustomCommand(cmCustomCommand const& cc);
762 void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
765 //----------------------------------------------------------------------------
766 cmTargetTraceDependencies
767 ::cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile):
768 Target(target)
770 // Convenience.
771 this->Makefile = this->Target->GetMakefile();
772 this->GlobalGenerator =
773 this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
775 // Queue all the source files already specified for the target.
776 std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
777 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
778 si != sources.end(); ++si)
780 // Queue the source file itself in case it is generated.
781 this->QueueOnce((*si)->GetFullPath());
783 // Queue the dependencies of the source file in case they are
784 // generated.
785 this->QueueDependencies(*si);
787 // Track the sources already known to the target.
788 this->TargetSources.insert(*si);
791 // Queue the VS project file to check dependencies on the rule to
792 // generate it.
793 if(vsProjectFile)
795 this->QueueOnce(vsProjectFile);
798 // Queue pre-build, pre-link, and post-build rule dependencies.
799 this->CheckCustomCommands(this->Target->GetPreBuildCommands());
800 this->CheckCustomCommands(this->Target->GetPreLinkCommands());
801 this->CheckCustomCommands(this->Target->GetPostBuildCommands());
804 //----------------------------------------------------------------------------
805 void cmTargetTraceDependencies::Trace()
807 // Process one dependency at a time until the queue is empty.
808 while(!this->DependencyQueue.empty())
810 // Get the next dependency in from queue.
811 std::string dep = this->DependencyQueue.front();
812 this->DependencyQueue.pop();
814 // Check if we know how to generate this dependency.
815 if(cmSourceFile* sf =
816 this->Makefile->GetSourceFileWithOutput(dep.c_str()))
818 // Queue dependencies needed to generate this file.
819 this->QueueDependencies(sf);
821 // Make sure this file is in the target.
822 if(this->TargetSources.insert(sf).second)
824 this->Target->AddSourceFile(sf);
830 //----------------------------------------------------------------------------
831 void cmTargetTraceDependencies::QueueOnce(std::string const& name)
833 if(this->DependenciesQueued.insert(name).second)
835 this->DependencyQueue.push(name);
839 //----------------------------------------------------------------------------
840 void
841 cmTargetTraceDependencies::QueueOnce(std::vector<std::string> const& names)
843 for(std::vector<std::string>::const_iterator i = names.begin();
844 i != names.end(); ++i)
846 this->QueueOnce(*i);
850 //----------------------------------------------------------------------------
851 bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
853 // Dependencies on targets (utilities) are supposed to be named by
854 // just the target name. However for compatibility we support
855 // naming the output file generated by the target (assuming there is
856 // no output-name property which old code would not have set). In
857 // that case the target name will be the file basename of the
858 // dependency.
859 std::string util = cmSystemTools::GetFilenameName(dep);
860 if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
862 util = cmSystemTools::GetFilenameWithoutLastExtension(util);
865 // Check for a non-imported target with this name.
866 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, util.c_str()))
868 // If we find the target and the dep was given as a full path,
869 // then make sure it was not a full path to something else, and
870 // the fact that the name matched a target was just a coincidence.
871 if(cmSystemTools::FileIsFullPath(dep.c_str()))
873 // This is really only for compatibility so we do not need to
874 // worry about configuration names and output names.
875 std::string tLocation = t->GetLocation(0);
876 tLocation = cmSystemTools::GetFilenamePath(tLocation);
877 std::string depLocation = cmSystemTools::GetFilenamePath(dep);
878 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
879 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
880 if(depLocation == tLocation)
882 this->Target->AddUtility(util.c_str());
883 return true;
886 else
888 // The original name of the dependency was not a full path. It
889 // must name a target, so add the target-level dependency.
890 this->Target->AddUtility(util.c_str());
891 return true;
895 // The dependency does not name a target built in this project.
896 return false;
899 //----------------------------------------------------------------------------
900 void cmTargetTraceDependencies::QueueDependencies(cmSourceFile* sf)
902 // Queue dependency added explicitly by the user.
903 if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
905 std::vector<std::string> objDeps;
906 cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
907 this->QueueOnce(objDeps);
910 // Queue dependencies added programatically by commands.
911 this->QueueOnce(sf->GetDepends());
913 // Queue custom command dependencies.
914 if(cmCustomCommand const* cc = sf->GetCustomCommand())
916 this->CheckCustomCommand(*cc);
921 //----------------------------------------------------------------------------
922 void
923 cmTargetTraceDependencies
924 ::CheckCustomCommand(cmCustomCommand const& cc)
926 // Transform command names that reference targets built in this
927 // project to corresponding target-level dependencies.
928 for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
929 cit != cc.GetCommandLines().end(); ++cit)
931 std::string const& command = *cit->begin();
932 // Look for a non-imported target with this name.
933 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, command.c_str()))
935 if(t->GetType() == cmTarget::EXECUTABLE)
937 // The command refers to an executable target built in
938 // this project. Add the target-level dependency to make
939 // sure the executable is up to date before this custom
940 // command possibly runs.
941 this->Target->AddUtility(command.c_str());
946 // Queue the custom command dependencies.
947 std::vector<std::string> const& depends = cc.GetDepends();
948 for(std::vector<std::string>::const_iterator di = depends.begin();
949 di != depends.end(); ++di)
951 std::string const& dep = *di;
952 if(!this->IsUtility(dep))
954 // The dependency does not name a target and may be a file we
955 // know how to generate. Queue it.
956 this->QueueOnce(dep);
961 //----------------------------------------------------------------------------
962 void
963 cmTargetTraceDependencies
964 ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
966 for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
967 cli != commands.end(); ++cli)
969 this->CheckCustomCommand(*cli);
973 //----------------------------------------------------------------------------
974 void cmTarget::TraceDependencies(const char* vsProjectFile)
976 // Use a helper object to trace the dependencies.
977 cmTargetTraceDependencies tracer(this, vsProjectFile);
978 tracer.Trace();
981 //----------------------------------------------------------------------------
982 void cmTarget::AddSources(std::vector<std::string> const& srcs)
984 for(std::vector<std::string>::const_iterator i = srcs.begin();
985 i != srcs.end(); ++i)
987 this->AddSource(i->c_str());
991 //----------------------------------------------------------------------------
992 cmSourceFile* cmTarget::AddSource(const char* s)
994 std::string src = s;
996 // For backwards compatibility replace varibles in source names.
997 // This should eventually be removed.
998 this->Makefile->ExpandVariablesInString(src);
1000 cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
1001 this->AddSourceFile(sf);
1002 return sf;
1005 //----------------------------------------------------------------------------
1006 struct cmTarget::SourceFileFlags
1007 cmTarget::GetTargetSourceFileFlags(const cmSourceFile* sf)
1009 struct SourceFileFlags flags;
1010 this->ConstructSourceFileFlags();
1011 std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
1012 this->Internal->SourceFlagsMap.find(sf);
1013 if(si != this->Internal->SourceFlagsMap.end())
1015 flags = si->second;
1017 return flags;
1020 //----------------------------------------------------------------------------
1021 void cmTarget::ConstructSourceFileFlags()
1023 if(this->Internal->SourceFileFlagsConstructed)
1025 return;
1027 this->Internal->SourceFileFlagsConstructed = true;
1029 // Process public headers to mark the source files.
1030 if(const char* files = this->GetProperty("PUBLIC_HEADER"))
1032 std::vector<std::string> relFiles;
1033 cmSystemTools::ExpandListArgument(files, relFiles);
1034 for(std::vector<std::string>::iterator it = relFiles.begin();
1035 it != relFiles.end(); ++it)
1037 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1039 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1040 flags.MacFolder = "Headers";
1041 flags.Type = cmTarget::SourceFileTypePublicHeader;
1046 // Process private headers after public headers so that they take
1047 // precedence if a file is listed in both.
1048 if(const char* files = this->GetProperty("PRIVATE_HEADER"))
1050 std::vector<std::string> relFiles;
1051 cmSystemTools::ExpandListArgument(files, relFiles);
1052 for(std::vector<std::string>::iterator it = relFiles.begin();
1053 it != relFiles.end(); ++it)
1055 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1057 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1058 flags.MacFolder = "PrivateHeaders";
1059 flags.Type = cmTarget::SourceFileTypePrivateHeader;
1064 // Mark sources listed as resources.
1065 if(const char* files = this->GetProperty("RESOURCE"))
1067 std::vector<std::string> relFiles;
1068 cmSystemTools::ExpandListArgument(files, relFiles);
1069 for(std::vector<std::string>::iterator it = relFiles.begin();
1070 it != relFiles.end(); ++it)
1072 if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
1074 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1075 flags.MacFolder = "Resources";
1076 flags.Type = cmTarget::SourceFileTypeResource;
1081 // Handle the MACOSX_PACKAGE_LOCATION property on source files that
1082 // were not listed in one of the other lists.
1083 std::vector<cmSourceFile*> const& sources = this->GetSourceFiles();
1084 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
1085 si != sources.end(); ++si)
1087 cmSourceFile* sf = *si;
1088 if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
1090 SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
1091 if(flags.Type == cmTarget::SourceFileTypeNormal)
1093 flags.MacFolder = location;
1094 if(strcmp(location, "Resources") == 0)
1096 flags.Type = cmTarget::SourceFileTypeResource;
1098 else
1100 flags.Type = cmTarget::SourceFileTypeMacContent;
1107 //----------------------------------------------------------------------------
1108 void cmTarget::MergeLinkLibraries( cmMakefile& mf,
1109 const char *selfname,
1110 const LinkLibraryVectorType& libs )
1112 // Only add on libraries we haven't added on before.
1113 // Assumption: the global link libraries could only grow, never shrink
1114 LinkLibraryVectorType::const_iterator i = libs.begin();
1115 i += this->PrevLinkedLibraries.size();
1116 for( ; i != libs.end(); ++i )
1118 // We call this so that the dependencies get written to the cache
1119 this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
1121 this->PrevLinkedLibraries = libs;
1124 //----------------------------------------------------------------------------
1125 void cmTarget::AddLinkDirectory(const char* d)
1127 // Make sure we don't add unnecessary search directories.
1128 if(this->LinkDirectoriesEmmitted.insert(d).second)
1130 this->LinkDirectories.push_back(d);
1134 //----------------------------------------------------------------------------
1135 const std::vector<std::string>& cmTarget::GetLinkDirectories()
1137 return this->LinkDirectories;
1140 //----------------------------------------------------------------------------
1141 void cmTarget::ClearDependencyInformation( cmMakefile& mf,
1142 const char* target )
1144 // Clear the dependencies. The cache variable must exist iff we are
1145 // recording dependency information for this target.
1146 std::string depname = target;
1147 depname += "_LIB_DEPENDS";
1148 if (this->RecordDependencies)
1150 mf.AddCacheDefinition(depname.c_str(), "",
1151 "Dependencies for target", cmCacheManager::STATIC);
1153 else
1155 if (mf.GetDefinition( depname.c_str() ))
1157 std::string message = "Target ";
1158 message += target;
1159 message += " has dependency information when it shouldn't.\n";
1160 message += "Your cache is probably stale. Please remove the entry\n ";
1161 message += depname;
1162 message += "\nfrom the cache.";
1163 cmSystemTools::Error( message.c_str() );
1168 //----------------------------------------------------------------------------
1169 void cmTarget::AddLinkLibrary(const std::string& lib,
1170 LinkLibraryType llt)
1172 this->AddFramework(lib.c_str(), llt);
1173 cmTarget::LibraryID tmp;
1174 tmp.first = lib;
1175 tmp.second = llt;
1176 this->LinkLibraries.push_back(tmp);
1177 this->OriginalLinkLibraries.push_back(tmp);
1180 //----------------------------------------------------------------------------
1181 bool cmTarget::NameResolvesToFramework(const std::string& libname)
1183 return this->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
1184 NameResolvesToFramework(libname);
1187 //----------------------------------------------------------------------------
1188 bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
1190 (void)llt; // TODO: What is this?
1191 if(this->NameResolvesToFramework(libname.c_str()))
1193 std::string frameworkDir = libname;
1194 frameworkDir += "/../";
1195 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1196 std::vector<std::string>::iterator i =
1197 std::find(this->Frameworks.begin(),
1198 this->Frameworks.end(), frameworkDir);
1199 if(i == this->Frameworks.end())
1201 this->Frameworks.push_back(frameworkDir);
1203 return true;
1205 return false;
1208 //----------------------------------------------------------------------------
1209 void cmTarget::AddLinkLibrary(cmMakefile& mf,
1210 const char *target, const char* lib,
1211 LinkLibraryType llt)
1213 // Never add a self dependency, even if the user asks for it.
1214 if(strcmp( target, lib ) == 0)
1216 return;
1218 this->AddFramework(lib, llt);
1219 cmTarget::LibraryID tmp;
1220 tmp.first = lib;
1221 tmp.second = llt;
1222 this->LinkLibraries.push_back( tmp );
1223 this->OriginalLinkLibraries.push_back(tmp);
1225 // Add the explicit dependency information for this target. This is
1226 // simply a set of libraries separated by ";". There should always
1227 // be a trailing ";". These library names are not canonical, in that
1228 // they may be "-framework x", "-ly", "/path/libz.a", etc.
1229 // We shouldn't remove duplicates here because external libraries
1230 // may be purposefully duplicated to handle recursive dependencies,
1231 // and we removing one instance will break the link line. Duplicates
1232 // will be appropriately eliminated at emit time.
1233 if(this->RecordDependencies)
1235 std::string targetEntry = target;
1236 targetEntry += "_LIB_DEPENDS";
1237 std::string dependencies;
1238 const char* old_val = mf.GetDefinition( targetEntry.c_str() );
1239 if( old_val )
1241 dependencies += old_val;
1243 switch (llt)
1245 case cmTarget::GENERAL:
1246 dependencies += "general";
1247 break;
1248 case cmTarget::DEBUG:
1249 dependencies += "debug";
1250 break;
1251 case cmTarget::OPTIMIZED:
1252 dependencies += "optimized";
1253 break;
1255 dependencies += ";";
1256 dependencies += lib;
1257 dependencies += ";";
1258 mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
1259 "Dependencies for the target",
1260 cmCacheManager::STATIC );
1265 //----------------------------------------------------------------------------
1266 void
1267 cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
1269 // There are two key parts of the dependency analysis: (1)
1270 // determining the libraries in the link line, and (2) constructing
1271 // the dependency graph for those libraries.
1273 // The latter is done using the cache entries that record the
1274 // dependencies of each library.
1276 // The former is a more thorny issue, since it is not clear how to
1277 // determine if two libraries listed on the link line refer to the a
1278 // single library or not. For example, consider the link "libraries"
1279 // /usr/lib/libtiff.so -ltiff
1280 // Is this one library or two? The solution implemented here is the
1281 // simplest (and probably the only practical) one: two libraries are
1282 // the same if their "link strings" are identical. Thus, the two
1283 // libraries above are considered distinct. This also means that for
1284 // dependency analysis to be effective, the CMake user must specify
1285 // libraries build by his project without using any linker flags or
1286 // file extensions. That is,
1287 // LINK_LIBRARIES( One Two )
1288 // instead of
1289 // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
1290 // The former is probably what most users would do, but it never
1291 // hurts to document the assumptions. :-) Therefore, in the analysis
1292 // code, the "canonical name" of a library is simply its name as
1293 // given to a LINK_LIBRARIES command.
1295 // Also, we will leave the original link line intact; we will just add any
1296 // dependencies that were missing.
1298 // There is a problem with recursive external libraries
1299 // (i.e. libraries with no dependency information that are
1300 // recursively dependent). We must make sure that the we emit one of
1301 // the libraries twice to satisfy the recursion, but we shouldn't
1302 // emit it more times than necessary. In particular, we must make
1303 // sure that handling this improbable case doesn't cost us when
1304 // dealing with the common case of non-recursive libraries. The
1305 // solution is to assume that the recursion is satisfied at one node
1306 // of the dependency tree. To illustrate, assume libA and libB are
1307 // extrenal and mutually dependent. Suppose libX depends on
1308 // libA, and libY on libA and libX. Then
1309 // TARGET_LINK_LIBRARIES( Y X A B A )
1310 // TARGET_LINK_LIBRARIES( X A B A )
1311 // TARGET_LINK_LIBRARIES( Exec Y )
1312 // would result in "-lY -lX -lA -lB -lA". This is the correct way to
1313 // specify the dependencies, since the mutual dependency of A and B
1314 // is resolved *every time libA is specified*.
1316 // Something like
1317 // TARGET_LINK_LIBRARIES( Y X A B A )
1318 // TARGET_LINK_LIBRARIES( X A B )
1319 // TARGET_LINK_LIBRARIES( Exec Y )
1320 // would result in "-lY -lX -lA -lB", and the mutual dependency
1321 // information is lost. This is because in some case (Y), the mutual
1322 // dependency of A and B is listed, while in another other case (X),
1323 // it is not. Depending on which line actually emits A, the mutual
1324 // dependency may or may not be on the final link line. We can't
1325 // handle this pathalogical case cleanly without emitting extra
1326 // libraries for the normal cases. Besides, the dependency
1327 // information for X is wrong anyway: if we build an executable
1328 // depending on X alone, we would not have the mutual dependency on
1329 // A and B resolved.
1331 // IMPROVEMENTS:
1332 // -- The current algorithm will not always pick the "optimal" link line
1333 // when recursive dependencies are present. It will instead break the
1334 // cycles at an aribtrary point. The majority of projects won't have
1335 // cyclic dependencies, so this is probably not a big deal. Note that
1336 // the link line is always correct, just not necessary optimal.
1339 // Expand variables in link library names. This is for backwards
1340 // compatibility with very early CMake versions and should
1341 // eventually be removed. This code was moved here from the end of
1342 // old source list processing code which was called just before this
1343 // method.
1344 for(LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
1345 p != this->LinkLibraries.end(); ++p)
1347 this->Makefile->ExpandVariablesInString(p->first, true, true);
1351 typedef std::vector< std::string > LinkLine;
1353 // The dependency map.
1354 DependencyMap dep_map;
1356 // 1. Build the dependency graph
1358 for(LinkLibraryVectorType::reverse_iterator lib
1359 = this->LinkLibraries.rbegin();
1360 lib != this->LinkLibraries.rend(); ++lib)
1362 this->GatherDependencies( mf, *lib, dep_map);
1365 // 2. Remove any dependencies that are already satisfied in the original
1366 // link line.
1368 for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
1369 lib != this->LinkLibraries.end(); ++lib)
1371 for( LinkLibraryVectorType::iterator lib2 = lib;
1372 lib2 != this->LinkLibraries.end(); ++lib2)
1374 this->DeleteDependency( dep_map, *lib, *lib2);
1379 // 3. Create the new link line by simply emitting any dependencies that are
1380 // missing. Start from the back and keep adding.
1382 std::set<DependencyMap::key_type> done, visited;
1383 std::vector<DependencyMap::key_type> newLinkLibraries;
1384 for(LinkLibraryVectorType::reverse_iterator lib =
1385 this->LinkLibraries.rbegin();
1386 lib != this->LinkLibraries.rend(); ++lib)
1388 // skip zero size library entries, this may happen
1389 // if a variable expands to nothing.
1390 if (lib->first.size() != 0)
1392 this->Emit( *lib, dep_map, done, visited, newLinkLibraries );
1396 // 4. Add the new libraries to the link line.
1398 for( std::vector<DependencyMap::key_type>::reverse_iterator k =
1399 newLinkLibraries.rbegin();
1400 k != newLinkLibraries.rend(); ++k )
1402 // get the llt from the dep_map
1403 this->LinkLibraries.push_back( std::make_pair(k->first,k->second) );
1405 this->LinkLibrariesAnalyzed = true;
1408 //----------------------------------------------------------------------------
1409 void cmTarget::InsertDependency( DependencyMap& depMap,
1410 const LibraryID& lib,
1411 const LibraryID& dep)
1413 depMap[lib].push_back(dep);
1416 //----------------------------------------------------------------------------
1417 void cmTarget::DeleteDependency( DependencyMap& depMap,
1418 const LibraryID& lib,
1419 const LibraryID& dep)
1421 // Make sure there is an entry in the map for lib. If so, delete all
1422 // dependencies to dep. There may be repeated entries because of
1423 // external libraries that are specified multiple times.
1424 DependencyMap::iterator map_itr = depMap.find( lib );
1425 if( map_itr != depMap.end() )
1427 DependencyList& depList = map_itr->second;
1428 DependencyList::iterator itr;
1429 while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
1430 depList.end() )
1432 depList.erase( itr );
1437 //----------------------------------------------------------------------------
1438 void cmTarget::Emit(const LibraryID lib,
1439 const DependencyMap& dep_map,
1440 std::set<LibraryID>& emitted,
1441 std::set<LibraryID>& visited,
1442 DependencyList& link_line )
1444 // It's already been emitted
1445 if( emitted.find(lib) != emitted.end() )
1447 return;
1450 // Emit the dependencies only if this library node hasn't been
1451 // visited before. If it has, then we have a cycle. The recursion
1452 // that got us here should take care of everything.
1454 if( visited.insert(lib).second )
1456 if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
1458 const DependencyList& dep_on = dep_map.find( lib )->second;
1459 DependencyList::const_reverse_iterator i;
1461 // To cater for recursive external libraries, we must emit
1462 // duplicates on this link line *unless* they were emitted by
1463 // some other node, in which case we assume that the recursion
1464 // was resolved then. We making the simplifying assumption that
1465 // any duplicates on a single link line are on purpose, and must
1466 // be preserved.
1468 // This variable will keep track of the libraries that were
1469 // emitted directory from the current node, and not from a
1470 // recursive call. This way, if we come across a library that
1471 // has already been emitted, we repeat it iff it has been
1472 // emitted here.
1473 std::set<DependencyMap::key_type> emitted_here;
1474 for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
1476 if( emitted_here.find(*i) != emitted_here.end() )
1478 // a repeat. Must emit.
1479 emitted.insert(*i);
1480 link_line.push_back( *i );
1482 else
1484 // Emit only if no-one else has
1485 if( emitted.find(*i) == emitted.end() )
1487 // emit dependencies
1488 Emit( *i, dep_map, emitted, visited, link_line );
1489 // emit self
1490 emitted.insert(*i);
1491 emitted_here.insert(*i);
1492 link_line.push_back( *i );
1500 //----------------------------------------------------------------------------
1501 void cmTarget::GatherDependencies( const cmMakefile& mf,
1502 const LibraryID& lib,
1503 DependencyMap& dep_map)
1505 // If the library is already in the dependency map, then it has
1506 // already been fully processed.
1507 if( dep_map.find(lib) != dep_map.end() )
1509 return;
1512 const char* deps = mf.GetDefinition( (lib.first+"_LIB_DEPENDS").c_str() );
1513 if( deps && strcmp(deps,"") != 0 )
1515 // Make sure this library is in the map, even if it has an empty
1516 // set of dependencies. This distinguishes the case of explicitly
1517 // no dependencies with that of unspecified dependencies.
1518 dep_map[lib];
1520 // Parse the dependency information, which is a set of
1521 // type, library pairs separated by ";". There is always a trailing ";".
1522 cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
1523 std::string depline = deps;
1524 std::string::size_type start = 0;
1525 std::string::size_type end;
1526 end = depline.find( ";", start );
1527 while( end != std::string::npos )
1529 std::string l = depline.substr( start, end-start );
1530 if( l.size() != 0 )
1532 if (l == "debug")
1534 llt = cmTarget::DEBUG;
1536 else if (l == "optimized")
1538 llt = cmTarget::OPTIMIZED;
1540 else if (l == "general")
1542 llt = cmTarget::GENERAL;
1544 else
1546 LibraryID lib2(l,llt);
1547 this->InsertDependency( dep_map, lib, lib2);
1548 this->GatherDependencies( mf, lib2, dep_map);
1549 llt = cmTarget::GENERAL;
1552 start = end+1; // skip the ;
1553 end = depline.find( ";", start );
1555 // cannot depend on itself
1556 this->DeleteDependency( dep_map, lib, lib);
1560 //----------------------------------------------------------------------------
1561 void cmTarget::SetProperty(const char* prop, const char* value)
1563 if (!prop)
1565 return;
1567 if (!value)
1569 value = "NOTFOUND";
1572 this->Properties.SetProperty(prop, value, cmProperty::TARGET);
1574 // If imported information is being set, wipe out cached
1575 // information.
1576 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1578 this->ImportInfoMap.clear();
1582 //----------------------------------------------------------------------------
1583 void cmTarget::AppendProperty(const char* prop, const char* value)
1585 if (!prop)
1587 return;
1589 this->Properties.AppendProperty(prop, value, cmProperty::TARGET);
1591 // If imported information is being set, wipe out cached
1592 // information.
1593 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1595 this->ImportInfoMap.clear();
1599 //----------------------------------------------------------------------------
1600 void cmTarget::MarkAsImported()
1602 this->IsImportedTarget = true;
1605 //----------------------------------------------------------------------------
1606 const char* cmTarget::GetDirectory(const char* config, bool implib)
1608 if (this->IsImported())
1610 return this->ImportedGetDirectory(config, implib);
1612 else
1614 return this->NormalGetDirectory(config, implib);
1618 //----------------------------------------------------------------------------
1619 const char* cmTarget::ImportedGetDirectory(const char* config, bool implib)
1621 this->Directory =
1622 cmSystemTools::GetFilenamePath(
1623 this->ImportedGetFullPath(config, implib));
1624 return this->Directory.c_str();
1627 //----------------------------------------------------------------------------
1628 const char* cmTarget::NormalGetDirectory(const char* config, bool implib)
1630 if(config && *config)
1632 // Do not create the directory when config is given:
1633 this->Directory = this->GetAndCreateOutputDir(implib, true);
1634 // Add the configuration's subdirectory.
1635 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
1636 AppendDirectoryForConfig("/", config, "", this->Directory);
1637 return this->Directory.c_str();
1639 else
1641 return this->GetOutputDir(implib);
1645 //----------------------------------------------------------------------------
1646 const char* cmTarget::GetLocation(const char* config)
1648 if (this->IsImported())
1650 return this->ImportedGetLocation(config);
1652 else
1654 return this->NormalGetLocation(config);
1658 //----------------------------------------------------------------------------
1659 const char* cmTarget::ImportedGetLocation(const char* config)
1661 this->Location = this->ImportedGetFullPath(config, false);
1662 return this->Location.c_str();
1665 //----------------------------------------------------------------------------
1666 const char* cmTarget::NormalGetLocation(const char* config)
1668 this->Location = this->GetDirectory(config);
1669 if(!this->Location.empty())
1671 this->Location += "/";
1673 if(!config)
1675 // No specific configuration was given so it will not appear on
1676 // the result of GetDirectory. Add a name here to be replaced at
1677 // build time.
1678 const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
1679 if(cfgid && strcmp(cfgid, ".") != 0)
1681 this->Location += cfgid;
1682 this->Location += "/";
1685 this->Location += this->GetFullName(config, false);
1686 return this->Location.c_str();
1689 //----------------------------------------------------------------------------
1690 void cmTarget::GetTargetVersion(int& major, int& minor)
1692 // Set the default values.
1693 major = 0;
1694 minor = 0;
1696 // Look for a VERSION property.
1697 if(const char* version = this->GetProperty("VERSION"))
1699 // Try to parse the version number and store the results that were
1700 // successfully parsed.
1701 int parsed_major;
1702 int parsed_minor;
1703 switch(sscanf(version, "%d.%d", &parsed_major, &parsed_minor))
1705 case 2: minor = parsed_minor; // no break!
1706 case 1: major = parsed_major; // no break!
1707 default: break;
1712 //----------------------------------------------------------------------------
1713 const char *cmTarget::GetProperty(const char* prop)
1715 return this->GetProperty(prop, cmProperty::TARGET);
1718 //----------------------------------------------------------------------------
1719 void cmTarget::ComputeObjectFiles()
1721 if (this->IsImported())
1723 return;
1725 #if 0
1726 std::vector<std::string> dirs;
1727 this->Makefile->GetLocalGenerator()->
1728 GetTargetObjectFileDirectories(this,
1729 dirs);
1730 std::string objectFiles;
1731 std::string objExtensionLookup1 = "CMAKE_";
1732 std::string objExtensionLookup2 = "_OUTPUT_EXTENSION";
1734 for(std::vector<std::string>::iterator d = dirs.begin();
1735 d != dirs.end(); ++d)
1737 for(std::vector<cmSourceFile*>::iterator s = this->SourceFiles.begin();
1738 s != this->SourceFiles.end(); ++s)
1740 cmSourceFile* sf = *s;
1741 if(const char* lang = sf->GetLanguage())
1743 std::string lookupObj = objExtensionLookup1 + lang;
1744 lookupObj += objExtensionLookup2;
1745 const char* obj = this->Makefile->GetDefinition(lookupObj.c_str());
1746 if(obj)
1748 if(objectFiles.size())
1750 objectFiles += ";";
1752 std::string objFile = *d;
1753 objFile += "/";
1754 objFile += this->Makefile->GetLocalGenerator()->
1755 GetSourceObjectName(*sf);
1756 objFile += obj;
1757 objectFiles += objFile;
1762 this->SetProperty("OBJECT_FILES", objectFiles.c_str());
1763 #endif
1766 //----------------------------------------------------------------------------
1767 const char *cmTarget::GetProperty(const char* prop,
1768 cmProperty::ScopeType scope)
1770 if(!prop)
1772 return 0;
1775 // Watch for special "computed" properties that are dependent on
1776 // other properties or variables. Always recompute them.
1777 if(this->GetType() == cmTarget::EXECUTABLE ||
1778 this->GetType() == cmTarget::STATIC_LIBRARY ||
1779 this->GetType() == cmTarget::SHARED_LIBRARY ||
1780 this->GetType() == cmTarget::MODULE_LIBRARY)
1782 if(!this->IsImported() && strcmp(prop,"LOCATION") == 0)
1784 // Set the LOCATION property of the target. Note that this
1785 // cannot take into account the per-configuration name of the
1786 // target because the configuration type may not be known at
1787 // CMake time. It is now deprecated as described in the
1788 // documentation.
1789 this->SetProperty("LOCATION", this->GetLocation(0));
1792 // Support "LOCATION_<CONFIG>".
1793 if(strncmp(prop, "LOCATION_", 9) == 0)
1795 std::string configName = prop+9;
1796 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1798 else
1800 // Support "<CONFIG>_LOCATION" for compatiblity.
1801 int len = static_cast<int>(strlen(prop));
1802 if(len > 9 && strcmp(prop+len-9, "_LOCATION") == 0)
1804 std::string configName(prop, len-9);
1805 if(configName != "IMPORTED")
1807 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1813 if (strcmp(prop,"IMPORTED") == 0)
1815 return this->IsImported()?"TRUE":"FALSE";
1818 if(!strcmp(prop,"SOURCES"))
1820 cmOStringStream ss;
1821 const char* sep = "";
1822 for(std::vector<cmSourceFile*>::const_iterator
1823 i = this->SourceFiles.begin();
1824 i != this->SourceFiles.end(); ++i)
1826 // Separate from the previous list entries.
1827 ss << sep;
1828 sep = ";";
1830 // Construct what is known about this source file location.
1831 cmSourceFileLocation const& location = (*i)->GetLocation();
1832 std::string sname = location.GetDirectory();
1833 if(!sname.empty())
1835 sname += "/";
1837 sname += location.GetName();
1839 // Append this list entry.
1840 ss << sname;
1842 this->SetProperty("SOURCES", ss.str().c_str());
1845 // the type property returns what type the target is
1846 if (!strcmp(prop,"TYPE"))
1848 switch( this->GetType() )
1850 case cmTarget::STATIC_LIBRARY:
1851 return "STATIC_LIBRARY";
1852 // break; /* unreachable */
1853 case cmTarget::MODULE_LIBRARY:
1854 return "MODULE_LIBRARY";
1855 // break; /* unreachable */
1856 case cmTarget::SHARED_LIBRARY:
1857 return "SHARED_LIBRARY";
1858 // break; /* unreachable */
1859 case cmTarget::EXECUTABLE:
1860 return "EXECUTABLE";
1861 // break; /* unreachable */
1862 case cmTarget::UTILITY:
1863 return "UTILITY";
1864 // break; /* unreachable */
1865 case cmTarget::GLOBAL_TARGET:
1866 return "GLOBAL_TARGET";
1867 // break; /* unreachable */
1868 case cmTarget::INSTALL_FILES:
1869 return "INSTALL_FILES";
1870 // break; /* unreachable */
1871 case cmTarget::INSTALL_PROGRAMS:
1872 return "INSTALL_PROGRAMS";
1873 // break; /* unreachable */
1874 case cmTarget::INSTALL_DIRECTORY:
1875 return "INSTALL_DIRECTORY";
1876 // break; /* unreachable */
1878 return 0;
1880 bool chain = false;
1881 const char *retVal =
1882 this->Properties.GetPropertyValue(prop, scope, chain);
1883 if (chain)
1885 return this->Makefile->GetProperty(prop,scope);
1887 return retVal;
1890 //----------------------------------------------------------------------------
1891 bool cmTarget::GetPropertyAsBool(const char* prop)
1893 return cmSystemTools::IsOn(this->GetProperty(prop));
1896 //----------------------------------------------------------------------------
1897 const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
1899 if(this->GetProperty("HAS_CXX"))
1901 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
1903 const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
1904 if (linkerLang==0)
1906 // if the property has not yet been set, collect all languages in the
1907 // target and then find the language with the highest preference value
1908 std::set<cmStdString> languages;
1909 this->GetLanguages(languages);
1911 std::string linkerLangList; // only used for the error message
1912 int maxLinkerPref = 0;
1913 bool multiplePreferedLanguages = false;
1914 for(std::set<cmStdString>::const_iterator sit = languages.begin();
1915 sit != languages.end(); ++sit)
1917 int linkerPref = gg->GetLinkerPreference(sit->c_str());
1918 if ((linkerPref > maxLinkerPref) || (linkerLang==0))
1920 maxLinkerPref = linkerPref;
1921 linkerLang = sit->c_str();
1922 linkerLangList = *sit;
1923 multiplePreferedLanguages = false;
1925 else if (linkerPref == maxLinkerPref)
1927 linkerLangList += "; ";
1928 linkerLangList += *sit;
1929 multiplePreferedLanguages = true;
1933 if (linkerLang!=0)
1935 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", linkerLang);
1937 if (multiplePreferedLanguages)
1939 cmOStringStream err;
1940 err << "Error: Target " << this->Name << " contains multiple languages "
1941 << "with the highest linker preference (" << maxLinkerPref << "): "
1942 << linkerLangList << "\n"
1943 << "You must set the LINKER_LANGUAGE property for this target.";
1944 cmSystemTools::Error(err.str().c_str());
1947 return this->GetProperty("LINKER_LANGUAGE");
1950 //----------------------------------------------------------------------------
1951 const char* cmTarget::GetCreateRuleVariable()
1953 switch(this->GetType())
1955 case cmTarget::STATIC_LIBRARY:
1956 return "_CREATE_STATIC_LIBRARY";
1957 case cmTarget::SHARED_LIBRARY:
1958 return "_CREATE_SHARED_LIBRARY";
1959 case cmTarget::MODULE_LIBRARY:
1960 return "_CREATE_SHARED_MODULE";
1961 case cmTarget::EXECUTABLE:
1962 return "_LINK_EXECUTABLE";
1963 case cmTarget::UTILITY:
1964 case cmTarget::GLOBAL_TARGET:
1965 case cmTarget::INSTALL_FILES:
1966 case cmTarget::INSTALL_PROGRAMS:
1967 case cmTarget::INSTALL_DIRECTORY:
1968 break;
1970 return "";
1973 //----------------------------------------------------------------------------
1974 const char* cmTarget::GetSuffixVariableInternal(TargetType type,
1975 bool implib)
1977 switch(type)
1979 case cmTarget::STATIC_LIBRARY:
1980 return "CMAKE_STATIC_LIBRARY_SUFFIX";
1981 case cmTarget::SHARED_LIBRARY:
1982 return (implib
1983 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1984 : "CMAKE_SHARED_LIBRARY_SUFFIX");
1985 case cmTarget::MODULE_LIBRARY:
1986 return (implib
1987 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1988 : "CMAKE_SHARED_MODULE_SUFFIX");
1989 case cmTarget::EXECUTABLE:
1990 return (implib
1991 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1992 : "CMAKE_EXECUTABLE_SUFFIX");
1993 case cmTarget::UTILITY:
1994 case cmTarget::GLOBAL_TARGET:
1995 case cmTarget::INSTALL_FILES:
1996 case cmTarget::INSTALL_PROGRAMS:
1997 case cmTarget::INSTALL_DIRECTORY:
1998 break;
2000 return "";
2004 //----------------------------------------------------------------------------
2005 const char* cmTarget::GetPrefixVariableInternal(TargetType type,
2006 bool implib)
2008 switch(type)
2010 case cmTarget::STATIC_LIBRARY:
2011 return "CMAKE_STATIC_LIBRARY_PREFIX";
2012 case cmTarget::SHARED_LIBRARY:
2013 return (implib
2014 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
2015 : "CMAKE_SHARED_LIBRARY_PREFIX");
2016 case cmTarget::MODULE_LIBRARY:
2017 return (implib
2018 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
2019 : "CMAKE_SHARED_MODULE_PREFIX");
2020 case cmTarget::EXECUTABLE:
2021 return (implib? "CMAKE_IMPORT_LIBRARY_PREFIX" : "");
2022 case cmTarget::UTILITY:
2023 case cmTarget::GLOBAL_TARGET:
2024 case cmTarget::INSTALL_FILES:
2025 case cmTarget::INSTALL_PROGRAMS:
2026 case cmTarget::INSTALL_DIRECTORY:
2027 break;
2029 return "";
2032 //----------------------------------------------------------------------------
2033 std::string cmTarget::GetPDBName(const char* config)
2035 std::string prefix;
2036 std::string base;
2037 std::string suffix;
2038 this->GetFullNameInternal(this->GetType(), config, false,
2039 prefix, base, suffix);
2040 return prefix+base+".pdb";
2043 //----------------------------------------------------------------------------
2044 std::string cmTarget::GetSOName(const char* config)
2046 if(this->IsImported())
2048 // Lookup the imported soname.
2049 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2051 if(info->NoSOName)
2053 // The imported library has no builtin soname so the name
2054 // searched at runtime will be just the filename.
2055 return cmSystemTools::GetFilenameName(info->Location);
2057 else
2059 // Use the soname given if any.
2060 return info->SOName;
2063 else
2065 return "";
2068 else
2070 // Compute the soname that will be built.
2071 std::string name;
2072 std::string soName;
2073 std::string realName;
2074 std::string impName;
2075 std::string pdbName;
2076 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2077 return soName;
2081 //----------------------------------------------------------------------------
2082 bool cmTarget::IsImportedSharedLibWithoutSOName(const char* config)
2084 if(this->IsImported() && this->GetType() == cmTarget::SHARED_LIBRARY)
2086 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2088 return info->NoSOName;
2091 return false;
2094 //----------------------------------------------------------------------------
2095 std::string cmTarget::NormalGetRealName(const char* config)
2097 // This should not be called for imported targets.
2098 // TODO: Split cmTarget into a class hierarchy to get compile-time
2099 // enforcement of the limited imported target API.
2100 if(this->IsImported())
2102 abort();
2105 if(this->GetType() == cmTarget::EXECUTABLE)
2107 // Compute the real name that will be built.
2108 std::string name;
2109 std::string realName;
2110 std::string impName;
2111 std::string pdbName;
2112 this->GetExecutableNames(name, realName, impName, pdbName, config);
2113 return realName;
2115 else
2117 // Compute the real name that will be built.
2118 std::string name;
2119 std::string soName;
2120 std::string realName;
2121 std::string impName;
2122 std::string pdbName;
2123 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2124 return realName;
2128 //----------------------------------------------------------------------------
2129 std::string cmTarget::GetFullName(const char* config, bool implib)
2131 if(this->IsImported())
2133 return this->GetFullNameImported(config, implib);
2135 else
2137 return this->GetFullNameInternal(this->GetType(), config, implib);
2141 //----------------------------------------------------------------------------
2142 std::string cmTarget::GetFullNameImported(const char* config, bool implib)
2144 return cmSystemTools::GetFilenameName(
2145 this->ImportedGetFullPath(config, implib));
2148 //----------------------------------------------------------------------------
2149 void cmTarget::GetFullNameComponents(std::string& prefix, std::string& base,
2150 std::string& suffix, const char* config,
2151 bool implib)
2153 this->GetFullNameInternal(this->GetType(), config, implib,
2154 prefix, base, suffix);
2157 //----------------------------------------------------------------------------
2158 std::string cmTarget::GetFullPath(const char* config, bool implib,
2159 bool realname)
2161 if(this->IsImported())
2163 return this->ImportedGetFullPath(config, implib);
2165 else
2167 return this->NormalGetFullPath(config, implib, realname);
2171 //----------------------------------------------------------------------------
2172 std::string cmTarget::NormalGetFullPath(const char* config, bool implib,
2173 bool realname)
2175 // Start with the output directory for the target.
2176 std::string fpath = this->GetDirectory(config, implib);
2177 fpath += "/";
2179 // Add the full name of the target.
2180 if(implib)
2182 fpath += this->GetFullName(config, true);
2184 else if(realname)
2186 fpath += this->NormalGetRealName(config);
2188 else
2190 fpath += this->GetFullName(config, false);
2192 return fpath;
2195 //----------------------------------------------------------------------------
2196 std::string cmTarget::ImportedGetFullPath(const char* config, bool implib)
2198 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2200 if(implib)
2202 return info->ImportLibrary;
2204 else
2206 return info->Location;
2209 else
2211 std::string result = this->GetName();
2212 result += "-NOTFOUND";
2213 return result;
2217 //----------------------------------------------------------------------------
2218 std::string
2219 cmTarget::GetFullNameInternal(TargetType type, const char* config,
2220 bool implib)
2222 std::string prefix;
2223 std::string base;
2224 std::string suffix;
2225 this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
2226 return prefix+base+suffix;
2229 //----------------------------------------------------------------------------
2230 void cmTarget::GetFullNameInternal(TargetType type,
2231 const char* config,
2232 bool implib,
2233 std::string& outPrefix,
2234 std::string& outBase,
2235 std::string& outSuffix)
2237 // Use just the target name for non-main target types.
2238 if(type != cmTarget::STATIC_LIBRARY &&
2239 type != cmTarget::SHARED_LIBRARY &&
2240 type != cmTarget::MODULE_LIBRARY &&
2241 type != cmTarget::EXECUTABLE)
2243 outPrefix = "";
2244 outBase = this->GetName();
2245 outSuffix = "";
2246 return;
2249 // Return an empty name for the import library if this platform
2250 // does not support import libraries.
2251 if(implib &&
2252 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2254 outPrefix = "";
2255 outBase = "";
2256 outSuffix = "";
2257 return;
2260 // The implib option is only allowed for shared libraries, module
2261 // libraries, and executables.
2262 if(type != cmTarget::SHARED_LIBRARY &&
2263 type != cmTarget::MODULE_LIBRARY &&
2264 type != cmTarget::EXECUTABLE)
2266 implib = false;
2269 // Compute the full name for main target types.
2270 const char* targetPrefix = (implib
2271 ? this->GetProperty("IMPORT_PREFIX")
2272 : this->GetProperty("PREFIX"));
2273 const char* targetSuffix = (implib
2274 ? this->GetProperty("IMPORT_SUFFIX")
2275 : this->GetProperty("SUFFIX"));
2276 const char* configPostfix = 0;
2277 if(config && *config)
2279 std::string configProp = cmSystemTools::UpperCase(config);
2280 configProp += "_POSTFIX";
2281 configPostfix = this->GetProperty(configProp.c_str());
2283 const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
2284 const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
2285 const char* ll =
2286 this->GetLinkerLanguage(
2287 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2288 // first try language specific suffix
2289 if(ll)
2291 if(!targetSuffix && suffixVar && *suffixVar)
2293 std::string langSuff = suffixVar + std::string("_") + ll;
2294 targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
2296 if(!targetPrefix && prefixVar && *prefixVar)
2298 std::string langPrefix = prefixVar + std::string("_") + ll;
2299 targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
2303 // if there is no prefix on the target use the cmake definition
2304 if(!targetPrefix && prefixVar)
2306 targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
2308 // if there is no suffix on the target use the cmake definition
2309 if(!targetSuffix && suffixVar)
2311 targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
2314 // frameworks do not have a prefix or a suffix
2315 if(this->IsFrameworkOnApple())
2317 targetPrefix = 0;
2318 targetSuffix = 0;
2321 // Begin the final name with the prefix.
2322 outPrefix = targetPrefix?targetPrefix:"";
2324 // Append the target name or property-specified name.
2325 const char* outName = 0;
2326 if(config && *config)
2328 std::string configProp = cmSystemTools::UpperCase(config);
2329 configProp += "_OUTPUT_NAME";
2330 outName = this->GetProperty(configProp.c_str());
2332 if(!outName)
2334 outName = this->GetProperty("OUTPUT_NAME");
2336 if(outName)
2338 outBase = outName;
2340 else
2342 outBase = this->GetName();
2345 // Append the per-configuration postfix.
2346 outBase += configPostfix?configPostfix:"";
2348 // Name shared libraries with their version number on some platforms.
2349 if(const char* version = this->GetProperty("VERSION"))
2351 if(type == cmTarget::SHARED_LIBRARY && !implib &&
2352 this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION"))
2354 outBase += "-";
2355 outBase += version;
2359 // Append the suffix.
2360 outSuffix = targetSuffix?targetSuffix:"";
2363 //----------------------------------------------------------------------------
2364 void cmTarget::GetLibraryNames(std::string& name,
2365 std::string& soName,
2366 std::string& realName,
2367 std::string& impName,
2368 std::string& pdbName,
2369 const char* config)
2371 // Get the names based on the real type of the library.
2372 this->GetLibraryNamesInternal(name, soName, realName, impName, pdbName,
2373 this->GetType(), config);
2376 //----------------------------------------------------------------------------
2377 void cmTarget::GetLibraryCleanNames(std::string& staticName,
2378 std::string& sharedName,
2379 std::string& sharedSOName,
2380 std::string& sharedRealName,
2381 std::string& importName,
2382 std::string& pdbName,
2383 const char* config)
2385 // Get the name as if this were a static library.
2386 std::string soName;
2387 std::string realName;
2388 std::string impName;
2389 this->GetLibraryNamesInternal(staticName, soName, realName, impName,
2390 pdbName, cmTarget::STATIC_LIBRARY, config);
2392 // Get the names as if this were a shared library.
2393 if(this->GetType() == cmTarget::STATIC_LIBRARY)
2395 // Since the real type is static then the user either specified
2396 // STATIC or did not specify a type. In the former case the
2397 // shared library will never be present. In the latter case the
2398 // type will never be MODULE. Either way the only names that
2399 // might have to be cleaned are the shared library names.
2400 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2401 importName, pdbName,
2402 cmTarget::SHARED_LIBRARY, config);
2404 else
2406 // Use the name of the real type of the library (shared or module).
2407 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2408 importName, pdbName, this->GetType(),
2409 config);
2413 //----------------------------------------------------------------------------
2414 void cmTarget::GetLibraryNamesInternal(std::string& name,
2415 std::string& soName,
2416 std::string& realName,
2417 std::string& impName,
2418 std::string& pdbName,
2419 TargetType type,
2420 const char* config)
2422 // This should not be called for imported targets.
2423 // TODO: Split cmTarget into a class hierarchy to get compile-time
2424 // enforcement of the limited imported target API.
2425 if(this->IsImported())
2427 abort();
2430 // Construct the name of the soname flag variable for this language.
2431 const char* ll =
2432 this->GetLinkerLanguage(
2433 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2434 std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
2435 if(ll)
2437 sonameFlag += "_";
2438 sonameFlag += ll;
2440 sonameFlag += "_FLAG";
2442 // Check for library version properties.
2443 const char* version = this->GetProperty("VERSION");
2444 const char* soversion = this->GetProperty("SOVERSION");
2445 if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
2446 !this->Makefile->GetDefinition(sonameFlag.c_str()))
2448 // Versioning is supported only for shared libraries and modules,
2449 // and then only when the platform supports an soname flag.
2450 version = 0;
2451 soversion = 0;
2453 if(version && !soversion)
2455 // The soversion must be set if the library version is set. Use
2456 // the library version as the soversion.
2457 soversion = version;
2460 // Get the components of the library name.
2461 std::string prefix;
2462 std::string base;
2463 std::string suffix;
2464 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2466 // The library name.
2467 name = prefix+base+suffix;
2469 // The library's soname.
2470 #if defined(__APPLE__)
2471 soName = prefix+base;
2472 #else
2473 soName = name;
2474 #endif
2475 if(soversion)
2477 soName += ".";
2478 soName += soversion;
2480 #if defined(__APPLE__)
2481 soName += suffix;
2482 #endif
2484 // The library's real name on disk.
2485 #if defined(__APPLE__)
2486 realName = prefix+base;
2487 #else
2488 realName = name;
2489 #endif
2490 if(version)
2492 realName += ".";
2493 realName += version;
2495 else if(soversion)
2497 realName += ".";
2498 realName += soversion;
2500 #if defined(__APPLE__)
2501 realName += suffix;
2502 #endif
2504 // The import library name.
2505 if(type == cmTarget::SHARED_LIBRARY ||
2506 type == cmTarget::MODULE_LIBRARY)
2508 impName = this->GetFullNameInternal(type, config, true);
2510 else
2512 impName = "";
2515 // The program database file name.
2516 pdbName = prefix+base+".pdb";
2519 //----------------------------------------------------------------------------
2520 void cmTarget::GetExecutableNames(std::string& name,
2521 std::string& realName,
2522 std::string& impName,
2523 std::string& pdbName,
2524 const char* config)
2526 // Get the names based on the real type of the executable.
2527 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2528 this->GetType(), config);
2531 //----------------------------------------------------------------------------
2532 void cmTarget::GetExecutableCleanNames(std::string& name,
2533 std::string& realName,
2534 std::string& impName,
2535 std::string& pdbName,
2536 const char* config)
2538 // Get the name and versioned name of this executable.
2539 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2540 cmTarget::EXECUTABLE, config);
2543 //----------------------------------------------------------------------------
2544 void cmTarget::GetExecutableNamesInternal(std::string& name,
2545 std::string& realName,
2546 std::string& impName,
2547 std::string& pdbName,
2548 TargetType type,
2549 const char* config)
2551 // This should not be called for imported targets.
2552 // TODO: Split cmTarget into a class hierarchy to get compile-time
2553 // enforcement of the limited imported target API.
2554 if(this->IsImported())
2556 abort();
2559 // This versioning is supported only for executables and then only
2560 // when the platform supports symbolic links.
2561 #if defined(_WIN32) && !defined(__CYGWIN__)
2562 const char* version = 0;
2563 #else
2564 // Check for executable version properties.
2565 const char* version = this->GetProperty("VERSION");
2566 if(type != cmTarget::EXECUTABLE || this->Makefile->IsOn("XCODE"))
2568 version = 0;
2570 #endif
2572 // Get the components of the executable name.
2573 std::string prefix;
2574 std::string base;
2575 std::string suffix;
2576 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2578 // The executable name.
2579 name = prefix+base+suffix;
2581 // The executable's real name on disk.
2582 #if defined(__CYGWIN__)
2583 realName = prefix+base;
2584 #else
2585 realName = name;
2586 #endif
2587 if(version)
2589 realName += "-";
2590 realName += version;
2592 #if defined(__CYGWIN__)
2593 realName += suffix;
2594 #endif
2596 // The import library name.
2597 impName = this->GetFullNameInternal(type, config, true);
2599 // The program database file name.
2600 pdbName = prefix+base+".pdb";
2603 //----------------------------------------------------------------------------
2604 void cmTarget::GenerateTargetManifest(const char* config)
2606 cmMakefile* mf = this->Makefile;
2607 cmLocalGenerator* lg = mf->GetLocalGenerator();
2608 cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2610 // Get the names.
2611 std::string name;
2612 std::string soName;
2613 std::string realName;
2614 std::string impName;
2615 std::string pdbName;
2616 if(this->GetType() == cmTarget::EXECUTABLE)
2618 this->GetExecutableNames(name, realName, impName, pdbName, config);
2620 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2621 this->GetType() == cmTarget::SHARED_LIBRARY ||
2622 this->GetType() == cmTarget::MODULE_LIBRARY)
2624 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2626 else
2628 return;
2631 // Get the directory.
2632 std::string dir = this->GetDirectory(config, false);
2634 // Add each name.
2635 std::string f;
2636 if(!name.empty())
2638 f = dir;
2639 f += "/";
2640 f += name;
2641 gg->AddToManifest(config? config:"", f);
2643 if(!soName.empty())
2645 f = dir;
2646 f += "/";
2647 f += soName;
2648 gg->AddToManifest(config? config:"", f);
2650 if(!realName.empty())
2652 f = dir;
2653 f += "/";
2654 f += realName;
2655 gg->AddToManifest(config? config:"", f);
2657 if(!pdbName.empty())
2659 f = dir;
2660 f += "/";
2661 f += pdbName;
2662 gg->AddToManifest(config? config:"", f);
2664 if(!impName.empty())
2666 f = this->GetDirectory(config, true);
2667 f += "/";
2668 f += impName;
2669 gg->AddToManifest(config? config:"", f);
2673 //----------------------------------------------------------------------------
2674 void cmTarget::SetPropertyDefault(const char* property,
2675 const char* default_value)
2677 // Compute the name of the variable holding the default value.
2678 std::string var = "CMAKE_";
2679 var += property;
2681 if(const char* value = this->Makefile->GetDefinition(var.c_str()))
2683 this->SetProperty(property, value);
2685 else if(default_value)
2687 this->SetProperty(property, default_value);
2691 //----------------------------------------------------------------------------
2692 bool cmTarget::HaveBuildTreeRPATH()
2694 return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
2695 !this->LinkLibraries.empty());
2698 //----------------------------------------------------------------------------
2699 bool cmTarget::HaveInstallTreeRPATH()
2701 const char* install_rpath = this->GetProperty("INSTALL_RPATH");
2702 return install_rpath && *install_rpath;
2705 //----------------------------------------------------------------------------
2706 bool cmTarget::NeedRelinkBeforeInstall()
2708 // Only executables and shared libraries can have an rpath and may
2709 // need relinking.
2710 if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
2711 this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
2712 this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
2714 return false;
2717 // If there is no install location this target will not be installed
2718 // and therefore does not need relinking.
2719 if(!this->GetHaveInstallRule())
2721 return false;
2724 // If skipping all rpaths completely then no relinking is needed.
2725 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
2727 return false;
2730 // If building with the install-tree rpath no relinking is needed.
2731 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2733 return false;
2736 // If chrpath is going to be used no relinking is needed.
2737 if(this->IsChrpathUsed())
2739 return false;
2742 // Check for rpath support on this platform.
2743 if(const char* ll = this->GetLinkerLanguage(
2744 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2746 std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2747 flagVar += ll;
2748 flagVar += "_FLAG";
2749 if(!this->Makefile->IsSet(flagVar.c_str()))
2751 // There is no rpath support on this platform so nothing needs
2752 // relinking.
2753 return false;
2756 else
2758 // No linker language is known. This error will be reported by
2759 // other code.
2760 return false;
2763 // If either a build or install tree rpath is set then the rpath
2764 // will likely change between the build tree and install tree and
2765 // this target must be relinked.
2766 return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
2769 //----------------------------------------------------------------------------
2770 std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
2772 // If building directly for installation then the build tree install_name
2773 // is the same as the install tree.
2774 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2776 return GetInstallNameDirForInstallTree(config);
2779 // Use the build tree directory for the target.
2780 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2781 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2782 !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
2784 std::string dir = this->GetDirectory(config);
2785 dir += "/";
2786 return dir;
2788 else
2790 return "";
2794 //----------------------------------------------------------------------------
2795 std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
2797 // Lookup the target property.
2798 const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
2799 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2800 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2801 install_name_dir && *install_name_dir)
2803 std::string dir = install_name_dir;
2804 dir += "/";
2805 return dir;
2807 else
2809 return "";
2813 //----------------------------------------------------------------------------
2814 const char* cmTarget::GetAndCreateOutputDir(bool implib, bool create)
2816 // The implib option is only allowed for shared libraries, module
2817 // libraries, and executables.
2818 if(this->GetType() != cmTarget::SHARED_LIBRARY &&
2819 this->GetType() != cmTarget::MODULE_LIBRARY &&
2820 this->GetType() != cmTarget::EXECUTABLE)
2822 implib = false;
2825 // Sanity check. Only generators on platforms supporting import
2826 // libraries should be asking for the import library output
2827 // directory.
2828 if(implib &&
2829 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2831 abort();
2833 if(implib && !this->DLLPlatform)
2835 abort();
2838 // Select whether we are constructing the directory for the main
2839 // target or the import library.
2840 std::string& out = implib? this->OutputDirImplib : this->OutputDir;
2842 if(out.empty())
2844 // Look for a target property defining the target output directory
2845 // based on the target type.
2846 const char* propertyName = 0;
2847 switch(this->GetType())
2849 case cmTarget::SHARED_LIBRARY:
2851 // For non-DLL platforms shared libraries are treated as
2852 // library targets. For DLL platforms the DLL part of a
2853 // shared library is treated as a runtime target and the
2854 // corresponding import library is treated as an archive
2855 // target.
2856 if(this->DLLPlatform)
2858 if(implib)
2860 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2862 else
2864 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2867 else
2869 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2871 } break;
2872 case cmTarget::STATIC_LIBRARY:
2874 // Static libraries are always treated as archive targets.
2875 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2876 } break;
2877 case cmTarget::MODULE_LIBRARY:
2879 // Module libraries are always treated as library targets.
2880 // Module import libraries are treated as archive targets.
2881 if(implib)
2883 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2885 else
2887 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2889 } break;
2890 case cmTarget::EXECUTABLE:
2892 // Executables are always treated as runtime targets.
2893 // Executable import libraries are treated as archive targets.
2894 if(implib)
2896 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2898 else
2900 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2902 } break;
2903 default: break;
2906 // Select an output directory.
2907 if(const char* outdir = this->GetProperty(propertyName))
2909 // Use the user-specified output directory.
2910 out = outdir;
2912 else if(this->GetType() == cmTarget::EXECUTABLE)
2914 // Lookup the output path for executables.
2915 out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
2917 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2918 this->GetType() == cmTarget::SHARED_LIBRARY ||
2919 this->GetType() == cmTarget::MODULE_LIBRARY)
2921 // Lookup the output path for libraries.
2922 out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
2924 if(out.empty())
2926 // Default to the current output directory.
2927 out = ".";
2929 // Convert the output path to a full path in case it is
2930 // specified as a relative path. Treat a relative path as
2931 // relative to the current output directory for this makefile.
2932 out =
2933 cmSystemTools::CollapseFullPath
2934 (out.c_str(), this->Makefile->GetStartOutputDirectory());
2936 // TODO: Make AppBundle and Framework directory computation in
2937 // target consistent. Why do we add the .framework part here for
2938 // frameworks but not the .app part for bundles? We should
2939 // probably not add it for either.
2940 if(this->IsFrameworkOnApple())
2942 out += "/";
2943 out += this->GetFullName(0, implib);
2944 out += ".framework";
2947 // Optionally make sure the output path exists on disk.
2948 if(create)
2950 if(!cmSystemTools::MakeDirectory(out.c_str()))
2952 cmSystemTools::Error("Error failed to create output directory: ",
2953 out.c_str());
2958 return out.c_str();
2961 //----------------------------------------------------------------------------
2962 const char* cmTarget::GetOutputDir(bool implib)
2964 return this->GetAndCreateOutputDir(implib, true);
2967 //----------------------------------------------------------------------------
2968 const char* cmTarget::GetExportMacro()
2970 // Define the symbol for targets that export symbols.
2971 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
2972 this->GetType() == cmTarget::MODULE_LIBRARY ||
2973 this->IsExecutableWithExports())
2975 if(const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL"))
2977 this->ExportMacro = custom_export_name;
2979 else
2981 std::string in = this->GetName();
2982 in += "_EXPORTS";
2983 this->ExportMacro = cmSystemTools::MakeCindentifier(in.c_str());
2985 return this->ExportMacro.c_str();
2987 else
2989 return 0;
2993 //----------------------------------------------------------------------------
2994 void cmTarget::GetLanguages(std::set<cmStdString>& languages) const
2996 for(std::vector<cmSourceFile*>::const_iterator
2997 i = this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
2999 if(const char* lang = (*i)->GetLanguage())
3001 languages.insert(lang);
3006 //----------------------------------------------------------------------------
3007 bool cmTarget::IsChrpathUsed()
3009 #if defined(CMAKE_USE_ELF_PARSER)
3010 // Only certain target types have an rpath.
3011 if(!(this->GetType() == cmTarget::SHARED_LIBRARY ||
3012 this->GetType() == cmTarget::MODULE_LIBRARY ||
3013 this->GetType() == cmTarget::EXECUTABLE))
3015 return false;
3018 // If the target will not be installed we do not need to change its
3019 // rpath.
3020 if(!this->GetHaveInstallRule())
3022 return false;
3025 // Skip chrpath if skipping rpath altogether.
3026 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
3028 return false;
3031 // Skip chrpath if it does not need to be changed at install time.
3032 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
3034 return false;
3037 // Allow the user to disable builtin chrpath explicitly.
3038 if(this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH"))
3040 return false;
3043 // Enable if the rpath flag uses a separator and the target uses ELF
3044 // binaries.
3045 if(const char* ll = this->GetLinkerLanguage(
3046 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
3048 std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
3049 sepVar += ll;
3050 sepVar += "_FLAG_SEP";
3051 const char* sep = this->Makefile->GetDefinition(sepVar.c_str());
3052 if(sep && *sep)
3054 // TODO: Add ELF check to ABI detection and get rid of
3055 // CMAKE_EXECUTABLE_FORMAT.
3056 if(const char* fmt =
3057 this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT"))
3059 return strcmp(fmt, "ELF") == 0;
3063 #endif
3064 return false;
3067 //----------------------------------------------------------------------------
3068 cmTarget::ImportInfo const*
3069 cmTarget::GetImportInfo(const char* config)
3071 // There is no imported information for non-imported targets.
3072 if(!this->IsImported())
3074 return 0;
3077 // Lookup/compute/cache the import information for this
3078 // configuration.
3079 std::string config_upper;
3080 if(config && *config)
3082 config_upper = cmSystemTools::UpperCase(config);
3084 else
3086 config_upper = "NOCONFIG";
3088 ImportInfoMapType::const_iterator i =
3089 this->ImportInfoMap.find(config_upper);
3090 if(i == this->ImportInfoMap.end())
3092 ImportInfo info;
3093 this->ComputeImportInfo(config_upper, info);
3094 ImportInfoMapType::value_type entry(config_upper, info);
3095 i = this->ImportInfoMap.insert(entry).first;
3098 // If the location is empty then the target is not available for
3099 // this configuration.
3100 if(i->second.Location.empty())
3102 return 0;
3105 // Return the import information.
3106 return &i->second;
3109 //----------------------------------------------------------------------------
3110 void cmTarget::ComputeImportInfo(std::string const& desired_config,
3111 ImportInfo& info)
3113 // This method finds information about an imported target from its
3114 // properties. The "IMPORTED_" namespace is reserved for properties
3115 // defined by the project exporting the target.
3117 // Initialize members.
3118 info.NoSOName = false;
3120 // Track the configuration-specific property suffix.
3121 std::string suffix = "_";
3122 suffix += desired_config;
3124 // Look for a mapping from the current project's configuration to
3125 // the imported project's configuration.
3126 std::vector<std::string> mappedConfigs;
3128 std::string mapProp = "MAP_IMPORTED_CONFIG_";
3129 mapProp += desired_config;
3130 if(const char* mapValue = this->GetProperty(mapProp.c_str()))
3132 cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
3136 // If a mapping was found, check its configurations.
3137 const char* loc = 0;
3138 for(std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
3139 !loc && mci != mappedConfigs.end(); ++mci)
3141 // Look for this configuration.
3142 std::string mcUpper = cmSystemTools::UpperCase(mci->c_str());
3143 std::string locProp = "IMPORTED_LOCATION_";
3144 locProp += mcUpper;
3145 loc = this->GetProperty(locProp.c_str());
3147 // If it was found, use it for all properties below.
3148 if(loc)
3150 suffix = "_";
3151 suffix += mcUpper;
3155 // If we needed to find one of the mapped configurations but did not
3156 // then the target is not found. The project does not want any
3157 // other configuration.
3158 if(!mappedConfigs.empty() && !loc)
3160 return;
3163 // If we have not yet found it then there are no mapped
3164 // configurations. Look for an exact-match.
3165 if(!loc)
3167 std::string locProp = "IMPORTED_LOCATION";
3168 locProp += suffix;
3169 loc = this->GetProperty(locProp.c_str());
3172 // If we have not yet found it then there are no mapped
3173 // configurations and no exact match.
3174 if(!loc)
3176 // The suffix computed above is not useful.
3177 suffix = "";
3179 // Look for a configuration-less location. This may be set by
3180 // manually-written code.
3181 loc = this->GetProperty("IMPORTED_LOCATION");
3184 // If we have not yet found it then the project is willing to try
3185 // any available configuration.
3186 if(!loc)
3188 std::vector<std::string> availableConfigs;
3189 if(const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS"))
3191 cmSystemTools::ExpandListArgument(iconfigs, availableConfigs);
3193 for(std::vector<std::string>::const_iterator
3194 aci = availableConfigs.begin();
3195 !loc && aci != availableConfigs.end(); ++aci)
3197 suffix = "_";
3198 suffix += cmSystemTools::UpperCase(availableConfigs[0]);
3199 std::string locProp = "IMPORTED_LOCATION";
3200 locProp += suffix;
3201 loc = this->GetProperty(locProp.c_str());
3205 // If we have not yet found it then the target is not available.
3206 if(!loc)
3208 return;
3211 // A provided configuration has been chosen. Load the
3212 // configuration's properties.
3213 info.Location = loc;
3215 // Get the soname.
3216 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3218 std::string soProp = "IMPORTED_SONAME";
3219 soProp += suffix;
3220 if(const char* config_soname = this->GetProperty(soProp.c_str()))
3222 info.SOName = config_soname;
3224 else if(const char* soname = this->GetProperty("IMPORTED_SONAME"))
3226 info.SOName = soname;
3230 // Get the "no-soname" mark.
3231 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3233 std::string soProp = "IMPORTED_NO_SONAME";
3234 soProp += suffix;
3235 if(const char* config_no_soname = this->GetProperty(soProp.c_str()))
3237 info.NoSOName = cmSystemTools::IsOn(config_no_soname);
3239 else if(const char* no_soname = this->GetProperty("IMPORTED_NO_SONAME"))
3241 info.NoSOName = cmSystemTools::IsOn(no_soname);
3245 // Get the import library.
3246 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3247 this->IsExecutableWithExports())
3249 std::string impProp = "IMPORTED_IMPLIB";
3250 impProp += suffix;
3251 if(const char* config_implib = this->GetProperty(impProp.c_str()))
3253 info.ImportLibrary = config_implib;
3255 else if(const char* implib = this->GetProperty("IMPORTED_IMPLIB"))
3257 info.ImportLibrary = implib;
3261 // Get the link interface.
3263 std::string linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
3264 linkProp += suffix;
3265 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3267 cmSystemTools::ExpandListArgument(config_libs,
3268 info.LinkInterface.Libraries);
3270 else if(const char* libs =
3271 this->GetProperty("IMPORTED_LINK_INTERFACE_LIBRARIES"))
3273 cmSystemTools::ExpandListArgument(libs,
3274 info.LinkInterface.Libraries);
3278 // Get the link dependencies.
3280 std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
3281 linkProp += suffix;
3282 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3284 cmSystemTools::ExpandListArgument(config_libs,
3285 info.LinkInterface.SharedDeps);
3287 else if(const char* libs =
3288 this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES"))
3290 cmSystemTools::ExpandListArgument(libs, info.LinkInterface.SharedDeps);
3295 //----------------------------------------------------------------------------
3296 cmTargetLinkInterface const* cmTarget::GetLinkInterface(const char* config)
3298 // Imported targets have their own link interface.
3299 if(this->IsImported())
3301 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
3303 return &info->LinkInterface;
3305 return 0;
3308 // Link interfaces are supported only for shared libraries and
3309 // executables that export symbols.
3310 if((this->GetType() != cmTarget::SHARED_LIBRARY &&
3311 !this->IsExecutableWithExports()))
3313 return 0;
3316 // Lookup any existing link interface for this configuration.
3317 std::map<cmStdString, cmTargetLinkInterface*>::iterator
3318 i = this->LinkInterface.find(config?config:"");
3319 if(i == this->LinkInterface.end())
3321 // Compute the link interface for this configuration.
3322 cmTargetLinkInterface* iface = this->ComputeLinkInterface(config);
3324 // Store the information for this configuration.
3325 std::map<cmStdString, cmTargetLinkInterface*>::value_type
3326 entry(config?config:"", iface);
3327 i = this->LinkInterface.insert(entry).first;
3330 return i->second;
3333 //----------------------------------------------------------------------------
3334 cmTargetLinkInterface* cmTarget::ComputeLinkInterface(const char* config)
3336 // Construct the property name suffix for this configuration.
3337 std::string suffix = "_";
3338 if(config && *config)
3340 suffix += cmSystemTools::UpperCase(config);
3342 else
3344 suffix += "NOCONFIG";
3347 // Lookup the link interface libraries.
3348 const char* libs = 0;
3350 // Lookup the per-configuration property.
3351 std::string propName = "LINK_INTERFACE_LIBRARIES";
3352 propName += suffix;
3353 libs = this->GetProperty(propName.c_str());
3355 // If not set, try the generic property.
3356 if(!libs)
3358 libs = this->GetProperty("LINK_INTERFACE_LIBRARIES");
3362 // If still not set, there is no link interface.
3363 if(!libs)
3365 return 0;
3368 // Allocate the interface.
3369 cmTargetLinkInterface* iface = new cmTargetLinkInterface;
3370 if(!iface)
3372 return 0;
3375 // Expand the list of libraries in the interface.
3376 cmSystemTools::ExpandListArgument(libs, iface->Libraries);
3378 // Now we need to construct a list of shared library dependencies
3379 // not included in the interface.
3380 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3382 // Use a set to keep track of what libraries have been emitted to
3383 // either list.
3384 std::set<cmStdString> emitted;
3385 for(std::vector<std::string>::const_iterator
3386 li = iface->Libraries.begin();
3387 li != iface->Libraries.end(); ++li)
3389 emitted.insert(*li);
3392 // Compute which library configuration to link.
3393 cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
3394 if(config && cmSystemTools::UpperCase(config) == "DEBUG")
3396 linkType = cmTarget::DEBUG;
3399 // Construct the list of libs linked for this configuration.
3400 cmTarget::LinkLibraryVectorType const& llibs =
3401 this->GetOriginalLinkLibraries();
3402 for(cmTarget::LinkLibraryVectorType::const_iterator li = llibs.begin();
3403 li != llibs.end(); ++li)
3405 // Skip entries that will resolve to the target itself, are empty,
3406 // or are not meant for this configuration.
3407 if(li->first == this->GetName() || li->first.empty() ||
3408 !(li->second == cmTarget::GENERAL || li->second == linkType))
3410 continue;
3413 // Skip entries that have already been emitted into either list.
3414 if(!emitted.insert(li->first).second)
3416 continue;
3419 // Add this entry if it is a shared library.
3420 if(cmTarget* tgt = this->Makefile->FindTargetToUse(li->first.c_str()))
3422 if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
3424 iface->SharedDeps.push_back(li->first);
3427 else
3429 // TODO: Recognize shared library file names. Perhaps this
3430 // should be moved to cmComputeLinkInformation, but that creates
3431 // a chicken-and-egg problem since this list is needed for its
3432 // construction.
3437 // Return the completed interface.
3438 return iface;
3441 //----------------------------------------------------------------------------
3442 cmComputeLinkInformation*
3443 cmTarget::GetLinkInformation(const char* config)
3445 // Link information does not make sense for static libraries.
3446 assert(this->GetType() != cmTarget::STATIC_LIBRARY);
3448 // Lookup any existing information for this configuration.
3449 std::map<cmStdString, cmComputeLinkInformation*>::iterator
3450 i = this->LinkInformation.find(config?config:"");
3451 if(i == this->LinkInformation.end())
3453 // Compute information for this configuration.
3454 cmComputeLinkInformation* info =
3455 new cmComputeLinkInformation(this, config);
3456 if(!info || !info->Compute())
3458 delete info;
3459 info = 0;
3462 // Store the information for this configuration.
3463 std::map<cmStdString, cmComputeLinkInformation*>::value_type
3464 entry(config?config:"", info);
3465 i = this->LinkInformation.insert(entry).first;
3467 return i->second;
3470 //----------------------------------------------------------------------------
3471 cmTargetLinkInformationMap
3472 ::cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r): derived()
3474 // Ideally cmTarget instances should never be copied. However until
3475 // we can make a sweep to remove that, this copy constructor avoids
3476 // allowing the resources (LinkInformation) from getting copied. In
3477 // the worst case this will lead to extra cmComputeLinkInformation
3478 // instances. We also enforce in debug mode that the map be emptied
3479 // when copied.
3480 static_cast<void>(r);
3481 assert(r.empty());
3484 //----------------------------------------------------------------------------
3485 cmTargetLinkInformationMap::~cmTargetLinkInformationMap()
3487 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3489 delete i->second;
3493 //----------------------------------------------------------------------------
3494 cmTargetLinkInterfaceMap
3495 ::cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap const& r): derived()
3497 // Ideally cmTarget instances should never be copied. However until
3498 // we can make a sweep to remove that, this copy constructor avoids
3499 // allowing the resources (LinkInterface) from getting copied. In
3500 // the worst case this will lead to extra cmTargetLinkInterface
3501 // instances. We also enforce in debug mode that the map be emptied
3502 // when copied.
3503 static_cast<void>(r);
3504 assert(r.empty());
3507 //----------------------------------------------------------------------------
3508 cmTargetLinkInterfaceMap::~cmTargetLinkInterfaceMap()
3510 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3512 delete i->second;
3516 //----------------------------------------------------------------------------
3517 cmTargetInternalPointer::cmTargetInternalPointer()
3519 this->Pointer = new cmTargetInternals;
3522 //----------------------------------------------------------------------------
3523 cmTargetInternalPointer
3524 ::cmTargetInternalPointer(cmTargetInternalPointer const&)
3526 // Ideally cmTarget instances should never be copied. However until
3527 // we can make a sweep to remove that, this copy constructor avoids
3528 // allowing the resources (Internals) to be copied.
3529 this->Pointer = new cmTargetInternals;
3532 //----------------------------------------------------------------------------
3533 cmTargetInternalPointer::~cmTargetInternalPointer()
3535 delete this->Pointer;
3538 //----------------------------------------------------------------------------
3539 cmTargetInternalPointer&
3540 cmTargetInternalPointer::operator=(cmTargetInternalPointer const& r)
3542 if(this == &r) { return *this; } // avoid warning on HP about self check
3543 // Ideally cmTarget instances should never be copied. However until
3544 // we can make a sweep to remove that, this copy constructor avoids
3545 // allowing the resources (Internals) to be copied.
3546 cmTargetInternals* oldPointer = this->Pointer;
3547 this->Pointer = new cmTargetInternals;
3548 delete oldPointer;
3549 return *this;