summaryrefslogtreecommitdiffstats
path: root/tools/gn/gyp_target_writer.cc
blob: 0f362d4bd6046eb997e9f82fcb1b98d1f75682ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/gyp_target_writer.h"

#include <iostream>

#include "base/file_util.h"
#include "base/files/file_path.h"
#include "tools/gn/build_settings.h"
#include "tools/gn/builder_record.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/gyp_binary_target_writer.h"
#include "tools/gn/gyp_script_target_writer.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/settings.h"
#include "tools/gn/target.h"
#include "tools/gn/toolchain.h"

GypTargetWriter::TargetGroup::TargetGroup()
    : debug(NULL),
      release(NULL),
      host_debug(NULL),
      host_release(NULL),
      debug64(NULL),
      release64(NULL),
      xcode_debug(NULL),
      xcode_release(NULL),
      xcode_host_debug(NULL),
      xcode_host_release(NULL) {
}

GypTargetWriter::GypTargetWriter(const Target* target,
                                 const Toolchain* toolchain,
                                 const SourceDir& gyp_dir,
                                 std::ostream& out)
    : settings_(target->settings()),
      target_(target),
      toolchain_(toolchain),
      gyp_dir_(gyp_dir),
      out_(out),
      // All GYP paths are relative to GYP file.
      path_output_(gyp_dir, ESCAPE_JSON, false) {
}

GypTargetWriter::~GypTargetWriter() {
}

// static
void GypTargetWriter::WriteFile(const SourceFile& gyp_file,
                                const std::vector<TargetGroup>& targets,
                                const Toolchain* debug_toolchain,
                                Err* err) {
  if (targets.empty())
    return;
  const Settings* debug_settings =
      targets[0].debug->item()->AsTarget()->settings();
  const BuildSettings* debug_build_settings = debug_settings->build_settings();

  base::FilePath gyp_file_path = debug_build_settings->GetFullPath(gyp_file);
  base::CreateDirectory(gyp_file_path.DirName());

  std::stringstream file;
  file << "# Generated by GN. Do not edit.\n\n";
  file << "{\n";

  // Optional GYP header specified in the default debug toolchain.
  if (!debug_toolchain->gyp_header().empty())
    file << debug_toolchain->gyp_header() << std::endl;

  file << "  'skip_includes': 1,\n";
  file << "  'targets': [\n";

  for (size_t i = 0; i < targets.size(); i++) {
    const Target* cur = targets[i].debug->item()->AsTarget();
    switch (cur->output_type()) {
      case Target::COPY_FILES:
        break;  // TODO(brettw)
      case Target::CUSTOM: {
        GypScriptTargetWriter writer(targets[i], debug_toolchain,
                                     gyp_file.GetDir(), file);
        writer.Run();
        break;
      }
      case Target::GROUP:
        break;  // TODO(brettw)
      case Target::EXECUTABLE:
      case Target::STATIC_LIBRARY:
      case Target::SHARED_LIBRARY:
      case Target::SOURCE_SET: {
        GypBinaryTargetWriter writer(targets[i], debug_toolchain,
                                     gyp_file.GetDir(), file);
        writer.Run();
        break;
      }
      default:
        CHECK(0);
    }
  }

  file << "  ],\n}\n";

  std::string contents = file.str();
  base::WriteFile(gyp_file_path, contents.c_str(),
                       static_cast<int>(contents.size()));
}

std::ostream& GypTargetWriter::Indent(int spaces) {
  return Indent(out_, spaces);
}

// static
std::ostream& GypTargetWriter::Indent(std::ostream& out, int spaces) {
  const char kSpaces[81] =
      "                                        "
      "                                        ";
  CHECK(static_cast<size_t>(spaces) <= arraysize(kSpaces) - 1);
  out.write(kSpaces, spaces);
  return out;
}