[Feature] --clear and --clean flags
I have been doing an experiment with implementing a --clean
and --clear
command. I ran into this issue on Windows where I was trying various compilers and had to often clear the cache to test if everything worked out of the box when doing cmake ..
.
- The
--clean
command deletesCMakeCache.txt
andCMakeFiles/**
(this is to prevent deletion of_deps
and other downloaded things that I would like to keep) - The
--clear
command doesrm -rf *
(there is no nice command for this in Windows and it's easier to have it in CMake because I only ever use it to clear abuild
directory)
I know you can probably do this with cmake -E
, but it would be cool if you could have an alias.clean
command like git.
If anyone would like to try here is part of the code that implements it:
HOOK_ENTRYPOINT()
{
dlog();
auto commandLine = GetCommandLineW();
if (wcsstr(commandLine, L" --clean"))
{
auto FileExists = [](const wchar_t* szFileName)
{
return GetFileAttributesW(szFileName) != INVALID_FILE_ATTRIBUTES;
};
bool cacheDeleted = true;
if (FileExists(L"CMakeCache.txt"))
{
if (system("del CMakeCache.txt > nul 2>&1") != 0)
{
cacheDeleted = false;
puts("Failed to delete CMakeCache.txt");
}
}
bool filesDeleted = true;
if (FileExists(L"CMakeFiles"))
{
if (system("rmdir /q /s CMakeFiles") != 0)
{
filesDeleted = false;
}
}
return filesDeleted && cacheDeleted ? EXIT_SUCCESS : EXIT_FAILURE;
}
else if (wcsstr(commandLine, L"--clear"))
{
// TODO: nicer error handling
// Thanks to Jonas for the help with the command
system("rmdir /s /q . > nul 2>&1 & dir /b");
return 0;
}
return original_EntryPoint();
}
(It will also properly signal to the user if there was any kind of issue deleting files, which happens when you leave Visual Studio open for instance).