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