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 generatePublicAccess = true;
52        public boolean listAllDeps = false;
53        public boolean listOnlyMissingDeps = false;
54    }
55
56    public static final Options sOptions = new Options();
57
58    public static void main(String[] args) {
59
60        Log log = new Log();
61
62        ArrayList<String> osJarPath = new ArrayList<String>();
63        String[] osDestJar = { null };
64
65        if (!processArgs(log, args, osJarPath, osDestJar)) {
66            log.error("Usage: layoutlib_create [-v] [-p] output.jar input.jar ...");
67            log.error("Usage: layoutlib_create [-v] [--list-deps|--missing-deps] input.jar ...");
68            System.exit(1);
69        }
70
71        if (sOptions.listAllDeps || sOptions.listOnlyMissingDeps) {
72            System.exit(listDeps(osJarPath, log));
73
74        } else {
75            System.exit(createLayoutLib(osDestJar[0], osJarPath, log));
76        }
77
78
79        System.exit(1);
80    }
81
82    private static int createLayoutLib(String osDestJar, ArrayList<String> osJarPath, Log log) {
83        log.info("Output: %1$s", osDestJar);
84        for (String path : osJarPath) {
85            log.info("Input :      %1$s", path);
86        }
87
88        try {
89            AsmGenerator agen = new AsmGenerator(log, osDestJar, new CreateInfo());
90
91            AsmAnalyzer aa = new AsmAnalyzer(log, osJarPath, agen,
92                    new String[] {                          // derived from
93                        "android.view.View",
94                        "android.app.Fragment"
95                    },
96                    new String[] {                          // include classes
97                        "android.*", // for android.R
98                        "android.util.*",
99                        "com.android.internal.util.*",
100                        "android.view.*",
101                        "android.widget.*",
102                        "com.android.internal.widget.*",
103                        "android.text.**",
104                        "android.graphics.*",
105                        "android.graphics.drawable.*",
106                        "android.content.*",
107                        "android.content.res.*",
108                        "org.apache.harmony.xml.*",
109                        "com.android.internal.R**",
110                        "android.pim.*", // for datepicker
111                        "android.os.*",  // for android.os.Handler
112                        "android.database.ContentObserver", // for Digital clock
113                        });
114            aa.analyze();
115            agen.generate();
116
117            // Throw an error if any class failed to get renamed by the generator
118            //
119            // IMPORTANT: if you're building the platform and you get this error message,
120            // it means the renameClasses[] array in AsmGenerator needs to be updated: some
121            // class should have been renamed but it was not found in the input JAR files.
122            Set<String> notRenamed = agen.getClassesNotRenamed();
123            if (notRenamed.size() > 0) {
124                // (80-column guide below for error formatting)
125                // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
126                log.error(
127                  "ERROR when running layoutlib_create: the following classes are referenced\n" +
128                  "by tools/layoutlib/create but were not actually found in the input JAR files.\n" +
129                  "This may be due to some platform classes having been renamed.");
130                for (String fqcn : notRenamed) {
131                    log.error("- Class not found: %s", fqcn.replace('/', '.'));
132                }
133                for (String path : osJarPath) {
134                    log.info("- Input JAR : %1$s", path);
135                }
136                return 1;
137            }
138
139            return 0;
140        } catch (IOException e) {
141            log.exception(e, "Failed to load jar");
142        } catch (LogAbortException e) {
143            e.error(log);
144        }
145
146        return 1;
147    }
148
149    private static int listDeps(ArrayList<String> osJarPath, Log log) {
150        DependencyFinder df = new DependencyFinder(log);
151        try {
152            List<Map<String, Set<String>>> result = df.findDeps(osJarPath);
153            if (sOptions.listAllDeps) {
154                df.printAllDeps(result);
155            } else if (sOptions.listOnlyMissingDeps) {
156                df.printMissingDeps(result);
157            }
158        } catch (IOException e) {
159            log.exception(e, "Failed to load jar");
160        }
161
162        return 0;
163    }
164
165    /**
166     * Returns true if args where properly parsed.
167     * Returns false if program should exit with command-line usage.
168     * <p/>
169     * Note: the String[0] is an output parameter wrapped in an array, since there is no
170     * "out" parameter support.
171     */
172    private static boolean processArgs(Log log, String[] args,
173            ArrayList<String> osJarPath, String[] osDestJar) {
174        boolean needs_dest = true;
175        for (int i = 0; i < args.length; i++) {
176            String s = args[i];
177            if (s.equals("-v")) {
178                log.setVerbose(true);
179            } else if (s.equals("-p")) {
180                sOptions.generatePublicAccess = false;
181            } else if (s.equals("--list-deps")) {
182                sOptions.listAllDeps = true;
183                needs_dest = false;
184            } else if (s.equals("--missing-deps")) {
185                sOptions.listOnlyMissingDeps = true;
186                needs_dest = false;
187            } else if (!s.startsWith("-")) {
188                if (needs_dest && osDestJar[0] == null) {
189                    osDestJar[0] = s;
190                } else {
191                    osJarPath.add(s);
192                }
193            } else {
194                log.error("Unknow argument: %s", s);
195                return false;
196            }
197        }
198
199        if (osJarPath.isEmpty()) {
200            log.error("Missing parameter: path to input jar");
201            return false;
202        }
203        if (needs_dest && osDestJar[0] == null) {
204            log.error("Missing parameter: path to output jar");
205            return false;
206        }
207
208        sOptions.generatePublicAccess = false;
209
210        return true;
211    }
212}
213