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.
5 #include "tools/gn/gyp_helper.h"
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"
14 GypHelper::GypHelper() {
17 GypHelper::~GypHelper() {
20 SourceFile
GypHelper::GetGypFileForTarget(const Target
* target
,
22 // Use the manually specified one if its there.
23 if (!target
->gyp_file().is_null())
24 return target
->gyp_file();
26 // Otherwise inherit the directory name.
27 const std::string
& dir
= target
->label().dir().value();
28 size_t last_slash
= dir
.size() - 1;
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);
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.");
44 return SourceFile(dir
+ dir_name
+ ".gyp");
47 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();
54 std::string
GypHelper::GetFullRefForTarget(const Target
* target
) const {
56 SourceFile gypfile
= GetGypFileForTarget(target
, &err
);
57 CHECK(gypfile
.is_source_absolute()) <<
58 "GYP files should not be system-absolute: " << gypfile
.value();
60 std::string
ret("<(DEPTH)/");
61 // Trim off the leading double-slash.
62 ret
.append(&gypfile
.value()[2], gypfile
.value().size() - 2);
64 ret
.append(GetNameForTarget(target
));
69 std::string
GypHelper::GetFileReference(const SourceFile
& file
) const {
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()));
80 // Source root relative, strip the "//".
81 ret
.assign("<(DEPTH)/");
82 ret
.append(&file
.value()[2], file
.value().size() - 2);
86 std::string
GypHelper::GetDirReference(const SourceDir
& dir
,
87 bool include_slash
) const {
92 if (dir
.is_system_absolute()) {
93 ret
= FilePathToUTF8(dir
.Resolve(base::FilePath()));
95 ret
.assign("<(DEPTH)/");
96 ret
.append(&dir
.value()[2], dir
.value().size() - 2);
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);