summaryrefslogtreecommitdiffstats
path: root/tools/gn/gyp_target_writer.cc
blob: 725dbdc7cd18afda791aa2b0684cb8d10cebdda2 (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
// 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"

GypTargetWriter::GypTargetWriter(const Target* target,
                                 const SourceDir& gyp_dir,
                                 std::ostream& out)
    : settings_(target->settings()),
      target_(target),
      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,
                                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);
  file_util::CreateDirectory(gyp_file_path.DirName());

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

  if (debug_settings->IsMac()) {
    // Global settings for make/ninja. This must match common.gypi :(
    file << "  'make_global_settings': [\n";
    file << "    ['CC', 'third_party/llvm-build/Release+Asserts/bin/clang'],\n";
    file <<
        "    ['CXX', 'third_party/llvm-build/Release+Asserts/bin/clang++'],\n";
    file << "    ['CC.host', '$(CC)'],\n";
    file << "    ['CXX.host', '$(CXX)'],\n";
    file << "  ],\n";
  }
  // TODO(brettw) android.

  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], 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], gyp_file.GetDir(), file);
        writer.Run();
        break;
      }
      default:
        CHECK(0);
    }
  }

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

  std::string contents = file.str();
  file_util::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;
}