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::TargetGroup::TargetGroup()
30 xcode_host_debug(NULL),
31 xcode_host_release(NULL) {
34 GypTargetWriter::GypTargetWriter(const Target* target,
35 const Toolchain* toolchain,
36 const SourceDir& gyp_dir,
38 : settings_(target->settings()),
40 toolchain_(toolchain),
43 // All GYP paths are relative to GYP file.
44 path_output_(gyp_dir, ESCAPE_JSON, false) {
47 GypTargetWriter::~GypTargetWriter() {
51 void GypTargetWriter::WriteFile(const SourceFile& gyp_file,
52 const std::vector<TargetGroup>& targets,
53 const Toolchain* debug_toolchain,
57 const Settings* debug_settings =
58 targets[0].debug->item()->AsTarget()->settings();
59 const BuildSettings* debug_build_settings = debug_settings->build_settings();
61 base::FilePath gyp_file_path = debug_build_settings->GetFullPath(gyp_file);
62 base::CreateDirectory(gyp_file_path.DirName());
64 std::stringstream file;
65 file << "# Generated by GN. Do not edit.\n\n";
68 // Optional GYP header specified in the default debug toolchain.
69 if (!debug_toolchain->gyp_header().empty())
70 file << debug_toolchain->gyp_header() << std::endl;
72 file << " 'skip_includes': 1,\n";
73 file << " 'targets': [\n";
75 for (size_t i = 0; i < targets.size(); i++) {
76 const Target* cur = targets[i].debug->item()->AsTarget();
77 switch (cur->output_type()) {
78 case Target::COPY_FILES:
79 break; // TODO(brettw)
80 case Target::CUSTOM: {
81 GypScriptTargetWriter writer(targets[i], debug_toolchain,
82 gyp_file.GetDir(), file);
87 break; // TODO(brettw)
88 case Target::EXECUTABLE:
89 case Target::STATIC_LIBRARY:
90 case Target::SHARED_LIBRARY:
91 case Target::SOURCE_SET: {
92 GypBinaryTargetWriter writer(targets[i], debug_toolchain,
93 gyp_file.GetDir(), file);
104 std::string contents = file.str();
105 file_util::WriteFile(gyp_file_path, contents.c_str(),
106 static_cast<int>(contents.size()));
109 std::ostream& GypTargetWriter::Indent(int spaces) {
110 return Indent(out_, spaces);
114 std::ostream& GypTargetWriter::Indent(std::ostream& out, int spaces) {
115 const char kSpaces[81] =
118 CHECK(static_cast<size_t>(spaces) <= arraysize(kSpaces) - 1);
119 out.write(kSpaces, spaces);