3addbb5d8f7459726b19b114f770a63d98efada4
[nit.git] / src / common_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
23 redef class ToolContext
24 var pkgconfig_phase: Phase = new PkgconfigPhase(self, null)
25 end
26
27 # Detects the `pkgconfig` annotation on the module declaration only.
28 class PkgconfigPhase
29 super Phase
30
31 redef fun process_annotated_node(nmoduledecl, nat)
32 do
33 # Skip if we are not interested
34 if nat.n_atid.n_id.text != "pkgconfig" then return
35
36 # Do some validity checks and print errors if the annotation is used incorrectly
37 var modelbuilder = toolcontext.modelbuilder
38
39 if not nmoduledecl isa AModuledecl then
40 modelbuilder.error(nat, "Syntax error: only the declaration of modules may use \"pkgconfig\".")
41 return
42 end
43
44 var args = nat.n_args
45 if args.is_empty then
46 modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects at least one argument.")
47 return
48 end
49
50 var pkgs = new Array[String]
51 for arg in args do
52 if not arg isa AExprAtArg then
53 modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
54 return
55 end
56
57 var expr = arg.n_expr
58 if not expr isa AStringFormExpr then
59 modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
60 return
61 end
62
63 var pkg = expr.collect_text
64 pkg = pkg.substring(1, pkg.length-2)
65 pkgs.add(pkg)
66 end
67
68 # retreive module
69 var nmodule = nmoduledecl.parent.as(AModule)
70
71 # check availability of pkg-config
72 var proc_which = new IProcess("which", "pkg-config")
73 proc_which.wait
74 var status = proc_which.status
75 if status != 0 then
76 modelbuilder.error(nat, "Error: program pkg-config not found, make sure it is installed.")
77 return
78 end
79
80 for pkg in pkgs do
81 var proc_exist = new Process("pkg-config", "--exists", pkg)
82 proc_exist.wait
83 status = proc_exist.status
84 if status == 1 then
85 modelbuilder.error(nat, "Error: package \"{pkg}\" unknown by pkg-config, make sure the development package is be installed.")
86 return
87 else if status != 0 then
88 modelbuilder.error(nat, "Error: something went wrong calling pkg-config, make sure it is correctly installed.")
89 return
90 end
91
92 # compiler
93 var proc = new IProcess("pkg-config", "--cflags", pkg)
94 var compiler_opts = proc.read_all
95 nmodule.c_compiler_options = "{nmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
96
97 # linker
98 proc = new IProcess("pkg-config", "--libs", pkg)
99 var linker_opts = proc.read_all
100 nmodule.c_linker_options = "{nmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
101 end
102
103 end
104 end