Bug 1291366 - Part 1: Use GENERATED_FILES to produce AppConstants.java. r=gps draft
authorNick Alexander <nalexander@mozilla.com>
Mon, 14 Nov 2016 20:06:31 -0800
changeset 445761 fddadcf8da9c1706685aa822dfa8f856d8182674
parent 445756 1bd3f784338d23edb0ce8560b5a8874084e99b72
child 445762 6296e08b7737002f9547cccfcfe8f35b6e7f7e8a
push id37604
push usernalexander@mozilla.com
push dateWed, 30 Nov 2016 07:13:23 +0000
reviewersgps
bugs1291366
milestone53.0a1
Bug 1291366 - Part 1: Use GENERATED_FILES to produce AppConstants.java. r=gps This patch lays the groundwork for two things. First, it paves the way for splitting AppConstants.java into two parts, a GeckoView part and a Fennec part. This is necessary because the Makefile.in preprocessing is not flexible enough to write two separate GeckoView and Fennec constants files into different directories. Second, this allows us to more flexibly generate the file contents. Gradle has a way to get compile-time constants into Java code, which we want to migrate to. The details don't matter right here, but this paves the way to move from preprocessing to generating the Gradle-style BuildConfig files while we continue to support both build systems. MozReview-Commit-ID: 2o8X99uLoaM
mobile/android/base/AdjustConstants.java.in
mobile/android/base/AppConstants.java.in
mobile/android/base/Makefile.in
mobile/android/base/adjust_sdk_app_token.in
mobile/android/base/generate_build_config.py
mobile/android/base/moz.build
--- a/mobile/android/base/AdjustConstants.java.in
+++ b/mobile/android/base/AdjustConstants.java.in
@@ -1,10 +1,9 @@
 //#filter substitution
