From f341e0d376cdb517a2fb6c68d835657c89e46b9a Mon Sep 17 00:00:00 2001 From: bbaren Date: Tue, 17 Jul 2018 16:02:09 -0700 Subject: Revert "Support RAII in wrapped_clang" This reverts commit 202658759, which does not correctly propagate Clang error codes. PiperOrigin-RevId: 204991287 --- tools/osx/crosstool/wrapped_clang.cc | 98 ++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 48 deletions(-) (limited to 'tools') 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 ConvertToCArgs(const std::vector &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 &args) { +// Turn our current process into a new process. Avoids fork overhead. +// Never returns. +void ExecProcess(const std::vector &args) { + std::vector exec_argv = ConvertToCArgs(args); + execv(args[0].c_str(), const_cast(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 &args) { std::vector 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 &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 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 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 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 cwd{getcwd(nullptr, 0), - std::free}; - if (cwd == nullptr) { - std::cerr << "Error determining current working directory\n"; - return EXIT_FAILURE; - } - std::vector 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 cwd{getcwd(nullptr, 0), + std::free}; + if (cwd == nullptr) { + std::cerr << "Error determining current working directory\n"; + abort(); + } + std::vector 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; -- cgit v1.2.3