cmake/Source/cmGlobalNinjaGenerator.h

477 lines
17 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2012-04-19 19:04:21 +03:00
#ifndef cmGlobalNinjaGenerator_h
2016-07-09 11:21:54 +02:00
#define cmGlobalNinjaGenerator_h
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <iosfwd>
#include <map>
#include <set>
#include <string>
2018-01-26 17:06:56 +01:00
#include <unordered_map>
2016-10-30 18:24:19 +01:00
#include <utility>
#include <vector>
2017-04-14 19:02:05 +02:00
#include "cmGlobalCommonGenerator.h"
#include "cmGlobalGenerator.h"
#include "cmGlobalGeneratorFactory.h"
#include "cmNinjaTypes.h"
#include "cmPolicies.h"
#include "cm_codecvt.hxx"
2016-10-30 18:24:19 +01:00
class cmCustomCommand;
2012-04-19 19:04:21 +03:00
class cmGeneratedFileStream;
class cmGeneratorTarget;
2017-04-14 19:02:05 +02:00
class cmLinkLineComputer;
2016-10-30 18:24:19 +01:00
class cmLocalGenerator;
2017-04-14 19:02:05 +02:00
class cmMakefile;
class cmOutputConverter;
class cmStateDirectory;
class cmake;
struct cmDocumentationEntry;
2012-04-19 19:04:21 +03:00
/**
* \class cmGlobalNinjaGenerator
* \brief Write a build.ninja file.
*
* The main differences between this generator and the UnixMakefile
* generator family are:
* - We don't care about VERBOSE variable or RULE_MESSAGES property since
* it is handle by Ninja's -v option.
* - We don't care about computing any progress status since Ninja manages
* it itself.
* - We don't care about generating a clean target since Ninja already have
* a clean tool.
* - We generate one build.ninja and one rules.ninja per project.
* - We try to minimize the number of generated rules: one per target and
* language.
* - We use Ninja special variable $in and $out to produce nice output.
* - We extensively use Ninja variable overloading system to minimize the
* number of generated rules.
*/
2015-11-17 17:22:37 +01:00
class cmGlobalNinjaGenerator : public cmGlobalCommonGenerator
2012-04-19 19:04:21 +03:00
{
public:
/// The default name of Ninja's build file. Typically: build.ninja.
static const char* NINJA_BUILD_FILE;
/// The default name of Ninja's rules file. Typically: rules.ninja.
/// It is included in the main build.ninja file.
static const char* NINJA_RULES_FILE;
/// The indentation string used when generating Ninja's build file.
static const char* INDENT;
2017-04-14 19:02:05 +02:00
/// The shell command used for a no-op.
static std::string const SHELL_NOOP;
2012-04-19 19:04:21 +03:00
/// Write @a count times INDENT level to output stream @a os.
static void Indent(std::ostream& os, int count);
/// Write a divider in the given output stream @a os.
static void WriteDivider(std::ostream& os);
2015-08-17 11:37:30 +02:00
static std::string EncodeRuleName(std::string const& name);
2016-07-09 11:21:54 +02:00
static std::string EncodeLiteral(const std::string& lit);
std::string EncodePath(const std::string& path);
2012-04-19 19:04:21 +03:00
2017-04-14 19:02:05 +02:00
cmLinkLineComputer* CreateLinkLineComputer(
cmOutputConverter* outputConverter,
2018-01-26 17:06:56 +01:00
cmStateDirectory const& stateDir) const override;
2017-04-14 19:02:05 +02:00
2012-04-19 19:04:21 +03:00
/**
* Write the given @a comment to the output stream @a os. It
* handles new line character properly.
*/
static void WriteComment(std::ostream& os, const std::string& comment);
2016-03-13 13:35:51 +01:00
/**
* Utilized by the generator factory to determine if this generator
* supports toolsets.
*/
static bool SupportsToolset() { return false; }
2016-10-30 18:24:19 +01:00
/**
* Utilized by the generator factory to determine if this generator
* supports platforms.
*/
static bool SupportsPlatform() { return false; }
2018-01-26 17:06:56 +01:00
bool IsIPOSupported() const override { return true; }
2017-07-20 19:35:53 +02:00
2012-04-19 19:04:21 +03:00
/**
* Write a build statement to @a os with the @a comment using
* the @a rule the list of @a outputs files and inputs.
* It also writes the variables bound to this build statement.
* @warning no escaping of any kind is done here.
*/
2016-07-09 11:21:54 +02:00
void WriteBuild(std::ostream& os, const std::string& comment,
const std::string& rule, const cmNinjaDeps& outputs,
2016-10-30 18:24:19 +01:00
const cmNinjaDeps& implicitOuts,
2013-11-03 12:27:13 +02:00
const cmNinjaDeps& explicitDeps,
const cmNinjaDeps& implicitDeps,
const cmNinjaDeps& orderOnlyDeps,
const cmNinjaVars& variables,
const std::string& rspfile = std::string(),
2018-01-26 17:06:56 +01:00
int cmdLineLimit = 0, bool* usedResponseFile = nullptr);
2012-04-19 19:04:21 +03:00
/**
* Helper to write a build statement with the special 'phony' rule.
*/
2016-07-09 11:21:54 +02:00
void WritePhonyBuild(std::ostream& os, const std::string& comment,
2013-11-03 12:27:13 +02:00
const cmNinjaDeps& outputs,
const cmNinjaDeps& explicitDeps,
const cmNinjaDeps& implicitDeps = cmNinjaDeps(),
const cmNinjaDeps& orderOnlyDeps = cmNinjaDeps(),
const cmNinjaVars& variables = cmNinjaVars());
2012-04-19 19:04:21 +03:00
void WriteCustomCommandBuild(const std::string& command,
const std::string& description,
2016-10-30 18:24:19 +01:00
const std::string& comment,
const std::string& depfile, bool uses_terminal,
2016-07-09 11:21:54 +02:00
bool restat, const cmNinjaDeps& outputs,
2012-04-19 19:04:21 +03:00
const cmNinjaDeps& deps = cmNinjaDeps(),
2014-08-03 19:52:23 +02:00
const cmNinjaDeps& orderOnly = cmNinjaDeps());
2012-08-04 10:26:08 +03:00
void WriteMacOSXContentBuild(const std::string& input,
const std::string& output);
2012-04-19 19:04:21 +03:00
/**
* Write a rule statement named @a name to @a os with the @a comment,
* the mandatory @a command, the @a depfile and the @a description.
* It also writes the variables bound to this rule statement.
* @warning no escaping of any kind is done here.
*/
2016-07-09 11:21:54 +02:00
static void WriteRule(std::ostream& os, const std::string& name,
2012-04-19 19:04:21 +03:00
const std::string& command,
const std::string& description,
2016-07-09 11:21:54 +02:00
const std::string& comment, const std::string& depfile,
const std::string& deptype, const std::string& rspfile,
2012-08-04 10:26:08 +03:00
const std::string& rspcontent,
2016-07-09 11:21:54 +02:00
const std::string& restat, bool generator);
2012-04-19 19:04:21 +03:00
/**
* Write a variable named @a name to @a os with value @a value and an
* optional @a comment. An @a indent level can be specified.
* @warning no escaping of any kind is done here.
*/
2016-07-09 11:21:54 +02:00
static void WriteVariable(std::ostream& os, const std::string& name,
2012-04-19 19:04:21 +03:00
const std::string& value,
2016-07-09 11:21:54 +02:00
const std::string& comment = "", int indent = 0);
2012-04-19 19:04:21 +03:00
/**
* Write an include statement including @a filename with an optional
* @a comment to the @a os stream.
*/
2016-07-09 11:21:54 +02:00
static void WriteInclude(std::ostream& os, const std::string& filename,
2012-04-19 19:04:21 +03:00
const std::string& comment = "");
/**
* Write a default target statement specifying @a targets as
* the default targets.
*/
2016-07-09 11:21:54 +02:00
static void WriteDefault(std::ostream& os, const cmNinjaDeps& targets,
2012-04-19 19:04:21 +03:00
const std::string& comment = "");
2015-08-17 11:37:30 +02:00
bool IsGCCOnWindows() const { return UsingGCCOnWindows; }
2012-08-04 10:26:08 +03:00
2012-04-19 19:04:21 +03:00
public:
2015-08-17 11:37:30 +02:00
cmGlobalNinjaGenerator(cmake* cm);
2012-04-19 19:04:21 +03:00
2016-07-09 11:21:54 +02:00
static cmGlobalGeneratorFactory* NewFactory()
{
return new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaGenerator>();
}
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
~cmGlobalNinjaGenerator() override {}
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
cmLocalGenerator* CreateLocalGenerator(cmMakefile* mf) override;
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
std::string GetName() const override
2016-07-09 11:21:54 +02:00
{
return cmGlobalNinjaGenerator::GetActualName();
}
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
static std::string GetActualName() { return "Ninja"; }
2012-04-19 19:04:21 +03:00
2017-04-14 19:02:05 +02:00
/** Get encoding used by generator for ninja files */
2018-01-26 17:06:56 +01:00
codecvt::Encoding GetMakefileEncoding() const override;
2017-04-14 19:02:05 +02:00
2013-03-16 19:13:01 +02:00
static void GetDocumentation(cmDocumentationEntry& entry);
2012-04-19 19:04:21 +03:00
2016-10-30 18:24:19 +01:00
void EnableLanguage(std::vector<std::string> const& languages,
2018-01-26 17:06:56 +01:00
cmMakefile* mf, bool optional) override;
2012-04-19 19:04:21 +03:00
2016-10-30 18:24:19 +01:00
void GenerateBuildCommand(std::vector<std::string>& makeCommand,
const std::string& makeProgram,
const std::string& projectName,
const std::string& projectDir,
const std::string& targetName,
2018-08-09 18:06:22 +02:00
const std::string& config, bool fast, int jobs,
bool verbose,
2016-10-30 18:24:19 +01:00
std::vector<std::string> const& makeOptions =
2018-01-26 17:06:56 +01:00
std::vector<std::string>()) override;
2012-04-19 19:04:21 +03:00
// Setup target names
2018-01-26 17:06:56 +01:00
const char* GetAllTargetName() const override { return "all"; }
const char* GetInstallTargetName() const override { return "install"; }
const char* GetInstallLocalTargetName() const override
2016-07-09 11:21:54 +02:00
{
2012-04-19 19:04:21 +03:00
return "install/local";
}
2018-01-26 17:06:56 +01:00
const char* GetInstallStripTargetName() const override
2016-07-09 11:21:54 +02:00
{
2012-04-19 19:04:21 +03:00
return "install/strip";
}
2018-01-26 17:06:56 +01:00
const char* GetTestTargetName() const override { return "test"; }
const char* GetPackageTargetName() const override { return "package"; }
const char* GetPackageSourceTargetName() const override
2016-07-09 11:21:54 +02:00
{
2012-04-19 19:04:21 +03:00
return "package_source";
}
2018-01-26 17:06:56 +01:00
const char* GetEditCacheTargetName() const override { return "edit_cache"; }
const char* GetRebuildCacheTargetName() const override
2016-07-09 11:21:54 +02:00
{
2012-04-19 19:04:21 +03:00
return "rebuild_cache";
}
2018-01-26 17:06:56 +01:00
const char* GetCleanTargetName() const override { return "clean"; }
2012-04-19 19:04:21 +03:00
2016-07-09 11:21:54 +02:00
cmGeneratedFileStream* GetBuildFileStream() const
{
return this->BuildFileStream;
}
2012-08-04 10:26:08 +03:00
2016-07-09 11:21:54 +02:00
cmGeneratedFileStream* GetRulesFileStream() const
{
return this->RulesFileStream;
}
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
std::string const& ConvertToNinjaPath(const std::string& path) const;
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
struct MapToNinjaPathImpl
{
2015-11-17 17:22:37 +01:00
cmGlobalNinjaGenerator* GG;
2016-07-09 11:21:54 +02:00
MapToNinjaPathImpl(cmGlobalNinjaGenerator* gg)
: GG(gg)
{
}
std::string operator()(std::string const& path)
{
2015-11-17 17:22:37 +01:00
return this->GG->ConvertToNinjaPath(path);
}
};
2016-07-09 11:21:54 +02:00
MapToNinjaPathImpl MapToNinjaPath() { return MapToNinjaPathImpl(this); }
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
void AddCXXCompileCommand(const std::string& commandLine,
const std::string& sourceFile);
2012-06-27 20:52:58 +03:00
2012-04-19 19:04:21 +03:00
/**
* Add a rule to the generated build system.
* Call WriteRule() behind the scene but perform some check before like:
* - Do not add twice the same rule.
*/
2016-07-09 11:21:54 +02:00
void AddRule(const std::string& name, const std::string& command,
const std::string& description, const std::string& comment,
const std::string& depfile, const std::string& deptype,
const std::string& rspfile, const std::string& rspcontent,
const std::string& restat, bool generator);
2012-04-19 19:04:21 +03:00
bool HasRule(const std::string& name);
void AddCustomCommandRule();
2012-08-04 10:26:08 +03:00
void AddMacOSXContentRule();
2016-07-09 11:21:54 +02:00
bool HasCustomCommandOutput(const std::string& output)
{
2012-08-04 10:26:08 +03:00
return this->CustomCommandOutputs.find(output) !=
2016-07-09 11:21:54 +02:00
this->CustomCommandOutputs.end();
2012-08-04 10:26:08 +03:00
}
/// Called when we have seen the given custom command. Returns true
/// if we has seen it before.
2016-07-09 11:21:54 +02:00
bool SeenCustomCommand(cmCustomCommand const* cc)
{
2012-08-04 10:26:08 +03:00
return !this->CustomCommands.insert(cc).second;
}
/// Called when we have seen the given custom command output.
2016-07-09 11:21:54 +02:00
void SeenCustomCommandOutput(const std::string& output)
{
2012-08-04 10:26:08 +03:00
this->CustomCommandOutputs.insert(output);
// We don't need the assumed dependencies anymore, because we have
// an output.
this->AssumedSourceDependencies.erase(output);
}
2016-07-09 11:21:54 +02:00
void AddAssumedSourceDependencies(const std::string& source,
const cmNinjaDeps& deps)
{
std::set<std::string>& ASD = this->AssumedSourceDependencies[source];
2012-08-04 10:26:08 +03:00
// Because we may see the same source file multiple times (same source
// specified in multiple targets), compute the union of any assumed
// dependencies.
ASD.insert(deps.begin(), deps.end());
}
2017-07-20 19:35:53 +02:00
void AppendTargetOutputs(
cmGeneratorTarget const* target, cmNinjaDeps& outputs,
cmNinjaTargetDepends depends = DependOnTargetArtifact);
void AppendTargetDepends(
cmGeneratorTarget const* target, cmNinjaDeps& outputs,
cmNinjaTargetDepends depends = DependOnTargetArtifact);
2016-10-30 18:24:19 +01:00
void AppendTargetDependsClosure(cmGeneratorTarget const* target,
cmNinjaDeps& outputs);
2018-01-26 17:06:56 +01:00
void AppendTargetDependsClosure(cmGeneratorTarget const* target,
cmNinjaOuts& outputs, bool omit_self);
2016-03-13 13:35:51 +01:00
void AddDependencyToAll(cmGeneratorTarget* target);
2012-08-04 10:26:08 +03:00
void AddDependencyToAll(const std::string& input);
2016-07-09 11:21:54 +02:00
const std::vector<cmLocalGenerator*>& GetLocalGenerators() const
{
return LocalGenerators;
}
2012-08-04 10:26:08 +03:00
2016-07-09 11:21:54 +02:00
bool IsExcluded(cmLocalGenerator* root, cmGeneratorTarget* target)
{
return cmGlobalGenerator::IsExcluded(root, target);
}
2012-08-04 10:26:08 +03:00
2016-07-09 11:21:54 +02:00
int GetRuleCmdLength(const std::string& name) { return RuleCmdLength[name]; }
2012-08-04 10:26:08 +03:00
2016-03-13 13:35:51 +01:00
void AddTargetAlias(const std::string& alias, cmGeneratorTarget* target);
2012-08-04 10:26:08 +03:00
2018-01-26 17:06:56 +01:00
void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
2015-04-27 22:25:09 +02:00
2015-11-17 17:22:37 +01:00
// Ninja generator uses 'deps' and 'msvc_deps_prefix' introduced in 1.3
static std::string RequiredNinjaVersion() { return "1.3"; }
static std::string RequiredNinjaVersionForConsolePool() { return "1.5"; }
2016-10-30 18:24:19 +01:00
static std::string RequiredNinjaVersionForImplicitOuts() { return "1.7"; }
2018-08-09 18:06:22 +02:00
static std::string RequiredNinjaVersionForManifestRestat() { return "1.8"; }
2015-04-27 22:25:09 +02:00
bool SupportsConsolePool() const;
2016-10-30 18:24:19 +01:00
bool SupportsImplicitOuts() const;
2018-08-09 18:06:22 +02:00
bool SupportsManifestRestat() const;
2012-04-19 19:04:21 +03:00
2017-04-14 19:02:05 +02:00
std::string NinjaOutputPath(std::string const& path) const;
2016-07-09 11:21:54 +02:00
bool HasOutputPathPrefix() const { return !this->OutputPathPrefix.empty(); }
void StripNinjaOutputPathPrefixAsSuffix(std::string& path);
2012-04-19 19:04:21 +03:00
2016-10-30 18:24:19 +01:00
bool WriteDyndepFile(std::string const& dir_top_src,
std::string const& dir_top_bld,
std::string const& dir_cur_src,
std::string const& dir_cur_bld,
std::string const& arg_dd,
std::vector<std::string> const& arg_ddis,
std::string const& module_dir,
std::vector<std::string> const& linked_target_dirs);
2016-07-09 11:21:54 +02:00
protected:
2018-01-26 17:06:56 +01:00
void Generate() override;
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const override { return true; }
2012-04-19 19:04:21 +03:00
private:
2018-01-26 17:06:56 +01:00
std::string GetEditCacheCommand() const override;
bool FindMakeProgram(cmMakefile* mf) override;
2016-10-30 18:24:19 +01:00
void CheckNinjaFeatures();
bool CheckLanguages(std::vector<std::string> const& languages,
2018-01-26 17:06:56 +01:00
cmMakefile* mf) const override;
2016-10-30 18:24:19 +01:00
bool CheckFortran(cmMakefile* mf) const;
2012-04-19 19:04:21 +03:00
void OpenBuildFileStream();
void CloseBuildFileStream();
2012-06-27 20:52:58 +03:00
void CloseCompileCommandsStream();
2012-04-19 19:04:21 +03:00
void OpenRulesFileStream();
void CloseRulesFileStream();
/// Write the common disclaimer text at the top of each build file.
void WriteDisclaimer(std::ostream& os);
void WriteAssumedSourceDependencies();
void WriteTargetAliases(std::ostream& os);
2016-07-09 11:21:54 +02:00
void WriteFolderTargets(std::ostream& os);
2013-11-03 12:27:13 +02:00
void WriteUnknownExplicitDependencies(std::ostream& os);
2012-04-19 19:04:21 +03:00
void WriteBuiltinTargets(std::ostream& os);
void WriteTargetAll(std::ostream& os);
void WriteTargetRebuildManifest(std::ostream& os);
2012-06-27 20:52:58 +03:00
void WriteTargetClean(std::ostream& os);
void WriteTargetHelp(std::ostream& os);
2012-04-19 19:04:21 +03:00
2016-10-30 18:24:19 +01:00
void ComputeTargetDependsClosure(
cmGeneratorTarget const* target,
std::set<cmGeneratorTarget const*>& depends);
2012-06-27 20:52:58 +03:00
std::string ninjaCmd() const;
2015-04-27 22:25:09 +02:00
/// The file containing the build statement. (the relationship of the
2012-04-19 19:04:21 +03:00
/// compilation DAG).
cmGeneratedFileStream* BuildFileStream;
/// The file containing the rule statements. (The action attached to each
/// edge of the compilation DAG).
cmGeneratedFileStream* RulesFileStream;
2012-06-27 20:52:58 +03:00
cmGeneratedFileStream* CompileCommandsStream;
2012-04-19 19:04:21 +03:00
/// The type used to store the set of rules added to the generated build
/// system.
typedef std::set<std::string> RulesSetType;
/// The set of rules added to the generated build system.
RulesSetType Rules;
2012-08-04 10:26:08 +03:00
/// Length of rule command, used by rsp file evaluation
std::map<std::string, int> RuleCmdLength;
2012-04-19 19:04:21 +03:00
/// The set of dependencies to add to the "all" target.
cmNinjaDeps AllDependencies;
2015-08-17 11:37:30 +02:00
bool UsingGCCOnWindows;
2012-04-19 19:04:21 +03:00
/// The set of custom commands we have seen.
std::set<cmCustomCommand const*> CustomCommands;
/// The set of custom command outputs we have seen.
std::set<std::string> CustomCommandOutputs;
2015-08-17 11:37:30 +02:00
/// Whether we are collecting known build outputs and needed
/// dependencies to determine unknown dependencies.
bool ComputingUnknownDependencies;
cmPolicies::PolicyStatus PolicyCMP0058;
2015-04-27 22:25:09 +02:00
/// The combined explicit dependencies of custom build commands
std::set<std::string> CombinedCustomCommandExplicitDependencies;
/// When combined with CombinedCustomCommandExplicitDependencies it allows
/// us to detect the set of explicit dependencies that have
2013-11-03 12:27:13 +02:00
std::set<std::string> CombinedBuildOutputs;
2012-04-19 19:04:21 +03:00
/// The mapping from source file to assumed dependencies.
2018-01-26 17:06:56 +01:00
std::map<std::string, std::set<std::string>> AssumedSourceDependencies;
2012-04-19 19:04:21 +03:00
2016-03-13 13:35:51 +01:00
typedef std::map<std::string, cmGeneratorTarget*> TargetAliasMap;
2012-04-19 19:04:21 +03:00
TargetAliasMap TargetAliases;
2016-03-13 13:35:51 +01:00
2018-01-26 17:06:56 +01:00
std::map<cmGeneratorTarget const*, cmNinjaOuts> TargetDependsClosures;
/// the local cache for calls to ConvertToNinjaPath
mutable std::unordered_map<std::string, std::string> ConvertToNinjaPathCache;
2016-10-30 18:24:19 +01:00
2016-03-13 13:35:51 +01:00
std::string NinjaCommand;
std::string NinjaVersion;
2016-10-30 18:24:19 +01:00
bool NinjaSupportsConsolePool;
bool NinjaSupportsImplicitOuts;
2018-08-09 18:06:22 +02:00
bool NinjaSupportsManifestRestat;
2016-10-30 18:24:19 +01:00
unsigned long NinjaSupportsDyndeps;
2016-07-09 11:21:54 +02:00
private:
void InitOutputPathPrefix();
std::string OutputPathPrefix;
std::string TargetAll;
std::string CMakeCacheFile;
2012-04-19 19:04:21 +03:00
};
#endif // ! cmGlobalNinjaGenerator_h