aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/AndroidNdkCrosstoolsTest.java
blob: 09c369904d2e45b3effd4b993989f0cc806f57b2 (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
// 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.bazel.rules.android.ndkcrosstools;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r10e.NdkMajorRevisionR10;
import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r12.NdkMajorRevisionR12;
import com.google.devtools.build.lib.events.NullEventHandler;
import com.google.devtools.build.lib.util.ResourceFileLoader;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.DefaultCpuToolchain;
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/** Tests for {@link AndroidNdkCrosstools}. */
@RunWith(Parameterized.class)
public class AndroidNdkCrosstoolsTest {
  private static final String HOST_PLATFORM = "linux-x86_64";
  private static final String REPOSITORY_NAME = "testrepository";

  private static class AndroidNdkCrosstoolsTestParams {
    private final ApiLevel apiLevel;
    private final NdkRelease ndkRelease;
    // ndkfiles.txt contains a list of every file in the ndk, created using this command at the
    // root of the Android NDK for version r10e (64-bit):
    //     find . -xtype f | sed 's|^\./||' | sort
    // and similarly for ndkdirectories, except "-xtype d" is used.
    //
    // It's unfortunate to have files like these, since they're large and brittle, but since the
    // whole NDK can't be checked in to test against, it's about the most that can be done right
    // now.
    private final String ndkFilesFilename;
    private final String ndkDirectoriesFilename;

    AndroidNdkCrosstoolsTestParams(
        ApiLevel apiLevel,
        NdkRelease ndkRelease,
        String ndkFilesFilename,
        String ndkDirectoriesFilename) {
      this.apiLevel = apiLevel;
      this.ndkRelease = ndkRelease;
      this.ndkFilesFilename = ndkFilesFilename;
      this.ndkDirectoriesFilename = ndkDirectoriesFilename;
    }

    ImmutableSet<String> getNdkFiles() throws IOException {
      String ndkFilesFileContent =
          ResourceFileLoader.loadResource(AndroidNdkCrosstoolsTest.class, ndkFilesFilename);
      ImmutableSet.Builder<String> ndkFiles = ImmutableSet.builder();
      for (String line : ndkFilesFileContent.split("\n")) {
        // The contents of the NDK are placed at "external/%repositoryName%/ndk".
        // The "external/%repositoryName%" part is removed using NdkPaths.stripRepositoryPrefix,
        // but to make it easier the "ndk/" part is added here.
        ndkFiles.add("ndk/" + line);
      }
      return ndkFiles.build();
    }

    ImmutableSet<String> getNdkDirectories() throws IOException {
      String ndkFilesFileContent =
          ResourceFileLoader.loadResource(AndroidNdkCrosstoolsTest.class, ndkDirectoriesFilename);
      ImmutableSet.Builder<String> ndkDirectories = ImmutableSet.builder();
      for (String line : ndkFilesFileContent.split("\n")) {
        ndkDirectories.add("ndk/" + line);
      }
      return ndkDirectories.build();
    }
  }

  @Parameters
  public static Collection<AndroidNdkCrosstoolsTestParams[]> data() {
    return ImmutableList.of(
        new AndroidNdkCrosstoolsTestParams[] {
            new AndroidNdkCrosstoolsTestParams(
                new NdkMajorRevisionR10()
                    .apiLevel(NullEventHandler.INSTANCE, REPOSITORY_NAME, "21"),
                NdkRelease.create("r10e (64-bit)"),
                "ndkfiles.txt",
                "ndkdirectories.txt"
            )
        },
        new AndroidNdkCrosstoolsTestParams[] {
            new AndroidNdkCrosstoolsTestParams(
                new NdkMajorRevisionR12()
                    .apiLevel(NullEventHandler.INSTANCE, REPOSITORY_NAME, "21"),
                NdkRelease.create("Pkg.Desc = Android NDK\nPkg.Revision = 12.1.297705\n"),
                "ndk12bfiles.txt",
                "ndk12bdirectories.txt"
            )
        });
  }

  private final ImmutableSet<String> ndkFiles;
  private final ImmutableSet<String> ndkDirectories;
  private final ImmutableList<CrosstoolRelease> crosstoolReleases;
  private final ImmutableMap<String, String> stlFilegroups;

  public AndroidNdkCrosstoolsTest(AndroidNdkCrosstoolsTestParams params) throws IOException {
    // NDK test data is based on the x86 64-bit Linux Android NDK.
    NdkPaths ndkPaths = new NdkPaths(REPOSITORY_NAME, HOST_PLATFORM, params.apiLevel);

    ImmutableList.Builder<CrosstoolRelease> crosstools = ImmutableList.builder();
    ImmutableMap.Builder<String, String> stlFilegroupsBuilder = ImmutableMap.builder();
    for (StlImpl ndkStlImpl : StlImpls.get(ndkPaths)) {
      // Protos are immutable, so this can be shared between tests.
      CrosstoolRelease crosstool =
          AndroidNdkCrosstools.create(params.ndkRelease, ndkPaths, ndkStlImpl, HOST_PLATFORM);
      crosstools.add(crosstool);
      stlFilegroupsBuilder.putAll(ndkStlImpl.getFilegroupNamesAndFilegroupFileGlobPatterns());
    }

    crosstoolReleases = crosstools.build();
    stlFilegroups = stlFilegroupsBuilder.build();
    ndkFiles = params.getNdkFiles();
    ndkDirectories = params.getNdkDirectories();
  }
  
  @Test
  public void testPathsExist() throws Exception {

    for (CrosstoolRelease crosstool : crosstoolReleases) {
      for (CToolchain toolchain : crosstool.getToolchainList()) {
  
        // Test that all tool paths exist.
        for (ToolPath toolpath : toolchain.getToolPathList()) {
          assertThat(ndkFiles).contains(toolpath.getPath());
        }
  
        // Test that all cxx_builtin_include_directory paths exist.
        for (String includeDirectory : toolchain.getCxxBuiltinIncludeDirectoryList()) {
          // Special case for builtin_sysroot.
          if (!includeDirectory.equals("%sysroot%/usr/include")) {
            String path = NdkPaths.stripRepositoryPrefix(includeDirectory);
            assertThat(ndkDirectories).contains(path);
          }
        }
  
        // Test that the builtin_sysroot path exists.
        {
          String builtinSysroot = NdkPaths.stripRepositoryPrefix(toolchain.getBuiltinSysroot());
          assertThat(ndkDirectories).contains(builtinSysroot);
        }
  
        // Test that all include directories added through unfiltered_cxx_flag exist.
        for (String flag : toolchain.getUnfilteredCxxFlagList()) {
          if (!flag.equals("-isystem")) {
            flag = NdkPaths.stripRepositoryPrefix(flag);
            assertThat(ndkDirectories).contains(flag);
          }
        }
      }
    }
  }

  @Test
  public void testStlFilegroupPathsExist() throws Exception {

    for (String fileglob : stlFilegroups.values()) {
      String fileglobNoWildcard = fileglob.substring(0, fileglob.lastIndexOf('/'));
      assertThat(ndkDirectories).contains(fileglobNoWildcard);
      assertThat(findFileByPattern(fileglob)).isTrue();
    }
  }

  private boolean findFileByPattern(String globPattern) {

    String start = globPattern.substring(0, globPattern.indexOf('*'));
    String end = globPattern.substring(globPattern.lastIndexOf('.'));
    for (String f : ndkFiles) {
      if (f.startsWith(start) && f.endsWith(end)) {
        return true;
      }
    }
    return false;
  }

  @Test
  public void testAllToolchainsHaveRuntimesFilegroup() {
    for (CrosstoolRelease crosstool : crosstoolReleases) {
      for (CToolchain toolchain : crosstool.getToolchainList()) {
        assertThat(toolchain.getDynamicRuntimesFilegroup()).isNotEmpty();
        assertThat(toolchain.getStaticRuntimesFilegroup()).isNotEmpty();
      }
    }
  }

  @Test
  public void testDefaultToolchainsExist() {

    for (CrosstoolRelease crosstool : crosstoolReleases) {

      Set<String> toolchainNames = new HashSet<>();
      for (CToolchain toolchain : crosstool.getToolchainList()) {
        toolchainNames.add(toolchain.getToolchainIdentifier());
      }

      for (DefaultCpuToolchain defaultCpuToolchain : crosstool.getDefaultToolchainList()) {
        assertThat(toolchainNames).contains(defaultCpuToolchain.getToolchainIdentifier());
      }
    }
  }

  /**
   * Tests that each (cpu, compiler, glibc) triple in each crosstool is unique in that crosstool.
   */
  @Test
  public void testCrosstoolTriples() {

    StringBuilder errorBuilder = new StringBuilder();
    for (CrosstoolRelease crosstool : crosstoolReleases) {

      // Create a map of (cpu, compiler, glibc) triples -> toolchain.
      ImmutableMultimap.Builder<String, CToolchain> triples = ImmutableMultimap.builder();
      for (CToolchain toolchain : crosstool.getToolchainList()) {
        String triple = "(" + Joiner.on(", ").join(
            toolchain.getTargetCpu(),
            toolchain.getCompiler(),
            toolchain.getTargetLibc()) + ")";
        triples.put(triple, toolchain);
      }

      // Collect all the duplicate triples.
      for (Entry<String, Collection<CToolchain>> entry : triples.build().asMap().entrySet()) {
        if (entry.getValue().size() > 1) {
          errorBuilder.append(entry.getKey() + ": " + Joiner.on(", ").join(
              Collections2.transform(entry.getValue(), new Function<CToolchain, String>() {
                @Override public String apply(CToolchain toolchain) {
                  return toolchain.getToolchainIdentifier();
                }
              })));
          errorBuilder.append("\n");
        }
      }
      errorBuilder.append("\n");
    }

    // This is a rather awkward condition to test on, but collecting all the duplicates first is
    // the only way to make a useful error message rather than finding the errors one by one.
    String error = errorBuilder.toString().trim();
    if (!error.isEmpty()) {
      fail("Toolchains contain duplicate (cpu, compiler, glibc) triples:\n" + error);
    }
  }
}