Add MigrationTools

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1232 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
alfred 2006-08-10 09:27:16 +00:00
parent c8b59d238c
commit 0dc8c58932
13 changed files with 1367 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0
Main-Class: org.tianocore.migration.ModuleInfo

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" >
<MsaHeader>
<ModuleName>MigrationTools</ModuleName>
<ModuleType>TOOL</ModuleType>
<GuidValue>09C77FAE-6374-408e-AB88-32822B92096E</GuidValue>
<Version>1.0</Version>
<Abstract>This is the EFI/Tiano Migration Tool Resources Module</Abstract>
<Description>
This Module provdes the entity, action and exception package for Migration
tools, This package will be shared for building tools or wizard tools.
</Description>
<Copyright>Copyright 2005-2006, Intel Corporation</Copyright>
<License>
All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the
BSD License which accompanies this distribution. The full text of the
license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
</License>
<Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052</Specification>
</MsaHeader>
<ModuleDefinitions>
<SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>
<BinaryModule>false</BinaryModule>
<OutputFileBasename>NULL</OutputFileBasename>
</ModuleDefinitions>
<SourceFiles>
<Filename>build.xml</Filename>
<Filename>org/tianocore/migration/Database.java</Filename>
<Filename>org/tianocore/migration/FirstPanel.java</Filename>
<Filename>org/tianocore/migration/Func.java</Filename>
<Filename>org/tianocore/migration/Guid.java</Filename>
<Filename>org/tianocore/migration/Macro.java</Filename>
<Filename>org/tianocore/migration/ModuleInfo.java</Filename>
<Filename>org/tianocore/migration/ModuleReader.java</Filename>
<Filename>org/tianocore/migration/MsaWriter.java</Filename>
<Filename>org/tianocore/migration/SourceFileReplacer.java</Filename>
<Filename>org/tianocore/migration/UI.java</Filename>
</SourceFiles>
</ModuleSurfaceArea>

View File

@ -0,0 +1,52 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-->
<project name="MigrationTools" default="MigrationTools" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<property environment="env"/>
<property name="WORKSPACE" value="${env.WORKSPACE}"/>
<path id="classpath">
<fileset dir="${WORKSPACE}/Tools/Jars" includes="*.jar"/>
<fileset dir="${env.XMLBEANS_HOME}/lib" includes="*.jar"/>
</path>
<property name="buildDir" value="build"/>
<property name="installLocation" value="${WORKSPACE}/Tools/bin"/>
<target name="MigrationTools" depends="install"/>
<target name="source">
<mkdir dir="${buildDir}"/>
<javac srcdir="." destdir="${buildDir}">
<classpath refid="classpath"/>
<compilerarg value="-Xlint"/>
</javac>
</target>
<target name="clean">
<delete dir="${buildDir}"/>
</target>
<target name="cleanall">
<delete dir="${buildDir}"/>
<delete file="${installLocation}/MigrationTools.jar"/>
<if>
<available file="${installLocation}/MigrationTools.jar"/>
<then>
<echo message="You must manually remove the file: ${installLocation}/MigrationTools.jar"/>
<echo message="Java has already loaded the file, and cannot remove it within ANT!"/>
</then>
</if>
</target>
<target name="install" depends="source">
<jar destfile="${installLocation}/MigrationTools.jar"
basedir="${buildDir}"
includes="**"
manifest="MANIFEST.MF"
/>
</target>
</project>

View File

