1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 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 # Intro the annotation `extra_java_files` to compile extra java files
19 # It is mainly used to declare public Java classes.
20 module extra_java_files
24 private import annotation
26 redef class ToolContext
27 var extra_java_files_phase
: Phase = new JavaExtraFilesPhase(self, [literal_phase
])
31 # Extra Java files to compile with the project
32 private var extra_java_files
: nullable Array[JavaFile] = null
35 private class JavaExtraFilesPhase
38 redef fun process_annotated_node
(nmoduledecl
, nat
)
40 # Skip if we are not interested
41 var annot_name
= "extra_java_files"
42 if nat
.name
!= annot_name
then return
44 # Do some validity checks and print errors if the annotation is used incorrectly
45 var modelbuilder
= toolcontext
.modelbuilder
47 if not nmoduledecl
isa AModuledecl then
48 modelbuilder
.error
(nat
, "Syntax Error: only the declaration of modules may use `{annot_name}`.")
54 modelbuilder
.error
(nat
, "Syntax Error: `{annot_name}` expects at least one argument.")
59 var nmodule
= nmoduledecl
.parent
.as(AModule)
60 var mmodule
= nmodule
.mmodule
.as(not null)
61 var java_files
= mmodule
.extra_java_files
62 if java_files
== null then
63 java_files
= new Array[JavaFile]
64 mmodule
.extra_java_files
= java_files
67 var format_error
= "Syntax Error: `{annot_name}` expects its arguments to be paths to java files."
69 var path
= arg
.as_string
71 modelbuilder
.error
(arg
, format_error
)
75 # Append specified path to directory of the Nit source file
76 var source_file
= nat
.location
.file
77 if source_file
!= null then path
= "{source_file.filename.dirname}/{path}"
79 if not path
.file_exists
then
80 modelbuilder
.error
(nat
, "FFI with Java Error: file `{path}` not found.")
84 var file
= new JavaFile(path
)
85 mmodule
.ffi_files
.add file
91 redef class JavaLanguage
92 redef fun compile_to_files
(mmodule
, compdir
)
96 # also copy over the java files
97 var extra_java_files
= mmodule
.extra_java_files
98 if extra_java_files
!= null then for file
in extra_java_files
do
99 var path
= file
.filename
100 path
.file_copy_to
("{compdir}/{path.basename("")}")