Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmTarget.cxx
blobf8300c9e4f6af8b2dfc52ed23ca7ac2d6d5270e0
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.cxx,v $
5 Language: C++
6 Date: $Date: 2008/02/01 13:56:00 $
7 Version: $Revision: 1.190 $
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 cmTarget::cmTarget()
38 this->Makefile = 0;
39 this->LinkLibrariesAnalyzed = false;
40 this->HaveInstallRule = false;
41 this->DLLPlatform = false;
42 this->IsImportedTarget = false;
45 //----------------------------------------------------------------------------
46 void cmTarget::DefineProperties(cmake *cm)
48 cm->DefineProperty
49 ("BUILD_WITH_INSTALL_RPATH", cmProperty::TARGET,
50 "Should build tree targets have install tree rpaths.",
51 "BUILD_WITH_INSTALL_RPATH is a boolean specifying whether to link "
52 "the target in the build tree with the INSTALL_RPATH. This takes "
53 "precedence over SKIP_BUILD_RPATH and avoids the need for relinking "
54 "before installation.");
56 cm->DefineProperty
57 ("CLEAN_DIRECT_OUTPUT", cmProperty::TARGET,
58 "Do not delete other variants of this target.",
59 "When a library is built CMake by default generates code to remove "
60 "any existing library using all possible names. This is needed "
61 "to support libraries that switch between STATIC and SHARED by "
62 "a user option. However when using OUTPUT_NAME to build a static "
63 "and shared library of the same name using different logical target "
64 "names the two targets will remove each other's files. This can be "
65 "prevented by setting the CLEAN_DIRECT_OUTPUT property to 1.");
67 cm->DefineProperty
68 ("COMPILE_FLAGS", cmProperty::TARGET,
69 "Additional flags to use when compiling this target's sources.",
70 "The COMPILE_FLAGS property sets additional compiler flags used "
71 "to build sources within the target. Use COMPILE_DEFINITIONS "
72 "to pass additional preprocessor definitions.");
74 cm->DefineProperty
75 ("COMPILE_DEFINITIONS", cmProperty::TARGET,
76 "Preprocessor definitions for compiling a target's sources.",
77 "The COMPILE_DEFINITIONS property may be set to a list of preprocessor "
78 "definitions using the syntax VAR or VAR=value. Function-style "
79 "definitions are not supported. CMake will automatically escape "
80 "the value correctly for the native build system (note that CMake "
81 "language syntax may require escapes to specify some values). "
82 "This property may be set on a per-configuration basis using the name "
83 "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
84 "(ex. \"COMPILE_DEFINITIONS_DEBUG\").\n"
85 "CMake will automatically drop some definitions that "
86 "are not supported by the native build tool. "
87 "The VS6 IDE does not support definitions with values "
88 "(but NMake does).\n"
89 "Dislaimer: Most native build tools have poor support for escaping "
90 "certain values. CMake has work-arounds for many cases but some "
91 "values may just not be possible to pass correctly. If a value "
92 "does not seem to be escaped correctly, do not attempt to "
93 "work-around the problem by adding escape sequences to the value. "
94 "Your work-around may break in a future version of CMake that "
95 "has improved escape support. Instead consider defining the macro "
96 "in a (configured) header file. Then report the limitation.");
98 cm->DefineProperty
99 ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::TARGET,
100 "Per-configuration preprocessor definitions on a target.",
101 "This is the configuration-specific version of COMPILE_DEFINITIONS.");
103 cm->DefineProperty
104 ("DEFINE_SYMBOL", cmProperty::TARGET,
105 "Define a symbol when compiling this target's sources.",
106 "DEFINE_SYMBOL sets the name of the preprocessor symbol defined when "
107 "compiling sources in a shared library. "
108 "If not set here then it is set to target_EXPORTS by default "
109 "(with some substitutions if the target is not a valid C "
110 "identifier). This is useful for headers to know whether they are "
111 "being included from inside their library our outside to properly "
112 "setup dllexport/dllimport decorations. ");
114 cm->DefineProperty
115 ("DEBUG_POSTFIX", cmProperty::TARGET,
116 "A postfix that will be applied to this target when build debug.",
117 "A property on a target that specifies a postfix to add to the "
118 "target name when built in debug mode. For example foo.dll "
119 "versus fooD.dll");
121 cm->DefineProperty
122 ("EchoString", cmProperty::TARGET,
123 "A message to be displayed when the target is built.",
124 "A message to display on some generators (such as makefiles) when "
125 "the target is built.");
127 cm->DefineProperty
128 ("FRAMEWORK", cmProperty::TARGET,
129 "This target is a framework on the Mac.",
130 "If a shared library target has this property set to true it will "
131 "be built as a framework when built on the mac. It will have the "
132 "directory structure required for a framework and will be suitable "
133 "to be used with the -framework option");
135 cm->DefineProperty
136 ("HAS_CXX", cmProperty::TARGET,
137 "Force a target to use the CXX linker.",
138 "Setting HAS_CXX on a target will force the target to use the "
139 "C++ linker (and C++ runtime libraries) for linking even if the "
140 "target has no C++ code in it.");
142 cm->DefineProperty
143 ("IMPORT_PREFIX", cmProperty::TARGET,
144 "What comes before the import library name.",
145 "Similar to the target property PREFIX, but used for import libraries "
146 "(typically corresponding to a DLL) instead of regular libraries. "
147 "A target property that can be set to override the prefix "
148 "(such as \"lib\") on an import library name.");
150 cm->DefineProperty
151 ("IMPORT_SUFFIX", cmProperty::TARGET,
152 "What comes after the import library name.",
153 "Similar to the target property SUFFIX, but used for import libraries "
154 "(typically corresponding to a DLL) instead of regular libraries. "
155 "A target property that can be set to override the suffix "
156 "(such as \".lib\") on an import library name.");
158 cm->DefineProperty
159 ("IMPORTED", cmProperty::TARGET,
160 "Read-only indication of whether a target is IMPORTED.",
161 "The boolean value of this property is true for targets created with "
162 "the IMPORTED option to add_executable or add_library. "
163 "It is false for targets built within the project.");
165 cm->DefineProperty
166 ("IMPORTED_CONFIGURATIONS", cmProperty::TARGET,
167 "Configurations provided for an IMPORTED target.",
168 "Lists configuration names available for an IMPORTED target. "
169 "The names correspond to configurations defined in the project from "
170 "which the target is imported. "
171 "If the importing project uses a different set of configurations "
172 "the names may be mapped using the MAP_IMPORTED_CONFIG_<CONFIG> "
173 "property. "
174 "Ignored for non-imported targets.");
176 cm->DefineProperty
177 ("IMPORTED_IMPLIB", cmProperty::TARGET,
178 "Full path to the import library for an IMPORTED target.",
179 "Specifies the location of the \".lib\" part of a windows DLL. "
180 "Ignored for non-imported targets.");
182 cm->DefineProperty
183 ("IMPORTED_IMPLIB_<CONFIG>", cmProperty::TARGET,
184 "Per-configuration version of IMPORTED_IMPLIB property.",
185 "This property is used when loading settings for the <CONFIG> "
186 "configuration of an imported target. "
187 "Configuration names correspond to those provided by the project "
188 "from which the target is imported.");
190 cm->DefineProperty
191 ("IMPORTED_LINK_DEPENDENT_LIBRARIES", cmProperty::TARGET,
192 "Dependent shared libraries of an imported shared library.",
193 "Shared libraries may be linked to other shared libraries as part "
194 "of their implementation. On some platforms the linker searches "
195 "for the dependent libraries of shared libraries they are including "
196 "in the link. This property lists "
197 "the dependent shared libraries of an imported library. The list "
198 "should be disjoint from the list of interface libraries in the "
199 "IMPORTED_LINK_INTERFACE_LIBRARIES property. On platforms requiring "
200 "dependent shared libraries to be found at link time CMake uses this "
201 "list to add appropriate files or paths to the link command line. "
202 "Ignored for non-imported targets.");
204 cm->DefineProperty
205 ("IMPORTED_LINK_DEPENDENT_LIBRARIES_<CONFIG>", cmProperty::TARGET,
206 "Per-configuration version of IMPORTED_LINK_DEPENDENT_LIBRARIES.",
207 "This property is used when loading settings for the <CONFIG> "
208 "configuration of an imported target. "
209 "Configuration names correspond to those provided by the project "
210 "from which the target is imported.");
212 cm->DefineProperty
213 ("IMPORTED_LINK_INTERFACE_LIBRARIES", cmProperty::TARGET,
214 "Transitive link interface of an IMPORTED target.",
215 "Lists libraries whose interface is included when an IMPORTED library "
216 "target is linked to another target. "
217 "The libraries will be included on the link line for the target. "
218 "Ignored for non-imported targets.");
220 cm->DefineProperty
221 ("IMPORTED_LINK_INTERFACE_LIBRARIES_<CONFIG>", cmProperty::TARGET,
222 "Per-configuration version of IMPORTED_LINK_INTERFACE_LIBRARIES.",
223 "This property is used when loading settings for the <CONFIG> "
224 "configuration of an imported target. "
225 "Configuration names correspond to those provided by the project "
226 "from which the target is imported.");
228 cm->DefineProperty
229 ("IMPORTED_LOCATION", cmProperty::TARGET,
230 "Full path to the main file on disk for an IMPORTED target.",
231 "Specifies the location of an IMPORTED target file on disk. "
232 "For executables this is the location of the executable file. "
233 "For bundles on OS X this is the location of the executable file "
234 "inside Contents/MacOS under the application bundle folder. "
235 "For static libraries and modules this is the location of the "
236 "library or module. "
237 "For shared libraries on non-DLL platforms this is the location of "
238 "the shared library. "
239 "For frameworks on OS X this is the location of the library file "
240 "symlink just inside the framework folder. "
241 "For DLLs this is the location of the \".dll\" part of the library. "
242 "Ignored for non-imported targets.");
244 cm->DefineProperty
245 ("IMPORTED_LOCATION_<CONFIG>", cmProperty::TARGET,
246 "Per-configuration version of IMPORTED_LOCATION property.",
247 "This property is used when loading settings for the <CONFIG> "
248 "configuration of an imported target. "
249 "Configuration names correspond to those provided by the project "
250 "from which the target is imported.");
252 cm->DefineProperty
253 ("IMPORTED_SONAME", cmProperty::TARGET,
254 "The \"soname\" of an IMPORTED target of shared library type.",
255 "Specifies the \"soname\" embedded in an imported shared library. "
256 "This is meaningful only on platforms supporting the feature. "
257 "Ignored for non-imported targets.");
259 cm->DefineProperty
260 ("IMPORTED_SONAME_<CONFIG>", cmProperty::TARGET,
261 "Per-configuration version of IMPORTED_SONAME property.",
262 "This property is used when loading settings for the <CONFIG> "
263 "configuration of an imported target. "
264 "Configuration names correspond to those provided by the project "
265 "from which the target is imported.");
267 cm->DefineProperty
268 ("EXCLUDE_FROM_ALL", cmProperty::TARGET,
269 "Exclude the target from the all target.",
270 "A property on a target that indicates if the target is excluded "
271 "from the default build target. If it is not, then with a Makefile "
272 "for example typing make will cause this target to be built. "
273 "The same concept applies to the default build of other generators. "
274 "Installing a target with EXCLUDE_FROM_ALL set to true has "
275 "undefined behavior.");
277 cm->DefineProperty
278 ("INSTALL_NAME_DIR", cmProperty::TARGET,
279 "Mac OSX directory name for installed targets.",
280 "INSTALL_NAME_DIR is a string specifying the "
281 "directory portion of the \"install_name\" field of shared libraries "
282 "on Mac OSX to use in the installed targets. ");
284 cm->DefineProperty
285 ("INSTALL_RPATH", cmProperty::TARGET,
286 "The rpath to use for installed targets.",
287 "A semicolon-separated list specifying the rpath "
288 "to use in installed targets (for platforms that support it).");
290 cm->DefineProperty
291 ("INSTALL_RPATH_USE_LINK_PATH", cmProperty::TARGET,
292 "Add paths to linker search and installed rpath.",
293 "INSTALL_RPATH_USE_LINK_PATH is a boolean that if set to true will "
294 "append directories in the linker search path and outside the "
295 "project to the INSTALL_RPATH. ");
297 cm->DefineProperty
298 ("LINK_FLAGS", cmProperty::TARGET,
299 "Additional flags to use when linking this target.",
300 "The LINK_FLAGS property can be used to add extra flags to the "
301 "link step of a target. LINK_FLAGS_<CONFIG> will add to the "
302 "configuration <CONFIG>, "
303 "for example, DEBUG, RELEASE, MINSIZEREL, RELWITHDEBINFO. ");
305 cm->DefineProperty
306 ("LINK_FLAGS_<CONFIG>", cmProperty::TARGET,
307 "Per-configuration linker flags for a target.",
308 "This is the configuration-specific version of LINK_FLAGS.");
310 cm->DefineProperty
311 ("LINK_SEARCH_END_STATIC", cmProperty::TARGET,
312 "End a link line such that static system libraries are used.",
313 "Some linkers support switches such as -Bstatic and -Bdynamic "
314 "to determine whether to use static or shared libraries for -lXXX "
315 "options. CMake uses these options to set the link type for "
316 "libraries whose full paths are not known or (in some cases) are in "
317 "implicit link directories for the platform. By default the "
318 "linker search type is left at -Bdynamic by the end of the library "
319 "list. This property switches the final linker search type to "
320 "-Bstatic.");
322 cm->DefineProperty
323 ("LINKER_LANGUAGE", cmProperty::TARGET,
324 "What tool to use for linking, based on language.",
325 "The LINKER_LANGUAGE property is used to change the tool "
326 "used to link an executable or shared library. The default is "
327 "set the language to match the files in the library. CXX and C "
328 "are common values for this property.");
330 cm->DefineProperty
331 ("LOCATION", cmProperty::TARGET,
332 "Deprecated. Use LOCATION_<CONFIG> or avoid altogether.",
333 "This property is provided for compatibility with CMake 2.4 and below. "
334 "It was meant to get the location of an executable target's output file "
335 "for use in add_custom_command. "
336 "In CMake 2.6 and above add_custom_command automatically recognizes a "
337 "target name in its COMMAND and DEPENDS options and computes the "
338 "target location. Therefore this property need not be used. "
339 "This property is not defined for IMPORTED targets because they "
340 "were not available in CMake 2.4 or below anyway.");
342 cm->DefineProperty
343 ("LOCATION_<CONFIG>", cmProperty::TARGET,
344 "Read-only property providing a target location on disk.",
345 "A read-only property that indicates where a target's main file is "
346 "located on disk for the configuration <CONFIG>. "
347 "The property is defined only for library and executable targets.");
349 cm->DefineProperty
350 ("LINK_INTERFACE_LIBRARIES", cmProperty::TARGET,
351 "List public interface libraries for a shared library or executable.",
352 "By default linking to a shared library target transitively "
353 "links to targets with which the library itself was linked. "
354 "For an executable with exports (see the ENABLE_EXPORTS property) "
355 "no default transitive link dependencies are used. "
356 "This property replaces the default transitive link dependencies with "
357 "an explict list. "
358 "When the target is linked into another target the libraries "
359 "listed (and recursively their link interface libraries) will be "
360 "provided to the other target also. "
361 "If the list is empty then no transitive link dependencies will be "
362 "incorporated when this target is linked into another target even if "
363 "the default set is non-empty.");
365 cm->DefineProperty
366 ("LINK_INTERFACE_LIBRARIES_<CONFIG>", cmProperty::TARGET,
367 "Per-configuration list of public interface libraries for a target.",
368 "This is the configuration-specific version of "
369 "LINK_INTERFACE_LIBRARIES.");
371 cm->DefineProperty
372 ("MAP_IMPORTED_CONFIG_<CONFIG>", cmProperty::TARGET,
373 "Map from project configuration to IMPORTED target's configuration.",
374 "List configurations of an imported target that may be used for "
375 "the current project's <CONFIG> configuration. "
376 "Targets imported from another project may not provide the same set "
377 "of configuration names available in the current project. "
378 "Setting this property tells CMake what imported configurations are "
379 "suitable for use when building the <CONFIG> configuration. "
380 "The first configuration in the list found to be provided by the "
381 "imported target is selected. If no matching configurations are "
382 "available the imported target is considered to be not found. "
383 "This property is ignored for non-imported targets.",
384 false /* TODO: make this chained */ );
386 cm->DefineProperty
387 ("OUTPUT_NAME", cmProperty::TARGET,
388 "Sets the real name of a target when it is built.",
389 "Sets the real name of a target when it is built and "
390 "can be used to help create two targets of the same name even though "
391 "CMake requires unique logical target names. There is also a "
392 "<CONFIG>_OUTPUT_NAME that can set the output name on a "
393 "per-configuration basis.");
395 cm->DefineProperty
396 ("PRE_INSTALL_SCRIPT", cmProperty::TARGET,
397 "Deprecated install support.",
398 "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
399 "old way to specify CMake scripts to run before and after "
400 "installing a target. They are used only when the old "
401 "INSTALL_TARGETS command is used to install the target. Use the "
402 "INSTALL command instead.");
404 cm->DefineProperty
405 ("PREFIX", cmProperty::TARGET,
406 "What comes before the library name.",
407 "A target property that can be set to override the prefix "
408 "(such as \"lib\") on a library name.");
410 cm->DefineProperty
411 ("POST_INSTALL_SCRIPT", cmProperty::TARGET,
412 "Deprecated install support.",
413 "The PRE_INSTALL_SCRIPT and POST_INSTALL_SCRIPT properties are the "
414 "old way to specify CMake scripts to run before and after "
415 "installing a target. They are used only when the old "
416 "INSTALL_TARGETS command is used to install the target. Use the "
417 "INSTALL command instead.");
419 cm->DefineProperty
420 ("PRIVATE_HEADER", cmProperty::TARGET,
421 "Specify private header files in a FRAMEWORK shared library target.",
422 "Shared library targets marked with the FRAMEWORK property generate "
423 "frameworks on OS X and normal shared libraries on other platforms. "
424 "This property may be set to a list of header files to be placed "
425 "in the PrivateHeaders directory inside the framework folder. "
426 "On non-Apple platforms these headers may be installed using the "
427 "PRIVATE_HEADER option to the install(TARGETS) command.");
429 cm->DefineProperty
430 ("PUBLIC_HEADER", cmProperty::TARGET,
431 "Specify public header files in a FRAMEWORK shared library target.",
432 "Shared library targets marked with the FRAMEWORK property generate "
433 "frameworks on OS X and normal shared libraries on other platforms. "
434 "This property may be set to a list of header files to be placed "
435 "in the Headers directory inside the framework folder. "
436 "On non-Apple platforms these headers may be installed using the "
437 "PUBLIC_HEADER option to the install(TARGETS) command.");
439 cm->DefineProperty
440 ("RESOURCE", cmProperty::TARGET,
441 "Specify resource files in a FRAMEWORK shared library target.",
442 "Shared library targets marked with the FRAMEWORK property generate "
443 "frameworks on OS X and normal shared libraries on other platforms. "
444 "This property may be set to a list of files to be placed "
445 "in the Resources directory inside the framework folder. "
446 "On non-Apple platforms these files may be installed using the "
447 "RESOURCE option to the install(TARGETS) command.");
449 cm->DefineProperty
450 ("SKIP_BUILD_RPATH", cmProperty::TARGET,
451 "Should rpaths be used for the build tree.",
452 "SKIP_BUILD_RPATH is a boolean specifying whether to skip automatic "
453 "generation of an rpath allowing the target to run from the "
454 "build tree. ");
456 cm->DefineProperty
457 ("SOVERSION", cmProperty::TARGET,
458 "What version number is this target.",
459 "For shared libraries VERSION and SOVERSION can be used to specify "
460 "the build version and api version respectively. When building or "
461 "installing appropriate symlinks are created if the platform "
462 "supports symlinks and the linker supports so-names. "
463 "If only one of both is specified the missing is assumed to have "
464 "the same version number. "
465 "For shared libraries and executables on Windows the VERSION "
466 "attribute is parsed to extract a \"major.minor\" version number. "
467 "These numbers are used as the image version of the binary. ");
469 cm->DefineProperty
470 ("STATIC_LIBRARY_FLAGS", cmProperty::TARGET,
471 "Extra flags to use when linking static libraries.",
472 "Extra flags to use when linking a static library.");
474 cm->DefineProperty
475 ("SUFFIX", cmProperty::TARGET,
476 "What comes after the library name.",
477 "A target property that can be set to override the suffix "
478 "(such as \".so\") on a library name.");
480 cm->DefineProperty
481 ("VERSION", cmProperty::TARGET,
482 "What version number is this target.",
483 "For shared libraries VERSION and SOVERSION can be used to specify "
484 "the build version and api version respectively. When building or "
485 "installing appropriate symlinks are created if the platform "
486 "supports symlinks and the linker supports so-names. "
487 "If only one of both is specified the missing is assumed to have "
488 "the same version number. "
489 "For executables VERSION can be used to specify the build version. "
490 "When building or installing appropriate symlinks are created if "
491 "the platform supports symlinks. "
492 "For shared libraries and executables on Windows the VERSION "
493 "attribute is parsed to extract a \"major.minor\" version number. "
494 "These numbers are used as the image version of the binary. ");
497 cm->DefineProperty
498 ("WIN32_EXECUTABLE", cmProperty::TARGET,
499 "Used to specify Windows executable with a WinMain entry point.",
500 "This can be set to indicate that a target is a Windows executable "
501 "in contrast to a console application for example. This changes "
502 "how the executable will be linked.");
504 cm->DefineProperty
505 ("ENABLE_EXPORTS", cmProperty::TARGET,
506 "Specify whether an executable exports symbols for loadable modules.",
507 "Normally an executable does not export any symbols because it is "
508 "the final program. It is possible for an executable to export "
509 "symbols to be used by loadable modules. When this property is "
510 "set to true CMake will allow other targets to \"link\" to the "
511 "executable with the TARGET_LINK_LIBRARIES command. "
512 "On all platforms a target-level dependency on the executable is "
513 "created for targets that link to it. "
514 "For non-DLL platforms the link rule is simply ignored since "
515 "the dynamic loader will automatically bind symbols when the "
516 "module is loaded. "
517 "For DLL platforms an import library will be created for the "
518 "exported symbols and then used for linking. "
519 "All Windows-based systems including Cygwin are DLL platforms.");
521 cm->DefineProperty
522 ("Fortran_MODULE_DIRECTORY", cmProperty::TARGET,
523 "Specify output directory for Fortran modules provided by the target.",
524 "If the target contains Fortran source files that provide modules "
525 "and the compiler supports a module output directory this specifies "
526 "the directory in which the modules will be placed. "
527 "When this property is not set the modules will be placed in the "
528 "build directory corresponding to the target's source directory.");
530 cm->DefineProperty
531 ("XCODE_ATTRIBUTE_<an-attribute>", cmProperty::TARGET,
532 "Set Xcode target attributes directly.",
533 "Tell the Xcode generator to set '<an-attribute>' to a given value "
534 "in the generated Xcode project. Ignored on other generators.");
536 cm->DefineProperty
537 ("GENERATOR_FILE_NAME", cmProperty::TARGET,
538 "Generator's file for this target.",
539 "An internal property used by some generators to record the name of "
540 "project or dsp file associated with this target.");
542 cm->DefineProperty
543 ("SOURCES", cmProperty::TARGET,
544 "Source names specified for a target.",
545 "Read-only list of sources specified for a target. "
546 "The names returned are suitable for passing to the "
547 "set_source_files_properties command.");
549 #if 0
550 cm->DefineProperty
551 ("OBJECT_FILES", cmProperty::TARGET,
552 "Used to get the resulting list of object files that make up a "
553 "target.",
554 "This can be used to put object files from one library "
555 "into another library. It is a read only property. It "
556 "converts the source list for the target into a list of full "
557 "paths to object names that will be produced by the target.");
558 #endif
560 #define CM_TARGET_FILE_TYPES_DOC \
561 "There are three kinds of target files that may be built: " \
562 "archive, library, and runtime. " \
563 "Executables are always treated as runtime targets. " \
564 "Static libraries are always treated as archive targets. " \
565 "Module libraries are always treated as library targets. " \
566 "For non-DLL platforms shared libraries are treated as library " \
567 "targets. " \
568 "For DLL platforms the DLL part of a shared library is treated as " \
569 "a runtime target and the corresponding import library is treated as " \
570 "an archive target. " \
571 "All Windows-based systems including Cygwin are DLL platforms."
573 cm->DefineProperty
574 ("ARCHIVE_OUTPUT_DIRECTORY", cmProperty::TARGET,
575 "Output directory in which to build ARCHIVE target files.",
576 "This property specifies the directory into which archive target files "
577 "should be built. "
578 CM_TARGET_FILE_TYPES_DOC " "
579 "This property is initialized by the value of the variable "
580 "CMAKE_ARCHIVE_OUTPUT_DIRECTORY if it is set when a target is created.");
581 cm->DefineProperty
582 ("LIBRARY_OUTPUT_DIRECTORY", cmProperty::TARGET,
583 "Output directory in which to build LIBRARY target files.",
584 "This property specifies the directory into which library target files "
585 "should be built. "
586 CM_TARGET_FILE_TYPES_DOC " "
587 "This property is initialized by the value of the variable "
588 "CMAKE_LIBRARY_OUTPUT_DIRECTORY if it is set when a target is created.");
589 cm->DefineProperty
590 ("RUNTIME_OUTPUT_DIRECTORY", cmProperty::TARGET,
591 "Output directory in which to build RUNTIME target files.",
592 "This property specifies the directory into which runtime target files "
593 "should be built. "
594 CM_TARGET_FILE_TYPES_DOC " "
595 "This property is initialized by the value of the variable "
596 "CMAKE_RUNTIME_OUTPUT_DIRECTORY if it is set when a target is created.");
598 // define some properties without documentation
599 cm->DefineProperty("DEBUG_OUTPUT_NAME", cmProperty::TARGET,0,0);
600 cm->DefineProperty("RELEASE_OUTPUT_NAME", cmProperty::TARGET,0,0);
603 void cmTarget::SetType(TargetType type, const char* name)
605 this->Name = name;
606 if(type == cmTarget::INSTALL_FILES ||
607 type == cmTarget::INSTALL_PROGRAMS ||
608 type == cmTarget::INSTALL_DIRECTORY)
610 abort();
612 // only add dependency information for library targets
613 this->TargetTypeValue = type;
614 if(this->TargetTypeValue >= STATIC_LIBRARY
615 && this->TargetTypeValue <= MODULE_LIBRARY)
617 this->RecordDependencies = true;
619 else
621 this->RecordDependencies = false;
625 //----------------------------------------------------------------------------
626 void cmTarget::SetMakefile(cmMakefile* mf)
628 // Set our makefile.
629 this->Makefile = mf;
631 // set the cmake instance of the properties
632 this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
634 // Check whether this is a DLL platform.
635 this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
636 this->Makefile->IsOn("CYGWIN") ||
637 this->Makefile->IsOn("MINGW"));
639 // Setup default property values.
640 this->SetPropertyDefault("INSTALL_NAME_DIR", "");
641 this->SetPropertyDefault("INSTALL_RPATH", "");
642 this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
643 this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
644 this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
645 this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
646 this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
647 this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
648 this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", 0);
650 // Collect the set of configuration types.
651 std::vector<std::string> configNames;
652 if(const char* configurationTypes =
653 mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
655 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
657 else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
659 if(*buildType)
661 configNames.push_back(buildType);
665 // Setup per-configuration property default values.
666 for(std::vector<std::string>::iterator ci = configNames.begin();
667 ci != configNames.end(); ++ci)
669 // Initialize per-configuration name postfix property from the
670 // variable only for non-executable targets. This preserves
671 // compatibility with previous CMake versions in which executables
672 // did not support this variable. Projects may still specify the
673 // property directly. TODO: Make this depend on backwards
674 // compatibility setting.
675 if(this->TargetTypeValue != cmTarget::EXECUTABLE)
677 std::string property = cmSystemTools::UpperCase(*ci);
678 property += "_POSTFIX";
679 this->SetPropertyDefault(property.c_str(), 0);
684 //----------------------------------------------------------------------------
685 bool cmTarget::IsExecutableWithExports()
687 return (this->GetType() == cmTarget::EXECUTABLE &&
688 this->GetPropertyAsBool("ENABLE_EXPORTS"));
691 //----------------------------------------------------------------------------
692 bool cmTarget::IsFrameworkOnApple()
694 return (this->GetType() == cmTarget::SHARED_LIBRARY &&
695 this->Makefile->IsOn("APPLE") &&
696 this->GetPropertyAsBool("FRAMEWORK"));
699 //----------------------------------------------------------------------------
700 bool cmTarget::IsAppBundleOnApple()
702 return (this->GetType() == cmTarget::EXECUTABLE &&
703 this->Makefile->IsOn("APPLE") &&
704 this->GetPropertyAsBool("MACOSX_BUNDLE"));
707 //----------------------------------------------------------------------------
708 class cmTargetTraceDependencies
710 public:
711 cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile);
712 void Trace();
713 private:
714 cmTarget* Target;
715 cmMakefile* Makefile;
716 cmGlobalGenerator* GlobalGenerator;
717 std::queue<cmStdString> DependencyQueue;
718 std::set<cmStdString> DependenciesQueued;
719 std::set<cmSourceFile*> TargetSources;
721 void QueueOnce(std::string const& name);
722 void QueueOnce(std::vector<std::string> const& names);
723 void QueueDependencies(cmSourceFile* sf);
724 bool IsUtility(std::string const& dep);
725 void CheckCustomCommand(cmCustomCommand const& cc);
726 void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
729 //----------------------------------------------------------------------------
730 cmTargetTraceDependencies
731 ::cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile):
732 Target(target)
734 // Convenience.
735 this->Makefile = this->Target->GetMakefile();
736 this->GlobalGenerator =
737 this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
739 // Queue all the source files already specified for the target.
740 std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
741 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
742 si != sources.end(); ++si)
744 // Queue the source file itself in case it is generated.
745 this->QueueOnce((*si)->GetFullPath());
747 // Queue the dependencies of the source file in case they are
748 // generated.
749 this->QueueDependencies(*si);
751 // Track the sources already known to the target.
752 this->TargetSources.insert(*si);
755 // Queue the VS project file to check dependencies on the rule to
756 // generate it.
757 if(vsProjectFile)
759 this->QueueOnce(vsProjectFile);
762 // Queue pre-build, pre-link, and post-build rule dependencies.
763 this->CheckCustomCommands(this->Target->GetPreBuildCommands());
764 this->CheckCustomCommands(this->Target->GetPreLinkCommands());
765 this->CheckCustomCommands(this->Target->GetPostBuildCommands());
768 //----------------------------------------------------------------------------
769 void cmTargetTraceDependencies::Trace()
771 // Process one dependency at a time until the queue is empty.
772 while(!this->DependencyQueue.empty())
774 // Get the next dependency in from queue.
775 std::string dep = this->DependencyQueue.front();
776 this->DependencyQueue.pop();
778 // Check if we know how to generate this dependency.
779 if(cmSourceFile* sf =
780 this->Makefile->GetSourceFileWithOutput(dep.c_str()))
782 // Queue dependencies needed to generate this file.
783 this->QueueDependencies(sf);
785 // Make sure this file is in the target.
786 if(this->TargetSources.insert(sf).second)
788 this->Target->AddSourceFile(sf);
794 //----------------------------------------------------------------------------
795 void cmTargetTraceDependencies::QueueOnce(std::string const& name)
797 if(this->DependenciesQueued.insert(name).second)
799 this->DependencyQueue.push(name);
803 //----------------------------------------------------------------------------
804 void
805 cmTargetTraceDependencies::QueueOnce(std::vector<std::string> const& names)
807 for(std::vector<std::string>::const_iterator i = names.begin();
808 i != names.end(); ++i)
810 this->QueueOnce(*i);
814 //----------------------------------------------------------------------------
815 bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
817 // Dependencies on targets (utilities) are supposed to be named by
818 // just the target name. However for compatibility we support
819 // naming the output file generated by the target (assuming there is
820 // no output-name property which old code would not have set). In
821 // that case the target name will be the file basename of the
822 // dependency.
823 std::string util = cmSystemTools::GetFilenameName(dep);
824 if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
826 util = cmSystemTools::GetFilenameWithoutLastExtension(util);
829 // Check for a non-imported target with this name.
830 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, util.c_str()))
832 // If we find the target and the dep was given as a full path,
833 // then make sure it was not a full path to something else, and
834 // the fact that the name matched a target was just a coincidence.
835 if(cmSystemTools::FileIsFullPath(dep.c_str()))
837 // This is really only for compatibility so we do not need to
838 // worry about configuration names and output names.
839 std::string tLocation = t->GetLocation(0);
840 tLocation = cmSystemTools::GetFilenamePath(tLocation);
841 std::string depLocation = cmSystemTools::GetFilenamePath(dep);
842 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
843 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
844 if(depLocation == tLocation)
846 this->Target->AddUtility(util.c_str());
847 return true;
850 else
852 // The original name of the dependency was not a full path. It
853 // must name a target, so add the target-level dependency.
854 this->Target->AddUtility(util.c_str());
855 return true;
859 // The dependency does not name a target built in this project.
860 return false;
863 //----------------------------------------------------------------------------
864 void cmTargetTraceDependencies::QueueDependencies(cmSourceFile* sf)
866 // Queue dependency added explicitly by the user.
867 if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
869 std::vector<std::string> objDeps;
870 cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
871 this->QueueOnce(objDeps);
874 // Queue dependencies added programatically by commands.
875 this->QueueOnce(sf->GetDepends());
877 // Queue custom command dependencies.
878 if(cmCustomCommand const* cc = sf->GetCustomCommand())
880 this->CheckCustomCommand(*cc);
885 //----------------------------------------------------------------------------
886 void
887 cmTargetTraceDependencies
888 ::CheckCustomCommand(cmCustomCommand const& cc)
890 // Transform command names that reference targets built in this
891 // project to corresponding target-level dependencies.
892 for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
893 cit != cc.GetCommandLines().end(); ++cit)
895 std::string const& command = *cit->begin();
896 // Look for a non-imported target with this name.
897 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, command.c_str()))
899 if(t->GetType() == cmTarget::EXECUTABLE)
901 // The command refers to an executable target built in
902 // this project. Add the target-level dependency to make
903 // sure the executable is up to date before this custom
904 // command possibly runs.
905 this->Target->AddUtility(command.c_str());
910 // Queue the custom command dependencies.
911 std::vector<std::string> const& depends = cc.GetDepends();
912 for(std::vector<std::string>::const_iterator di = depends.begin();
913 di != depends.end(); ++di)
915 std::string const& dep = *di;
916 if(!this->IsUtility(dep))
918 // The dependency does not name a target and may be a file we
919 // know how to generate. Queue it.
920 this->QueueOnce(dep);
925 //----------------------------------------------------------------------------
926 void
927 cmTargetTraceDependencies
928 ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
930 for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
931 cli != commands.end(); ++cli)
933 this->CheckCustomCommand(*cli);
937 //----------------------------------------------------------------------------
938 void cmTarget::TraceDependencies(const char* vsProjectFile)
940 // Use a helper object to trace the dependencies.
941 cmTargetTraceDependencies tracer(this, vsProjectFile);
942 tracer.Trace();
945 //----------------------------------------------------------------------------
946 void cmTarget::AddSources(std::vector<std::string> const& srcs)
948 for(std::vector<std::string>::const_iterator i = srcs.begin();
949 i != srcs.end(); ++i)
951 this->AddSource(i->c_str());
955 //----------------------------------------------------------------------------
956 cmSourceFile* cmTarget::AddSource(const char* s)
958 std::string src = s;
960 // For backwards compatibility replace varibles in source names.
961 // This should eventually be removed.
962 this->Makefile->ExpandVariablesInString(src);
964 cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
965 this->AddSourceFile(sf);
966 return sf;
969 //----------------------------------------------------------------------------
970 struct cmTarget::SourceFileFlags
971 cmTarget::GetTargetSourceFileFlags(const cmSourceFile* sf)
973 struct SourceFileFlags flags;
974 const char* files;
975 std::vector<std::string>::iterator it;
977 flags.PrivateHeader = false;
978 flags.PublicHeader = false;
979 flags.Resource = false;
981 files = this->GetProperty("PRIVATE_HEADER");
982 if ((files) && (*files))
984 std::vector<std::string> relFiles;
985 cmSystemTools::ExpandListArgument(files, relFiles);
986 for(it = relFiles.begin(); it != relFiles.end(); ++it)
988 if(sf == this->GetMakefile()->GetSource(it->c_str()))
990 flags.PrivateHeader = true;
991 break;
996 // Only consider marking it as a public header if it is *NOT* already marked
997 // as a private header:
999 if(!flags.PrivateHeader)
1001 files = this->GetProperty("PUBLIC_HEADER");
1002 if ((files) && (*files))
1004 std::vector<std::string> relFiles;
1005 cmSystemTools::ExpandListArgument(files, relFiles);
1006 for(it = relFiles.begin(); it != relFiles.end(); ++it)
1008 if(sf == this->GetMakefile()->GetSource(it->c_str()))
1010 flags.PublicHeader = true;
1011 break;
1017 const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION");
1018 if(location && cmStdString(location) == "Resources")
1020 flags.Resource = true;
1023 // Don't bother with the loop if it's already marked as a resource:
1025 if(!flags.Resource)
1027 files = this->GetProperty("RESOURCE");
1028 if ((files) && (*files))
1030 std::vector<std::string> relFiles;
1031 cmSystemTools::ExpandListArgument(files, relFiles);
1032 for(it = relFiles.begin(); it != relFiles.end(); ++it)
1034 if(sf == this->GetMakefile()->GetSource(it->c_str()))
1036 flags.Resource = true;
1037 break;
1043 return flags;
1046 //----------------------------------------------------------------------------
1047 void cmTarget::MergeLinkLibraries( cmMakefile& mf,
1048 const char *selfname,
1049 const LinkLibraryVectorType& libs )
1051 // Only add on libraries we haven't added on before.
1052 // Assumption: the global link libraries could only grow, never shrink
1053 LinkLibraryVectorType::const_iterator i = libs.begin();
1054 i += this->PrevLinkedLibraries.size();
1055 for( ; i != libs.end(); ++i )
1057 // We call this so that the dependencies get written to the cache
1058 this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
1060 this->PrevLinkedLibraries = libs;
1063 //----------------------------------------------------------------------------
1064 void cmTarget::AddLinkDirectory(const char* d)
1066 // Make sure we don't add unnecessary search directories.
1067 if(this->LinkDirectoriesEmmitted.insert(d).second)
1069 this->LinkDirectories.push_back(d);
1073 //----------------------------------------------------------------------------
1074 const std::vector<std::string>& cmTarget::GetLinkDirectories()
1076 return this->LinkDirectories;
1079 //----------------------------------------------------------------------------
1080 void cmTarget::ClearDependencyInformation( cmMakefile& mf,
1081 const char* target )
1083 // Clear the dependencies. The cache variable must exist iff we are
1084 // recording dependency information for this target.
1085 std::string depname = target;
1086 depname += "_LIB_DEPENDS";
1087 if (this->RecordDependencies)
1089 mf.AddCacheDefinition(depname.c_str(), "",
1090 "Dependencies for target", cmCacheManager::STATIC);
1092 else
1094 if (mf.GetDefinition( depname.c_str() ))
1096 std::string message = "Target ";
1097 message += target;
1098 message += " has dependency information when it shouldn't.\n";
1099 message += "Your cache is probably stale. Please remove the entry\n ";
1100 message += depname;
1101 message += "\nfrom the cache.";
1102 cmSystemTools::Error( message.c_str() );
1107 //----------------------------------------------------------------------------
1108 void cmTarget::AddLinkLibrary(const std::string& lib,
1109 LinkLibraryType llt)
1111 this->AddFramework(lib.c_str(), llt);
1112 cmTarget::LibraryID tmp;
1113 tmp.first = lib;
1114 tmp.second = llt;
1115 this->LinkLibraries.push_back(tmp);
1116 this->OriginalLinkLibraries.push_back(tmp);
1119 //----------------------------------------------------------------------------
1120 bool cmTarget::NameResolvesToFramework(const std::string& libname)
1122 return this->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
1123 NameResolvesToFramework(libname);
1126 //----------------------------------------------------------------------------
1127 bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
1129 (void)llt; // TODO: What is this?
1130 if(this->NameResolvesToFramework(libname.c_str()))
1132 std::string frameworkDir = libname;
1133 frameworkDir += "/../";
1134 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1135 std::vector<std::string>::iterator i =
1136 std::find(this->Frameworks.begin(),
1137 this->Frameworks.end(), frameworkDir);
1138 if(i == this->Frameworks.end())
1140 this->Frameworks.push_back(frameworkDir);
1142 return true;
1144 return false;
1147 //----------------------------------------------------------------------------
1148 void cmTarget::AddLinkLibrary(cmMakefile& mf,
1149 const char *target, const char* lib,
1150 LinkLibraryType llt)
1152 // Never add a self dependency, even if the user asks for it.
1153 if(strcmp( target, lib ) == 0)
1155 return;
1157 this->AddFramework(lib, llt);
1158 cmTarget::LibraryID tmp;
1159 tmp.first = lib;
1160 tmp.second = llt;
1161 this->LinkLibraries.push_back( tmp );
1162 this->OriginalLinkLibraries.push_back(tmp);
1164 // Add the explicit dependency information for this target. This is
1165 // simply a set of libraries separated by ";". There should always
1166 // be a trailing ";". These library names are not canonical, in that
1167 // they may be "-framework x", "-ly", "/path/libz.a", etc.
1168 // We shouldn't remove duplicates here because external libraries
1169 // may be purposefully duplicated to handle recursive dependencies,
1170 // and we removing one instance will break the link line. Duplicates
1171 // will be appropriately eliminated at emit time.
1172 if(this->RecordDependencies)
1174 std::string targetEntry = target;
1175 targetEntry += "_LIB_DEPENDS";
1176 std::string dependencies;
1177 const char* old_val = mf.GetDefinition( targetEntry.c_str() );
1178 if( old_val )
1180 dependencies += old_val;
1182 switch (llt)
1184 case cmTarget::GENERAL:
1185 dependencies += "general";
1186 break;
1187 case cmTarget::DEBUG:
1188 dependencies += "debug";
1189 break;
1190 case cmTarget::OPTIMIZED:
1191 dependencies += "optimized";
1192 break;
1194 dependencies += ";";
1195 dependencies += lib;
1196 dependencies += ";";
1197 mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
1198 "Dependencies for the target",
1199 cmCacheManager::STATIC );
1204 //----------------------------------------------------------------------------
1205 void
1206 cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
1208 // There are two key parts of the dependency analysis: (1)
1209 // determining the libraries in the link line, and (2) constructing
1210 // the dependency graph for those libraries.
1212 // The latter is done using the cache entries that record the
1213 // dependencies of each library.
1215 // The former is a more thorny issue, since it is not clear how to
1216 // determine if two libraries listed on the link line refer to the a
1217 // single library or not. For example, consider the link "libraries"
1218 // /usr/lib/libtiff.so -ltiff
1219 // Is this one library or two? The solution implemented here is the
1220 // simplest (and probably the only practical) one: two libraries are
1221 // the same if their "link strings" are identical. Thus, the two
1222 // libraries above are considered distinct. This also means that for
1223 // dependency analysis to be effective, the CMake user must specify
1224 // libraries build by his project without using any linker flags or
1225 // file extensions. That is,
1226 // LINK_LIBRARIES( One Two )
1227 // instead of
1228 // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
1229 // The former is probably what most users would do, but it never
1230 // hurts to document the assumptions. :-) Therefore, in the analysis
1231 // code, the "canonical name" of a library is simply its name as
1232 // given to a LINK_LIBRARIES command.
1234 // Also, we will leave the original link line intact; we will just add any
1235 // dependencies that were missing.
1237 // There is a problem with recursive external libraries
1238 // (i.e. libraries with no dependency information that are
1239 // recursively dependent). We must make sure that the we emit one of
1240 // the libraries twice to satisfy the recursion, but we shouldn't
1241 // emit it more times than necessary. In particular, we must make
1242 // sure that handling this improbable case doesn't cost us when
1243 // dealing with the common case of non-recursive libraries. The
1244 // solution is to assume that the recursion is satisfied at one node
1245 // of the dependency tree. To illustrate, assume libA and libB are
1246 // extrenal and mutually dependent. Suppose libX depends on
1247 // libA, and libY on libA and libX. Then
1248 // TARGET_LINK_LIBRARIES( Y X A B A )
1249 // TARGET_LINK_LIBRARIES( X A B A )
1250 // TARGET_LINK_LIBRARIES( Exec Y )
1251 // would result in "-lY -lX -lA -lB -lA". This is the correct way to
1252 // specify the dependencies, since the mutual dependency of A and B
1253 // is resolved *every time libA is specified*.
1255 // Something like
1256 // TARGET_LINK_LIBRARIES( Y X A B A )
1257 // TARGET_LINK_LIBRARIES( X A B )
1258 // TARGET_LINK_LIBRARIES( Exec Y )
1259 // would result in "-lY -lX -lA -lB", and the mutual dependency
1260 // information is lost. This is because in some case (Y), the mutual
1261 // dependency of A and B is listed, while in another other case (X),
1262 // it is not. Depending on which line actually emits A, the mutual
1263 // dependency may or may not be on the final link line. We can't
1264 // handle this pathalogical case cleanly without emitting extra
1265 // libraries for the normal cases. Besides, the dependency
1266 // information for X is wrong anyway: if we build an executable
1267 // depending on X alone, we would not have the mutual dependency on
1268 // A and B resolved.
1270 // IMPROVEMENTS:
1271 // -- The current algorithm will not always pick the "optimal" link line
1272 // when recursive dependencies are present. It will instead break the
1273 // cycles at an aribtrary point. The majority of projects won't have
1274 // cyclic dependencies, so this is probably not a big deal. Note that
1275 // the link line is always correct, just not necessary optimal.
1278 // Expand variables in link library names. This is for backwards
1279 // compatibility with very early CMake versions and should
1280 // eventually be removed. This code was moved here from the end of
1281 // old source list processing code which was called just before this
1282 // method.
1283 for(LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
1284 p != this->LinkLibraries.end(); ++p)
1286 this->Makefile->ExpandVariablesInString(p->first, true, true);
1290 typedef std::vector< std::string > LinkLine;
1292 // The dependency map.
1293 DependencyMap dep_map;
1295 // 1. Build the dependency graph
1297 for(LinkLibraryVectorType::reverse_iterator lib
1298 = this->LinkLibraries.rbegin();
1299 lib != this->LinkLibraries.rend(); ++lib)
1301 this->GatherDependencies( mf, *lib, dep_map);
1304 // 2. Remove any dependencies that are already satisfied in the original
1305 // link line.
1307 for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
1308 lib != this->LinkLibraries.end(); ++lib)
1310 for( LinkLibraryVectorType::iterator lib2 = lib;
1311 lib2 != this->LinkLibraries.end(); ++lib2)
1313 this->DeleteDependency( dep_map, *lib, *lib2);
1318 // 3. Create the new link line by simply emitting any dependencies that are
1319 // missing. Start from the back and keep adding.
1321 std::set<DependencyMap::key_type> done, visited;
1322 std::vector<DependencyMap::key_type> newLinkLibraries;
1323 for(LinkLibraryVectorType::reverse_iterator lib =
1324 this->LinkLibraries.rbegin();
1325 lib != this->LinkLibraries.rend(); ++lib)
1327 // skip zero size library entries, this may happen
1328 // if a variable expands to nothing.
1329 if (lib->first.size() != 0)
1331 this->Emit( *lib, dep_map, done, visited, newLinkLibraries );
1335 // 4. Add the new libraries to the link line.
1337 for( std::vector<DependencyMap::key_type>::reverse_iterator k =
1338 newLinkLibraries.rbegin();
1339 k != newLinkLibraries.rend(); ++k )
1341 // get the llt from the dep_map
1342 this->LinkLibraries.push_back( std::make_pair(k->first,k->second) );
1344 this->LinkLibrariesAnalyzed = true;
1347 //----------------------------------------------------------------------------
1348 void cmTarget::InsertDependency( DependencyMap& depMap,
1349 const LibraryID& lib,
1350 const LibraryID& dep)
1352 depMap[lib].push_back(dep);
1355 //----------------------------------------------------------------------------
1356 void cmTarget::DeleteDependency( DependencyMap& depMap,
1357 const LibraryID& lib,
1358 const LibraryID& dep)
1360 // Make sure there is an entry in the map for lib. If so, delete all
1361 // dependencies to dep. There may be repeated entries because of
1362 // external libraries that are specified multiple times.
1363 DependencyMap::iterator map_itr = depMap.find( lib );
1364 if( map_itr != depMap.end() )
1366 DependencyList& depList = map_itr->second;
1367 DependencyList::iterator itr;
1368 while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
1369 depList.end() )
1371 depList.erase( itr );
1376 //----------------------------------------------------------------------------
1377 void cmTarget::Emit(const LibraryID lib,
1378 const DependencyMap& dep_map,
1379 std::set<LibraryID>& emitted,
1380 std::set<LibraryID>& visited,
1381 DependencyList& link_line )
1383 // It's already been emitted
1384 if( emitted.find(lib) != emitted.end() )
1386 return;
1389 // Emit the dependencies only if this library node hasn't been
1390 // visited before. If it has, then we have a cycle. The recursion
1391 // that got us here should take care of everything.
1393 if( visited.insert(lib).second )
1395 if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
1397 const DependencyList& dep_on = dep_map.find( lib )->second;
1398 DependencyList::const_reverse_iterator i;
1400 // To cater for recursive external libraries, we must emit
1401 // duplicates on this link line *unless* they were emitted by
1402 // some other node, in which case we assume that the recursion
1403 // was resolved then. We making the simplifying assumption that
1404 // any duplicates on a single link line are on purpose, and must
1405 // be preserved.
1407 // This variable will keep track of the libraries that were
1408 // emitted directory from the current node, and not from a
1409 // recursive call. This way, if we come across a library that
1410 // has already been emitted, we repeat it iff it has been
1411 // emitted here.
1412 std::set<DependencyMap::key_type> emitted_here;
1413 for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
1415 if( emitted_here.find(*i) != emitted_here.end() )
1417 // a repeat. Must emit.
1418 emitted.insert(*i);
1419 link_line.push_back( *i );
1421 else
1423 // Emit only if no-one else has
1424 if( emitted.find(*i) == emitted.end() )
1426 // emit dependencies
1427 Emit( *i, dep_map, emitted, visited, link_line );
1428 // emit self
1429 emitted.insert(*i);
1430 emitted_here.insert(*i);
1431 link_line.push_back( *i );
1439 //----------------------------------------------------------------------------
1440 void cmTarget::GatherDependencies( const cmMakefile& mf,
1441 const LibraryID& lib,
1442 DependencyMap& dep_map)
1444 // If the library is already in the dependency map, then it has
1445 // already been fully processed.
1446 if( dep_map.find(lib) != dep_map.end() )
1448 return;
1451 const char* deps = mf.GetDefinition( (lib.first+"_LIB_DEPENDS").c_str() );
1452 if( deps && strcmp(deps,"") != 0 )
1454 // Make sure this library is in the map, even if it has an empty
1455 // set of dependencies. This distinguishes the case of explicitly
1456 // no dependencies with that of unspecified dependencies.
1457 dep_map[lib];
1459 // Parse the dependency information, which is a set of
1460 // type, library pairs separated by ";". There is always a trailing ";".
1461 cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
1462 std::string depline = deps;
1463 std::string::size_type start = 0;
1464 std::string::size_type end;
1465 end = depline.find( ";", start );
1466 while( end != std::string::npos )
1468 std::string l = depline.substr( start, end-start );
1469 if( l.size() != 0 )
1471 if (l == "debug")
1473 llt = cmTarget::DEBUG;
1475 else if (l == "optimized")
1477 llt = cmTarget::OPTIMIZED;
1479 else if (l == "general")
1481 llt = cmTarget::GENERAL;
1483 else
1485 LibraryID lib2(l,llt);
1486 this->InsertDependency( dep_map, lib, lib2);
1487 this->GatherDependencies( mf, lib2, dep_map);
1488 llt = cmTarget::GENERAL;
1491 start = end+1; // skip the ;
1492 end = depline.find( ";", start );
1494 // cannot depend on itself
1495 this->DeleteDependency( dep_map, lib, lib);
1499 //----------------------------------------------------------------------------
1500 void cmTarget::SetProperty(const char* prop, const char* value)
1502 if (!prop)
1504 return;
1506 if (!value)
1508 value = "NOTFOUND";
1511 this->Properties.SetProperty(prop, value, cmProperty::TARGET);
1513 // If imported information is being set, wipe out cached
1514 // information.
1515 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1517 this->ImportInfoMap.clear();
1521 //----------------------------------------------------------------------------
1522 void cmTarget::AppendProperty(const char* prop, const char* value)
1524 if (!prop)
1526 return;
1528 this->Properties.AppendProperty(prop, value, cmProperty::TARGET);
1530 // If imported information is being set, wipe out cached
1531 // information.
1532 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1534 this->ImportInfoMap.clear();
1538 //----------------------------------------------------------------------------
1539 void cmTarget::MarkAsImported()
1541 this->IsImportedTarget = true;
1544 //----------------------------------------------------------------------------
1545 const char* cmTarget::GetDirectory(const char* config, bool implib)
1547 if (this->IsImported())
1549 return this->ImportedGetDirectory(config, implib);
1551 else
1553 return this->NormalGetDirectory(config, implib);
1557 //----------------------------------------------------------------------------
1558 const char* cmTarget::ImportedGetDirectory(const char* config, bool implib)
1560 this->Directory =
1561 cmSystemTools::GetFilenamePath(
1562 this->ImportedGetFullPath(config, implib));
1563 return this->Directory.c_str();
1566 //----------------------------------------------------------------------------
1567 const char* cmTarget::NormalGetDirectory(const char* config, bool implib)
1569 if(config && *config)
1571 // Do not create the directory when config is given:
1572 this->Directory = this->GetAndCreateOutputDir(implib, true);
1573 // Add the configuration's subdirectory.
1574 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
1575 AppendDirectoryForConfig("/", config, "", this->Directory);
1576 return this->Directory.c_str();
1578 else
1580 return this->GetOutputDir(implib);
1584 //----------------------------------------------------------------------------
1585 const char* cmTarget::GetLocation(const char* config)
1587 if (this->IsImported())
1589 return this->ImportedGetLocation(config);
1591 else
1593 return this->NormalGetLocation(config);
1597 //----------------------------------------------------------------------------
1598 const char* cmTarget::ImportedGetLocation(const char* config)
1600 this->Location = this->ImportedGetFullPath(config, false);
1601 return this->Location.c_str();
1604 //----------------------------------------------------------------------------
1605 const char* cmTarget::NormalGetLocation(const char* config)
1607 this->Location = this->GetDirectory(config);
1608 if(!this->Location.empty())
1610 this->Location += "/";
1612 if(!config)
1614 // No specific configuration was given so it will not appear on
1615 // the result of GetDirectory. Add a name here to be replaced at
1616 // build time.
1617 const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
1618 if(cfgid && strcmp(cfgid, ".") != 0)
1620 this->Location += cfgid;
1621 this->Location += "/";
1624 this->Location += this->GetFullName(config, false);
1625 return this->Location.c_str();
1628 //----------------------------------------------------------------------------
1629 void cmTarget::GetTargetVersion(int& major, int& minor)
1631 // Set the default values.
1632 major = 0;
1633 minor = 0;
1635 // Look for a VERSION property.
1636 if(const char* version = this->GetProperty("VERSION"))
1638 // Try to parse the version number and store the results that were
1639 // successfully parsed.
1640 int parsed_major;
1641 int parsed_minor;
1642 switch(sscanf(version, "%d.%d", &parsed_major, &parsed_minor))
1644 case 2: minor = parsed_minor; // no break!
1645 case 1: major = parsed_major; // no break!
1646 default: break;
1651 //----------------------------------------------------------------------------
1652 const char *cmTarget::GetProperty(const char* prop)
1654 return this->GetProperty(prop, cmProperty::TARGET);
1657 //----------------------------------------------------------------------------
1658 void cmTarget::ComputeObjectFiles()
1660 if (this->IsImported())
1662 return;
1664 #if 0
1665 std::vector<std::string> dirs;
1666 this->Makefile->GetLocalGenerator()->
1667 GetTargetObjectFileDirectories(this,
1668 dirs);
1669 std::string objectFiles;
1670 std::string objExtensionLookup1 = "CMAKE_";
1671 std::string objExtensionLookup2 = "_OUTPUT_EXTENSION";
1673 for(std::vector<std::string>::iterator d = dirs.begin();
1674 d != dirs.end(); ++d)
1676 for(std::vector<cmSourceFile*>::iterator s = this->SourceFiles.begin();
1677 s != this->SourceFiles.end(); ++s)
1679 cmSourceFile* sf = *s;
1680 if(const char* lang = sf->GetLanguage())
1682 std::string lookupObj = objExtensionLookup1 + lang;
1683 lookupObj += objExtensionLookup2;
1684 const char* obj = this->Makefile->GetDefinition(lookupObj.c_str());
1685 if(obj)
1687 if(objectFiles.size())
1689 objectFiles += ";";
1691 std::string objFile = *d;
1692 objFile += "/";
1693 objFile += this->Makefile->GetLocalGenerator()->
1694 GetSourceObjectName(*sf);
1695 objFile += obj;
1696 objectFiles += objFile;
1701 this->SetProperty("OBJECT_FILES", objectFiles.c_str());
1702 #endif
1705 //----------------------------------------------------------------------------
1706 const char *cmTarget::GetProperty(const char* prop,
1707 cmProperty::ScopeType scope)
1709 if(!prop)
1711 return 0;
1714 // Watch for special "computed" properties that are dependent on
1715 // other properties or variables. Always recompute them.
1716 if(this->GetType() == cmTarget::EXECUTABLE ||
1717 this->GetType() == cmTarget::STATIC_LIBRARY ||
1718 this->GetType() == cmTarget::SHARED_LIBRARY ||
1719 this->GetType() == cmTarget::MODULE_LIBRARY)
1721 if(!this->IsImported() && strcmp(prop,"LOCATION") == 0)
1723 // Set the LOCATION property of the target. Note that this
1724 // cannot take into account the per-configuration name of the
1725 // target because the configuration type may not be known at
1726 // CMake time. It is now deprecated as described in the
1727 // documentation.
1728 this->SetProperty("LOCATION", this->GetLocation(0));
1731 // Support "LOCATION_<CONFIG>".
1732 if(strncmp(prop, "LOCATION_", 9) == 0)
1734 std::string configName = prop+9;
1735 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1737 else
1739 // Support "<CONFIG>_LOCATION" for compatiblity.
1740 int len = static_cast<int>(strlen(prop));
1741 if(len > 9 && strcmp(prop+len-9, "_LOCATION") == 0)
1743 std::string configName(prop, len-9);
1744 if(configName != "IMPORTED")
1746 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1752 if (strcmp(prop,"IMPORTED") == 0)
1754 return this->IsImported()?"TRUE":"FALSE";
1757 if(!strcmp(prop,"SOURCES"))
1759 cmOStringStream ss;
1760 const char* sep = "";
1761 for(std::vector<cmSourceFile*>::const_iterator
1762 i = this->SourceFiles.begin();
1763 i != this->SourceFiles.end(); ++i)
1765 // Separate from the previous list entries.
1766 ss << sep;
1767 sep = ";";
1769 // Construct what is known about this source file location.
1770 cmSourceFileLocation const& location = (*i)->GetLocation();
1771 std::string sname = location.GetDirectory();
1772 if(!sname.empty())
1774 sname += "/";
1776 sname += location.GetName();
1778 // Append this list entry.
1779 ss << sname;
1781 this->SetProperty("SOURCES", ss.str().c_str());
1784 // the type property returns what type the target is
1785 if (!strcmp(prop,"TYPE"))
1787 switch( this->GetType() )
1789 case cmTarget::STATIC_LIBRARY:
1790 return "STATIC_LIBRARY";
1791 // break; /* unreachable */
1792 case cmTarget::MODULE_LIBRARY:
1793 return "MODULE_LIBRARY";
1794 // break; /* unreachable */
1795 case cmTarget::SHARED_LIBRARY:
1796 return "SHARED_LIBRARY";
1797 // break; /* unreachable */
1798 case cmTarget::EXECUTABLE:
1799 return "EXECUTABLE";
1800 // break; /* unreachable */
1801 case cmTarget::UTILITY:
1802 return "UTILITY";
1803 // break; /* unreachable */
1804 case cmTarget::GLOBAL_TARGET:
1805 return "GLOBAL_TARGET";
1806 // break; /* unreachable */
1807 case cmTarget::INSTALL_FILES:
1808 return "INSTALL_FILES";
1809 // break; /* unreachable */
1810 case cmTarget::INSTALL_PROGRAMS:
1811 return "INSTALL_PROGRAMS";
1812 // break; /* unreachable */
1813 case cmTarget::INSTALL_DIRECTORY:
1814 return "INSTALL_DIRECTORY";
1815 // break; /* unreachable */
1817 return 0;
1819 bool chain = false;
1820 const char *retVal =
1821 this->Properties.GetPropertyValue(prop, scope, chain);
1822 if (chain)
1824 return this->Makefile->GetProperty(prop,scope);
1826 return retVal;
1829 //----------------------------------------------------------------------------
1830 bool cmTarget::GetPropertyAsBool(const char* prop)
1832 return cmSystemTools::IsOn(this->GetProperty(prop));
1835 //----------------------------------------------------------------------------
1836 const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
1838 if(this->GetProperty("HAS_CXX"))
1840 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
1842 const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
1843 if (linkerLang==0)
1845 // if the property has not yet been set, collect all languages in the
1846 // target and then find the language with the highest preference value
1847 std::set<cmStdString> languages;
1848 this->GetLanguages(languages);
1850 std::string linkerLangList; // only used for the error message
1851 int maxLinkerPref = 0;
1852 bool multiplePreferedLanguages = false;
1853 for(std::set<cmStdString>::const_iterator sit = languages.begin();
1854 sit != languages.end(); ++sit)
1856 int linkerPref = gg->GetLinkerPreference(sit->c_str());
1857 if ((linkerPref > maxLinkerPref) || (linkerLang==0))
1859 maxLinkerPref = linkerPref;
1860 linkerLang = sit->c_str();
1861 linkerLangList = *sit;
1862 multiplePreferedLanguages = false;
1864 else if (linkerPref == maxLinkerPref)
1866 linkerLangList += "; ";
1867 linkerLangList += *sit;
1868 multiplePreferedLanguages = true;
1872 if (linkerLang!=0)
1874 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", linkerLang);
1876 if (multiplePreferedLanguages)
1878 cmOStringStream err;
1879 err << "Error: Target " << this->Name << " contains multiple languages "
1880 << "with the highest linker preference (" << maxLinkerPref << "): "
1881 << linkerLangList << "\n"
1882 << "You must set the LINKER_LANGUAGE property for this target.";
1883 cmSystemTools::Error(err.str().c_str());
1886 return this->GetProperty("LINKER_LANGUAGE");
1889 //----------------------------------------------------------------------------
1890 const char* cmTarget::GetCreateRuleVariable()
1892 switch(this->GetType())
1894 case cmTarget::STATIC_LIBRARY:
1895 return "_CREATE_STATIC_LIBRARY";
1896 case cmTarget::SHARED_LIBRARY:
1897 return "_CREATE_SHARED_LIBRARY";
1898 case cmTarget::MODULE_LIBRARY:
1899 return "_CREATE_SHARED_MODULE";
1900 case cmTarget::EXECUTABLE:
1901 return "_LINK_EXECUTABLE";
1902 case cmTarget::UTILITY:
1903 case cmTarget::GLOBAL_TARGET:
1904 case cmTarget::INSTALL_FILES:
1905 case cmTarget::INSTALL_PROGRAMS:
1906 case cmTarget::INSTALL_DIRECTORY:
1907 break;
1909 return "";
1912 //----------------------------------------------------------------------------
1913 const char* cmTarget::GetSuffixVariableInternal(TargetType type,
1914 bool implib)
1916 switch(type)
1918 case cmTarget::STATIC_LIBRARY:
1919 return "CMAKE_STATIC_LIBRARY_SUFFIX";
1920 case cmTarget::SHARED_LIBRARY:
1921 return (implib
1922 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1923 : "CMAKE_SHARED_LIBRARY_SUFFIX");
1924 case cmTarget::MODULE_LIBRARY:
1925 return (implib
1926 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1927 : "CMAKE_SHARED_MODULE_SUFFIX");
1928 case cmTarget::EXECUTABLE:
1929 return (implib
1930 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1931 : "CMAKE_EXECUTABLE_SUFFIX");
1932 case cmTarget::UTILITY:
1933 case cmTarget::GLOBAL_TARGET:
1934 case cmTarget::INSTALL_FILES:
1935 case cmTarget::INSTALL_PROGRAMS:
1936 case cmTarget::INSTALL_DIRECTORY:
1937 break;
1939 return "";
1943 //----------------------------------------------------------------------------
1944 const char* cmTarget::GetPrefixVariableInternal(TargetType type,
1945 bool implib)
1947 switch(type)
1949 case cmTarget::STATIC_LIBRARY:
1950 return "CMAKE_STATIC_LIBRARY_PREFIX";
1951 case cmTarget::SHARED_LIBRARY:
1952 return (implib
1953 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
1954 : "CMAKE_SHARED_LIBRARY_PREFIX");
1955 case cmTarget::MODULE_LIBRARY:
1956 return (implib
1957 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
1958 : "CMAKE_SHARED_MODULE_PREFIX");
1959 case cmTarget::EXECUTABLE:
1960 return (implib? "CMAKE_IMPORT_LIBRARY_PREFIX" : "");
1961 case cmTarget::UTILITY:
1962 case cmTarget::GLOBAL_TARGET:
1963 case cmTarget::INSTALL_FILES:
1964 case cmTarget::INSTALL_PROGRAMS:
1965 case cmTarget::INSTALL_DIRECTORY:
1966 break;
1968 return "";
1971 //----------------------------------------------------------------------------
1972 std::string cmTarget::GetPDBName(const char* config)
1974 std::string prefix;
1975 std::string base;
1976 std::string suffix;
1977 this->GetFullNameInternal(this->GetType(), config, false,
1978 prefix, base, suffix);
1979 return prefix+base+".pdb";
1982 //----------------------------------------------------------------------------
1983 std::string cmTarget::GetSOName(const char* config)
1985 if(this->IsImported())
1987 // Lookup the imported soname.
1988 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
1990 return info->SOName;
1992 else
1994 return "";
1997 else
1999 // Compute the soname that will be built.
2000 std::string name;
2001 std::string soName;
2002 std::string realName;
2003 std::string impName;
2004 std::string pdbName;
2005 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2006 return soName;
2010 //----------------------------------------------------------------------------
2011 std::string cmTarget::GetFullName(const char* config, bool implib)
2013 if(this->IsImported())
2015 return this->GetFullNameImported(config, implib);
2017 else
2019 return this->GetFullNameInternal(this->GetType(), config, implib);
2023 //----------------------------------------------------------------------------
2024 std::string cmTarget::GetFullNameImported(const char* config, bool implib)
2026 return cmSystemTools::GetFilenameName(
2027 this->ImportedGetFullPath(config, implib));
2030 //----------------------------------------------------------------------------
2031 void cmTarget::GetFullNameComponents(std::string& prefix, std::string& base,
2032 std::string& suffix, const char* config,
2033 bool implib)
2035 this->GetFullNameInternal(this->GetType(), config, implib,
2036 prefix, base, suffix);
2039 //----------------------------------------------------------------------------
2040 std::string cmTarget::GetFullPath(const char* config, bool implib)
2042 if(this->IsImported())
2044 return this->ImportedGetFullPath(config, implib);
2046 else
2048 return this->NormalGetFullPath(config, implib);
2052 //----------------------------------------------------------------------------
2053 std::string cmTarget::NormalGetFullPath(const char* config, bool implib)
2055 // Start with the output directory for the target.
2056 std::string fpath = this->GetDirectory(config, implib);
2057 fpath += "/";
2059 // Add the full name of the target.
2060 fpath += this->GetFullName(config, implib);
2061 return fpath;
2064 //----------------------------------------------------------------------------
2065 std::string cmTarget::ImportedGetFullPath(const char* config, bool implib)
2067 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2069 if(implib)
2071 return info->ImportLibrary;
2073 else
2075 return info->Location;
2078 else
2080 std::string result = this->GetName();
2081 result += "-NOTFOUND";
2082 return result;
2086 //----------------------------------------------------------------------------
2087 std::string
2088 cmTarget::GetFullNameInternal(TargetType type, const char* config,
2089 bool implib)
2091 std::string prefix;
2092 std::string base;
2093 std::string suffix;
2094 this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
2095 return prefix+base+suffix;
2098 //----------------------------------------------------------------------------
2099 void cmTarget::GetFullNameInternal(TargetType type,
2100 const char* config,
2101 bool implib,
2102 std::string& outPrefix,
2103 std::string& outBase,
2104 std::string& outSuffix)
2106 // Use just the target name for non-main target types.
2107 if(type != cmTarget::STATIC_LIBRARY &&
2108 type != cmTarget::SHARED_LIBRARY &&
2109 type != cmTarget::MODULE_LIBRARY &&
2110 type != cmTarget::EXECUTABLE)
2112 outPrefix = "";
2113 outBase = this->GetName();
2114 outSuffix = "";
2115 return;
2118 // Return an empty name for the import library if this platform
2119 // does not support import libraries.
2120 if(implib &&
2121 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2123 outPrefix = "";
2124 outBase = "";
2125 outSuffix = "";
2126 return;
2129 // The implib option is only allowed for shared libraries, module
2130 // libraries, and executables.
2131 if(type != cmTarget::SHARED_LIBRARY &&
2132 type != cmTarget::MODULE_LIBRARY &&
2133 type != cmTarget::EXECUTABLE)
2135 implib = false;
2138 // Compute the full name for main target types.
2139 const char* targetPrefix = (implib
2140 ? this->GetProperty("IMPORT_PREFIX")
2141 : this->GetProperty("PREFIX"));
2142 const char* targetSuffix = (implib
2143 ? this->GetProperty("IMPORT_SUFFIX")
2144 : this->GetProperty("SUFFIX"));
2145 const char* configPostfix = 0;
2146 if(config && *config)
2148 std::string configProp = cmSystemTools::UpperCase(config);
2149 configProp += "_POSTFIX";
2150 configPostfix = this->GetProperty(configProp.c_str());
2152 const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
2153 const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
2154 const char* ll =
2155 this->GetLinkerLanguage(
2156 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2157 // first try language specific suffix
2158 if(ll)
2160 if(!targetSuffix && suffixVar && *suffixVar)
2162 std::string langSuff = suffixVar + std::string("_") + ll;
2163 targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
2165 if(!targetPrefix && prefixVar && *prefixVar)
2167 std::string langPrefix = prefixVar + std::string("_") + ll;
2168 targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
2172 // if there is no prefix on the target use the cmake definition
2173 if(!targetPrefix && prefixVar)
2175 targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
2177 // if there is no suffix on the target use the cmake definition
2178 if(!targetSuffix && suffixVar)
2180 targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
2183 // frameworks do not have a prefix or a suffix
2184 if(this->IsFrameworkOnApple())
2186 targetPrefix = 0;
2187 targetSuffix = 0;
2190 // Begin the final name with the prefix.
2191 outPrefix = targetPrefix?targetPrefix:"";
2193 // Append the target name or property-specified name.
2194 const char* outName = 0;
2195 if(config && *config)
2197 std::string configProp = cmSystemTools::UpperCase(config);
2198 configProp += "_OUTPUT_NAME";
2199 outName = this->GetProperty(configProp.c_str());
2201 if(!outName)
2203 outName = this->GetProperty("OUTPUT_NAME");
2205 if(outName)
2207 outBase = outName;
2209 else
2211 outBase = this->GetName();
2214 // Append the per-configuration postfix.
2215 outBase += configPostfix?configPostfix:"";
2217 // Name shared libraries with their version number on some platforms.
2218 if(const char* version = this->GetProperty("VERSION"))
2220 if(type == cmTarget::SHARED_LIBRARY && !implib &&
2221 this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION"))
2223 outBase += "-";
2224 outBase += version;
2228 // Append the suffix.
2229 outSuffix = targetSuffix?targetSuffix:"";
2232 //----------------------------------------------------------------------------
2233 void cmTarget::GetLibraryNames(std::string& name,
2234 std::string& soName,
2235 std::string& realName,
2236 std::string& impName,
2237 std::string& pdbName,
2238 const char* config)
2240 // Get the names based on the real type of the library.
2241 this->GetLibraryNamesInternal(name, soName, realName, impName, pdbName,
2242 this->GetType(), config);
2245 //----------------------------------------------------------------------------
2246 void cmTarget::GetLibraryCleanNames(std::string& staticName,
2247 std::string& sharedName,
2248 std::string& sharedSOName,
2249 std::string& sharedRealName,
2250 std::string& importName,
2251 std::string& pdbName,
2252 const char* config)
2254 // Get the name as if this were a static library.
2255 std::string soName;
2256 std::string realName;
2257 std::string impName;
2258 this->GetLibraryNamesInternal(staticName, soName, realName, impName,
2259 pdbName, cmTarget::STATIC_LIBRARY, config);
2261 // Get the names as if this were a shared library.
2262 if(this->GetType() == cmTarget::STATIC_LIBRARY)
2264 // Since the real type is static then the user either specified
2265 // STATIC or did not specify a type. In the former case the
2266 // shared library will never be present. In the latter case the
2267 // type will never be MODULE. Either way the only names that
2268 // might have to be cleaned are the shared library names.
2269 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2270 importName, pdbName,
2271 cmTarget::SHARED_LIBRARY, config);
2273 else
2275 // Use the name of the real type of the library (shared or module).
2276 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2277 importName, pdbName, this->GetType(),
2278 config);
2282 //----------------------------------------------------------------------------
2283 void cmTarget::GetLibraryNamesInternal(std::string& name,
2284 std::string& soName,
2285 std::string& realName,
2286 std::string& impName,
2287 std::string& pdbName,
2288 TargetType type,
2289 const char* config)
2291 // This should not be called for imported targets.
2292 // TODO: Split cmTarget into a class hierarchy to get compile-time
2293 // enforcement of the limited imported target API.
2294 if(this->IsImported())
2296 abort();
2299 // Construct the name of the soname flag variable for this language.
2300 const char* ll =
2301 this->GetLinkerLanguage(
2302 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2303 std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
2304 if(ll)
2306 sonameFlag += "_";
2307 sonameFlag += ll;
2309 sonameFlag += "_FLAG";
2311 // Check for library version properties.
2312 const char* version = this->GetProperty("VERSION");
2313 const char* soversion = this->GetProperty("SOVERSION");
2314 if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
2315 !this->Makefile->GetDefinition(sonameFlag.c_str()))
2317 // Versioning is supported only for shared libraries and modules,
2318 // and then only when the platform supports an soname flag.
2319 version = 0;
2320 soversion = 0;
2322 if(version && !soversion)
2324 // The soversion must be set if the library version is set. Use
2325 // the library version as the soversion.
2326 soversion = version;
2329 // Get the components of the library name.
2330 std::string prefix;
2331 std::string base;
2332 std::string suffix;
2333 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2335 // The library name.
2336 name = prefix+base+suffix;
2338 // The library's soname.
2339 #if defined(__APPLE__)
2340 soName = prefix+base;
2341 #else
2342 soName = name;
2343 #endif
2344 if(soversion)
2346 soName += ".";
2347 soName += soversion;
2349 #if defined(__APPLE__)
2350 soName += suffix;
2351 #endif
2353 // The library's real name on disk.
2354 #if defined(__APPLE__)
2355 realName = prefix+base;
2356 #else
2357 realName = name;
2358 #endif
2359 if(version)
2361 realName += ".";
2362 realName += version;
2364 else if(soversion)
2366 realName += ".";
2367 realName += soversion;
2369 #if defined(__APPLE__)
2370 realName += suffix;
2371 #endif
2373 // The import library name.
2374 if(type == cmTarget::SHARED_LIBRARY ||
2375 type == cmTarget::MODULE_LIBRARY)
2377 impName = this->GetFullNameInternal(type, config, true);
2379 else
2381 impName = "";
2384 // The program database file name.
2385 pdbName = prefix+base+".pdb";
2388 //----------------------------------------------------------------------------
2389 void cmTarget::GetExecutableNames(std::string& name,
2390 std::string& realName,
2391 std::string& impName,
2392 std::string& pdbName,
2393 const char* config)
2395 // Get the names based on the real type of the executable.
2396 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2397 this->GetType(), config);
2400 //----------------------------------------------------------------------------
2401 void cmTarget::GetExecutableCleanNames(std::string& name,
2402 std::string& realName,
2403 std::string& impName,
2404 std::string& pdbName,
2405 const char* config)
2407 // Get the name and versioned name of this executable.
2408 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2409 cmTarget::EXECUTABLE, config);
2412 //----------------------------------------------------------------------------
2413 void cmTarget::GetExecutableNamesInternal(std::string& name,
2414 std::string& realName,
2415 std::string& impName,
2416 std::string& pdbName,
2417 TargetType type,
2418 const char* config)
2420 // This should not be called for imported targets.
2421 // TODO: Split cmTarget into a class hierarchy to get compile-time
2422 // enforcement of the limited imported target API.
2423 if(this->IsImported())
2425 abort();
2428 // This versioning is supported only for executables and then only
2429 // when the platform supports symbolic links.
2430 #if defined(_WIN32) && !defined(__CYGWIN__)
2431 const char* version = 0;
2432 #else
2433 // Check for executable version properties.
2434 const char* version = this->GetProperty("VERSION");
2435 if(type != cmTarget::EXECUTABLE)
2437 version = 0;
2439 #endif
2441 // Get the components of the executable name.
2442 std::string prefix;
2443 std::string base;
2444 std::string suffix;
2445 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2447 // The executable name.
2448 name = prefix+base+suffix;
2450 // The executable's real name on disk.
2451 #if defined(__CYGWIN__)
2452 realName = prefix+base;
2453 #else
2454 realName = name;
2455 #endif
2456 if(version)
2458 realName += "-";
2459 realName += version;
2461 #if defined(__CYGWIN__)
2462 realName += suffix;
2463 #endif
2465 // The import library name.
2466 impName = this->GetFullNameInternal(type, config, true);
2468 // The program database file name.
2469 pdbName = prefix+base+".pdb";
2472 //----------------------------------------------------------------------------
2473 void cmTarget::GenerateTargetManifest(const char* config)
2475 cmMakefile* mf = this->Makefile;
2476 cmLocalGenerator* lg = mf->GetLocalGenerator();
2477 cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2479 // Get the names.
2480 std::string name;
2481 std::string soName;
2482 std::string realName;
2483 std::string impName;
2484 std::string pdbName;
2485 if(this->GetType() == cmTarget::EXECUTABLE)
2487 this->GetExecutableNames(name, realName, impName, pdbName, config);
2489 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2490 this->GetType() == cmTarget::SHARED_LIBRARY ||
2491 this->GetType() == cmTarget::MODULE_LIBRARY)
2493 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2495 else
2497 return;
2500 // Get the directory.
2501 std::string dir = this->GetDirectory(config, false);
2503 // Add each name.
2504 std::string f;
2505 if(!name.empty())
2507 f = dir;
2508 f += "/";
2509 f += name;
2510 gg->AddToManifest(config? config:"", f);
2512 if(!soName.empty())
2514 f = dir;
2515 f += "/";
2516 f += soName;
2517 gg->AddToManifest(config? config:"", f);
2519 if(!realName.empty())
2521 f = dir;
2522 f += "/";
2523 f += realName;
2524 gg->AddToManifest(config? config:"", f);
2526 if(!pdbName.empty())
2528 f = dir;
2529 f += "/";
2530 f += pdbName;
2531 gg->AddToManifest(config? config:"", f);
2533 if(!impName.empty())
2535 f = this->GetDirectory(config, true);
2536 f += "/";
2537 f += impName;
2538 gg->AddToManifest(config? config:"", f);
2542 //----------------------------------------------------------------------------
2543 void cmTarget::SetPropertyDefault(const char* property,
2544 const char* default_value)
2546 // Compute the name of the variable holding the default value.
2547 std::string var = "CMAKE_";
2548 var += property;
2550 if(const char* value = this->Makefile->GetDefinition(var.c_str()))
2552 this->SetProperty(property, value);
2554 else if(default_value)
2556 this->SetProperty(property, default_value);
2560 //----------------------------------------------------------------------------
2561 bool cmTarget::HaveBuildTreeRPATH()
2563 return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
2564 !this->LinkLibraries.empty());
2567 //----------------------------------------------------------------------------
2568 bool cmTarget::HaveInstallTreeRPATH()
2570 const char* install_rpath = this->GetProperty("INSTALL_RPATH");
2571 return install_rpath && *install_rpath;
2574 //----------------------------------------------------------------------------
2575 bool cmTarget::NeedRelinkBeforeInstall()
2577 // Only executables and shared libraries can have an rpath and may
2578 // need relinking.
2579 if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
2580 this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
2581 this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
2583 return false;
2586 // If there is no install location this target will not be installed
2587 // and therefore does not need relinking.
2588 if(!this->GetHaveInstallRule())
2590 return false;
2593 // If skipping all rpaths completely then no relinking is needed.
2594 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
2596 return false;
2599 // If building with the install-tree rpath no relinking is needed.
2600 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2602 return false;
2605 // If chrpath is going to be used no relinking is needed.
2606 if(this->IsChrpathUsed())
2608 return false;
2611 // Check for rpath support on this platform.
2612 if(const char* ll = this->GetLinkerLanguage(
2613 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2615 std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2616 flagVar += ll;
2617 flagVar += "_FLAG";
2618 if(!this->Makefile->IsSet(flagVar.c_str()))
2620 // There is no rpath support on this platform so nothing needs
2621 // relinking.
2622 return false;
2625 else
2627 // No linker language is known. This error will be reported by
2628 // other code.
2629 return false;
2632 // If either a build or install tree rpath is set then the rpath
2633 // will likely change between the build tree and install tree and
2634 // this target must be relinked.
2635 return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
2638 //----------------------------------------------------------------------------
2639 std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
2641 // If building directly for installation then the build tree install_name
2642 // is the same as the install tree.
2643 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2645 return GetInstallNameDirForInstallTree(config);
2648 // Use the build tree directory for the target.
2649 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2650 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2651 !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
2653 std::string dir = this->GetDirectory(config);
2654 dir += "/";
2655 return dir;
2657 else
2659 return "";
2663 //----------------------------------------------------------------------------
2664 std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
2666 // Lookup the target property.
2667 const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
2668 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2669 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2670 install_name_dir && *install_name_dir)
2672 std::string dir = install_name_dir;
2673 dir += "/";
2674 return dir;
2676 else
2678 return "";
2682 //----------------------------------------------------------------------------
2683 const char* cmTarget::GetAndCreateOutputDir(bool implib, bool create)
2685 // The implib option is only allowed for shared libraries, module
2686 // libraries, and executables.
2687 if(this->GetType() != cmTarget::SHARED_LIBRARY &&
2688 this->GetType() != cmTarget::MODULE_LIBRARY &&
2689 this->GetType() != cmTarget::EXECUTABLE)
2691 implib = false;
2694 // Sanity check. Only generators on platforms supporting import
2695 // libraries should be asking for the import library output
2696 // directory.
2697 if(implib &&
2698 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2700 abort();
2702 if(implib && !this->DLLPlatform)
2704 abort();
2707 // Select whether we are constructing the directory for the main
2708 // target or the import library.
2709 std::string& out = implib? this->OutputDirImplib : this->OutputDir;
2711 if(out.empty())
2713 // Look for a target property defining the target output directory
2714 // based on the target type.
2715 const char* propertyName = 0;
2716 switch(this->GetType())
2718 case cmTarget::SHARED_LIBRARY:
2720 // For non-DLL platforms shared libraries are treated as
2721 // library targets. For DLL platforms the DLL part of a
2722 // shared library is treated as a runtime target and the
2723 // corresponding import library is treated as an archive
2724 // target.
2725 if(this->DLLPlatform)
2727 if(implib)
2729 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2731 else
2733 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2736 else
2738 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2740 } break;
2741 case cmTarget::STATIC_LIBRARY:
2743 // Static libraries are always treated as archive targets.
2744 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2745 } break;
2746 case cmTarget::MODULE_LIBRARY:
2748 // Module libraries are always treated as library targets.
2749 // Module import libraries are treated as archive targets.
2750 if(implib)
2752 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2754 else
2756 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2758 } break;
2759 case cmTarget::EXECUTABLE:
2761 // Executables are always treated as runtime targets.
2762 // Executable import libraries are treated as archive targets.
2763 if(implib)
2765 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2767 else
2769 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2771 } break;
2772 default: break;
2775 // Select an output directory.
2776 if(const char* outdir = this->GetProperty(propertyName))
2778 // Use the user-specified output directory.
2779 out = outdir;
2781 else if(this->GetType() == cmTarget::EXECUTABLE)
2783 // Lookup the output path for executables.
2784 out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
2786 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2787 this->GetType() == cmTarget::SHARED_LIBRARY ||
2788 this->GetType() == cmTarget::MODULE_LIBRARY)
2790 // Lookup the output path for libraries.
2791 out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
2793 if(out.empty())
2795 // Default to the current output directory.
2796 out = ".";
2798 // Convert the output path to a full path in case it is
2799 // specified as a relative path. Treat a relative path as
2800 // relative to the current output directory for this makefile.
2801 out =
2802 cmSystemTools::CollapseFullPath
2803 (out.c_str(), this->Makefile->GetStartOutputDirectory());
2805 if(this->IsFrameworkOnApple())
2807 out += "/";
2808 out += this->GetFullName(0, implib);
2809 out += ".framework";
2812 // Optionally make sure the output path exists on disk.
2813 if(create)
2815 if(!cmSystemTools::MakeDirectory(out.c_str()))
2817 cmSystemTools::Error("Error failed to create output directory: ",
2818 out.c_str());
2823 return out.c_str();
2826 //----------------------------------------------------------------------------
2827 const char* cmTarget::GetOutputDir(bool implib)
2829 return this->GetAndCreateOutputDir(implib, true);
2832 //----------------------------------------------------------------------------
2833 const char* cmTarget::GetExportMacro()
2835 // Define the symbol for targets that export symbols.
2836 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
2837 this->GetType() == cmTarget::MODULE_LIBRARY ||
2838 this->IsExecutableWithExports())
2840 if(const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL"))
2842 this->ExportMacro = custom_export_name;
2844 else
2846 std::string in = this->GetName();
2847 in += "_EXPORTS";
2848 this->ExportMacro = cmSystemTools::MakeCindentifier(in.c_str());
2850 return this->ExportMacro.c_str();
2852 else
2854 return 0;
2858 //----------------------------------------------------------------------------
2859 void cmTarget::GetLanguages(std::set<cmStdString>& languages) const
2861 for(std::vector<cmSourceFile*>::const_iterator
2862 i = this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
2864 if(const char* lang = (*i)->GetLanguage())
2866 languages.insert(lang);
2871 //----------------------------------------------------------------------------
2872 bool cmTarget::IsChrpathUsed()
2874 // Enable use of "chrpath" if it is available, the user has turned
2875 // on the feature, and the rpath flag uses a separator.
2876 if(const char* ll = this->GetLinkerLanguage(
2877 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2879 std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2880 sepVar += ll;
2881 sepVar += "_FLAG_SEP";
2882 const char* sep = this->Makefile->GetDefinition(sepVar.c_str());
2883 if(sep && *sep)
2885 if(this->Makefile->IsSet("CMAKE_CHRPATH") &&
2886 this->Makefile->IsOn("CMAKE_USE_CHRPATH"))
2888 return true;
2892 return false;
2895 //----------------------------------------------------------------------------
2896 cmTarget::ImportInfo const*
2897 cmTarget::GetImportInfo(const char* config)
2899 // There is no imported information for non-imported targets.
2900 if(!this->IsImported())
2902 return 0;
2905 // Lookup/compute/cache the import information for this
2906 // configuration.
2907 std::string config_upper;
2908 if(config && *config)
2910 config_upper = cmSystemTools::UpperCase(config);
2912 else
2914 config_upper = "NOCONFIG";
2916 ImportInfoMapType::const_iterator i =
2917 this->ImportInfoMap.find(config_upper);
2918 if(i == this->ImportInfoMap.end())
2920 ImportInfo info;
2921 this->ComputeImportInfo(config_upper, info);
2922 ImportInfoMapType::value_type entry(config_upper, info);
2923 i = this->ImportInfoMap.insert(entry).first;
2926 // If the location is empty then the target is not available for
2927 // this configuration.
2928 if(i->second.Location.empty())
2930 return 0;
2933 // Return the import information.
2934 return &i->second;
2937 //----------------------------------------------------------------------------
2938 void cmTarget::ComputeImportInfo(std::string const& desired_config,
2939 ImportInfo& info)
2941 // This method finds information about an imported target from its
2942 // properties. The "IMPORTED_" namespace is reserved for properties
2943 // defined by the project exporting the target.
2945 // Track the configuration-specific property suffix.
2946 std::string suffix = "_";
2947 suffix += desired_config;
2949 // Look for a mapping from the current project's configuration to
2950 // the imported project's configuration.
2951 std::vector<std::string> mappedConfigs;
2953 std::string mapProp = "MAP_IMPORTED_CONFIG_";
2954 mapProp += desired_config;
2955 if(const char* mapValue = this->GetProperty(mapProp.c_str()))
2957 cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
2961 // If a mapping was found, check its configurations.
2962 const char* loc = 0;
2963 for(std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
2964 !loc && mci != mappedConfigs.end(); ++mci)
2966 // Look for this configuration.
2967 std::string mcUpper = cmSystemTools::UpperCase(mci->c_str());
2968 std::string locProp = "IMPORTED_LOCATION_";
2969 locProp += mcUpper;
2970 loc = this->GetProperty(locProp.c_str());
2972 // If it was found, use it for all properties below.
2973 if(loc)
2975 suffix = "_";
2976 suffix += mcUpper;
2980 // If we needed to find one of the mapped configurations but did not
2981 // then the target is not found. The project does not want any
2982 // other configuration.
2983 if(!mappedConfigs.empty() && !loc)
2985 return;
2988 // If we have not yet found it then there are no mapped
2989 // configurations. Look for an exact-match.
2990 if(!loc)
2992 std::string locProp = "IMPORTED_LOCATION";
2993 locProp += suffix;
2994 loc = this->GetProperty(locProp.c_str());
2997 // If we have not yet found it then there are no mapped
2998 // configurations and no exact match.
2999 if(!loc)
3001 // The suffix computed above is not useful.
3002 suffix = "";
3004 // Look for a configuration-less location. This may be set by
3005 // manually-written code.
3006 loc = this->GetProperty("IMPORTED_LOCATION");
3009 // If we have not yet found it then the project is willing to try
3010 // any available configuration.
3011 if(!loc)
3013 std::vector<std::string> availableConfigs;
3014 if(const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS"))
3016 cmSystemTools::ExpandListArgument(iconfigs, availableConfigs);
3018 for(std::vector<std::string>::const_iterator
3019 aci = availableConfigs.begin();
3020 !loc && aci != availableConfigs.end(); ++aci)
3022 suffix = "_";
3023 suffix += cmSystemTools::UpperCase(availableConfigs[0]);
3024 std::string locProp = "IMPORTED_LOCATION";
3025 locProp += suffix;
3026 loc = this->GetProperty(locProp.c_str());
3030 // If we have not yet found it then the target is not available.
3031 if(!loc)
3033 return;
3036 // A provided configuration has been chosen. Load the
3037 // configuration's properties.
3038 info.Location = loc;
3040 // Get the soname.
3041 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3043 std::string soProp = "IMPORTED_SONAME";
3044 soProp += suffix;
3045 if(const char* config_soname = this->GetProperty(soProp.c_str()))
3047 info.SOName = config_soname;
3049 else if(const char* soname = this->GetProperty("IMPORTED_SONAME"))
3051 info.SOName = soname;
3055 // Get the import library.
3056 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3057 this->IsExecutableWithExports())
3059 std::string impProp = "IMPORTED_IMPLIB";
3060 impProp += suffix;
3061 if(const char* config_implib = this->GetProperty(impProp.c_str()))
3063 info.ImportLibrary = config_implib;
3065 else if(const char* implib = this->GetProperty("IMPORTED_IMPLIB"))
3067 info.ImportLibrary = implib;
3071 // Get the link interface.
3073 std::string linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
3074 linkProp += suffix;
3075 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3077 cmSystemTools::ExpandListArgument(config_libs,
3078 info.LinkInterface.Libraries);
3080 else if(const char* libs =
3081 this->GetProperty("IMPORTED_LINK_INTERFACE_LIBRARIES"))
3083 cmSystemTools::ExpandListArgument(libs,
3084 info.LinkInterface.Libraries);
3088 // Get the link dependencies.
3090 std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
3091 linkProp += suffix;
3092 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3094 cmSystemTools::ExpandListArgument(config_libs,
3095 info.LinkInterface.SharedDeps);
3097 else if(const char* libs =
3098 this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES"))
3100 cmSystemTools::ExpandListArgument(libs, info.LinkInterface.SharedDeps);
3105 //----------------------------------------------------------------------------
3106 cmTargetLinkInterface const* cmTarget::GetLinkInterface(const char* config)
3108 // Imported targets have their own link interface.
3109 if(this->IsImported())
3111 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
3113 return &info->LinkInterface;
3115 return 0;
3118 // Link interfaces are supported only for shared libraries and
3119 // executables that export symbols.
3120 if((this->GetType() != cmTarget::SHARED_LIBRARY &&
3121 !this->IsExecutableWithExports()))
3123 return 0;
3126 // Lookup any existing link interface for this configuration.
3127 std::map<cmStdString, cmTargetLinkInterface*>::iterator
3128 i = this->LinkInterface.find(config?config:"");
3129 if(i == this->LinkInterface.end())
3131 // Compute the link interface for this configuration.
3132 cmTargetLinkInterface* interface = this->ComputeLinkInterface(config);
3134 // Store the information for this configuration.
3135 std::map<cmStdString, cmTargetLinkInterface*>::value_type
3136 entry(config?config:"", interface);
3137 i = this->LinkInterface.insert(entry).first;
3140 return i->second;
3143 //----------------------------------------------------------------------------
3144 cmTargetLinkInterface* cmTarget::ComputeLinkInterface(const char* config)
3146 // Construct the property name suffix for this configuration.
3147 std::string suffix = "_";
3148 if(config && *config)
3150 suffix += cmSystemTools::UpperCase(config);
3152 else
3154 suffix += "NOCONFIG";
3157 // Lookup the link interface libraries.
3158 const char* libs = 0;
3160 // Lookup the per-configuration property.
3161 std::string propName = "LINK_INTERFACE_LIBRARIES";
3162 propName += suffix;
3163 libs = this->GetProperty(propName.c_str());
3165 // If not set, try the generic property.
3166 if(!libs)
3168 libs = this->GetProperty("LINK_INTERFACE_LIBRARIES");
3172 // If still not set, there is no link interface.
3173 if(!libs)
3175 return 0;
3178 // Allocate the interface.
3179 cmTargetLinkInterface* interface = new cmTargetLinkInterface;
3180 if(!interface)
3182 return 0;
3185 // Expand the list of libraries in the interface.
3186 cmSystemTools::ExpandListArgument(libs, interface->Libraries);
3188 // Now we need to construct a list of shared library dependencies
3189 // not included in the interface.
3190 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3192 // Use a set to keep track of what libraries have been emitted to
3193 // either list.
3194 std::set<cmStdString> emitted;
3195 for(std::vector<std::string>::const_iterator
3196 li = interface->Libraries.begin();
3197 li != interface->Libraries.end(); ++li)
3199 emitted.insert(*li);
3202 // Compute which library configuration to link.
3203 cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
3204 if(config && cmSystemTools::UpperCase(config) == "DEBUG")
3206 linkType = cmTarget::DEBUG;
3209 // Construct the list of libs linked for this configuration.
3210 cmTarget::LinkLibraryVectorType const& llibs =
3211 this->GetOriginalLinkLibraries();
3212 for(cmTarget::LinkLibraryVectorType::const_iterator li = llibs.begin();
3213 li != llibs.end(); ++li)
3215 // Skip entries that will resolve to the target itself, are empty,
3216 // or are not meant for this configuration.
3217 if(li->first == this->GetName() || li->first.empty() ||
3218 !(li->second == cmTarget::GENERAL || li->second == linkType))
3220 continue;
3223 // Skip entries that have already been emitted into either list.
3224 if(!emitted.insert(li->first).second)
3226 continue;
3229 // Add this entry if it is a shared library.
3230 if(cmTarget* tgt = this->Makefile->FindTargetToUse(li->first.c_str()))
3232 if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
3234 interface->SharedDeps.push_back(li->first);
3237 else
3239 // TODO: Recognize shared library file names. Perhaps this
3240 // should be moved to cmComputeLinkInformation, but that creates
3241 // a chicken-and-egg problem since this list is needed for its
3242 // construction.
3247 // Return the completed interface.
3248 return interface;
3251 //----------------------------------------------------------------------------
3252 cmComputeLinkInformation*
3253 cmTarget::GetLinkInformation(const char* config)
3255 // Lookup any existing information for this configuration.
3256 std::map<cmStdString, cmComputeLinkInformation*>::iterator
3257 i = this->LinkInformation.find(config?config:"");
3258 if(i == this->LinkInformation.end())
3260 // Compute information for this configuration.
3261 cmComputeLinkInformation* info =
3262 new cmComputeLinkInformation(this, config);
3263 if(!info || !info->Compute())
3265 delete info;
3266 info = 0;
3269 // Store the information for this configuration.
3270 std::map<cmStdString, cmComputeLinkInformation*>::value_type
3271 entry(config?config:"", info);
3272 i = this->LinkInformation.insert(entry).first;
3274 return i->second;
3277 //----------------------------------------------------------------------------
3278 cmTargetLinkInformationMap
3279 ::cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r): derived()
3281 // Ideally cmTarget instances should never be copied. However until
3282 // we can make a sweep to remove that, this copy constructor avoids
3283 // allowing the resources (LinkInformation) from getting copied. In
3284 // the worst case this will lead to extra cmComputeLinkInformation
3285 // instances. We also enforce in debug mode that the map be emptied
3286 // when copied.
3287 static_cast<void>(r);
3288 assert(r.empty());
3291 //----------------------------------------------------------------------------
3292 cmTargetLinkInformationMap::~cmTargetLinkInformationMap()
3294 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3296 delete i->second;
3300 //----------------------------------------------------------------------------
3301 cmTargetLinkInterfaceMap
3302 ::cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap const& r): derived()
3304 // Ideally cmTarget instances should never be copied. However until
3305 // we can make a sweep to remove that, this copy constructor avoids
3306 // allowing the resources (LinkInterface) from getting copied. In
3307 // the worst case this will lead to extra cmTargetLinkInterface
3308 // instances. We also enforce in debug mode that the map be emptied
3309 // when copied.
3310 static_cast<void>(r);
3311 assert(r.empty());
3314 //----------------------------------------------------------------------------
3315 cmTargetLinkInterfaceMap::~cmTargetLinkInterfaceMap()
3317 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3319 delete i->second;