@ -0,0 +1,135 @@
package org.tianocore.migration;
import java.io.*;
import java.util.*;
public class Database {
Database() throws Exception {
if (System.getenv("WORKSPACE") == null) {
DatabasePath = "C:" + File.separator + "tianocore" + File.separator + "edk2" + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration";
} else {
DatabasePath = System.getenv("WORKSPACE") + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration";
}
importDBLib("Library.csv");
importDBGuid("Guid.csv", "Guid");
importDBGuid("Ppi.csv", "Ppi");
importDBGuid("Protocol.csv", "Protocol");
importDBMacro("Macro.csv");
}
public static String defaultpath = "C:" + File.separator + "tianocore" + File.separator + "edk2" + File.separator + "Tools" + File.separator + "Conf" + File.separator + "Migration";
public String DatabasePath;
public Set<String> error = new HashSet<String>();
private Map<String,Guid> hashguid = new HashMap<String,Guid>();
private Map<String,Func> hashfunc = new HashMap<String,Func>();
private Map<String,Macro> hashmacro = new HashMap<String,Macro>();
private void importDBLib(String filename) throws Exception {
BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
String line;
String[] linecontext;
Func lf;
if (rd.ready()) {
System.out.println("Found " + filename + " , Importing Library Database");
while ((line = rd.readLine()) != null) {
if (line.length() != 0) {
linecontext = line.split(",");
lf = new Func(linecontext);
hashfunc.put(lf.r8funcname,lf);
}
}
}
}
private void importDBGuid(String filename, String type) throws Exception {
BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
String line;
String[] linecontext;
Guid gu;
if (rd.ready()) {
System.out.println("Found " + filename + " , Importing " + type + " Database");
while ((line = rd.readLine()) != null) {
if (line.length() != 0) {
linecontext = line.split(",");
gu = new Guid(linecontext, type);
hashguid.put(gu.r8name,gu);
}
}
}
}
private void importDBMacro(String filename) throws Exception {
BufferedReader rd = new BufferedReader(new FileReader(DatabasePath + File.separator + filename));
String line;
String[] linecontext;
Macro mc;
if (rd.ready()) {
System.out.println("Found " + filename + " , Importing Macro Database");
while ((line = rd.readLine()) != null) {
if (line.length() != 0) {
linecontext = line.split(",");
mc = new Macro(linecontext);
hashmacro.put(mc.r8name,mc);
}
}
}
}
public String getR9Lib(String r8funcname) {
String temp = null;
if (hashfunc.containsKey(r8funcname)) {
temp = hashfunc.get(r8funcname).r9libname;
}
return temp;
}
public String getR9Func(String r8funcname) {
String temp = null;
if (hashfunc.containsKey(r8funcname)) {
temp = hashfunc.get(r8funcname).r9funcname;
}
return temp;
}
public boolean hasFunc(String r8lib) {
return hashfunc.containsKey(r8lib);
}
public boolean hasGuid(String r8guid) {
return hashguid.containsKey(r8guid);
}
public boolean hasMacro(String r8macro) {
return hashmacro.containsKey(r8macro);
}
public String getR9Macro(String r8macro) {
return hashmacro.get(r8macro).r9name; // the verification job of if the macro exists in the database is done when registering it
}
public String getR9Guidname(String r8Guid) {
String temp = null;
try {
temp = hashguid.get(r8Guid).r9name;
} catch (NullPointerException e) {
error.add("getR9Guidname :" + r8Guid);
}
return temp;
}
public String getGuidType(String r8Guid) {
String temp = null;
try {
temp = hashguid.get(r8Guid).type;
} catch (NullPointerException e) {
error.add("getR9Guidname :" + r8Guid);
}
return temp;
}
}

View File

@ -0,0 +1,139 @@
package org.tianocore.migration;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class FirstPanel extends JPanel implements ActionListener, UI {
/**
* Define class Serial Version UID
*/
private static final long serialVersionUID = 207759413522910399L;
private String modulepath;
private JButton moduleButton , goButton;
private JTextField moduletext;
private JTextArea log;
private JFileChooser fc;
private JCheckBox filebox, screenbox;
private boolean tofile = true, toscreen = true;
private PrintWriter logfile;
FirstPanel() throws Exception {
goButton = new JButton("Go");
goButton.addActionListener(this);
goButton.setActionCommand("go");
moduleButton = new JButton("Choose ModulePath");
moduleButton.addActionListener(this);
moduletext = new JTextField(30);
filebox = new JCheckBox("Output to logfile", true);
screenbox = new JCheckBox("Specify logfile", false);
JPanel modulePanel = new JPanel();
modulePanel.add(moduleButton);
modulePanel.add(moduletext);
modulePanel.add(filebox);
modulePanel.add(screenbox);
modulePanel.add(goButton);
add(modulePanel);
log = new JTextArea(50,25);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
add(logScrollPane);
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
//---------------------------------------------------------------------------------------//
public boolean yesOrNo(String question) {
return JOptionPane.showConfirmDialog(this, question, "Yes or No", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
}
public void print(String message) {
if (toscreen == true) {
log.append(message);
System.out.print(message);
}
if (tofile == true) {
logfile.append(message);
}
}
public void println(String message) {
print(message + "\n");
}
public void println(Set<String> hash) {
if (toscreen == true) {
log.append(hash + "\n");
System.out.println(hash);
}
if (tofile == true) {
logfile.append(hash + "\n");
}
}
//---------------------------------------------------------------------------------------//
/*
public boolean getOption(String item) {
if (item.matches("file")) {
}
}
*/
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == moduleButton ) {
int ret = fc.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
modulepath = fc.getSelectedFile().getAbsolutePath();
moduletext.setText(modulepath);
log.append("ModulePath: " + modulepath + "\n");
}
}
if ( e.getSource() == goButton ) {
try {
logfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "migration.log")));
println("Project MsaGen");
println("Copyright (c) 2006, Intel Corporation");
new ModuleInfo(modulepath, this, new Database());
logfile.flush();
} catch (Exception en) {
println(en.getMessage());
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("changed");
}
}
//---------------------------------------------------------------------------------------//
public static void init() throws Exception {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FirstPanel fp = new FirstPanel();
fp.setLayout(new BoxLayout(fp, BoxLayout.Y_AXIS));
fp.setOpaque(true);
frame.setContentPane(fp);
frame.setSize(800,600);
frame.setVisible(true);
}
}

