summaryrefslogtreecommitdiff
path: root/srcgen
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2018-07-12 15:58:48 +0100
committerPaul Duffin <paulduffin@google.com>2018-09-18 18:32:55 +0100
commit6a4a86b302cfc63e6917aef1f6937dae90646571 (patch)
tree2a196f81315b1fdd1d2bd5535a4a8846e5d36e9a /srcgen
parent8ac7044ca44505764fdc9fa4e96fe79c11a2736a (diff)
downloadbouncycastle-6a4a86b302cfc63e6917aef1f6937dae90646571.tar.gz
Use generated source not jarjar
This commit switches to generating source code for com.android.org.bouncycastle used on Android devices rather than relying on jajar to repackage the bytecode. Source code can (and must) be regenerated using scripts in external/bouncycastle/srcgen when changes are made to bouncycastle source code. Having source code for the Android internal bouncycastle is being done to enable the code to be included in stubs for system and other internal API surfaces: the stubs generation relies on having source (which can be annotated, or just have associated API metadata). Test: build Test: CtsLibcoreTestCases Bug: 111055375 Bug: 111734251 Change-Id: I6d21774088e73c936e99059a9a0f071080174015
Diffstat (limited to 'srcgen')
-rw-r--r--srcgen/Android.bp22
-rwxr-xr-xsrcgen/generate_android_src.sh35
-rw-r--r--srcgen/src/main/java/com/android/bouncycastle/srcgen/BouncyCastleTransform.java101
3 files changed, 158 insertions, 0 deletions
diff --git a/srcgen/Android.bp b/srcgen/Android.bp
new file mode 100644
index 00000000..c5ec8f9b
--- /dev/null
+++ b/srcgen/Android.bp
@@ -0,0 +1,22 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// 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.
+
+// Build the bouncycastle srcgen jar
+// ============================================================
+
+java_library_host {
+ name: "android_bouncycastle_srcgen",
+ libs: ["currysrc"],
+ srcs: ["src/**/*.java"],
+}
diff --git a/srcgen/generate_android_src.sh b/srcgen/generate_android_src.sh
new file mode 100755
index 00000000..5b8d3dfd
--- /dev/null
+++ b/srcgen/generate_android_src.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+if [ -z "$ANDROID_BUILD_TOP" ]; then
+ echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" 1>&2
+ exit 1
+fi
+
+CLASSPATH=${ANDROID_HOST_OUT}/framework/currysrc.jar:${ANDROID_HOST_OUT}/framework/android_bouncycastle_srcgen.jar
+BOUNCY_CASTLE_DIR=${ANDROID_BUILD_TOP}/external/bouncycastle
+
+cd ${ANDROID_BUILD_TOP}
+make -j15 currysrc android_bouncycastle_srcgen
+
+function do_transform() {
+ SRC_IN_DIR=$1
+ SRC_OUT_DIR=$2
+
+ if [ ! -d $SRC_OUT_DIR ]; then
+ echo ${SRC_OUT_DIR} does not exist >&2
+ exit 1
+ fi
+ rm -rf ${SRC_OUT_DIR}
+ mkdir -p ${SRC_OUT_DIR}
+
+ java -cp ${CLASSPATH} com.android.bouncycastle.srcgen.BouncyCastleTransform ${SRC_IN_DIR} ${SRC_OUT_DIR}
+}
+
+BCPROV_SRC_IN_DIR=${BOUNCY_CASTLE_DIR}/bcprov/src/main/java
+BCPROV_SRC_OUT_DIR=${BOUNCY_CASTLE_DIR}/android_bcprov/src/main/java
+do_transform ${BCPROV_SRC_IN_DIR} ${BCPROV_SRC_OUT_DIR}
+
+BCPKIX_SRC_IN_DIR=${BOUNCY_CASTLE_DIR}/bcpkix/src/main/java
+BCPKIX_SRC_OUT_DIR=${BOUNCY_CASTLE_DIR}/android_bcpkix/src/main/java
+do_transform ${BCPKIX_SRC_IN_DIR} ${BCPKIX_SRC_OUT_DIR}
+
diff --git a/srcgen/src/main/java/com/android/bouncycastle/srcgen/BouncyCastleTransform.java b/srcgen/src/main/java/com/android/bouncycastle/srcgen/BouncyCastleTransform.java
new file mode 100644
index 00000000..99e1796b
--- /dev/null
+++ b/srcgen/src/main/java/com/android/bouncycastle/srcgen/BouncyCastleTransform.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * 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.
+ */
+package com.android.bouncycastle.srcgen;
+
+import com.google.currysrc.Main;
+import com.google.currysrc.api.Rules;
+import com.google.currysrc.api.input.CompoundDirectoryInputFileGenerator;
+import com.google.currysrc.api.input.DirectoryInputFileGenerator;
+import com.google.currysrc.api.input.InputFileGenerator;
+import com.google.currysrc.api.match.SourceMatchers;
+import com.google.currysrc.api.output.BasicOutputSourceFileGenerator;
+import com.google.currysrc.api.output.OutputSourceFileGenerator;
+import com.google.currysrc.api.process.DefaultRule;
+import com.google.currysrc.api.process.Processor;
+import com.google.currysrc.api.process.Rule;
+import com.google.currysrc.processors.InsertHeader;
+import com.google.currysrc.processors.ModifyQualifiedNames;
+import com.google.currysrc.processors.ModifyStringLiterals;
+import com.google.currysrc.processors.RenamePackage;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Generates bouncycastle sources in the com.android.org.bouncycastle package.
+ */
+public class BouncyCastleTransform {
+ static final String ORIGINAL_PACKAGE = "org.bouncycastle";
+ static final String ANDROID_PACKAGE = "com.android.org.bouncycastle";
+
+ /**
+ * Usage:
+ * java BouncyCastleTransform {source dir} {target dir}
+ */
+ public static void main(String[] args) throws Exception {
+ String sourceDir = args[0];
+ String targetDir = args[1];
+ new Main(false /* debug */).execute(new BouncyCastleRules(sourceDir, targetDir));
+ }
+
+ static class BouncyCastleRules implements Rules {
+ private final String sourceDir;
+ private final String targetDir;
+
+ BouncyCastleRules(String sourceDir, String targetDir) {
+ this.sourceDir = sourceDir;
+ this.targetDir = targetDir;
+ }
+
+ @Override
+ public InputFileGenerator getInputFileGenerator() {
+ return new DirectoryInputFileGenerator(new File(sourceDir));
+ }
+
+ @Override
+ public List<Rule> getRuleList(File ignored) {
+ return Arrays.asList(
+ // Doc change: Insert a warning about the source code being generated.
+ createMandatoryRule(new InsertHeader("/* GENERATED SOURCE. DO NOT MODIFY. */\n")),
+ // AST change: Change the package of each CompilationUnit
+ createMandatoryRule(new RenamePackage(ORIGINAL_PACKAGE, ANDROID_PACKAGE)),
+ // AST change: Change all qualified names in code and javadoc.
+ createOptionalRule(new ModifyQualifiedNames(ORIGINAL_PACKAGE, ANDROID_PACKAGE)),
+ // AST change: Change all string literals containing package names in code.
+ createOptionalRule(new ModifyStringLiterals(ORIGINAL_PACKAGE, ANDROID_PACKAGE))
+ );
+ }
+
+ @Override
+ public OutputSourceFileGenerator getOutputSourceFileGenerator() {
+ File outputDir = new File(targetDir);
+ return new BasicOutputSourceFileGenerator(outputDir);
+ }
+ }
+
+ public static DefaultRule createMandatoryRule(Processor processor) {
+ return new DefaultRule(processor, SourceMatchers.all(), true /* mustModify */);
+ }
+
+ public static DefaultRule createOptionalRule(Processor processor) {
+ return new DefaultRule(processor, SourceMatchers.all(), false /* mustModify */);
+ }
+
+ private BouncyCastleTransform() {
+ }
+}