diff --git a/bakefile.jar b/bakefile.jar index 4e01078..8731420 100644 Binary files a/bakefile.jar and b/bakefile.jar differ diff --git a/src/fr/monlouyan/bakefile/BakefileParser.java b/src/fr/monlouyan/bakefile/BakefileParser.java index 9659613..4bf89c2 100644 --- a/src/fr/monlouyan/bakefile/BakefileParser.java +++ b/src/fr/monlouyan/bakefile/BakefileParser.java @@ -25,10 +25,21 @@ public class BakefileParser { */ private static final Pattern COMMAND_PATTERN = Pattern.compile("^\\s+(.+)$"); + /** + * Regex pour détecter les définitions de variables. + * Format : "FLAGS = -ansi -pedantic" + */ + private static final Pattern VARIABLE_PATTERN = Pattern.compile("^(\\w+)\\s*=\\s*(.*)$"); + /** * Première cible trouvée dans le fichier Bakefile. */ private static String firstTarget; + + /** + * Stocke les variables définies dans le Bakefile. + */ + private Map<String, String> variables = new HashMap<>(); public BakefileParser(String filename) { @@ -39,53 +50,80 @@ public class BakefileParser { public List<Rule> parse() { 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(1); } - + try { List<String> lines = Files.readAllLines(Paths.get(filename)); String currentTarget = null; List<String> dependencies = new ArrayList<>(); List<String> commands = new ArrayList<>(); - + for (String line : lines) { + Matcher varMatcher = VARIABLE_PATTERN.matcher(line); Matcher targetMatcher = TARGET_PATTERN.matcher(line); Matcher commandMatcher = COMMAND_PATTERN.matcher(line); - - if (targetMatcher.matches()) { + + if (varMatcher.matches()) { + // Stocke la variable + variables.put(varMatcher.group(1), varMatcher.group(2)); + } else if (targetMatcher.matches()) { if (firstTarget == null) { firstTarget = targetMatcher.group(1); } - - // Sauvegarde de la règle précédente si elle existe + if (currentTarget != null) { - rules.add(new Rule(currentTarget, dependencies, commands, phonyTargets.contains(currentTarget))); + rules.add(new Rule(currentTarget, dependencies, replaceVariables(commands), phonyTargets.contains(currentTarget))); } - - // Nouvelle cible détectée + currentTarget = targetMatcher.group(1); dependencies = new ArrayList<>(Arrays.asList(targetMatcher.group(2).trim().split("\\s+"))); commands = new ArrayList<>(); } else if (commandMatcher.matches()) { - // Ligne de commande associée à la dernière cible trouvée commands.add(commandMatcher.group(1)); } } - - // Ajout de la dernière règle après la boucle + if (currentTarget != null) { - rules.add(new Rule(currentTarget, dependencies, commands, phonyTargets.contains(currentTarget))); + rules.add(new Rule(currentTarget, dependencies, replaceVariables(commands), phonyTargets.contains(currentTarget))); } - + } catch (IOException e) { e.printStackTrace(); } return rules; } + /** + * Remplace les variables dans une liste de commandes. + * Ex : "gcc $(FLAGS) -o main main.c" devient "gcc -ansi -pedantic -o main main.c" + * @param commands Liste des commandes à modifier + * @return Liste avec les variables remplacées + */ + private List<String> replaceVariables(List<String> commands) { + List<String> resolvedCommands = new ArrayList<>(); + for (String command : commands) { + boolean replaced; + do { + replaced = false; + for (Map.Entry<String, String> entry : variables.entrySet()) { + String key = "$(" + entry.getKey() + ")"; + if (command.contains(key)) { + command = command.replace(key, entry.getValue()); + replaced = true; + } + } + } while (replaced); // Continue tant qu'on remplace des variables + resolvedCommands.add(command); + } + return resolvedCommands; + } + + + /** * Permet de récupérer la première cible trouvée dans le fichier Bakefile. * @return La première cible trouvée diff --git a/tests/bakefile.jar b/tests/bakefile.jar index 4e01078..8731420 100644 Binary files a/tests/bakefile.jar and b/tests/bakefile.jar differ diff --git a/tests/test-01-depuis-rien/bakefile.jar b/tests/test-01-depuis-rien/bakefile.jar deleted file mode 100644 index 4e01078..0000000 Binary files a/tests/test-01-depuis-rien/bakefile.jar and /dev/null differ diff --git a/tests/test-01-depuis-rien/Bakefile b/tests/test-01-from-nothing/Bakefile similarity index 58% rename from tests/test-01-depuis-rien/Bakefile rename to tests/test-01-from-nothing/Bakefile index 4e0e544..35bfebb 100644 --- a/tests/test-01-depuis-rien/Bakefile +++ b/tests/test-01-from-nothing/Bakefile @@ -1,2 +1,4 @@ +FLAGS = -ansi -pedabtic + main: main.c gcc -o main main.c \ No newline at end of file diff --git a/tests/test-01-depuis-rien/README.md b/tests/test-01-from-nothing/README.md similarity index 100% rename from tests/test-01-depuis-rien/README.md rename to tests/test-01-from-nothing/README.md diff --git a/tests/test-01-depuis-rien/main.c b/tests/test-01-from-nothing/main.c similarity index 100% rename from tests/test-01-depuis-rien/main.c rename to tests/test-01-from-nothing/main.c diff --git a/tests/test-02-existe-deja/Bakefile b/tests/test-02-already-exist/Bakefile similarity index 100% rename from tests/test-02-existe-deja/Bakefile rename to tests/test-02-already-exist/Bakefile diff --git a/tests/test-02-existe-deja/README.md b/tests/test-02-already-exist/README.md similarity index 100% rename from tests/test-02-existe-deja/README.md rename to tests/test-02-already-exist/README.md diff --git a/tests/test-02-existe-deja/main b/tests/test-02-already-exist/main similarity index 100% rename from tests/test-02-existe-deja/main rename to tests/test-02-already-exist/main diff --git a/tests/test-02-existe-deja/main.c b/tests/test-02-already-exist/main.c similarity index 100% rename from tests/test-02-existe-deja/main.c rename to tests/test-02-already-exist/main.c diff --git a/tests/test-02-existe-deja/bakefile.jar b/tests/test-02-existe-deja/bakefile.jar deleted file mode 100644 index 4e01078..0000000 Binary files a/tests/test-02-existe-deja/bakefile.jar and /dev/null differ diff --git a/tests/test-03-circulaire/bakefile.jar b/tests/test-03-circulaire/bakefile.jar deleted file mode 100644 index 4e01078..0000000 Binary files a/tests/test-03-circulaire/bakefile.jar and /dev/null differ diff --git a/tests/test-03-circulaire/Bakefile b/tests/test-03-circular/Bakefile similarity index 100% rename from tests/test-03-circulaire/Bakefile rename to tests/test-03-circular/Bakefile diff --git a/tests/test-03-circulaire/README.md b/tests/test-03-circular/README.md similarity index 100% rename from tests/test-03-circulaire/README.md rename to tests/test-03-circular/README.md diff --git a/tests/test-03-circulaire/a.c b/tests/test-03-circular/a.c similarity index 100% rename from tests/test-03-circulaire/a.c rename to tests/test-03-circular/a.c diff --git a/tests/test-03-circulaire/a.h b/tests/test-03-circular/a.h similarity index 100% rename from tests/test-03-circulaire/a.h rename to tests/test-03-circular/a.h diff --git a/tests/test-03-circulaire/b.c b/tests/test-03-circular/b.c similarity index 100% rename from tests/test-03-circulaire/b.c rename to tests/test-03-circular/b.c diff --git a/tests/test-03-circulaire/b.h b/tests/test-03-circular/b.h similarity index 100% rename from tests/test-03-circulaire/b.h rename to tests/test-03-circular/b.h diff --git a/tests/test-03-circulaire/c.c b/tests/test-03-circular/c.c similarity index 100% rename from tests/test-03-circulaire/c.c rename to tests/test-03-circular/c.c diff --git a/tests/test-03-circulaire/c.h b/tests/test-03-circular/c.h similarity index 100% rename from tests/test-03-circulaire/c.h rename to tests/test-03-circular/c.h diff --git a/tests/test-04-edited/Bakefile b/tests/test-04-edited/Bakefile new file mode 100644 index 0000000..d698de6 --- /dev/null +++ b/tests/test-04-edited/Bakefile @@ -0,0 +1,2 @@ +main: main.c + gcc -o main main.c diff --git a/tests/test-04-edited/README.md b/tests/test-04-edited/README.md new file mode 100644 index 0000000..6910a2c --- /dev/null +++ b/tests/test-04-edited/README.md @@ -0,0 +1,13 @@ +# Test 2 : Compilation où le résultat existe déjà + +## Description +Ce test vérifie que si l'exécutable `main` est déjà présent et que `main.c` +n'a pas été modifié, alors `Bake` ne tente pas de recompiler. + +## Fichiers utilisés +- `main.c` : Fichier source inchangé. +- `Bakefile` : Contient les règles de compilation. +- `main` : Exécutable déjà généré. + +## Résultat attendu +Aucun message indiquant la compilation ne doit apparaître. diff --git a/tests/test-04-edited/main b/tests/test-04-edited/main new file mode 100755 index 0000000..a65d3dd Binary files /dev/null and b/tests/test-04-edited/main differ diff --git a/tests/test-04-edited/main.c b/tests/test-04-edited/main.c new file mode 100644 index 0000000..dee38e7 --- /dev/null +++ b/tests/test-04-edited/main.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(void) { + printf("Ceci est un programme de test du Bakefile !\n"); + printf("test\n"); + return 0; +} diff --git a/tests/test-05-variables/Bakefile b/tests/test-05-variables/Bakefile new file mode 100644 index 0000000..736f996 --- /dev/null +++ b/tests/test-05-variables/Bakefile @@ -0,0 +1,4 @@ +FLAGS = -ansi -pedantic + +main: main.c + gcc $(FLAGS) -o main main.c diff --git a/tests/test-05-variables/README.md b/tests/test-05-variables/README.md new file mode 100644 index 0000000..28b2bdc --- /dev/null +++ b/tests/test-05-variables/README.md @@ -0,0 +1 @@ +# Test 5 : Utilisation de variables diff --git a/tests/test-05-variables/main.c b/tests/test-05-variables/main.c new file mode 100644 index 0000000..145de7a --- /dev/null +++ b/tests/test-05-variables/main.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main(void) { + printf("Ceci est un programme de test du Bakefile !\n"); + return 0; +} diff --git a/tests/test-06-variables-on-cascade/Bakefile b/tests/test-06-variables-on-cascade/Bakefile new file mode 100644 index 0000000..8c7779b --- /dev/null +++ b/tests/test-06-variables-on-cascade/Bakefile @@ -0,0 +1,6 @@ +FLAGS = -ansi -pedantic +FLAGSS = $(FLAGS) +FLAGSSS = $(FLAGSS) + +main: main.c + gcc $(FLAGSSS) -o main main.c diff --git a/tests/test-06-variables-on-cascade/README.md b/tests/test-06-variables-on-cascade/README.md new file mode 100644 index 0000000..0baeab4 --- /dev/null +++ b/tests/test-06-variables-on-cascade/README.md @@ -0,0 +1 @@ +# Test 6 : Gestion des variables en cascade diff --git a/tests/test-06-variables-on-cascade/main.c b/tests/test-06-variables-on-cascade/main.c new file mode 100644 index 0000000..145de7a --- /dev/null +++ b/tests/test-06-variables-on-cascade/main.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main(void) { + printf("Ceci est un programme de test du Bakefile !\n"); + return 0; +}