View File

@ -0,0 +1,51 @@
package org.tianocore.migration;
import java.util.regex.*;
public class Func {
Func(String r8func,String r8lib,String r9func,String r9lib) {
r8funcname = r8func;
r8libname = r8lib;
r9funcname = r9func;
r9libname = r9lib;
}
Func(String[] linecontext) {
r8funcname = linecontext[1];
r8libname = linecontext[0];
r9funcname = linecontext[2];
r9libname = linecontext[3];
}
public String r8funcname;
public String r8libname;
public String r9funcname;
public String r9libname;
public static Pattern ptnbrace = Pattern.compile("\\{[^\\{\\}]*\\}",Pattern.MULTILINE);
public static Pattern ptnfuncc = Pattern.compile("([a-zA-Z_]\\w*)\\s*\\([^\\)\\(]*\\)",Pattern.MULTILINE);
public static Pattern ptnfuncd = Pattern.compile("([a-zA-Z_]\\w*)\\s*\\([^\\)\\(]*\\)\\s*@",Pattern.MULTILINE);
public static Pattern ptnlowcase = Pattern.compile("[a-z]"); // must be removed
private static String reservedwords = "if for pack while switch return sizeof";
public static String register(Matcher mtr, ModuleInfo mi, Database db) {
String temp = null;
temp = mtr.group(1); // both changed and not changed funcc are registered , for finding all the non-local function calls
Matcher mtrlowcase = ptnlowcase.matcher(temp); // must be removed , so the two funcs can be merged
if (!reservedwords.contains(temp) && mtrlowcase.find()) {
mi.hashfuncc.add(temp);
}
return temp;
}
/*
public static String registerFuncD(Matcher mtr, ModuleInfo mi, Database db) {
String temp = null;
temp = mtr.group(1); // both changed and not changed funcd are registered , for finding all the non-local function calls
if (!reservedwords.contains(temp)) {
mi.hashfuncd.add(temp);
}
return temp;
}
*/
}

View File

@ -0,0 +1,49 @@
package org.tianocore.migration;
import java.util.regex.*;
public class Guid {
Guid (String r8, String t, String n, String r9, String gv, String p) {
r8name = r8;
type = t;
name = n;
r9name = r9;
guidvalue = gv;
pack = p;
}
Guid (String[] linecontext, String t) {
r8name = linecontext[1];
type = t;
name = linecontext[0];
r9name = linecontext[2];
guidvalue = linecontext[3];
pack = linecontext[4];
}
public String r8name;
public String type;
public String name;
public String r9name;
public String guidvalue;
public String pack;
public static Pattern ptnguid = Pattern.compile("g\\w*Guid");
public static String register(Matcher mtr, ModuleInfo mi, Database db) {
String type = null;
String temp = null;
temp = mtr.group();
if (db.hasGuid(temp)) { // only changed guids registered, because both changed and not changed guids are included in database
type = db.getGuidType(temp);
if (type.matches("Protocol")) {
mi.protocol.add(temp);
} else if (type.matches("Ppi")) {
mi.ppi.add(temp);
} else if (type.matches("Guid")) {
mi.guid.add(temp);
}
return temp;
}
return null;
}
}

View File

@ -0,0 +1,34 @@
package org.tianocore.migration;
import java.util.regex.*;
public class Macro {
Macro(String r8, String r9) {
r8name = r8;
r9name = r9;
}
Macro(String[] linecontext) {
r8name = linecontext[0];
r9name = linecontext[1];
}
public String r8name;
public String r9name;
public static Pattern ptntmacro = Pattern.compile("\\b[A-Z_]+\\s*?\\(?\\b",Pattern.MULTILINE);
private static String unmacro = "VOID UINTN BOOLEAN ASSERT OPTIONAL STATIC NULL TRUE IN OUT FALSE";
public static String register(Matcher mtr, ModuleInfo mi, Database db) {
String temp = null;
temp = mtr.group();
if (db.hasMacro(temp)) { // only changed macros registered, because the database of macro has only changed ones
if (!unmacro.contains(temp)) {
mi.hashnonlocalmacro.add(temp);
}
return temp;
}
return null;
}
}

View File

