aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationTest.java
blob: e88989108c6765c318b75e584eb3b42fba339ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.analysis.config;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment;
import com.google.devtools.build.lib.analysis.util.ConfigurationTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.rules.cpp.CppConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppOptions;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
import com.google.devtools.common.options.Options;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * Tests for {@link BuildConfiguration}.
 */
@RunWith(JUnit4.class)
public class BuildConfigurationTest extends ConfigurationTestCase {

  @Test
  public void testBasics() throws Exception {
    if (TestConstants.THIS_IS_BAZEL) {
      return;
    }

    BuildConfiguration config = create("--cpu=piii");
    String outputDirPrefix = outputBase
        + "/workspace/blaze-out/gcc-4.4.0-glibc-2.3.6-grte-piii-fastbuild";

    assertEquals(outputDirPrefix,
                 config.getOutputDirectory().getPath().toString());
    assertEquals(outputDirPrefix + "/bin",
                 config.getBinDirectory().getPath().toString());
    assertEquals(outputDirPrefix + "/include",
                 config.getIncludeDirectory().getPath().toString());
    assertEquals(outputDirPrefix + "/genfiles",
                 config.getGenfilesDirectory().getPath().toString());
    assertEquals(outputDirPrefix + "/testlogs",
                 config.getTestLogsDirectory().getPath().toString());
  }

  @Test
  public void testPlatformSuffix() throws Exception {
    if (TestConstants.THIS_IS_BAZEL) {
      return;
    }

    BuildConfiguration config = create("--platform_suffix=-test");
    assertEquals(outputBase + "/workspace/blaze-out/gcc-4.4.0-glibc-2.3.6-grte-k8-fastbuild-test",
        config.getOutputDirectory().getPath().toString());
  }

  @Test
  public void testEnvironment() throws Exception {
    if (TestConstants.THIS_IS_BAZEL) {
      return;
    }

    Map<String, String> env = create().getDefaultShellEnvironment();
    assertThat(env).containsEntry("LANG", "en_US");
    assertThat(env).containsKey("PATH");
    assertThat(env.get("PATH")).contains("/bin:/usr/bin");
    try {
      env.put("FOO", "bar");
      fail("modifiable default environment");
    } catch (UnsupportedOperationException ignored) {
      //expected exception
    }
  }

  @Test
  public void testHostCpu() throws Exception {
    for (String cpu : new String[] { "piii", "k8" }) {
      BuildConfiguration hostConfig = createHost("--host_cpu=" + cpu);
      assertEquals(cpu, hostConfig.getFragment(CppConfiguration.class).getTargetCpu());
    }
  }

  @Test
  public void testHostCrosstoolTop() throws Exception {
    if (TestConstants.THIS_IS_BAZEL) {
      return;
    }

    BuildConfigurationCollection configs = createCollection("--cpu=piii");
    BuildConfiguration config = Iterables.getOnlyElement(configs.getTargetConfigurations());
    assertEquals(Label.parseAbsoluteUnchecked("//third_party/crosstool/mock:cc-compiler-piii"),
        config.getFragment(CppConfiguration.class).getCcToolchainRuleLabel());

    BuildConfiguration hostConfig = configs.getHostConfiguration();
    assertEquals(Label.parseAbsoluteUnchecked("//third_party/crosstool/mock:cc-compiler-k8"),
        hostConfig.getFragment(CppConfiguration.class).getCcToolchainRuleLabel());
  }

  @Test
  public void testMakeEnvFlags() throws Exception {
    BuildConfiguration config = create();
    assertThat(config.getMakeEnvironment().get("STRIP")).contains("strip");
  }

  @Test
  public void testCaching() throws Exception {
    BuildConfiguration.Options a = Options.getDefaults(BuildConfiguration.Options.class);
    BuildConfiguration.Options b = Options.getDefaults(BuildConfiguration.Options.class);
    // The String representations of the BuildConfiguration.Options must be equal even if these are
    // different objects, if they were created with the same options (no options in this case).
    assertEquals(a.toString(), b.toString());
    assertEquals(a.cacheKey(), b.cacheKey());
  }

  private void checkInvalidCpuError(String cpuOption, Pattern messageRegex) throws Exception {
    try {
      create("--" + cpuOption + "=bogus");
      fail();
    } catch (InvalidConfigurationException e) {
      assertThat(e.getMessage()).matches(messageRegex);
    }
  }

  @Test
  public void testInvalidCpu() throws Exception {
    checkInvalidCpuError("cpu", Pattern.compile(
        "No toolchain found for cpu 'bogus'. Valid cpus are: \\[\n(  [\\w-]+,\n)+]"));
  }

  @Test
  public void testConfigurationsHaveUniqueOutputDirectories() throws Exception {
    assertConfigurationsHaveUniqueOutputDirectories(createCollection());
    assertConfigurationsHaveUniqueOutputDirectories(createCollection("--compilation_mode=opt"));
  }

  @Test
  public void testMultiCpu() throws Exception {
    if (TestConstants.THIS_IS_BAZEL) {
      return;
    }

    BuildConfigurationCollection master = createCollection("--multi_cpu=k8", "--multi_cpu=piii");
    assertThat(master.getTargetConfigurations()).hasSize(2);
    // Note: the cpus are sorted alphabetically.
    assertEquals("k8", master.getTargetConfigurations().get(0).getCpu());
    assertEquals("piii", master.getTargetConfigurations().get(1).getCpu());
  }

