aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
authorGravatar bbaren <bbaren@google.com>2018-07-17 16:02:09 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-17 16:03:55 -0700
commitf341e0d376cdb517a2fb6c68d835657c89e46b9a (patch)
treea29239c52a8ba4d1e267ce23fdace3eec22831f2 /tools
parent4d10250291a813302de64151be3b22d57e94749d (diff)
Revert "Support RAII in wrapped_clang"
This reverts commit 202658759, which does not correctly propagate Clang error codes. PiperOrigin-RevId: 204991287
Notes
The commit message should begin "This reverts commit f24958a11e3a54f563429aadc12bce033be047fa ...".
Diffstat (limited to 'tools')
-rw-r--r--tools/osx/crosstool/wrapped_clang.cc98
1 files changed, 50 insertions, 48 deletions
diff --git a/tools/osx/crosstool/wrapped_clang.cc b/tools/osx/crosstool/wrapped_clang.cc
index 232f082dc1..5f283a37fa 100644
--- a/tools/osx/crosstool/wrapped_clang.cc
+++ b/tools/osx/crosstool/wrapped_clang.cc
@@ -81,10 +81,19 @@ std::vector<const char *> ConvertToCArgs(const std::vector<std::string> &args) {
return c_args;
}
-// Spawns a subprocess for given arguments args. The first argument is used for
-// the executable path. Returns zero on success and an appropriate error number
-// otherwise.
-int RunSubProcess(const std::vector<std::string> &args) {
+// Turn our current process into a new process. Avoids fork overhead.
+// Never returns.
+void ExecProcess(const std::vector<std::string> &args) {
+ std::vector<const char *> exec_argv = ConvertToCArgs(args);
+ execv(args[0].c_str(), const_cast<char **>(exec_argv.data()));
+ std::cerr << "Error executing child process.'" << args[0] << "'. "
+ << strerror(errno) << "\n";
+ abort();
+}
+
+// Spawns a subprocess for given arguments args. The first argument is used
+// for the executable path.
+void RunSubProcess(const std::vector<std::string> &args) {
std::vector<const char *> exec_argv = ConvertToCArgs(args);
pid_t pid;
int status = posix_spawn(&pid, args[0].c_str(), NULL, NULL,
@@ -97,19 +106,18 @@ int RunSubProcess(const std::vector<std::string> &args) {
if (wait_status < 0) {
std::cerr << "Error waiting on child process '" << args[0] << "'. "
<< strerror(errno) << "\n";
- return errno;
+ abort();
}
if (WEXITSTATUS(status) != 0) {
std::cerr << "Error in child process '" << args[0] << "'. "
<< WEXITSTATUS(status) << "\n";
- return errno;
+ abort();
}
} else {
std::cerr << "Error forking process '" << args[0] << "'. "
<< strerror(status) << "\n";
- return status;
+ abort();
}
- return 0;
}
// Finds and replaces all instances of oldsub with newsub, in-place on str.
@@ -135,13 +143,13 @@ bool SetArgIfFlagPresent(const std::string &arg, const std::string &flagname,
return false;
}
-// Gets an environment variable in the current process environment. Logs an
-// error and returns the empty string if the variable is undefined.
+// Returns the DEVELOPER_DIR environment variable in the current process
+// environment. Aborts if this variable is unset.
std::string GetMandatoryEnvVar(const std::string &var_name) {
char *env_value = getenv(var_name.c_str());
- if (env_value == nullptr || *env_value == '\0') {
+ if (env_value == nullptr) {
std::cerr << "Error: " << var_name << " not set.\n";
- return "";
+ abort();
}
return env_value;
}
@@ -160,17 +168,11 @@ int main(int argc, char *argv[]) {
std::cerr << "Binary must either be named 'wrapped_clang' or "
"'wrapped_clang_pp', not "
<< binary_name << "\n";
- return EXIT_FAILURE;
+ abort();
}
std::string developer_dir = GetMandatoryEnvVar("DEVELOPER_DIR");
- if (developer_dir.empty()) {
- return EXIT_FAILURE;
- }
std::string sdk_root = GetMandatoryEnvVar("SDKROOT");
- if (sdk_root.empty()) {
- return EXIT_FAILURE;
- }
std::vector<std::string> processed_args = {"/usr/bin/xcrun", tool_name};
@@ -214,47 +216,47 @@ int main(int argc, char *argv[]) {
std::cerr << "Error in clang wrapper: If any dsym "
"hint is defined, then "
<< missing_dsym_flag << " must be defined\n";
- return EXIT_FAILURE;
+ abort();
} else {
postprocess = true;
dsyms_use_zip_file = !(dsym_bundle_zip.empty());
}
}
- if (RunSubProcess(processed_args) != 0) {
- return EXIT_FAILURE;
+ if (!postprocess) {
+ ExecProcess(processed_args);
+ std::cerr << "ExecProcess should not return. Please fix!\n";
+ abort();
}
- if (postprocess) {
- std::vector<std::string> dsymutil_args = {"/usr/bin/xcrun", "dsymutil",
- linked_binary, "-o", dsym_path};
- if (!dsyms_use_zip_file) {
- dsymutil_args.push_back("--flat");
- }
+ RunSubProcess(processed_args);
- if (RunSubProcess(dsymutil_args) != 0) {
- return EXIT_FAILURE;
- }
+ std::vector<std::string> dsymutil_args = {"/usr/bin/xcrun", "dsymutil",
+ linked_binary, "-o", dsym_path};
+ if (!dsyms_use_zip_file) {
+ dsymutil_args.push_back("--flat");
+ }
- if (dsyms_use_zip_file) {
- std::unique_ptr<char, decltype(std::free) *> cwd{getcwd(nullptr, 0),
- std::free};
- if (cwd == nullptr) {
- std::cerr << "Error determining current working directory\n";
- return EXIT_FAILURE;
- }
- std::vector<std::string> zip_args = {
- "/usr/bin/zip", "-q", "-r",
- std::string(cwd.get()) + "/" + dsym_bundle_zip, "."};
- if (chdir(dsym_path.c_str()) < 0) {
- std::cerr << "Error changing directory to '" << dsym_path << "'\n";
- return EXIT_FAILURE;
- }
+ RunSubProcess(dsymutil_args);
- if (RunSubProcess(zip_args) != 0) {
- return EXIT_FAILURE;
- }
+ if (dsyms_use_zip_file) {
+ std::unique_ptr<char, decltype(std::free) *> cwd{getcwd(nullptr, 0),
+ std::free};
+ if (cwd == nullptr) {
+ std::cerr << "Error determining current working directory\n";
+ abort();
+ }
+ std::vector<std::string> zip_args = {
+ "/usr/bin/zip", "-q", "-r",
+ std::string(cwd.get()) + "/" + dsym_bundle_zip, "."};
+ if (chdir(dsym_path.c_str()) < 0) {
+ std::cerr << "Error changing directory to '" << dsym_path << "'\n";
+ abort();
}
+
+ ExecProcess(zip_args);
+ std::cerr << "ExecProcess should not return. Please fix!\n";
+ abort();
}
return 0;