@ -0,0 +1,280 @@
package org.tianocore.migration;
import java.io.*;
import java.util.*;
import java.util.regex.*;
/*
Class ModuleInfo is built for scanning the source files, it contains all the needed
information and all the temporary data.
*/
public class ModuleInfo {
ModuleInfo(String modulepath, UI ui, Database db) throws Exception {
this.modulepath = modulepath;
this.ui = ui;
this.db = db;
moduleScan();
}
private String modulepath = null;
private Database db = null;
private UI ui = null;
public String modulename = null;
public String guidvalue = null;
public String moduletype = null;
public String entrypoint = null;
public Set<String> localmodulesources = new HashSet<String>(); //contains both .c and .h
public Set<String> localmoduleheaders = new HashSet<String>();
public Set<String> preprocessedccodes = new HashSet<String>();
public Set<String> hashfuncc = new HashSet<String>();
public Set<String> hashfuncd = new HashSet<String>();
public Set<String> hashnonlocalfunc = new HashSet<String>();
public Set<String> hashnonlocalmacro = new HashSet<String>();
public Set<String> hashEFIcall = new HashSet<String>();
public Set<String> hashr8only = new HashSet<String>();
public Set<String> hashrequiredr9libs = new HashSet<String>(); // hashrequiredr9libs is now all added in SourceFileReplacer
public Set<String> guid = new HashSet<String>();
public Set<String> protocol = new HashSet<String>();
public Set<String> ppi = new HashSet<String>();
private static String migrationcomment = "//%$//";
private void moduleScan() throws Exception {
String[] list = new File(modulepath).list();
boolean hasInf = false;
String infname = null;
boolean hasMsa = false;
String msaname = null;
for (int i = 0 ; i < list.length ; i++) {
if (new File(list[i]).isDirectory()) {
;
} else {
if (list[i].contains(".c") || list[i].contains(".C")) {
localmodulesources.add(list[i]);
} else if (list[i].contains(".h") || list[i].contains(".H")) {
localmodulesources.add(list[i]);
localmoduleheaders.add(list[i]); //the case that several .inf or .msa found is not concerned
} else if (list[i].contains(".inf")) {
if (ui.yesOrNo("Found .inf file : " + list[i] + "\nUse this file as this module's .inf ?")) {
hasInf = true;
infname = list[i];
} else {
continue;
}
} else if (list[i].contains(".msa")) {
if (ui.yesOrNo("Found .msa file : " + list[i] + "\nUse this file as this module's .msa ?")) {
hasMsa = true;
msaname = list[i];
} else {
continue;
}
}
}
}
ModuleReader mr = new ModuleReader(modulepath, this, db);
if (hasInf) { // this sequence shows using .inf as default
mr.readInf(infname);
} else if (hasMsa) {
mr.readMsa(msaname);
} else {
ui.println("No Inf Nor Msa Found");
}
CommentOutNonLocalHFile();
parsePreProcessedSourceCode();
new SourceFileReplacer(modulepath, this, db, ui).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
// show result
if (ui.yesOrNo("Parse Module Information Complete . See details ?")) {
ui.println("\nModule Information : ");
ui.println("Entrypoint : " + entrypoint);
show(protocol, "Protocol : ");
show(ppi, "Ppi : ");
show(guid, "Guid : ");
show(hashfuncc, "call : ");
show(hashfuncd, "def : ");
show(hashEFIcall, "EFIcall : ");
show(hashnonlocalmacro, "macro : ");
show(hashnonlocalfunc, "nonlocal : ");
show(hashr8only, "hashr8only : ");
}
new MsaWriter(modulepath, this, db).flush();
// remove temp directory
//File tempdir = new File(modulepath + File.separator + "temp");
//System.out.println("Deleting Dir");
//if (tempdir.exists()) tempdir.d;
ui.println("Errors Left : " + db.error);
ui.println("Complete!");
ui.println("Your R9 module is placed at " + modulepath + File.separator + "result");
ui.println("Your logfile is placed at " + modulepath);
}
private void show(Set<String> hash, String show) {
ui.println(show + hash.size());
ui.println(hash);
}
// add '//' to all non-local include lines
private void CommentOutNonLocalHFile() throws IOException {
BufferedReader rd;
String line;
String curFile;
PrintWriter outfile;
Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");
Matcher mtcinclude;
File tempdir = new File(modulepath + File.separator + "temp" + File.separator);
if (!tempdir.exists()) tempdir.mkdir();
Iterator<String> ii = localmodulesources.iterator();
while ( ii.hasNext() ) {
curFile = ii.next();
rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));
outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));
while ((line = rd.readLine()) != null) {
if (line.contains("#include")) {
mtcinclude = ptninclude.matcher(line);
if (mtcinclude.find() && localmoduleheaders.contains(mtcinclude.group(1))) {
} else {
line = migrationcomment + line;
}
}
outfile.append(line + '\n');
}
outfile.flush();
outfile.close();
}
}
/*
private void search(String line, Pattern ptn, Method md) {
matmacro = Func.ptntmacro.matcher(line);
while (matmacro.find()) {
if ((temp = Func.registerMacro(matmacro, this, db)) != null) {
}
}
}
*/
private void parsePreProcessedSourceCode() throws Exception {
//Cl cl = new Cl(modulepath);
//cl.execute("Fat.c");
//cl.generateAll(preprocessedccodes);
//
//System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
//System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
//String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
String[] list = new File(modulepath).list();
for (int i = 0 ; i < list.length ; i++) {
if (list[i].contains(".c")) { // without CL , change to .i
preprocessedccodes.add(list[i]);
}
}
//
Iterator<String> ii = preprocessedccodes.iterator();
BufferedReader rd = null;
String ifile = null;
String line = null;
String temp = null;
//StringBuffer result = new StringBuffer();
Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);
Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);
Matcher matguid;
Matcher matfuncc;
Matcher matfuncd;
Matcher matenclosereplace;
Matcher matefifuncc;
Matcher matentrypoint;
Matcher matmacro;
while (ii.hasNext()) {
StringBuffer wholefile = new StringBuffer();
ifile = ii.next();
rd = new BufferedReader(new FileReader(modulepath + File.separator + "temp" + File.separator + ifile));
while ((line = rd.readLine()) != null) {
wholefile.append(line + '\n');
}
line = wholefile.toString();
// if this is a Pei phase module , add these library class to .msa
matentrypoint = patentrypoint.matcher(line);
if (matentrypoint.find()) {
entrypoint = matentrypoint.group(2);
if (matentrypoint.group(1).matches("PEIM")) {
hashrequiredr9libs.add("PeimEntryPoint");
} else {
hashrequiredr9libs.add("UefiDriverEntryPoint");
}
}
// find guid
matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :
while (matguid.find()) { // 1.currently , find once , then call to identify which is it
if ((temp = Guid.register(matguid, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each
//matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
}
}
//matguid.appendTail(result);
//line = result.toString();
// find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
// This item is not simply replaced , special operation is required.
matefifuncc = patefifuncc.matcher(line);
while (matefifuncc.find()) {
hashEFIcall.add(matefifuncc.group(2));
}
// find function call
matfuncc = Func.ptnfuncc.matcher(line);
while (matfuncc.find()) {
if ((temp = Func.register(matfuncc, this, db)) != null) {
//ui.println(ifile + " dofunc " + temp);
//matfuncc.appendReplacement(result, db.getR9Func(temp));
}
}
//matfuncc.appendTail(result);
//line = result.toString();
// find macro
matmacro = Macro.ptntmacro.matcher(line);
while (matmacro.find()) {
if ((temp = Macro.register(matmacro, this, db)) != null) {
}
}
// find function definition
// replace all {} to @
while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {
line = matenclosereplace.replaceAll("@");
}
matfuncd = Func.ptnfuncd.matcher(line);
while (matfuncd.find()) {
if ((temp = Func.register(matfuncd, this, db)) != null) {
}
}
}
// op on hash
Iterator<String> funcci = hashfuncc.iterator();
while (funcci.hasNext()) {
if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {
hashnonlocalfunc.add(temp); // this set contains both changed and not changed items
}
}
}
public static void main(String[] args) throws Exception {
FirstPanel.init();
}
}

