1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 05:26:58 +08:00

cmCustomCommandGenerator: Add option to transform depfile

This commit is contained in:
Kyle Edwards
2020-10-05 12:15:17 -04:00
parent b2c14bc774
commit 596439b1bb
4 changed files with 113 additions and 37 deletions

View File

@@ -6,18 +6,22 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <cm/optional>
#include <cmext/algorithm> #include <cmext/algorithm>
#include "cmCryptoHash.h"
#include "cmCustomCommand.h" #include "cmCustomCommand.h"
#include "cmCustomCommandLines.h" #include "cmCustomCommandLines.h"
#include "cmGeneratorExpression.h" #include "cmGeneratorExpression.h"
#include "cmGeneratorTarget.h" #include "cmGeneratorTarget.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h" #include "cmLocalGenerator.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmProperty.h" #include "cmProperty.h"
#include "cmStateTypes.h" #include "cmStateTypes.h"
#include "cmStringAlgorithms.h" #include "cmStringAlgorithms.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmTransformDepfile.h"
namespace { namespace {
void AppendPaths(const std::vector<std::string>& inputs, void AppendPaths(const std::vector<std::string>& inputs,
@@ -42,7 +46,8 @@ void AppendPaths(const std::vector<std::string>& inputs,
cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc, cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
std::string config, std::string config,
cmLocalGenerator* lg) cmLocalGenerator* lg,
bool transformDepfile)
: CC(cc) : CC(cc)
, Config(std::move(config)) , Config(std::move(config))
, LG(lg) , LG(lg)
@@ -75,6 +80,36 @@ cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
this->CommandLines.push_back(std::move(argv)); this->CommandLines.push_back(std::move(argv));
} }
if (transformDepfile && !this->CommandLines.empty() &&
!cc.GetDepfile().empty() &&
this->LG->GetGlobalGenerator()->DepfileFormat()) {
cmCustomCommandLine argv;
argv.push_back(cmSystemTools::GetCMakeCommand());
argv.emplace_back("-E");
argv.emplace_back("cmake_transform_depfile");
switch (*this->LG->GetGlobalGenerator()->DepfileFormat()) {
case cmDepfileFormat::GccDepfile:
argv.emplace_back("gccdepfile");
break;
case cmDepfileFormat::VsTlog:
argv.emplace_back("vstlog");
break;
}
if (this->LG->GetCurrentBinaryDirectory() ==
this->LG->GetBinaryDirectory()) {
argv.emplace_back("./");
} else {
argv.push_back(cmStrCat(this->LG->MaybeConvertToRelativePath(
this->LG->GetBinaryDirectory(),
this->LG->GetCurrentBinaryDirectory()),
'/'));
}
argv.push_back(this->GetFullDepfile());
argv.push_back(this->GetInternalDepfile());
this->CommandLines.push_back(std::move(argv));
}
AppendPaths(cc.GetByproducts(), ge, this->LG, this->Config, AppendPaths(cc.GetByproducts(), ge, this->LG, this->Config,
this->Byproducts); this->Byproducts);
AppendPaths(cc.GetDepends(), ge, this->LG, this->Config, this->Depends); AppendPaths(cc.GetDepends(), ge, this->LG, this->Config, this->Depends);
@@ -97,7 +132,7 @@ cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
{ {
return static_cast<unsigned int>(this->CC.GetCommandLines().size()); return static_cast<unsigned int>(this->CommandLines.size());
} }
void cmCustomCommandGenerator::FillEmulatorsWithArguments() void cmCustomCommandGenerator::FillEmulatorsWithArguments()
@@ -234,6 +269,40 @@ void cmCustomCommandGenerator::AppendArguments(unsigned int c,
} }
} }
std::string cmCustomCommandGenerator::GetFullDepfile() const
{
std::string depfile = this->CC.GetDepfile();
if (depfile.empty()) {
return "";
}
if (!cmSystemTools::FileIsFullPath(depfile)) {
depfile = cmStrCat(this->LG->GetCurrentBinaryDirectory(), '/', depfile);
}
return cmSystemTools::CollapseFullPath(depfile);
}
std::string cmCustomCommandGenerator::GetInternalDepfile() const
{
std::string depfile = this->GetFullDepfile();
if (depfile.empty()) {
return "";
}
cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
std::string extension;
switch (*this->LG->GetGlobalGenerator()->DepfileFormat()) {
case cmDepfileFormat::GccDepfile:
extension = ".d";
break;
case cmDepfileFormat::VsTlog:
extension = ".tlog";
break;
}
return cmStrCat(this->LG->GetBinaryDirectory(), "/CMakeFiles/d/",
hash.HashString(depfile), extension);
}
const char* cmCustomCommandGenerator::GetComment() const const char* cmCustomCommandGenerator::GetComment() const
{ {
return this->CC.GetComment(); return this->CC.GetComment();

View File

@@ -31,7 +31,7 @@ class cmCustomCommandGenerator
public: public:
cmCustomCommandGenerator(cmCustomCommand const& cc, std::string config, cmCustomCommandGenerator(cmCustomCommand const& cc, std::string config,
cmLocalGenerator* lg); cmLocalGenerator* lg, bool transformDepfile = true);
cmCustomCommandGenerator(const cmCustomCommandGenerator&) = delete; cmCustomCommandGenerator(const cmCustomCommandGenerator&) = delete;
cmCustomCommandGenerator& operator=(const cmCustomCommandGenerator&) = cmCustomCommandGenerator& operator=(const cmCustomCommandGenerator&) =
delete; delete;
@@ -45,4 +45,6 @@ public:
std::vector<std::string> const& GetByproducts() const; std::vector<std::string> const& GetByproducts() const;
std::vector<std::string> const& GetDepends() const; std::vector<std::string> const& GetDepends() const;
bool HasOnlyEmptyCommandLines() const; bool HasOnlyEmptyCommandLines() const;
std::string GetFullDepfile() const;
std::string GetInternalDepfile() const;
}; };

