Home | History | Annotate | Download | only in gn
      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::GypTargetWriter(const Target* target,
     22                                  const Toolchain* toolchain,
     23                                  const SourceDir& gyp_dir,
     24                                  std::ostream& out)
     25     : settings_(target->settings()),
     26       target_(target),
     27       toolchain_(toolchain),
     28       gyp_dir_(gyp_dir),
     29       out_(out),
     30       // All GYP paths are relative to GYP file.
     31       path_output_(gyp_dir, ESCAPE_JSON, false) {
     32 }
     33 
     34 GypTargetWriter::~GypTargetWriter() {
     35 }
     36 
     37 // static
     38 void GypTargetWriter::WriteFile(const SourceFile& gyp_file,
     39                                 const std::vector<TargetGroup>& targets,
     40                                 const Toolchain* debug_toolchain,
     41                                 Err* err) {
     42   if (targets.empty())
     43     return;
     44   const Settings* debug_settings =
     45       targets[0].debug->item()->AsTarget()->settings();
     46   const BuildSettings* debug_build_settings = debug_settings->build_settings();
     47 
     48   base::FilePath gyp_file_path = debug_build_settings->GetFullPath(gyp_file);
     49   base::CreateDirectory(gyp_file_path.DirName());
     50 
     51   std::stringstream file;
     52   file << "# Generated by GN. Do not edit.\n\n";
     53   file << "{\n";
     54 
     55   // Optional GYP header specified in the default debug toolchain.
     56   if (!debug_toolchain->gyp_header().empty())
     57     file << debug_toolchain->gyp_header() << std::endl;
     58 
     59   file << "  'skip_includes': 1,\n";
     60   file << "  'targets': [\n";
     61 
     62   for (size_t i = 0; i < targets.size(); i++) {
     63     const Target* cur = targets[i].debug->item()->AsTarget();
     64     switch (cur->output_type()) {
     65       case Target::COPY_FILES:
     66         break;  // TODO(brettw)
     67       case Target::CUSTOM: {
     68         GypScriptTargetWriter writer(targets[i], debug_toolchain,
     69                                      gyp_file.GetDir(), file);
     70         writer.Run();
     71         break;
     72       }
     73       case Target::GROUP:
     74         break;  // TODO(brettw)
     75       case Target::EXECUTABLE:
     76       case Target::STATIC_LIBRARY:
     77       case Target::SHARED_LIBRARY:
     78       case Target::SOURCE_SET: {
     79         GypBinaryTargetWriter writer(targets[i], debug_toolchain,
     80                                      gyp_file.GetDir(), file);
     81         writer.Run();
     82         break;
     83       }
     84       default:
     85         CHECK(0);
     86     }
     87   }
     88 
     89   file << "  ],\n}\n";
     90 
     91   std::string contents = file.str();
     92   file_util::WriteFile(gyp_file_path, contents.c_str(),
     93                        static_cast<int>(contents.size()));
     94 }
     95 
     96 std::ostream& GypTargetWriter::Indent(int spaces) {
     97   return Indent(out_, spaces);
     98 }
     99 
    100 // static
    101 std::ostream& GypTargetWriter::Indent(std::ostream& out, int spaces) {
    102   const char kSpaces[81] =
    103       "                                        "
    104       "                                        ";
    105   CHECK(static_cast<size_t>(spaces) <= arraysize(kSpaces) - 1);
    106   out.write(kSpaces, spaces);
    107   return out;
    108 }
    109