View File

@ -0,0 +1,83 @@
package org.tianocore.migration;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.tianocore.*;
public class ModuleReader {
ModuleReader(String path, ModuleInfo moduleinfo, Database database) {
modulepath = path;
mi = moduleinfo;
db = database;
}
private String modulepath;
private ModuleInfo mi;
private Database db;
private static Pattern ptninfequation = Pattern.compile("([^ ]*) *= *([^ ]*)");
public void readMsa(String name) throws Exception {
ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(new File(modulepath + File.separator + name));
ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.getModuleSurfaceArea();
MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();
mi.modulename = msaheader.getModuleName();
mi.guidvalue = msaheader.getGuidValue();
mi.moduletype = msaheader.getModuleType().toString(); // ???
SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();
String temp;
Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList().iterator();
while (li.hasNext()) {
if (!mi.localmodulesources.contains(temp = li.next().toString())) {
System.out.println("Source File Missing ! : " + temp);
}
}
}
public void readInf(String name) throws Exception {
System.out.println("Reading From Inf : " + name);
BufferedReader rd = new BufferedReader(new FileReader(modulepath + File.separator + name));
String line;
String[] linecontext;
boolean inSrc = false;
Matcher mtrinfequation;
while ((line = rd.readLine()) != null) {
if (line.length() != 0) {
if (inSrc) {
if (line.contains("[")) {
inSrc = false;
} else {
linecontext = line.split(" ");
if (linecontext[2].length() != 0) {
if (!mi.localmodulesources.contains(linecontext[2])) {
System.out.println("Source File Missing ! : " + linecontext[2]);
}
}
}
} else {
if ((mtrinfequation = ptninfequation.matcher(line)).find()) {
if (mtrinfequation.group(1).matches("BASE_NAME")) {
mi.modulename = mtrinfequation.group(2);
}
if (mtrinfequation.group(1).matches("FILE_GUID")) {
mi.guidvalue = mtrinfequation.group(2);
}
if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {
mi.moduletype = mtrinfequation.group(2);
}
if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {
mi.entrypoint = mtrinfequation.group(2);
}
}
if (line.contains("sources")) {
inSrc = true;
}
}
}
}
}
}

