2004-08-25 06:20:07 +00:00
|
|
|
//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-08-25 06:20:07 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-08-25 06:20:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This header file implements the operating system Path concept.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-08-29 05:24:01 +00:00
|
|
|
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2004-12-13 18:41:28 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2011-04-03 22:53:19 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2004-11-14 22:37:42 +00:00
|
|
|
#include <cassert>
|
2008-01-09 19:42:09 +00:00
|
|
|
#include <cstring>
|
2006-07-07 18:11:32 +00:00
|
|
|
#include <ostream>
|
|
|
|
using namespace llvm;
|
2004-08-29 05:24:01 +00:00
|
|
|
using namespace sys;
|
2011-04-03 22:53:19 +00:00
|
|
|
namespace {
|
|
|
|
using support::ulittle32_t;
|
|
|
|
}
|
2004-08-25 06:20:07 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
2005-04-21 22:55:34 +00:00
|
|
|
//=== independent code.
|
2004-08-25 06:20:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-21 21:20:07 +00:00
|
|
|
bool Path::operator==(const Path &that) const {
|
|
|
|
return path == that.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Path::operator<(const Path& that) const {
|
|
|
|
return path < that.path;
|
|
|
|
}
|
|
|
|
|
2004-12-13 03:00:39 +00:00
|
|
|
bool
|
|
|
|
Path::isArchive() const {
|
2011-12-13 23:17:12 +00:00
|
|
|
fs::file_magic type;
|
2011-01-15 20:39:36 +00:00
|
|
|
if (fs::identify_magic(str(), type))
|
|
|
|
return false;
|
2011-12-13 23:17:12 +00:00
|
|
|
return type == fs::file_magic::archive;
|
2004-12-13 03:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::isDynamicLibrary() const {
|
2011-12-13 23:17:12 +00:00
|
|
|
fs::file_magic type;
|
2011-01-15 20:39:36 +00:00
|
|
|
if (fs::identify_magic(str(), type))
|
|
|
|
return false;
|
|
|
|
switch (type) {
|
|
|
|
default: return false;
|
2011-12-13 23:17:12 +00:00
|
|
|
case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
|
|
|
|
case fs::file_magic::macho_dynamically_linked_shared_lib:
|
|
|
|
case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
|
|
|
|
case fs::file_magic::elf_shared_object:
|
|
|
|
case fs::file_magic::pecoff_executable: return true;
|
2011-01-15 20:39:36 +00:00
|
|
|
}
|
2004-12-13 03:00:39 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 22:45:45 +00:00
|
|
|
bool
|
|
|
|
Path::isObjectFile() const {
|
2011-12-13 23:17:12 +00:00
|
|
|
fs::file_magic type;
|
|
|
|
if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
|
2011-01-15 20:39:36 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
2010-09-15 22:45:45 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 02:46:41 +00:00
|
|
|
void
|
2010-11-02 22:18:37 +00:00
|
|
|
Path::appendSuffix(StringRef suffix) {
|
|
|
|
if (!suffix.empty()) {
|
|
|
|
path.append(".");
|
|
|
|
path.append(suffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-06 05:32:21 +00:00
|
|
|
bool
|
|
|
|
Path::isBitcodeFile() const {
|
2011-12-13 23:17:12 +00:00
|
|
|
fs::file_magic type;
|
2011-01-15 20:39:36 +00:00
|
|
|
if (fs::identify_magic(str(), type))
|
2007-05-06 05:32:21 +00:00
|
|
|
return false;
|
2011-12-13 23:17:12 +00:00
|
|
|
return type == fs::file_magic::bitcode;
|
2007-05-06 05:32:21 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 21:02:39 +00:00
|
|
|
bool Path::hasMagicNumber(StringRef Magic) const {
|
2007-05-06 05:32:21 +00:00
|
|
|
std::string actualMagic;
|
2008-05-05 18:30:58 +00:00
|
|
|
if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
|
2007-05-06 05:32:21 +00:00
|
|
|
return Magic == actualMagic;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-25 06:20:07 +00:00
|
|
|
// Include the truly platform-specific parts of this class.
|
2004-12-24 06:29:17 +00:00
|
|
|
#if defined(LLVM_ON_UNIX)
|
2005-01-09 23:29:00 +00:00
|
|
|
#include "Unix/Path.inc"
|
2004-12-24 06:29:17 +00:00
|
|
|
#endif
|
|
|
|
#if defined(LLVM_ON_WIN32)
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "Windows/Path.inc"
|
2004-12-24 06:29:17 +00:00
|
|
|
#endif
|