cmake/Source/cmCommonTargetGenerator.cxx

316 lines
11 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-11-17 17:22:37 +01:00
#include "cmCommonTargetGenerator.h"
2016-10-30 18:24:19 +01:00
#include <set>
#include <sstream>
#include <utility>
2015-11-17 17:22:37 +01:00
#include "cmComputeLinkInformation.h"
#include "cmGeneratorTarget.h"
#include "cmGlobalCommonGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmLinkLineComputer.h"
2015-11-17 17:22:37 +01:00
#include "cmLocalCommonGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmLocalGenerator.h"
2015-11-17 17:22:37 +01:00
#include "cmMakefile.h"
2017-04-14 19:02:05 +02:00
#include "cmOutputConverter.h"
2020-08-30 11:54:41 +02:00
#include "cmProperty.h"
2021-09-14 00:13:48 +02:00
#include "cmRange.h"
2015-11-17 17:22:37 +01:00
#include "cmSourceFile.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2020-08-30 11:54:41 +02:00
#include "cmTarget.h"
2015-11-17 17:22:37 +01:00
2016-10-30 18:24:19 +01:00
cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
: GeneratorTarget(gt)
2015-11-17 17:22:37 +01:00
, Makefile(gt->Makefile)
2018-01-26 17:06:56 +01:00
, LocalCommonGenerator(
static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
, GlobalCommonGenerator(static_cast<cmGlobalCommonGenerator*>(
2016-07-09 11:21:54 +02:00
gt->LocalGenerator->GetGlobalGenerator()))
2021-09-14 00:13:48 +02:00
, ConfigNames(this->LocalCommonGenerator->GetConfigNames())
2015-11-17 17:22:37 +01:00
{
}
2019-11-11 23:01:05 +01:00
cmCommonTargetGenerator::~cmCommonTargetGenerator() = default;
2015-11-17 17:22:37 +01:00
2020-08-30 11:54:41 +02:00
std::vector<std::string> const& cmCommonTargetGenerator::GetConfigNames() const
2015-11-17 17:22:37 +01:00
{
2020-08-30 11:54:41 +02:00
return this->ConfigNames;
2015-11-17 17:22:37 +01:00
}
2020-08-30 11:54:41 +02:00
const char* cmCommonTargetGenerator::GetFeature(const std::string& feature,
const std::string& config)
2015-11-17 17:22:37 +01:00
{
2021-09-14 00:13:48 +02:00
return this->GeneratorTarget->GetFeature(feature, config)->c_str();
2015-11-17 17:22:37 +01:00
}
2017-04-14 19:02:05 +02:00
void cmCommonTargetGenerator::AddModuleDefinitionFlag(
2020-08-30 11:54:41 +02:00
cmLinkLineComputer* linkLineComputer, std::string& flags,
const std::string& config)
2015-11-17 17:22:37 +01:00
{
2017-07-20 19:35:53 +02:00
cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetModuleDefinitionInfo(config);
2017-07-20 19:35:53 +02:00
if (!mdi || mdi->DefFile.empty()) {
2015-11-17 17:22:37 +01:00
return;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// TODO: Create a per-language flag variable.
2021-09-14 00:13:48 +02:00
cmProp defFileFlag =
2015-11-17 17:22:37 +01:00
this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
2016-07-09 11:21:54 +02:00
if (!defFileFlag) {
2015-11-17 17:22:37 +01:00
return;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Append the flag and value. Use ConvertToLinkReference to help
// vs6's "cl -link" pass it to the linker.
2020-02-01 23:06:01 +01:00
std::string flag =
2021-09-14 00:13:48 +02:00
cmStrCat(*defFileFlag,
2020-02-01 23:06:01 +01:00
this->LocalCommonGenerator->ConvertToOutputFormat(
linkLineComputer->ConvertToLinkReference(mdi->DefFile),
cmOutputConverter::SHELL));
2018-01-26 17:06:56 +01:00
this->LocalCommonGenerator->AppendFlags(flags, flag);
2015-11-17 17:22:37 +01:00
}
2016-07-09 11:21:54 +02:00
void cmCommonTargetGenerator::AppendFortranFormatFlags(
std::string& flags, cmSourceFile const& source)
2015-11-17 17:22:37 +01:00
{
2020-08-30 11:54:41 +02:00
const std::string srcfmt = source.GetSafeProperty("Fortran_FORMAT");
2016-07-09 11:21:54 +02:00
cmOutputConverter::FortranFormat format =
2016-10-30 18:24:19 +01:00
cmOutputConverter::GetFortranFormat(srcfmt);
2016-07-09 11:21:54 +02:00
if (format == cmOutputConverter::FortranFormatNone) {
2020-08-30 11:54:41 +02:00
std::string const& tgtfmt =
this->GeneratorTarget->GetSafeProperty("Fortran_FORMAT");
2016-10-30 18:24:19 +01:00
format = cmOutputConverter::GetFortranFormat(tgtfmt);
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
const char* var = nullptr;
2016-07-09 11:21:54 +02:00
switch (format) {
case cmOutputConverter::FortranFormatFixed:
var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
break;
case cmOutputConverter::FortranFormatFree:
var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
break;
default:
break;
}
if (var) {
2018-01-26 17:06:56 +01:00
this->LocalCommonGenerator->AppendFlags(
2020-02-01 23:06:01 +01:00
flags, this->Makefile->GetSafeDefinition(var));
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
}
2020-08-30 11:54:41 +02:00
void cmCommonTargetGenerator::AppendFortranPreprocessFlags(
std::string& flags, cmSourceFile const& source)
{
const std::string srcpp = source.GetSafeProperty("Fortran_PREPROCESS");
cmOutputConverter::FortranPreprocess preprocess =
cmOutputConverter::GetFortranPreprocess(srcpp);
if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
std::string const& tgtpp =
this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
}
const char* var = nullptr;
switch (preprocess) {
case cmOutputConverter::FortranPreprocess::Needed:
var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON";
break;
case cmOutputConverter::FortranPreprocess::NotNeeded:
var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF";
break;
default:
break;
}
if (var) {
this->LocalCommonGenerator->AppendCompileOptions(
flags, this->Makefile->GetSafeDefinition(var));
}
}
std::string cmCommonTargetGenerator::GetFlags(const std::string& l,
const std::string& config,
const std::string& arch)
2015-11-17 17:22:37 +01:00
{
2020-08-30 11:54:41 +02:00
const std::string key = config + arch;
auto i = this->Configs[key].FlagsByLanguage.find(l);
if (i == this->Configs[key].FlagsByLanguage.end()) {
2015-11-17 17:22:37 +01:00
std::string flags;
2020-08-30 11:54:41 +02:00
this->LocalCommonGenerator->GetTargetCompileFlags(this->GeneratorTarget,
config, l, flags, arch);
2015-11-17 17:22:37 +01:00
ByLanguageMap::value_type entry(l, flags);
2020-08-30 11:54:41 +02:00
i = this->Configs[key].FlagsByLanguage.insert(entry).first;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
return i->second;
}
2020-08-30 11:54:41 +02:00
std::string cmCommonTargetGenerator::GetDefines(const std::string& l,
const std::string& config)
2015-11-17 17:22:37 +01:00
{
2020-08-30 11:54:41 +02:00
auto i = this->Configs[config].DefinesByLanguage.find(l);
if (i == this->Configs[config].DefinesByLanguage.end()) {
2015-11-17 17:22:37 +01:00
std::set<std::string> defines;
2020-08-30 11:54:41 +02:00
this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget, config,
l, defines);
2015-11-17 17:22:37 +01:00
std::string definesString;
2018-01-26 17:06:56 +01:00
this->LocalCommonGenerator->JoinDefines(defines, definesString, l);
2015-11-17 17:22:37 +01:00
ByLanguageMap::value_type entry(l, definesString);
2020-08-30 11:54:41 +02:00
i = this->Configs[config].DefinesByLanguage.insert(entry).first;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
return i->second;
}
2020-08-30 11:54:41 +02:00
std::string cmCommonTargetGenerator::GetIncludes(std::string const& l,
const std::string& config)
2015-11-17 17:22:37 +01:00
{
2020-08-30 11:54:41 +02:00
auto i = this->Configs[config].IncludesByLanguage.find(l);
if (i == this->Configs[config].IncludesByLanguage.end()) {
2015-11-17 17:22:37 +01:00
std::string includes;
2020-08-30 11:54:41 +02:00
this->AddIncludeFlags(includes, l, config);
2015-11-17 17:22:37 +01:00
ByLanguageMap::value_type entry(l, includes);
2020-08-30 11:54:41 +02:00
i = this->Configs[config].IncludesByLanguage.insert(entry).first;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
return i->second;
}
2020-08-30 11:54:41 +02:00
std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories(
const std::string& config) const
2015-11-17 17:22:37 +01:00
{
std::vector<std::string> dirs;
2016-03-13 13:35:51 +01:00
std::set<cmGeneratorTarget const*> emitted;
2015-11-17 17:22:37 +01:00
if (cmComputeLinkInformation* cli =
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetLinkInformation(config)) {
2015-11-17 17:22:37 +01:00
cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
2018-01-26 17:06:56 +01:00
for (auto const& item : items) {
cmGeneratorTarget const* linkee = item.Target;
2018-08-09 18:06:22 +02:00
if (linkee &&
!linkee->IsImported()
2016-07-09 11:21:54 +02:00
// We can ignore the INTERFACE_LIBRARY items because
// Target->GetLinkInformation already processed their
// link interface and they don't have any output themselves.
2017-04-14 19:02:05 +02:00
&& linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
2016-07-09 11:21:54 +02:00
emitted.insert(linkee).second) {
2016-03-13 13:35:51 +01:00
cmLocalGenerator* lg = linkee->GetLocalGenerator();
2020-02-01 23:06:01 +01:00
std::string di = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
lg->GetTargetDirectory(linkee));
2018-04-23 21:13:27 +02:00
dirs.push_back(std::move(di));
2015-11-17 17:22:37 +01:00
}
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
return dirs;
}
2020-08-30 11:54:41 +02:00
std::string cmCommonTargetGenerator::ComputeTargetCompilePDB(
const std::string& config) const
2017-04-14 19:02:05 +02:00
{
std::string compilePdbPath;
if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
return compilePdbPath;
}
2020-02-01 23:06:01 +01:00
2020-08-30 11:54:41 +02:00
compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config);
2017-04-14 19:02:05 +02:00
if (compilePdbPath.empty()) {
// Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
// A trailing slash tells the toolchain to add its default file name.
2020-08-30 11:54:41 +02:00
compilePdbPath = this->GeneratorTarget->GetSupportDirectory();
if (this->GlobalCommonGenerator->IsMultiConfig()) {
compilePdbPath += "/";
compilePdbPath += config;
}
compilePdbPath += "/";
2017-04-14 19:02:05 +02:00
if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
// Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
compilePdbPath += this->GeneratorTarget->GetName();
compilePdbPath += ".pdb";
}
}
return compilePdbPath;
}
2020-08-30 11:54:41 +02:00
std::string cmCommonTargetGenerator::GetManifests(const std::string& config)
2015-11-17 17:22:37 +01:00
{
std::vector<cmSourceFile const*> manifest_srcs;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetManifests(manifest_srcs, config);
2015-11-17 17:22:37 +01:00
std::vector<std::string> manifests;
2018-01-26 17:06:56 +01:00
manifests.reserve(manifest_srcs.size());
for (cmSourceFile const* manifest_src : manifest_srcs) {
manifests.push_back(this->LocalCommonGenerator->ConvertToOutputFormat(
2021-09-14 00:13:48 +02:00
this->LocalCommonGenerator->MaybeRelativeToWorkDir(
2018-01-26 17:06:56 +01:00
manifest_src->GetFullPath()),
2016-10-30 18:24:19 +01:00
cmOutputConverter::SHELL));
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
return cmJoin(manifests, " ");
}
2016-07-09 11:21:54 +02:00
2020-08-30 11:54:41 +02:00
std::string cmCommonTargetGenerator::GetAIXExports(std::string const&)
{
std::string aixExports;
if (this->GeneratorTarget->Target->IsAIX()) {
if (cmProp exportAll =
this->GeneratorTarget->GetProperty("AIX_EXPORT_ALL_SYMBOLS")) {
if (cmIsOff(*exportAll)) {
aixExports = "-n";
}
}
}
return aixExports;
}
2016-07-09 11:21:54 +02:00
void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
const std::string& lang,
const char* name, bool so)
{
// Lookup the flag to specify the version.
2020-02-01 23:06:01 +01:00
std::string fvar = cmStrCat("CMAKE_", lang, "_OSX_", name, "_VERSION_FLAG");
2021-09-14 00:13:48 +02:00
cmProp flag = this->Makefile->GetDefinition(fvar);
2016-07-09 11:21:54 +02:00
// Skip if no such flag.
if (!flag) {
return;
}
// Lookup the target version information.
int major;
int minor;
int patch;
2020-08-30 11:54:41 +02:00
std::string prop = cmStrCat("MACHO_", name, "_VERSION");
std::string fallback_prop = so ? "SOVERSION" : "VERSION";
this->GeneratorTarget->GetTargetVersionFallback(prop, fallback_prop, major,
minor, patch);
2016-07-09 11:21:54 +02:00
if (major > 0 || minor > 0 || patch > 0) {
// Append the flag since a non-zero version is specified.
std::ostringstream vflag;
2021-09-14 00:13:48 +02:00
vflag << *flag << major << "." << minor << "." << patch;
2018-01-26 17:06:56 +01:00
this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
2016-07-09 11:21:54 +02:00
}
}
2021-09-14 00:13:48 +02:00
std::string cmCommonTargetGenerator::GetLinkerLauncher(
const std::string& config)
{
std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
cmProp launcherProp =
this->GeneratorTarget->GetProperty(lang + "_LINKER_LAUNCHER");
if (cmNonempty(launcherProp)) {
// Convert ;-delimited list to single string
std::vector<std::string> args = cmExpandedList(*launcherProp, true);
if (!args.empty()) {
args[0] = this->LocalCommonGenerator->ConvertToOutputFormat(
args[0], cmOutputConverter::SHELL);
for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
i = this->LocalCommonGenerator->EscapeForShell(i);
}
return cmJoin(args, " ");
}
}
return std::string();
}