1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
//===--- ArgsToFrontendOutputsConverter.h -----------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_FRONTEND_ARGSTOFRONTENDOUTPUTSCONVERTER_H
#define SWIFT_FRONTEND_ARGSTOFRONTENDOUTPUTSCONVERTER_H
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SupplementaryOutputPaths.h"
#include "swift/Frontend/FrontendOptions.h"
#include "swift/Option/Options.h"
#include "llvm/Option/ArgList.h"
#include <vector>
namespace swift {
class OutputFileMap;
/// Given the command line arguments and information about the inputs,
/// Fill in all the information in FrontendInputsAndOutputs.
class ArgsToFrontendOutputsConverter {
const llvm::opt::ArgList &Args;
StringRef ModuleName;
FrontendInputsAndOutputs &InputsAndOutputs;
DiagnosticEngine &Diags;
public:
ArgsToFrontendOutputsConverter(const llvm::opt::ArgList &args,
StringRef moduleName,
FrontendInputsAndOutputs &inputsAndOutputs,
DiagnosticEngine &diags)
: Args(args), ModuleName(moduleName), InputsAndOutputs(inputsAndOutputs),
Diags(diags) {}
bool convert(std::vector<std::string> &mainOutputs,
std::vector<std::string> &mainOutputsForIndexUnits,
std::vector<SupplementaryOutputPaths> &supplementaryOutputs);
/// Try to read an output file list file.
/// \returns `None` if it could not open the filelist.
static std::optional<std::vector<std::string>>
readOutputFileList(StringRef filelistPath, DiagnosticEngine &diags);
};
struct OutputOptInfo {
StringRef PrettyName;
options::ID SingleID;
options::ID FilelistID;
StringRef SingleOptSpelling;
};
class OutputFilesComputer {
DiagnosticEngine &Diags;
const FrontendInputsAndOutputs &InputsAndOutputs;
const std::vector<std::string> OutputFileArguments;
const std::string OutputDirectoryArgument;
const std::string FirstInput;
const FrontendOptions::ActionType RequestedAction;
const llvm::opt::Arg *const ModuleNameArg;
const StringRef Suffix;
const bool HasTextualOutput;
const OutputOptInfo OutputInfo;
OutputFilesComputer(DiagnosticEngine &diags,
const FrontendInputsAndOutputs &inputsAndOutputs,
std::vector<std::string> outputFileArguments,
StringRef outputDirectoryArgument, StringRef firstInput,
FrontendOptions::ActionType requestedAction,
const llvm::opt::Arg *moduleNameArg, StringRef suffix,
bool hasTextualOutput,
OutputOptInfo optInfo);
public:
static std::optional<OutputFilesComputer>
create(const llvm::opt::ArgList &args, DiagnosticEngine &diags,
const FrontendInputsAndOutputs &inputsAndOutputs,
OutputOptInfo optInfo);
/// \return the output filenames on the command line or in the output
/// filelist. If there
/// were neither -o's nor an output filelist, returns an empty vector.
static std::optional<std::vector<std::string>>
getOutputFilenamesFromCommandLineOrFilelist(const llvm::opt::ArgList &args,
DiagnosticEngine &diags,
options::ID singleOpt,
options::ID filelistOpt);
std::optional<std::vector<std::string>> computeOutputFiles() const;
private:
std::optional<std::string> computeOutputFile(StringRef outputArg,
const InputFile &input) const;
/// \return the correct output filename when none was specified.
///
/// Such an absence should only occur when invoking the frontend
/// without the driver,
/// because the driver will always pass -o with an appropriate filename
/// if output is required for the requested action.
std::optional<std::string>
deriveOutputFileFromInput(const InputFile &input) const;
/// \return the correct output filename when a directory was specified.
///
/// Such a specification should only occur when invoking the frontend
/// directly, because the driver will always pass -o with an appropriate
/// filename if output is required for the requested action.
std::optional<std::string>
deriveOutputFileForDirectory(const InputFile &input) const;
std::string determineBaseNameOfOutput(const InputFile &input) const;
std::string deriveOutputFileFromParts(StringRef dir, StringRef base) const;
};
class SupplementaryOutputPathsComputer {
const llvm::opt::ArgList &Args;
DiagnosticEngine &Diags;
const FrontendInputsAndOutputs &InputsAndOutputs;
ArrayRef<std::string> OutputFiles;
StringRef ModuleName;
const FrontendOptions::ActionType RequestedAction;
public:
SupplementaryOutputPathsComputer(
const llvm::opt::ArgList &args, DiagnosticEngine &diags,
const FrontendInputsAndOutputs &inputsAndOutputs,
ArrayRef<std::string> outputFiles, StringRef moduleName);
std::optional<std::vector<SupplementaryOutputPaths>>
computeOutputPaths() const;
private:
/// \Return a set of supplementary output paths for each input that might
/// produce supplementary outputs, or None to signal an error.
/// \note
/// Batch-mode supports multiple primary inputs.
/// \par
/// The paths are derived from arguments
/// such as -emit-module-path. These are not the final computed paths,
/// merely the ones passed in via the command line.
/// \note
/// In the future, these will also include those passed in via whatever
/// filelist scheme gets implemented to handle cases where the command line
/// arguments become burdensome.
std::optional<std::vector<SupplementaryOutputPaths>>
getSupplementaryOutputPathsFromArguments() const;
/// Read a supplementary output file map file.
/// \returns `None` if it could not open the file map.
std::optional<std::vector<SupplementaryOutputPaths>>
readSupplementaryOutputFileMap() const;
/// Given an ID corresponding to supplementary output argument
/// (e.g. -emit-module-path), collect all such paths, and ensure
/// there are the right number of them.
std::optional<std::vector<std::string>>
getSupplementaryFilenamesFromArguments(options::ID pathID) const;
std::optional<SupplementaryOutputPaths> computeOutputPathsForOneInput(
StringRef outputFilename,
const SupplementaryOutputPaths &pathsFromFilelists,
const InputFile &) const;
StringRef deriveDefaultSupplementaryOutputPathExcludingExtension(
StringRef outputFilename, const InputFile &) const;
/// \return empty string if no output file.
std::string determineSupplementaryOutputFilename(
options::ID emitOpt, std::string pathFromArgumentsOrFilelists,
file_types::ID type, StringRef mainOutputIfUsable,
StringRef defaultSupplementaryOutputPathExcludingExtension) const;
void deriveModulePathParameters(StringRef mainOutputFile,
options::ID &emitOption,
std::string &extension,
std::string &mainOutputIfUsable) const;
};
} // namespace swift
#endif /* SWIFT_FRONTEND_ARGSTOFRONTENDOUTPUTSCONVERTER_H */
|