View File

@ -0,0 +1,145 @@
package org.tianocore.migration;
import java.io.*;
import java.util.*;
import org.tianocore.*;
import org.tianocore.SupportedArchitectures.Enum;
import org.apache.xmlbeans.*;
public class MsaWriter {
MsaWriter(String path, ModuleInfo moduleinfo, Database database) {
modulepath = path;
mi = moduleinfo;
db = database;
}
private String modulepath;
private ModuleInfo mi;
private Database db;
private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance();
private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.addNewModuleSurfaceArea();
private MsaHeaderDocument.MsaHeader msaheader = msa.addNewMsaHeader();
private ModuleDefinitionsDocument.ModuleDefinitions md = msa.addNewModuleDefinitions();
private SourceFilesDocument.SourceFiles sourcefiles = msa.addNewSourceFiles(); //found local .h files are not written
private GuidsDocument.Guids guids = msa.addNewGuids();
private ProtocolsDocument.Protocols protocols = msa.addNewProtocols();
private PPIsDocument.PPIs ppis = msa.addNewPPIs();
private PackageDependenciesDocument.PackageDependencies pd = msa.addNewPackageDependencies();
private LibraryClassDefinitionsDocument.LibraryClassDefinitions libclassdefs = msa.addNewLibraryClassDefinitions();
private ExternsDocument.Externs externs = msa.addNewExterns();
private String Query (String requirement) throws Exception {
String answer;
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
System.out.println(requirement);
while ((answer = rd.readLine()).length() == 0) ;
return answer;
}
private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {
Iterator<String> it;
String temp;
if (mi.modulename != null) {
msaheader.setModuleName(mi.modulename);
} else {
msaheader.setModuleName(mi.modulename = Query("ModuleName Not Found . Please Input ModuleName"));
}
if (mi.guidvalue != null) {
msaheader.setGuidValue(mi.guidvalue);
} else {
msaheader.setGuidValue(mi.guidvalue = Query("GuidValue Not Found . Please Input GuidValue"));
}
if (mi.moduletype != null) {
if (mi.moduletype.contains("PEI")) {
msaheader.setModuleType(ModuleTypeDef.Enum.forString("PEIM"));
} else {
msaheader.setModuleType(ModuleTypeDef.Enum.forString("DXE_DRIVER"));
}
} else {
msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.moduletype = Query("GuidValue Not Found . Please Input GuidValue")));
}
msaheader.setCopyright("Copyright (c) 2006, Intel Corporation");
msaheader.setVersion("1.0");
msaheader.setAbstract("Component name for module " + mi.modulename);
msaheader.setDescription("FIX ME!"); //???
msaheader.addNewLicense().setStringValue("All rights reserved.\n" +
" This software and associated documentation (if any) is furnished\n" +
" under a license and may only be used or copied in accordance\n" +
" with the terms of the license. Except as permitted by such\n" +
" license, no part of this software or documentation may be\n" +
" reproduced, stored in a retrieval system, or transmitted in any\n" +
" form or by any means without the express written consent of\n" +
" Intel Corporation.");
List<Enum> arch = new ArrayList<Enum>();
arch.add(SupportedArchitectures.IA_32);
arch.add(SupportedArchitectures.X_64);
arch.add(SupportedArchitectures.IPF);
arch.add(SupportedArchitectures.EBC);
md.setSupportedArchitectures(arch);
md.setBinaryModule(false);
md.setOutputFileBasename(mi.modulename);
pd.addNewPackage().setPackageGuid("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");
externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");
externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);
it = mi.localmodulesources.iterator();
while (it.hasNext()) {
sourcefiles.addNewFilename().setStringValue(it.next());
}
it = mi.protocol.iterator();
while (it.hasNext()) {
if ((temp = it.next()) != null) {
ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();
pr.setProtocolCName(temp);
pr.setUsage(UsageTypes.ALWAYS_CONSUMED);
}
}
it = mi.ppi.iterator();
while (it.hasNext()) {
if ((temp = it.next()) != null) {
PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();
pp.setPpiCName(temp);
pp.setUsage(UsageTypes.ALWAYS_CONSUMED);
}
}
it = mi.guid.iterator();
while (it.hasNext()) {
if ((temp = it.next()) != null) {
GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();
gcn.setGuidCName(temp);
gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);
}
}
it = mi.hashrequiredr9libs.iterator();
while (it.hasNext()) {
if ((temp = it.next()) != null && !temp.matches("%")) {
LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();
lc.setKeyword(temp);
lc.setUsage(UsageTypes.ALWAYS_CONSUMED);
}
}
return msadoc;
}
public void flush() throws Exception {
XmlOptions options = new XmlOptions();
options.setCharacterEncoding("UTF-8");
options.setSavePrettyPrint();
options.setSavePrettyPrintIndent(2);
options.setUseDefaultNamespace();
BufferedWriter bw = new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + mi.modulename + ".msa"));
fulfillMsadoc().save(bw, options);
bw.flush();
bw.close();
}
}

