1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmInstallTargetGenerator.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmake.h"
// TODO:
// - Consolidate component/configuration checks across multiple
// install generators
// - Skip IF(EXISTS) checks if nothing is done with the installed file
//----------------------------------------------------------------------------
cmInstallTargetGenerator
::cmInstallTargetGenerator(cmTarget& t, const char* dest, bool implib,
const char* file_permissions,
std::vector<std::string> const& configurations,
const char* component, bool optional):
cmInstallGenerator(dest), Target(&t), ImportLibrary(implib),
FilePermissions(file_permissions), Configurations(configurations),
Component(component), Optional(optional)
{
this->Target->SetHaveInstallRule(true);
}
//----------------------------------------------------------------------------
cmInstallTargetGenerator
::~cmInstallTargetGenerator()
{
}
//----------------------------------------------------------------------------
void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
{
// Track indentation.
Indent indent;
// Begin this block of installation.
std::string component_test = "NOT CMAKE_INSTALL_COMPONENT OR "
"\"${CMAKE_INSTALL_COMPONENT}\" MATCHES \"^(";
component_test += this->Component;
component_test += ")$\"";
os << indent << "IF(" << component_test << ")\n";
// Compute the build tree directory from which to copy the target.
std::string fromDir;
if(this->Target->NeedRelinkBeforeInstall())
{
fromDir = this->Target->GetMakefile()->GetStartOutputDirectory();
fromDir += cmake::GetCMakeFilesDirectory();
fromDir += "/CMakeRelink.dir/";
}
else
{
fromDir = this->Target->GetDirectory(0, this->ImportLibrary);
fromDir += "/";
}
// Generate a portion of the script for each configuration.
if(this->ConfigurationTypes->empty())
{
this->GenerateScriptForConfig(os, fromDir.c_str(),
this->ConfigurationName,
indent.Next());
}
else
{
for(std::vector<std::string>::const_iterator i =
this->ConfigurationTypes->begin();
i != this->ConfigurationTypes->end(); ++i)
{
this->GenerateScriptForConfig(os, fromDir.c_str(), i->c_str(),
indent.Next());
}
}
// End this block of installation.
os << indent << "ENDIF(" << component_test << ")\n";
}
//----------------------------------------------------------------------------
static std::string cmInstallTargetGeneratorEncodeConfig(const char* config)
{
std::string result;
for(const char* c = config; *c; ++c)
{
if(*c >= 'a' && *c <= 'z')
{
result += "[";
result += *c + ('A' - 'a');
result += *c;
result += "]";
}
else if(*c >= 'A' && *c <= 'Z')
{
result += "[";
result += *c;
result += *c + ('a' - 'A');
result += "]";
}
else
{
result += *c;
}
}
return result;
}
//----------------------------------------------------------------------------
void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
const char* fromDir,
const char* config,
Indent const& indent)
{
// Compute the per-configuration directory containing the files.
std::string fromDirConfig = fromDir;
this->Target->GetMakefile()->GetLocalGenerator()->GetGlobalGenerator()
->AppendDirectoryForConfig("", config, "/", fromDirConfig);
std::string config_test;
if(config && *config)
{
std::string config_upper = cmSystemTools::UpperCase(config);
// Skip this configuration for config-specific installation that
// does not match it.
if(!this->Configurations.empty())
{
bool found = false;
for(std::vector<std::string>::const_iterator i =
this->Configurations.begin();
!found && i != this->Configurations.end(); ++i)
{
found = found || (cmSystemTools::UpperCase(*i) == config_upper);
}
if(!found)
{
return;
}
}
// Generate a per-configuration block.
config_test = "\"${CMAKE_INSTALL_CONFIG_NAME}\" MATCHES \"^(";
config_test += cmInstallTargetGeneratorEncodeConfig(config);
config_test += ")$\"";
os << indent << "IF(" << config_test << ")\n";
this->GenerateScriptForConfigDir(os, fromDirConfig.c_str(), config,
indent.Next());
os << indent << "ENDIF(" << config_test << ")\n";
}
else
{
this->GenerateScriptForConfigDir(os, fromDirConfig.c_str(), config,
indent);
}
}
//----------------------------------------------------------------------------
void
cmInstallTargetGenerator
::GenerateScriptForConfigDir(std::ostream& os,
const char* fromDirConfig,
const char* config,
Indent const& indent)
{
// Compute the full path to the main installed file for this target.
std::string toFullPath = "$ENV{DESTDIR}";
toFullPath += this->Destination;
toFullPath += "/";
toFullPath += this->GetInstallFilename(this->Target, config,
this->ImportLibrary, false);
// Compute the list of files to install for this target.
std::vector<std::string> files;
std::string literal_args;
cmTarget::TargetType type = this->Target->GetType();
if(type == cmTarget::EXECUTABLE)
{
std::string targetName;
std::string targetNameReal;
std::string targetNameImport;
std::string targetNamePDB;
this->Target->GetExecutableNames(targetName, targetNameReal,
targetNameImport, targetNamePDB,
config);
if(this->ImportLibrary)
{
std::string from1 = fromDirConfig;
from1 += targetNameImport;
files.push_back(from1);
// An import library looks like a static library.
type = cmTarget::STATIC_LIBRARY;
}
else
{
std::string from1 = fromDirConfig;
from1 += targetName;
// Handle OSX Bundles.
if(this->Target->GetMakefile()->IsOn("APPLE") &&
this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
{
// Compute the source locations of the bundle executable and
// Info.plist file.
from1 += ".app";
files.push_back(from1);
type = cmTarget::INSTALL_DIRECTORY;
toFullPath += ".app/Contents/MacOS/";
toFullPath += this->GetInstallFilename(this->Target, config,
this->ImportLibrary, false);
literal_args += " USE_SOURCE_PERMISSIONS";
// TODO: Still need to apply install_name_tool and stripping
// to binaries inside bundle.
}
else
{
files.push_back(from1);
if(targetNameReal != targetName)
{
std::string from2 = fromDirConfig;
from2 += targetNameReal;
files.push_back(from2);
}
}
}
}
else
{
std::string targetName;
std::string targetNameSO;
std::string targetNameReal;
std::string targetNameImport;
std::string targetNamePDB;
this->Target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
targetNameImport, targetNamePDB,
config);
if(this->ImportLibrary)
{
std::string from1 = fromDirConfig;
from1 += targetNameImport;
files.push_back(from1);
// An import library looks like a static library.
type = cmTarget::STATIC_LIBRARY;
}
else
{
std::string from1 = fromDirConfig;
from1 += targetName;
files.push_back(from1);
if(targetNameSO != targetName)
{
std::string from2 = fromDirConfig;
from2 += targetNameSO;
files.push_back(from2);
}
if(targetNameReal != targetName &&
targetNameReal != targetNameSO)
{
std::string from3 = fromDirConfig;
from3 += targetNameReal;
files.push_back(from3);
}
}
}
// Write code to install the target file.
const char* no_dir_permissions = 0;
const char* no_rename = 0;
const char* no_properties = 0;
const char* no_component = 0;
std::vector<std::string> no_configurations;
bool optional = this->Optional || this->ImportLibrary;
this->AddInstallRule(os, this->Destination.c_str(), type, files,
optional, no_properties,
this->FilePermissions.c_str(), no_dir_permissions,
no_configurations, no_component,
no_rename, literal_args.c_str(),
indent);
os << indent << "IF(EXISTS \"" << toFullPath << "\")\n";
this->AddInstallNamePatchRule(os, indent.Next(), config, toFullPath);
this->AddRanlibRule(os, indent.Next(), type, toFullPath);
this->AddStripRule(os, indent.Next(), type, toFullPath);
os << indent << "ENDIF(EXISTS \"" << toFullPath << "\")\n";
}
//----------------------------------------------------------------------------
std::string
cmInstallTargetGenerator::GetInstallFilename(const char* config) const
{
return
cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
this->ImportLibrary, false);
}
//----------------------------------------------------------------------------
std::string cmInstallTargetGenerator::GetInstallFilename(cmTarget* target,
const char* config,
bool implib,
bool useSOName)
{
std::string fname;
// Compute the name of the library.
if(target->GetType() == cmTarget::EXECUTABLE)
{
std::string targetName;
std::string targetNameReal;
std::string targetNameImport;
std::string targetNamePDB;
target->GetExecutableNames(targetName, targetNameReal,
targetNameImport, targetNamePDB,
config);
if(implib)
{
// Use the import library name.
fname = targetNameImport;
}
else
{
// Use the canonical name.
fname = targetName;
}
}
else
{
std::string targetName;
std::string targetNameSO;
std::string targetNameReal;
std::string targetNameImport;
std::string targetNamePDB;
target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
targetNameImport, targetNamePDB, config);
if(implib)
{
// Use the import library name.
fname = targetNameImport;
}
else if(useSOName)
{
// Use the soname.
fname = targetNameSO;
}
else
{
// Use the canonical name.
fname = targetName;
}
}
return fname;
}
//----------------------------------------------------------------------------
void
cmInstallTargetGenerator
::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
const char* config, std::string const& toFullPath)
{
if(this->ImportLibrary ||
!(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
this->Target->GetType() == cmTarget::EXECUTABLE))
{
return;
}
// Fix the install_name settings in installed binaries.
std::string installNameTool =
this->Target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
if(!installNameTool.size())
{
return;
}
// Build a map of build-tree install_name to install-tree install_name for
// shared libraries linked to this target.
std::map<cmStdString, cmStdString> install_name_remap;
cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
if(config && cmSystemTools::UpperCase(config) == "DEBUG")
{
linkType = cmTarget::DEBUG;
}
// TODO: Merge with ComputeLinkInformation.
const cmTarget::LinkLibraryVectorType& inLibs =
this->Target->GetLinkLibraries();
for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
j != inLibs.end(); ++j)
{
std::string lib = j->first;
if((this->Target->GetType() == cmTarget::EXECUTABLE ||
lib != this->Target->GetName()) &&
(j->second == cmTarget::GENERAL || j->second == linkType))
{
if(cmTarget* tgt = this->Target->GetMakefile()->
GetLocalGenerator()->GetGlobalGenerator()->
FindTarget(0, lib.c_str(), false))
{
if(tgt->GetType() == cmTarget::SHARED_LIBRARY)
{
// If the build tree and install tree use different path
// components of the install_name field then we need to create a
// mapping to be applied after installation.
std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
std::string for_install =
tgt->GetInstallNameDirForInstallTree(config);
if(for_build != for_install)
{
std::string fname =
this->GetInstallFilename(tgt, config, false, true);
// Map from the build-tree install_name.
for_build += fname;
// Map to the install-tree install_name.
for_install += fname;
// Store the mapping entry.
install_name_remap[for_build] = for_install;
}
}
}
}
}
// Edit the install_name of the target itself if necessary.
std::string new_id;
if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
{
std::string for_build =
this->Target->GetInstallNameDirForBuildTree(config);
std::string for_install =
this->Target->GetInstallNameDirForInstallTree(config);
if(for_build != for_install)
{
// Prepare to refer to the install-tree install_name.
new_id = for_install;
new_id += this->GetInstallFilename(this->Target, config,
this->ImportLibrary, true);
}
}
// Write a rule to run install_name_tool to set the install-tree
// install_name value and references.
if(!new_id.empty() || !install_name_remap.empty())
{
os << indent << "EXECUTE_PROCESS(COMMAND \"" << installNameTool;
os << "\"";
if(!new_id.empty())
{
os << "\n" << indent << " -id \"" << new_id << "\"";
}
for(std::map<cmStdString, cmStdString>::const_iterator
i = install_name_remap.begin();
i != install_name_remap.end(); ++i)
{
os << "\n" << indent << " -change \""
<< i->first << "\" \"" << i->second << "\"";
}
os << "\n" << indent << " \"" << toFullPath << "\")\n";
}
}
//----------------------------------------------------------------------------
void
cmInstallTargetGenerator::AddStripRule(std::ostream& os,
Indent const& indent,
cmTarget::TargetType type,
const std::string& toFullPath)
{
// don't strip static libraries, because it removes the only symbol table
// they have so you can't link to them anymore
if(type == cmTarget::STATIC_LIBRARY)
{
return;
}
// Don't handle OSX Bundles.
if(this->Target->GetMakefile()->IsOn("APPLE") &&
this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
{
return;
}
if(! this->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
{
return;
}
os << indent << "IF(CMAKE_INSTALL_DO_STRIP)\n";
os << indent << " EXECUTE_PROCESS(COMMAND \""
<< this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
<< "\" \"" << toFullPath << "\")\n";
os << indent << "ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
}
//----------------------------------------------------------------------------
void
cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
Indent const& indent,
cmTarget::TargetType type,
const std::string& toFullPath)
{
// Static libraries need ranlib on this platform.
if(type != cmTarget::STATIC_LIBRARY)
{
return;
}
// Perform post-installation processing on the file depending
// on its type.
if(!this->Target->GetMakefile()->IsOn("APPLE"))
{
return;
}
std::string ranlib =
this->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
if(ranlib.empty())
{
return;
}
os << indent << "EXECUTE_PROCESS(COMMAND \""
<< ranlib << "\" \"" << toFullPath << "\")\n";
}
|