1# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5LOCAL_MOJO_ROOT := $(call my-dir)
6
7JNI_GENERATOR_TOOL := \
8	$(LOCAL_MOJO_ROOT)/base/android/jni_generator/jni_generator.py
9
10generated_sources_dir := $(local-generated-sources-dir)
11generated_files :=
12
13# $(1): a single Java file
14define generate-jni-header
15
16java_file := $(1)
17local_path := $(LOCAL_PATH)
18target_path := $(generated_sources_dir)/jni
19gen_h := $$(target_path)/$$(basename $$(notdir $$(java_file)))_jni.h
20
21$$(gen_h) : PRIVATE_PATH := $$(local_path)
22$$(gen_h) : PRIVATE_TARGET := $$(target_path)
23$$(gen_h) : PRIVATE_CUSTOM_TOOL = \
24  (cd $$(PRIVATE_PATH) && \
25   python $$(abspath $$(JNI_GENERATOR_TOOL)) \
26	 --input_file=$$(subst $$(PRIVATE_PATH)/,,$$<) \
27	 --output_dir=$$(abspath $$(PRIVATE_TARGET)) \
28	 --includes base/android/jni_generator/jni_generator_helper.h \
29	 --ptr_type long \
30	 --native_exports_optional)
31$$(gen_h) : $$(local_path)/$$(java_file) $$(JNI_GENERATOR_TOOL)
32	$$(transform-generated-source)
33
34generated_files += $$(gen_h)
35
36endef  # define generate-jni-header
37
38# Build each file separately since the build command needs to be done per-file.
39$(foreach file,$(LOCAL_JAVA_JNI_FILES),$(eval $(call generate-jni-header,$(file))))
40
41# Add the generated sources to the C includes.
42LOCAL_C_INCLUDES += $(generated_sources_dir)
43
44# Create a stamp file after all files have been generated.
45gen := $(generated_sources_dir)/.jni.stamp
46$(gen) : $(generated_files)
47	$(hide) echo $^ | sed -e 's/ /\n/g' > $@
48
49# Add the stamp file as a dependency to {import,export}_includes.
50$(local-intermediates-dir)/import_includes: | $(generated_sources_dir)/.jni.stamp
51