https://github.com/Kitware/CMake
Revision 5b949bbb9114379120c29134b5effd77e39dd134 authored by Brad King on 16 August 2022, 17:03:26 UTC, committed by Kitware Robot on 16 August 2022, 17:03:33 UTC
a5d45e685f Tests: Add case for ENVIRONMENT_MODIFICATION property OP=reset behavior
e2854b4fa2 cmCTestRunTest: Implement the ENVIRONMENT test property with EnvDiff too
bfa1c5285b cmSystemTools: Add EnvDiff class to hold ENVIRONMENT_MODIFICATION logic
a0b1c4ee90 cmCTestRunTest: Simplify by using GetSystemPathlistSeparator
4e6cbb1f13 cmCTestRunTest: Remove unnecessary CMAKE_BOOTSTRAP guard

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !7572
2 parent s 4a82938 + a5d45e6
Raw File
Tip revision: 5b949bbb9114379120c29134b5effd77e39dd134 authored by Brad King on 16 August 2022, 17:03:26 UTC
Merge topic 'refactor-environment-modification'
Tip revision: 5b949bb
cmBinUtilsWindowsPELinker.cxx
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */

#include "cmBinUtilsWindowsPELinker.h"

#include <sstream>
#include <vector>

#include <cm/memory>

#include "cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool.h"
#include "cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.h"
#include "cmRuntimeDependencyArchive.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"

#ifdef _WIN32
#  include <windows.h>
#endif

cmBinUtilsWindowsPELinker::cmBinUtilsWindowsPELinker(
  cmRuntimeDependencyArchive* archive)
  : cmBinUtilsLinker(archive)
{
}

bool cmBinUtilsWindowsPELinker::Prepare()
{
  std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
  if (tool.empty()) {
    std::vector<std::string> command;
    if (this->Archive->GetGetRuntimeDependenciesCommand("dumpbin", command)) {
      tool = "dumpbin";
    } else {
      tool = "objdump";
    }
  }
  if (tool == "dumpbin") {
    this->Tool =
      cm::make_unique<cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool>(
        this->Archive);
  } else if (tool == "objdump") {
    this->Tool =
      cm::make_unique<cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool>(
        this->Archive);
  } else {
    std::ostringstream e;
    e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
    this->SetError(e.str());
    return false;
  }

  return true;
}

bool cmBinUtilsWindowsPELinker::ScanDependencies(
  std::string const& file, cmStateEnums::TargetType /* unused */)
{
  std::vector<std::string> needed;
  if (!this->Tool->GetFileInfo(file, needed)) {
    return false;
  }
  for (auto& n : needed) {
    n = cmSystemTools::LowerCase(n);
  }
  std::string origin = cmSystemTools::GetFilenamePath(file);

  for (auto const& lib : needed) {
    if (!this->Archive->IsPreExcluded(lib)) {
      std::string path;
      bool resolved = false;
      if (!this->ResolveDependency(lib, origin, path, resolved)) {
        return false;
      }
      if (resolved) {
        if (!this->Archive->IsPostExcluded(path)) {
          bool unique;
          this->Archive->AddResolvedPath(lib, path, unique);
          if (unique &&
              !this->ScanDependencies(path, cmStateEnums::SHARED_LIBRARY)) {
            return false;
          }
        }
      } else {
        this->Archive->AddUnresolvedPath(lib);
      }
    }
  }

  return true;
}

bool cmBinUtilsWindowsPELinker::ResolveDependency(std::string const& name,
                                                  std::string const& origin,
                                                  std::string& path,
                                                  bool& resolved)
{
  auto dirs = this->Archive->GetSearchDirectories();

#ifdef _WIN32
  char buf[MAX_PATH];
  unsigned int len;
  if ((len = GetWindowsDirectoryA(buf, MAX_PATH)) > 0) {
    dirs.insert(dirs.begin(), std::string(buf, len));
  }
  if ((len = GetSystemDirectoryA(buf, MAX_PATH)) > 0) {
    dirs.insert(dirs.begin(), std::string(buf, len));
  }
#endif

  dirs.insert(dirs.begin(), origin);

  for (auto const& searchPath : dirs) {
    path = cmStrCat(searchPath, '/', name);
    if (cmSystemTools::PathExists(path)) {
      resolved = true;
      return true;
    }
  }

  resolved = false;
  return true;
}
back to top