|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Renesas Electronics Europe. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.cdt.build.gcc.core.tests; |
| 12 | + |
| 13 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 14 | +import static org.hamcrest.Matchers.contains; |
| 15 | +import static org.hamcrest.Matchers.hasItem; |
| 16 | +import static org.hamcrest.Matchers.hasItems; |
| 17 | +import static org.hamcrest.Matchers.is; |
| 18 | +import static org.hamcrest.Matchers.not; |
| 19 | +import static org.hamcrest.Matchers.nullValue; |
| 20 | + |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import org.eclipse.cdt.build.gcc.core.ClangToolChain; |
| 25 | +import org.eclipse.cdt.core.build.IToolChain; |
| 26 | +import org.eclipse.cdt.core.envvar.EnvironmentVariable; |
| 27 | +import org.eclipse.cdt.core.envvar.IEnvironmentVariable; |
| 28 | +import org.eclipse.core.runtime.Platform; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for org.eclipse.cdt.build.gcc.core.ClangToolChain |
| 33 | + * Tests that the environment variables CC and CXX are set correctly. |
| 34 | + */ |
| 35 | +public class TestClangToolChain { |
| 36 | + |
| 37 | + /** |
| 38 | + * Tests: |
| 39 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 40 | + * Where pathToToolChain=a/clang.exe |
| 41 | + * Where envVars is null. |
| 42 | + * |
| 43 | + * Expected: |
| 44 | + * IToolChain.getVariables() has items "CC=a/clang.exe", "CXX=a/clang++.exe" |
| 45 | + */ |
| 46 | + @Test |
| 47 | + public void clangExe() throws Exception { |
| 48 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang.exe"), null, null); |
| 49 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 50 | + assertThat(Arrays.asList(variables), hasItems( // |
| 51 | + new EnvironmentVariable("CC", "a/clang.exe"), // |
| 52 | + new EnvironmentVariable("CXX", "a/clang++.exe"))); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Tests: |
| 57 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 58 | + * Where pathToToolChain=null |
| 59 | + * Where envVars is null. |
| 60 | + * |
| 61 | + * Expected: |
| 62 | + * IToolChain.getVariables() is null |
| 63 | + */ |
| 64 | + @Test |
| 65 | + public void nullPathToToolChain() throws Exception { |
| 66 | + IToolChain tc = new ClangToolChain(null, (java.nio.file.Path) null, null, null); |
| 67 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 68 | + assertThat(variables, is(nullValue())); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Tests: |
| 73 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 74 | + * Where pathToToolChain=a/clang++.exe |
| 75 | + * Where envVars is null. |
| 76 | + * |
| 77 | + * Expected: |
| 78 | + * IToolChain.getVariables() has items "CC=a/clang.exe", "CXX=a/clang++.exe" |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void clangPlusPlusExe() throws Exception { |
| 82 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang++.exe"), null, null); |
| 83 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 84 | + assertThat(Arrays.asList(variables), hasItems( // |
| 85 | + new EnvironmentVariable("CC", "a/clang.exe"), // |
| 86 | + new EnvironmentVariable("CXX", "a/clang++.exe"))); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Tests: |
| 91 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 92 | + * Where pathToToolChain=a/clang (no extension) |
| 93 | + * Where envVars is null. |
| 94 | + * |
| 95 | + * Expected: |
| 96 | + * IToolChain.getVariables() has items "CC=a/clang", "CXX=a/clang++" |
| 97 | + */ |
| 98 | + @Test |
| 99 | + public void clangNoExt() throws Exception { |
| 100 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang"), null, null); |
| 101 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 102 | + assertThat(Arrays.asList(variables), hasItems( // |
| 103 | + new EnvironmentVariable("CC", "a/clang"), // |
| 104 | + new EnvironmentVariable("CXX", "a/clang++"))); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Tests: |
| 109 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 110 | + * Where pathToToolChain=a/clang++ (no extension) |
| 111 | + * Where envVars is null. |
| 112 | + * |
| 113 | + * Expected: |
| 114 | + * IToolChain.getVariables() has items "CC=a/clang", "CXX=a/clang++" |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void clangPlusPlusNoExt() throws Exception { |
| 118 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang++"), null, null); |
| 119 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 120 | + assertThat(Arrays.asList(variables), hasItems( // |
| 121 | + new EnvironmentVariable("CC", "a/clang"), // |
| 122 | + new EnvironmentVariable("CXX", "a/clang++"))); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Tests: |
| 127 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 128 | + * Where pathToToolChain=a/c++.exe |
| 129 | + * Where envVars is null. |
| 130 | + * |
| 131 | + * Expected: |
| 132 | + * No compiler overrides added. |
| 133 | + * IToolChain.getVariables() has only the PATH item. |
| 134 | + */ |
| 135 | + @Test |
| 136 | + public void cPlusPlusExe() throws Exception { |
| 137 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/c++.exe"), null, null); |
| 138 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 139 | + assertThat(Arrays.asList(variables), |
| 140 | + contains(new EnvironmentVariable("PATH", "a", IEnvironmentVariable.ENVVAR_PREPEND, null))); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Tests: |
| 145 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 146 | + * Where pathToToolChain=a/clang.exe |
| 147 | + * Where envVars has items "CC=CCtestvalue". |
| 148 | + * |
| 149 | + * Expected: |
| 150 | + * IToolChain.getVariables() has items "CC=CCtestvalue", "CXX=a/clang++.exe" |
| 151 | + */ |
| 152 | + @Test |
| 153 | + public void clangExeCCExists() throws Exception { |
| 154 | + IEnvironmentVariable[] envVars = { new EnvironmentVariable("CC", "CCtestvalue") }; |
| 155 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang.exe"), null, envVars); |
| 156 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 157 | + assertThat(Arrays.asList(variables), hasItems( // |
| 158 | + new EnvironmentVariable("CC", "CCtestvalue"), // |
| 159 | + new EnvironmentVariable("CXX", "a/clang++.exe"))); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Tests: |
| 164 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 165 | + * Where pathToToolChain=a/clang++.exe |
| 166 | + * Where envVars has items "CC=CCtestvalue", "CXX=CXXtestvalue". |
| 167 | + * |
| 168 | + * Expected: |
| 169 | + * IToolChain.getVariables() has items "CC=CCtestvalue", "CXX=CXXtestvalue" |
| 170 | + */ |
| 171 | + @Test |
| 172 | + public void clangPPExeCCCXXExists() throws Exception { |
| 173 | + IEnvironmentVariable[] envVars = { new EnvironmentVariable("CC", "CCtestvalue"), |
| 174 | + new EnvironmentVariable("CXX", "CXXtestvalue") }; |
| 175 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang++.exe"), null, envVars); |
| 176 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 177 | + assertThat(Arrays.asList(variables), hasItems( // |
| 178 | + new EnvironmentVariable("CC", "CCtestvalue"), // |
| 179 | + new EnvironmentVariable("CXX", "CXXtestvalue"))); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Tests: |
| 184 | + * ClangToolChain(IToolChainProvider provider, Path pathToToolChain, String arch, IEnvironmentVariable[] envVars) |
| 185 | + * Where pathToToolChain=a/clang |
| 186 | + * Where envVars has items "cc=cctestvalue" (lowercase cc). |
| 187 | + * |
| 188 | + * Expected: |
| 189 | + * envVars has items "CC=CCtestvalue", "CXX=a/clang++.exe" |
| 190 | + * (Windows): IToolChain.getVariables() has items "cc=cctestvalue", "CXX=clang++" |
| 191 | + * (non-Windows): IToolChain.getVariables() has items "cc=cctestvalue", "CC=clang", "CXX=clang++" |
| 192 | + */ |
| 193 | + @Test |
| 194 | + public void clangccExists() throws Exception { |
| 195 | + IEnvironmentVariable[] envVars = { new EnvironmentVariable("cc", "cctestvalue") }; |
| 196 | + IToolChain tc = new ClangToolChain(null, Paths.get("a/clang"), null, envVars); |
| 197 | + IEnvironmentVariable[] variables = tc.getVariables(); |
| 198 | + if (Platform.OS_WIN32.equals(Platform.getOS())) { |
| 199 | + /* |
| 200 | + * Windows: case-insensitive environment; cc is treated the same to CC; CC is not added. |
| 201 | + */ |
| 202 | + assertThat(Arrays.asList(variables), not(hasItem(new EnvironmentVariable("CC", "cctestvalue")))); |
| 203 | + assertThat(Arrays.asList(variables), hasItems( // |
| 204 | + new EnvironmentVariable("cc", "cctestvalue"), // |
| 205 | + new EnvironmentVariable("CXX", "a/clang++"))); |
| 206 | + // the clang override value not applied because the envVar already existed. |
| 207 | + assertThat(Arrays.asList(variables), not(hasItem(new EnvironmentVariable("CC", "a/clang")))); |
| 208 | + } else { |
| 209 | + /* |
| 210 | + * Non-Windows: case-sensitive environment; cc is treated differently to CC; CC is added in addition to any cc. |
| 211 | + */ |
| 212 | + assertThat(Arrays.asList(variables), hasItems(// |
| 213 | + new EnvironmentVariable("cc", "cctestvalue"), // |
| 214 | + new EnvironmentVariable("CC", "a/clang"), // |
| 215 | + new EnvironmentVariable("CXX", "a/clang++"))); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments