• 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 "tools/gn/gyp_helper.h"
6 
7 #include "tools/gn/err.h"
8 #include "tools/gn/filesystem_utils.h"
9 #include "tools/gn/settings.h"
10 #include "tools/gn/source_file.h"
11 #include "tools/gn/target.h"
12 #include "tools/gn/toolchain.h"
13 
GypHelper()14 GypHelper::GypHelper() {
15 }
16 
~GypHelper()17 GypHelper::~GypHelper() {
18 }
19 
GetGypFileForTarget(const Target * target,Err * err) const20 SourceFile GypHelper::GetGypFileForTarget(const Target* target,
21                                           Err* err) const {
22   // Use the manually specified one if its there.
23   if (!target->gyp_file().is_null())
24     return target->gyp_file();
25 
26   // Otherwise inherit the directory name.
27   const std::string& dir = target->label().dir().value();
28   size_t last_slash = dir.size() - 1;
29   std::string dir_name;
30   if (last_slash > 0) {
31     size_t next_to_last_slash = dir.rfind('/', last_slash - 1);
32     if (next_to_last_slash != std::string::npos) {
33       dir_name = dir.substr(next_to_last_slash + 1,
34                             last_slash - next_to_last_slash - 1);
35     }
36   }
37   if (dir_name.empty()) {
38     *err = Err(Location(), "Need to specify a gyp_file value for " +
39         target->label().GetUserVisibleName(false) + "\n"
40         "since I can't make up one with no name.");
41     return SourceFile();
42   }
43 
44   return SourceFile(dir + dir_name + ".gyp");
45 }
46 
GetNameForTarget(const Target * target) const47 std::string GypHelper::GetNameForTarget(const Target* target) const {
48   if (target->settings()->is_default())
49     return target->label().name();  // Default toolchain has no suffix.
50   return target->label().name() + "_" +
51          target->settings()->toolchain_label().name();
52 }
53 
GetFullRefForTarget(const Target * target) const54 std::string GypHelper::GetFullRefForTarget(const Target* target) const {
55   Err err;
56   SourceFile gypfile = GetGypFileForTarget(target, &err);
57   CHECK(gypfile.is_source_absolute()) <<
58       "GYP files should not be system-absolute: " << gypfile.value();
59 
60   std::string ret("<(DEPTH)/");
61   // Trim off the leading double-slash.
62   ret.append(&gypfile.value()[2], gypfile.value().size() - 2);
63   ret.push_back(':');
64   ret.append(GetNameForTarget(target));
65 
66   return ret;
67 }
68 
GetFileReference(const SourceFile & file) const69 std::string GypHelper::GetFileReference(const SourceFile& file) const {
70   std::string ret;
71   if (file.is_null())
72     return ret;
73 
74   // Use FilePath's resolver to get the system paths out (on Windows this
75   // requires special handling). Since it's absolute, it doesn't matter what
76   // we pass in as the source root.
77   if (file.is_system_absolute())
78     return FilePathToUTF8(file.Resolve(base::FilePath()));
79 
80   // Source root relative, strip the "//".
81   ret.assign("<(DEPTH)/");
82   ret.append(&file.value()[2], file.value().size() - 2);
83   return ret;
84 }
85 
GetDirReference(const SourceDir & dir,bool include_slash) const86 std::string GypHelper::GetDirReference(const SourceDir& dir,
87                                        bool include_slash) const {
88   std::string ret;
89   if (dir.is_null())
90     return ret;
91 
92   if (dir.is_system_absolute()) {
93     ret = FilePathToUTF8(dir.Resolve(base::FilePath()));
94   } else {
95     ret.assign("<(DEPTH)/");
96     ret.append(&dir.value()[2], dir.value().size() - 2);
97   }
98 
99   // Optionally trim the slash off the end.
100   if (!ret.empty() && !include_slash &&
101       (ret[ret.size() - 1] == '/' || ret[ret.size() - 1] == '\\'))
102     ret.resize(ret.size() - 1);
103 
104   return ret;
105 }
106