MCPcopy Create free account
hub / github.com/chigraph/chigraph / compileCCode

Function compileCCode

libchigraph/src/GraphModule.cpp:61–173  ·  view source on GitHub ↗

Compile C code to a llvm::Module

Source from the content-addressed store, hash-verified

59
60// Compile C code to a llvm::Module
61std::unique_ptr<llvm::Module> compileCCode(const char* execPath, boost::string_view code,
62 const std::vector<std::string>& args,
63 llvm::LLVMContext& ctx, Result& res) {
64 std::vector<const char*> cArgs;
65 for (const auto& arg : args) { cArgs.push_back(arg.c_str()); }
66 cArgs.push_back("-nostdlib");
67
68 std::string errors;
69
70 // call chi-ctollvm
71 std::unique_ptr<llvm::Module> mod;
72 {
73 std::vector<const char*> argsToChiCtoLLVM;
74 for (auto arg : cArgs) {
75 argsToChiCtoLLVM.push_back("-c");
76 argsToChiCtoLLVM.push_back(arg);
77 }
78
79 std::string generatedBitcode;
80 try {
81 exec_stream_t ctollvmExe;
82 ctollvmExe.set_wait_timeout(
83 exec_stream_t::s_out | exec_stream_t::s_err | exec_stream_t::s_in, 100'000);
84 ctollvmExe.set_buffer_limit(
85 exec_stream_t::s_out | exec_stream_t::s_err | exec_stream_t::s_in, 10'000'000);
86
87 auto exeLoc = fs::path(execPath).parent_path() /
88 "chi-ctollvm"
89#if WIN32
90 ".exe"
91#endif
92 ;
93 assert(fs::is_regular_file(exeLoc) &&
94 "chi-ctollvm isn't installed in the same directory as chi");
95
96 ctollvmExe.set_text_mode(exec_stream_t::s_in);
97 ctollvmExe.set_binary_mode(exec_stream_t::s_out);
98
99 ctollvmExe.start(exeLoc.string().c_str(), argsToChiCtoLLVM.begin(),
100 argsToChiCtoLLVM.end());
101
102 // push it the code and close the stream
103 ctollvmExe.in() << code;
104 if (!ctollvmExe.close_in()) {
105 res.addEntry("EUKN", "Failed to close input stream to chi-ctollvm", {});
106 return nullptr;
107 }
108
109 // get stderr and stdout
110 generatedBitcode = read_all(ctollvmExe.out(), res);
111 auto errs = read_all(ctollvmExe.err(), res);
112
113 if (!ctollvmExe.close()) {
114 res.addEntry("EUKN", "Failed to close chi-ctollvm process", {});
115 return nullptr;
116 }
117
118 auto errCode = ctollvmExe.exit_code();

Callers 3

codegenMethod · 0.85
generateModuleMethod · 0.85

Calls 7

c_strMethod · 0.80
startMethod · 0.80
beginMethod · 0.80
endMethod · 0.80
addEntryMethod · 0.80
getMethod · 0.80
read_allFunction · 0.70

Tested by

no test coverage detected