Halide 22.0.0 Halide compiler and libraries

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