View File

@ -0,0 +1,338 @@
package org.tianocore.migration;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SourceFileReplacer {
SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {
modulepath = path;
mi = moduleinfo;
db = database;
ui = fp;
}
private String modulepath;
private ModuleInfo mi;
private Database db;
private UI ui;
private boolean showdetails = false;
private class r8tor9 {
r8tor9(String r8, String r9) {
r8thing = r8;
r9thing = r9;
}
public String r8thing;
public String r9thing;
}
// these sets are used only for printing log of the changes in current file
private Set<r8tor9> filefunc = new HashSet<r8tor9>();
private Set<r8tor9> filemacro = new HashSet<r8tor9>();
private Set<r8tor9> fileguid = new HashSet<r8tor9>();
private Set<r8tor9> fileppi = new HashSet<r8tor9>();
private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();
private Set<String> filer8only = new HashSet<String>();
private String r8only = "EfiLibInstallDriverBinding " +
"EfiLibInstallAllDriverProtocols " +
"EfiLibCompareLanguage " +
"BufToHexString " +
"EfiStrTrim " +
"EfiValueToHexStr " +
"HexStringToBuf " +
"IsHexDigit " +
"NibbleToHexChar " +
"GetHob " +
"GetHobListSize " +
"GetHobVersion " +
"GetHobBootMode " +
"GetCpuHobInfo " +
"GetDxeCoreHobInfo " +
"GetNextFirmwareVolumeHob " +
"GetNextGuidHob " +
"GetPalEntryHobInfo " +
"GetIoPortSpaceAddressHobInfo ";
public void flush() throws Exception {
PrintWriter outfile;
String temp = null;
if (ui.yesOrNo("Change Source Code is to be doing . See details ?")) {
showdetails = true;
}
File tempdir = new File(modulepath + File.separator + "result" + File.separator);
if (!tempdir.exists()) tempdir.mkdir();
String[] list = new File(modulepath + File.separator + "temp").list(); //what I change is the non-local .h commented-out files
for (int i = 0 ; i < list.length ; i++) {
if (list[i].contains(".c")) {
ui.println("\nModifying file : " + list[i]);
outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + list[i])));
outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + list[i]));
outfile.flush();
outfile.close();
} else {
if (list[i].contains(".h")) {
temp = list[i];
} else if (list[i].contains(".C")) {
temp = list[i].replaceFirst(".C", ".c");
} else if (list[i].contains(".H")) {
temp = list[i].replaceFirst(".H", ".h");
} else {
continue;
}
ui.println("\nCopying file : " + temp);
outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + temp)));
outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + list[i]));
outfile.flush();
outfile.close();
}
}
if (!mi.hashr8only.isEmpty()) {
addr8only();
}
}
private void addr8only() throws Exception {
String paragraph = null;
String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");
PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));
PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));
//outfile1.append("#include \"R8Lib.h\"\n\n");
//outfile2.append("#include \"R8Lib.h\"\n\n");
Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);
Matcher mtrr8only = ptnr8only.matcher(line);
Matcher mtrr8onlyhead;
while (mtrr8only.find()) {
if (mi.hashr8only.contains(mtrr8only.group(2))) {
paragraph = mtrr8only.group();
outfile1.append(paragraph + "\n\n");
if (mtrr8only.group(1).length() != 0) {
mi.hashrequiredr9libs.add(mtrr8only.group(1));
}
//generate R8lib.h
while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {
paragraph = mtrr8onlyhead.replaceAll(";");
}
outfile2.append(paragraph + "\n\n");
}
}
outfile1.flush();
outfile1.close();
outfile2.flush();
outfile2.close();
}
private String sourcefiletostring(String filename) throws Exception {
BufferedReader rd = new BufferedReader(new FileReader(filename));
StringBuffer wholefile = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
wholefile.append(line + "\n");
}
return wholefile.toString();
}
// Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!
private String sourcefilereplace(String filename) throws Exception {
BufferedReader rd = new BufferedReader(new FileReader(filename));
StringBuffer wholefile = new StringBuffer();
String line;
String r8thing;
String r9thing;
r8tor9 temp;
boolean addr8 = false;
Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !
//Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
while ((line = rd.readLine()) != null) {
wholefile.append(line + "\n");
}
line = wholefile.toString();
// replace BS -> gBS , RT -> gRT
Matcher mat = pat.matcher(line);
if (mat.find()) { // add a library here
ui.println("Converting all BS->gBS,RT->gRT");
line = mat.replaceAll("g$1$2$3"); //unknown correctiveness
}
mat.reset();
while (mat.find()) {
if (mat.group(1).matches("BS")) {
mi.hashrequiredr9libs.add("UefiBootServicesTableLib");
}
if (mat.group(1).matches("RT")) {
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");
}
}
/*
// remove EFI_DRIVER_ENTRY_POINT
Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");
Matcher matentrypoint = patentrypoint.matcher(line);
if (matentrypoint.find()) {
ui.println("Deleting Entry_Point");
line = matentrypoint.replaceAll("");
}
*/
// start replacing names
Iterator<String> it;
// Converting non-locla function
it = mi.hashnonlocalfunc.iterator();
while (it.hasNext()) {
r8thing = it.next();
if (r8thing.matches("EfiInitializeDriverLib")) { //s
mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e
} else if (r8thing.matches("DxeInitializeDriverLib")) { //c
mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i
mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a
mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l
} else { //
mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here
}
if ((r9thing = db.getR9Func(r8thing)) != null) {
if (!r8thing.equals(r9thing)) {
if (line.contains(r8thing)) {
line = line.replaceAll(r8thing, r9thing);
filefunc.add(new r8tor9(r8thing, r9thing));
Iterator<r8tor9> rt = filefunc.iterator();
while (rt.hasNext()) {
temp = rt.next();
if (r8only.contains(temp.r8thing)) {
mi.localmodulesources.add("R8Lib.h");
mi.localmodulesources.add("R8Lib.c");
mi.localmoduleheaders.add("R8Lib.h");
filer8only.add(r8thing);
mi.hashr8only.add(r8thing);
addr8 = true;
}
}
}
}
}
} //is any of the guids changed?
if (addr8 == true) {
line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");
}
// Converting macro
it = mi.hashnonlocalmacro.iterator();
while (it.hasNext()) { //macros are all assumed MdePkg currently
r8thing = it.next();
//mi.hashrequiredr9libs.add(db.getR9Lib(r8thing));
if ((r9thing = db.getR9Macro(r8thing)) != null) {
if (line.contains(r8thing)) {
line = line.replaceAll(r8thing, r9thing);
filemacro.add(new r8tor9(r8thing, r9thing));
}
}
}
// Converting guid
replaceGuid(line, mi.guid, "guid", fileguid);
replaceGuid(line, mi.ppi, "ppi", fileppi);
replaceGuid(line, mi.protocol, "protocol", fileprotocol);
// Converting Pei
// First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);
if (mi.moduletype.contains("PEIM")) {
Matcher mtrpei = ptnpei.matcher(line);
while (mtrpei.find()) { // ! add a library here !
line = mtrpei.replaceAll("PeiServices$1#%$2");
mi.hashrequiredr9libs.add("PeiServicesLib");
}
mtrpei.reset();
if (line.contains("PeiServicesCopyMem")) {
line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");
mi.hashrequiredr9libs.add("BaseMemoryLib");
}
if (line.contains("PeiServicesSetMem")) {
line = line.replaceAll("PeiServicesSetMem#%", "SetMem");
mi.hashrequiredr9libs.add("BaseMemoryLib");
}
// Second , find all #% to drop the arg "PeiServices"
Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);
Matcher mtrpeiarg = ptnpeiarg.matcher(line);
while (mtrpeiarg.find()) {
line = mtrpeiarg.replaceAll("$1");
}
}
Matcher mtrmac;
mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);
if (mtrmac.find()) {
line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
}
mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);
if (mtrmac.find()) {
line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
}
mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);
if (mtrmac.find()) {
line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
}
mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);
if (mtrmac.find()) {
line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
}
if (line.contains("EFI_UINTN_ALIGN_MASK")) {
line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
}
show(filefunc, "function");
show(filemacro, "macro");
show(fileguid, "guid");
show(fileppi, "ppi");
show(fileprotocol, "protocol");
if (!filer8only.isEmpty()) {
ui.println("Converting r8only : " + filer8only);
}
filefunc.clear();
filemacro.clear();
fileguid.clear();
fileppi.clear();
fileprotocol.clear();
filer8only.clear();
return line;
}
private void show(Set<r8tor9> hash, String sh) {
Iterator<r8tor9> it = hash.iterator();
r8tor9 temp;
if (showdetails) {
if (!hash.isEmpty()) {
ui.print("Converting " + sh + " : ");
while (it.hasNext()) {
temp = it.next();
ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");
}
ui.println("");
}
}
}
private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {
Iterator<String> it;
String r8thing;
String r9thing;
it = hash.iterator();
while (it.hasNext()) {
r8thing = it.next();
if ((r9thing = db.getR9Guidname(r8thing)) != null) {
if (!r8thing.equals(r9thing)) {
if (line.contains(r8thing)) {
line = line.replaceAll(r8thing, r9thing);
filehash.add(new r8tor9(r8thing, r9thing));
}
}
}
}
}
}

View File

@ -0,0 +1,14 @@
package org.tianocore.migration;
import java.util.*;
public interface UI {
public boolean yesOrNo(String question);
public void print(String message);
public void println(String message);
public void println(Set<String> hash);
}