Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmGetFilenameComponentCommand.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmGetFilenameComponentCommand.h"
4
5
#include "cmExecutionStatus.h"
6
#include "cmMakefile.h"
7
#include "cmStateTypes.h"
8
#include "cmStringAlgorithms.h"
9
#include "cmSystemTools.h"
10
#include "cmValue.h"
11
12
// cmGetFilenameComponentCommand
13
bool cmGetFilenameComponentCommand(std::vector<std::string> const& args,
14
                                   cmExecutionStatus& status)
15
0
{
16
0
  if (args.size() < 3) {
17
0
    status.SetError("called with incorrect number of arguments");
18
0
    cmSystemTools::SetFatalErrorOccurred();
19
0
    return false;
20
0
  }
21
22
  // Check and see if the value has been stored in the cache
23
  // already, if so use that value
24
0
  if (args.size() >= 4 && args.back() == "CACHE") {
25
0
    cmValue cacheValue = status.GetMakefile().GetDefinition(args.front());
26
0
    if (cacheValue && !cmIsNOTFOUND(*cacheValue)) {
27
0
      return true;
28
0
    }
29
0
  }
30
31
0
  std::string result;
32
0
  std::string filename = args[1];
33
0
  if (filename.find("[HKEY") != std::string::npos) {
34
    // Check the registry as the target application would view it.
35
0
    cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
36
0
    cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
37
0
    if (status.GetMakefile().PlatformIs64Bit()) {
38
0
      view = cmSystemTools::KeyWOW64_64;
39
0
      other_view = cmSystemTools::KeyWOW64_32;
40
0
    }
41
0
    cmSystemTools::ExpandRegistryValues(filename, view);
42
0
    if (filename.find("/registry") != std::string::npos) {
43
0
      std::string other = args[1];
44
0
      cmSystemTools::ExpandRegistryValues(other, other_view);
45
0
      if (other.find("/registry") == std::string::npos) {
46
0
        filename = other;
47
0
      }
48
0
    }
49
0
  }
50
0
  std::string storeArgs;
51
0
  std::string programArgs;
52
0
  if (args[2] == "DIRECTORY" || args[2] == "PATH") {
53
0
    result = cmSystemTools::GetFilenamePath(filename);
54
0
  } else if (args[2] == "NAME") {
55
0
    result = cmSystemTools::GetFilenameName(filename);
56
0
  } else if (args[2] == "PROGRAM") {
57
0
    for (unsigned int i = 2; i < args.size(); ++i) {
58
0
      if (args[i] == "PROGRAM_ARGS") {
59
0
        i++;
60
0
        if (i < args.size()) {
61
0
          storeArgs = args[i];
62
0
        }
63
0
      }
64
0
    }
65
66
    // First assume the path to the program was specified with no
67
    // arguments and with no quoting or escaping for spaces.
68
    // Only bother doing this if there is non-whitespace.
69
0
    if (!cmTrimWhitespace(filename).empty()) {
70
0
      result = cmSystemTools::FindProgram(filename);
71
0
    }
72
73
    // If that failed then assume a command-line string was given
74
    // and split the program part from the rest of the arguments.
75
0
    if (result.empty()) {
76
0
      std::string program;
77
0
      if (cmSystemTools::SplitProgramFromArgs(filename, program,
78
0
                                              programArgs)) {
79
0
        if (cmSystemTools::FileExists(program)) {
80
0
          result = program;
81
0
        } else {
82
0
          result = cmSystemTools::FindProgram(program);
83
0
        }
84
0
      }
85
0
      if (result.empty()) {
86
0
        programArgs.clear();
87
0
      }
88
0
    }
89
0
  } else if (args[2] == "EXT") {
90
0
    result = cmSystemTools::GetFilenameExtension(filename);
91
0
  } else if (args[2] == "NAME_WE") {
92
0
    result = cmSystemTools::GetFilenameWithoutExtension(filename);
93
0
  } else if (args[2] == "LAST_EXT") {
94
0
    result = cmSystemTools::GetFilenameLastExtension(filename);
95
0
  } else if (args[2] == "NAME_WLE") {
96
0
    result = cmSystemTools::GetFilenameWithoutLastExtension(filename);
97
0
  } else if (args[2] == "ABSOLUTE" || args[2] == "REALPATH") {
98
    // If the path given is relative, evaluate it relative to the
99
    // current source directory unless the user passes a different
100
    // base directory.
101
0
    std::string baseDir = status.GetMakefile().GetCurrentSourceDirectory();
102
0
    for (unsigned int i = 3; i < args.size(); ++i) {
103
0
      if (args[i] == "BASE_DIR") {
104
0
        ++i;
105
0
        if (i < args.size()) {
106
0
          baseDir = args[i];
107
0
        }
108
0
      }
109
0
    }
110
    // Collapse the path to its simplest form.
111
0
    result = cmSystemTools::CollapseFullPath(filename, baseDir);
112
0
    if (args[2] == "REALPATH") {
113
      // Resolve symlinks if possible
114
0
      result = cmSystemTools::GetRealPath(result);
115
0
    }
116
0
    result = cmSystemTools::GetActualCaseForPath(result);
117
0
  } else {
118
0
    std::string err = "unknown component " + args[2];
119
0
    status.SetError(err);
120
0
    cmSystemTools::SetFatalErrorOccurred();
121
0
    return false;
122
0
  }
123
124
0
  if (args.size() >= 4 && args.back() == "CACHE") {
125
0
    if (!programArgs.empty() && !storeArgs.empty()) {
126
0
      status.GetMakefile().AddCacheDefinition(
127
0
        storeArgs, programArgs, "",
128
0
        args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
129
0
    }
130
0
    status.GetMakefile().AddCacheDefinition(
131
0
      args.front(), result, "",
132
0
      args[2] == "PATH" ? cmStateEnums::FILEPATH : cmStateEnums::STRING);
133
0
  } else {
134
0
    if (!programArgs.empty() && !storeArgs.empty()) {
135
0
      status.GetMakefile().AddDefinition(storeArgs, programArgs);
136
0
    }
137
0
    status.GetMakefile().AddDefinition(args.front(), result);
138
0
  }
139
140
0
  return true;
141
0
}