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

Function compile

chi/compile.cpp:34–139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32namespace po = boost::program_options;
33
34int compile(const std::vector<std::string>& opts) {
35 po::options_description compile_opts("compile options");
36 compile_opts.add_options()("input-file", po::value<std::string>(), "Input file")(
37 "output,o", po::value<std::string>(), "Output file, - for stdout")(
38 "output-type,t", po::value<std::string>(),
39 "The output type, either bc or ll. If an output file is defined, then this can be "
40 "inferred")("directory,C", po::value<std::string>(),
41 "Directory to cd to before doing anything")(
42 "no-dependencies,D", "Don't link the dependencies into the module");
43
44 po::positional_options_description pos;
45 pos.add("input-file", 1);
46
47 po::variables_map vm;
48 po::store(po::command_line_parser(opts).options(compile_opts).positional(pos).run(), vm);
49
50 if (vm.count("input-file") == 0) {
51 std::cerr << "chi compile: error: no input files" << std::endl;
52 return 1;
53 }
54
55 // cd first if it was specified
56 if (vm.count("directory")) { fs::current_path(vm["directory"].as<std::string>()); }
57
58 fs::path infile = vm["input-file"].as<std::string>();
59
60 Context c{fs::current_path()};
61
62 // add .chimod suffix if it doesn't have it
63 if (infile.extension().empty()) { infile.replace_extension(".chimod"); }
64
65 // resolve the path---first see if it's relative to the current directory. if it's not, then
66 // try to get it relative to 'src'
67 infile = fs::absolute(infile, fs::current_path());
68 if (!fs::is_regular_file(infile)) {
69 infile = fs::absolute(infile, c.workspacePath() / "src");
70
71 // if we still didn't find it, then error
72 if (!fs::is_regular_file(infile)) {
73 std::cerr << "chi compile: failed to find module: " << infile << std::endl;
74 return 1;
75 }
76 }
77
78 // get module name
79 auto moduleName = fs::relative(infile, c.workspacePath() / "src").replace_extension("");
80
81 Result res;
82
83 // load it as a module
84 ChiModule* chiModule;
85 res += c.loadModule(moduleName, chi::LoadSettings::Default, &chiModule);
86
87 if (!res) {
88 std::cerr << res << std::endl;
89 return 1;
90 }
91

Callers 1

mainFunction · 0.85

Calls 5

workspacePathMethod · 0.80
loadModuleMethod · 0.80
compileModuleMethod · 0.80
c_strMethod · 0.80
getMethod · 0.80

Tested by

no test coverage detected