2025-02-04 10:18:26 +01:00
|
|
|
package fr.monlouyan.bakefile;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
2025-02-04 16:27:07 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2025-02-08 22:35:00 +01:00
|
|
|
import java.util.stream.Collectors;
|
2025-02-04 10:18:26 +01:00
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Parseur de fichier Bakefile.
|
|
|
|
* Cette classe est responsable de l'analyse syntaxique du fichier Bakefile
|
|
|
|
* pour extraire les règles de build, les dépendances et les commandes associées.
|
|
|
|
*
|
|
|
|
* @author Moncef STITI, Yanis HAMOUDI, Louay DARDOURI
|
|
|
|
* @version 1.0
|
|
|
|
*/
|
2025-02-04 10:18:26 +01:00
|
|
|
public class BakefileParser {
|
2025-02-22 21:21:51 +01:00
|
|
|
|
|
|
|
/**
|
2025-02-04 19:27:56 +01:00
|
|
|
* Nom du fichier Bakefile à parser (donc Bakefile...).
|
|
|
|
*/
|
2025-02-04 10:18:26 +01:00
|
|
|
private String filename;
|
2025-02-22 21:21:51 +01:00
|
|
|
|
|
|
|
/**
|
2025-02-04 16:27:07 +01:00
|
|
|
* Regex pour détecter les targets et leurs dépendances.
|
2025-02-28 21:34:56 +01:00
|
|
|
* Format : "nom1 nom2 nom3 : dépendance1 dépendance2"
|
2025-03-15 17:02:26 +01:00
|
|
|
* La nouvelle regex gère plusieurs cibles séparées par des espaces
|
2025-02-04 16:27:07 +01:00
|
|
|
*/
|
2025-03-15 17:02:26 +01:00
|
|
|
private static final Pattern TARGET_PATTERN = Pattern.compile("^([^:#]+?)\\s*:\\s*([^#]*?)\\s*(?:#.*)?$");
|
2025-02-04 16:27:07 +01:00
|
|
|
|
2025-02-22 21:21:51 +01:00
|
|
|
/**
|
2025-02-04 16:27:07 +01:00
|
|
|
* Regex pour détecter les lignes de commande associées à une target.
|
|
|
|
* Format : " gcc -o program program.c" (ligne indentée)
|
|
|
|
*/
|
2025-02-08 22:40:49 +01:00
|
|
|
private static final Pattern COMMAND_PATTERN = Pattern.compile("^\\t(.+)$");
|
2025-02-04 16:27:07 +01:00
|
|
|
|
2025-02-22 21:21:51 +01:00
|
|
|
/**
|
2025-02-04 19:58:04 +01:00
|
|
|
* Regex pour détecter les définitions de variables.
|
|
|
|
* Format : "FLAGS = -ansi -pedantic"
|
|
|
|
*/
|
2025-02-08 22:35:00 +01:00
|
|
|
private static final Pattern VARIABLE_PATTERN = Pattern.compile("^(\\w+)\\s*=\\s*([^#]*?)\\s*(?:#.*)?$");
|
2025-02-04 19:58:04 +01:00
|
|
|
|
2025-02-22 21:21:51 +01:00
|
|
|
/**
|
2025-02-08 23:15:07 +01:00
|
|
|
* Regex pour détecter les déclarations .PHONY
|
|
|
|
* Format : ".PHONY: clean all"
|
|
|
|
*/
|
|
|
|
private static final Pattern PHONY_PATTERN = Pattern.compile("^\\.PHONY:\\s*([^#]*?)\\s*(?:#.*)?$");
|
|
|
|
|
2025-02-22 21:21:51 +01:00
|
|
|
/**
|
|
|
|
* Regex pour détecter les références de variables.
|
|
|
|
* Format : "${VAR}" ou "$(VAR)"
|
|
|
|
*/
|
|
|
|
private static final Pattern VARIABLE_REFERENCE = Pattern.compile("\\$\\{(\\w+)\\}|\\$\\((\\w+)\\)");
|
|
|
|
|
|
|
|
/**
|
2025-02-04 19:27:56 +01:00
|
|
|
* Première cible trouvée dans le fichier Bakefile.
|
|
|
|
*/
|
|
|
|
private static String firstTarget;
|
2025-02-04 19:58:04 +01:00
|
|
|
|
2025-02-22 21:21:51 +01:00
|
|
|
/**
|
2025-02-04 19:58:04 +01:00
|
|
|
* Stocke les variables définies dans le Bakefile.
|
|
|
|
*/
|
|
|
|
private Map<String, String> variables = new HashMap<>();
|
2025-02-04 19:27:56 +01:00
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Constructeur de la classe BakefileParser.
|
|
|
|
* @param filename Nom du fichier Bakefile à parser
|
|
|
|
*/
|
2025-02-04 10:18:26 +01:00
|
|
|
public BakefileParser(String filename) {
|
|
|
|
this.filename = filename;
|
2025-02-04 19:27:56 +01:00
|
|
|
firstTarget = null;
|
2025-02-04 10:18:26 +01:00
|
|
|
}
|
2025-02-04 16:27:07 +01:00
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Remplacer les variables dans une chaîne.
|
|
|
|
* @param input Chaîne à traiter
|
|
|
|
* @return Chaîne avec les variables remplacées
|
|
|
|
*/
|
2025-03-15 17:02:26 +01:00
|
|
|
private String replaceVariables(String input) {
|
|
|
|
if (input == null) return null;
|
|
|
|
|
|
|
|
String result = input;
|
|
|
|
Set<String> processedVars = new HashSet<>();
|
|
|
|
boolean changed;
|
|
|
|
|
|
|
|
do {
|
|
|
|
changed = false;
|
|
|
|
Matcher matcher = VARIABLE_REFERENCE.matcher(result);
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
|
|
while (matcher.find()) {
|
|
|
|
String varName = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
|
|
|
|
if (!processedVars.contains(varName) && variables.containsKey(varName)) {
|
|
|
|
String replacement = variables.get(varName);
|
|
|
|
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
|
|
|
changed = true;
|
|
|
|
processedVars.add(varName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
matcher.appendTail(sb);
|
|
|
|
result = sb.toString();
|
|
|
|
|
|
|
|
// Si aucun changement n'a été fait dans ce passage, arrêter
|
|
|
|
if (!changed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Réinitialiser processedVars pour le prochain passage si nécessaire
|
|
|
|
processedVars.clear();
|
|
|
|
|
|
|
|
} while (changed);
|
|
|
|
|
|
|
|
return result.trim();
|
|
|
|
}
|
2025-02-22 21:21:51 +01:00
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Remplacer les variables dans une liste de chaînes.
|
|
|
|
* @param items Liste de chaînes à traiter
|
|
|
|
* @return Liste de chaînes avec les variables remplacées
|
|
|
|
*/
|
2025-02-22 21:21:51 +01:00
|
|
|
private List<String> replaceVariablesInList(List<String> items) {
|
|
|
|
return items.stream()
|
|
|
|
.map(this::replaceVariables)
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Découper les dépendances en une liste de chaînes.
|
|
|
|
* @param depStr Chaîne de dépendances
|
|
|
|
* @return Liste de dépendances
|
|
|
|
*/
|
2025-02-08 23:15:07 +01:00
|
|
|
private List<String> splitDependencies(String depStr) {
|
2025-02-08 22:35:00 +01:00
|
|
|
if (depStr == null || depStr.trim().isEmpty()) {
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String resolvedStr = replaceVariables(depStr.trim());
|
|
|
|
return Arrays.stream(resolvedStr.split("\\s+"))
|
|
|
|
.map(String::trim)
|
|
|
|
.filter(s -> !s.isEmpty())
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Découper les cibles en une liste de chaînes.
|
|
|
|
* @param targetStr Chaîne de cibles
|
|
|
|
* @return Liste de cibles
|
|
|
|
*/
|
2025-02-28 21:34:56 +01:00
|
|
|
private List<String> splitTargets(String targetStr) {
|
|
|
|
if (targetStr == null || targetStr.trim().isEmpty()) {
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String resolvedStr = replaceVariables(targetStr.trim());
|
|
|
|
return Arrays.stream(resolvedStr.split("\\s+"))
|
|
|
|
.map(String::trim)
|
|
|
|
.filter(s -> !s.isEmpty())
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
2025-03-07 22:13:57 +01:00
|
|
|
/**
|
|
|
|
* Analyser le fichier Bakefile pour extraire les règles de build.
|
|
|
|
* @return Liste des règles extraites
|
|
|
|
*/
|
2025-02-04 17:31:25 +01:00
|
|
|
public List<Rule> parse() {
|
2025-03-15 17:36:13 +01:00
|
|
|
List<Rule> rules = new ArrayList<>();
|
|
|
|
Set<String> phonyTargets = new HashSet<>();
|
|
|
|
|
|
|
|
if (!Files.exists(Paths.get(filename))) {
|
|
|
|
System.out.println("*** No targets specified and no makefile found. Stop.");
|
|
|
|
System.exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
List<String> lines = Files.readAllLines(Paths.get(filename));
|
|
|
|
List<String> currentTargets = null;
|
|
|
|
List<String> dependencies = new ArrayList<>();
|
|
|
|
List<String> commands = new ArrayList<>();
|
|
|
|
boolean inContinuedCommand = false;
|
|
|
|
StringBuilder continuedCommand = new StringBuilder();
|
|
|
|
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
|
|
String line = lines.get(i).replace("\r", "");
|
|
|
|
|
|
|
|
// Ignorer les lignes vides
|
|
|
|
if (line.trim().isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gérer les erreurs de format (espaces au lieu de tabulations)
|
|
|
|
if (line.matches("^ +.*$") && !inContinuedCommand) {
|
|
|
|
System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop.");
|
|
|
|
System.exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Si nous sommes en train de traiter une ligne continuée
|
|
|
|
if (inContinuedCommand) {
|
|
|
|
if (line.endsWith("\\")) {
|
|
|
|
// Encore une continuation
|
|
|
|
continuedCommand.append(" ").append(line.substring(0, line.length() - 1).trim());
|
|
|
|
} else {
|
|
|
|
// Fin de la continuation
|
|
|
|
continuedCommand.append(" ").append(line.trim());
|
|
|
|
commands.add(continuedCommand.toString().trim());
|
|
|
|
inContinuedCommand = false;
|
|
|
|
continuedCommand = new StringBuilder();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matcher pour les déclarations .PHONY
|
|
|
|
Matcher phonyMatcher = PHONY_PATTERN.matcher(line);
|
|
|
|
if (phonyMatcher.matches()) {
|
|
|
|
String[] phonies = phonyMatcher.group(1).trim().split("\\s+");
|
|
|
|
Collections.addAll(phonyTargets, phonies);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matcher pour les déclarations de variables
|
|
|
|
Matcher varMatcher = VARIABLE_PATTERN.matcher(line);
|
|
|
|
if (varMatcher.matches()) {
|
|
|
|
String varName = varMatcher.group(1);
|
|
|
|
String varValue = varMatcher.group(2).trim();
|
|
|
|
// Évaluer les variables référencées dans la valeur
|
|
|
|
varValue = replaceVariables(varValue);
|
|
|
|
variables.put(varName, varValue);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matcher pour les cibles et dépendances
|
|
|
|
Matcher targetMatcher = TARGET_PATTERN.matcher(line);
|
|
|
|
if (targetMatcher.matches()) {
|
|
|
|
// Si nous avions des cibles précédentes, créons les règles correspondantes
|
|
|
|
if (currentTargets != null) {
|
|
|
|
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes
|
|
|
|
for (String target : currentTargets) {
|
|
|
|
String resolvedTarget = replaceVariables(target.trim());
|
|
|
|
rules.add(new Rule(
|
|
|
|
resolvedTarget,
|
|
|
|
replaceVariablesInList(dependencies),
|
|
|
|
replaceVariablesInList(commands),
|
|
|
|
phonyTargets.contains(resolvedTarget)
|
|
|
|
));
|
|
|
|
|
|
|
|
if (firstTarget == null) {
|
|
|
|
firstTarget = resolvedTarget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configuration pour les nouvelles cibles
|
|
|
|
String targetStr = targetMatcher.group(1);
|
|
|
|
currentTargets = splitTargets(targetStr);
|
|
|
|
|
|
|
|
String depStr = targetMatcher.group(2);
|
|
|
|
dependencies = splitDependencies(depStr);
|
|
|
|
commands = new ArrayList<>();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matcher pour les lignes de commande
|
|
|
|
Matcher commandMatcher = COMMAND_PATTERN.matcher(line);
|
|
|
|
if (commandMatcher.matches()) {
|
|
|
|
String command = commandMatcher.group(1);
|
|
|
|
|
|
|
|
// Gérer la continuation de ligne
|
|
|
|
if (command.endsWith("\\")) {
|
|
|
|
inContinuedCommand = true;
|
|
|
|
continuedCommand = new StringBuilder(command.substring(0, command.length() - 1).trim());
|
|
|
|
} else {
|
|
|
|
commands.add(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traiter les dernières cibles
|
|
|
|
if (currentTargets != null) {
|
|
|
|
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes
|
|
|
|
for (String target : currentTargets) {
|
|
|
|
String resolvedTarget = replaceVariables(target.trim());
|
|
|
|
rules.add(new Rule(
|
|
|
|
resolvedTarget,
|
|
|
|
replaceVariablesInList(dependencies),
|
|
|
|
replaceVariablesInList(commands),
|
|
|
|
phonyTargets.contains(resolvedTarget)
|
|
|
|
));
|
|
|
|
|
|
|
|
if (firstTarget == null) {
|
|
|
|
firstTarget = resolvedTarget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BakeCLI.isDebug()) {
|
|
|
|
System.out.println("Debug: Parsed " + rules.size() + " rules.");
|
|
|
|
for (Rule rule : rules) {
|
|
|
|
System.out.println("Debug: Rule: " + rule.getName());
|
|
|
|
System.out.println("Debug: Commands: " + rule.getCommands().size());
|
|
|
|
for (String cmd : rule.getCommands()) {
|
|
|
|
System.out.println("Debug: [" + cmd + "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return rules;
|
|
|
|
}
|
2025-02-04 19:27:56 +01:00
|
|
|
|
2025-02-22 21:21:51 +01:00
|
|
|
/**
|
|
|
|
* Récupérer la première cible
|
|
|
|
* @return String la première cible
|
|
|
|
*/
|
2025-02-08 23:15:07 +01:00
|
|
|
public static String getFirstTarget() {
|
|
|
|
return firstTarget;
|
|
|
|
}
|
|
|
|
}
|