| 1 | //===- MultiArchCreateCLI.h -------------------------------------*- C++ -*-===// |
| 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 | // Declares the CLI action class for `clang-ssaf-linker multi-arch create`. |
| 10 | // Bundles StaticLibrary/MultiArchStaticLibrary inputs into one |
| 11 | // MultiArchStaticLibrary, or LUSummaryEncoding/MultiArchSharedLibrary inputs |
| 12 | // into one MultiArchSharedLibrary. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_MULTIARCHCREATECLI_H |
| 17 | #define LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_MULTIARCHCREATECLI_H |
| 18 | |
| 19 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/LUSummaryEncoding.h" |
| 20 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/MultiArchSharedLibrary.h" |
| 21 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/MultiArchStaticLibrary.h" |
| 22 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/StaticLibrary.h" |
| 23 | #include "clang/ScalableStaticAnalysis/Core/Model/BuildNamespace.h" |
| 24 | #include "clang/ScalableStaticAnalysis/Core/Serialization/SerializationFormat.h" |
| 25 | #include "clang/ScalableStaticAnalysis/Tool/Utils.h" |
| 26 | #include "llvm/ADT/ArrayRef.h" |
| 27 | #include "llvm/ADT/DenseMap.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
| 29 | #include "llvm/Support/Timer.h" |
| 30 | #include <memory> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace clang::ssaf { |
| 35 | |
| 36 | /// Runs the `multi-arch create` action for `clang-ssaf-linker`. |
| 37 | class MultiArchCreateCLI { |
| 38 | public: |
| 39 | /// Orchestrates validation, construction, and serialization of the MultiArch |
| 40 | /// artifact. |
| 41 | void run(llvm::TimerGroup &TG, llvm::ArrayRef<std::string> InputPaths, |
| 42 | llvm::StringRef OutputPath, bool Verbose, bool Time); |
| 43 | |
| 44 | private: |
| 45 | /// Validates the output path and every input path. |
| 46 | void validate(unsigned Level, llvm::Timer &TValidate); |
| 47 | |
| 48 | /// Constructs the artifact from the inputs. |
| 49 | ArtifactEncoding create(unsigned Level, llvm::Timer &TRead, |
| 50 | llvm::Timer &TBundle); |
| 51 | |
| 52 | /// Reads Artifact from file at \p Index. |
| 53 | ArtifactEncoding readInput(size_t Index, unsigned Level, llvm::Timer &TRead); |
| 54 | |
| 55 | /// Returns the namespace of the static-family input. |
| 56 | static const BuildNamespace &staticFamilyNamespace(const ArtifactEncoding &E); |
| 57 | |
| 58 | /// Returns the namespace of the shared-family input. |
| 59 | static const NestedBuildNamespace & |
| 60 | sharedFamilyNamespace(const ArtifactEncoding &E); |
| 61 | |
| 62 | /// Constructs a MultiArchStaticLibrary from the inputs. |
| 63 | ArtifactEncoding createStaticLibrary(ArtifactEncoding First, unsigned Level, |
| 64 | llvm::Timer &TRead, |
| 65 | llvm::Timer &TBundle); |
| 66 | |
| 67 | /// Constructs a MultiArchSharedLibrary from the inputs. |
| 68 | ArtifactEncoding createSharedLibrary(ArtifactEncoding First, unsigned Level, |
| 69 | llvm::Timer &TRead, |
| 70 | llvm::Timer &TBundle); |
| 71 | |
| 72 | /// Adds one input to the \p Bundle. |
| 73 | void addStaticInput(MultiArchStaticLibrary &Bundle, ArtifactEncoding Encoding, |
| 74 | size_t Index, unsigned Level, llvm::Timer &TBundle); |
| 75 | void addSharedInput(MultiArchSharedLibrary &Bundle, ArtifactEncoding Encoding, |
| 76 | size_t Index, unsigned Level, llvm::Timer &TBundle); |
| 77 | |
| 78 | /// Inserts one member, failing on duplicate target triple. |
| 79 | void addStaticMember(MultiArchStaticLibrary &Bundle, |
| 80 | std::unique_ptr<StaticLibrary> Member, |
| 81 | llvm::StringRef SourceFile); |
| 82 | void addSharedMember(MultiArchSharedLibrary &Bundle, |
| 83 | std::unique_ptr<LUSummaryEncoding> Member, |
| 84 | llvm::StringRef SourceFile); |
| 85 | |
| 86 | /// Serializes the artifact to the validated output path. |
| 87 | void write(const ArtifactEncoding &Bundle, unsigned Level, |
| 88 | llvm::Timer &TWrite); |
| 89 | |
| 90 | // Arguments captured by run() before dispatching to bundling methods. |
| 91 | // InputPaths and OutputPath are non-owning: they alias the driver's cl::opt |
| 92 | // storage, which outlives the call. |
| 93 | llvm::ArrayRef<std::string> InputPaths; |
| 94 | llvm::StringRef OutputPath; |
| 95 | bool Verbose = false; |
| 96 | bool Time = false; |
| 97 | |
| 98 | // State populated during validate() and consumed by later phases. |
| 99 | FormatFile OutputFile; |
| 100 | std::vector<FormatFile> InputFiles; |
| 101 | |
| 102 | /// Maps each inserted member to the input file that contributed it, so a |
| 103 | /// duplicate can name both contributors. Keyed on the member's address, not |
| 104 | /// its triple spelling: the member sets are keyed by Triple enum components, |
| 105 | /// which fold alias spellings ("arm64" / "aarch64") and OS versions, so two |
| 106 | /// colliding slices can carry triple strings that differ. The values alias |
| 107 | /// InputFiles[I].Path, which outlives the run. |
| 108 | llvm::DenseMap<const void *, llvm::StringRef> SourceByMember; |
| 109 | }; |
| 110 | |
| 111 | } // namespace clang::ssaf |
| 112 | |
| 113 | #endif // LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_MULTIARCHCREATECLI_H |
| 114 | |