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/config_values_generator.h"
6
7 #include "base/strings/string_util.h"
8 #include "gn/build_settings.h"
9 #include "gn/config_values.h"
10 #include "gn/frameworks_utils.h"
11 #include "gn/scope.h"
12 #include "gn/settings.h"
13 #include "gn/value.h"
14 #include "gn/value_extractors.h"
15 #include "gn/variables.h"
16
17 namespace {
18
GetStringList(Scope * scope,const char * var_name,ConfigValues * config_values,std::vector<std::string> & (ConfigValues::* accessor)(),Err * err)19 void GetStringList(Scope* scope,
20 const char* var_name,
21 ConfigValues* config_values,
22 std::vector<std::string>& (ConfigValues::*accessor)(),
23 Err* err) {
24 const Value* value = scope->GetValue(var_name, true);
25 if (!value)
26 return; // No value, empty input and succeed.
27
28 ExtractListOfStringValues(*value, &(config_values->*accessor)(), err);
29 }
30
GetDirList(Scope * scope,const char * var_name,ConfigValues * config_values,const SourceDir input_dir,std::vector<SourceDir> & (ConfigValues::* accessor)(),Err * err)31 void GetDirList(Scope* scope,
32 const char* var_name,
33 ConfigValues* config_values,
34 const SourceDir input_dir,
35 std::vector<SourceDir>& (ConfigValues::*accessor)(),
36 Err* err) {
37 const Value* value = scope->GetValue(var_name, true);
38 if (!value)
39 return; // No value, empty input and succeed.
40
41 std::vector<SourceDir> result;
42 ExtractListOfRelativeDirs(scope->settings()->build_settings(), *value,
43 input_dir, &result, err);
44 (config_values->*accessor)().swap(result);
45 }
46
47 } // namespace
48
ConfigValuesGenerator(ConfigValues * dest_values,Scope * scope,const SourceDir & input_dir,Err * err)49 ConfigValuesGenerator::ConfigValuesGenerator(ConfigValues* dest_values,
50 Scope* scope,
51 const SourceDir& input_dir,
52 Err* err)
53 : config_values_(dest_values),
54 scope_(scope),
55 input_dir_(input_dir),
56 err_(err) {}
57
58 ConfigValuesGenerator::~ConfigValuesGenerator() = default;
59
Run()60 void ConfigValuesGenerator::Run() {
61 #define FILL_STRING_CONFIG_VALUE(name) \
62 GetStringList(scope_, #name, config_values_, &ConfigValues::name, err_);
63 #define FILL_DIR_CONFIG_VALUE(name) \
64 GetDirList(scope_, #name, config_values_, input_dir_, &ConfigValues::name, \
65 err_);
66
67 FILL_STRING_CONFIG_VALUE(arflags)
68 FILL_STRING_CONFIG_VALUE(asmflags)
69 FILL_STRING_CONFIG_VALUE(cflags)
70 FILL_STRING_CONFIG_VALUE(cflags_c)
71 FILL_STRING_CONFIG_VALUE(cflags_cc)
72 FILL_STRING_CONFIG_VALUE(cflags_objc)
73 FILL_STRING_CONFIG_VALUE(cflags_objcc)
74 FILL_STRING_CONFIG_VALUE(defines)
75 FILL_DIR_CONFIG_VALUE(framework_dirs)
76 FILL_DIR_CONFIG_VALUE(include_dirs)
77 FILL_STRING_CONFIG_VALUE(ldflags)
78 FILL_DIR_CONFIG_VALUE(lib_dirs)
79 FILL_STRING_CONFIG_VALUE(rustflags)
80 FILL_STRING_CONFIG_VALUE(rustenv)
81
82 #undef FILL_STRING_CONFIG_VALUE
83 #undef FILL_DIR_CONFIG_VALUE
84
85 // Inputs
86 const Value* inputs_value = scope_->GetValue(variables::kInputs, true);
87 if (inputs_value) {
88 ExtractListOfRelativeFiles(scope_->settings()->build_settings(),
89 *inputs_value, input_dir_,
90 &config_values_->inputs(), err_);
91 }
92
93 // Libs
94 const Value* libs_value = scope_->GetValue(variables::kLibs, true);
95 if (libs_value) {
96 ExtractListOfLibs(scope_->settings()->build_settings(), *libs_value,
97 input_dir_, &config_values_->libs(), err_);
98 }
99
100 // Externs
101 const Value* externs_value = scope_->GetValue(variables::kExterns, true);
102 if (externs_value) {
103 ExtractListOfExterns(scope_->settings()->build_settings(), *externs_value,
104 input_dir_, &config_values_->externs(), err_);
105 }
106
107 // Frameworks
108 const Value* frameworks_value =
109 scope_->GetValue(variables::kFrameworks, true);
110 if (frameworks_value) {
111 std::vector<std::string> frameworks;
112 if (!ExtractListOfStringValues(*frameworks_value, &frameworks, err_))
113 return;
114
115 // All strings must end with ".frameworks".
116 for (const std::string& framework : frameworks) {
117 std::string_view framework_name = GetFrameworkName(framework);
118 if (framework_name.empty()) {
119 *err_ = Err(*frameworks_value,
120 "This frameworks value is wrong."
121 "All listed frameworks names must not include any\n"
122 "path component and have \".framework\" extension.");
123 return;
124 }
125 }
126
127 config_values_->frameworks().swap(frameworks);
128 }
129
130 // Precompiled headers.
131 const Value* precompiled_header_value =
132 scope_->GetValue(variables::kPrecompiledHeader, true);
133 if (precompiled_header_value) {
134 if (!precompiled_header_value->VerifyTypeIs(Value::STRING, err_))
135 return;
136
137 // Check for common errors. This is a string and not a file.
138 const std::string& pch_string = precompiled_header_value->string_value();
139 if (base::StartsWith(pch_string, "//", base::CompareCase::SENSITIVE)) {
140 *err_ = Err(
141 *precompiled_header_value, "This precompiled_header value is wrong.",
142 "You need to specify a string that the compiler will match against\n"
143 "the #include lines rather than a GN-style file name.\n");
144 return;
145 }
146 config_values_->set_precompiled_header(pch_string);
147 }
148
149 const Value* precompiled_source_value =
150 scope_->GetValue(variables::kPrecompiledSource, true);
151 if (precompiled_source_value) {
152 config_values_->set_precompiled_source(input_dir_.ResolveRelativeFile(
153 *precompiled_source_value, err_,
154 scope_->settings()->build_settings()->root_path_utf8()));
155 if (err_->has_error())
156 return;
157 }
158 }
159