PDA

View Full Version : How do I get QMAKE_CLEAN to delete a directory?



Chuk
28th September 2007, 07:29
Is there a trick to getting QMAKE_CLEAN, or actually QMAKE_DISTCLEAN, to remove a directory? The generated Makefile has a DEL_DIR command, but everything in QMAKE_CLEAN gets tagged with DEL_FILE.

I would actually expect qmake to remove directories declared with, for example OBJECTS_DIR, when running "make distclean". But I would settle for a workaround.

wysota
28th September 2007, 10:04
make distclean should remove any directory that is created by running qmake, including OBJECTS_DIR. At least that's the case on Linux/gcc. Make clean won't do that as it only cleans temporary files.

Chuk
28th September 2007, 18:22
Oops, forgot to mention this under Windows. I havn't had a chance to check it on Linux yet.

There are a fair number of generated files that do not get deleted with "make distclean". I've added them to QMAKE_DISTCLEAN, it's just the directories that are left behind now. In Unix, rm will remove directories. In Windows, del will not remove directories. The generated Makefile has


DEL_FILE = del
DEL_DIR = rmdir

But the make clean section only uses DEL_FILE. I've used the line below to take care of emptying the directories without a prompt, but it still leaves the empty directory behind.


win32:QMAKE_DEL_FILE = del /q

I guess I'll go look for rm.exe and put that in there.

Chuk
29th September 2007, 23:31
I got it working, with a small patch to qmake. I've submitted it as a bug, since the option QMAKE_DEL_DIR exists but is never used.

For windows, it's in the file {QTDIR}\qmake\generators\win32\winmakefile.cpp

For version 4.3.1 it's at line 490, replace the distclean block with this one :



t << "distclean: clean";
{
const char *clean_targets[] = { "QMAKE_DISTCLEAN", 0 };
for(int i = 0; clean_targets[i]; ++i) {
const QStringList &list = project->values(clean_targets[i]);
const QString del_statement("-$(DEL_FILE)");
const QString del_dir_statement("-$(DEL_DIR)");

if(project->isActiveConfig("no_delete_multiple_files")) {
for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
t << "\n\t" << del_statement << " " << escapeFilePath((*it));
} else {
QString files, file;
QStringList dirs;

const int commandlineLimit = 2047; // NT limit, expanded
for(QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
if ( (*it).endsWith("/") )
{
QString s = (*it);
dirs += escapeFilePath(s.remove(s.size()-1, 1));
}
else
{
file = " " + escapeFilePath((*it));
if(del_statement.length() + files.length() +
qMax(fixEnvVariables(file).length(), file.length()) > commandlineLimit) {
t << "\n\t" << del_statement << files;
files.clear();
}
files += file;
}
}
if(!files.isEmpty())
t << "\n\t" << del_statement << files;
if(!dirs.isEmpty())
foreach(QString dir, dirs)
t << "\n\t" << del_dir_statement << " " << dir;
}
}
}


This allows the following to work in a .pro file (as I expect it to) :



QMAKE_DISTCLEAN += object_script.* bin/ build/
win32:QMAKE_DEL_FILE = del /q
win32:QMAKE_DEL_DIR = rmdir /s /q


Any file listed in QMAKE_DISTCLEAN that ends with a "/" will be get the QMAKE_DEL_DIR command, while anything without a "/" at the end gets the QMAKE_DEL_FILE command as it previously did.

MaikoID
31st March 2011, 18:25
Hi..

In Qt 4.7 the QMAKE_DEL_DIR can delete my temp created files ?

duthils
6th February 2012, 20:19
Got around this problem with a :

unix:QMAKE_DISTCLEAN += -r dir1 dir2
win32:QMAKE_DISTCLEAN += /s /f /q dir1 dir2 && rd /s /q dir1 dir2

Not very pretty, but does the job.