cmake/Source/cmExportLibraryDependenciesCommand.cxx

164 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-04-14 19:02:05 +02:00
#include <map>
#include <utility>
2020-02-01 23:06:01 +01:00
#include <cm/memory>
#include "cmsys/FStream.hxx"
#include "cmExecutionStatus.h"
#include "cmGeneratedFileStream.h"
2016-07-09 11:21:54 +02:00
#include "cmGlobalGenerator.h"
2020-08-30 11:54:41 +02:00
#include "cmLocalGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmTarget.h"
#include "cmTargetLinkLibraryType.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2020-08-30 11:54:41 +02:00
class cmListFileBacktrace;
2020-02-01 23:06:01 +01:00
static void FinalAction(cmMakefile& makefile, std::string const& filename,
bool append)
{
// Use copy-if-different if not appending.
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmsys::ofstream> foutPtr;
2020-02-01 23:06:01 +01:00
if (append) {
2019-11-11 23:01:05 +01:00
const auto openmodeApp = std::ios::app;
2020-02-01 23:06:01 +01:00
foutPtr = cm::make_unique<cmsys::ofstream>(filename.c_str(), openmodeApp);
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmGeneratedFileStream> ap(
2020-02-01 23:06:01 +01:00
new cmGeneratedFileStream(filename, 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) {
2020-02-01 23:06:01 +01:00
cmSystemTools::Error("Error Writing " + filename);
cmSystemTools::ReportLastSystemError("");
return;
2016-07-09 11:21:54 +02:00
}
// Collect dependency information about all library targets built in
// the project.
2020-02-01 23:06:01 +01:00
cmake* cm = makefile.GetCMakeInstance();
cmGlobalGenerator* global = cm->GetGlobalGenerator();
2020-08-30 11:54:41 +02:00
const auto& 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;
2020-08-30 11:54:41 +02:00
for (const auto& local : locals) {
2019-11-11 23:01:05 +01:00
for (auto const& tgt : local->GetTargets()) {
// 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.
2020-02-01 23:06:01 +01:00
std::string targetEntry = cmStrCat(target.GetName(), "_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) {
2020-02-01 23:06:01 +01:00
std::string ltVar = cmStrCat(li.first, "_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).
2021-11-20 13:41:27 +01:00
if (cmValue outname = libtgt->GetProperty("OUTPUT_NAME")) {
2020-08-30 11:54:41 +02:00
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";
}
2020-02-01 23:06:01 +01:00
bool cmExportLibraryDependenciesCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
if (args.empty()) {
status.SetError("called with incorrect number of arguments");
return false;
}
std::string const& filename = args[0];
bool const append = args.size() > 1 && args[1] == "APPEND";
2020-08-30 11:54:41 +02:00
status.GetMakefile().AddGeneratorAction(
[filename, append](cmLocalGenerator& lg, const cmListFileBacktrace&) {
FinalAction(*lg.GetMakefile(), filename, append);
2020-02-01 23:06:01 +01:00
});
return true;
}