blob: 2597ec7da15f4f62449063aebc08f4f01df16ab2 [file] [log] [blame]
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build_overrides/chip.gni")
import("//build/config/android/config.gni")
javac_runner = "${chip_root}/build/chip/java/javac_runner.py"
jar_runner = "${chip_root}/build/chip/java/jar_runner.py"
assert(android_sdk_root != "", "android_sdk_root must be specified")
# Declare a java library target
#
# sources: List of .java files included in this library.
#
# output_name: File name for the output .jar (not including extension).
# Defaults to the input .jar file name.
#
# javac_flags: additional flags to pass to the javac compiler
#
template("java_library") {
# Figure out the output name
_jar_name = target_name
if (defined(invoker.output_name)) {
_jar_name = invoker.output_name
} else {
_jar_name += ".jar"
}
# Additional flags
_javac_flags = [
"-Werror",
"-Xlint:all",
]
if (defined(invoker.javac_flags)) {
_javac_flags += invoker.javac_flags
}
_deps = []
if (defined(invoker.deps)) {
_deps = invoker.deps
}
_data_deps = []
if (defined(invoker.data_deps)) {
_data_deps = invoker.data_deps
}
# What files will be compiled
_java_files = invoker.sources
# Generates a .java file containing all sources to be compiled
_java_sources_file = "$target_gen_dir/$target_name.sources"
if (defined(invoker.java_sources_file)) {
_java_sources_file = invoker.java_sources_file
}
write_file(_java_sources_file, rebase_path(_java_files, root_build_dir))
# Compiles the given files into a directory and generates a 'class list'
_javac_target_name = target_name + "__javac"
_class_dir = rebase_path(target_out_dir, root_build_dir) + "/classes"
_class_list_file = "$target_gen_dir/$target_name.classlist"
action(_javac_target_name) {
sources = _java_files
outputs = [ _class_list_file ]
script = javac_runner
args = [
"--classdir",
_class_dir,
"--outfile",
rebase_path(_class_list_file, root_build_dir),
"--",
"-d",
_class_dir,
"@" + rebase_path(_java_sources_file, root_build_dir),
] + _javac_flags
}
# Bundles all files within the 'class directory' into a jar file
_jar_output = "$root_out_dir/lib/$_jar_name"
action(target_name) {
deps = [ ":$_javac_target_name" ] + _deps
data_deps = _data_deps
outputs = [ _jar_output ]
script = jar_runner
args = [
"cf",
rebase_path(_jar_output, root_build_dir),
"-C",
_class_dir,
".",
]
}
}
template("android_library") {
java_library(target_name) {
forward_variables_from(invoker, "*")
if (!defined(javac_flags)) {
javac_flags = []
}
javac_flags += [
"-classpath",
"${android_sdk_root}/platforms/android-${android_sdk_version}/android.jar",
"-Xlint:-options",
"-source",
"8",
"-target",
"8",
]
}
}