Coverage Report

Created: 2026-09-14 06:43

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
static bool GetSourceFilePropertyGENERATED(std::string const& name,
18
                                           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
  static std::string const sOne = "1";
30
0
  static std::string const sZero = "0";
31
0
  {
32
0
    auto file =
33
0
      cmSystemTools::CollapseFullPath(name, mf.GetCurrentBinaryDirectory());
34
0
    if (mf.GetGlobalGenerator()->IsGeneratedFile(file)) {
35
0
      propertyValue = cmValue(sOne);
36
0
      return true;
37
0
    }
38
0
  }
39
0
  {
40
0
    auto file =
41
0
      cmSystemTools::CollapseFullPath(name, mf.GetCurrentSourceDirectory());
42
0
    if (mf.GetGlobalGenerator()->IsGeneratedFile(file)) {
43
0
      propertyValue = cmValue(sOne);
44
0
      return true;
45
0
    }
46
0
  }
47
0
  propertyValue = cmValue(sZero);
48
0
  return true;
49
0
}
50
51
cmGetSourceFilePropertyResult cmGetSourceFileProperty(
52
  std::string const& sourceName, std::string const& propertyName,
53
  cmExecutionStatus& status, bool sourceFileDirectoryOptionEnabled,
54
  bool sourceFileTargetOptionEnabled,
55
  std::vector<std::string>& sourceFileDirectories,
56
  std::vector<std::string>& sourceFileTargetDirectories,
57
  cmValue& propertyValue, bool alwaysCreateSource)
58
0
{
59
0
  std::vector<cmMakefile*> sourceFileDirectoryMakefiles;
60
0
  if (!SetPropertyCommand::HandleAndValidateSourceFileDirectoryScopes(
61
0
        status, sourceFileDirectoryOptionEnabled,
62
0
        sourceFileTargetOptionEnabled, sourceFileDirectories,
63
0
        sourceFileTargetDirectories, sourceFileDirectoryMakefiles)) {
64
0
    return cmGetSourceFilePropertyResult::ScopeError;
65
0
  }
66
67
0
  bool const sourceFilePathsShouldBeAbsolute =
68
0
    sourceFileDirectoryOptionEnabled || sourceFileTargetOptionEnabled;
69
0
  cmMakefile& directoryMakefile = *sourceFileDirectoryMakefiles[0];
70
71
  // Special handling for GENERATED property.
72
  // Note: Only if CMP0163 is set to NEW.
73
0
  if (propertyName == "GENERATED"_s) {
74
0
    auto cmp0163 = directoryMakefile.GetPolicyStatus(cmPolicies::CMP0163);
75
0
    bool const cmp0163new =
76
0
      cmp0163 != cmPolicies::OLD && cmp0163 != cmPolicies::WARN;
77
0
    if (cmp0163new) {
78
0
      if (!GetSourceFilePropertyGENERATED(sourceName, status.GetMakefile(),
79
0
                                          propertyValue)) {
80
0
        return cmGetSourceFilePropertyResult::Error;
81
0
      }
82
0
      return cmGetSourceFilePropertyResult::Success;
83
0
    }
84
0
  }
85
86
0
  std::string const absolutePath =
87
0
    SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
88
0
      status, sourceName, sourceFilePathsShouldBeAbsolute);
89
90
0
  cmSourceFile* sf = nullptr;
91
0
  if (alwaysCreateSource || propertyName == "LOCATION"_s) {
92
0
    sf = directoryMakefile.GetOrCreateSource(absolutePath);
93
0
    if (!sf) {
94
0
      return cmGetSourceFilePropertyResult::Error;
95
0
    }
96
0
  } else {
97
0
    sf = directoryMakefile.GetSource(absolutePath);
98
0
    if (!sf) {
99
0
      return cmGetSourceFilePropertyResult::SourceNotFound;
100
0
    }
101
0
  }
102
103
0
  propertyValue = sf->GetPropertyForUser(propertyName);
104
0
  return cmGetSourceFilePropertyResult::Success;
105
0
}