-//#include @OBJDIR@/adjust_sdk_app_token
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.adjust.AdjustHelperInterface;
--- a/mobile/android/base/AppConstants.java.in
+++ b/mobile/android/base/AppConstants.java.in
@@ -88,18 +88,17 @@ public class AppConstants {
     // GRE_BUILDID is exactly the same as MOZ_APP_BUILDID unless you're running
     // on XULRunner, which is never the case on Android.
     public static final String MOZ_APP_BUILDID = "@MOZ_BUILDID@";
     public static final String MOZ_APP_ID = "@MOZ_APP_ID@";
     public static final String MOZ_APP_NAME = "@MOZ_APP_NAME@";
     public static final String MOZ_APP_VENDOR = "@MOZ_APP_VENDOR@";
     public static final String MOZ_APP_VERSION = "@MOZ_APP_VERSION@";
     public static final String MOZ_APP_DISPLAYNAME = "@MOZ_APP_DISPLAYNAME@";
-    // MOZ_APP_UA_NAME is already quoted when it gets substituted, like MOZILLA_VERSION.
-    public static final String MOZ_APP_UA_NAME = @MOZ_APP_UA_NAME@;
+    public static final String MOZ_APP_UA_NAME = "@MOZ_APP_UA_NAME@";
 
     // MOZILLA_VERSION is already quoted when it gets substituted in. If we
     // add additional quotes we end up with ""x.y"", which is a syntax error.
     public static final String MOZILLA_VERSION = @MOZILLA_VERSION@;
 
     public static final String MOZ_MOZILLA_API_KEY = "@MOZ_MOZILLA_API_KEY@";
     public static final boolean MOZ_STUMBLER_BUILD_TIME_ENABLED =
 //#ifdef MOZ_ANDROID_MLS_STUMBLER
@@ -121,17 +120,17 @@ public class AppConstants {
 //#else
     null;
 //#endif
 
     public static final String MOZ_CHILD_PROCESS_NAME = "@MOZ_CHILD_PROCESS_NAME@";
     public static final String MOZ_UPDATE_CHANNEL = "@MOZ_UPDATE_CHANNEL@";
     public static final String OMNIJAR_NAME = "@OMNIJAR_NAME@";
     public static final String OS_TARGET = "@OS_TARGET@";
-    public static final String TARGET_XPCOM_ABI = @TARGET_XPCOM_ABI@;
+    public static final String TARGET_XPCOM_ABI = "@TARGET_XPCOM_ABI@";
 
     public static final String USER_AGENT_BOT_LIKE = "Redirector/" + AppConstants.MOZ_APP_VERSION +
         " (Android; rv:" + AppConstants.MOZ_APP_VERSION + ")";
 
     public static final String USER_AGENT_FENNEC_MOBILE = "Mozilla/5.0 (Android " +
         Build.VERSION.RELEASE + "; Mobile; rv:" +
         AppConstants.MOZ_APP_VERSION + ") Gecko/" +
         AppConstants.MOZ_APP_VERSION + " Firefox/" +
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -320,34 +320,16 @@ GeneratedJNIWrappers.cpp: $(ANNOTATION_P
 
 # This annotation processing step also generates
 # FennecJNIWrappers.h and FennecJNINatives.h
 FennecJNIWrappers.cpp: $(ANNOTATION_PROCESSOR_JAR_FILES) $(FENNEC_JARS)
 	$(JAVA) -classpath $(all_jars_classpath):$(JAVA_BOOTCLASSPATH):$(JAVA_CLASSPATH):$(ANNOTATION_PROCESSOR_JAR_FILES) \
 		org.mozilla.gecko.annotationProcessors.AnnotationProcessor \
 		Fennec $(FENNEC_JARS)
 
-# Certain source files need to be preprocessed.  This special rule
-# generates these files into generated/org/mozilla/gecko for
-# consumption by the build system and IDEs.
-
-# The list in moz.build looks like
-# 'preprocessed/org/mozilla/gecko/AppConstants.java'.  The list in
-# constants_PP_JAVAFILES looks like
-# 'generated/preprocessed/org/mozilla/gecko/AppConstants.java'.  We
-# need to write AppConstants.java.in to
-# generated/preprocessed/org/mozilla/gecko.
-preprocessed := $(addsuffix .in,$(subst generated/preprocessed/org/mozilla/gecko/,,$(filter generated/preprocessed/org/mozilla/gecko/%,$(constants_PP_JAVAFILES))))
-
-preprocessed_PATH := generated/preprocessed/org/mozilla/gecko
-preprocessed_KEEP_PATH := 1
-preprocessed_FLAGS := --marker='//\\\#'
-
-PP_TARGETS += preprocessed
-
 include $(topsrcdir)/config/rules.mk
 
 not_android_res_files := \
   *.mkdir.done* \
   *.DS_Store* \
   *\#* \
   *.rej \
   *.orig \
deleted file mode 100644
--- a/mobile/android/base/adjust_sdk_app_token.in
+++ /dev/null
@@ -1,3 +0,0 @@
-//#ifdef MOZ_INSTALL_TRACKING
-//#define MOZ_INSTALL_TRACKING_ADJUST_SDK_APP_TOKEN @MOZ_ADJUST_SDK_KEY@
-//#endif
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/generate_build_config.py
@@ -0,0 +1,116 @@
+#!/bin/python
+
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+'''
+Generate BuildConfig Java source files.
+
+BuildConfig files are the Gradle way of configuring Java values at build time.
+
+This is the moz.build equivalent of Gradle's `buildConfigField` (see
+http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html#com.android.build.gradle.internal.dsl.ProductFlavor:buildConfigField(java.lang.String,%20java.lang.String,%20java.lang.String).
+This mechanism will be replaced with the Gradle native version as we
+transition to Gradle.
+'''
+
+from __future__ import (
+    print_function,
+    unicode_literals,
+)
+
+from collections import defaultdict
+import os
+import sys
+
+import buildconfig
+from mozbuild.preprocessor import Preprocessor
+
+
+def main(output_file, input_filename):
+    # input_filename is an absolute path, so there's no need to join with __DIR__.
+    pp = Preprocessor(defines=buildconfig.defines, marker='//#')
+
+    CONFIG = defaultdict(lambda: None)
+    CONFIG.update(buildconfig.substs)
+    DEFINES = {}
+
+    for var in ('MOZ_ANDROID_ACTIVITY_STREAM'
+                'MOZ_ANDROID_ANR_REPORTER',
+                'MOZ_ANDROID_BEAM',
+                'MOZ_ANDROID_CUSTOM_TABS',
+                'MOZ_ANDROID_DOWNLOADS_INTEGRATION',
+                'MOZ_ANDROID_DOWNLOAD_CONTENT_SERVICE',
+                'MOZ_ANDROID_EXCLUDE_FONTS',
+                'MOZ_ANDROID_GCM',
+                'MOZ_ANDROID_MLS_STUMBLER',
+                'MOZ_ANDROID_SEARCH_ACTIVITY',
+                'MOZ_DEBUG',
+                'MOZ_INSTALL_TRACKING',
+                'MOZ_LOCALE_SWITCHER',
+                'MOZ_NATIVE_DEVICES',
+                'MOZ_SWITCHBOARD'):
+        if CONFIG[var]:
+            DEFINES[var] = 1
+
+    for var in ('MOZ_ANDROID_GCM_SENDERID',
+                'MOZ_PKG_SPECIAL',
+                'MOZ_UPDATER'):
+        if CONFIG[var]:
+            DEFINES[var] = CONFIG[var]
+
+    for var in ('ANDROID_CPU_ARCH',
+                'ANDROID_PACKAGE_NAME',
+                'GRE_MILESTONE',
+                'MOZ_ANDROID_APPLICATION_CLASS',
+                'MOZ_ANDROID_BROWSER_INTENT_CLASS',
+                'MOZ_ANDROID_SEARCH_INTENT_CLASS',
+                'MOZ_APP_BASENAME',
+                'MOZ_APP_DISPLAYNAME',
+                'MOZ_APP_ID',
+                'MOZ_APP_NAME',
+                'MOZ_APP_UA_NAME',
+                'MOZ_APP_VENDOR',
+                'MOZ_APP_VERSION',
+                'MOZ_CHILD_PROCESS_NAME',
+                'MOZ_CRASHREPORTER',
+                'MOZ_MOZILLA_API_KEY',
+                'MOZ_UPDATE_CHANNEL',
+                'OMNIJAR_NAME',
+                'OS_TARGET',
+                'TARGET_XPCOM_ABI'):
+        DEFINES[var] = CONFIG[var]
+
+    # Mangle our package name to avoid Bug 750548.
+    DEFINES['MANGLED_ANDROID_PACKAGE_NAME'] = CONFIG['ANDROID_PACKAGE_NAME'].replace('fennec', 'f3nn3c')
+    DEFINES['MOZ_APP_ABI'] = CONFIG['TARGET_XPCOM_ABI']
+    if not CONFIG['COMPILE_ENVIRONMENT']:
+        # These should really come from the included binaries, but that's not easy.
+        DEFINES['MOZ_APP_ABI'] = 'arm-eabi-gcc3'
+        DEFINES['TARGET_XPCOM_ABI'] = 'arm-eabi-gcc3'
+
+    if '-march=armv7' in CONFIG['OS_CFLAGS']:
+        DEFINES['MOZ_MIN_CPU_VERSION'] = 7
+    else:
+        DEFINES['MOZ_MIN_CPU_VERSION'] = 5
+
+    # It's okay to use MOZ_ADJUST_SDK_KEY here because this doesn't
+    # leak the value to build logs.
+    if CONFIG['MOZ_INSTALL_TRACKING']:
+        DEFINES['MOZ_INSTALL_TRACKING_ADJUST_SDK_APP_TOKEN'] = CONFIG['MOZ_ADJUST_SDK_KEY']
+
+    # TODO: mark buildid.h as a dependency?  How about the buildconfig itself?
+    DEFINES['MOZ_BUILDID'] = open(os.path.join(buildconfig.topobjdir, 'buildid.h')).readline().split()[2]
+
+    pp.context.update(DEFINES)
+
+    with open(input_filename, 'rU') as input:
+        pp.processFile(input=input, output=output_file)
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main(sys.stdout, *sys.argv[1:]))
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -1,17 +1,26 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 DIRS += ['locales']
 
-CONFIGURE_SUBST_FILES += ['adjust_sdk_app_token']
+GENERATED_FILES += [
+    'generated/preprocessed/org/mozilla/gecko/AdjustConstants.java',
+    'generated/preprocessed/org/mozilla/gecko/AppConstants.java',
+]
+x = GENERATED_FILES['generated/preprocessed/org/mozilla/gecko/AdjustConstants.java']
+x.script = 'generate_build_config.py'
+x.inputs += ['AdjustConstants.java.in']
+y = GENERATED_FILES['generated/preprocessed/org/mozilla/gecko/AppConstants.java']
+y.script = 'generate_build_config.py'
+y.inputs += ['AppConstants.java.in']
 
 include('android-services.mozbuild')
 
 geckoview_source_dir = TOPSRCDIR + '/mobile/android/geckoview/src/main/'
 geckoview_thirdparty_source_dir = TOPSRCDIR + '/mobile/android/geckoview/src/thirdparty/'
 thirdparty_source_dir = TOPSRCDIR + '/mobile/android/thirdparty/'
 
 constants_jar = add_java_jar('constants')