Path: blob/master/debugtools/DDR_Autoblob/src/com/ibm/j9ddr/autoblob/GenerateSpecSuperset.java
6007 views
/*******************************************************************************1* Copyright (c) 1991, 2014 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9ddr.autoblob;2223import java.io.File;24import java.io.FileFilter;25import java.io.FileReader;26import java.io.FileWriter;27import java.util.HashMap;2829/**30* Utility class to generate the Java code needed for DDR - this consists of 2 steps31*32* 1. Merge the component supersets into a single c-build superset33* 2. Generate a new c-build superset which is the combined superset for the last i-build and the current c-build34*35* @author adam36*37*/38public class GenerateSpecSuperset {39private static final HashMap<String, String> opts = new HashMap<String, String>();40private static final String OPT_SRCDIR = "-s"; //where to find the component supersets41private static final String OPT_OUTPUTDIR = "-d"; //where to generate the output42private static final String OPT_FILENAME = "-f"; //superset file name43private static File outdir = null;4445static {46opts.put(OPT_OUTPUTDIR, null);47opts.put(OPT_SRCDIR, null);48opts.put(OPT_FILENAME, "superset.spec.dat"); //default value49}5051/**52* @param args53*/54public static void main(String[] args) throws Exception {55if(!parseArgs(args)) {56System.exit(1); //abort if invalid args are encountered57}58createOutputDir();59createSpecSuperset();60System.out.println("Finished");61}6263private static void createSpecSuperset() throws Exception {64System.out.println("Creating superset");65String[] parts = opts.get(OPT_SRCDIR).split(",");66File out = new File(outdir, opts.get(OPT_FILENAME));67FileWriter writer = new FileWriter(out);6869for(int i = 0; i < parts.length; i++) {70System.out.println("Scanning source directory for *.superset : " + parts[i]);71File srcdir = new File(parts[i]);72if(!srcdir.exists() || !srcdir.isDirectory()) {73System.err.println("Error : The specified source directory (" + srcdir.getAbsolutePath() + ") does not exist, or is not a directory.");74System.exit(1);75}76File[] files = srcdir.listFiles(new FileFilter() {7778public boolean accept(File pathname) {79return pathname.getName().endsWith(".superset");80}81});82if(0 == files.length) {83System.err.println("Error : The source directory (" + srcdir.getAbsolutePath() + ") does not contain any superset files (*.superset).");84System.exit(1);85}86char[] buffer = new char[4096];87int charsread = 0;88for(File file : files) {89System.out.println(("Processing " + file.getName()));90FileReader reader = new FileReader(file);91while((charsread = reader.read(buffer)) != -1) {92writer.write(buffer, 0, charsread);93}94reader.close();95}96}97writer.close();98}99100//creates the output directory for the superset and java code101private static void createOutputDir() {102outdir = new File(opts.get(OPT_OUTPUTDIR));103if(outdir.exists()) {104if(outdir.isDirectory()) {105System.out.println("Cleaning existing output directory " + outdir.getAbsolutePath());106//directory exists so clean it out107for(File file : outdir.listFiles()) {108file.delete();109}110} else {111//can't create as there is a conflict112System.err.println("Error : The specified output directory (" + outdir.getAbsolutePath() + ") already exists as a file.");113System.exit(1);114}115} else {116//dir does not exist so create it117System.out.println("Creating output directory " + outdir.getAbsolutePath());118if(!outdir.mkdirs()) {119System.err.println("Error : The could not create the output directory (" + outdir.getAbsolutePath() + ").");120System.exit(1);121}122}123}124125private static boolean parseArgs(String[] args) {126boolean validargs = true;127//parse args128for(int i = 0; (i + 1) < args.length; i+=2) {129if(opts.containsKey(args[i])) {130opts.put(args[i], args[i+1]);131} else {132System.err.println("Option " + args[i] + " was not recognised");133}134}135//check none are missing136for(String key : opts.keySet()) {137if(null == opts.get(key)) {138validargs = false;139System.err.println("Required option " + key + " was not set on the command line");140}141}142return validargs;143}144145}146147148