blob: 9b45dcd49ad262bbb5cfa2fd4ad8fc88a0e2b74c [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
Elliott Hughesdfb74c52016-10-24 12:53:17 -07002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaobf8a2852016-05-27 11:59:09 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
Elliott Hughesdfb74c52016-10-24 12:53:17 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Josh Gaobf8a2852016-05-27 11:59:09 -07009 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Josh Gao16016df2016-11-07 18:27:16 -080019#include <errno.h>
20#include <libgen.h>
21#include <sys/stat.h>
22#include <unistd.h>
23
Josh Gaobf8a2852016-05-27 11:59:09 -070024#include <string>
George Burgess IVb97049c2017-07-24 15:05:05 -070025#include <unordered_set>
Josh Gaobf8a2852016-05-27 11:59:09 -070026#include <vector>
27
Josh Gaobfb6bae2016-07-15 17:25:21 -070028#include <llvm/ADT/StringRef.h>
29
Josh Gaobf8a2852016-05-27 11:59:09 -070030std::string getWorkingDir();
George Burgess IVb97049c2017-07-24 15:05:05 -070031std::vector<std::string> collectHeaders(const std::string& directory,
32 const std::unordered_set<std::string>& ignored_directories);
Josh Gaobf8a2852016-05-27 11:59:09 -070033
Josh Gao16016df2016-11-07 18:27:16 -080034static inline std::string dirname(const std::string& path) {
35 std::unique_ptr<char, decltype(&free)> path_copy(strdup(path.c_str()), free);
36 return dirname(path_copy.get());
37}
38
39static inline bool is_directory(const std::string& path) {
40 struct stat st;
41 if (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) {
42 return true;
43 }
44 return false;
45}
46
47static inline bool mkdirs(const std::string& path) {
48 if (is_directory(path)) {
49 return true;
50 }
51
52 std::string parent = dirname(path);
53 if (parent == path) {
54 return false;
55 }
56
57 if (!mkdirs(parent)) {
58 return false;
59 }
60
61 if (mkdir(path.c_str(), 0700) != 0) {
62 if (errno != EEXIST) {
63 return false;
64 }
65 return is_directory(path);
66 }
67
68 return true;
69}
70
71static inline std::string to_string(const char* c) {
Josh Gaobf8a2852016-05-27 11:59:09 -070072 return c;
73}
74
Josh Gao16016df2016-11-07 18:27:16 -080075static inline const std::string& to_string(const std::string& str) {
Josh Gaobf8a2852016-05-27 11:59:09 -070076 return str;
77}
Josh Gaobf8a2852016-05-27 11:59:09 -070078
79template <typename Collection>
Josh Gao16016df2016-11-07 18:27:16 -080080static inline std::string Join(Collection c, const std::string& delimiter = ", ") {
Josh Gaobf8a2852016-05-27 11:59:09 -070081 std::string result;
82 for (const auto& item : c) {
Josh Gaof8592a32016-07-26 18:58:27 -070083 using namespace std;
Josh Gaobfb6bae2016-07-15 17:25:21 -070084 result.append(to_string(item));
Josh Gaobf8a2852016-05-27 11:59:09 -070085 result.append(delimiter);
86 }
87 if (!result.empty()) {
88 result.resize(result.length() - delimiter.length());
89 }
90 return result;
91}
Josh Gaobfb6bae2016-07-15 17:25:21 -070092
93llvm::StringRef StripPrefix(llvm::StringRef string, llvm::StringRef prefix);