Skip to content

Commit

Permalink
Test Java build rules
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 committed Sep 22, 2021
1 parent bd275b2 commit d350f76
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/android.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ jobs:
path: |
.environment/gn_out/.ninja_log
.environment/pigweed-venv/*.log
- name: Build android examples
- name: Build Android examples
run: |
./scripts/run_in_build_env.sh \
"./scripts/build/build_examples.py --no-log-timestamps --platform android build"
- name: Run Android build rule tests
run: |
./scripts/run_in_build_env.sh \
"ninja -C out/android-arm64-chip_tool build/chip/java/tests:java_build_test.tests"
3 changes: 3 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") {

if (chip_build_tests) {
deps += [ "//src:tests" ]
if (current_os == "android") {
deps += [ "${chip_root}/build/chip/java/tests:java_build_test" ]
}
}

if (chip_with_lwip) {
Expand Down
65 changes: 65 additions & 0 deletions build/chip/java/tests/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) 2021 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_overrides/pigweed.gni")
import("$dir_pw_build/python.gni")
import("${chip_root}/build/chip/java/rules.gni")

pw_python_script("java_build_test") {
inputs = [
"expected_output/child_library_2_expected.json",
"expected_output/grandchild_library_expected.json",
"expected_output/child_library_expected.json",
"expected_output/java_library_expected.json",
"expected_output/child_prebuilt_expected.json",
"expected_output/java_prebuilt_expected.json",
]
other_deps = [ ":java_library" ]
tests = [ "test.py" ]
}

java_library("java_library") {
sources = [
"IncludedInSources.java",
]
deps = [
":child_library",
":child_library_2",
":java_prebuilt",
]
}

java_library("child_library") {
sources = [ "IncludedInChildSources.java" ]
}

java_library("child_library_2") {
sources = [ "IncludedInChildSources2.java" ]

deps = [ ":grandchild_library" ]
}

java_library("grandchild_library") {
sources = [ "IncludedInGrandchildSources.java" ]
}

java_prebuilt("java_prebuilt") {
jar_path = "/tmp/chip_java_build_test/prebuilt_jar.jar"
deps = [ ":child_prebuilt" ]
}

java_prebuilt("child_prebuilt") {
jar_path = "/tmp/chip_java_build_test/child_jar.jar"
}
3 changes: 3 additions & 0 deletions build/chip/java/tests/IncludedInChildJar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package build.chip.java.tests;

public class IncludedInChildJar {}
3 changes: 3 additions & 0 deletions build/chip/java/tests/IncludedInChildSources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package build.chip.java.tests;

public class IncludedInChildSources {}
5 changes: 5 additions & 0 deletions build/chip/java/tests/IncludedInChildSources2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package build.chip.java.tests;

public class IncludedInChildSources2 {
IncludedInGrandchildSources includedInGrandchildSources;
}
3 changes: 3 additions & 0 deletions build/chip/java/tests/IncludedInGrandchildSources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package build.chip.java.tests;

public class IncludedInGrandchildSources {}
5 changes: 5 additions & 0 deletions build/chip/java/tests/IncludedInJar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package build.chip.java.tests;

public class IncludedInJar {
IncludedInChildJar includedInChildJar;
}
9 changes: 9 additions & 0 deletions build/chip/java/tests/IncludedInSources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package build.chip.java.tests;

public class IncludedInSources {
IncludedInChildSources includedInChildSources;
IncludedInChildSources2 includedInChildSources2;
IncludedInGrandchildSources includedInGrandchildSources;

IncludedInJar includedInJar;
}
8 changes: 8 additions & 0 deletions build/chip/java/tests/empty_build_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"deps_info": {
"name": "",
"jar_path": "",
"deps_configs": [],
"deps_jars": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"deps_info": {
"name": "child_library_2.json",
"jar_path": "python/lib/build/chip/java/tests/child_library_2.jar",
"deps_configs": [
"python/gen/build/chip/java/tests/grandchild_library.json"
],
"deps_jars": [
"python/lib/build/chip/java/tests/grandchild_library.jar"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"deps_info": {
"name": "child_library.json",
"jar_path": "python/lib/build/chip/java/tests/child_library.jar",
"deps_configs": [],
"deps_jars": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"deps_info": {
"name": "child_prebuilt.json",
"jar_path": "python/lib/build/chip/java/tests/child_jar.jar",
"deps_configs": [],
"deps_jars": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"deps_info": {
"name": "grandchild_library.json",
"jar_path": "python/lib/build/chip/java/tests/grandchild_library.jar",
"deps_configs": [],
"deps_jars": []
}
}
18 changes: 18 additions & 0 deletions build/chip/java/tests/expected_output/java_library_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"deps_info": {
"name": "java_library.json",
"jar_path": "python/lib/build/chip/java/tests/java_library.jar",
"deps_configs": [
"python/gen/build/chip/java/tests/child_library.json",
"python/gen/build/chip/java/tests/child_library_2.json",
"python/gen/build/chip/java/tests/java_prebuilt.json"
],
"deps_jars": [
"python/lib/build/chip/java/tests/child_library.jar",
"python/lib/build/chip/java/tests/child_library_2.jar",
"python/lib/build/chip/java/tests/grandchild_library.jar",
"python/lib/build/chip/java/tests/prebuilt_jar.jar",
"python/lib/build/chip/java/tests/child_jar.jar"
]
}
}
12 changes: 12 additions & 0 deletions build/chip/java/tests/expected_output/java_prebuilt_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"deps_info": {
"name": "java_prebuilt.json",
"jar_path": "python/lib/build/chip/java/tests/prebuilt_jar.jar",
"deps_configs": [
"python/gen/build/chip/java/tests/child_prebuilt.json"
],
"deps_jars": [
"python/lib/build/chip/java/tests/child_jar.jar"
]
}
}
47 changes: 47 additions & 0 deletions build/chip/java/tests/generate_jars_for_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
import subprocess
import sys

"""Generate JARs used to test the java_prebuilt rule."""

chip_root = os.getenv('PW_PROJECT_ROOT')
test_dir = chip_root + '/build/chip/java/tests'
tmp_dir = '/tmp/chip_java_build_test'

def generateJar(source_file, output_name):
tmp_classes_dir = tmp_dir + '/classes'
os.makedirs(tmp_dir, exist_ok=True)
os.makedirs(tmp_dir + '/classes', exist_ok=True)
javac_runner_command = [
'python3',
chip_root + '/build/chip/java/javac_runner.py',
'--classdir',
tmp_classes_dir,
'--outfile',
tmp_dir + '/prebuilt_jar.classlist',
'--build-config',
test_dir + '/empty_build_config.json',
'--',
'-d',
tmp_classes_dir,
source_file,
]
subprocess.check_call(javac_runner_command)

jar_runner_command = [
'python3',
chip_root + '/build/chip/java/jar_runner.py',
'cf',
tmp_dir + '/' + output_name,
'-C',
tmp_classes_dir,
'.',
]
subprocess.check_call(jar_runner_command)

def main():
generateJar(test_dir + '/IncludedInJar.java', 'prebuilt_jar.jar')
generateJar(test_dir + '/IncludedInChildJar.java', 'child_jar.jar')

if __name__ == '__main__':
sys.exit(main())
74 changes: 74 additions & 0 deletions build/chip/java/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# Copyright (c) 2021 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.

"""
Test for GN Java build rules. This test should be executed using ninja, and
generate_jars_for_test.py should have been called before running this test.
"""

import json
import os
from os import path
import subprocess
import unittest

class JavaBuildTest(unittest.TestCase):
chip_root = os.getenv('PW_PROJECT_ROOT')
local_test_dir = '/build/chip/java/tests'
test_dir = chip_root + local_test_dir

jars_dir = 'python/lib' + local_test_dir
configs_dir = 'python/gen' + local_test_dir

tmp_dir = '/tmp/chip_java_build_test'

# Target names in the BUILD.gn
targets_to_check = [
'java_library',
'child_library',
'child_library_2',
'grandchild_library',
]
prebuilt_targets_to_check = [
'java_prebuilt',
'child_prebuilt'
]

def testExpectedJarsCreated(self):
jars_dir = JavaBuildTest.jars_dir
for target in JavaBuildTest.targets_to_check:
self.assertTrue(path.exists(jars_dir + '/' + target + '.jar'))

# Prebuilt jars should have been copied to the output directory.
self.assertTrue(path.exists(jars_dir + '/prebuilt_jar.jar'))
self.assertTrue(path.exists(jars_dir + '/child_jar.jar'))

def testBuildConfigMatchesExpected(self):
self.maxDiff = None
configs_dir = JavaBuildTest.configs_dir
expected_dir = JavaBuildTest.test_dir + '/expected_output'

for target in (JavaBuildTest.targets_to_check + JavaBuildTest.prebuilt_targets_to_check):
with open(expected_dir + '/' + target + '_expected.json', 'r') as expected_config, open(configs_dir + '/' + target + '.json', 'r') as actual_config:
expected_json = json.load(expected_config)['deps_info']
actual_json = json.load(actual_config)['deps_info']

self.assertEqual(expected_json['name'], actual_json['name'])
self.assertEqual(expected_json['jar_path'], actual_json['jar_path'])
self.assertCountEqual(expected_json['deps_configs'], actual_json['deps_configs'])
self.assertCountEqual(expected_json['deps_jars'], actual_json['deps_jars'])

if __name__ == '__main__':
unittest.main()
25 changes: 17 additions & 8 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def validate_build_environment(self):
% licenses)

def generate(self):
self._Execute([
'python3', 'build/chip/java/tests/generate_jars_for_test.py'
], title='Generating JARs for Java build rules test')

if not os.path.exists(self.output_dir):
# NRF does a in-place update of SDK tools
if not self._runner.dry_run:
Expand Down Expand Up @@ -120,11 +124,6 @@ def _build(self):
self._Execute(['ninja', '-C', self.output_dir],
title='Building JNI ' + self.identifier)

# NOTE: the following IDE-specific build instructions are NOT used:
# - "rsync -a out/"android_$TARGET_CPU"/lib/*.jar src/android/CHIPTool/app/libs"
# => using the 'ninjaOutputDir' project property instead to take the jar files directly
# from the output

# JNILibs will be copied as long as they reside in src/main/jniLibs/ABI:
# https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs
# to avoid redefined in IDE mode, copy to another place and add that path in build.gradle
Expand All @@ -133,6 +132,7 @@ def _build(self):
# when using dry run.
jnilibs_dir = os.path.join(
self.root, 'src/android/CHIPTool/app/libs/jniLibs', self.board.AbiName())
libs_dir = os.path.join(self.root, 'src/android/CHIPTool/app/libs')
self._Execute(['mkdir', '-p', jnilibs_dir],
title='Prepare Native libs ' + self.identifier)

Expand All @@ -149,21 +149,30 @@ def _build(self):
self._Execute(['cp', os.path.join(self.output_dir, 'lib', 'jni', self.board.AbiName(
), libName), os.path.join(jnilibs_dir, libName)])

jars = {
'CHIPController.jar': 'src/controller/java/CHIPController.jar',
'SetupPayloadParser.jar': 'src/setup_payload/java/SetupPayloadParser.jar'
}
for jarName in jars.keys():
self._Execute(['cp', os.path.join(
self.output_dir, 'lib', jars[jarName]), os.path.join(libs_dir, jarName)])

# App compilation
self._Execute([
'%s/src/android/CHIPTool/gradlew' % self.root, '-p',
'%s/src/android/CHIPTool' % self.root,
'-PchipSdkJarDir=%s' % os.path.join(self.output_dir, 'lib'),
'-PbuildDir=%s' % self.output_dir, 'assembleDebug'
],
title='Building APP ' + self.identifier)

def build_outputs(self):
outputs = {
'CHIPController.jar':
os.path.join(self.output_dir, 'lib', 'CHIPController.jar'),
os.path.join(self.output_dir, 'lib',
'src/controller/java/CHIPController.jar'),
'SetupPayloadParser.jar':
os.path.join(self.output_dir, 'lib', 'SetupPayloadParser.jar'),
os.path.join(self.output_dir, 'lib',
'src/setup_payload/java/SetupPayloadParser.jar'),
'ChipTool-debug.apk':
os.path.join(self.output_dir, 'outputs', 'apk', 'debug',
'app-debug.apk'),
Expand Down
Loading

0 comments on commit d350f76

Please sign in to comment.