| 1 | //===- lld/Common/Driver.h - Linker Driver Emulator -----------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLD_COMMON_DRIVER_H |
| 10 | #define LLD_COMMON_DRIVER_H |
| 11 | |
| 12 | #include "llvm/ADT/ArrayRef.h" |
| 13 | #include "llvm/Support/raw_ostream.h" |
| 14 | |
| 15 | namespace lld { |
| 16 | enum Flavor { |
| 17 | Invalid, |
| 18 | Gnu, // -flavor gnu |
| 19 | MinGW, // -flavor gnu MinGW |
| 20 | WinLink, // -flavor link |
| 21 | Darwin, // -flavor darwin |
| 22 | Wasm, // -flavor wasm |
| 23 | }; |
| 24 | |
| 25 | using Driver = bool (*)(llvm::ArrayRef<const char *>, llvm::raw_ostream &, |
| 26 | llvm::raw_ostream &, bool, bool); |
| 27 | |
| 28 | struct DriverDef { |
| 29 | Flavor f; |
| 30 | Driver d; |
| 31 | }; |
| 32 | |
| 33 | struct Result { |
| 34 | int retCode; |
| 35 | bool canRunAgain; |
| 36 | }; |
| 37 | |
| 38 | // Generic entry point when using LLD as a library, safe for re-entry, supports |
| 39 | // crash recovery. Returns a general completion code and a boolean telling |
| 40 | // whether it can be called again. In some cases, a crash could corrupt memory |
| 41 | // and re-entry would not be possible anymore. Use exitLld() in that case to |
| 42 | // properly exit your application and avoid intermittent crashes on exit caused |
| 43 | // by cleanup. |
| 44 | Result lldMain(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS, |
| 45 | llvm::raw_ostream &stderrOS, llvm::ArrayRef<DriverDef> drivers); |
| 46 | } // namespace lld |
| 47 | |
| 48 | // With this macro, library users must specify which drivers they use, provide |
| 49 | // that information to lldMain() in the `drivers` param, and link the |
| 50 | // corresponding driver library in their executable. |
| 51 | #define LLD_HAS_DRIVER(name) \ |
| 52 | namespace lld { \ |
| 53 | namespace name { \ |
| 54 | bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS, \ |
| 55 | llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput); \ |
| 56 | } \ |
| 57 | } |
| 58 | |
| 59 | // An array which declares that all LLD drivers are linked in your executable. |
| 60 | // Must be used along with LLD_HAS_DRIVERS. See examples in LLD unittests. |
| 61 | #define LLD_ALL_DRIVERS \ |
| 62 | { \ |
| 63 | {lld::WinLink, &lld::coff::link}, {lld::Gnu, &lld::elf::link}, \ |
| 64 | {lld::MinGW, &lld::mingw::link}, {lld::Darwin, &lld::macho::link}, { \ |
| 65 | lld::Wasm, &lld::wasm::link \ |
| 66 | } \ |
| 67 | } |
| 68 | |
| 69 | #endif |
| 70 | |