nitc: refactor MModule cflags and ldflags to support different target platforms
[nit.git] / src / ffi / c_compiler_options.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
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 # Offers the annotations `cflags` and `ldflags` to specify
18 # options for the C compiler directly or indirectly. Differs from the `pkgconfig`
19 # annotation by the separation of the options between the compiler and linker.
20 module c_compiler_options
21
22 import c
23 import cpp
24 private import annotation
25 private import platform
26
27 redef class ToolContext
28 # Phase to find `cflags`, `ldflags` and `cppflags`
29 var cflags_phase: Phase = new CCompilerOptionsPhase(self, [platform_phase])
30 end
31
32 private class CCompilerOptionsPhase
33 super Phase
34
35 fun compiler_annotation_name: String do return "cflags"
36 fun linker_annotation_name: String do return "ldflags"
37 fun cpp_compiler_annotation_name: String do return "cppflags"
38
39 redef fun process_annotated_node(nmoduledecl, nat)
40 do
41 # Skip if we are not interested
42 var annotation_name = nat.name
43 if annotation_name != compiler_annotation_name and
44 annotation_name != linker_annotation_name and
45 annotation_name != cpp_compiler_annotation_name then return
46
47 # Do some validity checks and print errors if the annotation is used incorrectly
48 var modelbuilder = toolcontext.modelbuilder
49
50 if not nmoduledecl isa AModuledecl then
51 modelbuilder.error(nat, "Syntax error: only the declaration of modules may use \"{annotation_name}\".")
52 return
53 end
54
55 var args = nat.n_args
56 if args.is_empty then
57 modelbuilder.error(nat, "Syntax error: \"{annotation_name}\" expects at least one argument.")
58 return
59 end
60
61 var options = new Array[CCompilerOption]
62 for expr in args do
63 if expr isa AStringFormExpr then
64 var text = expr.collect_text
65 text = text.substring(1, text.length-2)
66 var opt = new DirectCCompilerOption(text)
67 options.add(opt)
68 else if expr isa ACallExpr then
69 # We support calls to "exec" only
70 var exec_args = expr.n_args.to_a
71 if expr.n_id.text != "exec" or exec_args.is_empty then
72 modelbuilder.error(nat, "Syntax error: \"{annotation_name}\" accepts only calls to `exec` with the command as arguments.")
73 return
74 end
75
76 var exec_args_as_strings = new Array[String]
77 for exec_arg in exec_args do
78 if not exec_arg isa AStringFormExpr then
79 modelbuilder.error(nat, "Syntax error: calls to `exec` expects the arguments to be String literals.")
80 return
81 else
82 var arg_string = exec_arg.collect_text
83 arg_string = arg_string.substring(1, arg_string.length-2)
84 exec_args_as_strings.add(arg_string)
85 end
86 end
87
88 var opt = new ExecCCompilerOption(exec_args_as_strings, expr)
89 options.add(opt)
90 else
91 modelbuilder.error(nat, "Syntax error: \"{annotation_name}\" expects its arguments to be the name of the package as String literals.")
92 return
93 end
94 end
95
96 # process calls to external command
97 var simplified_options = new Array[DirectCCompilerOption]
98 for opt in options do
99 if opt isa ExecCCompilerOption then
100 # prepare to execute command
101 var cmd_args = opt.command
102 var proc
103 if cmd_args.length == 1 then
104 proc = new IProcess.from_a(cmd_args[0], new Array[String])
105 else if cmd_args.length > 1 then
106 var rest_args = cmd_args.subarray(1, cmd_args.length-1)
107 proc = new IProcess.from_a(cmd_args[0], rest_args)
108 else abort
109
110 # wait for its completion
111 proc.wait
112
113 # check result
114 var status = proc.status
115 if status != 0 then
116 modelbuilder.error(opt.exec_node, "Annotation error: Something went wrong executing the argument of annotation \"{annotation_name}\", make sure the command is valid.")
117 return
118 end
119
120 # process result
121 var result = proc.read_all.replace("\n", " ")
122 if result.is_empty then
123 modelbuilder.error(opt.exec_node, "Annotation error: Got no result from the command, make sure it is valid.")
124 return
125 end
126 simplified_options.add(new DirectCCompilerOption(result))
127 else
128 assert opt isa DirectCCompilerOption
129 simplified_options.add(opt)
130 end
131 end
132
133 # Retrieve module
134 var mmodule = nmoduledecl.parent.as(AModule).mmodule.as(not null)
135
136 # Get target platform from annotation on annotation
137 var platform = ""
138
139 ## Is there an imported platform?
140 var target_platform = mmodule.target_platform
141 if target_platform != null then
142 platform = target_platform.name or else ""
143 end
144
145 ## Is the platform declared explicitly?
146 var annots = nat.n_annotations
147 if annots != null then
148 var items = annots.n_items
149 if items.length > 1 then
150 modelbuilder.error(annots, "Annotation error: `annotation_name` accepts only a single annotation, the platform name")
151 return
152 end
153 assert items.length == 1
154
155 var item = items.first
156 platform = item.name
157 end
158
159 # Store the flags in the module
160 for opt in simplified_options do
161 var arg = opt.option
162 if annotation_name == compiler_annotation_name then
163 mmodule.cflags.add_one(platform, arg)
164 else if annotation_name == linker_annotation_name then
165 mmodule.ldflags.add_one(platform, arg)
166 else if annotation_name == cpp_compiler_annotation_name then
167 mmodule.cppflags.add_one(platform, arg)
168 else abort
169 end
170 end
171
172 end
173
174 abstract class CCompilerOption
175 end
176
177 class DirectCCompilerOption
178 super CCompilerOption
179
180 var option: String
181 end
182
183 class ExecCCompilerOption
184 super CCompilerOption
185
186 var command: Array[String]
187 var exec_node: ACallExpr
188 end