- add sources.
[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/filesystem_utils.h"
13 #include "tools/gn/gyp_binary_target_writer.h"
14 #include "tools/gn/scheduler.h"
15 #include "tools/gn/settings.h"
16 #include "tools/gn/target.h"
17
18 GypTargetWriter::GypTargetWriter(const Target* target, std::ostream& out)
19     : settings_(target->settings()),
20       target_(target),
21       out_(out) {
22 }
23
24 GypTargetWriter::~GypTargetWriter() {
25 }
26
27 // static
28 void GypTargetWriter::WriteFile(const SourceFile& gyp_file,
29                                 const std::vector<TargetGroup>& targets,
30                                 Err* err) {
31   if (targets.empty())
32     return;
33   const BuildSettings* debug_build_settings =
34       targets[0].debug->settings()->build_settings();
35
36   std::stringstream file;
37   file << "# Generated by GN. Do not edit.\n\n";
38   file << "{\n";
39   file << "  'skip_includes': 1,\n";
40
41   if (targets[0].debug->settings()->IsMac()) {
42     // Global settings for make/ninja. This must match common.gypi :(
43     file << "  'make_global_settings': [\n";
44     file << "    ['CC', 'third_party/llvm-build/Release+Asserts/bin/clang'],\n";
45     file <<
46         "    ['CXX', 'third_party/llvm-build/Release+Asserts/bin/clang++'],\n";
47     file << "    ['CC.host', '$(CC)'],\n";
48     file << "    ['CXX.host', '$(CXX)'],\n";
49     file << "  ],\n";
50   }
51   // TODO(brettw) android.
52
53   file << "  'targets': [\n";
54
55   for (size_t i = 0; i < targets.size(); i++) {
56     switch (targets[i].debug->output_type()) {
57       case Target::COPY_FILES:
58       case Target::CUSTOM:
59       case Target::GROUP:
60         break;  // TODO(brettw)
61       case Target::EXECUTABLE:
62       case Target::STATIC_LIBRARY:
63       case Target::SHARED_LIBRARY:
64       case Target::SOURCE_SET: {
65         GypBinaryTargetWriter writer(targets[i], file);
66         writer.Run();
67         break;
68       }
69       default:
70         CHECK(0);
71     }
72   }
73
74   file << "  ],\n}\n";
75
76   base::FilePath gyp_file_path = debug_build_settings->GetFullPath(gyp_file);
77   std::string contents = file.str();
78   file_util::WriteFile(gyp_file_path, contents.c_str(),
79                        static_cast<int>(contents.size()));
80 }
81