• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetFrameworksList(Scope * scope,const char * var_name,ConfigValues * config_values,std::vector<std::string> & (ConfigValues::* accessor)(),Err * err)47 void GetFrameworksList(Scope* scope,
48                        const char* var_name,
49                        ConfigValues* config_values,
50                        std::vector<std::string>& (ConfigValues::*accessor)(),
51                        Err* err) {
52   const Value* value = scope->GetValue(var_name, true);
53   if (!value)
54     return;
55 
56   std::vector<std::string> frameworks;
57   if (!ExtractListOfStringValues(*value, &frameworks, err))
58     return;
59 
60   // All strings must end with ".frameworks".
61   for (const std::string& framework : frameworks) {
62     std::string_view framework_name = GetFrameworkName(framework);
63     if (framework_name.empty()) {
64       *err = Err(*value,
65                  "This frameworks value is wrong. "
66                  "All listed frameworks names must not include any\n"
67                  "path component and have \".framework\" extension.");
68       return;
69     }
70   }
71 
72   (config_values->*accessor)().swap(frameworks);
73 }
74 
75 }  // namespace
76 
ConfigValuesGenerator(ConfigValues * dest_values,Scope * scope,const SourceDir & input_dir,Err * err)77 ConfigValuesGenerator::ConfigValuesGenerator(ConfigValues* dest_values,
78                                              Scope* scope,
79                                              const SourceDir& input_dir,
80                                              Err* err)
81     : config_values_(dest_values),
82       scope_(scope),
83       input_dir_(input_dir),
84       err_(err) {}
85 
86 ConfigValuesGenerator::~ConfigValuesGenerator() = default;
87 
Run()88 void ConfigValuesGenerator::Run() {
89 #define FILL_STRING_CONFIG_VALUE(name) \
90   GetStringList(scope_, #name, config_values_, &ConfigValues::name, err_);
91 #define FILL_DIR_CONFIG_VALUE(name)                                          \
92   GetDirList(scope_, #name, config_values_, input_dir_, &ConfigValues::name, \
93              err_);
94 
95   FILL_STRING_CONFIG_VALUE(arflags)
96   FILL_STRING_CONFIG_VALUE(asmflags)
97   FILL_STRING_CONFIG_VALUE(cflags)
98   FILL_STRING_CONFIG_VALUE(cflags_c)
99   FILL_STRING_CONFIG_VALUE(cflags_cc)
100   FILL_STRING_CONFIG_VALUE(cflags_objc)
101   FILL_STRING_CONFIG_VALUE(cflags_objcc)
102   FILL_STRING_CONFIG_VALUE(defines)
103   FILL_DIR_CONFIG_VALUE(framework_dirs)
104   FILL_DIR_CONFIG_VALUE(include_dirs)
105   FILL_STRING_CONFIG_VALUE(ldflags)
106   FILL_DIR_CONFIG_VALUE(lib_dirs)
107   FILL_STRING_CONFIG_VALUE(rustflags)
108   FILL_STRING_CONFIG_VALUE(rustenv)
109   FILL_STRING_CONFIG_VALUE(swiftflags)
110 
111 #undef FILL_STRING_CONFIG_VALUE
112 #undef FILL_DIR_CONFIG_VALUE
113 
114   // Inputs
115   const Value* inputs_value = scope_->GetValue(variables::kInputs, true);
116   if (inputs_value) {
117     ExtractListOfRelativeFiles(scope_->settings()->build_settings(),
118                                *inputs_value, input_dir_,
119                                &config_values_->inputs(), err_);
120   }
121 
122   // Libs
123   const Value* libs_value = scope_->GetValue(variables::kLibs, true);
124   if (libs_value) {
125     ExtractListOfLibs(scope_->settings()->build_settings(), *libs_value,
126                       input_dir_, &config_values_->libs(), err_);
127   }
128 
129   // Externs
130   const Value* externs_value = scope_->GetValue(variables::kExterns, true);
131   if (externs_value) {
132     ExtractListOfExterns(scope_->settings()->build_settings(), *externs_value,
133                          input_dir_, &config_values_->externs(), err_);
134   }
135 
136   // Frameworks
137   GetFrameworksList(scope_, variables::kFrameworks, config_values_,
138                     &ConfigValues::frameworks, err_);
139   GetFrameworksList(scope_, variables::kWeakFrameworks, config_values_,
140                     &ConfigValues::weak_frameworks, err_);
141 
142   // Precompiled headers.
143   const Value* precompiled_header_value =
144       scope_->GetValue(variables::kPrecompiledHeader, true);
145   if (precompiled_header_value) {
146     if (!precompiled_header_value->VerifyTypeIs(Value::STRING, err_))
147       return;
148 
149     // Check for common errors. This is a string and not a file.
150     const std::string& pch_string = precompiled_header_value->string_value();
151     if (base::StartsWith(pch_string, "//", base::CompareCase::SENSITIVE)) {
152       *err_ = Err(
153           *precompiled_header_value, "This precompiled_header value is wrong. ",
154           "You need to specify a string that the compiler will match against\n"
155           "the #include lines rather than a GN-style file name.\n");
156       return;
157     }
158     config_values_->set_precompiled_header(pch_string);
159   }
160 
161   const Value* precompiled_source_value =
162       scope_->GetValue(variables::kPrecompiledSource, true);
163   if (precompiled_source_value) {
164     config_values_->set_precompiled_source(input_dir_.ResolveRelativeFile(
165         *precompiled_source_value, err_,
166         scope_->settings()->build_settings()->root_path_utf8()));
167     if (err_->has_error())
168       return;
169   }
170 }
171