nitc: use simplify_path to manipulate sane filenames
[nit.git] / src / nitc.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Nit compiler main module
18 package nitc
19
20 import abstracttool
21 import analysis
22 import program
23 import compiling
24 import syntax
25 import native_interface
26 import separate_options
27
28 # The main class of the nitcompiler program
29 class NitCompiler
30 super AbstractCompiler
31 readable var _opt_output: OptionString = new OptionString("Output file", "-o", "--output")
32 readable var _opt_boost: OptionBool = new OptionBool("Optimize compilation", "-O", "--boost")
33 readable var _opt_no_cc: OptionBool = new OptionBool("Do not invoke C compiler", "--no-cc")
34 readable var _opt_cc_no_link: OptionBool = new OptionBool("Do not invoke C linker", "--cc-no-link")
35 readable var _opt_global: OptionBool = new OptionBool("Use global compilation", "--global")
36 readable var _opt_global_no_STF_opt: OptionBool = new OptionBool("Do not use SFT optimization", "--no-global-SFT-optimization")
37 readable var _opt_global_no_DMR_opt: OptionBool = new OptionBool("Do not use dead method removal optimization", "--no-global-DMR-optimization")
38 readable var _opt_global_no_inline_get_set: OptionBool = new OptionBool("Do not automatically inline getters/setters", "--no-global-get-set-inlining")
39 readable var _opt_global_no_out_of_init_get_test_opt: OptionBool = new OptionBool("Do not remove get tests outside object initialization", "--no-global-OOIT-optimization")
40 readable var _opt_global_no_RFIMA: OptionBool = new OptionBool("Do not use a specialized algorithm to find reachable methods from initializers", "--no-global-RFIM-analysis")
41 readable var _opt_global_callgraph: OptionEnum = new OptionEnum(["none", "cha", "rta"], "The algorithm to use to build the callgraph", 2, "--global-callgraph")
42 readable var _opt_clibdir: OptionString = new OptionString("NIT C library directory", "--clibdir")
43 readable var _opt_bindir: OptionString = new OptionString("NIT tools directory", "--bindir")
44 readable var _opt_compdir: OptionString = new OptionString("Intermediate compilation directory", "--compdir")
45 readable var _opt_extension_prefix: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix")
46 readable var _opt_output_format: OptionEnum = new OptionEnum(["none", "C", "icode"], "The type of code we want to be generated", 1, "--output-format")
47
48 init
49 do
50 super("nitc")
51 option_context.add_option(opt_output, opt_boost, opt_no_cc, opt_cc_no_link, opt_cc_libs, opt_cc_lib_paths, opt_cc_include_paths, opt_global, opt_clibdir, opt_bindir, opt_compdir, opt_extension_prefix, opt_global_no_STF_opt, opt_global_no_DMR_opt, opt_global_callgraph, opt_global_no_inline_get_set, opt_global_no_RFIMA, opt_global_no_out_of_init_get_test_opt, opt_output_format)
52 end
53
54 redef fun process_options
55 do
56 super
57 output_file = opt_output.value
58 boost = opt_boost.value
59 no_cc = opt_no_cc.value
60 cc_link = not opt_cc_no_link.value
61 cc_lib_paths = opt_cc_lib_paths.value
62 cc_libs = opt_cc_libs.value
63 cc_include_paths = opt_cc_include_paths.value
64 var ext = opt_extension_prefix.value
65 if ext != null then ext_prefix = ext else ext_prefix = ""
66 global = opt_global.value
67 use_SFT_optimization = not opt_global_no_STF_opt.value
68 no_dead_method_removal = opt_global_no_DMR_opt.value
69 no_inline_get_set = opt_global_no_inline_get_set.value
70 no_callgraph_from_init = opt_global_no_RFIMA.value
71 no_out_of_init_get_test_removal = opt_global_no_out_of_init_get_test_opt.value
72 global_callgraph = opt_global_callgraph.value_name
73 compdir = opt_compdir.value
74 if compdir == null then
75 var dir = once ("NIT_COMPDIR".to_symbol).environ
76 if not dir.is_empty then
77 compdir = dir
78 end
79 if compdir == null then
80 compdir = ".nit_compile"
81 end
82 end
83 compdir += ext_prefix
84 compdir = compdir.simplify_path
85
86 clibdir = opt_clibdir.value
87 if clibdir == null then
88 var dir = once ("NIT_DIR".to_symbol).environ
89 if dir.is_empty then
90 dir = "{sys.program_name.dirname}/../clib"
91 if dir.file_exists then clibdir = dir
92 else
93 dir = "{dir}/clib"
94 if dir.file_exists then clibdir = dir
95 end
96 if clibdir == null then
97 fatal_error(null, "Error: Cannot locate NIT C library directory. Uses --clibdir or envvar NIT_DIR.")
98 end
99 end
100 clibdir = clibdir.simplify_path
101
102 bindir = opt_bindir.value
103 if bindir == null then
104 var dir = once ("NIT_DIR".to_symbol).environ
105 if dir.is_empty then
106 dir = "{sys.program_name.dirname}/../bin"
107 if dir.file_exists then bindir = dir
108 else
109 dir = "{dir}/bin"
110 if dir.file_exists then bindir = dir
111 end
112 if bindir == null then
113 fatal_error(null, "Error: Cannot locate NIT tools directory. Uses --bindir or envvar NIT_DIR.")
114 end
115 end
116 bindir = bindir.simplify_path
117 end
118
119 redef fun perform_work(mods)
120 do
121 for mod in mods do
122 var p = new Program(mod, self)
123 p.output_format = opt_output_format.value_name
124 p.compute_main_method
125 p.generate_allocation_iroutines
126 if global then
127 p.do_global_pre_analysis_optimizations
128 p.do_global_analysis
129 p.do_global_post_analysis_optimizations
130 if opt_log.value then
131 p.dump_global_optimizations_information(log_directory)
132 p.dump_global_analysis_information(log_directory)
133 end
134 end
135 p.do_table_computation
136 p.compile_prog
137 end
138 end
139 end
140
141 var c = new NitCompiler
142 c.exec_cmd_line