1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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.
26 intrude import ffi_base
28 import header_dependency
30 import c_compiler_options
34 import extra_java_files
38 # Does this module uses the FFI?
39 var uses_ffi
: Bool = false
41 # C compilation unit for the FFI files
42 private var ffi_ccu
: nullable CCompilationUnit = null
44 # Foreign language used in this AModule
45 private var present_languages
= new HashSet[FFILanguage]
47 # Complete the compilation of the FFI code
48 fun finalize_ffi_wrapper
(compdir
: String, mainmodule
: MModule)
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))
55 language
.compile_to_files
(self, compdir
)
58 # include dependancies FFI
59 for mod
in header_dependencies
do
60 if mod
.uses_ffi
then ffi_ccu
.header_custom
.add
("#include \"{mod.c_name}._ffi
.h\
"\n")
63 var cflags
= self.cflags
[""].join
(" ")
65 ffi_ccu
.write_as_impl
(self, compdir
)
66 for filename
in ffi_ccu
.files
do
67 var f
= new ExternCFile(filename
, cflags
)
68 f
.pkgconfigs
.add_all pkgconfigs
73 # Avoid the compile a ffi propdef more than once
74 # See `AMethPropdef::compile_ffi_method`
75 # FIXME find a better way
76 private var compiled_ffi_methods
= new HashSet[AMethPropdef]
81 # Ensures all of the general foreign code of the module has been analyzed.
82 # Manages header blocks, extern class types and foreign dependancies between modules
83 fun ensure_compile_ffi_wrapper
86 if mmodule
== null or mmodule
.ffi_ccu
!= null then return
88 # ready extern code compiler
89 var ffi_ccu
= new CCompilationUnit
90 mmodule
.ffi_ccu
= ffi_ccu
93 for block
in n_extern_code_blocks
do
94 var language
= block
.language
95 assert language
!= null
96 mmodule
.present_languages
.add
(language
)
97 language
.compile_module_block
(block
, ffi_ccu
, mmodule
)
100 ffi_ccu
.header_c_base
.add
( "#include \"{mmodule.c_name}._nitni
.h\
"\n" )
102 ffi_ccu
.body_decl
.add
("#ifdef ANDROID\n")
103 ffi_ccu
.body_decl
.add
(" #include <android/log.h>\n")
104 ffi_ccu
.body_decl
.add
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)\n")
105 ffi_ccu
.body_decl
.add
("#else\n")
106 ffi_ccu
.body_decl
.add
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)\n")
107 ffi_ccu
.body_decl
.add
("#endif\n")
109 for nclassdef
in n_classdefs
do
110 # Does it declares an extern type?
111 if nclassdef
isa AStdClassdef and nclassdef
.n_extern_code_block
!= null then
112 mmodule
.uses_ffi
= true
113 var language
= nclassdef
.n_extern_code_block
.language
114 assert language
!= null
115 mmodule
.present_languages
.add
(language
)
116 nclassdef
.n_extern_code_block
.language
.compile_extern_class
(
117 nclassdef
.n_extern_code_block
.as(not null), nclassdef
, ffi_ccu
, mmodule
)
123 redef class AMethPropdef
124 # Compile the necessary wrapper around this extern method or constructor
125 fun compile_ffi_method
(mmodule
: MModule)
127 assert n_extern_code_block
!= null
129 if mmodule
.compiled_ffi_methods
.has
(self) then return
130 mmodule
.compiled_ffi_methods
.add
self
132 var language
= n_extern_code_block
.language
133 assert language
!= null
134 mmodule
.present_languages
.add
(language
)
135 n_extern_code_block
.language
.compile_extern_method
(
136 n_extern_code_block
.as(not null), self, mmodule
.ffi_ccu
.as(not null), mmodule
)
140 redef class VerifyNitniCallbacksPhase
141 redef fun process_npropdef
(npropdef
)
145 if not npropdef
isa AMethPropdef then return
147 var code_block
= npropdef
.n_extern_code_block
148 if code_block
== null then return
150 var lang
= code_block
.language
153 # Associate callbacks used by an extern method to its foreign language
154 for callback
in npropdef
.foreign_callbacks
.all
do
155 var map
= npropdef
.mpropdef
.mclassdef
.mmodule
.ffi_callbacks
156 if not map
.keys
.has
(lang
) then map
[lang
] = new HashSet[NitniCallback]
157 map
[lang
].add
(callback
)