tan  0.0.1
cli_main.cpp
1 #include "cli/cli.h"
2 #include "tan/tan.h"
3 #include "llvm/Support/CommandLine.h"
4 #include <iostream>
5 #include <filesystem>
6 #include "config.h"
7 #include "driver/driver.h"
8 
9 namespace cmd = llvm::cl;
10 namespace fs = std::filesystem;
11 using namespace tanlang;
12 
13 int cli_main(int argc, char **argv) {
14  // cmd parser
15  cmd::OptionCategory cl_category("tanc");
16  cmd::opt<str> opt_output_file(
17  "o", cmd::desc("Output filename"), cmd::value_desc("output"), cmd::init("a.out"), cmd::cat(cl_category));
18  cmd::list<str> opt_link_libraries(
19  "l", cmd::desc("Libraries to link against"), cmd::value_desc("libraries"), cmd::Prefix, cmd::cat(cl_category));
20  cmd::list<str> opt_library_path("L", cmd::desc("Library search path"), cmd::Prefix, cmd::cat(cl_category));
21  cmd::list<str> opt_source_files(cmd::Positional,
22  cmd::Required,
23  cmd::desc("Files to compile"),
24  cmd::value_desc("<source files>"),
25  cmd::OneOrMore,
26  cmd::cat(cl_category));
27  cmd::list<str> opt_import_dirs("I", cmd::desc("Import search directories"), cmd::Prefix, cmd::cat(cl_category));
28  cmd::opt<bool> opt_print_ir_code("print-ir", cmd::desc("Print LLVM IR code"), cmd::cat(cl_category));
29  cmd::opt<bool> opt_print_ast("print-ast", cmd::desc("Print abstract syntax tree"), cmd::cat(cl_category));
30  cmd::opt<TanCompileType> opt_output_type(cmd::desc("Output type"),
31  cmd::values(clEnumValN(DLIB, "shared", "Shared library"),
32  clEnumValN(SLIB, "static", "Static library"),
33  clEnumValN(EXE, "exe", "Executable"),
34  clEnumValN(OBJ, "obj", "Object file")),
35  cmd::init(EXE),
36  cmd::cat(cl_category));
37  cmd::opt<TanOptLevel> opt_optimization_level(cmd::desc("Optimization level"),
38  cmd::values(clEnumValN(O0, "g", "None"),
39  clEnumVal(O0, "None"),
40  clEnumVal(O1, "Less"),
41  clEnumVal(O2, "Default"),
42  clEnumVal(O3, "Aggressive")),
43  cmd::init(O0),
44  cmd::cat(cl_category));
45 
46  // std::cout << "PID: " << getpid() << '\n';
47  // std::cout << "Args: ";
48  // for (int i = 0; i < argc; ++i)
49  // std::cout << argv[i] << ' ';
50  // std::cout << '\n';
51 
52  /// Remove options created by LLVM/Clang
53  /// We don't want tons of flags not created by this file appearing in the output of `tanc --help`
54  cmd::HideUnrelatedOptions(cl_category);
55 
56  // Parse args
57  cmd::ParseCommandLineOptions(
58  argc, argv, fmt::format("tanc version: {}.{}.{} \n", TAN_VERSION[0], TAN_VERSION[1], TAN_VERSION[2]));
59  vector<str> source_files(opt_source_files);
60 
61  // Init
62  if (!init_compiler(argc, argv)) {
63  // Cannot use Error class here since it's not initialized
64  throw std::runtime_error("Unable to init tanc compiler");
65  }
66 
67  // Build compilation config
68  TanCompilation config;
69  config.type = EXE;
70  config.out_file = opt_output_file.getValue();
71  config.lib_dirs = vector<str>(opt_library_path);
72  config.link_files = vector<str>(opt_link_libraries);
73  config.import_dirs = vector<str>(opt_import_dirs);
74  config.type = opt_output_type.getValue();
75  config.opt_level = opt_optimization_level.getValue();
76 
77  config.verbose = 0;
78  if (opt_print_ast) {
79  config.verbose = 2;
80  } else if (opt_print_ir_code) {
81  config.verbose = 1;
82  }
83 
84  // Reset command line parser in case it will be used by clang
85  cmd::ResetCommandLineParser();
86 
87  try {
88  // Create and run CompilerDriver
89  CompilerDriver driver(config);
90  driver.run(source_files);
91 
92  return 0;
93 
94  } catch (const CompileException &e) {
95  std::cerr << e.what() << '\n';
96  }
97 
98  return 1;
99 }
Compile a list of C++ and/or tan source files, and perform linking.
Definition: driver.h:22
Compilation configuration.
Definition: tan.h:42
vector< str > import_dirs
Library search paths.
Definition: tan.h:49
unsigned verbose
Optimization level,.
Definition: tan.h:45
str out_file
Verbose level, 0 non-verbose, 1 print LLVM IR, 2, print LLVM IR and abstract syntax tree.
Definition: tan.h:46
vector< str > link_files
Output filename, invalid if TanCompilation::type is set to OBJ.
Definition: tan.h:47
TanOptLevel opt_level
Type of compilation,.
Definition: tan.h:44
vector< str > lib_dirs
Files to link against.
Definition: tan.h:48