
This adds the java_cpp_features GN rule to autogenerate Java String constants representing C++ features. This refactors parts of java_cpp_strings so java_cpp_features can share it. This aims to address the most common syntaxes for declaring C++ features (string literal names, "brace" and "equals brace" initialization, with & without the "base::" namespace). Design: http://go/autogen-java-features Bug: 1060097 Fixed: 1091031 Test: vpython build/android/gpy/java_cpp_strings_tests.py Test: vpython build/android/gpy/java_cpp_features_tests.py Test: tools/md_browser/md_browser.py docs/android_accessing_cpp_features_in_java.md Change-Id: I5311f72f8837c122186148cf183f4219087df96a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2412840 Auto-Submit: Nate Fischer <ntfschr@chromium.org> Reviewed-by: Andrew Grieve <agrieve@chromium.org> Reviewed-by: Henrique Nakashima <hnakashima@chromium.org> Reviewed-by: Peter Wen <wnwen@chromium.org> Commit-Queue: Andrew Grieve <agrieve@chromium.org> Cr-Commit-Position: refs/heads/master@{#812889}
3.2 KiB
3.2 KiB
Accessing C++ Switches In Java
[TOC]
Introduction
Accessing C++ switches in Java is implemented via a Python script which analyzes the C++ switches file and generates the corresponding Java class, based on a template file. The template file must be specified in the GN target.
Usage
-
Create a template file (ex.
FooSwitches.java.tmpl
). Change "Copyright 2020" to be whatever the year is at the time of writing (as you would for any other file).// Copyright 2020 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. package org.chromium.foo; // Be sure to escape any curly braces in your template by doubling as // follows. /** * Contains command line switches that are specific to the foo project. */ public final class FooSwitches {{ {NATIVE_STRINGS} // Prevents instantiation. private FooSwitches() {{}} }}
-
Add a new build target and add it to the
srcjar_deps
of anandroid_library
target:if (is_android) { import("//build/config/android/rules.gni") } if (is_android) { java_cpp_strings("java_switches_srcjar") { # External code should depend on ":foo_java" instead. visibility = [ ":*" ] sources = [ "//base/android/foo_switches.cc", ] template = "//base/android/java_templates/FooSwitches.java.tmpl" } # If there's already an android_library target, you can add # java_switches_srcjar to that target's srcjar_deps. Otherwise, the best # practice is to create a new android_library just for this target. android_library("foo_java") { srcjar_deps = [ ":java_switches_srcjar" ] } }
-
The generated file
out/Default/gen/.../org/chromium/foo/FooSwitches.java
would contain:// Copyright $YEAR The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. package org.chromium.foo; /** * Contains command line switches that are specific to the foo project. */ public final class FooSwitches { // ...snip... // This following string constants were inserted by // java_cpp_strings.py // From // ../../base/android/foo_switches.cc // Into // ../../base/android/java_templates/FooSwitches.java.tmpl // Documentation for the C++ switch is copied here. public static final String SOME_SWITCH = "some-switch"; // ...snip... // Prevents instantiation. private FooSwitches() {} }