summaryrefslogtreecommitdiffstats
path: root/tools/gn/import_manager.cc
blob: 2d39846d981b89d99efa50eb8989cfd14b17f676 (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
// Copyright (c) 2013 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 "tools/gn/import_manager.h"

#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "tools/gn/parse_tree.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/scope_per_file_provider.h"

namespace {

// Returns a newly-allocated scope on success, null on failure.
Scope* UncachedImport(const Settings* settings,
                      const SourceFile& file,
                      const ParseNode* node_for_err,
                      Err* err) {
  const ParseNode* node = g_scheduler->input_file_manager()->SyncLoadFile(
      node_for_err->GetRange(), settings->build_settings(), file, err);
  if (!node)
    return nullptr;

  scoped_ptr<Scope> scope(new Scope(settings->base_config()));
  scope->set_source_dir(file.GetDir());

  // Don't allow ScopePerFileProvider to provide target-related variables.
  // These will be relative to the imported file, which is probably not what
  // people mean when they use these.
  ScopePerFileProvider per_file_provider(scope.get(), false);

  scope->SetProcessingImport();
  node->Execute(scope.get(), err);
  if (err->has_error())
    return nullptr;
  scope->ClearProcessingImport();

  return scope.release();
}

}  // namesapce

ImportManager::ImportManager() {
}

ImportManager::~ImportManager() {
  STLDeleteContainerPairSecondPointers(imports_.begin(), imports_.end());
}

bool ImportManager::DoImport(const SourceFile& file,
                             const ParseNode* node_for_err,
                             Scope* scope,
                             Err* err) {
  // See if we have a cached import, but be careful to actually do the scope
  // copying outside of the lock.
  const Scope* imported_scope = nullptr;
  {
    base::AutoLock lock(lock_);
    ImportMap::const_iterator found = imports_.find(file);
    if (found != imports_.end())
      imported_scope = found->second;
  }

  if (!imported_scope) {
    // Do a new import of the file.
    imported_scope = UncachedImport(scope->settings(), file,
                                    node_for_err, err);
    if (!imported_scope)
      return false;

    // We loaded the file outside the lock. This means that there could be a
    // race and the file was already loaded on a background thread. Recover
    // from this and use the existing one if that happens.
    {
      base::AutoLock lock(lock_);
      ImportMap::const_iterator found = imports_.find(file);
      if (found != imports_.end()) {
        delete imported_scope;
        imported_scope = found->second;
      } else {
        imports_[file] = imported_scope;
      }
    }
  }

  Scope::MergeOptions options;
  options.skip_private_vars = true;
  options.mark_dest_used = true;  // Don't require all imported values be used.
  return imported_scope->NonRecursiveMergeTo(scope, options, node_for_err,
                                             "import", err);
}