mirror of
https://github.com/MaskRay/ccls.git
synced 2025-06-08 01:04:54 +00:00
Implement temp dir creation on windows
Prerequisite for: Calling an external command to create compile_commands.json
This commit is contained in:
parent
7a79ed92b2
commit
f45b0f9c77
@ -18,6 +18,7 @@ limitations under the License.
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace ccls {
|
namespace ccls {
|
||||||
std::string NormalizePath(const std::string &path);
|
std::string NormalizePath(const std::string &path);
|
||||||
@ -32,4 +33,10 @@ std::string GetExternalCommandOutput(const std::vector<std::string> &command,
|
|||||||
std::string_view input);
|
std::string_view input);
|
||||||
|
|
||||||
void SpawnThread(void *(*fn)(void *), void *arg);
|
void SpawnThread(void *(*fn)(void *), void *arg);
|
||||||
|
|
||||||
|
std::optional<std::string> TryMakeTempDirectory();
|
||||||
|
|
||||||
|
/// Removes a directory and all its contents.
|
||||||
|
void RemoveDirectoryRecursive(const std::string& path);
|
||||||
|
|
||||||
} // namespace ccls
|
} // namespace ccls
|
||||||
|
@ -130,6 +130,27 @@ void SpawnThread(void *(*fn)(void *), void *arg) {
|
|||||||
pthread_create(&thd, &attr, fn, arg);
|
pthread_create(&thd, &attr, fn, arg);
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// callback function required for RemoveDirectoryRecursive()
|
||||||
|
static int nftwCallback(const char *name, const struct stat * /*unused*/,
|
||||||
|
int /*unused*/, FTW * /*unused*/) {
|
||||||
|
remove(name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RemoveDirectoryRecursive(const AbsolutePath &path) {
|
||||||
|
// https://stackoverflow.com/a/5467788/2192139
|
||||||
|
nftw(path.path.c_str(), nftwCallback, 64, FTW_DEPTH | FTW_PHYS);
|
||||||
|
}
|
||||||
|
|
||||||
|
optional<std::string> TryMakeTempDirectory() {
|
||||||
|
char tmpl[] = "/tmp/ccls-XXXXXX";
|
||||||
|
if(!mkdtemp(tmpl)) {
|
||||||
|
return nullopt;
|
||||||
|
}
|
||||||
|
return tmpl;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ccls
|
} // namespace ccls
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
#include "platform.hh"
|
#include "platform.hh"
|
||||||
|
|
||||||
#include "utils.hh"
|
#include "utils.hh"
|
||||||
|
#include "log.hh"
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
@ -65,6 +66,47 @@ std::string GetExternalCommandOutput(const std::vector<std::string> &command,
|
|||||||
void SpawnThread(void *(*fn)(void *), void *arg) {
|
void SpawnThread(void *(*fn)(void *), void *arg) {
|
||||||
std::thread(fn, arg).detach();
|
std::thread(fn, arg).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RemoveDirectoryRecursive(const std::string &path) {
|
||||||
|
std::string pathString = path; // create modifiable copy
|
||||||
|
// parameter 'pFrom' must be double NULL-terminated:
|
||||||
|
pathString.append(2, 0);
|
||||||
|
|
||||||
|
// We use SHFileOperation, because its FO_DELETE operation is recursive.
|
||||||
|
SHFILEOPSTRUCT op;
|
||||||
|
memset(&op, 0, sizeof(op));
|
||||||
|
op.wFunc = FO_DELETE;
|
||||||
|
op.pFrom = pathString.c_str();
|
||||||
|
op.fFlags = FOF_NO_UI;
|
||||||
|
SHFileOperation(&op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> TryMakeTempDirectory() {
|
||||||
|
// get "temp" dir
|
||||||
|
char tmpdir_buf[MAX_PATH];
|
||||||
|
DWORD len = GetTempPath(MAX_PATH, tmpdir_buf);
|
||||||
|
|
||||||
|
// Unfortunately, there is no mkdtemp() on windows. We append a (random)
|
||||||
|
// GUID to use as a unique directory name.
|
||||||
|
GUID guid;
|
||||||
|
CoCreateGuid(&guid);
|
||||||
|
// simplest way to append the guid to the existing c string:
|
||||||
|
len += snprintf(tmpdir_buf + len, MAX_PATH - len,
|
||||||
|
"ccls-%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
|
||||||
|
guid.Data1, guid.Data2, guid.Data3,
|
||||||
|
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
|
||||||
|
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
|
||||||
|
|
||||||
|
std::string dirPath(tmpdir_buf, len);
|
||||||
|
|
||||||
|
// finally, create the dir
|
||||||
|
const bool createSuccessful = (_mkdir(dirPath.c_str()) != -1) ||
|
||||||
|
(errno == EEXIST);
|
||||||
|
|
||||||
|
if(createSuccessful) return dirPath;
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -318,37 +318,41 @@ void Project::LoadDirectory(const std::string &root, Project::Folder &folder) {
|
|||||||
} else {
|
} else {
|
||||||
// If `compilationDatabaseCommand` is specified, execute it to get the
|
// If `compilationDatabaseCommand` is specified, execute it to get the
|
||||||
// compdb.
|
// compdb.
|
||||||
#ifdef _WIN32
|
|
||||||
// TODO
|
// generally it would be nice if we could just let clang load the
|
||||||
#else
|
// compilation database (compile_commands.json) from memory.
|
||||||
char tmpdir[] = "/tmp/ccls-compdb-XXXXXX";
|
// However, clang insists on reading compile_commands.json from a
|
||||||
if (!mkdtemp(tmpdir))
|
// directory, so we create a temporary directory just for clang to read
|
||||||
|
// from.
|
||||||
|
|
||||||
|
auto tmpdir = TryMakeTempDirectory();
|
||||||
|
if(!tmpdir.has_value()) {
|
||||||
|
LOG_S(ERROR) << "could not create temp directory for external "
|
||||||
|
"compile_commands.json command";
|
||||||
return;
|
return;
|
||||||
CDBDir = tmpdir;
|
}
|
||||||
|
|
||||||
|
CDBDir = tmpdir.value();
|
||||||
sys::path::append(Path, CDBDir, "compile_commands.json");
|
sys::path::append(Path, CDBDir, "compile_commands.json");
|
||||||
rapidjson::StringBuffer input;
|
rapidjson::StringBuffer input;
|
||||||
rapidjson::Writer<rapidjson::StringBuffer> writer(input);
|
rapidjson::Writer<rapidjson::StringBuffer> writer(input);
|
||||||
JsonWriter json_writer(&writer);
|
JsonWriter json_writer(&writer);
|
||||||
Reflect(json_writer, *g_config);
|
Reflect(json_writer, *g_config);
|
||||||
|
LOG_S(INFO) << "Starting external command " <<
|
||||||
|
g_config->compilationDatabaseCommand << " " << root;
|
||||||
std::string contents = GetExternalCommandOutput(
|
std::string contents = GetExternalCommandOutput(
|
||||||
std::vector<std::string>{g_config->compilationDatabaseCommand, root},
|
std::vector<std::string>{g_config->compilationDatabaseCommand, root},
|
||||||
input.GetString());
|
input.GetString());
|
||||||
FILE *fout = fopen(Path.c_str(), "wb");
|
FILE *fout = fopen(Path.c_str(), "wb");
|
||||||
fwrite(contents.c_str(), contents.size(), 1, fout);
|
fwrite(contents.c_str(), contents.size(), 1, fout);
|
||||||
fclose(fout);
|
fclose(fout);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string err_msg;
|
std::string err_msg;
|
||||||
std::unique_ptr<tooling::CompilationDatabase> CDB =
|
std::unique_ptr<tooling::CompilationDatabase> CDB =
|
||||||
tooling::CompilationDatabase::loadFromDirectory(CDBDir, err_msg);
|
tooling::CompilationDatabase::loadFromDirectory(CDBDir, err_msg);
|
||||||
if (!g_config->compilationDatabaseCommand.empty()) {
|
if (!g_config->compilationDatabaseCommand.empty()) {
|
||||||
#ifdef _WIN32
|
RemoveDirectoryRecursive(CDBDir.c_str());
|
||||||
// TODO
|
|
||||||
#else
|
|
||||||
unlink(Path.c_str());
|
|
||||||
rmdir(CDBDir.c_str());
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProjectProcessor proc(folder);
|
ProjectProcessor proc(folder);
|
||||||
|
Loading…
Reference in New Issue
Block a user