Coverage Report

Created: 2026-10-28 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSourceFilePropertyHelper.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 "cmSourceFilePropertyHelper.h"
4
5
#include <cm/string_view>
6
#include <cmext/string_view>
7
8
#include "cmExecutionStatus.h"
9
#include "cmGlobalGenerator.h"
10
#include "cmMakefile.h"
11
#include "cmPolicies.h"
12
#include "cmSetPropertyCommand.h"
13
#include "cmSourceFile.h"
14
#include "cmSystemTools.h"
15
#include "cmValue.h"
16
17
namespace {
18
bool GetSourceFilePropertyGENERATED(std::string const& name, cmMakefile& mf,
19
                                    cmValue& propertyValue)
20
0
{
21
  // Globally set as generated?
22
  // Note: If the given "name" only contains a filename or a relative path
23
  //       the file's location is ambiguous. In general, one would expect
24
  //       it in the source-directory, because that is where source files
25
  //       are located normally. However, generated files are normally
26
  //       generated in the build-directory. Therefore, we first check for
27
  //       a generated file in the build-directory before we check for a
28
  //       generated file in the source-directory.
29
0
  {
30
0
    auto file =
31
0
      cmSystemTools::CollapseFullPath(name, mf.GetCurrentBinaryDirectory());
32
0
    if (mf.GetGlobalGenerator()->IsGeneratedFile(file)) {
33
0
      propertyValue = cmValue::True;
34
0
      return true;
35
0
    }
36
0
  }
37
0
  {
38
0
    auto file =
39
0
      cmSystemTools::CollapseFullPath(name, mf.GetCurrentSourceDirectory());
40
0
    if (mf.GetGlobalGenerator()->IsGeneratedFile(file)) {
41
0
      propertyValue = cmValue::True;
42
0
      return true;
43
0
    }
44
0
  }
45
0
  propertyValue = cmValue::False;
46
0
  return true;
47
0
}
48
}
49
50
cmGetSourceFilePropertyResult cmGetSourceFileProperty(
51
  std::string const& sourceName, std::string const& propertyName,
52
  cmExecutionStatus& status, bool sourceFileDirectoryOptionEnabled,
53
  bool sourceFileTargetOptionEnabled,
54
  std::vector<std::string>& sourceFileDirectories,
55
  std::vector<std::string>& sourceFileTargetDirectories,
56
  cmValue& propertyValue, bool alwaysCreateSource)
57
0
{
58
0
  std::vector<cmMakefile*> sourceFileDirectoryMakefiles;
59
0
  if (!SetPropertyCommand::HandleAndValidateSourceFileDirectoryScopes(
60
0
        status, sourceFileDirectoryOptionEnabled,
61
0
        sourceFileTargetOptionEnabled, sourceFileDirectories,
62
0
        sourceFileTargetDirectories, sourceFileDirectoryMakefiles)) {
63
0
    return cmGetSourceFilePropertyResult::ScopeError;
64
0
  }
65
66
0
  bool const sourceFilePathsShouldBeAbsolute =
67
0
    sourceFileDirectoryOptionEnabled || sourceFileTargetOptionEnabled;
68
0
  cmMakefile& directoryMakefile = *sourceFileDirectoryMakefiles[0];
69
70
  // Special handling for GENERATED property.
71
  // Note: Only if CMP0163 is set to NEW.
72
0
  if (propertyName == "GENERATED"_s) {
73
0
    auto cmp0163 = directoryMakefile.GetPolicyStatus(cmPolicies::CMP0163);
74
0
    bool const cmp0163new =
75
0
      cmp0163 != cmPolicies::OLD && cmp0163 != cmPolicies::WARN;
76
0
    if (cmp0163new) {
77
0
      if (!GetSourceFilePropertyGENERATED(sourceName, status.GetMakefile(),
78
0
                                          propertyValue)) {
79
0
        return cmGetSourceFilePropertyResult::Error;
80
0
      }
81
0
      return cmGetSourceFilePropertyResult::Success;
82
0
    }
83
0
  }
84
85
0
  std::string const absolutePath =
86
0
    SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
87
0
      status, sourceName, sourceFilePathsShouldBeAbsolute);
88
89
0
  cmSourceFile* sf = nullptr;
90
0
  if (alwaysCreateSource || propertyName == "LOCATION"_s) {
91
0
    sf = directoryMakefile.GetOrCreateSource(absolutePath);
92
0
    if (!sf) {
93
0
      return cmGetSourceFilePropertyResult::Error;
94
0
    }
95
0
  } else {
96
0
    sf = directoryMakefile.GetSource(absolutePath);
97
0
    if (!sf) {
98
0
      return cmGetSourceFilePropertyResult::SourceNotFound;
99
0
    }
100
0
  }
101
102
0
  propertyValue = sf->GetPropertyForUser(propertyName);
103
0
  return cmGetSourceFilePropertyResult::Success;
104
0
}