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 "gn/copy_target_generator.h"
6
7 #include "gn/build_settings.h"
8 #include "gn/filesystem_utils.h"
9 #include "gn/ohos_variables.h"
10 #include "gn/parse_tree.h"
11 #include "gn/scope.h"
12 #include "gn/value.h"
13
CopyTargetGenerator(Target * target,Scope * scope,const FunctionCallNode * function_call,Err * err)14 CopyTargetGenerator::CopyTargetGenerator(Target* target,
15 Scope* scope,
16 const FunctionCallNode* function_call,
17 Err* err)
18 : TargetGenerator(target, scope, function_call, err) {}
19
20 CopyTargetGenerator::~CopyTargetGenerator() = default;
21
FillCopyLinkableFile()22 bool CopyTargetGenerator::FillCopyLinkableFile()
23 {
24 const Value* value = scope_->GetValue(variables::kCopyLinkableFile, true);
25 if (!value)
26 return true;
27 if (!value->VerifyTypeIs(Value::BOOLEAN, err_))
28 return false;
29 target_->set_copy_linkable_file(value->boolean_value());
30 return true;
31 }
32
FillCopyRustTargetInfo()33 bool CopyTargetGenerator::FillCopyRustTargetInfo() {
34 const Value* rustCrateType =
35 scope_->GetValue(variables::kCopyRustCrateType, true);
36 const Value* rustCrateName =
37 scope_->GetValue(variables::kCopyRustCrateName, true);
38
39 if (!rustCrateName || !rustCrateType) {
40 return true;
41 }
42
43 if (!(rustCrateType->VerifyTypeIs(Value::STRING, err_) &&
44 rustCrateName->VerifyTypeIs(Value::STRING, err_)))
45 return false;
46
47 target_->rust_values().crate_name() =
48 std::move(rustCrateName->string_value());
49
50 if (rustCrateType->string_value() == "bin") {
51 target_->rust_values().set_crate_type(RustValues::CRATE_BIN);
52 return true;
53 }
54 if (rustCrateType->string_value() == "cdylib") {
55 target_->rust_values().set_crate_type(RustValues::CRATE_CDYLIB);
56 return true;
57 }
58 if (rustCrateType->string_value() == "dylib") {
59 target_->rust_values().set_crate_type(RustValues::CRATE_DYLIB);
60 return true;
61 }
62 if (rustCrateType->string_value() == "proc-macro") {
63 target_->rust_values().set_crate_type(RustValues::CRATE_PROC_MACRO);
64 return true;
65 }
66 if (rustCrateType->string_value() == "rlib") {
67 target_->rust_values().set_crate_type(RustValues::CRATE_RLIB);
68 return true;
69 }
70 if (rustCrateType->string_value() == "staticlib") {
71 target_->rust_values().set_crate_type(RustValues::CRATE_STATICLIB);
72 return true;
73 }
74
75 *err_ =
76 Err(rustCrateType->origin(),
77 "Inadmissible crate type \"" + rustCrateType->string_value() + "\".");
78 return false;
79 }
80
DoRun()81 void CopyTargetGenerator::DoRun() {
82 target_->set_output_type(Target::COPY_FILES);
83
84 if (!FillSources())
85 return;
86 if (!FillOutputs(true))
87 return;
88
89 if (target_->sources().empty()) {
90 *err_ = Err(
91 function_call_, "Empty sources for copy command.",
92 "You have to specify at least one file to copy in the \"sources\".");
93 return;
94 }
95 if (target_->action_values().outputs().list().size() != 1) {
96 *err_ = Err(
97 function_call_, "Copy command must have exactly one output.",
98 "You must specify exactly one value in the \"outputs\" array for the "
99 "destination of the copy\n(see \"gn help copy\"). If there are "
100 "multiple sources to copy, use source expansion\n(see \"gn help "
101 "source_expansion\").");
102 return;
103 }
104
105 if (!FillCopyLinkableFile())
106 return;
107
108 if (!FillCopyRustTargetInfo())
109 return;
110 }
111