nullable: convert lib, tools and tests
[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 private import compiling
22
23 # The main class of the nitcompiler program
24 class NitCompiler
25 special AbstractCompiler
26 readable attr _opt_output: OptionString = new OptionString("Output file", "-o", "--output")
27 readable attr _opt_boost: OptionBool = new OptionBool("Optimize compilation", "-O", "--boost")
28 readable attr _opt_no_cc: OptionBool = new OptionBool("Do not invoke C compiler", "--no_cc")
29 readable attr _opt_global: OptionBool = new OptionBool("Use global compilation", "--global")
30 readable attr _opt_clibdir: OptionString = new OptionString("NIT C library directory", "--clibdir")
31 readable attr _opt_bindir: OptionString = new OptionString("NIT tools directory", "--bindir")
32 readable attr _opt_compdir: OptionString = new OptionString("Intermediate compilation directory", "--compdir")
33 readable attr _opt_extension_prefix: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix")
34
35 init
36 do
37 super
38 option_context.add_option(opt_output, opt_boost, opt_no_cc, opt_global, opt_clibdir, opt_bindir, opt_compdir, opt_extension_prefix)
39 end
40
41 redef meth process_options
42 do
43 super
44 output_file = opt_output.value
45 boost = opt_boost.value
46 no_cc = opt_no_cc.value
47 var ext = opt_extension_prefix.value
48 if ext != null then ext_prefix = ext else ext_prefix = ""
49 global = opt_global.value
50 compdir = opt_compdir.value
51 if compdir == null then
52 var dir = once ("NIT_COMPDIR".to_symbol).environ
53 if not dir.is_empty then
54 compdir = dir
55 end
56 if compdir == null then
57 compdir = ".nit_compile"
58 end
59 end
60 compdir += ext_prefix
61
62 clibdir = opt_clibdir.value
63 if clibdir == null then
64 var dir = once ("NIT_DIR".to_symbol).environ
65 if dir.is_empty then
66 var dir = "{sys.program_name.dirname}/../lib"
67 if dir.file_exists then clibdir = dir
68 else
69 dir = "{dir}/lib"
70 if dir.file_exists then clibdir = dir
71 end
72 if clibdir == null then
73 error("Error: Cannot locate NIT C library directory. Uses --clibdir or envvar NIT_DIR.")
74 exit(1)
75 end
76 end
77 bindir = opt_bindir.value
78
79 if bindir == null then
80 var dir = once ("NIT_DIR".to_symbol).environ
81 if dir.is_empty then
82 var dir = "{sys.program_name.dirname}/../bin"
83 if dir.file_exists then bindir = dir
84 else
85 dir = "{dir}/bin"
86 if dir.file_exists then bindir = dir
87 end
88 if bindir == null then
89 error("Error: Cannot locate NIT tools directory. Uses --bindir or envvar NIT_DIR.")
90 exit(1)
91 end
92 end
93 end
94
95 redef meth perform_work(mods)
96 do
97 for mod in mods do
98 assert mod isa MMSrcModule
99 mod.compile_prog_to_c(self)
100 end
101
102 end
103 end
104
105 var c = new NitCompiler
106 c.exec_cmd_line