- add sources.
[platform/framework/web/crosswalk.git] / src / tools / gn / config.cc
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "tools/gn/config.h"
6
7 #include "tools/gn/err.h"
8 #include "tools/gn/input_file_manager.h"
9 #include "tools/gn/item_node.h"
10 #include "tools/gn/item_tree.h"
11 #include "tools/gn/scheduler.h"
12
13 Config::Config(const Settings* settings, const Label& label)
14     : Item(settings, label) {
15 }
16
17 Config::~Config() {
18 }
19
20 Config* Config::AsConfig() {
21   return this;
22 }
23
24 const Config* Config::AsConfig() const {
25   return this;
26 }
27
28 // static
29 Config* Config::GetConfig(const Settings* settings,
30                           const LocationRange& specified_from_here,
31                           const Label& label,
32                           Item* dep_from,
33                           Err* err) {
34   DCHECK(!label.is_null());
35
36   ItemTree* tree = &settings->build_settings()->item_tree();
37   base::AutoLock lock(tree->lock());
38
39   ItemNode* node = tree->GetExistingNodeLocked(label);
40   Config* config = NULL;
41   if (!node) {
42     config = new Config(settings, label);
43     node = new ItemNode(config);
44     node->set_originally_referenced_from_here(specified_from_here);
45     tree->AddNodeLocked(node);
46
47     // Only schedule loading the given target if somebody is depending on it
48     // (and we optimize by not re-asking it to run the current file).
49     // Otherwise, we're probably generating it right now.
50     if (dep_from && dep_from->label().dir() != label.dir()) {
51       settings->build_settings()->toolchain_manager().ScheduleInvocationLocked(
52           specified_from_here, label.GetToolchainLabel(), label.dir(),
53           err);
54     }
55   } else if ((config = node->item()->AsConfig())) {
56     // Previously saw this item as a config.
57
58     // If we have no dep_from, we're generating it. In this case, it had better
59     // not already be generated.
60     if (!dep_from && node->state() != ItemNode::REFERENCED) {
61       *err = Err(specified_from_here, "Duplicate config definition.",
62           "You already told me about a config with this name.");
63       return NULL;
64     }
65   } else {
66     // Previously saw this thing as a non-config.
67     *err = Err(specified_from_here,
68                "Config name already used.",
69                "Previously you specified a " +
70                node->item()->GetItemTypeName() + " with this name instead.");
71     return NULL;
72   }
73
74   // Keep a record of the guy asking us for this dependency. We know if
75   // somebody is adding a dependency, that guy it himself not resolved.
76   if (dep_from) {
77     if (!dep_from->item_node()->AddDependency(
78             settings->build_settings(), specified_from_here, node, err))
79       return NULL;
80   }
81   return config;
82 }