|
| 1 | +// SPDX-FileCopyrightText : © 2025 TU Wien <vadl@tuwien.ac.at> |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package vadl.rtl; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
| 24 | +import vadl.DockerExecutionTest; |
| 25 | +import vadl.configuration.RtlConfiguration; |
| 26 | +import vadl.pass.PassOrders; |
| 27 | +import vadl.pass.exception.DuplicatedPassKeyException; |
| 28 | + |
| 29 | +public abstract class RtlDockerTest extends DockerExecutionTest { |
| 30 | + |
| 31 | + private static final String RTL_BASE_IMAGE = |
| 32 | + "ghcr.io/openvadl/rtl-test-base@sha256:975bacbae46da25dda52340a18772d349c7e43e3dd8aae43be61f254fc728ca8"; |
| 33 | + |
| 34 | + private static final ConcurrentHashMap<String, ImageFromDockerfile> imageCache = |
| 35 | + new ConcurrentHashMap<>(); |
| 36 | + |
| 37 | + protected ImageFromDockerfile generateRtlImage(String specPath, |
| 38 | + RtlConfiguration configuration) { |
| 39 | + |
| 40 | + final var cacheKey = String.valueOf(Set.of(specPath, configuration).hashCode()); |
| 41 | + return RtlDockerTest.imageCache.computeIfAbsent(cacheKey, (k) -> { |
| 42 | + |
| 43 | + try { |
| 44 | + // Generate RTL core |
| 45 | + setupPassManagerAndRunSpec(specPath, PassOrders.rtl(configuration)); |
| 46 | + |
| 47 | + // Return image with generated files |
| 48 | + return getImage(configuration); |
| 49 | + } catch (IOException | DuplicatedPassKeyException e) { |
| 50 | + throw new RuntimeException(e); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + private ImageFromDockerfile getImage(RtlConfiguration configuration |
| 56 | + ) { |
| 57 | + |
| 58 | + // find output dir |
| 59 | + var outputPath = Path.of(configuration.outputPath() + "/rtl").toAbsolutePath(); |
| 60 | + if (!outputPath.toFile().exists()) { |
| 61 | + throw new IllegalStateException("RTL output path was not found (not generated?)"); |
| 62 | + } |
| 63 | + |
| 64 | + // Get redis cache for faster compilation using sccache |
| 65 | + var redisCache = getRunningRedisCache(); |
| 66 | + |
| 67 | + var dockerImage = new ImageFromDockerfile() |
| 68 | + .withDockerfileFromBuilder(d -> { |
| 69 | + d.from(RTL_BASE_IMAGE); |
| 70 | + |
| 71 | + d.workDir("/rtl"); |
| 72 | + |
| 73 | + // Copy files into container |
| 74 | + d.copy("/rtl", "/rtl"); |
| 75 | + d.copy("/scripts", "/scripts"); |
| 76 | + |
| 77 | + d.run("chmod +x /scripts/*"); |
| 78 | + |
| 79 | + d.build(); |
| 80 | + } |
| 81 | + ) |
| 82 | + // Make generated sources available to image builder |
| 83 | + .withFileFromPath("/rtl", outputPath) |
| 84 | + // make scripts available to image builder |
| 85 | + .withFileFromClasspath("/scripts", "/scripts/rtl"); |
| 86 | + |
| 87 | + // As we have to use the same network as the redis cache, we have to build it there |
| 88 | + return redisCache.setupEnv(dockerImage); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments