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_target_writer.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "tools/gn/build_settings.h"
12 #include "tools/gn/builder_record.h"
13 #include "tools/gn/filesystem_utils.h"
14 #include "tools/gn/gyp_binary_target_writer.h"
15 #include "tools/gn/gyp_script_target_writer.h"
16 #include "tools/gn/scheduler.h"
17 #include "tools/gn/settings.h"
18 #include "tools/gn/target.h"
19 #include "tools/gn/toolchain.h"
21 GypTargetWriter::GypTargetWriter(const Target* target,
22 const Toolchain* toolchain,
23 const SourceDir& gyp_dir,
25 : settings_(target->settings()),
27 toolchain_(toolchain),
30 // All GYP paths are relative to GYP file.
31 path_output_(gyp_dir, ESCAPE_JSON, false) {
34 GypTargetWriter::~GypTargetWriter() {
38 void GypTargetWriter::WriteFile(const SourceFile& gyp_file,
39 const std::vector<TargetGroup>& targets,
40 const Toolchain* debug_toolchain,
44 const Settings* debug_settings =
45 targets[0].debug->item()->AsTarget()->settings();
46 const BuildSettings* debug_build_settings = debug_settings->build_settings();
48 base::FilePath gyp_file_path = debug_build_settings->GetFullPath(gyp_file);
49 base::CreateDirectory(gyp_file_path.DirName());
51 std::stringstream file;
52 file << "# Generated by GN. Do not edit.\n\n";
55 // Optional GYP header specified in the default debug toolchain.
56 if (!debug_toolchain->gyp_header().empty())
57 file << debug_toolchain->gyp_header() << std::endl;
59 file << " 'skip_includes': 1,\n";
60 file << " 'targets': [\n";
62 for (size_t i = 0; i < targets.size(); i++) {
63 const Target* cur = targets[i].debug->item()->AsTarget();
64 switch (cur->output_type()) {
65 case Target::COPY_FILES:
66 break; // TODO(brettw)
67 case Target::CUSTOM: {
68 GypScriptTargetWriter writer(targets[i], debug_toolchain,
69 gyp_file.GetDir(), file);
74 break; // TODO(brettw)
75 case Target::EXECUTABLE:
76 case Target::STATIC_LIBRARY:
77 case Target::SHARED_LIBRARY:
78 case Target::SOURCE_SET: {
79 GypBinaryTargetWriter writer(targets[i], debug_toolchain,
80 gyp_file.GetDir(), file);
91 std::string contents = file.str();
92 file_util::WriteFile(gyp_file_path, contents.c_str(),
93 static_cast<int>(contents.size()));
96 std::ostream& GypTargetWriter::Indent(int spaces) {
97 return Indent(out_, spaces);
101 std::ostream& GypTargetWriter::Indent(std::ostream& out, int spaces) {
102 const char kSpaces[81] =
105 CHECK(static_cast<size_t>(spaces) <= arraysize(kSpaces) - 1);
106 out.write(kSpaces, spaces);