cmLocalNinjaGenerator.h 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2011 Peter Collingbourne <peter@pcc.me.uk>
  Copyright 2011 Nicolas Despres <nicolas.despres@gmail.com>

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#ifndef cmLocalNinjaGenerator_h
#  define cmLocalNinjaGenerator_h

#  include "cmLocalGenerator.h"
#  include "cmNinjaTypes.h"

19
class cmCustomCommandGenerator;
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
class cmGlobalNinjaGenerator;
class cmGeneratedFileStream;
class cmake;

/**
 * \class cmLocalNinjaGenerator
 * \brief Write a local build.ninja file.
 *
 * cmLocalNinjaGenerator produces a local build.ninja file from its
 * member Makefile.
 */
class cmLocalNinjaGenerator : public cmLocalGenerator
{
public:
  /// Default constructor.
  cmLocalNinjaGenerator();

  /// Destructor.
  virtual ~cmLocalNinjaGenerator();

  /// Overloaded methods. @see cmLocalGenerator::Generate()
  virtual void Generate();

  /// Overloaded methods. @see cmLocalGenerator::Configure()
  virtual void Configure();

  /// Overloaded methods. @see cmLocalGenerator::GetTargetDirectory()
  virtual std::string GetTargetDirectory(cmTarget const& target) const;

  const cmGlobalNinjaGenerator* GetGlobalNinjaGenerator() const;
  cmGlobalNinjaGenerator* GetGlobalNinjaGenerator();

  /**
   * Shortcut to get the cmake instance throw the global generator.
   * @return an instance of the cmake object.
   */
  const cmake* GetCMakeInstance() const;
  cmake* GetCMakeInstance();

59 60
  std::string const& GetConfigName() const
  { return this->ConfigName; }
61 62 63 64 65 66 67 68 69 70 71 72 73

  /// @return whether we are processing the top CMakeLists.txt file.
  bool isRootMakefile() const;

  /// @returns the relative path between the HomeOutputDirectory and this
  /// local generators StartOutputDirectory.
  std::string GetHomeRelativeOutputPath() const
  { return this->HomeRelativeOutputPath; }

  std::string ConvertToNinjaPath(const char *path);

  struct map_to_ninja_path {
    cmLocalNinjaGenerator *LocalGenerator;
74 75
    map_to_ninja_path(cmLocalNinjaGenerator *LocalGen)
      : LocalGenerator(LocalGen) {}
76 77 78 79
    std::string operator()(const std::string &path) {
      return LocalGenerator->ConvertToNinjaPath(path.c_str());
    }
  };
80

81 82 83 84
  map_to_ninja_path MapToNinjaPath() {
    return map_to_ninja_path(this);
  }

85 86
  void ExpandRuleVariables(std::string& string,
                           const RuleVariables& replaceValues) {
87 88
    cmLocalGenerator::ExpandRuleVariables(string, replaceValues);
  }
89 90 91

  std::string BuildCommandLine(const std::vector<std::string> &cmdLines);

92 93 94
  void AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs);
  void AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs);

95
  void AddCustomCommandTarget(cmCustomCommand const* cc, cmTarget* target);
96
  void AppendCustomCommandLines(cmCustomCommandGenerator const& ccg,
97
                                std::vector<std::string> &cmdLines);
98
  void AppendCustomCommandDeps(cmCustomCommandGenerator const& ccg,
99 100
                               cmNinjaDeps &ninjaDeps);

101 102
  virtual std::string ConvertToLinkReference(std::string const& lib,
                                             OutputFormat format = SHELL);
103 104 105


protected:
106 107
  virtual std::string ConvertToIncludeReference(std::string const& path,
                                                OutputFormat format = SHELL);
108 109 110 111 112 113 114 115 116 117


private:
  cmGeneratedFileStream& GetBuildFileStream() const;
  cmGeneratedFileStream& GetRulesFileStream() const;

  void WriteBuildFileTop();
  void WriteProjectHeader(std::ostream& os);
  void WriteNinjaFilesInclusion(std::ostream& os);
  void WriteProcessedMakefile(std::ostream& os);
118
  void WritePools(std::ostream& os);
119 120 121

  void SetConfigName();

122
  void WriteCustomCommandRule();
123
  void WriteCustomCommandBuildStatement(cmCustomCommand const *cc,
124 125 126 127
                                        const cmNinjaDeps& orderOnlyDeps);

  void WriteCustomCommandBuildStatements();

128
  std::string MakeCustomLauncher(cmCustomCommandGenerator const& ccg);
129

130 131 132
  std::string ConfigName;
  std::string HomeRelativeOutputPath;

133
  typedef std::map<cmCustomCommand const*, std::set<cmTarget*> >
134 135 136 137 138
    CustomCommandTargetMap;
  CustomCommandTargetMap CustomCommandTargets;
};

#endif // ! cmLocalNinjaGenerator_h