View File

@@ -14,6 +14,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <cm/optional>
#include <cmext/algorithm> #include <cmext/algorithm>
#include "cm_codecvt.hxx" #include "cm_codecvt.hxx"
@@ -26,6 +27,7 @@
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmTarget.h" #include "cmTarget.h"
#include "cmTargetDepend.h" #include "cmTargetDepend.h"
#include "cmTransformDepfile.h"
#if !defined(CMAKE_BOOTSTRAP) #if !defined(CMAKE_BOOTSTRAP)
# include <cm3p/json/value.h> # include <cm3p/json/value.h>
@@ -452,6 +454,10 @@ public:
virtual bool ShouldStripResourcePath(cmMakefile*) const; virtual bool ShouldStripResourcePath(cmMakefile*) const;
virtual bool SupportsCustomCommandDepfile() const { return false; } virtual bool SupportsCustomCommandDepfile() const { return false; }
virtual cm::optional<cmDepfileFormat> DepfileFormat() const
{
return cm::nullopt;
}
std::string GetSharedLibFlagsForLanguage(std::string const& lang) const; std::string GetSharedLibFlagsForLanguage(std::string const& lang) const;

View File

@@ -307,6 +307,7 @@ CMAKE_CXX_SOURCES="\
cmContinueCommand \ cmContinueCommand \
cmCoreTryCompile \ cmCoreTryCompile \
cmCreateTestSourceList \ cmCreateTestSourceList \
cmCryptoHash \
cmCustomCommand \ cmCustomCommand \
cmCustomCommandGenerator \ cmCustomCommandGenerator \
cmCustomCommandLines \ cmCustomCommandLines \
@@ -539,6 +540,18 @@ KWSYS_FILES="\
SystemTools.hxx \ SystemTools.hxx \
Terminal.h" Terminal.h"
LIBRHASH_C_SOURCES="\
librhash/algorithms.c \
librhash/byte_order.c \
librhash/hex.c \
librhash/md5.c \
librhash/rhash.c \
librhash/sha1.c \
librhash/sha256.c \
librhash/sha3.c \
librhash/sha512.c \
"
if ${cmake_system_mingw}; then if ${cmake_system_mingw}; then
LIBUV_C_SOURCES="\ LIBUV_C_SOURCES="\
src/fs-poll.c \ src/fs-poll.c \
@@ -1016,7 +1029,6 @@ cmake_ld_flags=${LDFLAGS}
# Add generator-specific files # Add generator-specific files
if test "${cmake_bootstrap_generator}" = "Ninja"; then if test "${cmake_bootstrap_generator}" = "Ninja"; then
CMAKE_CXX_SOURCES="${CMAKE_CXX_SOURCES} \ CMAKE_CXX_SOURCES="${CMAKE_CXX_SOURCES} \
cmCryptoHash \
cmFortranParserImpl \ cmFortranParserImpl \
cmGlobalNinjaGenerator \ cmGlobalNinjaGenerator \
cmLocalNinjaGenerator \ cmLocalNinjaGenerator \
@@ -1037,18 +1049,6 @@ if test "${cmake_bootstrap_generator}" = "Ninja"; then
src/lib_json/json_value.cpp \ src/lib_json/json_value.cpp \
src/lib_json/json_writer.cpp \ src/lib_json/json_writer.cpp \
" "
LIBRHASH_C_SOURCES="\
librhash/algorithms.c \
librhash/byte_order.c \
librhash/hex.c \
librhash/md5.c \
librhash/rhash.c \
librhash/sha1.c \
librhash/sha256.c \
librhash/sha3.c \
librhash/sha512.c \
"
else else
CMAKE_CXX_SOURCES="${CMAKE_CXX_SOURCES} \ CMAKE_CXX_SOURCES="${CMAKE_CXX_SOURCES} \
cmDepends \ cmDepends \
@@ -1062,7 +1062,6 @@ else
" "
JSONCPP_CXX_SOURCES= JSONCPP_CXX_SOURCES=
LIBRHASH_C_SOURCES=
fi fi
# Add Cygwin-specific flags # Add Cygwin-specific flags
@@ -1632,17 +1631,17 @@ if test "x${bootstrap_system_libuv}" = "x"; then
objs="${objs} uv-`cmake_obj ${a}`" objs="${objs} uv-`cmake_obj ${a}`"
done done
fi fi
if test "x${bootstrap_system_librhash}" = "x"; then
for a in ${LIBRHASH_C_SOURCES}; do
objs="${objs} rhash-`cmake_obj ${a}`"
done
fi
if test "${cmake_bootstrap_generator}" = "Ninja"; then if test "${cmake_bootstrap_generator}" = "Ninja"; then
if test "x${bootstrap_system_jsoncpp}" = "x"; then if test "x${bootstrap_system_jsoncpp}" = "x"; then
for a in ${JSONCPP_CXX_SOURCES}; do for a in ${JSONCPP_CXX_SOURCES}; do
objs="${objs} jsoncpp-`cmake_obj ${a}`" objs="${objs} jsoncpp-`cmake_obj ${a}`"
done done
fi fi
if test "x${bootstrap_system_librhash}" = "x"; then
for a in ${LIBRHASH_C_SOURCES}; do
objs="${objs} rhash-`cmake_obj ${a}`"
done
fi
fi fi
libs="" libs=""
@@ -1702,6 +1701,15 @@ else
libs="${libs} -luv" libs="${libs} -luv"
fi fi
if test "x${bootstrap_system_librhash}" != "x"; then
if test `which pkg-config`; then
use_librhash_flags="`pkg-config --cflags librhash`"
cmake_c_flags="${cmake_c_flags} ${use_librhash_flags}"
cmake_cxx_flags="${cmake_cxx_flags} ${use_librhash_flags}"
fi
libs="${libs} -lrhash"
fi
if test "${cmake_bootstrap_generator}" = "Ninja"; then if test "${cmake_bootstrap_generator}" = "Ninja"; then
jsoncpp_cxx_flags= jsoncpp_cxx_flags=
if test "x${bootstrap_system_jsoncpp}" = "x"; then if test "x${bootstrap_system_jsoncpp}" = "x"; then
@@ -1713,15 +1721,6 @@ if test "${cmake_bootstrap_generator}" = "Ninja"; then
fi fi
libs="${libs} -ljsoncpp" libs="${libs} -ljsoncpp"
fi fi
if test "x${bootstrap_system_librhash}" != "x"; then
if test `which pkg-config`; then
use_librhash_flags="`pkg-config --cflags librhash`"
cmake_c_flags="${cmake_c_flags} ${use_librhash_flags}"
cmake_cxx_flags="${cmake_cxx_flags} ${use_librhash_flags}"
fi
libs="${libs} -lrhash"
fi
fi fi
if test "x${cmake_ansi_cxx_flags}" != "x"; then if test "x${cmake_ansi_cxx_flags}" != "x"; then
@@ -1845,6 +1844,12 @@ if test "x${bootstrap_system_libuv}" = "x"; then
write_source_rule "c" "uv-`cmake_obj ${a}`" "${src}" "${uv_c_flags}" write_source_rule "c" "uv-`cmake_obj ${a}`" "${src}" "${uv_c_flags}"
done done
fi fi
if test "x${bootstrap_system_librhash}" = "x"; then
for a in ${LIBRHASH_C_SOURCES}; do
src=`cmake_escape_artifact "${cmake_source_dir}/Utilities/cmlibrhash/${a}"`
write_source_rule "c" "rhash-`cmake_obj ${a}`" "${src}" ""
done
fi
if test "${cmake_bootstrap_generator}" = "Ninja"; then if test "${cmake_bootstrap_generator}" = "Ninja"; then
if test "x${bootstrap_system_jsoncpp}" = "x"; then if test "x${bootstrap_system_jsoncpp}" = "x"; then
for a in ${JSONCPP_CXX_SOURCES}; do for a in ${JSONCPP_CXX_SOURCES}; do
@@ -1852,12 +1857,6 @@ if test "${cmake_bootstrap_generator}" = "Ninja"; then
write_source_rule "cxx" "jsoncpp-`cmake_obj ${a}`" "${src}" "${jsoncpp_cxx_flags}" write_source_rule "cxx" "jsoncpp-`cmake_obj ${a}`" "${src}" "${jsoncpp_cxx_flags}"
done done
fi fi
if test "x${bootstrap_system_librhash}" = "x"; then
for a in ${LIBRHASH_C_SOURCES}; do
src=`cmake_escape_artifact "${cmake_source_dir}/Utilities/cmlibrhash/${a}"`
write_source_rule "c" "rhash-`cmake_obj ${a}`" "${src}" ""
done
fi
fi fi
if test "${cmake_bootstrap_generator}" = "Ninja"; then if test "${cmake_bootstrap_generator}" = "Ninja"; then
echo " echo "