ninja_target_writer.h revision d3868032626d59662ff73b372b5d584c1d144c53
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#ifndef TOOLS_GN_NINJA_TARGET_WRITER_H_
6#define TOOLS_GN_NINJA_TARGET_WRITER_H_
7
8#include <iosfwd>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/files/file_path.h"
13#include "tools/gn/filesystem_utils.h"
14#include "tools/gn/ninja_helper.h"
15#include "tools/gn/path_output.h"
16#include "tools/gn/settings.h"
17
18class Target;
19
20// Generates one target's ".ninja" file. The toplevel "build.ninja" file is
21// generated by the NinjaBuildGenerator.
22class NinjaTargetWriter {
23 public:
24  NinjaTargetWriter(const Target* target, std::ostream& out);
25  ~NinjaTargetWriter();
26
27  void Run();
28
29  static void RunAndWriteFile(const Target* target);
30
31 private:
32  void WriteCopyRules();
33
34  void WriteCustomRules();
35  void WriteCustomArg(const std::string& arg);
36
37  // Writs the rules for compiling each source, writing all output files
38  // to the given vector.
39  //
40  // common_deps is a precomputed string of all ninja files that are common
41  // to each build step. This is added to each one.
42  void WriteCustomSourceRules(const std::string& custom_rule_name,
43                              const std::string& common_deps,
44                              const SourceDir& script_cd,
45                              const std::string& script_cd_to_root,
46                              std::vector<OutputFile>* output_files);
47
48  void WriteCompilerVars();
49  void WriteSources(std::vector<OutputFile>* object_files);
50  void WriteLinkerStuff(const std::vector<OutputFile>& object_files);
51
52  // Returns NULL if the source type should not be compiled on this target.
53  const char* GetCommandForSourceType(SourceFileType type) const;
54
55  const char* GetCommandForTargetType() const;
56
57  const Settings* settings_;  // Non-owning.
58  const Target* target_;  // Non-owning.
59  std::ostream& out_;
60  PathOutput path_output_;
61
62  NinjaHelper helper_;
63
64  DISALLOW_COPY_AND_ASSIGN(NinjaTargetWriter);
65};
66
67#endif  // TOOLS_GN_NINJA_TARGET_WRITER_H_
68