//===- Filesystem.cpp -----------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file contains a few utility functions to handle files. // //===----------------------------------------------------------------------===// #include "lld/Common/Filesystem.h" #include "lld/Common/ErrorHandler.h" #include "llvm/Config/llvm-config.h" #include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Parallel.h" #include "llvm/Support/Path.h" #include "llvm/Support/TimeProfiler.h" #if LLVM_ON_UNIX #include <unistd.h> #endif #include <thread> usingnamespacellvm; usingnamespacelld; // Removes a given file asynchronously. This is a performance hack, // so remove this when operating systems are improved. // // On Linux (and probably on other Unix-like systems), unlink(2) is a // noticeably slow system call. As of 2016, unlink takes 250 // milliseconds to remove a 1 GB file on ext4 filesystem on my machine. // // To create a new result file, we first remove existing file. So, if // you repeatedly link a 1 GB program in a regular compile-link-debug // cycle, every cycle wastes 250 milliseconds only to remove a file. // Since LLD can link a 1 GB binary in about 5 seconds, that waste // actually counts. // // This function spawns a background thread to remove the file. // The calling thread returns almost immediately. void lld::unlinkAsync(StringRef path) { … } // Simulate file creation to see if Path is writable. // // Determining whether a file is writable or not is amazingly hard, // and after all the only reliable way of doing that is to actually // create a file. But we don't want to do that in this function // because LLD shouldn't update any file if it will end in a failure. // We also don't want to reimplement heuristics to determine if a // file is writable. So we'll let FileOutputBuffer do the work. // // FileOutputBuffer doesn't touch a destination file until commit() // is called. We use that class without calling commit() to predict // if the given file is writable. std::error_code lld::tryCreateFile(StringRef path) { … } // Creates an empty file to and returns a raw_fd_ostream to write to it. std::unique_ptr<raw_fd_ostream> lld::openFile(StringRef file) { … } // The merged bitcode after LTO is large. Try opening a file stream that // supports reading, seeking and writing. Such a file allows BitcodeWriter to // flush buffered data to reduce memory consumption. If this fails, open a file // stream that supports only write. std::unique_ptr<raw_fd_ostream> lld::openLTOOutputFile(StringRef file) { … }