|
18 | 18 |
|
19 | 19 | #ifdef _MSC_VER |
20 | 20 | #include <io.h> |
| 21 | +#include <process.h> // For _spawnvp |
21 | 22 | #else |
22 | 23 | #include <cstdlib> |
| 24 | +#include <spawn.h> |
23 | 25 | #include <sys/mman.h> // For mmap |
| 26 | +#include <sys/wait.h> |
24 | 27 | #include <unistd.h> |
| 28 | +extern char **environ; |
25 | 29 | #endif |
| 30 | + |
26 | 31 | #include <sys/stat.h> |
27 | 32 | #include <sys/types.h> |
28 | 33 |
|
|
31 | 36 | #include <linux/limits.h> // For PATH_MAX |
32 | 37 | #include <ucontext.h> // For swapcontext |
33 | 38 | #endif |
| 39 | + |
34 | 40 | #if defined(_MSC_VER) && !defined(NOMINMAX) |
35 | 41 | #define NOMINMAX |
36 | 42 | #endif |
| 43 | + |
37 | 44 | #ifdef _WIN32 |
38 | 45 | #include <Objbase.h> // needed for CoCreateGuid |
39 | 46 | #include <Shlobj.h> // needed for SHGetFolderPath |
40 | 47 | #include <windows.h> |
41 | 48 | #else |
42 | 49 | #include <dlfcn.h> |
43 | 50 | #endif |
| 51 | + |
44 | 52 | #ifdef __APPLE__ |
45 | 53 | #define CAN_GET_RUNNING_PROGRAM_NAME |
46 | 54 | #include <mach-o/dyld.h> |
@@ -497,6 +505,32 @@ void write_entire_file(const std::string &pathname, const void *source, size_t s |
497 | 505 | f.close(); |
498 | 506 | } |
499 | 507 |
|
| 508 | +int run_process(std::vector<std::string> args) { |
| 509 | + internal_assert(!args.empty()) << "run_process called with empty args\n"; |
| 510 | + |
| 511 | + std::vector<char *> argv; |
| 512 | + argv.reserve(args.size() + 1); |
| 513 | + for (auto &a : args) { |
| 514 | + argv.push_back(a.data()); |
| 515 | + } |
| 516 | + argv.push_back(nullptr); |
| 517 | + |
| 518 | + debug(2) << "Running process: " << PrintSpan(args) << "\n"; |
| 519 | + |
| 520 | +#ifdef _WIN32 |
| 521 | + // Wait for completion; return the child's exit code. |
| 522 | + int rc = _spawnvp(_P_WAIT, argv[0], argv.data()); |
| 523 | + return (rc >= 0) ? rc : -1; |
| 524 | +#else |
| 525 | + pid_t pid = 0; |
| 526 | + int status = posix_spawnp(&pid, argv[0], nullptr, nullptr, argv.data(), environ); |
| 527 | + if (status != 0 || waitpid(pid, &status, 0) == -1) { |
| 528 | + return -1; |
| 529 | + } |
| 530 | + return WIFEXITED(status) ? WEXITSTATUS(status) : -1; |
| 531 | +#endif |
| 532 | +} |
| 533 | + |
500 | 534 | bool add_would_overflow(int bits, int64_t a, int64_t b) { |
501 | 535 | int64_t max_val = 0x7fffffffffffffffLL >> (64 - bits); |
502 | 536 | int64_t min_val = -max_val - 1; |
|
0 commit comments