Skip to content

Commit 0df2450

Browse files
committed
Replace calls to system() with new run_process() helper.
1 parent c0ab358 commit 0df2450

5 files changed

Lines changed: 46 additions & 17 deletions

File tree

src/CodeGen_Metal_Dev.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -883,16 +883,11 @@ vector<char> CodeGen_Metal_Dev::compile_to_src() {
883883
// Compile the Metal source to a metallib.
884884
string metalir = tmpfile + ".ir";
885885
string metallib = tmpfile + "lib";
886-
string cmd = string(metal_compiler) + " -c -o " + metalir + " " + tmpfile;
887-
debug(2) << "Running: " << cmd << "\n";
888886

889-
int ret = system(cmd.c_str());
887+
int ret = run_process({metal_compiler, "-c", "-o", metalir, tmpfile});
890888
user_assert(ret == 0) << "Metal compiler set, but failed to compile Metal source to Metal IR.\n";
891889

892-
cmd = string(metal_linker) + " -o " + metallib + " " + metalir;
893-
debug(2) << "Running: " << cmd << "\n";
894-
895-
ret = system(cmd.c_str());
890+
ret = run_process({metal_linker, "-o", metallib, metalir});
896891
user_assert(ret == 0) << "Metal linker set, but failed to compile Metal IR to Metal library.\n";
897892

898893
// Read the metallib into a buffer.

src/CodeGen_PTX_Dev.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,8 @@ vector<char> CodeGen_PTX_Dev::compile_to_src() {
756756
f.write(buffer.data(), buffer.size());
757757
f.close();
758758

759-
string cmd = "ptxas --gpu-name " + mcpu_target() + " " + ptx.pathname() + " -o " + sass.pathname();
760-
if (system(cmd.c_str()) == 0) {
761-
cmd = "nvdisasm " + sass.pathname();
762-
int ret = system(cmd.c_str());
763-
(void)ret; // Don't care if it fails
759+
if (run_process({"ptxas", "--gpu-name", mcpu_target(), ptx.pathname(), "-o", sass.pathname()}) == 0) {
760+
(void)run_process({"nvdisasm", sass.pathname()}); // Don't care if it fails
764761
}
765762

766763
// Note: It works to embed the contents of the .sass file in

src/HexagonOffload.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,11 +1101,8 @@ Buffer<uint8_t> compile_module_to_hexagon_shared_object(const Module &device_cod
11011101
write_entire_file(input.pathname(), shared_object);
11021102

11031103
debug(1) << "Signing tool: (" << signer << ")\n";
1104-
std::string cmd = signer + " " + input.pathname() + " " + output.pathname();
1105-
int result = system(cmd.c_str());
1106-
internal_assert(result == 0)
1107-
<< "HL_HEXAGON_CODE_SIGNER failed: result = " << result
1108-
<< " for cmd (" << cmd << ")";
1104+
int result = run_process({signer, input.pathname(), output.pathname()});
1105+
internal_assert(result == 0) << "HL_HEXAGON_CODE_SIGNER failed: result = " << result;
11091106

11101107
shared_object = read_entire_file(output.pathname());
11111108
}

src/Util.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818

1919
#ifdef _MSC_VER
2020
#include <io.h>
21+
#include <process.h> // For _spawnvp
2122
#else
2223
#include <cstdlib>
24+
#include <spawn.h>
2325
#include <sys/mman.h> // For mmap
26+
#include <sys/wait.h>
2427
#include <unistd.h>
28+
extern char **environ;
2529
#endif
30+
2631
#include <sys/stat.h>
2732
#include <sys/types.h>
2833

@@ -31,16 +36,19 @@
3136
#include <linux/limits.h> // For PATH_MAX
3237
#include <ucontext.h> // For swapcontext
3338
#endif
39+
3440
#if defined(_MSC_VER) && !defined(NOMINMAX)
3541
#define NOMINMAX
3642
#endif
43+
3744
#ifdef _WIN32
3845
#include <Objbase.h> // needed for CoCreateGuid
3946
#include <Shlobj.h> // needed for SHGetFolderPath
4047
#include <windows.h>
4148
#else
4249
#include <dlfcn.h>
4350
#endif
51+
4452
#ifdef __APPLE__
4553
#define CAN_GET_RUNNING_PROGRAM_NAME
4654
#include <mach-o/dyld.h>
@@ -497,6 +505,32 @@ void write_entire_file(const std::string &pathname, const void *source, size_t s
497505
f.close();
498506
}
499507

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+
500534
bool add_would_overflow(int bits, int64_t a, int64_t b) {
501535
int64_t max_val = 0x7fffffffffffffffLL >> (64 - bits);
502536
int64_t min_val = -max_val - 1;

src/Util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ class TemporaryFile final {
371371
TemporaryFile &operator=(TemporaryFile &&) = delete;
372372
};
373373

374+
/** Run an executable with the given arguments, without going through the
375+
* shell. The first element of args should be the program name/path.
376+
* Returns the exit code of the process, or -1 if the process could
377+
* not be started. */
378+
int run_process(std::vector<std::string> args);
379+
374380
/** Routines to test if math would overflow for signed integers with
375381
* the given number of bits. */
376382
// @{

0 commit comments

Comments
 (0)