src: improve messages (and sometime location) of errors and warnings
[nit.git] / src / ffi / pkgconfig.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 `PkgconfigPhase` to use the external program "pkg-config" in order
18 # to discover what options to pass to the C or C++ compiler.
19 module pkgconfig
20
21 import c
22 private import annotation
23 private import literal
24
25 redef class ToolContext
26 var pkgconfig_phase: Phase = new PkgconfigPhase(self, [literal_phase])
27 end
28
29 # Detects the `pkgconfig` annotation on the module declaration only.
30 class PkgconfigPhase
31 super Phase
32
33 redef fun process_annotated_node(nmoduledecl, nat)
34 do
35 # Skip if we are not interested
36 if nat.name != "pkgconfig" then return
37
38 # Do some validity checks and print errors if the annotation is used incorrectly
39 var modelbuilder = toolcontext.modelbuilder
40
41 if not nmoduledecl isa AModuledecl then
42 modelbuilder.error(nat, "Syntax Error: only the declaration of modules may use `pkgconfig`.")
43 return
44 end
45
46 # retreive module
47 var nmodule = nmoduledecl.parent.as(AModule)
48 var mmodule = nmodule.mmodule.as(not null)
49
50 # target pkgs
51 var pkgs = new Array[String]
52
53 var args = nat.n_args
54 if args.is_empty then
55 # use module name
56 pkgs.add(mmodule.name)
57 else
58 for arg in args do
59 var pkg = arg.as_string
60 if pkg == null then
61 modelbuilder.error(nat, "Syntax Error: `pkgconfig` expects its arguments to be the name of the package as String literals.")
62 return
63 end
64
65 pkgs.add(pkg)
66 end
67 end
68
69 # check availability of pkg-config
70 var proc_which = new ProcessReader("which", "pkg-config")
71 proc_which.wait
72 var status = proc_which.status
73 if status != 0 then
74 modelbuilder.error(nat, "Error: program `pkg-config` not found, make sure it is installed.")
75 return
76 end
77
78 for pkg in pkgs do
79 var proc_exist = new Process("pkg-config", "--exists", pkg)
80 proc_exist.wait
81 status = proc_exist.status
82 if status == 1 then
83 modelbuilder.error(nat, "Error: package `{pkg}` unknown by `pkg-config`, make sure the development package is be installed.")
84 return
85 else if status != 0 then
86 modelbuilder.error(nat, "Error: something went wrong calling `pkg-config`, make sure it is correctly installed.")
87 return
88 end
89
90 mmodule.pkgconfigs.add pkg
91 end
92
93 end
94 end