blob: b9f12f2b8d5153bfe6284e18e6178f3e29c548a0 [file] [log] [blame] [view]
Ian Vollickb99472e2019-03-07 21:35:261# Accessing C++ Switches In Java
2
3[TOC]
4
5## Introduction
6
7Accessing C++ switches in Java is implemented via a Python script which analyzes
8the C++ switches file and spits out the corresponding Java class. The generated
9class name will be based upon the switch file name, and the path must be
10specified in a comment within the switch file itself.
11
12## Usage
13
141. Add directives to your C++ switch file
15
16 ```cpp
17 // GENERATED_JAVA_PACKAGE: org.chromium.chrome
18
19 // ...snip...
20
21 // Documentation for the following switch.
22 const char kSomeSwitch[] = "some-switch";
23
24 // ...snip...
25 ```
26
272. Create a template file
28 ```java
29 // Copyright {YEAR} The Chromium Authors. All rights reserved.
30 // Use of this source code is governed by a BSD-style license that can be
31 // found in the LICENSE file.
32
33 // This file is autogenerated by
34 // {SCRIPT_NAME}
35 // From
36 // {SOURCE_PATH}, and
37 // {TEMPLATE_PATH}
38
39 package my.java.package
40
41 // Be sure to escape any curly braces in your template by doubling as
42 // follows.
43 public abstract class MySwitches {{
44
45 {NATIVE_SWITCHES}
46
47 }}
48 ```
49
503. Add a new build target
51
52 ```gn
53 import("//build/config/android/rules.gni")
54
55 java_cpp_strings("foo_generated_switch") {
56 sources = [
57 "//base/android/native_foo_switches.cc",
58 ]
59 template = "//base/android/java_templates/MySwitches.java.tmpl"
60 }
61 ```
62
635. Add the new target to the desired android_library targets srcjar_deps:
64
65 ```gn
66 android_library("base_java") {
67 srcjar_deps = [
68 ":foo_generated_switches",
69 ]
70 }
71 ```
72
735. The generated file `org/chromium/chrome/NativeFooSwitches.java` would contain:
74
75 ```java
76 package org.chromium.chrome;
77
78 public final class NativeFooSwitches {
79 // ...snip...
80
81 public static final String SOME_SWITCH = "some-switch";
82
83 // ...snip...
84 }
85 ```
86
87## Code
88* [Generator
89code](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_strings.py?dr=C&sq=package:chromium)
90and
91[Tests](https://cs.chromium.org/chromium/src/build/android/gyp/java_cpp_strings_tests.py?dr=C&sq=package:chromium)
92* [GN
93template](https://cs.chromium.org/chromium/src/build/config/android/rules.gni?sq=package:chromium&dr=C)