1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012-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 # This module implements the FFI with different languages
22 redef class MMSrcModule
23 redef fun compile_separate_module
(cprogram
: CProgram)
27 if is_extern_hybrid
then
28 var visitor
= new FFIVisitor( cprogram
.program
.tc
, self )
29 # TODO use cprogram to add generated files?
31 # actually compile stub
32 accept_ffi_visitor
( visitor
)
42 redef class MMLocalClass
46 redef class FFIVisitor
49 var compdir
= tc
.compdir
.as(not null)
50 var base_name
= "{mmmodule.cname}._ffi"
51 var c_file
= "{base_name}.c"
52 var h_file
= "{base_name}.h"
55 var module_info
= "/*\n\tExtern implementation of Nit module {mmmodule.name}\n*/\n"
58 var guard
= "{mmmodule.cname.to_s.to_upper}_NIT_H"
61 var stream
= new OFStream.open
( "{compdir}/{h_file}" )
62 stream
.write
( module_info
)
63 stream
.write
( "#include <{mmmodule.name}._nitni.h>\n\n" )
64 stream
.write
( "#ifndef {guard}\n" )
65 stream
.write
( "#define {guard}\n\n" )
66 compilation_unit
.header_c_base
.write_to_stream
( stream
)
67 compilation_unit
.header_custom
.write_to_stream
( stream
)
68 compilation_unit
.header_c_types
.write_to_stream
( stream
)
69 compilation_unit
.header_decl
.write_to_stream
( stream
)
70 stream
.write
( "#endif\n" )
74 stream
= new OFStream.open
( "{compdir}/{c_file}" )
75 stream
.write
( module_info
)
76 stream
.write
( "#include \"{h_file}\
"\n" )
77 compilation_unit
.body_decl
.write_to_stream
( stream
)
78 compilation_unit
.body_custom
.write_to_stream
( stream
)
79 compilation_unit
.body_impl
.write_to_stream
( stream
)