cmake/Source/cmExportLibraryDependenciesCommand.cxx

169 lines
5.1 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. */
2014-08-03 19:52:23 +02:00
#include "cmExportLibraryDependenciesCommand.h"
2016-07-09 11:21:54 +02:00
2017-07-20 19:35:53 +02:00
#include "cmsys/FStream.hxx"
2017-04-14 19:02:05 +02:00
#include <map>
2018-01-26 17:06:56 +01:00
#include <memory> // IWYU pragma: keep
2017-04-14 19:02:05 +02:00
#include <utility>
2018-01-26 17:06:56 +01:00
#include "cmAlgorithms.h"
#include "cmGeneratedFileStream.h"
2016-07-09 11:21:54 +02:00
#include "cmGlobalGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
#include "cmTargetLinkLibraryType.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2017-04-14 19:02:05 +02:00
class cmExecutionStatus;
2016-07-09 11:21:54 +02:00
bool cmExportLibraryDependenciesCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// store the arguments for the final pass
this->Filename = args[0];
this->Append = false;
2016-07-09 11:21:54 +02:00
if (args.size() > 1) {
if (args[1] == "APPEND") {
this->Append = true;
}
2016-07-09 11:21:54 +02:00
}
return true;
}
void cmExportLibraryDependenciesCommand::FinalPass()
{
// export_library_dependencies() shouldn't modify anything
// ensure this by calling a const method
this->ConstFinalPass();
}
void cmExportLibraryDependenciesCommand::ConstFinalPass() const
{
// Use copy-if-different if not appending.
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmsys::ofstream> foutPtr;
2016-07-09 11:21:54 +02:00
if (this->Append) {
2018-01-26 17:06:56 +01:00
foutPtr =
cm::make_unique<cmsys::ofstream>(this->Filename.c_str(), std::ios::app);
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmGeneratedFileStream> ap(
new cmGeneratedFileStream(this->Filename.c_str(), true));
ap->SetCopyIfDifferent(true);
2018-01-26 17:06:56 +01:00
foutPtr = std::move(ap);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
std::ostream& fout = *foutPtr;
2016-07-09 11:21:54 +02:00
if (!fout) {
cmSystemTools::Error("Error Writing ", this->Filename.c_str());
cmSystemTools::ReportLastSystemError("");
return;
2016-07-09 11:21:54 +02:00
}
// Collect dependency information about all library targets built in
// the project.
cmake* cm = this->Makefile->GetCMakeInstance();
cmGlobalGenerator* global = cm->GetGlobalGenerator();
2015-11-17 17:22:37 +01:00
const std::vector<cmMakefile*>& locals = global->GetMakefiles();
2015-04-27 22:25:09 +02:00
std::map<std::string, std::string> libDepsOld;
std::map<std::string, std::string> libDepsNew;
std::map<std::string, std::string> libTypes;
2018-01-26 17:06:56 +01:00
for (cmMakefile* local : locals) {
const cmTargets& tgts = local->GetTargets();
for (auto const& tgt : tgts) {
// Get the current target.
2018-01-26 17:06:56 +01:00
cmTarget const& target = tgt.second;
// Skip non-library targets.
2017-04-14 19:02:05 +02:00
if (target.GetType() < cmStateEnums::STATIC_LIBRARY ||
target.GetType() > cmStateEnums::MODULE_LIBRARY) {
continue;
2016-07-09 11:21:54 +02:00
}
// Construct the dependency variable name.
std::string targetEntry = target.GetName();
targetEntry += "_LIB_DEPENDS";
2015-04-27 22:25:09 +02:00
// Construct the dependency variable value with the direct link
// dependencies.
std::string valueOld;
std::string valueNew;
2015-04-27 22:25:09 +02:00
cmTarget::LinkLibraryVectorType const& libs =
target.GetOriginalLinkLibraries();
2018-01-26 17:06:56 +01:00
for (cmTarget::LibraryID const& li : libs) {
std::string ltVar = li.first;
ltVar += "_LINK_TYPE";
std::string ltValue;
2018-01-26 17:06:56 +01:00
switch (li.second) {
2016-03-13 13:35:51 +01:00
case GENERAL_LibraryType:
valueNew += "general;";
ltValue = "general";
break;
2016-03-13 13:35:51 +01:00
case DEBUG_LibraryType:
valueNew += "debug;";
ltValue = "debug";
break;
2016-03-13 13:35:51 +01:00
case OPTIMIZED_LibraryType:
valueNew += "optimized;";
ltValue = "optimized";
break;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
std::string lib = li.first;
2016-07-09 11:21:54 +02:00
if (cmTarget* libtgt = global->FindTarget(lib)) {
// Handle simple output name changes. This command is
// deprecated so we do not support full target name
// translation (which requires per-configuration info).
2016-07-09 11:21:54 +02:00
if (const char* outname = libtgt->GetProperty("OUTPUT_NAME")) {
lib = outname;
}
2016-07-09 11:21:54 +02:00
}
valueOld += lib;
valueOld += ";";
valueNew += lib;
valueNew += ";";
std::string& ltEntry = libTypes[ltVar];
2016-07-09 11:21:54 +02:00
if (ltEntry.empty()) {
ltEntry = ltValue;
2016-07-09 11:21:54 +02:00
} else if (ltEntry != ltValue) {
ltEntry = "general";
}
2016-07-09 11:21:54 +02:00
}
libDepsNew[targetEntry] = valueNew;
libDepsOld[targetEntry] = valueOld;
}
2016-07-09 11:21:54 +02:00
}
// Generate dependency information for both old and new style CMake
// versions.
const char* vertest =
"\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
2016-10-30 18:24:19 +01:00
fout << "# Generated by CMake\n\n";
2014-08-03 19:52:23 +02:00
fout << "if(" << vertest << ")\n";
fout << " # Information for CMake 2.6 and above.\n";
2018-01-26 17:06:56 +01:00
for (auto const& i : libDepsNew) {
if (!i.second.empty()) {
fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
fout << "else()\n";
fout << " # Information for CMake 2.4 and lower.\n";
2018-01-26 17:06:56 +01:00
for (auto const& i : libDepsOld) {
if (!i.second.empty()) {
fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (auto const& i : libTypes) {
if (i.second != "general") {
fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
fout << "endif()\n";
}