Added instruction on how to remove WARNINGS with -v flag set
[mirror_edk2.git] / Tools / Java / Source / CheckTools / src / org / tianocore / CheckTools / CheckTools.java
1 /** @file
2 Verify the tool configuration file for location of the correct tools.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 /**
16 * This tool checks to see if specified tool chain paths exist.
17 * It will check all specified paths, as indicated by the last field of the
18 * tool property line equal to _PATH
19 *
20 * no option = check 1) the file specified in target.txt or 2) tools_def.txt
21 * if neither is found, we check the tools_def.template file.
22 * -i = INTERACTIVE checks the "active" tools_def.txt file and lets the
23 * user modify invalid entries.
24 *
25 * -s = SCAN will check all standard locations for tool chains
26 * C:\Program Files\Microsoft Visual Studio *
27 * C:\WINDDK
28 * C:\Bin
29 * C:\ASL
30 * C:\MASM*
31 * /opt/tiano
32 *
33 * -f = FILE check the tools in this file instead of tools_def.txt, or
34 * a file that was specified in target.txt
35 *
36 * -t = TEST can be used with -f or -s, not with -i.
37 *
38 */
39 package org.tianocore.CheckTools;
40
41 import java.io.*;
42
43 public class CheckTools {
44 private static int DEBUG = 0;
45
46 private static final String copyright = "Copyright (c) 2006, Intel Corporation All rights reserved.";
47
48 private static final String version = "Version 0.1";
49
50 private int VERBOSE = 0;
51
52 // private String argv[];
53
54 private final int DEFAULT = 1;
55
56 private final int TEST = 2;
57
58 private final int SCAN = 4;
59
60 private final int INTERACTIVE = 8;
61
62 private boolean USERFILE = false;
63
64 private String inFile = "";
65
66 private final int PASS = 0;
67
68 private final int FAIL = 1;
69
70 public static void main(String[] argv) {
71 int exitCode = new CheckTools().checkTool(argv);
72 if (exitCode == -1) {
73 new CheckTools().usage();
74 System.exit(1);
75 }
76 System.exit(exitCode);
77 }
78
79 private int checkTool(String[] arguments) {
80 String WORKSPACE = System.getenv("WORKSPACE");
81 if ((DEBUG > 0) || (VERBOSE > 0))
82 System.out.println("Verifying Tool Chains for WORKSPACE: " + WORKSPACE);
83 String SEP = System.getProperty("file.separator");
84 int returnCode = 0;
85
86 if (WORKSPACE == null) {
87 System.out.println("Please set the environment variable, WORKSPACE and run again.");
88 System.exit(1);
89 }
90 String targetTxt = WORKSPACE + SEP + "Tools" + SEP + "Conf" + SEP + "target.txt";
91
92 if ((DEBUG > 1) && (arguments.length > 0))
93 System.out.println("Arguments: ");
94 int cmdCode = DEFAULT;
95 if (arguments.length > 0) {
96 cmdCode = DEFAULT;
97 for (int i = 0; i < arguments.length; i++) {
98 String arg = arguments[i];
99 if (DEBUG > 1)
100 System.out.println(" [" + i + "] " + arg);
101 if (!(arg.toLowerCase().startsWith("-t") || arg.toLowerCase().startsWith("-s")
102 || arg.toLowerCase().startsWith("-i") || arg.toLowerCase().startsWith("-v") || arg
103 .toLowerCase()
104 .startsWith(
105 "-f"))) {
106 // Only allow valid option flags
107 System.out.println("Invalid argument: " + arg);
108 usage();
109 System.exit(FAIL);
110 }
111 if (arg.toLowerCase().startsWith("-t")) {
112 if (cmdCode == DEFAULT) {
113 cmdCode = TEST;
114 } else {
115 System.out.println("Invalid Options");
116 usage();
117 System.exit(FAIL);
118 }
119 }
120 if (arg.toLowerCase().startsWith("-s")) {
121 if (cmdCode == DEFAULT) {
122 cmdCode = SCAN;
123 } else {
124 System.out.println("Invalid Options");
125 usage();
126 System.exit(FAIL);
127 }
128 }
129 if (arg.toLowerCase().startsWith("-i")) {
130 // Interactive can be specified with any
131 // other option - it turns on the query
132 // on fail mode.
133 cmdCode = cmdCode | INTERACTIVE;
134 }
135 if (arg.toLowerCase().startsWith("-f")) {
136 i++;
137 inFile = arguments[i];
138 USERFILE = true;
139 }
140 if (arg.startsWith("-v")) {
141 // Verbose level can be increased to print
142 // more INFO messages.
143 VERBOSE += 1;
144 }
145 if (arg.startsWith("-V")) {
146 System.out.println(copyright);
147 System.out.println("CheckTools, " + version);
148 System.exit(PASS);
149 }
150 }
151 }
152
153 if (inFile.length() < 1) {
154 //
155 // Check the target.txt file for a Tool Configuration File.
156 // If not set, we use tools_def.txt, unless we are running with the
157 // INTERACTIVE flag - where we check the template file before copying over to the
158 // tools_def.txt file.
159 //
160 inFile = "tools_def.txt";
161 File target = new File(targetTxt);
162 String readLine = null;
163 String fileLine[] = new String[2];
164 if (target.exists()) {
165 try {
166 FileReader fileReader = new FileReader(targetTxt);
167 BufferedReader bufReader = new BufferedReader(fileReader);
168 while ((readLine = bufReader.readLine()) != null) {
169 if (readLine.startsWith("TOOL_CHAIN_CONF")) {
170 fileLine = readLine.trim().split("=");
171 if (fileLine[1].trim().length() > 0) {
172 if (fileLine[1].trim().contains("Tools/Conf/"))
173 inFile = fileLine[1].replace("Tools/Conf/", "").trim();
174 else
175 inFile = fileLine[1].trim();
176 }
177 }
178 }
179 bufReader.close();
180 } catch (IOException e) {
181 System.out.println(" [target.txt] Read Error: " + e);
182 System.exit(FAIL);
183 }
184 }
185 }
186
187 // OK, now check the infile of we had one.
188 String toolsDef = WORKSPACE + SEP + "Tools" + SEP + "Conf" + SEP + inFile;
189 File toolsFile = new File(toolsDef);
190 if (!toolsFile.exists()) {
191 // use the template file
192 if (USERFILE) {
193 System.out.println("Could not locate the specified file: " + inFile);
194 System.out.println(" It must be located in the WORKSPACE" + SEP + "Tools" + SEP + "Conf directory");
195 System.exit(FAIL);
196 }
197 toolsDef = WORKSPACE + SEP + "Tools" + SEP + "Conf" + SEP + "tools_def.template";
198 File toolsTemplate = new File(toolsDef);
199 if (!toolsTemplate.exists()) {
200 System.out.println("Your WORKSPACE is not properly configured!");
201 System.exit(FAIL);
202 } else {
203 System.out.println("**** WARNING: No Tool Configuration File was found, using the template file, "
204 + toolsDef);
205 }
206 }
207
208 //
209 // at this point the file, toolsDef points to a tool configuration file of some sort.
210 //
211 // check tool configuration file
212 if (DEBUG > 2)
213 System.out.println("Calling checkTools(" + toolsDef + ", " + cmdCode + ", " + VERBOSE + ")");
214 returnCode = new ToolChecks().checkTools(toolsDef, cmdCode, VERBOSE);
215
216 return returnCode;
217 }
218
219 private void usage() {
220 System.out.println("Usage: checkTools [-i] [-s | -scan] [-t | -test] [[-f | -filename] filename.txt]");
221 }
222 }