Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
halide_test_dirs.h
Go to the documentation of this file.
1#ifndef HALIDE_TEST_DIRS_H
2#define HALIDE_TEST_DIRS_H
3
4// This file may be used by AOT tests, so it deliberately does not
5// include Halide.h
6
7#include <cassert>
8#include <string>
9
10#ifdef _WIN32
11#ifndef NOMINMAX
12#define NOMINMAX
13#endif
14#include <windows.h>
15#else
16#include <stdio.h>
17#include <sys/stat.h>
18#include <unistd.h>
19#endif
20
21namespace Halide {
22namespace Internal {
23
24namespace Test {
25
26inline std::string get_env_variable(const char *name) {
27#ifdef _MSC_VER
28 char buf[MAX_PATH];
29 size_t read = 0;
30 if (getenv_s(&read, buf, name) != 0) read = 0;
31 if (read) {
32 return std::string(buf);
33 }
34#else
35 char *buf = getenv(name);
36 if (buf) {
37 return std::string(buf);
38 }
39#endif
40 return "";
41}
42
43// Return absolute path to the current directory. Return empty string if
44// an error occurs. (Does not assert.)
45inline std::string get_current_directory() {
46#ifdef _WIN32
47 std::string dir;
48 char p[MAX_PATH];
49 DWORD ret = GetCurrentDirectoryA(MAX_PATH, p);
50 if (ret != 0) {
51 dir = p;
52 }
53 return dir;
54#else
55 std::string dir;
56 char *p = getcwd(nullptr, 0);
57 if (p) {
58 dir = p;
59 free(p);
60 }
61 return dir;
62#endif
63}
64
65} // namespace Test
66
67/** Return the path to a directory that can be safely written to
68 * when running tests; the contents directory may or may not outlast
69 * the lifetime of test itself (ie, the files may be cleaned up after test
70 * execution). The path is guaranteed to be an absolute path and end in
71 * a directory separator, so a leaf filename can simply be appended. It
72 * is not guaranteed that this directory will be empty. If the path cannot
73 * be created, the function will assert-fail and return an invalid path.
74 */
75inline std::string get_test_tmp_dir() {
76 // If TEST_TMPDIR is specified, we assume it is a valid absolute path
77 std::string dir = Test::get_env_variable("TEST_TMPDIR");
78 if (dir.empty()) {
79 // If not specified, use current dir.
81 }
82 bool is_absolute = dir.size() >= 1 && dir[0] == '/';
83 char sep = '/';
84#ifdef _WIN32
85 // Allow for C:\whatever or c:/whatever on Windows
86 if (dir.size() >= 3 && dir[1] == ':' && (dir[2] == '\\' || dir[2] == '/')) {
87 is_absolute = true;
88 sep = dir[2];
89 }
90#endif
91 if (!is_absolute) {
92 assert(false && "get_test_tmp_dir() is not an absolute path");
93 return "/unlikely_path/";
94 }
95 if (dir[dir.size() - 1] != sep) {
96 dir += sep;
97 }
98 return dir;
99}
100
101} // namespace Internal
102} // namespace Halide
103
104#endif // HALIDE_TEST_DIRS_H
unsigned long DWORD
Definition mini_d3d12.h:182
std::string get_env_variable(const char *name)
std::string get_current_directory()
std::string get_test_tmp_dir()
Return the path to a directory that can be safely written to when running tests; the contents directo...
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
void free(void *)
char * getenv(const char *)