Skip to content

Commit

Permalink
Add support to compile java matter controller test example in host wi…
Browse files Browse the repository at this point in the history
…thout android (#23630)

* Add suppoprt to compile java matter controller test in host

* Restyled by gn

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jul 31, 2023
1 parent ec1981d commit c1da792
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 31 deletions.
26 changes: 26 additions & 0 deletions build/chip/java/config.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2022 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.

java_path = getenv("JAVA_PATH")
declare_args() {
java_matter_controller_dependent_paths = []
build_java_matter_controller = false
if (java_path != "") {
java_matter_controller_dependent_paths += [
"${java_path}/include/",
"${java_path}/include/linux/",
]
build_java_matter_controller = true
}
}
5 changes: 3 additions & 2 deletions build/chip/java/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# limitations under the License.

import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/java/config.gni")
import("${chip_root}/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"
write_build_config = "${chip_root}/build/chip/java/write_build_config.py"

assert(android_sdk_root != "", "android_sdk_root must be specified")
assert(android_sdk_root != "" || build_java_matter_controller,
"android_sdk_root or java_path must be specified")

# Declare a java library target
#
Expand Down
4 changes: 4 additions & 0 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ config("strict_warnings") {
]
}

if (getenv("JAVA_PATH") != "") {
cflags -= [ "-Wshadow" ]
}

cflags_cc = [ "-Wnon-virtual-dtor" ]

ldflags = []
Expand Down
1 change: 0 additions & 1 deletion examples/build_overrides/build.gni
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
declare_args() {
# Root directory for build files.
build_root = "//third_party/connectedhomeip/build"
build_java_matter_controller = false
}
3 changes: 0 additions & 3 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("${build_root}/config/android_abi.gni")
import("${chip_root}/build/chip/java/rules.gni")
import("${chip_root}/build/chip/tools.gni")

build_java_matter_controller = true

java_binary("java-matter-controller") {
output_name = "java-matter-controller"
deps = [
Expand Down
1 change: 1 addition & 0 deletions scripts/build/build/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def BuildHostTarget():
TargetPart('all-clusters-minimal', app=HostApp.ALL_CLUSTERS),
TargetPart('chip-tool', app=HostApp.CHIP_TOOL),
TargetPart('thermostat', app=HostApp.THERMOSTAT),
TargetPart('java-matter-controller', app=HostApp.JAVA_MATTER_CONTROLLER),
TargetPart('minmdns', app=HostApp.MIN_MDNS),
TargetPart('light', app=HostApp.LIGHT),
TargetPart('lock', app=HostApp.LOCK),
Expand Down
83 changes: 83 additions & 0 deletions scripts/build/builders/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class HostApp(Enum):
TV_CASTING = auto()
BRIDGE = auto()
DYNAMIC_BRIDGE = auto()
JAVA_MATTER_CONTROLLER = auto()

def ExamplePath(self):
if self == HostApp.ALL_CLUSTERS:
Expand Down Expand Up @@ -98,6 +99,8 @@ def ExamplePath(self):
return 'bridge-app/linux'
elif self == HostApp.DYNAMIC_BRIDGE:
return 'dynamic-bridge-app/linux'
elif self == HostApp.JAVA_MATTER_CONTROLLER:
return 'java-matter-controller'
else:
raise Exception('Unknown app type: %r' % self)

Expand Down Expand Up @@ -168,6 +171,9 @@ def OutputNames(self):
elif self == HostApp.DYNAMIC_BRIDGE:
yield 'dynamic-chip-bridge-app'
yield 'dynamic-chip-bridge-app.map'
elif self == HostApp.JAVA_MATTER_CONTROLLER:
yield 'java-matter-controller'
yield 'java-matter-controller.map'
else:
raise Exception('Unknown app type: %r' % self)

Expand Down Expand Up @@ -357,6 +363,41 @@ def GnBuildArgs(self):
else:
raise Exception('Unknown host board type: %r' % self)

def copyToExampleApp(self, jnilibs_dir, libs_dir, libs, jars):
self._Execute(
["mkdir", "-p", jnilibs_dir], title="Prepare Native libs " + self.identifier
)

for libName in libs:
self._Execute(
[
"cp",
os.path.join(
self.output_dir, "lib", "jni", self.board.AbiName(), libName
),
os.path.join(jnilibs_dir, libName),
]
)

for jarName in jars.keys():
self._Execute(
[
"cp",
os.path.join(self.output_dir, "lib", jars[jarName]),
os.path.join(libs_dir, jarName),
]
)

def createJavaExecutable(self, java_program):
self._Execute(
[
"chmod",
"+x",
"%s/bin/%s" % (self.output_dir, java_program),
],
title="Make Java program executable",
)

def GnBuildEnv(self):
if self.board == HostBoard.ARM64:
self.build_env['PKG_CONFIG_PATH'] = os.path.join(
Expand All @@ -370,6 +411,22 @@ def SysRootPath(self, name):

def generate(self):
super(HostBuilder, self).generate()
if 'JAVA_PATH' in os.environ:
self._Execute(
["third_party/java_deps/set_up_java_deps.sh"],
title="Setting up Java deps",
)

exampleName = self.app.ExamplePath()
if exampleName == "java-matter-controller":
self._Execute(
[
"cp",
os.path.join(self.root, "Manifest.txt"),
self.output_dir,
],
title="Copying Manifest.txt to " + self.output_dir,
)

if self.app == HostApp.TESTS and self.use_coverage:
self.coverage_dir = os.path.join(self.output_dir, 'coverage')
Expand All @@ -385,6 +442,32 @@ def PreBuildCommand(self):
'--exclude', os.path.join(self.chip_dir, 'third_party/*'),
'--exclude', '/usr/include/*',
'--output-file', os.path.join(self.coverage_dir, 'lcov_base.info')], title="Initial coverage baseline")
if self.app.exampleName == "java-matter-controller" and 'JAVA_PATH' in os.environ:
jnilibs_dir = os.path.join(
self.root,
"examples/",
self.app.ExampleName(),
"app/libs/jniLibs",
self.board.AbiName(),
)

libs_dir = os.path.join(
self.root, "examples/", self.app.ExampleName(), "app/libs"
)

libs = [
"libSetupPayloadParser.so",
"libCHIPController.so",
"libc++_shared.so",
]

jars = {
"CHIPController.jar": "third_party/connectedhomeip/src/controller/java/CHIPController.jar",
"SetupPayloadParser.jar": "third_party/connectedhomeip/src/setup_payload/java/SetupPayloadParser.jar",
}

self.copyToExampleApp(jnilibs_dir, libs_dir, libs, jars)
self.createJavaExecutable("java-matter-controller")

def PostBuildCommand(self):
if self.app == HostApp.TESTS and self.use_coverage:
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/testdata/all_targets_linux_x64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ efr32-{brd4161a,brd4187c,brd4163a,brd4164a,brd4166a,brd4170a,brd4186a,brd4187a,b
esp32-{m5stack,c3devkit,devkitc,qemu}-{all-clusters,all-clusters-minimal,ota-requestor,ota-requestor,shell,light,lock,bridge,temperature-measurement,ota-requestor,tests}[-rpc][-ipv6only]
genio-lighting-app
linux-fake-tests[-mbedtls][-boringssl][-asan][-tsan][-libfuzzer][-coverage][-dmalloc][-clang]
linux-{x64,arm64}-{rpc-console,all-clusters,all-clusters-minimal,chip-tool,thermostat,minmdns,light,lock,shell,ota-provider,ota-requestor,python-bindings,tv-app,tv-casting-app,bridge,dynamic-bridge,tests,chip-cert,address-resolve-tool}[-nodeps][-minmdns-verbose][-libnl][-same-event-loop][-no-interactive][-ipv6only][-no-ble][-no-wifi][-no-thread][-mbedtls][-boringssl][-asan][-tsan][-libfuzzer][-coverage][-dmalloc][-clang][-test][-rpc]
linux-{x64,arm64}-{rpc-console,all-clusters,all-clusters-minimal,chip-tool,thermostat,java-matter-controller,minmdns,light,lock,shell,ota-provider,ota-requestor,python-bindings,tv-app,tv-casting-app,bridge,dynamic-bridge,tests,chip-cert,address-resolve-tool}[-nodeps][-minmdns-verbose][-libnl][-same-event-loop][-no-interactive][-ipv6only][-no-ble][-no-wifi][-no-thread][-mbedtls][-boringssl][-asan][-tsan][-libfuzzer][-coverage][-dmalloc][-clang][-test][-rpc]
linux-x64-efr32-test-runner[-clang]
imx-{chip-tool,lighting-app,thermostat,all-clusters-app,all-clusters-minimal-app,ota-provider-app}[-release]
infineon-psoc6-{lock,light,all-clusters,all-clusters-minimal}[-ota][-updateimage]
Expand Down
13 changes: 10 additions & 3 deletions src/controller/data_model/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import("//build_overrides/pigweed.gni")

import("$dir_pw_build/python.gni")
import("${chip_root}/build/chip/chip_codegen.gni")
import("${chip_root}/build/chip/java/config.gni")
import("${chip_root}/src/app/chip_data_model.gni")

chip_data_model("data_model") {
Expand All @@ -29,7 +30,7 @@ chip_data_model("data_model") {
allow_circular_includes_from = [ "${chip_root}/src/controller" ]
}

if (current_os == "android") {
if (current_os == "android" || build_java_matter_controller) {
chip_codegen("java-jni-generate") {
input = "controller-clusters.matter"
generator = "java"
Expand Down Expand Up @@ -170,15 +171,21 @@ if (current_os == "android") {

source_set("java-jni-sources") {
sources = get_target_outputs(":java-jni-generate")
public_configs = [ "${chip_root}/src:includes" ]

public_configs = [ "${chip_root}/src:includes" ]
deps = [
":data_model",
":java-jni-generate",
"${chip_root}/src/inet",
"${chip_root}/src/lib",
"${chip_root}/src/platform",
"${chip_root}/src/platform/android",
]

if (build_java_matter_controller) {
include_dirs = java_matter_controller_dependent_paths
deps += [ "${chip_root}/src/platform/Linux" ]
} else {
deps += [ "${chip_root}/src/platform/android" ]
}
}
}
29 changes: 18 additions & 11 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("${build_root}/config/android_abi.gni")
import("${chip_root}/build/chip/java/config.gni")
import("${chip_root}/build/chip/java/rules.gni")

if (defined(build_java_matter_controller)) {
build_java_matter_controller = build_java_matter_controller
} else {
build_java_matter_controller = false
if (!build_java_matter_controller) {
import("${build_root}/config/android_abi.gni")
}

shared_library("jni") {
Expand Down Expand Up @@ -60,12 +58,24 @@ shared_library("jni") {
"${chip_root}/src/lib",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/platform",
"${chip_root}/src/platform/android",
]

public_configs = [ "${chip_root}/src:includes" ]

output_dir = "${root_out_dir}/lib/jni/${android_abi}"
if (build_java_matter_controller) {
defines = [ "JAVA_MATTER_CONTROLLER_TEST" ]
include_dirs = java_matter_controller_dependent_paths

deps += [ "${chip_root}/src/platform/Linux" ]

cflags = [ "-Wno-unknown-pragmas" ]

output_dir = "${root_out_dir}/lib/jni"
} else {
deps += [ "${chip_root}/src/platform/android" ]

output_dir = "${root_out_dir}/lib/jni/${android_abi}"
}

ldflags = [ "-Wl,--gc-sections" ]
}
Expand All @@ -75,10 +85,7 @@ android_library("java") {

deps = [ "${chip_root}/third_party/java_deps:annotation" ]

data_deps = [
":jni",
"${chip_root}/build/chip/java:shared_cpplib",
]
data_deps = [ ":jni" ]

sources = [
"src/chip/clusterinfo/ClusterCommandCallback.java",
Expand Down
9 changes: 7 additions & 2 deletions src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import("//build_overrides/nlio.gni")
import("//build_overrides/nlunit_test.gni")

import("${chip_root}/build/chip/chip_version.gni")
import("${chip_root}/build/chip/java/config.gni")
import("${chip_root}/build/chip/tests.gni")
import("${chip_root}/src/lib/core/core.gni")

Expand Down Expand Up @@ -145,8 +146,12 @@ static_library("support") {
"verhoeff/Verhoeff36.cpp",
]

# added JNI helper on android
if (current_os == "android") {
if (current_os == "android" || build_java_matter_controller) {
if (build_java_matter_controller) {
include_dirs = java_matter_controller_dependent_paths
}

# added JNI helper on android
sources += [
"CHIPJNIError.h",
"JniReferences.cpp",
Expand Down
17 changes: 9 additions & 8 deletions src/setup_payload/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("${build_root}/config/android_abi.gni")
import("${chip_root}/build/chip/java/config.gni")
import("${chip_root}/build/chip/java/rules.gni")

if (defined(build_java_matter_controller)) {
build_java_matter_controller = build_java_matter_controller
} else {
build_java_matter_controller = false
if (!build_java_matter_controller) {
import("${build_root}/config/android_abi.gni")
}

shared_library("jni") {
output_name = "libSetupPayloadParser"
if (build_java_matter_controller) {
include_dirs = java_matter_controller_dependent_paths
output_dir = "${root_out_dir}/lib/jni"
} else {
output_dir = "${root_out_dir}/lib/jni/${android_abi}"
}

sources = [ "SetupPayloadParser-JNI.cpp" ]

deps = [
"${chip_root}/src/lib",
"${chip_root}/src/setup_payload",
]

output_dir = "${root_out_dir}/lib/jni/${android_abi}"
}

android_library("java") {
Expand Down

0 comments on commit c1da792

Please sign in to comment.