1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tools.layoutlib.create;
18
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.List;
22import java.util.Map;
23import java.util.Set;
24
25
26/**
27 * Entry point for the layoutlib_create tool.
28 * <p/>
29 * The tool does not currently rely on any external configuration file.
30 * Instead the configuration is mostly done via the {@link CreateInfo} class.
31 * <p/>
32 * For a complete description of the tool and its implementation, please refer to
33 * the "README.txt" file at the root of this project.
34 * <p/>
35 * For a quick test, invoke this as follows:
36 * <pre>
37 * $ make layoutlib
38 * </pre>
39 * which does:
40 * <pre>
41 * $ make layoutlib_create &lt;bunch of framework jars&gt;
42 * $ java -jar out/host/linux-x86/framework/layoutlib_create.jar \
43 *        out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar \
44 *        out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar \
45 *        out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
46 * </pre>
47 */
48public class Main {
49
50    public static class Options {
51        public boolean listAllDeps = false;
52        public boolean listOnlyMissingDeps = false;
53    }
54
55    public static final Options sOptions = new Options();
56
57    public static void main(String[] args) {
58
59        Log log = new Log();
60
61        ArrayList<String> osJarPath = new ArrayList<String>();
62        String[] osDestJar = { null };
63
64        if (!processArgs(log, args, osJarPath, osDestJar)) {
65            log.error("Usage: layoutlib_create [-v] output.jar input.jar ...");
66            log.error("Usage: layoutlib_create [-v] [--list-deps|--missing-deps] input.jar ...");
67            System.exit(1);
68        }
69
70        if (sOptions.listAllDeps || sOptions.listOnlyMissingDeps) {
71            System.exit(listDeps(osJarPath, log));
72
73        } else {
74            System.exit(createLayoutLib(osDestJar[0], osJarPath, log));
75        }
76
77
78        System.exit(1);
79    }
80
81    private static int createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log) {
82        log.info("Output: %1$s", osDestJar);
83        for (String path : osJarPath) {
84            log.info("Input :      %1$s", path);
85        }
86
87        try {
88            CreateInfo info = new CreateInfo();
89            Set<String> excludeClasses = info.getExcludedClasses();
90            AsmGenerator agen = new AsmGenerator(log, osDestJar, info);
91
92            AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen,
93                    new String[] {                          // derived from
94                        "android.view.View",
95                        "android.app.Fragment"
96                    },
97                    new String[] {                          // include classes
98                        "android.*", // for android.R
99                        "android.util.*",
100                        "com.android.internal.util.*",
101                        "android.view.*",
102                        "android.widget.*",
103                        "com.android.internal.widget.*",
104                        "android.text.**",
105                        "android.graphics.*",
106                        "android.graphics.drawable.**",
107                        "android.content.*",
108                        "android.content.res.*",
109                        "android.preference.*",
110                        "org.apache.harmony.xml.*",
111                        "com.android.internal.R**",
112                        "android.pim.*", // for datepicker
113                        "android.os.*",  // for android.os.Handler
114                        "android.database.ContentObserver", // for Digital clock
115                        "com.android.i18n.phonenumbers.*",  // for TextView with autolink attribute
116                        "android.app.DatePickerDialog",     // b.android.com/28318
117                        "android.app.TimePickerDialog",     // b.android.com/61515
118                        "com.android.internal.view.menu.ActionMenu",
119                        "android.icu.**",                   // needed by LayoutLib
120                        "android.annotation.NonNull",       // annotations
121                        "android.annotation.Nullable",      // annotations
122                        "com.android.internal.transition.EpicenterTranslateClipReveal",
123                    },
124                    excludeClasses,
125                    new String[] {
126                        "com/android/i18n/phonenumbers/data/*",
127                        "android/icu/impl/data/**"
128                    });
129            aa.analyze();
130            agen.generate();
131
132            // Throw an error if any class failed to get renamed by the generator
133            //
134            // IMPORTANT: if you're building the platform and you get this error message,
135            // it means the renameClasses[] array in AsmGenerator needs to be updated: some
136            // class should have been renamed but it was not found in the input JAR files.
137            Set<String> notRenamed = agen.getClassesNotRenamed();
138            if (notRenamed.size() > 0) {
139                // (80-column guide below for error formatting)
140                // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
141                log.error(
142                  "ERROR when running layoutlib_create: the following classes are referenced\n" +
143                  "by tools/layoutlib/create but were not actually found in the input JAR files.\n" +
144                  "This may be due to some platform classes having been renamed.");
145                for (String fqcn : notRenamed) {
146                    log.error("- Class not found: %s", fqcn.replace('/', '.'));
147                }
148                for (String path : osJarPath) {
149                    log.info("- Input JAR : %1$s", path);
150                }
151                return 1;
152            }
153
154            return 0;
155        } catch (IOException e) {
156            log.exception(e, "Failed to load jar");
157        } catch (LogAbortException e) {
158            e.error(log);
159        }
160
161        return 1;
162    }
163
164    private static int listDeps(ArrayList<String> osJarPath, Log log) {
165        DependencyFinder df = new DependencyFinder(log);
166        try {
167            List<Map<String, Set<String>>> result = df.findDeps(osJarPath);
168            if (sOptions.listAllDeps) {
169                df.printAllDeps(result);
170            } else if (sOptions.listOnlyMissingDeps) {
171                df.printMissingDeps(result);
172            }
173        } catch (IOException e) {
174            log.exception(e, "Failed to load jar");
175        }
176
177        return 0;
178    }
179
180    /**
181     * Returns true if args where properly parsed.
182     * Returns false if program should exit with command-line usage.
183     * <p/>
184     * Note: the String[0] is an output parameter wrapped in an array, since there is no
185     * "out" parameter support.
186     */
187    private static boolean processArgs(Log log, String[] args,
188            ArrayList<String> osJarPath, String[] osDestJar) {
189        boolean needs_dest = true;
190        for (String s : args) {
191            if (s.equals("-v")) {
192                log.setVerbose(true);
193            } else if (s.equals("--list-deps")) {
194                sOptions.listAllDeps = true;
195                needs_dest = false;
196            } else if (s.equals("--missing-deps")) {
197                sOptions.listOnlyMissingDeps = true;
198                needs_dest = false;
199            } else if (!s.startsWith("-")) {
200                if (needs_dest && osDestJar[0] == null) {
201                    osDestJar[0] = s;
202                } else {
203                    osJarPath.add(s);
204                }
205            } else {
206                log.error("Unknown argument: %s", s);
207                return false;
208            }
209        }
210
211        if (osJarPath.isEmpty()) {
212            log.error("Missing parameter: path to input jar");
213            return false;
214        }
215        if (needs_dest && osDestJar[0] == null) {
216            log.error("Missing parameter: path to output jar");
217            return false;
218        }
219
220        return true;
221    }
222}
223