summaryrefslogtreecommitdiffstats
path: root/tools/gn/config_values_extractors_unittest.cc
blob: fa0d0700a29e743a4d31d001b575d21d35fb44f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <sstream>

#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/config.h"
#include "tools/gn/config_values_extractors.h"
#include "tools/gn/target.h"
#include "tools/gn/test_with_scope.h"

namespace {

struct FlagWriter {
  void operator()(const std::string& dir, std::ostream& out) const {
    out << dir << " ";
  }
};

struct IncludeWriter {
  void operator()(const SourceDir& dir, std::ostream& out) const {
    out << dir.value() << " ";
  }
};

}  // namespace

TEST(ConfigValuesExtractors, IncludeOrdering) {
  TestWithScope setup;
  Err err;

  // Construct a chain of dependencies: target -> dep1 -> dep2
  // Add representative values: cflags (opaque, always copied) and include_dirs
  // (uniquified) to each one so we can check what comes out the other end.

  // Set up dep2, direct and all dependent configs.
  Config dep2_all(setup.settings(), Label(SourceDir("//dep2/"), "all"));
  dep2_all.config_values().cflags().push_back("--dep2-all");
  dep2_all.config_values().include_dirs().push_back(SourceDir("//dep2/all/"));

  Config dep2_direct(setup.settings(), Label(SourceDir("//dep2/"), "direct"));
  dep2_direct.config_values().cflags().push_back("--dep2-direct");
  dep2_direct.config_values().include_dirs().push_back(
      SourceDir("//dep2/direct/"));

  Target dep2(setup.settings(), Label(SourceDir("//dep2/"), "dep2"));
  dep2.set_output_type(Target::SOURCE_SET);
  dep2.visibility().SetPublic();
  dep2.SetToolchain(setup.toolchain());
  dep2.all_dependent_configs().push_back(LabelConfigPair(&dep2_all));
  dep2.public_configs().push_back(LabelConfigPair(&dep2_direct));

  // Set up dep1, direct and all dependent configs.
  Config dep1_all(setup.settings(), Label(SourceDir("//dep1/"), "all"));
  dep1_all.config_values().cflags().push_back("--dep1-all");
  dep1_all.config_values().include_dirs().push_back(SourceDir("//dep1/all/"));

  Config dep1_direct(setup.settings(), Label(SourceDir("//dep1/"), "direct"));
  dep1_direct.config_values().cflags().push_back("--dep1-direct");
  dep1_direct.config_values().include_dirs().push_back(
      SourceDir("//dep1/direct/"));

  Target dep1(setup.settings(), Label(SourceDir("//dep1/"), "dep1"));
  dep1.set_output_type(Target::SOURCE_SET);
  dep1.visibility().SetPublic();
  dep1.SetToolchain(setup.toolchain());
  dep1.all_dependent_configs().push_back(LabelConfigPair(&dep1_all));
  dep1.public_configs().push_back(LabelConfigPair(&dep1_direct));
  dep1.private_deps().push_back(LabelTargetPair(&dep2));

  // Set up target, direct and all dependent configs.
  Config target_all(setup.settings(), Label(SourceDir("//target/"), "all"));
  target_all.config_values().cflags().push_back("--target-all");
  target_all.config_values().include_dirs().push_back(
      SourceDir("//target/all/"));

  Config target_direct(setup.settings(),
                       Label(SourceDir("//target/"), "direct"));
  target_direct.config_values().cflags().push_back("--target-direct");
  target_direct.config_values().include_dirs().push_back(
      SourceDir("//target/direct/"));

  // This config is applied directly to target.
  Config target_config(setup.settings(),
                       Label(SourceDir("//target/"), "config"));
  target_config.config_values().cflags().push_back("--target-config");
  target_config.config_values().include_dirs().push_back(
      SourceDir("//target/config/"));

  Target target(setup.settings(), Label(SourceDir("//target/"), "target"));
  target.set_output_type(Target::SOURCE_SET);
  target.SetToolchain(setup.toolchain());
  target.all_dependent_configs().push_back(LabelConfigPair(&target_all));
  target.public_configs().push_back(LabelConfigPair(&target_direct));
  target.configs().push_back(LabelConfigPair(&target_config));
  target.private_deps().push_back(LabelTargetPair(&dep1));


  // Additionally add some values directly on "target".
  target.config_values().cflags().push_back("--target");
  target.config_values().include_dirs().push_back(
      SourceDir("//target/"));

  // Mark targets resolved. This should push dependent configs.
  ASSERT_TRUE(dep2.OnResolved(&err));
  ASSERT_TRUE(dep1.OnResolved(&err));
  ASSERT_TRUE(target.OnResolved(&err));

  // Verify cflags by serializing.
  std::ostringstream flag_out;
  FlagWriter flag_writer;
  RecursiveTargetConfigToStream<std::string, FlagWriter>(
      &target, &ConfigValues::cflags, flag_writer, flag_out);
  EXPECT_EQ(flag_out.str(),
            "--target --target-config --target-all --target-direct "
            "--dep1-all --dep2-all --dep1-direct ");

  // Verify include dirs by serializing.
  std::ostringstream include_out;
  IncludeWriter include_writer;
  RecursiveTargetConfigToStream<SourceDir, IncludeWriter>(
      &target, &ConfigValues::include_dirs, include_writer, include_out);
  EXPECT_EQ(include_out.str(),
            "//target/ //target/config/ //target/all/ //target/direct/ "
            "//dep1/all/ //dep2/all/ //dep1/direct/ ");
}