summaryrefslogtreecommitdiffstats
path: root/tools/gn/script_target_generator.cc
blob: 1725b7fc7918f0dc79875bf56cc6357b39b923bb (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
// 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/script_target_generator.h"

#include "tools/gn/err.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/scope.h"
#include "tools/gn/value.h"
#include "tools/gn/value_extractors.h"

ScriptTargetGenerator::ScriptTargetGenerator(Target* target,
                                             Scope* scope,
                                             const Token& function_token,
                                             Err* err)
    : TargetGenerator(target, scope, function_token, err) {
}

ScriptTargetGenerator::~ScriptTargetGenerator() {
}

void ScriptTargetGenerator::DoRun() {
  target_->set_output_type(Target::CUSTOM);

  FillExternal();
  FillSources();
  FillScript();
  FillScriptArgs();
  FillOutputs();

  // Script outputs don't depend on the current toolchain so we can skip adding
  // that dependency.
}

void ScriptTargetGenerator::FillScript() {
  // If this gets called, the target type requires a script, so error out
  // if it doesn't have one.
  // TODO(brettw) hook up a constant in variables.h
  const Value* value = scope_->GetValue("script", true);
  if (!value) {
    *err_ = Err(function_token_, "This target type requires a \"script\".");
    return;
  }
  if (!value->VerifyTypeIs(Value::STRING, err_))
    return;

  target_->script_values().set_script(
      scope_->GetSourceDir().ResolveRelativeFile(value->string_value()));
}

void ScriptTargetGenerator::FillScriptArgs() {
  const Value* value = scope_->GetValue("args", true);
  if (!value)
    return;

  std::vector<std::string> args;
  if (!ExtractListOfStringValues(*value, &args, err_))
    return;
  target_->script_values().swap_in_args(&args);
}

void ScriptTargetGenerator::FillOutputs() {
  // TODO(brettw) hook up a constant in variables.h
  const Value* value = scope_->GetValue("outputs", true);
  if (!value)
    return;

  Target::FileList outputs;
  if (!ExtractListOfRelativeFiles(scope_->settings()->build_settings(), *value,
                                  scope_->GetSourceDir(), &outputs, err_))
    return;

  // Validate that outputs are in the output dir.
  CHECK(outputs.size() == value->list_value().size());
  for (size_t i = 0; i < outputs.size(); i++) {
    if (!EnsureStringIsInOutputDir(
            GetBuildSettings()->build_dir(),
            outputs[i].value(), value->list_value()[i], err_))
      return;
  }
  target_->script_values().swap_in_outputs(&outputs);
}