Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3f5268d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes Icd25d2d0,I39ef10b0,I06bb80fe

* changes:
  Rearrange the methods on ErrorReporter to be more convenient.
  Add glue to run product-config-test as a standalone commandline executable.
  CommandException to cleanly exit product-config on error.
parents 2adfd28c 0c7e0c0e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ java_test_host {
    static_libs: [
        "junit"
    ],
    manifest: "TEST_MANIFEST.MF",
    test_suites: ["general-tests"]
}
+2 −0
Original line number Diff line number Diff line
Manifest-Version: 1.0
Main-Class: com.android.build.config.TestRunner
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.config;

/**
 * Exception to indicate that a fatal error has occurred.  Throwing this
 * will cause errors to be printed, cleanup to occur, and the command to
 * exit with a failure code.
 *
 * These are user errors. Throwing other exceptions will result in
 * the stack trace being shown.
 */
public class CommandException extends RuntimeException {
    public CommandException() {
        super();
    }

    public CommandException(String message) {
        super(message);
    }

    public CommandException(String message, Throwable chain) {
        super(message, chain);
    }
}
+54 −14
Original line number Diff line number Diff line
@@ -49,6 +49,16 @@ public class ErrorReporter {
     */
    private boolean mHadError;

    public static class FatalException extends RuntimeException {
        FatalException(String message) {
            super(message);
        }

        FatalException(String message, Throwable chain) {
            super(message, chain);
        }
    }

    /**
     * Whether errors are errors, warnings or hidden.
     */
@@ -127,6 +137,35 @@ public class ErrorReporter {
        public String getHelp() {
            return mHelp;
        }

        /**
         * Add an error with no source position.
         */
        public void add(String message) {
            ErrorReporter.this.add(this, false, new Position(), message);
        }

        /**
         * Add an error.
         */
        public void add(Position pos, String message) {
            ErrorReporter.this.add(this, false, pos, message);
        }

        /**
         * Add an error with no source position, and throw a FatalException, stopping processing
         * immediately.
         */
        public void fatal(String message) {
            ErrorReporter.this.add(this, true, new Position(), message);
        }

        /**
         * Add an error, and throw a FatalException, stopping processing immediately.
         */
        public void fatal(Position pos, String message) {
            ErrorReporter.this.add(this, true, pos, message);
        }
    }

    /**
@@ -154,6 +193,13 @@ public class ErrorReporter {
        public String getMessage() {
            return mMessage;
        }

        @Override
        public String toString() {
            return mPosition
                    + "[" + mCategory.getLevel().getLabel() + " " + mCategory.getCode() + "] "
                    + mMessage;
        }
    }

    private void initLocked() {
@@ -190,23 +236,17 @@ public class ErrorReporter {
        }
    }

    /**
     * Add an error with no source position.
     */
    public void add(Category category, String message) {
        add(category, new Position(), message);
    }

    /**
     * Add an error.
     */
    public void add(Category category, Position pos, String message) {
    private void add(Category category, boolean fatal, Position pos, String message) {
        synchronized (mEntries) {
            initLocked();
            if (mCategories.get(category.getCode()) != category) {
                throw new RuntimeException("Errors.Category used from the wrong Errors object.");
            }
            mEntries.add(new Entry(category, pos, message));
            final Entry entry = new Entry(category, pos, message);
            mEntries.add(entry);
            final Level level = category.getLevel();
            if (level == Level.WARNING || level == Level.ERROR) {
                mHadWarningOrError = true;
@@ -214,6 +254,9 @@ public class ErrorReporter {
            if (level == Level.ERROR) {
                mHadError = true;
            }
            if (fatal) {
                throw new FatalException(entry.toString());
            }
        }
    }

@@ -250,13 +293,10 @@ public class ErrorReporter {
    public void printErrors(PrintStream out) {
        synchronized (mEntries) {
            for (Entry entry: mEntries) {
                final Category category = entry.getCategory();
                final Level level = category.getLevel();
                if (level == Level.HIDDEN) {
                if (entry.getCategory().getLevel() == Level.HIDDEN) {
                    continue;
                }
                out.println(entry.getPosition() + "[" + level.getLabel() + " "
                        + category.getCode() + "] " + entry.getMessage());
                out.println(entry.toString());
            }
        }
    }
+32 −14
Original line number Diff line number Diff line
@@ -38,27 +38,45 @@ public class Main {

        // TODO: Get the variables that were defined in starlark and use that to write
        // out the make, soong and bazel input files.
        mErrors.ERROR_COMMAND_LINE.add("asdf");
        throw new RuntimeException("poop");
    }

    public static void main(String[] args) {
        Errors errors = new Errors();
        int exitCode = 0;

        try {
            Options options = Options.parse(errors, args);
            if (errors.hadError()) {
                Options.printHelp(System.err);
                System.err.println();
            errors.printErrors(System.err);
            System.exit(1);
                throw new CommandException();
            }

            switch (options.getAction()) {
                case DEFAULT:
                    (new Main(errors, options)).run();
                errors.printErrors(System.err);
                    return;
                case HELP:
                    Options.printHelp(System.out);
                    return;
            }
        } catch (CommandException ex) {
            // These are user errors, so don't show a stack trace
            exitCode = 1;
        } catch (Throwable ex) {
            // These are programming errors in the code of this tool, so print the exception.
            // We'll try to print this.  If it's something unrecoverable, then we'll hope
            // for the best. We will still print the errors below, because they can be useful
            // for debugging.
            ex.printStackTrace(System.err);
            System.err.println();
            exitCode = 1;
        } finally {
            // Print errors and warnings
            errors.printErrors(System.err);
        }
        System.exit(exitCode);
    }
}
Loading