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/ninja_toolchain_writer.h" 6 7 #include <fstream> 8 9 #include "base/file_util.h" 10 #include "base/strings/stringize_macros.h" 11 #include "tools/gn/build_settings.h" 12 #include "tools/gn/settings.h" 13 #include "tools/gn/target.h" 14 #include "tools/gn/toolchain.h" 15 16 NinjaToolchainWriter::NinjaToolchainWriter( 17 const Settings* settings, 18 const std::vector<const Target*>& targets, 19 std::ostream& out) 20 : settings_(settings), 21 targets_(targets), 22 out_(out), 23 path_output_(settings_->build_settings()->build_dir(), 24 ESCAPE_NINJA, true), 25 helper_(settings->build_settings()) { 26 } 27 28 NinjaToolchainWriter::~NinjaToolchainWriter() { 29 } 30 31 void NinjaToolchainWriter::Run() { 32 WriteRules(); 33 WriteSubninjas(); 34 } 35 36 // static 37 bool NinjaToolchainWriter::RunAndWriteFile( 38 const Settings* settings, 39 const std::vector<const Target*>& targets) { 40 NinjaHelper helper(settings->build_settings()); 41 base::FilePath ninja_file(settings->build_settings()->GetFullPath( 42 helper.GetNinjaFileForToolchain(settings).GetSourceFile( 43 settings->build_settings()))); 44 file_util::CreateDirectory(ninja_file.DirName()); 45 46 std::ofstream file; 47 file.open(FilePathToUTF8(ninja_file).c_str(), 48 std::ios_base::out | std::ios_base::binary); 49 if (file.fail()) 50 return false; 51 52 NinjaToolchainWriter gen(settings, targets, file); 53 gen.Run(); 54 return true; 55 } 56 57 void NinjaToolchainWriter::WriteRules() { 58 const Toolchain* tc = settings_->toolchain(); 59 std::string indent(" "); 60 61 for (int i = Toolchain::TYPE_NONE + 1; i < Toolchain::TYPE_NUMTYPES; i++) { 62 Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(i); 63 const Toolchain::Tool& tool = tc->GetTool(tool_type); 64 if (tool.empty()) 65 continue; 66 67 out_ << "rule " << Toolchain::ToolTypeToName(tool_type) << std::endl; 68 69 #define WRITE_ARG(name) \ 70 if (!tool.name.empty()) \ 71 out_ << indent << " " STRINGIZE(name) " = " << tool.name << std::endl; 72 WRITE_ARG(command); 73 WRITE_ARG(depfile); 74 WRITE_ARG(deps); 75 WRITE_ARG(description); 76 WRITE_ARG(pool); 77 WRITE_ARG(restat); 78 WRITE_ARG(rspfile); 79 WRITE_ARG(rspfile_content); 80 #undef WRITE_ARG 81 } 82 out_ << std::endl; 83 } 84 85 void NinjaToolchainWriter::WriteSubninjas() { 86 for (size_t i = 0; i < targets_.size(); i++) { 87 if (targets_[i]->output_type() != Target::NONE) { 88 out_ << "subninja "; 89 path_output_.WriteFile(out_, helper_.GetNinjaFileForTarget(targets_[i])); 90 out_ << std::endl; 91 } 92 } 93 out_ << std::endl; 94 } 95