  /**
   * Check that the cpus are sorted alphabetically regardless of the order in which they are
   * specified.
   */
  @Test
  public void testMultiCpuSorting() throws Exception {
    if (TestConstants.THIS_IS_BAZEL) {
      return;
    }

    for (int order = 0; order < 2; order++) {
      BuildConfigurationCollection master;
      if (order == 0) {
        master = createCollection("--multi_cpu=k8", "--multi_cpu=piii");
      } else {
        master = createCollection("--multi_cpu=piii", "--multi_cpu=k8");
      }
      assertThat(master.getTargetConfigurations()).hasSize(2);
      assertEquals("k8", master.getTargetConfigurations().get(0).getCpu());
      assertEquals("piii", master.getTargetConfigurations().get(1).getCpu());
    }
  }

  @Test
  public void testTargetEnvironment() throws Exception {
    BuildConfiguration oneEnvConfig = create("--target_environment=//foo");
    assertThat(oneEnvConfig.getTargetEnvironments()).containsExactly(Label.parseAbsolute("//foo"));

    BuildConfiguration twoEnvsConfig =
        create("--target_environment=//foo", "--target_environment=//bar");
    assertThat(twoEnvsConfig.getTargetEnvironments())
        .containsExactly(Label.parseAbsolute("//foo"), Label.parseAbsolute("//bar"));

    BuildConfiguration noEnvsConfig = create();
    assertThat(noEnvsConfig.getTargetEnvironments()).isEmpty();
  }

  @SafeVarargs
  @SuppressWarnings("unchecked")
  private final ConfigurationFragmentFactory createMockFragment(
      final Class<? extends Fragment> creates, final Class<? extends Fragment>... dependsOn) {
    return new ConfigurationFragmentFactory() {

      @Override
      public Class<? extends Fragment> creates() {
        return creates;
      }

      @Override
      public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
        return ImmutableSet.of();
      }

      @Override
      public Fragment create(ConfigurationEnvironment env, BuildOptions buildOptions)
          throws InvalidConfigurationException {
        for (Class<? extends Fragment> fragmentType : dependsOn) {
          env.getFragment(buildOptions, fragmentType);
        }
        return new Fragment() {

        };
      }
    };
  }

  @Test
  public void testCycleInFragments() throws Exception {
    configurationFactory = new ConfigurationFactory(
        getAnalysisMock().createConfigurationCollectionFactory(),
        createMockFragment(CppConfiguration.class, JavaConfiguration.class),
        createMockFragment(JavaConfiguration.class, CppConfiguration.class));
    try {
      createCollection();
      fail();
    } catch (IllegalStateException e) {
      // expected
    }
  }

  @Test
  public void testMissingFragment() throws Exception {
    configurationFactory = new ConfigurationFactory(
        getAnalysisMock().createConfigurationCollectionFactory(),
        createMockFragment(CppConfiguration.class, JavaConfiguration.class));
    try {
      createCollection();
      fail();
    } catch (RuntimeException e) {
      // expected
    }
  }

  @Test
  public void testGlobalMakeVariableOverride() throws Exception {
    assertThat(create().getMakeEnvironment()).containsEntry("COMPILATION_MODE", "fastbuild");
    BuildConfiguration config = create("--define", "COMPILATION_MODE=fluttershy");
    assertThat(config.getMakeEnvironment()).containsEntry("COMPILATION_MODE", "fluttershy");
  }

  @Test
  public void testGetOptionClass() throws Exception {
    BuildConfiguration config = create();
    // Directly defined option:
    assertEquals(BuildConfiguration.Options.class, config.getOptionClass("compilation_mode"));
    // Option defined in a fragment:
    assertEquals(CppOptions.class, config.getOptionClass("lipo"));
    // Unrecognized option:
    assertNull(config.getOptionClass("do_my_laundry"));
  }

  @Test
  public void testGetOptionValue() throws Exception {
    // Directly defined options:
    assertEquals(CompilationMode.DBG, create("-c", "dbg").getOptionValue("compilation_mode"));
    assertEquals(CompilationMode.OPT, create("-c", "opt").getOptionValue("compilation_mode"));

    // Options defined in a fragment:
    assertEquals(Boolean.TRUE, create("--cc_include_scanning")
        .getOptionValue("cc_include_scanning"));
    assertEquals(Boolean.FALSE, create("--nocc_include_scanning")
        .getOptionValue("cc_include_scanning"));

    // Unrecognized option:
    assertNull(create().getOptionValue("do_my_dishes"));

    // Legitimately null option:
    assertNull(create().getOptionValue("test_filter"));
  }

  @Test
  public void testNoDistinctHostConfigurationUnsupportedWithDynamicConfigs() throws Exception {
    checkError(
        "--nodistinct_host_configuration does not currently work with dynamic configurations",
        "--nodistinct_host_configuration", "--experimental_dynamic_configs");
  }

  @Test
  public void testEqualsOrIsSupersetOf() throws Exception {
    BuildConfiguration config = create();
    BuildConfiguration trimmedConfig = config.clone(
        ImmutableSet.<Class<? extends Fragment>>of(CppConfiguration.class),
        TestRuleClassProvider.getRuleClassProvider());
    BuildConfiguration hostConfig = createHost();

    assertTrue(config.equalsOrIsSupersetOf(trimmedConfig));
    assertFalse(config.equalsOrIsSupersetOf(hostConfig));
    assertFalse(trimmedConfig.equalsOrIsSupersetOf(config));
  }
}