1bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao/* 2dfb74c5f597542c7587a4144eb31b143d82a2281Elliott Hughes * Copyright (C) 2016 The Android Open Source Project 3bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * 4bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * Licensed under the Apache License, Version 2.0 (the "License"); 5bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * you may not use this file except in compliance with the License. 6bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * You may obtain a copy of the License at 7bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * 8dfb74c5f597542c7587a4144eb31b143d82a2281Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * 10bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * Unless required by applicable law or agreed to in writing, software 11bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * distributed under the License is distributed on an "AS IS" BASIS, 12bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * See the License for the specific language governing permissions and 14bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao * limitations under the License. 15bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao */ 16bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao 17bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao#pragma once 18bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao 1916016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao#include <errno.h> 2016016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao#include <libgen.h> 2116016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao#include <sys/stat.h> 2216016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao#include <unistd.h> 2316016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 24bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao#include <string> 25bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao#include <vector> 26bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao 27bfb6bae9fb5ef42e4f9ae2847f30f41938c04900Josh Gao#include <llvm/ADT/StringRef.h> 28bfb6bae9fb5ef42e4f9ae2847f30f41938c04900Josh Gao 29bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gaostd::string getWorkingDir(); 303091f5a06e82186bbf8cfde3498fa9aef4d389eeJosh Gaostd::vector<std::string> collectHeaders(const std::string& directory); 31bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao 3216016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gaostatic inline std::string dirname(const std::string& path) { 3316016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao std::unique_ptr<char, decltype(&free)> path_copy(strdup(path.c_str()), free); 3416016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return dirname(path_copy.get()); 3516016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao} 3616016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 3716016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gaostatic inline bool is_directory(const std::string& path) { 3816016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao struct stat st; 3916016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao if (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) { 4016016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return true; 4116016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao } 4216016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return false; 4316016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao} 4416016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 4516016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gaostatic inline bool mkdirs(const std::string& path) { 4616016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao if (is_directory(path)) { 4716016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return true; 4816016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao } 4916016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 5016016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao std::string parent = dirname(path); 5116016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao if (parent == path) { 5216016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return false; 5316016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao } 5416016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 5516016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao if (!mkdirs(parent)) { 5616016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return false; 5716016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao } 5816016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 5916016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao if (mkdir(path.c_str(), 0700) != 0) { 6016016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao if (errno != EEXIST) { 6116016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return false; 6216016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao } 6316016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return is_directory(path); 6416016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao } 6516016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 6616016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao return true; 6716016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao} 6816016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gao 6916016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gaostatic inline std::string to_string(const char* c) { 70bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao return c; 71bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao} 72bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao 7316016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gaostatic inline const std::string& to_string(const std::string& str) { 74bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao return str; 75bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao} 76bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao 77bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gaotemplate <typename Collection> 7816016df79f846d6d21266c3b1dce1e7c24dc4be1Josh Gaostatic inline std::string Join(Collection c, const std::string& delimiter = ", ") { 79bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao std::string result; 80bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao for (const auto& item : c) { 81f8592a3ff3d452b7d784d34814e5d04c20348343Josh Gao using namespace std; 82bfb6bae9fb5ef42e4f9ae2847f30f41938c04900Josh Gao result.append(to_string(item)); 83bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao result.append(delimiter); 84bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao } 85bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao if (!result.empty()) { 86bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao result.resize(result.length() - delimiter.length()); 87bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao } 88bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao return result; 89bf8a285e64055ffd7e0c8b91519b75ff9ad94184Josh Gao} 90bfb6bae9fb5ef42e4f9ae2847f30f41938c04900Josh Gao 91bfb6bae9fb5ef42e4f9ae2847f30f41938c04900Josh Gaollvm::StringRef StripPrefix(llvm::StringRef string, llvm::StringRef prefix); 92