xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/InitLLVM.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===-- InitLLVM.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h"
1004eeddc0SDimitry Andric #include "llvm/ADT/StringRef.h"
115f757f3fSDimitry Andric #include "llvm/Support/AutoConvert.h"
125f757f3fSDimitry Andric #include "llvm/Support/Error.h"
1304eeddc0SDimitry Andric #include "llvm/Support/ErrorHandling.h"
140b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
150b57cec5SDimitry Andric #include "llvm/Support/PrettyStackTrace.h"
160b57cec5SDimitry Andric #include "llvm/Support/Signals.h"
1704eeddc0SDimitry Andric #include "llvm/Support/SwapByteOrder.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #ifdef _WIN32
208c27c554SDimitry Andric #include "llvm/Support/Windows/WindowsSupport.h"
210b57cec5SDimitry Andric #endif
220b57cec5SDimitry Andric 
235f757f3fSDimitry Andric #ifdef __MVS__
245f757f3fSDimitry Andric #include <unistd.h>
255f757f3fSDimitry Andric 
CleanupStdHandles(void * Cookie)265f757f3fSDimitry Andric void CleanupStdHandles(void *Cookie) {
275f757f3fSDimitry Andric   llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs();
285f757f3fSDimitry Andric   Outs->flush();
295f757f3fSDimitry Andric   Errs->flush();
305f757f3fSDimitry Andric   llvm::restoreStdHandleAutoConversion(STDIN_FILENO);
315f757f3fSDimitry Andric   llvm::restoreStdHandleAutoConversion(STDOUT_FILENO);
325f757f3fSDimitry Andric   llvm::restoreStdHandleAutoConversion(STDERR_FILENO);
335f757f3fSDimitry Andric }
345f757f3fSDimitry Andric #endif
355f757f3fSDimitry Andric 
360b57cec5SDimitry Andric using namespace llvm;
370b57cec5SDimitry Andric using namespace llvm::sys;
380b57cec5SDimitry Andric 
InitLLVM(int & Argc,const char ** & Argv,bool InstallPipeSignalExitHandler)39480093f4SDimitry Andric InitLLVM::InitLLVM(int &Argc, const char **&Argv,
40e8d8bef9SDimitry Andric                    bool InstallPipeSignalExitHandler) {
41*7a6dacacSDimitry Andric #ifndef NDEBUG
42*7a6dacacSDimitry Andric   static std::atomic<bool> Initialized{false};
43*7a6dacacSDimitry Andric   assert(!Initialized && "InitLLVM was already initialized!");
44*7a6dacacSDimitry Andric   Initialized = true;
45*7a6dacacSDimitry Andric #endif
465f757f3fSDimitry Andric #ifdef __MVS__
475f757f3fSDimitry Andric   // Bring stdin/stdout/stderr into a known state.
485f757f3fSDimitry Andric   sys::AddSignalHandler(CleanupStdHandles, nullptr);
495f757f3fSDimitry Andric #endif
50480093f4SDimitry Andric   if (InstallPipeSignalExitHandler)
51e8d8bef9SDimitry Andric     // The pipe signal handler must be installed before any other handlers are
52e8d8bef9SDimitry Andric     // registered. This is because the Unix \ref RegisterHandlers function does
53e8d8bef9SDimitry Andric     // not perform a sigaction() for SIGPIPE unless a one-shot handler is
54e8d8bef9SDimitry Andric     // present, to allow long-lived processes (like lldb) to fully opt-out of
55e8d8bef9SDimitry Andric     // llvm's SIGPIPE handling and ignore the signal safely.
56480093f4SDimitry Andric     sys::SetOneShotPipeSignalFunction(sys::DefaultOneShotPipeSignalHandler);
57e8d8bef9SDimitry Andric   // Initialize the stack printer after installing the one-shot pipe signal
58e8d8bef9SDimitry Andric   // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested.
59e8d8bef9SDimitry Andric   StackPrinter.emplace(Argc, Argv);
600b57cec5SDimitry Andric   sys::PrintStackTraceOnErrorSignal(Argv[0]);
610b57cec5SDimitry Andric   install_out_of_memory_new_handler();
620b57cec5SDimitry Andric 
635f757f3fSDimitry Andric #ifdef __MVS__
645f757f3fSDimitry Andric 
655f757f3fSDimitry Andric   // We use UTF-8 as the internal character encoding. On z/OS, all external
665f757f3fSDimitry Andric   // output is encoded in EBCDIC. In order to be able to read all
675f757f3fSDimitry Andric   // error messages, we turn conversion to EBCDIC on for stderr fd.
685f757f3fSDimitry Andric   std::string Banner = std::string(Argv[0]) + ": ";
695f757f3fSDimitry Andric   ExitOnError ExitOnErr(Banner);
705f757f3fSDimitry Andric 
715f757f3fSDimitry Andric   // If turning on conversion for stderr fails then the error message
725f757f3fSDimitry Andric   // may be garbled. There is no solution to this problem.
735f757f3fSDimitry Andric   ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDERR_FILENO)));
745f757f3fSDimitry Andric   ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDOUT_FILENO)));
755f757f3fSDimitry Andric #endif
765f757f3fSDimitry Andric 
770b57cec5SDimitry Andric #ifdef _WIN32
780b57cec5SDimitry Andric   // We use UTF-8 as the internal character encoding. On Windows,
790b57cec5SDimitry Andric   // arguments passed to main() may not be encoded in UTF-8. In order
800b57cec5SDimitry Andric   // to reliably detect encoding of command line arguments, we use an
810b57cec5SDimitry Andric   // Windows API to obtain arguments, convert them to UTF-8, and then
820b57cec5SDimitry Andric   // write them back to the Argv vector.
830b57cec5SDimitry Andric   //
840b57cec5SDimitry Andric   // There's probably other way to do the same thing (e.g. using
850b57cec5SDimitry Andric   // wmain() instead of main()), but this way seems less intrusive
860b57cec5SDimitry Andric   // than that.
870b57cec5SDimitry Andric   std::string Banner = std::string(Argv[0]) + ": ";
880b57cec5SDimitry Andric   ExitOnError ExitOnErr(Banner);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args, Alloc)));
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   // GetCommandLineArguments doesn't terminate the vector with a
930b57cec5SDimitry Andric   // nullptr.  Do it to make it compatible with the real argv.
940b57cec5SDimitry Andric   Args.push_back(nullptr);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   Argc = Args.size() - 1;
970b57cec5SDimitry Andric   Argv = Args.data();
980b57cec5SDimitry Andric #endif
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
~InitLLVM()1015f757f3fSDimitry Andric InitLLVM::~InitLLVM() {
1025f757f3fSDimitry Andric #ifdef __MVS__
1035f757f3fSDimitry Andric   CleanupStdHandles(nullptr);
1045f757f3fSDimitry Andric #endif
1055f757f3fSDimitry Andric   llvm_shutdown();
1065f757f3fSDimitry Andric }
107