ffi/pkgconfig: use the new pkgconfig infrastructure in the Makefile
[nit.git] / src / ffi / ffi.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 # FFI concers common between the compilers and the interpreter.
18 # Offers services to compile modules using foreign code. Mainly allows
19 # to wrap foreign code in Nit methods.
20 module ffi
21
22 import modelbuilder
23
24 import nitni
25
26 intrude import ffi_base
27 import extern_classes
28 import header_dependency
29 import pkgconfig
30 import c_compiler_options
31 import c
32 import cpp
33 import java
34 import extra_java_files
35
36 redef class MModule
37 # Does this module uses the FFI?
38 var uses_ffi: Bool = false
39
40 # C compilation unit for the FFI files
41 private var ffi_ccu: nullable CCompilationUnit = null
42
43 # Foreign language used in this AModule
44 private var present_languages = new HashSet[FFILanguage]
45
46 # Complete the compilation of the FFI code
47 fun finalize_ffi_wrapper(compdir: String, mainmodule: MModule)
48 do
49 for language in ffi_callbacks.keys do
50 for callback in ffi_callbacks[language] do
51 language.compile_callback(callback, self, mainmodule, ffi_ccu.as(not null))
52 end
53
54 language.compile_to_files(self, compdir)
55 end
56
57 # include dependancies FFI
58 for mod in header_dependencies do
59 if mod.uses_ffi then ffi_ccu.header_custom.add("#include \"{mod.name}._ffi.h\"\n")
60 end
61
62 ffi_ccu.write_as_impl(self, compdir)
63 for filename in ffi_ccu.files do
64 var f = new ExternCFile(filename, c_compiler_options)
65 f.pkgconfigs.add_all pkgconfigs
66 ffi_files.add(f)
67 end
68 end
69
70 # Avoid the compile a ffi propdef more than once
71 # See `AMethPropdef::compile_ffi_method`
72 # FIXME find a better way
73 private var compiled_ffi_methods = new HashSet[AMethPropdef]
74 end
75
76 redef class AModule
77
78 # Ensures all of the general foreign code of the module has been analyzed.
79 # Manages header blocks, extern class types and foreign dependancies between modules
80 fun ensure_compile_ffi_wrapper
81 do
82 var mmodule = mmodule
83 if mmodule == null or mmodule.ffi_ccu != null then return
84
85 # ready extern code compiler
86 var ffi_ccu = new CCompilationUnit
87 mmodule.ffi_ccu = ffi_ccu
88
89 # generate code
90 for block in n_extern_code_blocks do
91 var language = block.language
92 assert language != null
93 mmodule.present_languages.add(language)
94 language.compile_module_block(block, ffi_ccu, mmodule)
95 end
96
97 ffi_ccu.header_c_base.add( "#include \"{mmodule.name}._nitni.h\"\n" )
98
99 ffi_ccu.body_decl.add("#ifdef ANDROID\n")
100 ffi_ccu.body_decl.add(" #include <android/log.h>\n")
101 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\", __VA_ARGS__)\n")
102 ffi_ccu.body_decl.add("#else\n")
103 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)\n")
104 ffi_ccu.body_decl.add("#endif\n")
105
106 for nclassdef in n_classdefs do
107 # Does it declares an extern type?
108 if nclassdef isa AStdClassdef and nclassdef.n_extern_code_block != null then
109 mmodule.uses_ffi = true
110 var language = nclassdef.n_extern_code_block.language
111 assert language != null
112 mmodule.present_languages.add(language)
113 nclassdef.n_extern_code_block.language.compile_extern_class(
114 nclassdef.n_extern_code_block.as(not null), nclassdef, ffi_ccu, mmodule)
115 end
116 end
117 end
118 end
119
120 redef class AMethPropdef
121 # Compile the necessary wrapper around this extern method or constructor
122 fun compile_ffi_method(mmodule: MModule)
123 do
124 assert n_extern_code_block != null
125
126 if mmodule.compiled_ffi_methods.has(self) then return
127 mmodule.compiled_ffi_methods.add self
128
129 var language = n_extern_code_block.language
130 assert language != null
131 mmodule.present_languages.add(language)
132 n_extern_code_block.language.compile_extern_method(
133 n_extern_code_block.as(not null), self, mmodule.ffi_ccu.as(not null), mmodule)
134 end
135 end
136
137 redef class VerifyNitniCallbacksPhase
138 redef fun process_npropdef(npropdef)
139 do
140 super
141
142 if not npropdef isa AMethPropdef then return
143
144 var code_block = npropdef.n_extern_code_block
145 if code_block == null then return
146
147 var lang = code_block.language
148 assert lang != null
149
150 # Associate callbacks used by an extern method to its foreign language
151 for callback in npropdef.foreign_callbacks.all do
152 var map = npropdef.mpropdef.mclassdef.mmodule.ffi_callbacks
153 if not map.keys.has(lang) then map[lang] = new HashSet[NitniCallback]
154 map[lang].add(callback)
155 end
156 end
157 end