1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-10 03:53:21 +02:00

Implements the interface of IMakefile

This commit is contained in:
Alain Magloire 2003-09-07 19:42:53 +00:00
parent 3e86e80afd
commit 9147013836
17 changed files with 557 additions and 273 deletions

View file

@ -13,6 +13,13 @@ package org.eclipse.cdt.make.internal.core.makefile;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.make.core.makefile.IInferenceRule;
import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IRule;
import org.eclipse.cdt.make.core.makefile.IStatement;
import org.eclipse.cdt.make.core.makefile.ITargetRule;
/**
* Makefile : ( statement ) *
* statement : rule | macro_definition | comments | empty
@ -29,99 +36,148 @@ import java.util.List;
* internal_macro : "$<" | "$*" | "$@" | "$?" | "$%"
*/
public abstract class AbstractMakefile {
public abstract class AbstractMakefile implements IMakefile {
public AbstractMakefile() {
}
public abstract Statement[] getStatements();
public abstract void addStatement(Statement statement);
public abstract IStatement[] getStatements();
public abstract IStatement[] getBuiltins();
public abstract void addStatement(IStatement statement);
public Rule[] getRules() {
Statement[] stmts = getStatements();
public IRule[] getRules() {
IStatement[] stmts = getStatements();
List array = new ArrayList(stmts.length);
for (int i = 0; i < stmts.length; i++) {
if (stmts[i] instanceof Rule) {
if (stmts[i] instanceof IRule) {
array.add(stmts[i]);
}
}
return (Rule[]) array.toArray(new Rule[0]);
return (IRule[]) array.toArray(new IRule[0]);
}
public Rule getRule(String target) {
Rule[] rules = getRules();
public IRule[] getRule(String target) {
IRule[] rules = getRules();
List array = new ArrayList(rules.length);
for (int i = 0; i < rules.length; i++) {
if (rules[i].getTarget().equals(target)) {
return rules[i];
}
}
return null;
}
public InferenceRule[] getInferenceRules() {
Rule[] rules = getRules();
List array = new ArrayList(rules.length);
for (int i = 0; i < rules.length; i++) {
if (rules[i] instanceof InferenceRule) {
array.add(rules[i]);
}
}
return (InferenceRule[]) array.toArray(new InferenceRule[0]);
return (IRule[]) array.toArray(new IRule[0]);
}
public InferenceRule getInferenceRule(String target) {
InferenceRule[] irules = getInferenceRules();
public IInferenceRule[] getInferenceRules() {
IRule[] rules = getRules();
List array = new ArrayList(rules.length);
for (int i = 0; i < rules.length; i++) {
if (rules[i] instanceof IInferenceRule) {
array.add(rules[i]);
}
}
return (IInferenceRule[]) array.toArray(new IInferenceRule[0]);
}
public IInferenceRule[] getInferenceRule(String target) {
IInferenceRule[] irules = getInferenceRules();
List array = new ArrayList(irules.length);
for (int i = 0; i < irules.length; i++) {
if (irules[i].getTarget().equals(target)) {
return irules[i];
array.add(irules[i]);
}
}
return null;
return (IInferenceRule[]) array.toArray(new IInferenceRule[0]);
}
public TargetRule[] getTargetRules() {
Rule[] rules = getRules();
List array = new ArrayList(rules.length);
for (int i = 0; i < rules.length; i++) {
if (rules[i] instanceof TargetRule) {
array.add(rules[i]);
public ITargetRule[] getTargetRules() {
IRule[] trules = getRules();
List array = new ArrayList(trules.length);
for (int i = 0; i < trules.length; i++) {
if (trules[i] instanceof ITargetRule) {
array.add(trules[i]);
}
}
return (TargetRule[]) array.toArray(new TargetRule[0]);
return (ITargetRule[]) array.toArray(new ITargetRule[0]);
}
public TargetRule getTargetRule(String target) {
TargetRule[] trules = getTargetRules();
public ITargetRule[] getTargetRule(String target) {
ITargetRule[] trules = getTargetRules();
List array = new ArrayList(trules.length);
for (int i = 0; i < trules.length; i++) {
if (trules[i].getTarget().equals(target)) {
return trules[i];
array.add(trules[i]);
}
}
return null;
return (ITargetRule[]) array.toArray(new ITargetRule[0]);
}
public MacroDefinition[] getMacroDefinitions() {
Statement[] stmts = getStatements();
public IMacroDefinition[] getMacroDefinitions() {
IStatement[] stmts = getStatements();
List array = new ArrayList(stmts.length);
for (int i = 0; i < stmts.length; i++) {
if (stmts[i] instanceof MacroDefinition) {
if (stmts[i] instanceof IMacroDefinition) {
array.add(stmts[i]);
}
}
return (MacroDefinition[]) array.toArray(new MacroDefinition[0]);
return (IMacroDefinition[]) array.toArray(new IMacroDefinition[0]);
}
public MacroDefinition getMacroDefinition(String name) {
MacroDefinition[] variables = getMacroDefinitions();
public IMacroDefinition[] getMacroDefinition(String name) {
IMacroDefinition[] variables = getMacroDefinitions();
List array = new ArrayList(variables.length);
for (int i = 0; i < variables.length; i++) {
if (variables[i].getName().equals(name)) {
return variables[i];
array.add(variables[i]);
}
}
return null;
return (IMacroDefinition[]) array.toArray(new IMacroDefinition[0]);
}
public void addStatements(Statement[] stmts) {
public IMacroDefinition[] getBuiltinMacroDefinitions() {
IStatement[] stmts = getBuiltins();
List array = new ArrayList(stmts.length);
for (int i = 0; i < stmts.length; i++) {
if (stmts[i] instanceof IMacroDefinition) {
array.add(stmts[i]);
}
}
return (IMacroDefinition[]) array.toArray(new IMacroDefinition[0]);
}
public IMacroDefinition[] getBuiltinMacroDefinition(String name) {
IMacroDefinition[] variables = getBuiltinMacroDefinitions();
List array = new ArrayList(variables.length);
for (int i = 0; i < variables.length; i++) {
if (variables[i].getName().equals(name)) {
array.add(variables[i]);
}
}
return (IMacroDefinition[]) array.toArray(new IMacroDefinition[0]);
}
public IInferenceRule[] getBuiltinInferenceRules() {
IStatement[] stmts = getBuiltins();
List array = new ArrayList(stmts.length);
for (int i = 0; i < stmts.length; i++) {
if (stmts[i] instanceof IInferenceRule) {
array.add(stmts[i]);
}
}
return (IInferenceRule[]) array.toArray(new IInferenceRule[0]);
}
public IInferenceRule[] getBuiltinInferenceRule(String target) {
IInferenceRule[] irules = getBuiltinInferenceRules();
List array = new ArrayList(irules.length);
for (int i = 0; i < irules.length; i++) {
if (irules[i].getTarget().equals(target)) {
array.add(irules[i]);
}
}
return (IInferenceRule[]) array.toArray(new IInferenceRule[0]);
}
public void addStatements(IStatement[] stmts) {
for (int i = 0; i < stmts.length; i++) {
addStatement(stmts[i]);
}

View file

@ -10,22 +10,16 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import org.eclipse.cdt.make.core.makefile.ICommand;
/**
* Makefile : ( statement ) *
* statement : command | ..
* command : <tab> prefix_command string <nl>
* prefix_command : '-' | '@' | '+'
*/
public class Command extends Statement implements ICommand {
public class Command extends Statement {
final public static char HYPHEN = '-';
final public static String HYPHEN_STRING = "-";
final public static char AT = '@';
final public static String AT_STRING = "@";
final public static char PLUS = '+';
final public static String PLUS_STRING = "+";
final public static char TAB = '\t';
final public static char NL = '\n';
String command = "";

View file

@ -10,15 +10,14 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
public class Comment extends Statement {
import org.eclipse.cdt.make.core.makefile.IComment;
final public static char pound = '#';
final public static String square = "#";
public class Comment extends Statement implements IComment {
String comment;
public Comment(String cmt) {
if (cmt.startsWith(square)) {
if (cmt.startsWith(POUND_STRING)) {
comment = cmt.substring(1);
} else {
comment = cmt;
@ -27,7 +26,7 @@ public class Comment extends Statement {
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(square).append(comment).append('\n');
buffer.append(POUND_STRING).append(comment).append('\n');
return buffer.toString();
}

View file

@ -10,19 +10,22 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
public class EmptyLine extends Statement {
import org.eclipse.cdt.make.core.makefile.IEmptyLine;
final public static String newline = "\n";
final public static char nl = '\n';
public class EmptyLine extends Statement implements IEmptyLine {
final public static char NL = '\n';
final public static String NL_STRING = "\n";
public EmptyLine() {
super();
}
public String toString() {
return newline;
return NL_STRING;
}
public boolean equals(EmptyLine stmt) {
public boolean equals(IEmptyLine stmt) {
return true;
}
}

View file

@ -10,13 +10,24 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import org.eclipse.cdt.make.core.makefile.ICommand;
import org.eclipse.cdt.make.core.makefile.ITarget;
public class InferenceRule extends Rule {
public InferenceRule(String target) {
public InferenceRule(String tgt) {
this (new Target(tgt));
}
public InferenceRule(ITarget target) {
this(target, new Command[0]);
}
public InferenceRule(String target, Command[] commands) {
public InferenceRule(String tgt, ICommand[] commands) {
this(new Target(tgt), commands);
}
public InferenceRule(ITarget target, ICommand[] commands) {
super(target, commands);
}
@ -27,7 +38,7 @@ public class InferenceRule extends Rule {
StringBuffer buffer = new StringBuffer();
buffer.append(target).append(':');
buffer.append('\n');
Command[] cmds = getCommands();
ICommand[] cmds = getCommands();
for (int i = 0; i < cmds.length; i++) {
buffer.append(cmds[i].toString());
}

View file

@ -10,9 +10,11 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
/**
*/
public class MacroDefinition extends Statement {
public class MacroDefinition extends Statement implements IMacroDefinition {
String name;
StringBuffer value;

View file

@ -14,6 +14,8 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.eclipse.cdt.make.core.makefile.*;
/**
* Makefile : ( statement ) *
* statement : rule | macro_definition | comments | empty
@ -46,10 +48,10 @@ public abstract class Makefile {
return filename;
}
public Rule[] getRules() {
public IInferenceRule[] getRules() {
return null;
}
public Rule getRule(String target) {
public IInferenceRule getRule(String target) {
return null;
}
public InferenceRule[] getInferenceRules() {
@ -58,23 +60,23 @@ public abstract class Makefile {
public InferenceRule getInferenceRule(String target) {
return null;
}
public TargetRule[] getTargetRules() {
public ITargetRule[] getTargetRules() {
return null;
}
public TargetRule getTargetRule(String target) {
public ITargetRule getTargetRule(String target) {
return null;
}
public Rule[] getBuiltinRules() {
public IInferenceRule[] getBuiltinRules() {
return null;
}
public Rule getBuiltinRule(String target) {
public IInferenceRule getBuiltinRule(String target) {
return null;
}
public Rule[] getBuiltinInferenceRules() {
public IInferenceRule[] getBuiltinInferenceRules() {
return null;
}
public Rule getBuiltinInferenceRule(String target) {
public IInferenceRule getBuiltinInferenceRule(String target) {
return null;
}

View file

@ -0,0 +1,86 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
/**
*/
public class MakefileReader extends LineNumberReader {
public MakefileReader(Reader reader) {
super(reader);
}
public MakefileReader(Reader reader, int sz) {
super(reader, sz);
}
public String readLine() throws IOException {
boolean done = false;
StringBuffer buffer = new StringBuffer();
boolean escapedLine = false;
boolean escapedCommand = false;
while (!done) {
String line = super.readLine();
if (line == null) {
return null;
}
if (escapedLine && line.length() > 0) {
// Eat the spaces at the beginning.
int i = 0;
while (i < line.length() && (MakefileUtil.isSpace(line.charAt(i)))) {
i++ ;
}
line = line.substring(i);
} else if (escapedCommand && line.length() > 0) {
// Only eat the first tab
if (line.charAt(0) == '\t') {
line.substring(1);
}
}
// According to POSIX rules:
// When an escaped <newline>(one preceded by a backslash) is found
// anywhere in the makefile except in a command line, it shall be replaced,
// along with any leading white space on the following line, with a single <space>
//
// When an escaped <newline> is found in a command line in a makefile,
// the command line shall contain the backslash, the <newline>, and the next line,
// except that the first character of the next line shall not be included if it is a <tab>
if (MakefileUtil.isEscapedLine(line)) {
int index = line.lastIndexOf('\\');
if (index > 0) {
if (!escapedLine && MakefileUtil.isCommand(line)) {
escapedCommand = true;
buffer.append(line);
} else {
escapedLine = true;
buffer.append(line.substring(0, index));
buffer.append(' ');
}
}
} else {
done = true;
escapedLine = false;
escapedCommand = false;
buffer.append(line);
}
}
return buffer.toString();
}
}

View file

@ -0,0 +1,149 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import java.util.ArrayList;
import java.util.List;
/**
*/
public class MakefileUtil {
private MakefileUtil() {
}
public static String[] findPrerequisites(String line) {
return findTargets(line);
}
public static String[] findTargets(String line) {
List aList = new ArrayList();
int space;
// Trim away trailing and prepending spaces.
line = line.trim();
while ((space = indexOf(line, ' ')) != -1) {
aList.add(line.substring(0, space).trim());
line = line.substring(space + 1).trim();
}
// The last target.
if (line.length() > 0) {
aList.add(line);
}
return (String[]) aList.toArray(new String[0]);
}
public static boolean isMacroDefinition(String line) {
return isMacroDefinition(line.toCharArray());
}
public static boolean isMacroDefinition(char[] line) {
return indexOf(line, '=') != -1;
}
public static boolean isTargetRule(String line) {
return isTargetRule(line.toCharArray());
}
public static boolean isTargetRule(char[] line) {
return indexOf(line, ':') != -1;
}
public static boolean isCommand(String line) {
return line.length() > 1 && line.startsWith("\t");
}
public static boolean isCommand(char[] line) {
return (line.length > 1 && line[0] == '\t');
}
public static boolean isEscapedLine(String line) {
return (line.endsWith("\\") && !line.endsWith("\\\\"));
}
public static boolean isEmptyLine(String line) {
return isEmptyLine(line.toCharArray());
}
public static boolean isEmptyLine(char[] line) {
boolean empty = true;
for (int i = 0; i < line.length; i++) {
if (!isSpace(line[i])) {
empty = false;
break;
}
}
return empty;
}
public static boolean isInferenceRule(String line) {
return line.startsWith(".") && line.indexOf(':') != -1;
}
public static boolean isInferenceRule(char[] line) {
boolean period = false;
boolean colon = false;
for (int i = 0; i < line.length; i++) {
if (line[0] == '.') {
period = true;
}
if (line[i] == ':') {
colon = true;
}
}
return period && colon;
}
public static boolean isSpace(char c) {
return (c == ' ' || c == '\t' || c == '\r' || c == '\n');
}
public static int indexOfComment(String line) {
return indexOfComment(line.toCharArray());
}
public static int indexOfComment(char[] line) {
boolean escaped = false;
for (int i = 0; i < line.length; i++) {
if (line[i] == '#' && !escaped) {
return i;
}
escaped = line[i] == '\\';
}
return -1;
}
public static int indexOf(String line, char c) {
return indexOf(line.toCharArray(), c);
}
/**
* Special indexOf() method that makes sure that what we are searching
* is not between parentheses, brackets or quotes
*/
public static int indexOf(char[] line, char c) {
int paren = 0;
int bracket = 0;
boolean escaped = false;
for (int i = 0; i < line.length; i++) {
if (line[i] == '(' && !escaped) {
paren++;
} else if (line[i] == '{' && !escaped) {
bracket++;
} else if (line[i] == ')' && !escaped) {
paren--;
} else if (line[i] == '}' && !escaped) {
bracket--;
} else if (line[i] == c) {
if (paren == 0 && bracket == 0) {
return i;
}
}
escaped = line[i] == '\\';
}
return -1;
}
}

View file

@ -10,45 +10,50 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
public abstract class Rule extends Statement {
import org.eclipse.cdt.make.core.makefile.ICommand;
import org.eclipse.cdt.make.core.makefile.IInferenceRule;
import org.eclipse.cdt.make.core.makefile.IRule;
import org.eclipse.cdt.make.core.makefile.ITarget;
Command[] commands;
String target;
public abstract class Rule extends Statement implements IRule, IInferenceRule {
public Rule(String target) {
this(target, new Command[0]);
ICommand[] commands;
ITarget target;
public Rule(ITarget tgt) {
this(tgt, new Command[0]);
}
public Rule(String tgt, Command[] cmds) {
public Rule(ITarget tgt, ICommand[] cmds) {
target = tgt;
commands = cmds;
}
public Command[] getCommands() {
public ICommand[] getCommands() {
return commands;
}
public void setCommand(Command[] cmds) {
public void setCommand(ICommand[] cmds) {
commands = cmds;
}
public String getTarget() {
public ITarget getTarget() {
return target;
}
public void setTarget(String tgt) {
public void setTarget(ITarget tgt) {
target = tgt;
}
public void addCommand(Command cmd) {
Command[] newCmds = new Command[commands.length + 1];
public void addCommand(ICommand cmd) {
ICommand[] newCmds = new ICommand[commands.length + 1];
System.arraycopy(commands, 0, newCmds, 0, commands.length);
newCmds[commands.length] = cmd;
commands = newCmds;
}
public void addCommands(Command[] cmds) {
Command[] newCmds = new Command[commands.length + cmds.length];
public void addCommands(ICommand[] cmds) {
ICommand[] newCmds = new ICommand[commands.length + cmds.length];
System.arraycopy(commands, 0, newCmds, 0, commands.length);
System.arraycopy(cmds, 0, newCmds, commands.length, cmds.length);
commands = newCmds;

View file

@ -10,7 +10,43 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
public abstract class Statement {
import org.eclipse.cdt.make.core.makefile.IStatement;
public abstract class Statement implements IStatement {
int endLine;
int startLine;
public Statement() {
}
public abstract String toString();
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IStatement#getEndLine()
*/
public int getEndLine() {
return endLine;
}
public void setEndLine(int lineno) {
endLine = lineno;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.core.makefile.IStatement#getStartLine()
*/
public int getStartLine() {
return startLine;
}
public void setStartLine(int lineno) {
startLine = lineno;
}
public void setLines(int start, int end) {
setStartLine(start);
setEndLine(end);
}
}

View file

@ -12,18 +12,22 @@ package org.eclipse.cdt.make.internal.core.makefile;
import java.io.File;
public class Target {
import org.eclipse.cdt.make.core.makefile.*;
File file;
TargetRule rule;
public class Target implements ITarget {
public Target(TargetRule r) {
rule = r;
file = new File(rule.getTarget());
String target;
public Target(String t) {
target = t;
}
public String toString() {
return target;
}
public boolean exits() {
return file.exists();
return new File(target).exists();
}
public int make(boolean echo) {
@ -35,6 +39,6 @@ public class Target {
}
public long lastModified() {
return file.lastModified();
return new File(target).lastModified();
}
}

View file

@ -10,6 +10,10 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import org.eclipse.cdt.make.core.makefile.ICommand;
import org.eclipse.cdt.make.core.makefile.ITarget;
import org.eclipse.cdt.make.core.makefile.ITargetRule;
/**
* Makefile : ( statement ) *
* statement : rule | macro_definition | comments | empty
@ -26,28 +30,28 @@ package org.eclipse.cdt.make.internal.core.makefile;
* internal_macro : "$<" | "$*" | "$@" | "$?" | "$%"
*/
public class TargetRule extends Rule {
public class TargetRule extends Rule implements ITargetRule {
String[] dependencies;
ITarget[] dependencies;
public TargetRule(String target) {
this(target, new String[0]);
public TargetRule(ITarget target) {
this(target, new ITarget[0]);
}
public TargetRule(String target, String[] pres) {
this(target, pres, new Command[0]);
public TargetRule(ITarget target, ITarget[] deps) {
this(target, deps, new ICommand[0]);
}
public TargetRule(String target, String[] deps, Command[] commands) {
public TargetRule(ITarget target, ITarget[] deps, ICommand[] commands) {
super(target, commands);
dependencies = deps;
}
public String[] getDependencies() {
public ITarget[] getDependencies() {
return dependencies;
}
public void setDependecies(String[] reqs) {
public void setDependecies(ITarget[] reqs) {
dependencies = reqs;
}
@ -63,7 +67,7 @@ public class TargetRule extends Rule {
buffer.append(dependencies[i]).append(' ');
}
buffer.append('\n');
Command[] cmds = getCommands();
ICommand[] cmds = getCommands();
for (int i = 0; i < cmds.length; i++) {
buffer.append(cmds[i].toString());
}

View file

@ -1,109 +0,0 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*/
public class Util {
private Util() {
}
public static String readLine(BufferedReader br) throws IOException {
boolean done = false;
StringBuffer buffer = new StringBuffer();
boolean escaped = false;
while (!done) {
String line = br.readLine();
if (line == null) {
return null;
}
if (escaped) {
line = line.trim();
}
// Eat the spaces at the beginning.
if (line.length() > 0 && line.charAt(0) == ' ') {
int i = 1;
while (i < line.length() && line.charAt(i) == ' ') {
i++;
}
line = line.substring(i);
}
if (line.endsWith("\\")) {
escaped = true;
int index = line.indexOf('\\');
if (index > 0) {
buffer.append(line.substring(0, index));
buffer.append(' ');
}
} else {
done = true;
escaped = false;
buffer.append(line);
}
}
return buffer.toString();
}
public static String[] findTargets(String line) {
List aList = new ArrayList();
int space;
while ((space = indexOf(line, ' ')) != -1) {
aList.add(line.substring(0, space).trim());
line = line.substring(space + 1).trim();
}
if (line.length() > 0) {
aList.add(line);
}
return (String[]) aList.toArray(new String[0]);
}
public static String[] findPrerequisites(String line) {
return findTargets(line);
}
public static boolean isMacroDefinition(char[] line) {
return indexOf(line, '=') != -1;
}
public static boolean isRule(char[] line) {
return indexOf(line, ':') != -1;
}
public static int indexOf(String s, char c) {
return indexOf(s.toCharArray(), c);
}
public static int indexOf(char[] line, char c) {
int level = 0;
for (int i = 0; i < line.length; i++) {
if (line[i] == '(' || line[i] == '{') {
level++;
} else if (line[i] == ')' || line[i] == '}') {
level--;
} else if (line[i] == c) {
if (level == 0) {
return i;
}
}
}
return -1;
}
}

View file

@ -32,7 +32,7 @@ public class PosixBuiltinMacroDefinitions {
new MacroDefinition("SCCSFLAGS="),
new MacroDefinition("SCCSGETFLAGS=-s")};
MacroDefinition getMacroDefinition(String name) {
public MacroDefinition getMacroDefinition(String name) {
for (int i = 0; i < macros.length; i++) {
if (name.equals(macros[i].getName())) {
return macros[i];
@ -41,7 +41,7 @@ public class PosixBuiltinMacroDefinitions {
return null;
}
MacroDefinition[] getMacroDefinitions() {
public MacroDefinition[] getMacroDefinitions() {
return macros;
}
}

View file

@ -108,7 +108,7 @@ public class PosixBuiltinRules {
new Command("rm -f $*.o")})
};
InferenceRule getInferenceRule(String name) {
public InferenceRule getInferenceRule(String name) {
for (int i = 0; i < rules.length; i++) {
if (name.equals(rules[i].getTarget())) {
return rules[i];
@ -117,7 +117,7 @@ public class PosixBuiltinRules {
return null;
}
InferenceRule[] getInferenceRules() {
public InferenceRule[] getInferenceRules() {
return rules;
}
}

View file

@ -10,22 +10,25 @@
***********************************************************************/
package org.eclipse.cdt.make.internal.core.makefile.posix;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.make.core.makefile.IStatement;
import org.eclipse.cdt.make.internal.core.makefile.AbstractMakefile;
import org.eclipse.cdt.make.internal.core.makefile.Command;
import org.eclipse.cdt.make.internal.core.makefile.Comment;
import org.eclipse.cdt.make.internal.core.makefile.EmptyLine;
import org.eclipse.cdt.make.internal.core.makefile.InferenceRule;
import org.eclipse.cdt.make.internal.core.makefile.MacroDefinition;
import org.eclipse.cdt.make.internal.core.makefile.MakefileReader;
import org.eclipse.cdt.make.internal.core.makefile.Rule;
import org.eclipse.cdt.make.internal.core.makefile.Statement;
import org.eclipse.cdt.make.internal.core.makefile.Target;
import org.eclipse.cdt.make.internal.core.makefile.TargetRule;
import org.eclipse.cdt.make.internal.core.makefile.Util;
import org.eclipse.cdt.make.internal.core.makefile.MacroDefinition;
import org.eclipse.cdt.make.internal.core.makefile.MakefileUtil;
/**
* Makefile : ( statement ) *
@ -48,85 +51,117 @@ public class PosixMakefile extends AbstractMakefile {
List statements;
public PosixMakefile(String name) throws IOException {
this(new BufferedReader(new FileReader(name)));
this(new FileReader(name));
}
public PosixMakefile(BufferedReader reader) throws IOException {
public PosixMakefile(Reader reader) throws IOException {
this(new MakefileReader(reader));
}
public PosixMakefile(MakefileReader reader) throws IOException {
super();
statements = new ArrayList();
parse(reader);
}
void parse(BufferedReader br) throws IOException {
void parse(MakefileReader reader) throws IOException {
String line;
Rule rule = null;
while ((line = Util.readLine(br)) != null) {
if (line.length() == 0) {
// Empty Line.
statements.add(new EmptyLine());
} else if (line.startsWith("#")) {
int startLine = 0;
int endLine = 0;
while ((line = reader.readLine()) != null) {
startLine = endLine + 1;
endLine = reader.getLineNumber();
// Strip away any comments.
int pound = MakefileUtil.indexOfComment(line);
if (pound != -1) {
// Comment.
statements.add(new Comment(line));
} else if (line.startsWith("\t")) {
Statement stmt = new Comment(line.substring(pound + 1));
stmt.setLines(startLine, endLine);
addStatement(stmt);
line = line.substring(0, pound);
}
if (MakefileUtil.isEmptyLine(line)) {
// Empty Line.
Statement stmt = new EmptyLine();
stmt.setLines(startLine, endLine);
addStatement(stmt);
} else if (MakefileUtil.isCommand(line)) {
// Command.
Command cmd = new Command(line);
if (rule != null) {
rule.addCommand(cmd);
rule.setEndLine(endLine);
} else {
throw new IOException("Error Parsing");
}
} else if (line.startsWith(".")) {
} else if (MakefileUtil.isInferenceRule(line)) {
// Inference Rule
String tgt;
int index = Util.indexOf(line, ':');
int index = MakefileUtil.indexOf(line, ':');
if (index != -1) {
tgt = line.substring(0, index);
} else {
tgt = line;
}
rule = new InferenceRule(tgt);
statements.add(rule);
} else {
char[] array = line.toCharArray();
if (Util.isMacroDefinition(array)) {
// MacroDefinition
statements.add(new MacroDefinition(line));
} else if (Util.isRule(array)) {
String[] targets;
String[] reqs = new String[0];
String cmd = null;
int index = Util.indexOf(array, ':');
if (index != -1) {
String target = line.substring(0, index);
// Break the targets
targets = Util.findTargets(target.trim());
rule = new InferenceRule(new Target(tgt));
rule.setLines(startLine, endLine);
addStatement(rule);
} else if (MakefileUtil.isMacroDefinition(line)) {
// MacroDefinition
Statement stmt = new MacroDefinition(line);
stmt.setLines(startLine, endLine);
addStatement(stmt);
} else if (MakefileUtil.isTargetRule(line)) {
String[] targets;
String[] reqs = new String[0];
String cmd = null;
int index = MakefileUtil.indexOf(line.toCharArray(), ':');
if (index != -1) {
String target = line.substring(0, index);
// Break the targets
targets = MakefileUtil.findTargets(target.trim());
String req = line.substring(index + 1);
int semicolon = Util.indexOf(req, ';');
if (semicolon != -1) {
cmd = req.substring(semicolon + 1);
req = req.substring(0, semicolon);
}
reqs = Util.findPrerequisites(req.trim());
} else {
targets = new String[] { line };
}
for (int i = 0; i < targets.length; i++) {
rule = new TargetRule(targets[i], reqs);
statements.add(rule);
if (cmd != null) {
rule.addCommand(new Command(cmd));
}
String req = line.substring(index + 1);
int semicolon = MakefileUtil.indexOf(req, ';');
if (semicolon != -1) {
cmd = req.substring(semicolon + 1);
req = req.substring(0, semicolon);
}
reqs = MakefileUtil.findPrerequisites(req.trim());
} else {
targets = MakefileUtil.findTargets(line);
}
Target[] preqs = new Target[reqs.length];
for (int i = 0; i < reqs.length; i++) {
preqs[i] = new Target(reqs[i]);
}
for (int i = 0; i < targets.length; i++) {
rule = new TargetRule(new Target(targets[i]), preqs);
rule.setLines(startLine, endLine);
addStatement(rule);
if (cmd != null) {
rule.addCommand(new Command(cmd));
}
}
} else {
// ???
}
}
}
public Statement[] getStatements() {
return (Statement[]) statements.toArray(new Statement[0]);
public IStatement[] getStatements() {
return (IStatement[]) statements.toArray(new Statement[0]);
}
public IStatement[] getBuiltins() {
IStatement[] macros = new PosixBuiltinMacroDefinitions().getMacroDefinitions();
IStatement[] rules = new PosixBuiltinRules().getInferenceRules();
IStatement[] stmts = new IStatement[macros.length + rules.length];
System.arraycopy(macros, 0, stmts, 0, macros.length);
System.arraycopy(rules, 0, stmts, macros.length, rules.length);
return stmts;
}
public void addStatement(Statement stmt) {
@ -140,7 +175,7 @@ public class PosixMakefile extends AbstractMakefile {
filename = args[0];
}
PosixMakefile makefile = new PosixMakefile(filename);
Statement[] statements = makefile.getStatements();
IStatement[] statements = makefile.getStatements();
for (int i = 0; i < statements.length; i++) {
//System.out.println("Rule[" + i +"]");
System.out.print(statements[i]);
@ -150,4 +185,11 @@ public class PosixMakefile extends AbstractMakefile {
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.internal.core.makefile.AbstractMakefile#addStatement(org.eclipse.cdt.make.core.makefile.IStatement)
*/
public void addStatement(IStatement statement) {
statements.add(statement);
}
}