#ifndef HALIDE_TEST_DIRS_H
#define HALIDE_TEST_DIRS_H
#include <string>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace Halide {
namespace Internal {
namespace Test {
inline std::string get_env_variable(const char *name) {
#ifdef _MSC_VER
char buf[MAX_PATH];
size_t read = 0;
if (getenv_s(&read, buf, name) != 0) read = 0;
if (read) {
return std::string(buf);
}
#else
char *buf = getenv(name);
if (buf) {
return std::string(buf);
}
#endif
return "";
}
inline std::string get_current_directory() {
#ifdef _WIN32
std::string dir;
char p[MAX_PATH];
DWORD ret = GetCurrentDirectoryA(MAX_PATH, p);
if (ret != 0) {
dir = p;
}
return dir;
#else
std::string dir;
char *p = getcwd(nullptr, 0);
if (p) {
dir = p;
free(p);
}
return dir;
#endif
}
}
inline std::string get_test_tmp_dir() {
std::string dir = Test::get_env_variable("TEST_TMPDIR");
if (dir.empty()) {
dir = Test::get_current_directory();
}
bool is_absolute = dir.size() >= 1 && dir[0] == '/';
char sep = '/';
#ifdef _WIN32
if (dir.size() >= 3 && dir[1] == ':' && (dir[2] == '\\' || dir[2] == '/')) {
is_absolute = true;
sep = dir[2];
}
#endif
if (!is_absolute) {
assert(false && "get_test_tmp_dir() is not an absolute path");
return "/unlikely_path/";
}
if (dir[dir.size() - 1] != sep) {
dir += sep;
}
return dir;
}
}
}
#endif