Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 1 | # Accessing C++ Switches In Java |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## Introduction |
| 6 | |
| 7 | Accessing C++ switches in Java is implemented via a Python script which analyzes |
Nate Fischer | eb70fb4 | 2020-08-22 15:25:55 | [diff] [blame] | 8 | the C++ switches file and generates the corresponding Java class, based on a |
| 9 | template file. The template file must be specified in the GN target. |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 10 | |
| 11 | ## Usage |
| 12 | |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 13 | 1. Create a template file (ex. `FooSwitches.java.tmpl`). Change "Copyright |
| 14 | 2020" to be whatever the year is at the time of writing (as you would for any |
| 15 | other file). |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 16 | ```java |
Avi Drissman | 7b017a99 | 2022-09-07 15:50:38 | [diff] [blame] | 17 | // Copyright 2020 The Chromium Authors |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 18 | // Use of this source code is governed by a BSD-style license that can be |
| 19 | // found in the LICENSE file. |
| 20 | |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 21 | package org.chromium.foo; |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 22 | |
| 23 | // Be sure to escape any curly braces in your template by doubling as |
| 24 | // follows. |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 25 | /** |
| 26 | * Contains command line switches that are specific to the foo project. |
| 27 | */ |
| 28 | public final class FooSwitches {{ |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 29 | |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 30 | {NATIVE_STRINGS} |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 31 | |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 32 | // Prevents instantiation. |
| 33 | private FooSwitches() {{}} |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 34 | }} |
| 35 | ``` |
| 36 | |
Nate Fischer | eb70fb4 | 2020-08-22 15:25:55 | [diff] [blame] | 37 | 2. Add a new build target and add it to the `srcjar_deps` of an |
| 38 | `android_library` target: |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 39 | |
| 40 | ```gn |
Nate Fischer | eb70fb4 | 2020-08-22 15:25:55 | [diff] [blame] | 41 | if (is_android) { |
| 42 | import("//build/config/android/rules.gni") |
| 43 | } |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 44 | |
Nate Fischer | eb70fb4 | 2020-08-22 15:25:55 | [diff] [blame] | 45 | if (is_android) { |
| 46 | java_cpp_strings("java_switches_srcjar") { |
| 47 | # External code should depend on ":foo_java" instead. |
| 48 | visibility = [ ":*" ] |
| 49 | sources = [ |
| 50 | "//base/android/foo_switches.cc", |
| 51 | ] |
| 52 | template = "//base/android/java_templates/FooSwitches.java.tmpl" |
| 53 | } |
| 54 | |
| 55 | # If there's already an android_library target, you can add |
| 56 | # java_switches_srcjar to that target's srcjar_deps. Otherwise, the best |
| 57 | # practice is to create a new android_library just for this target. |
| 58 | android_library("foo_java") { |
| 59 | srcjar_deps = [ ":java_switches_srcjar" ] |
| 60 | } |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 61 | } |
| 62 | ``` |
| 63 | |
Nate Fischer | eb70fb4 | 2020-08-22 15:25:55 | [diff] [blame] | 64 | 3. The generated file `out/Default/gen/.../org/chromium/foo/FooSwitches.java` |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 65 | would contain: |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 66 | |
| 67 | ```java |
Avi Drissman | 7b017a99 | 2022-09-07 15:50:38 | [diff] [blame] | 68 | // Copyright $YEAR The Chromium Authors |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 69 | // Use of this source code is governed by a BSD-style license that can be |
| 70 | // found in the LICENSE file. |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 71 | |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 72 | package org.chromium.foo; |
| 73 | |
| 74 | /** |
| 75 | * Contains command line switches that are specific to the foo project. |
| 76 | */ |
| 77 | public final class FooSwitches { |
| 78 | |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 79 | // ...snip... |
| 80 | |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 81 | // This following string constants were inserted by |
| 82 | // java_cpp_strings.py |
| 83 | // From |
| 84 | // ../../base/android/foo_switches.cc |
| 85 | // Into |
| 86 | // ../../base/android/java_templates/FooSwitches.java.tmpl |
| 87 | |
| 88 | // Documentation for the C++ switch is copied here. |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 89 | public static final String SOME_SWITCH = "some-switch"; |
| 90 | |
| 91 | // ...snip... |
Nate Fischer | 9320324 | 2019-10-16 15:31:58 | [diff] [blame] | 92 | |
| 93 | // Prevents instantiation. |
| 94 | private FooSwitches() {} |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 95 | } |
| 96 | ``` |
| 97 | |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 98 | ## See also |
| 99 | * [Accessing C++ Enums In Java](android_accessing_cpp_enums_in_java.md) |
| 100 | * [Accessing C++ Features In Java](android_accessing_cpp_features_in_java.md) |
| 101 | |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 102 | ## Code |
| 103 | * [Generator |
| 104 | code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_strings.py?dr=C&sq=package:chromium) |
| 105 | and |
| 106 | [Tests](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_strings_tests.py?dr=C&sq=package:chromium) |
| 107 | * [GN |
| 108 | template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?sq=package:chromium&dr=C) |