aboutsummaryrefslogtreecommitdiff
path: root/tools/driver/StandardLibrary.cc
blob: 7680054122bf26caba9121e311efa404e50dd6a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include "StandardLibrary.h"
#include "cps/Diagnostics.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"

namespace {
struct LibraryFile {
    const char *Name;
    const char *Data;
    size_t Size;
};
#include "StandardLibrary.inc"
}

StandardLibrary::~StandardLibrary() {
    if (!Directory.empty()) llvm::sys::fs::remove_directories(Directory);
}

bool StandardLibrary::load(std::vector<std::string> &Inputs) {
    auto Error = llvm::sys::fs::createUniqueDirectory("cpsc-std", Directory);
    if (Error) {
        llvm::errs() << cps::diagnosticPrefix(cps::Severity::Fatal)
                     << "cannot extract standard library: " << Error.message() << "\n";
        return false;
    }
    for (const auto &File : LibraryFiles) {
        llvm::SmallString<256> Path(Directory);
        llvm::sys::path::append(Path, File.Name);
        llvm::raw_fd_ostream Output(Path, Error);
        if (!Error) {
            Output.write(File.Data, File.Size);
            Output.close();
            Error = Output.error();
        }
        if (Error) {
            llvm::errs() << cps::diagnosticPrefix(cps::Severity::Fatal)
                         << "cannot extract standard library file '" << File.Name
                         << "': " << Error.message() << "\n";
            Output.clear_error();
            return false;
        }
        if (llvm::sys::path::extension(Path) == ".cps") Inputs.emplace_back(Path);
    }
    return true;
}