Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / gn / gyp_target_writer.cc
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_target_writer.h"
6
7 #include <iostream>
8
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"
20
21 GypTargetWriter::TargetGroup::TargetGroup()
22     : debug(NULL),
23       release(NULL),
24       host_debug(NULL),
25       host_release(NULL),
26       debug64(NULL),
27       release64(NULL),
28       xcode_debug(NULL),
29       xcode_release(NULL),
30       xcode_host_debug(NULL),
31       xcode_host_release(NULL) {
32 }
33
34 GypTargetWriter::GypTargetWriter(const Target* target,
35                                  const Toolchain* toolchain,
36                                  const SourceDir& gyp_dir,
37                                  std::ostream& out)
38     : settings_(target->settings()),
39       target_(target),
40       toolchain_(toolchain),
41       gyp_dir_(gyp_dir),
42       out_(out),
43       // All GYP paths are relative to GYP file.
44       path_output_(gyp_dir, ESCAPE_JSON, false) {
45 }
46
47 GypTargetWriter::~GypTargetWriter() {
48 }
49
50 // static
51 void GypTargetWriter::WriteFile(const SourceFile& gyp_file,
52                                 const std::vector<TargetGroup>& targets,
53                                 const Toolchain* debug_toolchain,
54                                 Err* err) {
55   if (targets.empty())
56     return;
57   const Settings* debug_settings =
58       targets[0].debug->item()->AsTarget()->settings();
59   const BuildSettings* debug_build_settings = debug_settings->build_settings();
60
61   base::FilePath gyp_file_path = debug_build_settings->GetFullPath(gyp_file);
62   base::CreateDirectory(gyp_file_path.DirName());
63
64   std::stringstream file;
65   file << "# Generated by GN. Do not edit.\n\n";
66   file << "{\n";
67
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;
71
72   file << "  'skip_includes': 1,\n";
73   file << "  'targets': [\n";
74
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);
83         writer.Run();
84         break;
85       }
86       case Target::GROUP:
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);
94         writer.Run();
95         break;
96       }
97       default:
98         CHECK(0);
99     }
100   }
101
102   file << "  ],\n}\n";
103
104   std::string contents = file.str();
105   file_util::WriteFile(gyp_file_path, contents.c_str(),
106                        static_cast<int>(contents.size()));
107 }
108
109 std::ostream& GypTargetWriter::Indent(int spaces) {
110   return Indent(out_, spaces);
111 }
112
113 // static
114 std::ostream& GypTargetWriter::Indent(std::ostream& out, int spaces) {
115   const char kSpaces[81] =
116       "                                        "
117       "                                        ";
118   CHECK(static_cast<size_t>(spaces) <= arraysize(kSpaces) - 1);
119   out.write(kSpaces, spaces);
120   return out;
121 }