Commit e025495b authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'makefiles_additional_clean_files'

f945c3e7 Tests: Extend MakeClean test to cover subdirectories without targets
1ded3599 Makefiles: Process ADDTIONAL_CLEAN_FILES dir prop at directory level
827da111 Makefiles: Make build root targets "all", "clean" and "preinstall" recursive
adc34597 Makefiles: Avoid pointer repurposing
0d41b45c Makefiles: Inline range loop range arguments
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !3338
parents a9fb9a87 f945c3e7
Pipeline #137340 running with stage
This diff is collapsed.
......@@ -165,7 +165,8 @@ protected:
void WriteDirectoryRule2(std::ostream& ruleFileStream,
cmLocalUnixMakefileGenerator3* lg, const char* pass,
bool check_all, bool check_relink);
bool check_all, bool check_relink,
std::vector<std::string> const& commands = {});
void WriteDirectoryRules2(std::ostream& ruleFileStream,
cmLocalUnixMakefileGenerator3* lg);
......
......@@ -15,6 +15,7 @@
#include "cmCustomCommandGenerator.h"
#include "cmFileTimeCache.h"
#include "cmGeneratedFileStream.h"
#include "cmGeneratorExpression.h"
#include "cmGeneratorTarget.h"
#include "cmGlobalGenerator.h"
#include "cmGlobalUnixMakefileGenerator3.h"
......@@ -115,10 +116,9 @@ void cmLocalUnixMakefileGenerator3::Generate()
this->Makefile->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES");
// Generate the rule files for each target.
const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
for (cmGeneratorTarget* target : targets) {
for (cmGeneratorTarget* target : this->GetGeneratorTargets()) {
if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
continue;
}
......@@ -154,8 +154,7 @@ void cmLocalUnixMakefileGenerator3::ComputeHomeRelativeOutputPath()
void cmLocalUnixMakefileGenerator3::GetLocalObjectFiles(
std::map<std::string, LocalObjectInfo>& localObjectFiles)
{
const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
for (cmGeneratorTarget* gt : targets) {
for (cmGeneratorTarget* gt : this->GetGeneratorTargets()) {
if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
continue;
}
......@@ -353,9 +352,8 @@ void cmLocalUnixMakefileGenerator3::WriteLocalMakefileTargets(
// for each target we just provide a rule to cd up to the top and do a make
// on the target
const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
std::string localName;
for (cmGeneratorTarget* target : targets) {
for (cmGeneratorTarget* target : this->GetGeneratorTargets()) {
if ((target->GetType() == cmStateEnums::EXECUTABLE) ||
(target->GetType() == cmStateEnums::STATIC_LIBRARY) ||
(target->GetType() == cmStateEnums::SHARED_LIBRARY) ||
......@@ -1092,6 +1090,56 @@ void cmLocalUnixMakefileGenerator3::AppendCleanCommand(
}
}
void cmLocalUnixMakefileGenerator3::AppendDirectoryCleanCommand(
std::vector<std::string>& commands)
{
std::vector<std::string> cleanFiles;
// Look for additional files registered for cleaning in this directory.
if (const char* prop_value =
this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
cmGeneratorExpression ge;
std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop_value);
cmSystemTools::ExpandListArgument(
cge->Evaluate(this,
this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE")),
cleanFiles);
}
if (cleanFiles.empty()) {
return;
}
cmLocalGenerator* rootLG =
this->GetGlobalGenerator()->GetLocalGenerators().at(0);
std::string const& binaryDir = rootLG->GetCurrentBinaryDirectory();
std::string const& currentBinaryDir = this->GetCurrentBinaryDirectory();
std::string cleanfile = currentBinaryDir;
cleanfile += "/CMakeFiles/cmake_directory_clean.cmake";
// Write clean script
{
std::string cleanfilePath = cmSystemTools::CollapseFullPath(cleanfile);
cmsys::ofstream fout(cleanfilePath.c_str());
if (!fout) {
cmSystemTools::Error("Could not create " + cleanfilePath);
return;
}
fout << "file(REMOVE_RECURSE\n";
for (std::string const& cfl : cleanFiles) {
std::string fc = rootLG->MaybeConvertToRelativePath(
binaryDir, cmSystemTools::CollapseFullPath(cfl, currentBinaryDir));
fout << " " << cmOutputConverter::EscapeForCMake(fc) << "\n";
}
fout << ")\n";
}
// Create command
{
std::string remove = "$(CMAKE_COMMAND) -P ";
remove += this->ConvertToOutputFormat(
rootLG->MaybeConvertToRelativePath(binaryDir, cleanfile),
cmOutputConverter::SHELL);
commands.push_back(std::move(remove));
}
}
void cmLocalUnixMakefileGenerator3::AppendEcho(
std::vector<std::string>& commands, std::string const& text, EchoColor color,
EchoProgress const* progress)
......
......@@ -227,6 +227,7 @@ protected:
const std::set<std::string>& files,
cmGeneratorTarget* target,
const char* filename = nullptr);
void AppendDirectoryCleanCommand(std::vector<std::string>& commands);
// Helper methods for dependency updates.
bool ScanDependencies(std::string const& targetDir,
......
......@@ -172,18 +172,6 @@ void cmMakefileTargetGenerator::WriteTargetBuildRules()
this->CleanFiles.insert(files.begin(), files.end());
}
// Look for additional files registered for cleaning in this directory.
if (const char* prop_value =
this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
std::vector<std::string> const files = evaluatedFiles(prop_value);
// For relative path support
std::string const& binaryDir =
this->LocalGenerator->GetCurrentBinaryDirectory();
for (std::string const& cfl : files) {
this->CleanFiles.insert(cmSystemTools::CollapseFullPath(cfl, binaryDir));
}
}
// Look for additional files registered for cleaning in this target.
if (const char* prop_value =
this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
......
......@@ -2,45 +2,56 @@ cmake_minimum_required(VERSION 3.14)
project(ToClean)
# Utility variables
set(TSD ${ToClean_SOURCE_DIR})
set(TBD ${ToClean_BINARY_DIR})
set(CSD ${CMAKE_CURRENT_SOURCE_DIR})
set(CBD ${CMAKE_CURRENT_BINARY_DIR})
set(CLEAN_FILE_CONTENT "File registered for cleaning.\n")
# Lists build-time-generated files that should be cleaned away
set(TOCLEAN_FILES)
set_property(GLOBAL PROPERTY TOCLEAN_FILES "")
function(addCleanFile FILENAME)
set_property(GLOBAL APPEND PROPERTY TOCLEAN_FILES "${FILENAME}")
endfunction()
function(writeCleanFile FILENAME)
file(WRITE "${FILENAME}" ${CLEAN_FILE_CONTENT})
endfunction()
# Build a simple project whose compiled objects should be cleaned.
add_executable(toclean toclean.cxx)
list(APPEND TOCLEAN_FILES
"${TBD}${CMAKE_FILES_DIRECTORY}/toclean.dir/toclean.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
addCleanFile("${CBD}${CMAKE_FILES_DIRECTORY}/toclean.dir/toclean.cxx${CMAKE_CXX_OUTPUT_EXTENSION}")
# Create a post build custom command that copies the toclean output executable
# to a custom location
function(addToCleanPostBuildCopy FILENAME)
add_custom_command(TARGET toclean POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy $<TARGET_FILE:toclean> ${FILENAME})
endfunction()
# Create a custom command whose output should be cleaned.
set(CustomCommandFile "${TBD}/CustomCommandFile.txt")
set(CustomCommandFile "${CBD}/CustomCommandFile.txt")
add_custom_command(OUTPUT ${CustomCommandFile}
DEPENDS ${TSD}/toclean.cxx
DEPENDS ${CSD}/toclean.cxx
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${TSD}/toclean.cxx ${CustomCommandFile})
ARGS -E copy ${CSD}/toclean.cxx ${CustomCommandFile})
add_custom_target(generate ALL DEPENDS ${CustomCommandFile})
list(APPEND TOCLEAN_FILES ${CustomCommandFile})
addCleanFile(${CustomCommandFile})
### Tests ADDITIONAL_MAKE_CLEAN_FILES directory property
if("${CMAKE_GENERATOR}" MATCHES "Makefile")
# Create a file that must be registered for cleaning.
set(MakeDirPropFile "${TBD}/MakeDirPropFile.txt")
file(WRITE "${MakeDirPropFile}" ${CLEAN_FILE_CONTENT})
set(MakeDirPropFile "${CBD}/MakeDirPropFile.txt")
writeCleanFile("${MakeDirPropFile}")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${MakeDirPropFile}")
list(APPEND TOCLEAN_FILES "${MakeDirPropFile}")
addCleanFile(${MakeDirPropFile})
# Create a custom command whose output should be cleaned, but whose name
# is not known until generate-time
set(MakeDirPropExpFileRel "MakeDirProp_copy${CMAKE_EXECUTABLE_SUFFIX}")
set(MakeDirPropExpFile "$<TARGET_FILE_DIR:toclean>/${MakeDirPropExpFileRel}")
add_custom_command(TARGET toclean POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy $<TARGET_FILE:toclean> ${MakeDirPropExpFile})
addToCleanPostBuildCopy("${MakeDirPropExpFile}")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${MakeDirPropExpFile})
list(APPEND TOCLEAN_FILES "${TBD}/${MakeDirPropExpFileRel}")
addCleanFile("${CBD}/${MakeDirPropExpFileRel}")
endif()
......@@ -48,51 +59,52 @@ endif()
# Register a file path relative to the build directory
set(DirPropFileRel "DirPropFileRel.txt")
file(WRITE "${TBD}/${DirPropFileRel}" ${CLEAN_FILE_CONTENT})
writeCleanFile("${CBD}/${DirPropFileRel}")
set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${DirPropFileRel})
list(APPEND TOCLEAN_FILES "${TBD}/${DirPropFileRel}")
addCleanFile("${CBD}/${DirPropFileRel}")
# Register an absolute file path
set(DirPropFileAbs "${TBD}/DirPropFileAbs.txt")
file(WRITE "${DirPropFileAbs}" ${CLEAN_FILE_CONTENT})
set(DirPropFileAbs "${CBD}/DirPropFileAbs.txt")
writeCleanFile("${DirPropFileAbs}")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropFileAbs})
list(APPEND TOCLEAN_FILES "${DirPropFileAbs}")
addCleanFile("${DirPropFileAbs}")
# Create a custom command whose output should be cleaned, but whose name
# is not known until generate-time
set(DirPropExpFileRel "DirProp_copy${CMAKE_EXECUTABLE_SUFFIX}")
set(DirPropExpFile "$<TARGET_FILE_DIR:toclean>/${DirPropExpFileRel}")
add_custom_command(TARGET toclean POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy $<TARGET_FILE:toclean> ${DirPropExpFile})
addToCleanPostBuildCopy("${DirPropExpFile}")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropExpFile})
list(APPEND TOCLEAN_FILES "${TBD}/${DirPropExpFileRel}")
addCleanFile("${CBD}/${DirPropExpFileRel}")
### Tests ADDITIONAL_CLEAN_FILES target property
# Register a file path relative to the build directory
set(TgtPropFileRel "TargetPropFileRel.txt")
file(WRITE "${TBD}/${TgtPropFileRel}" ${CLEAN_FILE_CONTENT})
writeCleanFile("${CBD}/${TgtPropFileRel}")
set_target_properties(toclean PROPERTIES ADDITIONAL_CLEAN_FILES ${TgtPropFileRel})
list(APPEND TOCLEAN_FILES "${TBD}/${TgtPropFileRel}")
addCleanFile("${CBD}/${TgtPropFileRel}")
# Register an absolute file path
set(TgtPropFileAbs "${TBD}/TargetPropFileAbs.txt")
file(WRITE "${TgtPropFileAbs}" ${CLEAN_FILE_CONTENT})
set(TgtPropFileAbs "${CBD}/TargetPropFileAbs.txt")
writeCleanFile("${TgtPropFileAbs}")
set_property(TARGET toclean APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${TgtPropFileAbs})
list(APPEND TOCLEAN_FILES "${TgtPropFileAbs}")
addCleanFile("${TgtPropFileAbs}")
# Create a custom command whose output should be cleaned, but whose name
# is not known until generate-time
set(TgtPropExpFileRel "TgtProp_copy${CMAKE_EXECUTABLE_SUFFIX}")
set(TgtPropExpFile "$<TARGET_FILE_DIR:toclean>/${TgtPropExpFileRel}")
add_custom_command(TARGET toclean POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy $<TARGET_FILE:toclean> ${TgtPropExpFile})
addToCleanPostBuildCopy("${TgtPropExpFile}")
set_property(TARGET toclean APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${TgtPropExpFile})
list(APPEND TOCLEAN_FILES "${TBD}/${TgtPropExpFileRel}")
addCleanFile("${CBD}/${TgtPropExpFileRel}")
# Process subdirectory without targets
add_subdirectory(EmptySubDir)
# Configure a file listing these build-time-generated files.
configure_file(${TSD}/ToCleanFiles.cmake.in ${TBD}/ToCleanFiles.cmake @ONLY)
get_property(TOCLEAN_FILES GLOBAL PROPERTY TOCLEAN_FILES)
configure_file(${CSD}/ToCleanFiles.cmake.in ${CBD}/ToCleanFiles.cmake @ONLY)
cmake_minimum_required(VERSION 3.14)
# Subdirectory CMakeLists.txt without targets
set(CSD ${CMAKE_CURRENT_SOURCE_DIR})
set(CBD ${CMAKE_CURRENT_BINARY_DIR})
# Register a file path relative to the build directory
set(DirPropFileRel "DirPropFileRel.txt")
writeCleanFile("${CBD}/${DirPropFileRel}")
set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${DirPropFileRel})
addCleanFile("${CBD}/${DirPropFileRel}")
# Register an absolute file path
set(DirPropFileAbs "${CBD}/DirPropFileAbs.txt")
writeCleanFile("${DirPropFileAbs}")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES ${DirPropFileAbs})
addCleanFile("${DirPropFileAbs}")
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