cmake/Source/cmLinkLineDeviceComputer.cxx

124 lines
3.5 KiB
C++
Raw Normal View History

2017-04-14 19:02:05 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmLinkLineDeviceComputer.h"
2017-07-20 19:35:53 +02:00
2018-10-28 12:09:07 +01:00
#include <set>
2017-07-20 19:35:53 +02:00
#include <sstream>
2018-10-28 12:09:07 +01:00
#include <utility>
2017-07-20 19:35:53 +02:00
2018-08-09 18:06:22 +02:00
#include "cmAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmComputeLinkInformation.h"
#include "cmGeneratorTarget.h"
#include "cmGlobalNinjaGenerator.h"
2017-07-20 19:35:53 +02:00
#include "cmStateTypes.h"
class cmOutputConverter;
2017-04-14 19:02:05 +02:00
cmLinkLineDeviceComputer::cmLinkLineDeviceComputer(
2017-07-20 19:35:53 +02:00
cmOutputConverter* outputConverter, cmStateDirectory const& stateDir)
2017-04-14 19:02:05 +02:00
: cmLinkLineComputer(outputConverter, stateDir)
{
}
cmLinkLineDeviceComputer::~cmLinkLineDeviceComputer()
{
}
2018-10-28 12:09:07 +01:00
static bool cmLinkItemValidForDevice(std::string const& item)
{
// Valid items are:
// * Non-flags (does not start in '-')
// * Specific flags --library, --library-path, -l, -L
// For example:
// * 'cublas_device' => pass-along
// * '--library pthread' => pass-along
// * '-lpthread' => pass-along
// * '-pthread' => drop
// * '-a' => drop
return (!cmHasLiteralPrefix(item, "-") || //
cmHasLiteralPrefix(item, "-l") || //
cmHasLiteralPrefix(item, "-L") || //
cmHasLiteralPrefix(item, "--library"));
}
2017-04-14 19:02:05 +02:00
std::string cmLinkLineDeviceComputer::ComputeLinkLibraries(
cmComputeLinkInformation& cli, std::string const& stdLibString)
{
// Write the library flags to the build rule.
std::ostringstream fout;
2018-10-28 12:09:07 +01:00
// Generate the unique set of link items when device linking.
// The nvcc device linker is designed so that each static library
// with device symbols only needs to be listed once as it doesn't
// care about link order.
std::set<std::string> emitted;
2017-04-14 19:02:05 +02:00
typedef cmComputeLinkInformation::ItemVector ItemVector;
ItemVector const& items = cli.GetItems();
std::string config = cli.GetConfig();
2018-01-26 17:06:56 +01:00
for (auto const& item : items) {
2018-08-09 18:06:22 +02:00
if (item.Target) {
bool skip = false;
switch (item.Target->GetType()) {
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::INTERFACE_LIBRARY:
skip = true;
break;
case cmStateEnums::STATIC_LIBRARY:
skip = item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
break;
default:
break;
}
if (skip) {
continue;
}
2017-04-14 19:02:05 +02:00
}
2018-10-28 12:09:07 +01:00
std::string out;
2018-01-26 17:06:56 +01:00
if (item.IsPath) {
2018-08-09 18:06:22 +02:00
// nvcc understands absolute paths to libraries ending in '.a' should
// be passed to nvlink. Other extensions like '.so' or '.dylib' are
// rejected by the nvcc front-end even though nvlink knows to ignore
// them. Bypass the front-end via '-Xnvlink'.
if (!cmHasLiteralSuffix(item.Value, ".a")) {
2018-10-28 12:09:07 +01:00
out += "-Xnvlink ";
2018-08-09 18:06:22 +02:00
}
2018-10-28 12:09:07 +01:00
out +=
this->ConvertToOutputFormat(this->ConvertToLinkReference(item.Value));
} else if (cmLinkItemValidForDevice(item.Value)) {
out += item.Value;
}
if (emitted.insert(out).second) {
fout << out << " ";
2017-04-14 19:02:05 +02:00
}
}
if (!stdLibString.empty()) {
fout << stdLibString << " ";
}
return fout.str();
}
std::string cmLinkLineDeviceComputer::GetLinkerLanguage(cmGeneratorTarget*,
std::string const&)
{
return "CUDA";
}
cmNinjaLinkLineDeviceComputer::cmNinjaLinkLineDeviceComputer(
2017-07-20 19:35:53 +02:00
cmOutputConverter* outputConverter, cmStateDirectory const& stateDir,
2017-04-14 19:02:05 +02:00
cmGlobalNinjaGenerator const* gg)
: cmLinkLineDeviceComputer(outputConverter, stateDir)
, GG(gg)
{
}
std::string cmNinjaLinkLineDeviceComputer::ConvertToLinkReference(
std::string const& lib) const
{
return GG->ConvertToNinjaPath(lib);
}