Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmTarget.cxx
blobf39d8cf9f7c75e99dfa1797e030bd8f305b38611
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.cxx,v $
5 Language: C++
6 Date: $Date: 2008/02/14 20:31:08 $
7 Version: $Revision: 1.196 $
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 "Build an executable with a WinMain entry point on windows.",
500 "When this property is set to true the executable when linked "
501 "on Windows will be created with a WinMain() entry point instead "
502 "of of just main()."
503 "This makes it a GUI executable instead of a console application. "
504 "See the CMAKE_MFC_FLAG variable documentation to configure use "
505 "of MFC for WinMain executables.");
507 cm->DefineProperty
508 ("MACOSX_BUNDLE", cmProperty::TARGET,
509 "Build an executable as an application bundle on Mac OS X.",
510 "When this property is set to true the executable when built "
511 "on Mac OS X will be created as an application bundle. "
512 "This makes it a GUI executable that can be launched from "
513 "the Finder.\n"
514 "The bundle Info.plist file is generated automatically. "
515 "The following target properties may be set to specify "
516 "its content:"
517 " MACOSX_BUNDLE_INFO_STRING\n"
518 " MACOSX_BUNDLE_ICON_FILE\n"
519 " MACOSX_BUNDLE_GUI_IDENTIFIER\n"
520 " MACOSX_BUNDLE_LONG_VERSION_STRING\n"
521 " MACOSX_BUNDLE_BUNDLE_NAME\n"
522 " MACOSX_BUNDLE_SHORT_VERSION_STRING\n"
523 " MACOSX_BUNDLE_BUNDLE_VERSION\n"
524 " MACOSX_BUNDLE_COPYRIGHT\n"
527 cm->DefineProperty
528 ("ENABLE_EXPORTS", cmProperty::TARGET,
529 "Specify whether an executable exports symbols for loadable modules.",
530 "Normally an executable does not export any symbols because it is "
531 "the final program. It is possible for an executable to export "
532 "symbols to be used by loadable modules. When this property is "
533 "set to true CMake will allow other targets to \"link\" to the "
534 "executable with the TARGET_LINK_LIBRARIES command. "
535 "On all platforms a target-level dependency on the executable is "
536 "created for targets that link to it. "
537 "For non-DLL platforms the link rule is simply ignored since "
538 "the dynamic loader will automatically bind symbols when the "
539 "module is loaded. "
540 "For DLL platforms an import library will be created for the "
541 "exported symbols and then used for linking. "
542 "All Windows-based systems including Cygwin are DLL platforms.");
544 cm->DefineProperty
545 ("Fortran_MODULE_DIRECTORY", cmProperty::TARGET,
546 "Specify output directory for Fortran modules provided by the target.",
547 "If the target contains Fortran source files that provide modules "
548 "and the compiler supports a module output directory this specifies "
549 "the directory in which the modules will be placed. "
550 "When this property is not set the modules will be placed in the "
551 "build directory corresponding to the target's source directory.");
553 cm->DefineProperty
554 ("XCODE_ATTRIBUTE_<an-attribute>", cmProperty::TARGET,
555 "Set Xcode target attributes directly.",
556 "Tell the Xcode generator to set '<an-attribute>' to a given value "
557 "in the generated Xcode project. Ignored on other generators.");
559 cm->DefineProperty
560 ("GENERATOR_FILE_NAME", cmProperty::TARGET,
561 "Generator's file for this target.",
562 "An internal property used by some generators to record the name of "
563 "project or dsp file associated with this target.");
565 cm->DefineProperty
566 ("SOURCES", cmProperty::TARGET,
567 "Source names specified for a target.",
568 "Read-only list of sources specified for a target. "
569 "The names returned are suitable for passing to the "
570 "set_source_files_properties command.");
572 #if 0
573 cm->DefineProperty
574 ("OBJECT_FILES", cmProperty::TARGET,
575 "Used to get the resulting list of object files that make up a "
576 "target.",
577 "This can be used to put object files from one library "
578 "into another library. It is a read only property. It "
579 "converts the source list for the target into a list of full "
580 "paths to object names that will be produced by the target.");
581 #endif
583 #define CM_TARGET_FILE_TYPES_DOC \
584 "There are three kinds of target files that may be built: " \
585 "archive, library, and runtime. " \
586 "Executables are always treated as runtime targets. " \
587 "Static libraries are always treated as archive targets. " \
588 "Module libraries are always treated as library targets. " \
589 "For non-DLL platforms shared libraries are treated as library " \
590 "targets. " \
591 "For DLL platforms the DLL part of a shared library is treated as " \
592 "a runtime target and the corresponding import library is treated as " \
593 "an archive target. " \
594 "All Windows-based systems including Cygwin are DLL platforms."
596 cm->DefineProperty
597 ("ARCHIVE_OUTPUT_DIRECTORY", cmProperty::TARGET,
598 "Output directory in which to build ARCHIVE target files.",
599 "This property specifies the directory into which archive target files "
600 "should be built. "
601 CM_TARGET_FILE_TYPES_DOC " "
602 "This property is initialized by the value of the variable "
603 "CMAKE_ARCHIVE_OUTPUT_DIRECTORY if it is set when a target is created.");
604 cm->DefineProperty
605 ("LIBRARY_OUTPUT_DIRECTORY", cmProperty::TARGET,
606 "Output directory in which to build LIBRARY target files.",
607 "This property specifies the directory into which library target files "
608 "should be built. "
609 CM_TARGET_FILE_TYPES_DOC " "
610 "This property is initialized by the value of the variable "
611 "CMAKE_LIBRARY_OUTPUT_DIRECTORY if it is set when a target is created.");
612 cm->DefineProperty
613 ("RUNTIME_OUTPUT_DIRECTORY", cmProperty::TARGET,
614 "Output directory in which to build RUNTIME target files.",
615 "This property specifies the directory into which runtime target files "
616 "should be built. "
617 CM_TARGET_FILE_TYPES_DOC " "
618 "This property is initialized by the value of the variable "
619 "CMAKE_RUNTIME_OUTPUT_DIRECTORY if it is set when a target is created.");
621 // define some properties without documentation
622 cm->DefineProperty("DEBUG_OUTPUT_NAME", cmProperty::TARGET,0,0);
623 cm->DefineProperty("RELEASE_OUTPUT_NAME", cmProperty::TARGET,0,0);
626 void cmTarget::SetType(TargetType type, const char* name)
628 this->Name = name;
629 if(type == cmTarget::INSTALL_FILES ||
630 type == cmTarget::INSTALL_PROGRAMS ||
631 type == cmTarget::INSTALL_DIRECTORY)
633 abort();
635 // only add dependency information for library targets
636 this->TargetTypeValue = type;
637 if(this->TargetTypeValue >= STATIC_LIBRARY
638 && this->TargetTypeValue <= MODULE_LIBRARY)
640 this->RecordDependencies = true;
642 else
644 this->RecordDependencies = false;
648 //----------------------------------------------------------------------------
649 void cmTarget::SetMakefile(cmMakefile* mf)
651 // Set our makefile.
652 this->Makefile = mf;
654 // set the cmake instance of the properties
655 this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
657 // Check whether this is a DLL platform.
658 this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
659 this->Makefile->IsOn("CYGWIN") ||
660 this->Makefile->IsOn("MINGW"));
662 // Setup default property values.
663 this->SetPropertyDefault("INSTALL_NAME_DIR", "");
664 this->SetPropertyDefault("INSTALL_RPATH", "");
665 this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
666 this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
667 this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
668 this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
669 this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
670 this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
671 this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", 0);
673 // Collect the set of configuration types.
674 std::vector<std::string> configNames;
675 if(const char* configurationTypes =
676 mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
678 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
680 else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
682 if(*buildType)
684 configNames.push_back(buildType);
688 // Setup per-configuration property default values.
689 for(std::vector<std::string>::iterator ci = configNames.begin();
690 ci != configNames.end(); ++ci)
692 // Initialize per-configuration name postfix property from the
693 // variable only for non-executable targets. This preserves
694 // compatibility with previous CMake versions in which executables
695 // did not support this variable. Projects may still specify the
696 // property directly. TODO: Make this depend on backwards
697 // compatibility setting.
698 if(this->TargetTypeValue != cmTarget::EXECUTABLE)
700 std::string property = cmSystemTools::UpperCase(*ci);
701 property += "_POSTFIX";
702 this->SetPropertyDefault(property.c_str(), 0);
707 //----------------------------------------------------------------------------
708 bool cmTarget::IsExecutableWithExports()
710 return (this->GetType() == cmTarget::EXECUTABLE &&
711 this->GetPropertyAsBool("ENABLE_EXPORTS"));
714 //----------------------------------------------------------------------------
715 bool cmTarget::IsFrameworkOnApple()
717 return (this->GetType() == cmTarget::SHARED_LIBRARY &&
718 this->Makefile->IsOn("APPLE") &&
719 this->GetPropertyAsBool("FRAMEWORK"));
722 //----------------------------------------------------------------------------
723 bool cmTarget::IsAppBundleOnApple()
725 return (this->GetType() == cmTarget::EXECUTABLE &&
726 this->Makefile->IsOn("APPLE") &&
727 this->GetPropertyAsBool("MACOSX_BUNDLE"));
730 //----------------------------------------------------------------------------
731 class cmTargetTraceDependencies
733 public:
734 cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile);
735 void Trace();
736 private:
737 cmTarget* Target;
738 cmMakefile* Makefile;
739 cmGlobalGenerator* GlobalGenerator;
740 std::queue<cmStdString> DependencyQueue;
741 std::set<cmStdString> DependenciesQueued;
742 std::set<cmSourceFile*> TargetSources;
744 void QueueOnce(std::string const& name);
745 void QueueOnce(std::vector<std::string> const& names);
746 void QueueDependencies(cmSourceFile* sf);
747 bool IsUtility(std::string const& dep);
748 void CheckCustomCommand(cmCustomCommand const& cc);
749 void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
752 //----------------------------------------------------------------------------
753 cmTargetTraceDependencies
754 ::cmTargetTraceDependencies(cmTarget* target, const char* vsProjectFile):
755 Target(target)
757 // Convenience.
758 this->Makefile = this->Target->GetMakefile();
759 this->GlobalGenerator =
760 this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
762 // Queue all the source files already specified for the target.
763 std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
764 for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
765 si != sources.end(); ++si)
767 // Queue the source file itself in case it is generated.
768 this->QueueOnce((*si)->GetFullPath());
770 // Queue the dependencies of the source file in case they are
771 // generated.
772 this->QueueDependencies(*si);
774 // Track the sources already known to the target.
775 this->TargetSources.insert(*si);
778 // Queue the VS project file to check dependencies on the rule to
779 // generate it.
780 if(vsProjectFile)
782 this->QueueOnce(vsProjectFile);
785 // Queue pre-build, pre-link, and post-build rule dependencies.
786 this->CheckCustomCommands(this->Target->GetPreBuildCommands());
787 this->CheckCustomCommands(this->Target->GetPreLinkCommands());
788 this->CheckCustomCommands(this->Target->GetPostBuildCommands());
791 //----------------------------------------------------------------------------
792 void cmTargetTraceDependencies::Trace()
794 // Process one dependency at a time until the queue is empty.
795 while(!this->DependencyQueue.empty())
797 // Get the next dependency in from queue.
798 std::string dep = this->DependencyQueue.front();
799 this->DependencyQueue.pop();
801 // Check if we know how to generate this dependency.
802 if(cmSourceFile* sf =
803 this->Makefile->GetSourceFileWithOutput(dep.c_str()))
805 // Queue dependencies needed to generate this file.
806 this->QueueDependencies(sf);
808 // Make sure this file is in the target.
809 if(this->TargetSources.insert(sf).second)
811 this->Target->AddSourceFile(sf);
817 //----------------------------------------------------------------------------
818 void cmTargetTraceDependencies::QueueOnce(std::string const& name)
820 if(this->DependenciesQueued.insert(name).second)
822 this->DependencyQueue.push(name);
826 //----------------------------------------------------------------------------
827 void
828 cmTargetTraceDependencies::QueueOnce(std::vector<std::string> const& names)
830 for(std::vector<std::string>::const_iterator i = names.begin();
831 i != names.end(); ++i)
833 this->QueueOnce(*i);
837 //----------------------------------------------------------------------------
838 bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
840 // Dependencies on targets (utilities) are supposed to be named by
841 // just the target name. However for compatibility we support
842 // naming the output file generated by the target (assuming there is
843 // no output-name property which old code would not have set). In
844 // that case the target name will be the file basename of the
845 // dependency.
846 std::string util = cmSystemTools::GetFilenameName(dep);
847 if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
849 util = cmSystemTools::GetFilenameWithoutLastExtension(util);
852 // Check for a non-imported target with this name.
853 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, util.c_str()))
855 // If we find the target and the dep was given as a full path,
856 // then make sure it was not a full path to something else, and
857 // the fact that the name matched a target was just a coincidence.
858 if(cmSystemTools::FileIsFullPath(dep.c_str()))
860 // This is really only for compatibility so we do not need to
861 // worry about configuration names and output names.
862 std::string tLocation = t->GetLocation(0);
863 tLocation = cmSystemTools::GetFilenamePath(tLocation);
864 std::string depLocation = cmSystemTools::GetFilenamePath(dep);
865 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
866 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
867 if(depLocation == tLocation)
869 this->Target->AddUtility(util.c_str());
870 return true;
873 else
875 // The original name of the dependency was not a full path. It
876 // must name a target, so add the target-level dependency.
877 this->Target->AddUtility(util.c_str());
878 return true;
882 // The dependency does not name a target built in this project.
883 return false;
886 //----------------------------------------------------------------------------
887 void cmTargetTraceDependencies::QueueDependencies(cmSourceFile* sf)
889 // Queue dependency added explicitly by the user.
890 if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
892 std::vector<std::string> objDeps;
893 cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
894 this->QueueOnce(objDeps);
897 // Queue dependencies added programatically by commands.
898 this->QueueOnce(sf->GetDepends());
900 // Queue custom command dependencies.
901 if(cmCustomCommand const* cc = sf->GetCustomCommand())
903 this->CheckCustomCommand(*cc);
908 //----------------------------------------------------------------------------
909 void
910 cmTargetTraceDependencies
911 ::CheckCustomCommand(cmCustomCommand const& cc)
913 // Transform command names that reference targets built in this
914 // project to corresponding target-level dependencies.
915 for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
916 cit != cc.GetCommandLines().end(); ++cit)
918 std::string const& command = *cit->begin();
919 // Look for a non-imported target with this name.
920 if(cmTarget* t = this->GlobalGenerator->FindTarget(0, command.c_str()))
922 if(t->GetType() == cmTarget::EXECUTABLE)
924 // The command refers to an executable target built in
925 // this project. Add the target-level dependency to make
926 // sure the executable is up to date before this custom
927 // command possibly runs.
928 this->Target->AddUtility(command.c_str());
933 // Queue the custom command dependencies.
934 std::vector<std::string> const& depends = cc.GetDepends();
935 for(std::vector<std::string>::const_iterator di = depends.begin();
936 di != depends.end(); ++di)
938 std::string const& dep = *di;
939 if(!this->IsUtility(dep))
941 // The dependency does not name a target and may be a file we
942 // know how to generate. Queue it.
943 this->QueueOnce(dep);
948 //----------------------------------------------------------------------------
949 void
950 cmTargetTraceDependencies
951 ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
953 for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
954 cli != commands.end(); ++cli)
956 this->CheckCustomCommand(*cli);
960 //----------------------------------------------------------------------------
961 void cmTarget::TraceDependencies(const char* vsProjectFile)
963 // Use a helper object to trace the dependencies.
964 cmTargetTraceDependencies tracer(this, vsProjectFile);
965 tracer.Trace();
968 //----------------------------------------------------------------------------
969 void cmTarget::AddSources(std::vector<std::string> const& srcs)
971 for(std::vector<std::string>::const_iterator i = srcs.begin();
972 i != srcs.end(); ++i)
974 this->AddSource(i->c_str());
978 //----------------------------------------------------------------------------
979 cmSourceFile* cmTarget::AddSource(const char* s)
981 std::string src = s;
983 // For backwards compatibility replace varibles in source names.
984 // This should eventually be removed.
985 this->Makefile->ExpandVariablesInString(src);
987 cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
988 this->AddSourceFile(sf);
989 return sf;
992 //----------------------------------------------------------------------------
993 struct cmTarget::SourceFileFlags
994 cmTarget::GetTargetSourceFileFlags(const cmSourceFile* sf)
996 struct SourceFileFlags flags;
997 const char* files;
998 std::vector<std::string>::iterator it;
1000 flags.PrivateHeader = false;
1001 flags.PublicHeader = false;
1002 flags.Resource = false;
1004 files = this->GetProperty("PRIVATE_HEADER");
1005 if ((files) && (*files))
1007 std::vector<std::string> relFiles;
1008 cmSystemTools::ExpandListArgument(files, relFiles);
1009 for(it = relFiles.begin(); it != relFiles.end(); ++it)
1011 if(sf == this->GetMakefile()->GetSource(it->c_str()))
1013 flags.PrivateHeader = true;
1014 break;
1019 // Only consider marking it as a public header if it is *NOT* already marked
1020 // as a private header:
1022 if(!flags.PrivateHeader)
1024 files = this->GetProperty("PUBLIC_HEADER");
1025 if ((files) && (*files))
1027 std::vector<std::string> relFiles;
1028 cmSystemTools::ExpandListArgument(files, relFiles);
1029 for(it = relFiles.begin(); it != relFiles.end(); ++it)
1031 if(sf == this->GetMakefile()->GetSource(it->c_str()))
1033 flags.PublicHeader = true;
1034 break;
1040 const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION");
1041 if(location && cmStdString(location) == "Resources")
1043 flags.Resource = true;
1046 // Don't bother with the loop if it's already marked as a resource:
1048 if(!flags.Resource)
1050 files = this->GetProperty("RESOURCE");
1051 if ((files) && (*files))
1053 std::vector<std::string> relFiles;
1054 cmSystemTools::ExpandListArgument(files, relFiles);
1055 for(it = relFiles.begin(); it != relFiles.end(); ++it)
1057 if(sf == this->GetMakefile()->GetSource(it->c_str()))
1059 flags.Resource = true;
1060 break;
1066 return flags;
1069 //----------------------------------------------------------------------------
1070 void cmTarget::MergeLinkLibraries( cmMakefile& mf,
1071 const char *selfname,
1072 const LinkLibraryVectorType& libs )
1074 // Only add on libraries we haven't added on before.
1075 // Assumption: the global link libraries could only grow, never shrink
1076 LinkLibraryVectorType::const_iterator i = libs.begin();
1077 i += this->PrevLinkedLibraries.size();
1078 for( ; i != libs.end(); ++i )
1080 // We call this so that the dependencies get written to the cache
1081 this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
1083 this->PrevLinkedLibraries = libs;
1086 //----------------------------------------------------------------------------
1087 void cmTarget::AddLinkDirectory(const char* d)
1089 // Make sure we don't add unnecessary search directories.
1090 if(this->LinkDirectoriesEmmitted.insert(d).second)
1092 this->LinkDirectories.push_back(d);
1096 //----------------------------------------------------------------------------
1097 const std::vector<std::string>& cmTarget::GetLinkDirectories()
1099 return this->LinkDirectories;
1102 //----------------------------------------------------------------------------
1103 void cmTarget::ClearDependencyInformation( cmMakefile& mf,
1104 const char* target )
1106 // Clear the dependencies. The cache variable must exist iff we are
1107 // recording dependency information for this target.
1108 std::string depname = target;
1109 depname += "_LIB_DEPENDS";
1110 if (this->RecordDependencies)
1112 mf.AddCacheDefinition(depname.c_str(), "",
1113 "Dependencies for target", cmCacheManager::STATIC);
1115 else
1117 if (mf.GetDefinition( depname.c_str() ))
1119 std::string message = "Target ";
1120 message += target;
1121 message += " has dependency information when it shouldn't.\n";
1122 message += "Your cache is probably stale. Please remove the entry\n ";
1123 message += depname;
1124 message += "\nfrom the cache.";
1125 cmSystemTools::Error( message.c_str() );
1130 //----------------------------------------------------------------------------
1131 void cmTarget::AddLinkLibrary(const std::string& lib,
1132 LinkLibraryType llt)
1134 this->AddFramework(lib.c_str(), llt);
1135 cmTarget::LibraryID tmp;
1136 tmp.first = lib;
1137 tmp.second = llt;
1138 this->LinkLibraries.push_back(tmp);
1139 this->OriginalLinkLibraries.push_back(tmp);
1142 //----------------------------------------------------------------------------
1143 bool cmTarget::NameResolvesToFramework(const std::string& libname)
1145 return this->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()->
1146 NameResolvesToFramework(libname);
1149 //----------------------------------------------------------------------------
1150 bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
1152 (void)llt; // TODO: What is this?
1153 if(this->NameResolvesToFramework(libname.c_str()))
1155 std::string frameworkDir = libname;
1156 frameworkDir += "/../";
1157 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1158 std::vector<std::string>::iterator i =
1159 std::find(this->Frameworks.begin(),
1160 this->Frameworks.end(), frameworkDir);
1161 if(i == this->Frameworks.end())
1163 this->Frameworks.push_back(frameworkDir);
1165 return true;
1167 return false;
1170 //----------------------------------------------------------------------------
1171 void cmTarget::AddLinkLibrary(cmMakefile& mf,
1172 const char *target, const char* lib,
1173 LinkLibraryType llt)
1175 // Never add a self dependency, even if the user asks for it.
1176 if(strcmp( target, lib ) == 0)
1178 return;
1180 this->AddFramework(lib, llt);
1181 cmTarget::LibraryID tmp;
1182 tmp.first = lib;
1183 tmp.second = llt;
1184 this->LinkLibraries.push_back( tmp );
1185 this->OriginalLinkLibraries.push_back(tmp);
1187 // Add the explicit dependency information for this target. This is
1188 // simply a set of libraries separated by ";". There should always
1189 // be a trailing ";". These library names are not canonical, in that
1190 // they may be "-framework x", "-ly", "/path/libz.a", etc.
1191 // We shouldn't remove duplicates here because external libraries
1192 // may be purposefully duplicated to handle recursive dependencies,
1193 // and we removing one instance will break the link line. Duplicates
1194 // will be appropriately eliminated at emit time.
1195 if(this->RecordDependencies)
1197 std::string targetEntry = target;
1198 targetEntry += "_LIB_DEPENDS";
1199 std::string dependencies;
1200 const char* old_val = mf.GetDefinition( targetEntry.c_str() );
1201 if( old_val )
1203 dependencies += old_val;
1205 switch (llt)
1207 case cmTarget::GENERAL:
1208 dependencies += "general";
1209 break;
1210 case cmTarget::DEBUG:
1211 dependencies += "debug";
1212 break;
1213 case cmTarget::OPTIMIZED:
1214 dependencies += "optimized";
1215 break;
1217 dependencies += ";";
1218 dependencies += lib;
1219 dependencies += ";";
1220 mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
1221 "Dependencies for the target",
1222 cmCacheManager::STATIC );
1227 //----------------------------------------------------------------------------
1228 void
1229 cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
1231 // There are two key parts of the dependency analysis: (1)
1232 // determining the libraries in the link line, and (2) constructing
1233 // the dependency graph for those libraries.
1235 // The latter is done using the cache entries that record the
1236 // dependencies of each library.
1238 // The former is a more thorny issue, since it is not clear how to
1239 // determine if two libraries listed on the link line refer to the a
1240 // single library or not. For example, consider the link "libraries"
1241 // /usr/lib/libtiff.so -ltiff
1242 // Is this one library or two? The solution implemented here is the
1243 // simplest (and probably the only practical) one: two libraries are
1244 // the same if their "link strings" are identical. Thus, the two
1245 // libraries above are considered distinct. This also means that for
1246 // dependency analysis to be effective, the CMake user must specify
1247 // libraries build by his project without using any linker flags or
1248 // file extensions. That is,
1249 // LINK_LIBRARIES( One Two )
1250 // instead of
1251 // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
1252 // The former is probably what most users would do, but it never
1253 // hurts to document the assumptions. :-) Therefore, in the analysis
1254 // code, the "canonical name" of a library is simply its name as
1255 // given to a LINK_LIBRARIES command.
1257 // Also, we will leave the original link line intact; we will just add any
1258 // dependencies that were missing.
1260 // There is a problem with recursive external libraries
1261 // (i.e. libraries with no dependency information that are
1262 // recursively dependent). We must make sure that the we emit one of
1263 // the libraries twice to satisfy the recursion, but we shouldn't
1264 // emit it more times than necessary. In particular, we must make
1265 // sure that handling this improbable case doesn't cost us when
1266 // dealing with the common case of non-recursive libraries. The
1267 // solution is to assume that the recursion is satisfied at one node
1268 // of the dependency tree. To illustrate, assume libA and libB are
1269 // extrenal and mutually dependent. Suppose libX depends on
1270 // libA, and libY on libA and libX. Then
1271 // TARGET_LINK_LIBRARIES( Y X A B A )
1272 // TARGET_LINK_LIBRARIES( X A B A )
1273 // TARGET_LINK_LIBRARIES( Exec Y )
1274 // would result in "-lY -lX -lA -lB -lA". This is the correct way to
1275 // specify the dependencies, since the mutual dependency of A and B
1276 // is resolved *every time libA is specified*.
1278 // Something like
1279 // TARGET_LINK_LIBRARIES( Y X A B A )
1280 // TARGET_LINK_LIBRARIES( X A B )
1281 // TARGET_LINK_LIBRARIES( Exec Y )
1282 // would result in "-lY -lX -lA -lB", and the mutual dependency
1283 // information is lost. This is because in some case (Y), the mutual
1284 // dependency of A and B is listed, while in another other case (X),
1285 // it is not. Depending on which line actually emits A, the mutual
1286 // dependency may or may not be on the final link line. We can't
1287 // handle this pathalogical case cleanly without emitting extra
1288 // libraries for the normal cases. Besides, the dependency
1289 // information for X is wrong anyway: if we build an executable
1290 // depending on X alone, we would not have the mutual dependency on
1291 // A and B resolved.
1293 // IMPROVEMENTS:
1294 // -- The current algorithm will not always pick the "optimal" link line
1295 // when recursive dependencies are present. It will instead break the
1296 // cycles at an aribtrary point. The majority of projects won't have
1297 // cyclic dependencies, so this is probably not a big deal. Note that
1298 // the link line is always correct, just not necessary optimal.
1301 // Expand variables in link library names. This is for backwards
1302 // compatibility with very early CMake versions and should
1303 // eventually be removed. This code was moved here from the end of
1304 // old source list processing code which was called just before this
1305 // method.
1306 for(LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
1307 p != this->LinkLibraries.end(); ++p)
1309 this->Makefile->ExpandVariablesInString(p->first, true, true);
1313 typedef std::vector< std::string > LinkLine;
1315 // The dependency map.
1316 DependencyMap dep_map;
1318 // 1. Build the dependency graph
1320 for(LinkLibraryVectorType::reverse_iterator lib
1321 = this->LinkLibraries.rbegin();
1322 lib != this->LinkLibraries.rend(); ++lib)
1324 this->GatherDependencies( mf, *lib, dep_map);
1327 // 2. Remove any dependencies that are already satisfied in the original
1328 // link line.
1330 for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
1331 lib != this->LinkLibraries.end(); ++lib)
1333 for( LinkLibraryVectorType::iterator lib2 = lib;
1334 lib2 != this->LinkLibraries.end(); ++lib2)
1336 this->DeleteDependency( dep_map, *lib, *lib2);
1341 // 3. Create the new link line by simply emitting any dependencies that are
1342 // missing. Start from the back and keep adding.
1344 std::set<DependencyMap::key_type> done, visited;
1345 std::vector<DependencyMap::key_type> newLinkLibraries;
1346 for(LinkLibraryVectorType::reverse_iterator lib =
1347 this->LinkLibraries.rbegin();
1348 lib != this->LinkLibraries.rend(); ++lib)
1350 // skip zero size library entries, this may happen
1351 // if a variable expands to nothing.
1352 if (lib->first.size() != 0)
1354 this->Emit( *lib, dep_map, done, visited, newLinkLibraries );
1358 // 4. Add the new libraries to the link line.
1360 for( std::vector<DependencyMap::key_type>::reverse_iterator k =
1361 newLinkLibraries.rbegin();
1362 k != newLinkLibraries.rend(); ++k )
1364 // get the llt from the dep_map
1365 this->LinkLibraries.push_back( std::make_pair(k->first,k->second) );
1367 this->LinkLibrariesAnalyzed = true;
1370 //----------------------------------------------------------------------------
1371 void cmTarget::InsertDependency( DependencyMap& depMap,
1372 const LibraryID& lib,
1373 const LibraryID& dep)
1375 depMap[lib].push_back(dep);
1378 //----------------------------------------------------------------------------
1379 void cmTarget::DeleteDependency( DependencyMap& depMap,
1380 const LibraryID& lib,
1381 const LibraryID& dep)
1383 // Make sure there is an entry in the map for lib. If so, delete all
1384 // dependencies to dep. There may be repeated entries because of
1385 // external libraries that are specified multiple times.
1386 DependencyMap::iterator map_itr = depMap.find( lib );
1387 if( map_itr != depMap.end() )
1389 DependencyList& depList = map_itr->second;
1390 DependencyList::iterator itr;
1391 while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
1392 depList.end() )
1394 depList.erase( itr );
1399 //----------------------------------------------------------------------------
1400 void cmTarget::Emit(const LibraryID lib,
1401 const DependencyMap& dep_map,
1402 std::set<LibraryID>& emitted,
1403 std::set<LibraryID>& visited,
1404 DependencyList& link_line )
1406 // It's already been emitted
1407 if( emitted.find(lib) != emitted.end() )
1409 return;
1412 // Emit the dependencies only if this library node hasn't been
1413 // visited before. If it has, then we have a cycle. The recursion
1414 // that got us here should take care of everything.
1416 if( visited.insert(lib).second )
1418 if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
1420 const DependencyList& dep_on = dep_map.find( lib )->second;
1421 DependencyList::const_reverse_iterator i;
1423 // To cater for recursive external libraries, we must emit
1424 // duplicates on this link line *unless* they were emitted by
1425 // some other node, in which case we assume that the recursion
1426 // was resolved then. We making the simplifying assumption that
1427 // any duplicates on a single link line are on purpose, and must
1428 // be preserved.
1430 // This variable will keep track of the libraries that were
1431 // emitted directory from the current node, and not from a
1432 // recursive call. This way, if we come across a library that
1433 // has already been emitted, we repeat it iff it has been
1434 // emitted here.
1435 std::set<DependencyMap::key_type> emitted_here;
1436 for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
1438 if( emitted_here.find(*i) != emitted_here.end() )
1440 // a repeat. Must emit.
1441 emitted.insert(*i);
1442 link_line.push_back( *i );
1444 else
1446 // Emit only if no-one else has
1447 if( emitted.find(*i) == emitted.end() )
1449 // emit dependencies
1450 Emit( *i, dep_map, emitted, visited, link_line );
1451 // emit self
1452 emitted.insert(*i);
1453 emitted_here.insert(*i);
1454 link_line.push_back( *i );
1462 //----------------------------------------------------------------------------
1463 void cmTarget::GatherDependencies( const cmMakefile& mf,
1464 const LibraryID& lib,
1465 DependencyMap& dep_map)
1467 // If the library is already in the dependency map, then it has
1468 // already been fully processed.
1469 if( dep_map.find(lib) != dep_map.end() )
1471 return;
1474 const char* deps = mf.GetDefinition( (lib.first+"_LIB_DEPENDS").c_str() );
1475 if( deps && strcmp(deps,"") != 0 )
1477 // Make sure this library is in the map, even if it has an empty
1478 // set of dependencies. This distinguishes the case of explicitly
1479 // no dependencies with that of unspecified dependencies.
1480 dep_map[lib];
1482 // Parse the dependency information, which is a set of
1483 // type, library pairs separated by ";". There is always a trailing ";".
1484 cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
1485 std::string depline = deps;
1486 std::string::size_type start = 0;
1487 std::string::size_type end;
1488 end = depline.find( ";", start );
1489 while( end != std::string::npos )
1491 std::string l = depline.substr( start, end-start );
1492 if( l.size() != 0 )
1494 if (l == "debug")
1496 llt = cmTarget::DEBUG;
1498 else if (l == "optimized")
1500 llt = cmTarget::OPTIMIZED;
1502 else if (l == "general")
1504 llt = cmTarget::GENERAL;
1506 else
1508 LibraryID lib2(l,llt);
1509 this->InsertDependency( dep_map, lib, lib2);
1510 this->GatherDependencies( mf, lib2, dep_map);
1511 llt = cmTarget::GENERAL;
1514 start = end+1; // skip the ;
1515 end = depline.find( ";", start );
1517 // cannot depend on itself
1518 this->DeleteDependency( dep_map, lib, lib);
1522 //----------------------------------------------------------------------------
1523 void cmTarget::SetProperty(const char* prop, const char* value)
1525 if (!prop)
1527 return;
1529 if (!value)
1531 value = "NOTFOUND";
1534 this->Properties.SetProperty(prop, value, cmProperty::TARGET);
1536 // If imported information is being set, wipe out cached
1537 // information.
1538 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1540 this->ImportInfoMap.clear();
1544 //----------------------------------------------------------------------------
1545 void cmTarget::AppendProperty(const char* prop, const char* value)
1547 if (!prop)
1549 return;
1551 this->Properties.AppendProperty(prop, value, cmProperty::TARGET);
1553 // If imported information is being set, wipe out cached
1554 // information.
1555 if(this->IsImported() && strncmp(prop, "IMPORTED", 8) == 0)
1557 this->ImportInfoMap.clear();
1561 //----------------------------------------------------------------------------
1562 void cmTarget::MarkAsImported()
1564 this->IsImportedTarget = true;
1567 //----------------------------------------------------------------------------
1568 const char* cmTarget::GetDirectory(const char* config, bool implib)
1570 if (this->IsImported())
1572 return this->ImportedGetDirectory(config, implib);
1574 else
1576 return this->NormalGetDirectory(config, implib);
1580 //----------------------------------------------------------------------------
1581 const char* cmTarget::ImportedGetDirectory(const char* config, bool implib)
1583 this->Directory =
1584 cmSystemTools::GetFilenamePath(
1585 this->ImportedGetFullPath(config, implib));
1586 return this->Directory.c_str();
1589 //----------------------------------------------------------------------------
1590 const char* cmTarget::NormalGetDirectory(const char* config, bool implib)
1592 if(config && *config)
1594 // Do not create the directory when config is given:
1595 this->Directory = this->GetAndCreateOutputDir(implib, true);
1596 // Add the configuration's subdirectory.
1597 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
1598 AppendDirectoryForConfig("/", config, "", this->Directory);
1599 return this->Directory.c_str();
1601 else
1603 return this->GetOutputDir(implib);
1607 //----------------------------------------------------------------------------
1608 const char* cmTarget::GetLocation(const char* config)
1610 if (this->IsImported())
1612 return this->ImportedGetLocation(config);
1614 else
1616 return this->NormalGetLocation(config);
1620 //----------------------------------------------------------------------------
1621 const char* cmTarget::ImportedGetLocation(const char* config)
1623 this->Location = this->ImportedGetFullPath(config, false);
1624 return this->Location.c_str();
1627 //----------------------------------------------------------------------------
1628 const char* cmTarget::NormalGetLocation(const char* config)
1630 this->Location = this->GetDirectory(config);
1631 if(!this->Location.empty())
1633 this->Location += "/";
1635 if(!config)
1637 // No specific configuration was given so it will not appear on
1638 // the result of GetDirectory. Add a name here to be replaced at
1639 // build time.
1640 const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
1641 if(cfgid && strcmp(cfgid, ".") != 0)
1643 this->Location += cfgid;
1644 this->Location += "/";
1647 this->Location += this->GetFullName(config, false);
1648 return this->Location.c_str();
1651 //----------------------------------------------------------------------------
1652 void cmTarget::GetTargetVersion(int& major, int& minor)
1654 // Set the default values.
1655 major = 0;
1656 minor = 0;
1658 // Look for a VERSION property.
1659 if(const char* version = this->GetProperty("VERSION"))
1661 // Try to parse the version number and store the results that were
1662 // successfully parsed.
1663 int parsed_major;
1664 int parsed_minor;
1665 switch(sscanf(version, "%d.%d", &parsed_major, &parsed_minor))
1667 case 2: minor = parsed_minor; // no break!
1668 case 1: major = parsed_major; // no break!
1669 default: break;
1674 //----------------------------------------------------------------------------
1675 const char *cmTarget::GetProperty(const char* prop)
1677 return this->GetProperty(prop, cmProperty::TARGET);
1680 //----------------------------------------------------------------------------
1681 void cmTarget::ComputeObjectFiles()
1683 if (this->IsImported())
1685 return;
1687 #if 0
1688 std::vector<std::string> dirs;
1689 this->Makefile->GetLocalGenerator()->
1690 GetTargetObjectFileDirectories(this,
1691 dirs);
1692 std::string objectFiles;
1693 std::string objExtensionLookup1 = "CMAKE_";
1694 std::string objExtensionLookup2 = "_OUTPUT_EXTENSION";
1696 for(std::vector<std::string>::iterator d = dirs.begin();
1697 d != dirs.end(); ++d)
1699 for(std::vector<cmSourceFile*>::iterator s = this->SourceFiles.begin();
1700 s != this->SourceFiles.end(); ++s)
1702 cmSourceFile* sf = *s;
1703 if(const char* lang = sf->GetLanguage())
1705 std::string lookupObj = objExtensionLookup1 + lang;
1706 lookupObj += objExtensionLookup2;
1707 const char* obj = this->Makefile->GetDefinition(lookupObj.c_str());
1708 if(obj)
1710 if(objectFiles.size())
1712 objectFiles += ";";
1714 std::string objFile = *d;
1715 objFile += "/";
1716 objFile += this->Makefile->GetLocalGenerator()->
1717 GetSourceObjectName(*sf);
1718 objFile += obj;
1719 objectFiles += objFile;
1724 this->SetProperty("OBJECT_FILES", objectFiles.c_str());
1725 #endif
1728 //----------------------------------------------------------------------------
1729 const char *cmTarget::GetProperty(const char* prop,
1730 cmProperty::ScopeType scope)
1732 if(!prop)
1734 return 0;
1737 // Watch for special "computed" properties that are dependent on
1738 // other properties or variables. Always recompute them.
1739 if(this->GetType() == cmTarget::EXECUTABLE ||
1740 this->GetType() == cmTarget::STATIC_LIBRARY ||
1741 this->GetType() == cmTarget::SHARED_LIBRARY ||
1742 this->GetType() == cmTarget::MODULE_LIBRARY)
1744 if(!this->IsImported() && strcmp(prop,"LOCATION") == 0)
1746 // Set the LOCATION property of the target. Note that this
1747 // cannot take into account the per-configuration name of the
1748 // target because the configuration type may not be known at
1749 // CMake time. It is now deprecated as described in the
1750 // documentation.
1751 this->SetProperty("LOCATION", this->GetLocation(0));
1754 // Support "LOCATION_<CONFIG>".
1755 if(strncmp(prop, "LOCATION_", 9) == 0)
1757 std::string configName = prop+9;
1758 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1760 else
1762 // Support "<CONFIG>_LOCATION" for compatiblity.
1763 int len = static_cast<int>(strlen(prop));
1764 if(len > 9 && strcmp(prop+len-9, "_LOCATION") == 0)
1766 std::string configName(prop, len-9);
1767 if(configName != "IMPORTED")
1769 this->SetProperty(prop, this->GetLocation(configName.c_str()));
1775 if (strcmp(prop,"IMPORTED") == 0)
1777 return this->IsImported()?"TRUE":"FALSE";
1780 if(!strcmp(prop,"SOURCES"))
1782 cmOStringStream ss;
1783 const char* sep = "";
1784 for(std::vector<cmSourceFile*>::const_iterator
1785 i = this->SourceFiles.begin();
1786 i != this->SourceFiles.end(); ++i)
1788 // Separate from the previous list entries.
1789 ss << sep;
1790 sep = ";";
1792 // Construct what is known about this source file location.
1793 cmSourceFileLocation const& location = (*i)->GetLocation();
1794 std::string sname = location.GetDirectory();
1795 if(!sname.empty())
1797 sname += "/";
1799 sname += location.GetName();
1801 // Append this list entry.
1802 ss << sname;
1804 this->SetProperty("SOURCES", ss.str().c_str());
1807 // the type property returns what type the target is
1808 if (!strcmp(prop,"TYPE"))
1810 switch( this->GetType() )
1812 case cmTarget::STATIC_LIBRARY:
1813 return "STATIC_LIBRARY";
1814 // break; /* unreachable */
1815 case cmTarget::MODULE_LIBRARY:
1816 return "MODULE_LIBRARY";
1817 // break; /* unreachable */
1818 case cmTarget::SHARED_LIBRARY:
1819 return "SHARED_LIBRARY";
1820 // break; /* unreachable */
1821 case cmTarget::EXECUTABLE:
1822 return "EXECUTABLE";
1823 // break; /* unreachable */
1824 case cmTarget::UTILITY:
1825 return "UTILITY";
1826 // break; /* unreachable */
1827 case cmTarget::GLOBAL_TARGET:
1828 return "GLOBAL_TARGET";
1829 // break; /* unreachable */
1830 case cmTarget::INSTALL_FILES:
1831 return "INSTALL_FILES";
1832 // break; /* unreachable */
1833 case cmTarget::INSTALL_PROGRAMS:
1834 return "INSTALL_PROGRAMS";
1835 // break; /* unreachable */
1836 case cmTarget::INSTALL_DIRECTORY:
1837 return "INSTALL_DIRECTORY";
1838 // break; /* unreachable */
1840 return 0;
1842 bool chain = false;
1843 const char *retVal =
1844 this->Properties.GetPropertyValue(prop, scope, chain);
1845 if (chain)
1847 return this->Makefile->GetProperty(prop,scope);
1849 return retVal;
1852 //----------------------------------------------------------------------------
1853 bool cmTarget::GetPropertyAsBool(const char* prop)
1855 return cmSystemTools::IsOn(this->GetProperty(prop));
1858 //----------------------------------------------------------------------------
1859 const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
1861 if(this->GetProperty("HAS_CXX"))
1863 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
1865 const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
1866 if (linkerLang==0)
1868 // if the property has not yet been set, collect all languages in the
1869 // target and then find the language with the highest preference value
1870 std::set<cmStdString> languages;
1871 this->GetLanguages(languages);
1873 std::string linkerLangList; // only used for the error message
1874 int maxLinkerPref = 0;
1875 bool multiplePreferedLanguages = false;
1876 for(std::set<cmStdString>::const_iterator sit = languages.begin();
1877 sit != languages.end(); ++sit)
1879 int linkerPref = gg->GetLinkerPreference(sit->c_str());
1880 if ((linkerPref > maxLinkerPref) || (linkerLang==0))
1882 maxLinkerPref = linkerPref;
1883 linkerLang = sit->c_str();
1884 linkerLangList = *sit;
1885 multiplePreferedLanguages = false;
1887 else if (linkerPref == maxLinkerPref)
1889 linkerLangList += "; ";
1890 linkerLangList += *sit;
1891 multiplePreferedLanguages = true;
1895 if (linkerLang!=0)
1897 const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", linkerLang);
1899 if (multiplePreferedLanguages)
1901 cmOStringStream err;
1902 err << "Error: Target " << this->Name << " contains multiple languages "
1903 << "with the highest linker preference (" << maxLinkerPref << "): "
1904 << linkerLangList << "\n"
1905 << "You must set the LINKER_LANGUAGE property for this target.";
1906 cmSystemTools::Error(err.str().c_str());
1909 return this->GetProperty("LINKER_LANGUAGE");
1912 //----------------------------------------------------------------------------
1913 const char* cmTarget::GetCreateRuleVariable()
1915 switch(this->GetType())
1917 case cmTarget::STATIC_LIBRARY:
1918 return "_CREATE_STATIC_LIBRARY";
1919 case cmTarget::SHARED_LIBRARY:
1920 return "_CREATE_SHARED_LIBRARY";
1921 case cmTarget::MODULE_LIBRARY:
1922 return "_CREATE_SHARED_MODULE";
1923 case cmTarget::EXECUTABLE:
1924 return "_LINK_EXECUTABLE";
1925 case cmTarget::UTILITY:
1926 case cmTarget::GLOBAL_TARGET:
1927 case cmTarget::INSTALL_FILES:
1928 case cmTarget::INSTALL_PROGRAMS:
1929 case cmTarget::INSTALL_DIRECTORY:
1930 break;
1932 return "";
1935 //----------------------------------------------------------------------------
1936 const char* cmTarget::GetSuffixVariableInternal(TargetType type,
1937 bool implib)
1939 switch(type)
1941 case cmTarget::STATIC_LIBRARY:
1942 return "CMAKE_STATIC_LIBRARY_SUFFIX";
1943 case cmTarget::SHARED_LIBRARY:
1944 return (implib
1945 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1946 : "CMAKE_SHARED_LIBRARY_SUFFIX");
1947 case cmTarget::MODULE_LIBRARY:
1948 return (implib
1949 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1950 : "CMAKE_SHARED_MODULE_SUFFIX");
1951 case cmTarget::EXECUTABLE:
1952 return (implib
1953 ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
1954 : "CMAKE_EXECUTABLE_SUFFIX");
1955 case cmTarget::UTILITY:
1956 case cmTarget::GLOBAL_TARGET:
1957 case cmTarget::INSTALL_FILES:
1958 case cmTarget::INSTALL_PROGRAMS:
1959 case cmTarget::INSTALL_DIRECTORY:
1960 break;
1962 return "";
1966 //----------------------------------------------------------------------------
1967 const char* cmTarget::GetPrefixVariableInternal(TargetType type,
1968 bool implib)
1970 switch(type)
1972 case cmTarget::STATIC_LIBRARY:
1973 return "CMAKE_STATIC_LIBRARY_PREFIX";
1974 case cmTarget::SHARED_LIBRARY:
1975 return (implib
1976 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
1977 : "CMAKE_SHARED_LIBRARY_PREFIX");
1978 case cmTarget::MODULE_LIBRARY:
1979 return (implib
1980 ? "CMAKE_IMPORT_LIBRARY_PREFIX"
1981 : "CMAKE_SHARED_MODULE_PREFIX");
1982 case cmTarget::EXECUTABLE:
1983 return (implib? "CMAKE_IMPORT_LIBRARY_PREFIX" : "");
1984 case cmTarget::UTILITY:
1985 case cmTarget::GLOBAL_TARGET:
1986 case cmTarget::INSTALL_FILES:
1987 case cmTarget::INSTALL_PROGRAMS:
1988 case cmTarget::INSTALL_DIRECTORY:
1989 break;
1991 return "";
1994 //----------------------------------------------------------------------------
1995 std::string cmTarget::GetPDBName(const char* config)
1997 std::string prefix;
1998 std::string base;
1999 std::string suffix;
2000 this->GetFullNameInternal(this->GetType(), config, false,
2001 prefix, base, suffix);
2002 return prefix+base+".pdb";
2005 //----------------------------------------------------------------------------
2006 std::string cmTarget::GetSOName(const char* config)
2008 if(this->IsImported())
2010 // Lookup the imported soname.
2011 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2013 return info->SOName;
2015 else
2017 return "";
2020 else
2022 // Compute the soname that will be built.
2023 std::string name;
2024 std::string soName;
2025 std::string realName;
2026 std::string impName;
2027 std::string pdbName;
2028 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2029 return soName;
2033 //----------------------------------------------------------------------------
2034 std::string cmTarget::NormalGetRealName(const char* config)
2036 // This should not be called for imported targets.
2037 // TODO: Split cmTarget into a class hierarchy to get compile-time
2038 // enforcement of the limited imported target API.
2039 if(this->IsImported())
2041 abort();
2044 if(this->GetType() == cmTarget::EXECUTABLE)
2046 // Compute the real name that will be built.
2047 std::string name;
2048 std::string realName;
2049 std::string impName;
2050 std::string pdbName;
2051 this->GetExecutableNames(name, realName, impName, pdbName, config);
2052 return realName;
2054 else
2056 // Compute the real name that will be built.
2057 std::string name;
2058 std::string soName;
2059 std::string realName;
2060 std::string impName;
2061 std::string pdbName;
2062 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2063 return realName;
2067 //----------------------------------------------------------------------------
2068 std::string cmTarget::GetFullName(const char* config, bool implib)
2070 if(this->IsImported())
2072 return this->GetFullNameImported(config, implib);
2074 else
2076 return this->GetFullNameInternal(this->GetType(), config, implib);
2080 //----------------------------------------------------------------------------
2081 std::string cmTarget::GetFullNameImported(const char* config, bool implib)
2083 return cmSystemTools::GetFilenameName(
2084 this->ImportedGetFullPath(config, implib));
2087 //----------------------------------------------------------------------------
2088 void cmTarget::GetFullNameComponents(std::string& prefix, std::string& base,
2089 std::string& suffix, const char* config,
2090 bool implib)
2092 this->GetFullNameInternal(this->GetType(), config, implib,
2093 prefix, base, suffix);
2096 //----------------------------------------------------------------------------
2097 std::string cmTarget::GetFullPath(const char* config, bool implib,
2098 bool realname)
2100 if(this->IsImported())
2102 return this->ImportedGetFullPath(config, implib);
2104 else
2106 return this->NormalGetFullPath(config, implib, realname);
2110 //----------------------------------------------------------------------------
2111 std::string cmTarget::NormalGetFullPath(const char* config, bool implib,
2112 bool realname)
2114 // Start with the output directory for the target.
2115 std::string fpath = this->GetDirectory(config, implib);
2116 fpath += "/";
2118 // Add the full name of the target.
2119 if(implib)
2121 fpath += this->GetFullName(config, true);
2123 else if(realname)
2125 fpath += this->NormalGetRealName(config);
2127 else
2129 fpath += this->GetFullName(config, false);
2131 return fpath;
2134 //----------------------------------------------------------------------------
2135 std::string cmTarget::ImportedGetFullPath(const char* config, bool implib)
2137 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
2139 if(implib)
2141 return info->ImportLibrary;
2143 else
2145 return info->Location;
2148 else
2150 std::string result = this->GetName();
2151 result += "-NOTFOUND";
2152 return result;
2156 //----------------------------------------------------------------------------
2157 std::string
2158 cmTarget::GetFullNameInternal(TargetType type, const char* config,
2159 bool implib)
2161 std::string prefix;
2162 std::string base;
2163 std::string suffix;
2164 this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
2165 return prefix+base+suffix;
2168 //----------------------------------------------------------------------------
2169 void cmTarget::GetFullNameInternal(TargetType type,
2170 const char* config,
2171 bool implib,
2172 std::string& outPrefix,
2173 std::string& outBase,
2174 std::string& outSuffix)
2176 // Use just the target name for non-main target types.
2177 if(type != cmTarget::STATIC_LIBRARY &&
2178 type != cmTarget::SHARED_LIBRARY &&
2179 type != cmTarget::MODULE_LIBRARY &&
2180 type != cmTarget::EXECUTABLE)
2182 outPrefix = "";
2183 outBase = this->GetName();
2184 outSuffix = "";
2185 return;
2188 // Return an empty name for the import library if this platform
2189 // does not support import libraries.
2190 if(implib &&
2191 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2193 outPrefix = "";
2194 outBase = "";
2195 outSuffix = "";
2196 return;
2199 // The implib option is only allowed for shared libraries, module
2200 // libraries, and executables.
2201 if(type != cmTarget::SHARED_LIBRARY &&
2202 type != cmTarget::MODULE_LIBRARY &&
2203 type != cmTarget::EXECUTABLE)
2205 implib = false;
2208 // Compute the full name for main target types.
2209 const char* targetPrefix = (implib
2210 ? this->GetProperty("IMPORT_PREFIX")
2211 : this->GetProperty("PREFIX"));
2212 const char* targetSuffix = (implib
2213 ? this->GetProperty("IMPORT_SUFFIX")
2214 : this->GetProperty("SUFFIX"));
2215 const char* configPostfix = 0;
2216 if(config && *config)
2218 std::string configProp = cmSystemTools::UpperCase(config);
2219 configProp += "_POSTFIX";
2220 configPostfix = this->GetProperty(configProp.c_str());
2222 const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
2223 const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
2224 const char* ll =
2225 this->GetLinkerLanguage(
2226 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2227 // first try language specific suffix
2228 if(ll)
2230 if(!targetSuffix && suffixVar && *suffixVar)
2232 std::string langSuff = suffixVar + std::string("_") + ll;
2233 targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
2235 if(!targetPrefix && prefixVar && *prefixVar)
2237 std::string langPrefix = prefixVar + std::string("_") + ll;
2238 targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
2242 // if there is no prefix on the target use the cmake definition
2243 if(!targetPrefix && prefixVar)
2245 targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
2247 // if there is no suffix on the target use the cmake definition
2248 if(!targetSuffix && suffixVar)
2250 targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
2253 // frameworks do not have a prefix or a suffix
2254 if(this->IsFrameworkOnApple())
2256 targetPrefix = 0;
2257 targetSuffix = 0;
2260 // Begin the final name with the prefix.
2261 outPrefix = targetPrefix?targetPrefix:"";
2263 // Append the target name or property-specified name.
2264 const char* outName = 0;
2265 if(config && *config)
2267 std::string configProp = cmSystemTools::UpperCase(config);
2268 configProp += "_OUTPUT_NAME";
2269 outName = this->GetProperty(configProp.c_str());
2271 if(!outName)
2273 outName = this->GetProperty("OUTPUT_NAME");
2275 if(outName)
2277 outBase = outName;
2279 else
2281 outBase = this->GetName();
2284 // Append the per-configuration postfix.
2285 outBase += configPostfix?configPostfix:"";
2287 // Name shared libraries with their version number on some platforms.
2288 if(const char* version = this->GetProperty("VERSION"))
2290 if(type == cmTarget::SHARED_LIBRARY && !implib &&
2291 this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION"))
2293 outBase += "-";
2294 outBase += version;
2298 // Append the suffix.
2299 outSuffix = targetSuffix?targetSuffix:"";
2302 //----------------------------------------------------------------------------
2303 void cmTarget::GetLibraryNames(std::string& name,
2304 std::string& soName,
2305 std::string& realName,
2306 std::string& impName,
2307 std::string& pdbName,
2308 const char* config)
2310 // Get the names based on the real type of the library.
2311 this->GetLibraryNamesInternal(name, soName, realName, impName, pdbName,
2312 this->GetType(), config);
2315 //----------------------------------------------------------------------------
2316 void cmTarget::GetLibraryCleanNames(std::string& staticName,
2317 std::string& sharedName,
2318 std::string& sharedSOName,
2319 std::string& sharedRealName,
2320 std::string& importName,
2321 std::string& pdbName,
2322 const char* config)
2324 // Get the name as if this were a static library.
2325 std::string soName;
2326 std::string realName;
2327 std::string impName;
2328 this->GetLibraryNamesInternal(staticName, soName, realName, impName,
2329 pdbName, cmTarget::STATIC_LIBRARY, config);
2331 // Get the names as if this were a shared library.
2332 if(this->GetType() == cmTarget::STATIC_LIBRARY)
2334 // Since the real type is static then the user either specified
2335 // STATIC or did not specify a type. In the former case the
2336 // shared library will never be present. In the latter case the
2337 // type will never be MODULE. Either way the only names that
2338 // might have to be cleaned are the shared library names.
2339 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2340 importName, pdbName,
2341 cmTarget::SHARED_LIBRARY, config);
2343 else
2345 // Use the name of the real type of the library (shared or module).
2346 this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
2347 importName, pdbName, this->GetType(),
2348 config);
2352 //----------------------------------------------------------------------------
2353 void cmTarget::GetLibraryNamesInternal(std::string& name,
2354 std::string& soName,
2355 std::string& realName,
2356 std::string& impName,
2357 std::string& pdbName,
2358 TargetType type,
2359 const char* config)
2361 // This should not be called for imported targets.
2362 // TODO: Split cmTarget into a class hierarchy to get compile-time
2363 // enforcement of the limited imported target API.
2364 if(this->IsImported())
2366 abort();
2369 // Construct the name of the soname flag variable for this language.
2370 const char* ll =
2371 this->GetLinkerLanguage(
2372 this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
2373 std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
2374 if(ll)
2376 sonameFlag += "_";
2377 sonameFlag += ll;
2379 sonameFlag += "_FLAG";
2381 // Check for library version properties.
2382 const char* version = this->GetProperty("VERSION");
2383 const char* soversion = this->GetProperty("SOVERSION");
2384 if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
2385 !this->Makefile->GetDefinition(sonameFlag.c_str()))
2387 // Versioning is supported only for shared libraries and modules,
2388 // and then only when the platform supports an soname flag.
2389 version = 0;
2390 soversion = 0;
2392 if(version && !soversion)
2394 // The soversion must be set if the library version is set. Use
2395 // the library version as the soversion.
2396 soversion = version;
2399 // Get the components of the library name.
2400 std::string prefix;
2401 std::string base;
2402 std::string suffix;
2403 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2405 // The library name.
2406 name = prefix+base+suffix;
2408 // The library's soname.
2409 #if defined(__APPLE__)
2410 soName = prefix+base;
2411 #else
2412 soName = name;
2413 #endif
2414 if(soversion)
2416 soName += ".";
2417 soName += soversion;
2419 #if defined(__APPLE__)
2420 soName += suffix;
2421 #endif
2423 // The library's real name on disk.
2424 #if defined(__APPLE__)
2425 realName = prefix+base;
2426 #else
2427 realName = name;
2428 #endif
2429 if(version)
2431 realName += ".";
2432 realName += version;
2434 else if(soversion)
2436 realName += ".";
2437 realName += soversion;
2439 #if defined(__APPLE__)
2440 realName += suffix;
2441 #endif
2443 // The import library name.
2444 if(type == cmTarget::SHARED_LIBRARY ||
2445 type == cmTarget::MODULE_LIBRARY)
2447 impName = this->GetFullNameInternal(type, config, true);
2449 else
2451 impName = "";
2454 // The program database file name.
2455 pdbName = prefix+base+".pdb";
2458 //----------------------------------------------------------------------------
2459 void cmTarget::GetExecutableNames(std::string& name,
2460 std::string& realName,
2461 std::string& impName,
2462 std::string& pdbName,
2463 const char* config)
2465 // Get the names based on the real type of the executable.
2466 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2467 this->GetType(), config);
2470 //----------------------------------------------------------------------------
2471 void cmTarget::GetExecutableCleanNames(std::string& name,
2472 std::string& realName,
2473 std::string& impName,
2474 std::string& pdbName,
2475 const char* config)
2477 // Get the name and versioned name of this executable.
2478 this->GetExecutableNamesInternal(name, realName, impName, pdbName,
2479 cmTarget::EXECUTABLE, config);
2482 //----------------------------------------------------------------------------
2483 void cmTarget::GetExecutableNamesInternal(std::string& name,
2484 std::string& realName,
2485 std::string& impName,
2486 std::string& pdbName,
2487 TargetType type,
2488 const char* config)
2490 // This should not be called for imported targets.
2491 // TODO: Split cmTarget into a class hierarchy to get compile-time
2492 // enforcement of the limited imported target API.
2493 if(this->IsImported())
2495 abort();
2498 // This versioning is supported only for executables and then only
2499 // when the platform supports symbolic links.
2500 #if defined(_WIN32) && !defined(__CYGWIN__)
2501 const char* version = 0;
2502 #else
2503 // Check for executable version properties.
2504 const char* version = this->GetProperty("VERSION");
2505 if(type != cmTarget::EXECUTABLE || this->Makefile->IsOn("XCODE"))
2507 version = 0;
2509 #endif
2511 // Get the components of the executable name.
2512 std::string prefix;
2513 std::string base;
2514 std::string suffix;
2515 this->GetFullNameInternal(type, config, false, prefix, base, suffix);
2517 // The executable name.
2518 name = prefix+base+suffix;
2520 // The executable's real name on disk.
2521 #if defined(__CYGWIN__)
2522 realName = prefix+base;
2523 #else
2524 realName = name;
2525 #endif
2526 if(version)
2528 realName += "-";
2529 realName += version;
2531 #if defined(__CYGWIN__)
2532 realName += suffix;
2533 #endif
2535 // The import library name.
2536 impName = this->GetFullNameInternal(type, config, true);
2538 // The program database file name.
2539 pdbName = prefix+base+".pdb";
2542 //----------------------------------------------------------------------------
2543 void cmTarget::GenerateTargetManifest(const char* config)
2545 cmMakefile* mf = this->Makefile;
2546 cmLocalGenerator* lg = mf->GetLocalGenerator();
2547 cmGlobalGenerator* gg = lg->GetGlobalGenerator();
2549 // Get the names.
2550 std::string name;
2551 std::string soName;
2552 std::string realName;
2553 std::string impName;
2554 std::string pdbName;
2555 if(this->GetType() == cmTarget::EXECUTABLE)
2557 this->GetExecutableNames(name, realName, impName, pdbName, config);
2559 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2560 this->GetType() == cmTarget::SHARED_LIBRARY ||
2561 this->GetType() == cmTarget::MODULE_LIBRARY)
2563 this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
2565 else
2567 return;
2570 // Get the directory.
2571 std::string dir = this->GetDirectory(config, false);
2573 // Add each name.
2574 std::string f;
2575 if(!name.empty())
2577 f = dir;
2578 f += "/";
2579 f += name;
2580 gg->AddToManifest(config? config:"", f);
2582 if(!soName.empty())
2584 f = dir;
2585 f += "/";
2586 f += soName;
2587 gg->AddToManifest(config? config:"", f);
2589 if(!realName.empty())
2591 f = dir;
2592 f += "/";
2593 f += realName;
2594 gg->AddToManifest(config? config:"", f);
2596 if(!pdbName.empty())
2598 f = dir;
2599 f += "/";
2600 f += pdbName;
2601 gg->AddToManifest(config? config:"", f);
2603 if(!impName.empty())
2605 f = this->GetDirectory(config, true);
2606 f += "/";
2607 f += impName;
2608 gg->AddToManifest(config? config:"", f);
2612 //----------------------------------------------------------------------------
2613 void cmTarget::SetPropertyDefault(const char* property,
2614 const char* default_value)
2616 // Compute the name of the variable holding the default value.
2617 std::string var = "CMAKE_";
2618 var += property;
2620 if(const char* value = this->Makefile->GetDefinition(var.c_str()))
2622 this->SetProperty(property, value);
2624 else if(default_value)
2626 this->SetProperty(property, default_value);
2630 //----------------------------------------------------------------------------
2631 bool cmTarget::HaveBuildTreeRPATH()
2633 return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
2634 !this->LinkLibraries.empty());
2637 //----------------------------------------------------------------------------
2638 bool cmTarget::HaveInstallTreeRPATH()
2640 const char* install_rpath = this->GetProperty("INSTALL_RPATH");
2641 return install_rpath && *install_rpath;
2644 //----------------------------------------------------------------------------
2645 bool cmTarget::NeedRelinkBeforeInstall()
2647 // Only executables and shared libraries can have an rpath and may
2648 // need relinking.
2649 if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
2650 this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
2651 this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
2653 return false;
2656 // If there is no install location this target will not be installed
2657 // and therefore does not need relinking.
2658 if(!this->GetHaveInstallRule())
2660 return false;
2663 // If skipping all rpaths completely then no relinking is needed.
2664 if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
2666 return false;
2669 // If building with the install-tree rpath no relinking is needed.
2670 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2672 return false;
2675 // If chrpath is going to be used no relinking is needed.
2676 if(this->IsChrpathUsed())
2678 return false;
2681 // Check for rpath support on this platform.
2682 if(const char* ll = this->GetLinkerLanguage(
2683 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2685 std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2686 flagVar += ll;
2687 flagVar += "_FLAG";
2688 if(!this->Makefile->IsSet(flagVar.c_str()))
2690 // There is no rpath support on this platform so nothing needs
2691 // relinking.
2692 return false;
2695 else
2697 // No linker language is known. This error will be reported by
2698 // other code.
2699 return false;
2702 // If either a build or install tree rpath is set then the rpath
2703 // will likely change between the build tree and install tree and
2704 // this target must be relinked.
2705 return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
2708 //----------------------------------------------------------------------------
2709 std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
2711 // If building directly for installation then the build tree install_name
2712 // is the same as the install tree.
2713 if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
2715 return GetInstallNameDirForInstallTree(config);
2718 // Use the build tree directory for the target.
2719 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2720 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2721 !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
2723 std::string dir = this->GetDirectory(config);
2724 dir += "/";
2725 return dir;
2727 else
2729 return "";
2733 //----------------------------------------------------------------------------
2734 std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
2736 // Lookup the target property.
2737 const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
2738 if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
2739 !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
2740 install_name_dir && *install_name_dir)
2742 std::string dir = install_name_dir;
2743 dir += "/";
2744 return dir;
2746 else
2748 return "";
2752 //----------------------------------------------------------------------------
2753 const char* cmTarget::GetAndCreateOutputDir(bool implib, bool create)
2755 // The implib option is only allowed for shared libraries, module
2756 // libraries, and executables.
2757 if(this->GetType() != cmTarget::SHARED_LIBRARY &&
2758 this->GetType() != cmTarget::MODULE_LIBRARY &&
2759 this->GetType() != cmTarget::EXECUTABLE)
2761 implib = false;
2764 // Sanity check. Only generators on platforms supporting import
2765 // libraries should be asking for the import library output
2766 // directory.
2767 if(implib &&
2768 !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
2770 abort();
2772 if(implib && !this->DLLPlatform)
2774 abort();
2777 // Select whether we are constructing the directory for the main
2778 // target or the import library.
2779 std::string& out = implib? this->OutputDirImplib : this->OutputDir;
2781 if(out.empty())
2783 // Look for a target property defining the target output directory
2784 // based on the target type.
2785 const char* propertyName = 0;
2786 switch(this->GetType())
2788 case cmTarget::SHARED_LIBRARY:
2790 // For non-DLL platforms shared libraries are treated as
2791 // library targets. For DLL platforms the DLL part of a
2792 // shared library is treated as a runtime target and the
2793 // corresponding import library is treated as an archive
2794 // target.
2795 if(this->DLLPlatform)
2797 if(implib)
2799 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2801 else
2803 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2806 else
2808 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2810 } break;
2811 case cmTarget::STATIC_LIBRARY:
2813 // Static libraries are always treated as archive targets.
2814 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2815 } break;
2816 case cmTarget::MODULE_LIBRARY:
2818 // Module libraries are always treated as library targets.
2819 // Module import libraries are treated as archive targets.
2820 if(implib)
2822 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2824 else
2826 propertyName = "LIBRARY_OUTPUT_DIRECTORY";
2828 } break;
2829 case cmTarget::EXECUTABLE:
2831 // Executables are always treated as runtime targets.
2832 // Executable import libraries are treated as archive targets.
2833 if(implib)
2835 propertyName = "ARCHIVE_OUTPUT_DIRECTORY";
2837 else
2839 propertyName = "RUNTIME_OUTPUT_DIRECTORY";
2841 } break;
2842 default: break;
2845 // Select an output directory.
2846 if(const char* outdir = this->GetProperty(propertyName))
2848 // Use the user-specified output directory.
2849 out = outdir;
2851 else if(this->GetType() == cmTarget::EXECUTABLE)
2853 // Lookup the output path for executables.
2854 out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
2856 else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
2857 this->GetType() == cmTarget::SHARED_LIBRARY ||
2858 this->GetType() == cmTarget::MODULE_LIBRARY)
2860 // Lookup the output path for libraries.
2861 out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
2863 if(out.empty())
2865 // Default to the current output directory.
2866 out = ".";
2868 // Convert the output path to a full path in case it is
2869 // specified as a relative path. Treat a relative path as
2870 // relative to the current output directory for this makefile.
2871 out =
2872 cmSystemTools::CollapseFullPath
2873 (out.c_str(), this->Makefile->GetStartOutputDirectory());
2875 if(this->IsFrameworkOnApple())
2877 out += "/";
2878 out += this->GetFullName(0, implib);
2879 out += ".framework";
2882 // Optionally make sure the output path exists on disk.
2883 if(create)
2885 if(!cmSystemTools::MakeDirectory(out.c_str()))
2887 cmSystemTools::Error("Error failed to create output directory: ",
2888 out.c_str());
2893 return out.c_str();
2896 //----------------------------------------------------------------------------
2897 const char* cmTarget::GetOutputDir(bool implib)
2899 return this->GetAndCreateOutputDir(implib, true);
2902 //----------------------------------------------------------------------------
2903 const char* cmTarget::GetExportMacro()
2905 // Define the symbol for targets that export symbols.
2906 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
2907 this->GetType() == cmTarget::MODULE_LIBRARY ||
2908 this->IsExecutableWithExports())
2910 if(const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL"))
2912 this->ExportMacro = custom_export_name;
2914 else
2916 std::string in = this->GetName();
2917 in += "_EXPORTS";
2918 this->ExportMacro = cmSystemTools::MakeCindentifier(in.c_str());
2920 return this->ExportMacro.c_str();
2922 else
2924 return 0;
2928 //----------------------------------------------------------------------------
2929 void cmTarget::GetLanguages(std::set<cmStdString>& languages) const
2931 for(std::vector<cmSourceFile*>::const_iterator
2932 i = this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
2934 if(const char* lang = (*i)->GetLanguage())
2936 languages.insert(lang);
2941 //----------------------------------------------------------------------------
2942 bool cmTarget::IsChrpathUsed()
2944 // Enable use of "chrpath" if it is available, the user has turned
2945 // on the feature, and the rpath flag uses a separator.
2946 if(const char* ll = this->GetLinkerLanguage(
2947 this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
2949 std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
2950 sepVar += ll;
2951 sepVar += "_FLAG_SEP";
2952 const char* sep = this->Makefile->GetDefinition(sepVar.c_str());
2953 if(sep && *sep)
2955 if(this->Makefile->IsSet("CMAKE_CHRPATH") &&
2956 this->Makefile->IsOn("CMAKE_USE_CHRPATH"))
2958 return true;
2962 return false;
2965 //----------------------------------------------------------------------------
2966 cmTarget::ImportInfo const*
2967 cmTarget::GetImportInfo(const char* config)
2969 // There is no imported information for non-imported targets.
2970 if(!this->IsImported())
2972 return 0;
2975 // Lookup/compute/cache the import information for this
2976 // configuration.
2977 std::string config_upper;
2978 if(config && *config)
2980 config_upper = cmSystemTools::UpperCase(config);
2982 else
2984 config_upper = "NOCONFIG";
2986 ImportInfoMapType::const_iterator i =
2987 this->ImportInfoMap.find(config_upper);
2988 if(i == this->ImportInfoMap.end())
2990 ImportInfo info;
2991 this->ComputeImportInfo(config_upper, info);
2992 ImportInfoMapType::value_type entry(config_upper, info);
2993 i = this->ImportInfoMap.insert(entry).first;
2996 // If the location is empty then the target is not available for
2997 // this configuration.
2998 if(i->second.Location.empty())
3000 return 0;
3003 // Return the import information.
3004 return &i->second;
3007 //----------------------------------------------------------------------------
3008 void cmTarget::ComputeImportInfo(std::string const& desired_config,
3009 ImportInfo& info)
3011 // This method finds information about an imported target from its
3012 // properties. The "IMPORTED_" namespace is reserved for properties
3013 // defined by the project exporting the target.
3015 // Track the configuration-specific property suffix.
3016 std::string suffix = "_";
3017 suffix += desired_config;
3019 // Look for a mapping from the current project's configuration to
3020 // the imported project's configuration.
3021 std::vector<std::string> mappedConfigs;
3023 std::string mapProp = "MAP_IMPORTED_CONFIG_";
3024 mapProp += desired_config;
3025 if(const char* mapValue = this->GetProperty(mapProp.c_str()))
3027 cmSystemTools::ExpandListArgument(mapValue, mappedConfigs);
3031 // If a mapping was found, check its configurations.
3032 const char* loc = 0;
3033 for(std::vector<std::string>::const_iterator mci = mappedConfigs.begin();
3034 !loc && mci != mappedConfigs.end(); ++mci)
3036 // Look for this configuration.
3037 std::string mcUpper = cmSystemTools::UpperCase(mci->c_str());
3038 std::string locProp = "IMPORTED_LOCATION_";
3039 locProp += mcUpper;
3040 loc = this->GetProperty(locProp.c_str());
3042 // If it was found, use it for all properties below.
3043 if(loc)
3045 suffix = "_";
3046 suffix += mcUpper;
3050 // If we needed to find one of the mapped configurations but did not
3051 // then the target is not found. The project does not want any
3052 // other configuration.
3053 if(!mappedConfigs.empty() && !loc)
3055 return;
3058 // If we have not yet found it then there are no mapped
3059 // configurations. Look for an exact-match.
3060 if(!loc)
3062 std::string locProp = "IMPORTED_LOCATION";
3063 locProp += suffix;
3064 loc = this->GetProperty(locProp.c_str());
3067 // If we have not yet found it then there are no mapped
3068 // configurations and no exact match.
3069 if(!loc)
3071 // The suffix computed above is not useful.
3072 suffix = "";
3074 // Look for a configuration-less location. This may be set by
3075 // manually-written code.
3076 loc = this->GetProperty("IMPORTED_LOCATION");
3079 // If we have not yet found it then the project is willing to try
3080 // any available configuration.
3081 if(!loc)
3083 std::vector<std::string> availableConfigs;
3084 if(const char* iconfigs = this->GetProperty("IMPORTED_CONFIGURATIONS"))
3086 cmSystemTools::ExpandListArgument(iconfigs, availableConfigs);
3088 for(std::vector<std::string>::const_iterator
3089 aci = availableConfigs.begin();
3090 !loc && aci != availableConfigs.end(); ++aci)
3092 suffix = "_";
3093 suffix += cmSystemTools::UpperCase(availableConfigs[0]);
3094 std::string locProp = "IMPORTED_LOCATION";
3095 locProp += suffix;
3096 loc = this->GetProperty(locProp.c_str());
3100 // If we have not yet found it then the target is not available.
3101 if(!loc)
3103 return;
3106 // A provided configuration has been chosen. Load the
3107 // configuration's properties.
3108 info.Location = loc;
3110 // Get the soname.
3111 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3113 std::string soProp = "IMPORTED_SONAME";
3114 soProp += suffix;
3115 if(const char* config_soname = this->GetProperty(soProp.c_str()))
3117 info.SOName = config_soname;
3119 else if(const char* soname = this->GetProperty("IMPORTED_SONAME"))
3121 info.SOName = soname;
3125 // Get the import library.
3126 if(this->GetType() == cmTarget::SHARED_LIBRARY ||
3127 this->IsExecutableWithExports())
3129 std::string impProp = "IMPORTED_IMPLIB";
3130 impProp += suffix;
3131 if(const char* config_implib = this->GetProperty(impProp.c_str()))
3133 info.ImportLibrary = config_implib;
3135 else if(const char* implib = this->GetProperty("IMPORTED_IMPLIB"))
3137 info.ImportLibrary = implib;
3141 // Get the link interface.
3143 std::string linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
3144 linkProp += suffix;
3145 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3147 cmSystemTools::ExpandListArgument(config_libs,
3148 info.LinkInterface.Libraries);
3150 else if(const char* libs =
3151 this->GetProperty("IMPORTED_LINK_INTERFACE_LIBRARIES"))
3153 cmSystemTools::ExpandListArgument(libs,
3154 info.LinkInterface.Libraries);
3158 // Get the link dependencies.
3160 std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
3161 linkProp += suffix;
3162 if(const char* config_libs = this->GetProperty(linkProp.c_str()))
3164 cmSystemTools::ExpandListArgument(config_libs,
3165 info.LinkInterface.SharedDeps);
3167 else if(const char* libs =
3168 this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES"))
3170 cmSystemTools::ExpandListArgument(libs, info.LinkInterface.SharedDeps);
3175 //----------------------------------------------------------------------------
3176 cmTargetLinkInterface const* cmTarget::GetLinkInterface(const char* config)
3178 // Imported targets have their own link interface.
3179 if(this->IsImported())
3181 if(cmTarget::ImportInfo const* info = this->GetImportInfo(config))
3183 return &info->LinkInterface;
3185 return 0;
3188 // Link interfaces are supported only for shared libraries and
3189 // executables that export symbols.
3190 if((this->GetType() != cmTarget::SHARED_LIBRARY &&
3191 !this->IsExecutableWithExports()))
3193 return 0;
3196 // Lookup any existing link interface for this configuration.
3197 std::map<cmStdString, cmTargetLinkInterface*>::iterator
3198 i = this->LinkInterface.find(config?config:"");
3199 if(i == this->LinkInterface.end())
3201 // Compute the link interface for this configuration.
3202 cmTargetLinkInterface* interface = this->ComputeLinkInterface(config);
3204 // Store the information for this configuration.
3205 std::map<cmStdString, cmTargetLinkInterface*>::value_type
3206 entry(config?config:"", interface);
3207 i = this->LinkInterface.insert(entry).first;
3210 return i->second;
3213 //----------------------------------------------------------------------------
3214 cmTargetLinkInterface* cmTarget::ComputeLinkInterface(const char* config)
3216 // Construct the property name suffix for this configuration.
3217 std::string suffix = "_";
3218 if(config && *config)
3220 suffix += cmSystemTools::UpperCase(config);
3222 else
3224 suffix += "NOCONFIG";
3227 // Lookup the link interface libraries.
3228 const char* libs = 0;
3230 // Lookup the per-configuration property.
3231 std::string propName = "LINK_INTERFACE_LIBRARIES";
3232 propName += suffix;
3233 libs = this->GetProperty(propName.c_str());
3235 // If not set, try the generic property.
3236 if(!libs)
3238 libs = this->GetProperty("LINK_INTERFACE_LIBRARIES");
3242 // If still not set, there is no link interface.
3243 if(!libs)
3245 return 0;
3248 // Allocate the interface.
3249 cmTargetLinkInterface* interface = new cmTargetLinkInterface;
3250 if(!interface)
3252 return 0;
3255 // Expand the list of libraries in the interface.
3256 cmSystemTools::ExpandListArgument(libs, interface->Libraries);
3258 // Now we need to construct a list of shared library dependencies
3259 // not included in the interface.
3260 if(this->GetType() == cmTarget::SHARED_LIBRARY)
3262 // Use a set to keep track of what libraries have been emitted to
3263 // either list.
3264 std::set<cmStdString> emitted;
3265 for(std::vector<std::string>::const_iterator
3266 li = interface->Libraries.begin();
3267 li != interface->Libraries.end(); ++li)
3269 emitted.insert(*li);
3272 // Compute which library configuration to link.
3273 cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
3274 if(config && cmSystemTools::UpperCase(config) == "DEBUG")
3276 linkType = cmTarget::DEBUG;
3279 // Construct the list of libs linked for this configuration.
3280 cmTarget::LinkLibraryVectorType const& llibs =
3281 this->GetOriginalLinkLibraries();
3282 for(cmTarget::LinkLibraryVectorType::const_iterator li = llibs.begin();
3283 li != llibs.end(); ++li)
3285 // Skip entries that will resolve to the target itself, are empty,
3286 // or are not meant for this configuration.
3287 if(li->first == this->GetName() || li->first.empty() ||
3288 !(li->second == cmTarget::GENERAL || li->second == linkType))
3290 continue;
3293 // Skip entries that have already been emitted into either list.
3294 if(!emitted.insert(li->first).second)
3296 continue;
3299 // Add this entry if it is a shared library.
3300 if(cmTarget* tgt = this->Makefile->FindTargetToUse(li->first.c_str()))
3302 if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
3304 interface->SharedDeps.push_back(li->first);
3307 else
3309 // TODO: Recognize shared library file names. Perhaps this
3310 // should be moved to cmComputeLinkInformation, but that creates
3311 // a chicken-and-egg problem since this list is needed for its
3312 // construction.
3317 // Return the completed interface.
3318 return interface;
3321 //----------------------------------------------------------------------------
3322 cmComputeLinkInformation*
3323 cmTarget::GetLinkInformation(const char* config)
3325 // Link information does not make sense for static libraries.
3326 assert(this->GetType() != cmTarget::STATIC_LIBRARY);
3328 // Lookup any existing information for this configuration.
3329 std::map<cmStdString, cmComputeLinkInformation*>::iterator
3330 i = this->LinkInformation.find(config?config:"");
3331 if(i == this->LinkInformation.end())
3333 // Compute information for this configuration.
3334 cmComputeLinkInformation* info =
3335 new cmComputeLinkInformation(this, config);
3336 if(!info || !info->Compute())
3338 delete info;
3339 info = 0;
3342 // Store the information for this configuration.
3343 std::map<cmStdString, cmComputeLinkInformation*>::value_type
3344 entry(config?config:"", info);
3345 i = this->LinkInformation.insert(entry).first;
3347 return i->second;
3350 //----------------------------------------------------------------------------
3351 cmTargetLinkInformationMap
3352 ::cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r): derived()
3354 // Ideally cmTarget instances should never be copied. However until
3355 // we can make a sweep to remove that, this copy constructor avoids
3356 // allowing the resources (LinkInformation) from getting copied. In
3357 // the worst case this will lead to extra cmComputeLinkInformation
3358 // instances. We also enforce in debug mode that the map be emptied
3359 // when copied.
3360 static_cast<void>(r);
3361 assert(r.empty());
3364 //----------------------------------------------------------------------------
3365 cmTargetLinkInformationMap::~cmTargetLinkInformationMap()
3367 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3369 delete i->second;
3373 //----------------------------------------------------------------------------
3374 cmTargetLinkInterfaceMap
3375 ::cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap const& r): derived()
3377 // Ideally cmTarget instances should never be copied. However until
3378 // we can make a sweep to remove that, this copy constructor avoids
3379 // allowing the resources (LinkInterface) from getting copied. In
3380 // the worst case this will lead to extra cmTargetLinkInterface
3381 // instances. We also enforce in debug mode that the map be emptied
3382 // when copied.
3383 static_cast<void>(r);
3384 assert(r.empty());
3387 //----------------------------------------------------------------------------
3388 cmTargetLinkInterfaceMap::~cmTargetLinkInterfaceMap()
3390 for(derived::iterator i = this->begin(); i != this->end(); ++i)
3392 delete i->second;