Commit 6dd410c2 authored by Peter Collingbourne's avatar Peter Collingbourne

Ninja: Add the Ninja generator

parent 7eb8d903
#=============================================================================
# Copyright 2011 Kitware, Inc.
#
# 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.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
FIND_PROGRAM(CMAKE_MAKE_PROGRAM ninja
DOC "Program used to build from build.ninja files.")
MARK_AS_ADVANCED(CMAKE_MAKE_PROGRAM)
......@@ -833,6 +833,12 @@ function(ExternalProject_Add_StepTargets name)
foreach(step ${steps})
add_custom_target(${name}-${step}
DEPENDS ${stamp_dir}${cfgdir}/${name}-${step})
# Depend on other external projects (target-level).
get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
foreach(arg IN LISTS deps)
add_dependencies(${name}-${step} ${arg})
endforeach()
endforeach()
endfunction(ExternalProject_Add_StepTargets)
......@@ -1451,9 +1457,18 @@ function(ExternalProject_Add name)
# depends on the 'done' mark so that it rebuilds when this project
# rebuilds. It is important that 'done' is not the output of any
# custom command so that CMake does not propagate build rules to
# other external project targets.
# other external project targets, which may cause problems during
# parallel builds. However, the Ninja generator needs to see the entire
# dependency graph, and can cope with custom commands belonging to
# multiple targets, so we add the 'done' mark as an output for Ninja only.
set(complete_outputs ${cmf_dir}${cfgdir}/${name}-complete)
if(${CMAKE_GENERATOR} MATCHES "Ninja")
set(complete_outputs
${complete_outputs} ${stamp_dir}${cfgdir}/${name}-done)
endif()
add_custom_command(
OUTPUT ${cmf_dir}${cfgdir}/${name}-complete
OUTPUT ${complete_outputs}
COMMENT "Completed '${name}'"
COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
COMMAND ${CMAKE_COMMAND} -E touch ${cmf_dir}${cfgdir}/${name}-complete
......
......@@ -353,6 +353,24 @@ IF (WIN32)
ENDIF(NOT UNIX)
ENDIF (WIN32)
# Ninja only works on UNIX.
IF(UNIX)
SET(SRCS ${SRCS}
cmGlobalNinjaGenerator.cxx
cmGlobalNinjaGenerator.h
cmNinjaTypes.h
cmLocalNinjaGenerator.cxx
cmLocalNinjaGenerator.h
cmNinjaTargetGenerator.cxx
cmNinjaTargetGenerator.h
cmNinjaNormalTargetGenerator.cxx
cmNinjaNormalTargetGenerator.h
cmNinjaUtilityTargetGenerator.cxx
cmNinjaUtilityTargetGenerator.h
)
ADD_DEFINITIONS(-DCMAKE_USE_NINJA)
ENDIF(UNIX)
# create a library used by the command line and the GUI
ADD_LIBRARY(CMakeLib ${SRCS})
TARGET_LINK_LIBRARIES(CMakeLib cmsys
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*============================================================================
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"
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;
public:
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();
const char* GetConfigName() const
{ return this->ConfigName.c_str(); }
std::string GetObjectFileName(const cmTarget& target,
const cmSourceFile& source);
/// @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; }
protected:
virtual std::string ConvertToLinkReference(std::string const& lib);
virtual std::string ConvertToIncludeReference(std::string const& path);
private:
friend class cmGlobalNinjaGenerator;
// In order to access to protected member of the local generator.
friend class cmNinjaTargetGenerator;
friend class cmNinjaNormalTargetGenerator;
friend class cmNinjaUtilityTargetGenerator;
private:
cmGeneratedFileStream& GetBuildFileStream() const;
cmGeneratedFileStream& GetRulesFileStream() const;
void WriteBuildFileTop();
void WriteProjectHeader(std::ostream& os);
void WriteNinjaFilesInclusion(std::ostream& os);
void WriteProcessedMakefile(std::ostream& os);
void SetConfigName();
std::string ConvertToNinjaPath(const char *path);
struct map_to_ninja_path {
cmLocalNinjaGenerator *LocalGenerator;
map_to_ninja_path(cmLocalNinjaGenerator *LocalGenerator)
: LocalGenerator(LocalGenerator) {}
std::string operator()(const std::string &path) {
return LocalGenerator->ConvertToNinjaPath(path.c_str());
}
};
map_to_ninja_path MapToNinjaPath() {
return map_to_ninja_path(this);
}
void AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs);
void AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs);
void AppendCustomCommandDeps(const cmCustomCommand *cc,
cmNinjaDeps &ninjaDeps);
std::string BuildCommandLine(const std::vector<std::string> &cmdLines);
void AppendCustomCommandLines(const cmCustomCommand *cc,
std::vector<std::string> &cmdLines);
void WriteCustomCommandRule();
void WriteCustomCommandBuildStatement(cmCustomCommand *cc,
const cmNinjaDeps& orderOnlyDeps);
void AddCustomCommandTarget(cmCustomCommand* cc, cmTarget* target);
void WriteCustomCommandBuildStatements();
private:
std::string ConfigName;
std::string HomeRelativeOutputPath;
typedef std::map<cmCustomCommand*, std::set<cmTarget*> >
CustomCommandTargetMap;
CustomCommandTargetMap CustomCommandTargets;
};
#endif // ! cmLocalNinjaGenerator_h
This diff is collapsed.
/*============================================================================
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 cmNinjaNormalTargetGenerator_h
# define cmNinjaNormalTargetGenerator_h
# include "cmNinjaTargetGenerator.h"
# include "cmNinjaTypes.h"
class cmSourceFile;
class cmNinjaNormalTargetGenerator : public cmNinjaTargetGenerator
{
public:
cmNinjaNormalTargetGenerator(cmTarget* target);
~cmNinjaNormalTargetGenerator();
void Generate();
private:
std::string LanguageLinkerRule() const;
const char* GetVisibleTypeName() const;
void WriteLanguagesRules();
void WriteLinkRule();
void WriteLinkStatement();
std::vector<std::string> ComputeLinkCmd();
private:
// Target name info.
std::string TargetNameOut;
std::string TargetNameSO;
std::string TargetNameReal;
std::string TargetNameImport;
std::string TargetNamePDB;
const char *TargetLinkLanguage;
};
#endif // ! cmNinjaNormalTargetGenerator_h
This diff is collapsed.
/*============================================================================
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 cmNinjaTargetGenerator_h
#define cmNinjaTargetGenerator_h
#include "cmStandardIncludes.h"
#include "cmNinjaTypes.h"
#include "cmLocalNinjaGenerator.h"
class cmTarget;
class cmGlobalNinjaGenerator;
class cmGeneratedFileStream;
class cmMakefile;
class cmSourceFile;
class cmCustomCommand;
class cmNinjaTargetGenerator
{
public:
/// Create a cmNinjaTargetGenerator according to the @a target's type.
static cmNinjaTargetGenerator* New(cmTarget* target);
/// Build a NinjaTargetGenerator.
cmNinjaTargetGenerator(cmTarget* target);
/// Destructor.
virtual ~cmNinjaTargetGenerator();
virtual void Generate() = 0;
std::string GetTargetName() const;
protected:
cmGeneratedFileStream& GetBuildFileStream() const;
cmGeneratedFileStream& GetRulesFileStream() const;
cmTarget* GetTarget() const
{ return this->Target; }
cmLocalNinjaGenerator* GetLocalGenerator() const
{ return this->LocalGenerator; }
cmGlobalNinjaGenerator* GetGlobalGenerator() const;
cmMakefile* GetMakefile() const
{ return this->Makefile; }
const char* GetConfigName() const;
std::string LanguageCompilerRule(const std::string& lang) const
{ return lang + "_COMPILER"; }
const char* GetFeature(const char* feature);
bool GetFeatureAsBool(const char* feature);
void AddFeatureFlags(std::string& flags, const char* lang);
/**
* Compute the flags for compilation of object files for a given @a language.
* @note Generally it is the value of the variable whose name is computed
* by LanguageFlagsVarName().
*/
std::string ComputeFlagsForObject(cmSourceFile *source,
const std::string& language);
std::string ComputeDefines(cmSourceFile *source,
const std::string& language);
std::string ConvertToNinjaPath(const char *path) const;
cmLocalNinjaGenerator::map_to_ninja_path MapToNinjaPath() const {
return this->GetLocalGenerator()->MapToNinjaPath();
}
/// @return the list of link dependency for the given target @a target.
cmNinjaDeps ComputeLinkDeps() const;
/// @return the source file path for the given @a source.
std::string GetSourceFilePath(cmSourceFile* source) const;
/// @return the object file path for the given @a source.
std::string GetObjectFilePath(cmSourceFile* source) const;
/// @return the file path where the target named @a name is generated.
std::string GetTargetFilePath(const std::string& name) const;
/// @return the output path for the target.
virtual std::string GetTargetOutputDir() const;
void WriteLanguageRules(const std::string& language);
void WriteCompileRule(const std::string& language);
void WriteObjectBuildStatements();
void WriteObjectBuildStatement(cmSourceFile* source);
void WriteCustomCommandBuildStatement(cmCustomCommand *cc);
cmNinjaDeps GetObjects() const
{ return this->Objects; }
private:
cmTarget* Target;
cmMakefile* Makefile;
cmLocalNinjaGenerator* LocalGenerator;
/// List of object files for this target.
cmNinjaDeps Objects;
};
#endif // ! cmNinjaTargetGenerator_h
/*============================================================================
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 cmNinjaTypes_h
# define cmNinjaTypes_h
typedef std::vector<std::string> cmNinjaDeps;
typedef std::map<std::string, std::string> cmNinjaVars;
#endif // ! cmNinjaTypes_h
#include "cmNinjaUtilityTargetGenerator.h"
#include "cmCustomCommand.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalNinjaGenerator.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmTarget.h"
cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(cmTarget *target)
: cmNinjaTargetGenerator(target) {}
cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator() {}
void cmNinjaUtilityTargetGenerator::Generate()
{
std::vector<std::string> commands;
cmNinjaDeps deps, outputs;
const std::vector<cmCustomCommand> *cmdLists[2] = {
&this->GetTarget()->GetPreBuildCommands(),
&this->GetTarget()->GetPostBuildCommands()
};
for (unsigned i = 0; i != 2; ++i) {
for (std::vector<cmCustomCommand>::const_iterator
ci = cmdLists[i]->begin(); ci != cmdLists[i]->end(); ++ci) {
this->GetLocalGenerator()->AppendCustomCommandDeps(&*ci, deps);
this->GetLocalGenerator()->AppendCustomCommandLines(&*ci, commands);
}
}
const std::vector<cmSourceFile*>& sources =
this->GetTarget()->GetSourceFiles();
for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
source != sources.end(); ++source)
{
if(cmCustomCommand* cc = (*source)->GetCustomCommand())
{
this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
// Depend on all custom command outputs.
const std::vector<std::string>& outputs = cc->GetOutputs();
std::transform(outputs.begin(), outputs.end(),
std::back_inserter(deps), MapToNinjaPath());
}
}
this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(), deps);
if (commands.empty()) {
cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
"Utility command for "
+ this->GetTargetName(),
outputs,
deps);
} else {
std::string command =
this->GetLocalGenerator()->BuildCommandLine(commands);
const char *echoStr = this->GetTarget()->GetProperty("EchoString");
std::string desc;
if (echoStr)
desc = echoStr;
else
desc = "Running utility command for " + this->GetTargetName();
// TODO: fix problematic global targets. For now, search and replace the
// makefile vars.
cmSystemTools::ReplaceString(command, "$(CMAKE_SOURCE_DIR)",
this->GetTarget()->GetMakefile()->GetHomeDirectory());
cmSystemTools::ReplaceString(command, "$(CMAKE_BINARY_DIR)",
this->GetTarget()->GetMakefile()->GetHomeOutputDirectory());
cmSystemTools::ReplaceString(command, "$(ARGS)", "");
if (command.find('$') != std::string::npos)
return;
std::string utilCommandName = cmake::GetCMakeFilesDirectoryPostSlash();
utilCommandName += this->GetTargetName() + ".util";
this->GetGlobalGenerator()->WriteCustomCommandBuild(
command,
desc,
"Utility command for " + this->GetTargetName(),
cmNinjaDeps(1, utilCommandName),
deps);
cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
"",
outputs,
cmNinjaDeps(1, utilCommandName),
cmNinjaDeps(),
cmNinjaDeps(),
cmNinjaVars());
}
this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
this->GetTarget());
}
/*============================================================================
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 cmNinjaUtilityTargetGenerator_h
# define cmNinjaUtilityTargetGenerator_h
# include "cmNinjaTargetGenerator.h"
# include "cmNinjaTypes.h"
class cmSourceFile;
class cmNinjaUtilityTargetGenerator : public cmNinjaTargetGenerator
{
public:
cmNinjaUtilityTargetGenerator(cmTarget* target);
~cmNinjaUtilityTargetGenerator();
void Generate();
};
#endif // ! cmNinjaUtilityTargetGenerator_h
......@@ -84,6 +84,10 @@
#endif
#include "cmGlobalUnixMakefileGenerator3.h"
#ifdef CMAKE_USE_NINJA
# include "cmGlobalNinjaGenerator.h"
#endif
#if defined(CMAKE_HAVE_VS_GENERATORS)
#include "cmCallVisualStudioMacro.h"
#endif
......@@ -2614,6 +2618,10 @@ void cmake::AddDefaultGenerators()
#endif
this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
&cmGlobalUnixMakefileGenerator3::New;
#ifdef CMAKE_USE_NINJA
this->Generators[cmGlobalNinjaGenerator::GetActualName()] =
&cmGlobalNinjaGenerator::New;
#endif
#ifdef CMAKE_USE_XCODE
this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
&cmGlobalXCodeGenerator::New;
......
......@@ -334,6 +334,15 @@ IF(BUILD_TESTING)
--build-target car
--test-command car
)
IF(${CMAKE_TEST_GENERATOR} MATCHES "Ninja")
# The Ninja generator does not create a recursive build system. Start
# from the root directory.
SET(SubProject_SUBDIR)
ELSE()
SET(SubProject_SUBDIR "/foo")
ENDIF()
# For stage 2, do not run cmake again.
# Then build the foo sub project which should build
# the bar library which should be referenced because
......@@ -341,13 +350,14 @@ IF(BUILD_TESTING)
# directly in the foo sub project
ADD_TEST(SubProject-Stage2 ${CMAKE_CTEST_COMMAND}
--build-and-test
"${CMake_SOURCE_DIR}/Tests/SubProject/foo"
"${CMake_BINARY_DIR}/Tests/SubProject/foo"
"${CMake_SOURCE_DIR}/Tests/SubProject${SubProject_SUBDIR}"
"${CMake_BINARY_DIR}/Tests/SubProject${SubProject_SUBDIR}"
--build-generator ${CMAKE_TEST_GENERATOR}
--build-makeprogram ${CMAKE_TEST_MAKEPROGRAM}
--build-nocmake
--build-project foo
--build-target foo
--build-exe-dir "${CMake_BINARY_DIR}/Tests/SubProject/foo"
--test-command foo
)
SET_TESTS_PROPERTIES ( SubProject-Stage2 PROPERTIES DEPENDS SubProject)
......
......@@ -24,6 +24,12 @@ IF ("${PROJECT_SOURCE_DIR}" STREQUAL "${ANOTHER_PROJ_SOURCE_DIR}")
MATH(EXPR MAXPATH "${MAXPATH} - 46")
ENDIF()
# Ninja imposes a maximum path component count of 30. Permit more
# path components in the source path.
IF(${CMAKE_GENERATOR} MATCHES "Ninja")
MATH(EXPR MAXPATH "${MAXPATH} - 44")
ENDIF()
# MAXPATH less 25 for last /and/deeper/simple.cxx part and small safety
MATH(EXPR MAXPATH "${MAXPATH} - 25")
STRING(LENGTH "${DEEPDIR}" DEEPDIR_LEN)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment