xref: /aosp_15_r20/frameworks/base/ravenwood/tools/hoststubgen/src/com/android/hoststubgen/HostStubGenMain.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2023 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 @file:JvmName("HostStubGenMain")
17 
18 package com.android.hoststubgen
19 
20 import java.io.PrintWriter
21 
22 /**
23  * Entry point.
24  */
mainnull25 fun main(args: Array<String>) {
26     executableName = "HostStubGen"
27     runMainWithBoilerplate {
28         // Parse the command line arguments.
29         var clanupOnError = false
30         try {
31             val options = HostStubGenOptions.parseArgs(args)
32             clanupOnError = options.cleanUpOnError.get
33 
34             log.v("$executableName started")
35             log.v("Options: $options")
36 
37             // Run.
38             HostStubGen(options).run()
39         } catch (e: Throwable) {
40             if (clanupOnError) {
41                 TODO("Remove output jars here")
42             }
43             throw e
44         }
45     }
46 }
47 
runMainWithBoilerplatenull48 inline fun runMainWithBoilerplate(realMain: () -> Unit) {
49     var success = false
50 
51     try {
52         realMain()
53 
54         success = true
55     } catch (e: Throwable) {
56         log.e("$executableName: Error: ${e.message}")
57         if (e !is UserErrorException) {
58             e.printStackTrace(PrintWriter(log.getWriter(LogLevel.Error)))
59         }
60     } finally {
61         log.i("$executableName finished")
62         log.flush()
63     }
64 
65     System.exit(if (success) 0 else 1 )
66 }
67