1*e710425bSDimitry Andric //===- Utils.cpp ----------------------------------------------------------===//
2*e710425bSDimitry Andric //
3*e710425bSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e710425bSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e710425bSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e710425bSDimitry Andric //
7*e710425bSDimitry Andric //===----------------------------------------------------------------------===//
8*e710425bSDimitry Andric //
9*e710425bSDimitry Andric // Implements utility functions for TextAPI Darwin operations.
10*e710425bSDimitry Andric //
11*e710425bSDimitry Andric //===----------------------------------------------------------------------===//
12*e710425bSDimitry Andric 
13*e710425bSDimitry Andric #include "llvm/TextAPI/Utils.h"
14*e710425bSDimitry Andric 
15*e710425bSDimitry Andric using namespace llvm;
16*e710425bSDimitry Andric using namespace llvm::MachO;
17*e710425bSDimitry Andric 
replace_extension(SmallVectorImpl<char> & Path,const Twine & Extension)18*e710425bSDimitry Andric void llvm::MachO::replace_extension(SmallVectorImpl<char> &Path,
19*e710425bSDimitry Andric                                     const Twine &Extension) {
20*e710425bSDimitry Andric   StringRef P(Path.begin(), Path.size());
21*e710425bSDimitry Andric   auto ParentPath = sys::path::parent_path(P);
22*e710425bSDimitry Andric   auto Filename = sys::path::filename(P);
23*e710425bSDimitry Andric 
24*e710425bSDimitry Andric   if (!ParentPath.ends_with(Filename.str() + ".framework")) {
25*e710425bSDimitry Andric     sys::path::replace_extension(Path, Extension);
26*e710425bSDimitry Andric     return;
27*e710425bSDimitry Andric   }
28*e710425bSDimitry Andric   // Framework dylibs do not have a file extension, in those cases the new
29*e710425bSDimitry Andric   // extension is appended. e.g. given Path: "Foo.framework/Foo" and Extension:
30*e710425bSDimitry Andric   // "tbd", the result is "Foo.framework/Foo.tbd".
31*e710425bSDimitry Andric   SmallString<8> Storage;
32*e710425bSDimitry Andric   StringRef Ext = Extension.toStringRef(Storage);
33*e710425bSDimitry Andric 
34*e710425bSDimitry Andric   // Append '.' if needed.
35*e710425bSDimitry Andric   if (!Ext.empty() && Ext[0] != '.')
36*e710425bSDimitry Andric     Path.push_back('.');
37*e710425bSDimitry Andric 
38*e710425bSDimitry Andric   // Append extension.
39*e710425bSDimitry Andric   Path.append(Ext.begin(), Ext.end());
40*e710425bSDimitry Andric }
41