compile_src.cpp 1.26 KB
Newer Older
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
#include <migraphx/compile_src.hpp>
#include <migraphx/file_buffer.hpp>
#include <migraphx/tmp_dir.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/errors.hpp>
#include <cassert>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

std::vector<char> src_compiler::compile(const std::vector<src_file>& srcs) const
{
    assert(not srcs.empty());
    tmp_dir td{"compile"};
    auto params = flags;

    params += " -I.";

    auto out = output;

    for(const auto& src : srcs)
    {
        fs::path full_path   = td.path / src.path;
        fs::path parent_path = full_path.parent_path();
        fs::create_directories(parent_path);
        write_buffer(full_path.string(), src.content.first, src.len());
        if(src.path.extension().string() == ".cpp")
        {
            params += " " + src.path.filename().string();
            if(out.empty())
                out = src.path.stem().string() + ".o";
        }
    }

35
    params += " -o " + out;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    td.execute(compiler, params);

    auto out_path = td.path / out;
    if(not fs::exists(out_path))
        MIGRAPHX_THROW("Output file missing: " + out);

    if(process)
        out_path = process(out_path);

    return read_buffer(out_path.string());
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx