aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/objc/ProtoSupport.java
blob: 79be67af49e0ff4243a909c9595255a06d05c055 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// 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.rules.objc;

import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.PrerequisiteArtifacts;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.FileType;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.util.ArrayList;

import javax.annotation.Nullable;

/**
 * Support for generating Objective C proto static libraries that registers actions that generate
 * the Objective C protos and validates the rules' attributes.
 *
 * This ProtoSupport class supports 2 protocol buffer compilers, named ProtocolBuffers2 and the
 * open-sourced version named protobuf. When refering to a specific library, the naming will either
 * refer to PB2 (for ProtocolBuffers2) or protobuf (the open-source version). When the context is
 * independent of the library, the naming will just refer to "proto". The selection of which proto
 * library to use depends on the presence of the 'portable_proto_filters' rule attribute.
 *
 * Keep in mind that these libraries are independent of the proto syntax used. ProtocolBuffers2
 * supports proto2 syntax, but the protobuf library supports both proto2 and proto3 syntax.
 *
 * <p>Methods on this class can be called in any order without impacting the result.
 */
final class ProtoSupport {
  private static final PathFragment BAZEL_TOOLS_PREFIX = new PathFragment("external/bazel_tools/");

  private static final Function<Artifact, PathFragment> PARENT_PATHFRAGMENT =
      new Function<Artifact, PathFragment>() {
        @Override
        public PathFragment apply(Artifact input) {
          return input.getExecPath().getParentDirectory();
        }
      };

  /**
   * Type of target that is generating the protos. There is a distinction because when generating
   * the protos from an objc_proto_library, the attributes of the rule need to be considered, but
   * when generating from a linking target (e.g. objc_binary), the attributes don't exist, but the
   * ObjcProtoProviders need to be checked.
   */
  enum TargetType {
    /**
     * The generating rule is an objc_proto_library rule.
     */
    PROTO_TARGET,

    /**
     * The generating target is a linking rule, which generates, compiles and links all the protos
     * in the transitive closure of dependencies.
     */
    LINKING_TARGET,
  }

  @VisibleForTesting
  static final String NO_PROTOS_ERROR =
      "no protos to compile - a non-empty deps attribute is required";

  @VisibleForTesting
  static final String FILES_DEPRECATED_WARNING =
      "Using files and filegroups in objc_proto_library is deprecated";

  @VisibleForTesting
  static final String PORTABLE_PROTO_FILTERS_NOT_EXCLUSIVE_ERROR =
      "The portable_proto_filters attribute is incompatible with the options_file, output_cpp, "
          + "per_proto_includes and use_objc_header_names attributes.";

  @VisibleForTesting
  static final String PORTABLE_PROTO_FILTERS_EMPTY_ERROR =
      "The portable_proto_filters attribute can't be empty";

  private static final String UNIQUE_DIRECTORY_NAME = "_generated_protos";

  /**
   * List of file name segments that should be upper cased when being generated. More information
   * available in the generateProtobufFilename() method.
   */
  private static final ImmutableSet<String> UPPERCASE_SEGMENTS =
      ImmutableSet.of("url", "http", "https");

  private final RuleContext ruleContext;
  private final Attributes attributes;
  private final TargetType targetType;
  private final IntermediateArtifacts intermediateArtifacts;

  /**
   * Creates a new proto support.
   *
   * @param ruleContext context this proto library is constructed in
   * @param targetType the type of target generating the protos
   */
  public ProtoSupport(RuleContext ruleContext, TargetType targetType) {
    this.ruleContext = ruleContext;
    this.attributes = new Attributes(ruleContext);
    this.targetType = targetType;
    if (targetType != TargetType.PROTO_TARGET) {
      // Use a a prefixed version of the intermediate artifacts to avoid naming collisions, as
      // the proto compilation step happens in the same context as the linking target.
      this.intermediateArtifacts =
          new IntermediateArtifacts(
              ruleContext, "_protos", "protos", ruleContext.getConfiguration());
    } else {
      this.intermediateArtifacts = ObjcRuleClasses.intermediateArtifacts(ruleContext);
    }
  }

  /**
   * Validates proto support.
   * <ul>
   * <li>Validates that there are protos specified to be compiled.
   * <li>Validates that, when enabling the open source protobuf library, the options for the PB2
   *     are not specified also.
   * <li>Validates that, when enabling the open source protobuf library, the rule specifies at least
   *     one portable proto filter file.
   * </ul>
   *
   * @return this proto support
   */
  public ProtoSupport validate() {
    if (attributes.getProtoFiles().isEmpty()) {
      ruleContext.ruleError(NO_PROTOS_ERROR);
    }

    if (usesProtobufLibrary()) {
      if (attributes.getPortableProtoFilters().isEmpty()) {
        ruleContext.ruleError(PORTABLE_PROTO_FILTERS_EMPTY_ERROR);
      }

      if (attributes.outputsCpp()
          || attributes.usesObjcHeaderNames()
          || attributes.needsPerProtoIncludes()
          || attributes.getOptionsFile() != null) {
        ruleContext.ruleError(PORTABLE_PROTO_FILTERS_NOT_EXCLUSIVE_ERROR);
      }
    } else {
      if (attributes.outputsCpp()) {
        ruleContext.ruleWarning("The output_cpp attributes has been deprecated. Please "
            + "refer to b/29342376 for information on possible alternatives");
      }
    }
    return this;
  }

  /**
   * Returns the intermediate artifacts associated with generated proto compilation.
   */
  public IntermediateArtifacts getIntermediateArtifacts() {
    return intermediateArtifacts;
  }

  /**
   * Registers actions required for compiling the proto files.
   *
   * @return this proto support
   */
  public ProtoSupport registerActions() {
    if (!Iterables.isEmpty(getFilteredProtoSources())) {
      registerProtoInputListFileAction();
      registerGenerateProtoFilesAction();
    }
    return this;
  }

  /**
   * Returns the common object for a proto specific compilation environment.
   */
  public ObjcCommon getCommon() {
    ObjcCommon.Builder commonBuilder =
        new ObjcCommon.Builder(ruleContext)
            .setIntermediateArtifacts(intermediateArtifacts)
            .setHasModuleMap()
            .setCompilationArtifacts(getCompilationArtifacts());

    if (targetType == TargetType.LINKING_TARGET) {
      commonBuilder.addDepObjcProviders(
          ruleContext.getPrerequisites("deps", Mode.TARGET, ObjcProvider.class));
    } else if (targetType == TargetType.PROTO_TARGET) {
      commonBuilder.addDepObjcProviders(
          ruleContext.getPrerequisites(
              ObjcRuleClasses.PROTO_LIB_ATTR, Mode.TARGET, ObjcProvider.class));

      if (usesProtobufLibrary() && experimentalAutoUnion()) {
        commonBuilder.addDirectDependencyHeaderSearchPaths(getUserHeaderSearchPaths());
      } else {
        commonBuilder.addUserHeaderSearchPaths(getUserHeaderSearchPaths());
      }
    }
    return commonBuilder.build();
  }

  /**
   * Adds required configuration to the XcodeProvider support class for proto compilation.
   *
   * @param xcodeProviderBuilder The builder for the XcodeProvider support class.
   * @return this proto support
   */
  public ProtoSupport addXcodeProviderOptions(XcodeProvider.Builder xcodeProviderBuilder) {
    xcodeProviderBuilder
        .addUserHeaderSearchPaths(getUserHeaderSearchPaths())
        .addCopts(ObjcRuleClasses.objcConfiguration(ruleContext).getCopts())
        .addHeaders(getGeneratedHeaders())
        .setCompilationArtifacts(getCompilationArtifacts());
    return this;
  }

  /**
   * Adds the files needed to be built by the rule.
   *
   * @param filesToBuild An aggregated set of the files to be built by the rule.
   * @return this proto support
   */
  public ProtoSupport addFilesToBuild(NestedSetBuilder<Artifact> filesToBuild) {
    filesToBuild.addAll(getGeneratedSources()).addAll(getGeneratedHeaders());
    return this;
  }

  /**
   * Returns the proto compilation artifacts for the current rule.
   */
  public CompilationArtifacts getCompilationArtifacts() {
    ImmutableList<Artifact> generatedSources = getGeneratedSources();
    CompilationArtifacts.Builder builder =
        new CompilationArtifacts.Builder()
            .setIntermediateArtifacts(intermediateArtifacts)
            .setPchFile(Optional.<Artifact>absent())
            .addAdditionalHdrs(getGeneratedHeaders());

    if (targetType == TargetType.PROTO_TARGET && !usesProtobufLibrary()) {
      builder.addAdditionalHdrs(generatedSources);
    }

    if (experimentalAutoUnion()) {
      if (targetType == TargetType.PROTO_TARGET && !usesProtobufLibrary()
          || targetType == TargetType.LINKING_TARGET) {
        builder.addNonArcSrcs(generatedSources);
      }
    } else {
      if (targetType == TargetType.PROTO_TARGET) {
        builder.addNonArcSrcs(generatedSources);
      }
    }

    return builder.build();
  }

  /**
   * Returns the include paths for the generated protos.
   */
  public ImmutableSet<PathFragment> getUserHeaderSearchPaths() {
    PathFragment workspaceRelativeOutputDir = getWorkspaceRelativeOutputDir();

    ImmutableSet.Builder<PathFragment> searchPathEntriesBuilder =
        new ImmutableSet.Builder<PathFragment>().add(workspaceRelativeOutputDir);

    if (attributes.needsPerProtoIncludes()) {
      PathFragment generatedProtoDir =
          new PathFragment(workspaceRelativeOutputDir, ruleContext.getLabel().getPackageFragment());

      searchPathEntriesBuilder
          .add(generatedProtoDir)
          .addAll(Iterables.transform(getGeneratedHeaders(), PARENT_PATHFRAGMENT));
    }

    return searchPathEntriesBuilder.build();
  }

  /**
   * Returns whether the rule is configured to use the protobuf library.
   */
  public boolean usesProtobufLibrary() {
    return attributes.hasPortableProtoFilters() || targetType == TargetType.LINKING_TARGET;
  }

  private Iterable<Artifact> getAllProtoSources() {
    NestedSetBuilder<Artifact> protos = NestedSetBuilder.stableOrder();

    if (experimentalAutoUnion() && targetType == TargetType.LINKING_TARGET) {
      Iterable<ObjcProtoProvider> objcProtoProviders =
          ruleContext.getPrerequisites("deps", Mode.TARGET, ObjcProtoProvider.class);
      for (ObjcProtoProvider objcProtoProvider : objcProtoProviders) {
        protos.addTransitive(objcProtoProvider.getProtoSources());
      }
    }

    protos.addTransitive(attributes.getProtoFiles());

    return protos.build();
  }

  private Iterable<Artifact> getFilteredProtoSources() {
    // Transform the well known proto artifacts by removing the external/bazel_tools prefix if
    // present. Otherwise the comparison for filtering out the well known types is not possible.
    ImmutableSet.Builder<PathFragment> wellKnownProtoPathsBuilder = new ImmutableSet.Builder<>();
    for (Artifact wellKnownProto : attributes.getWellKnownTypeProtos()) {
      PathFragment execPath = wellKnownProto.getExecPath();
      if (execPath.startsWith(BAZEL_TOOLS_PREFIX)) {
        wellKnownProtoPathsBuilder.add(execPath.relativeTo(BAZEL_TOOLS_PREFIX));
      } else {
        wellKnownProtoPathsBuilder.add(execPath);
      }
    }

    ImmutableSet<PathFragment> wellKnownProtoPaths = wellKnownProtoPathsBuilder.build();

    // Filter out the well known types from being sent to be generated, as these protos have already
    // been generated and linked in libprotobuf.a.
    ImmutableSet.Builder<Artifact> filteredProtos = new ImmutableSet.Builder<>();
    for (Artifact proto : getAllProtoSources()) {
      if (!wellKnownProtoPaths.contains(proto.getExecPath())) {
        filteredProtos.add(proto);
      }
    }

    return filteredProtos.build();
  }

  private NestedSet<Artifact> getPortableProtoFilters() {
    NestedSetBuilder<Artifact> portableProtoFilters = NestedSetBuilder.stableOrder();

    if (experimentalAutoUnion() && targetType == TargetType.LINKING_TARGET) {
      Iterable<ObjcProtoProvider> objcProtoProviders =
          ruleContext.getPrerequisites("deps", Mode.TARGET, ObjcProtoProvider.class);
      for (ObjcProtoProvider objcProtoProvider : objcProtoProviders) {
        portableProtoFilters.addTransitive(objcProtoProvider.getPortableProtoFilters());
      }
    }

    portableProtoFilters.addAll(attributes.getPortableProtoFilters());
    return portableProtoFilters.build();
  }

  private boolean experimentalAutoUnion() {
    ObjcConfiguration objcConfiguration = ObjcRuleClasses.objcConfiguration(ruleContext);
    return objcConfiguration.experimentalAutoTopLevelUnionObjCProtos();
  }

  private void registerProtoInputListFileAction() {
    ruleContext.registerAction(
        new FileWriteAction(
            ruleContext.getActionOwner(),
            getProtoInputListFile(),
            getProtoInputListFileContents(),
            false));
  }

  private void registerGenerateProtoFilesAction() {
    ruleContext.registerAction(
        ObjcRuleClasses.spawnOnDarwinActionBuilder()
            .setMnemonic("GenObjcProtos")
            .addInputs(getGenerateActionInputs())
            .addOutputs(getGenerateActionOutputs())
            .setExecutable(new PathFragment("/usr/bin/python"))
            .setCommandLine(getGenerateCommandLine())
            .build(ruleContext));
  }

  private Artifact getProtoInputListFile() {
    return ruleContext.getUniqueDirectoryArtifact(
        "_protos", "_proto_input_files", ruleContext.getConfiguration().getGenfilesDirectory());
  }

  private String getProtoInputListFileContents() {
    return Artifact.joinExecPaths("\n", getFilteredProtoSources());
  }

  private PathFragment getWorkspaceRelativeOutputDir() {
    // Generate sources in a package-and-rule-scoped directory; adds both the
    // package-and-rule-scoped directory and the header-containing-directory to the include path
    // of dependers.
    PathFragment rootRelativeOutputDir = ruleContext.getUniqueDirectory(UNIQUE_DIRECTORY_NAME);

    return new PathFragment(
        ruleContext.getBinOrGenfilesDirectory().getExecPath(), rootRelativeOutputDir);
  }

  private ImmutableList<Artifact> getGeneratedHeaders() {
    boolean useObjcName = attributes.usesObjcHeaderNames() || usesProtobufLibrary();
    return generatedOutputArtifacts(FileType.of(".pb" + (useObjcName ? "objc.h" : ".h")));
  }

  private ImmutableList<Artifact> getGeneratedSources() {
    return generatedOutputArtifacts(
        FileType.of(
            ".pb"
                + (usesProtobufLibrary() ? "objc" : "")
                + (attributes.outputsCpp() ? ".cc" : ".m")));
  }

  private NestedSet<Artifact> getGenerateActionInputs() {
    NestedSetBuilder<Artifact> inputsBuilder =
        NestedSetBuilder.<Artifact>stableOrder()
            .add(attributes.getProtoCompiler())
            .addAll(getAllProtoSources())
            .add(getProtoInputListFile())
            .addAll(attributes.getProtoCompilerSupport())
            .addAll(getPortableProtoFilters());

    Artifact optionsFile = attributes.getOptionsFile();
    if (optionsFile != null) {
      inputsBuilder.add(optionsFile);
    }

    return inputsBuilder.build();
  }

  private Iterable<Artifact> getGenerateActionOutputs() {
    return Iterables.concat(getGeneratedHeaders(), getGeneratedSources());
  }

  private CustomCommandLine getGenerateCommandLine() {
    if (usesProtobufLibrary()) {
      return getProtobufCommandLine();
    } else {
      return getPb2CommandLine();
    }
  }

  private CustomCommandLine getPb2CommandLine() {
    CustomCommandLine.Builder commandLineBuilder =
        new CustomCommandLine.Builder()
            .add(attributes.getProtoCompiler().getExecPathString())
            .add("--input-file-list")
            .add(getProtoInputListFile().getExecPathString())
            .add("--output-dir")
            .add(getWorkspaceRelativeOutputDir().getSafePathString())
            .add("--working-dir")
            .add(".");

    if (attributes.getOptionsFile() != null) {
      commandLineBuilder
          .add("--compiler-options-path")
          .add(attributes.getOptionsFile().getExecPathString());
    }

    if (attributes.outputsCpp()) {
      commandLineBuilder.add("--generate-cpp");
    }

    if (attributes.usesObjcHeaderNames()) {
      commandLineBuilder.add("--use-objc-header-names");
    }
    return commandLineBuilder.build();
  }

  private CustomCommandLine getProtobufCommandLine() {
    CustomCommandLine.Builder commandLineBuilder =
        new CustomCommandLine.Builder()
            .add(attributes.getProtoCompiler().getExecPathString())
            .add("--input-file-list")
            .add(getProtoInputListFile().getExecPathString())
            .add("--output-dir")
            .add(getWorkspaceRelativeOutputDir().getSafePathString())
            .add("--force")
            .add("--proto-root-dir")
            .add(".");

    boolean configAdded = false;
    for (Artifact portableProtoFilter : getPortableProtoFilters()) {
      String configFlag;
      if (!configAdded) {
        configFlag = "--config";
        configAdded = true;
      } else {
        configFlag = "--extra-filter-config";
      }

      commandLineBuilder.add(configFlag).add(portableProtoFilter.getExecPathString());
    }
    return commandLineBuilder.build();
  }

  private ImmutableList<Artifact> generatedOutputArtifacts(FileType newFileType) {
    ImmutableList.Builder<Artifact> builder = new ImmutableList.Builder<>();
    for (Artifact protoFile : getFilteredProtoSources()) {
      String protoFileName = FileSystemUtils.removeExtension(protoFile.getFilename());
      String generatedOutputName;
      if (attributes.outputsCpp()) {
        generatedOutputName = protoFileName;
      } else if (usesProtobufLibrary()) {
        // The protobuf library generates filenames with some slight modifications.
        generatedOutputName = generateProtobufFilename(protoFileName);
      } else {
        String lowerUnderscoreBaseName = protoFileName.replace('-', '_').toLowerCase();
        generatedOutputName = LOWER_UNDERSCORE.to(UPPER_CAMEL, lowerUnderscoreBaseName);
      }

      PathFragment generatedFilePath =
          new PathFragment(
              protoFile.getRootRelativePath().getParentDirectory(),
              new PathFragment(generatedOutputName));

      PathFragment outputFile =
          FileSystemUtils.appendExtension(
              generatedFilePath, newFileType.getExtensions().get(0));

      if (outputFile != null) {
        builder.add(
            ruleContext.getUniqueDirectoryArtifact(
                UNIQUE_DIRECTORY_NAME, outputFile, ruleContext.getBinOrGenfilesDirectory()));
      }
    }
    return builder.build();
  }

  /**
   * Processes the case of the proto file name in the same fashion as the objective_c generator's
   * UnderscoresToCamelCase function.
   *
   * https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc
   */
  private String generateProtobufFilename(String protoFilename) {
    boolean lastCharWasDigit = false;
    boolean lastCharWasUpper = false;
    boolean lastCharWasLower = false;

    StringBuilder currentSegment = new StringBuilder();

    ArrayList<String> segments = new ArrayList<>();

    for (int i = 0; i < protoFilename.length(); i++) {
      char currentChar = protoFilename.charAt(i);
      if (CharMatcher.javaDigit().matches(currentChar)) {
        if (!lastCharWasDigit) {
          segments.add(currentSegment.toString());
          currentSegment = new StringBuilder();
        }
        currentSegment.append(currentChar);
        lastCharWasDigit = true;
        lastCharWasUpper = false;
        lastCharWasLower = false;
      } else if (CharMatcher.javaLowerCase().matches(currentChar)) {
        if (!lastCharWasLower && !lastCharWasUpper) {
          segments.add(currentSegment.toString());
          currentSegment = new StringBuilder();
        }
        currentSegment.append(currentChar);
        lastCharWasDigit = false;
        lastCharWasUpper = false;
        lastCharWasLower = true;
      } else if (CharMatcher.javaUpperCase().matches(currentChar)) {
        if (!lastCharWasUpper) {
          segments.add(currentSegment.toString());
          currentSegment = new StringBuilder();
        }
        currentSegment.append(Character.toLowerCase(currentChar));
        lastCharWasDigit = false;
        lastCharWasUpper = true;
        lastCharWasLower = false;
      } else {
        lastCharWasDigit = false;
        lastCharWasUpper = false;
        lastCharWasLower = false;
      }
    }

    segments.add(currentSegment.toString());

    StringBuilder casedSegments = new StringBuilder();
    for (String segment : segments) {
      if (UPPERCASE_SEGMENTS.contains(segment)) {
        casedSegments.append(segment.toUpperCase());
      } else {
        casedSegments.append(LOWER_UNDERSCORE.to(UPPER_CAMEL, segment));
      }
    }
    return casedSegments.toString();
  }

  /**
   * Common rule attributes used by an Objective C proto library.
   */
  private static class Attributes {
    private final RuleContext ruleContext;

    private Attributes(RuleContext ruleContext) {
      this.ruleContext = ruleContext;
    }

    /**
     * Returns whether the generated files should be C++ or Objective C.
     */
    boolean outputsCpp() {
      if (ruleContext.attributes().has(ObjcProtoLibraryRule.OUTPUT_CPP_ATTR, Type.BOOLEAN)) {
        return ruleContext.attributes().get(ObjcProtoLibraryRule.OUTPUT_CPP_ATTR, Type.BOOLEAN);
      }
      return false;
    }

    /**
     * Returns whether the generated header files should have be of type pb.h or pbobjc.h.
     */
    boolean usesObjcHeaderNames() {
      if (ruleContext
          .attributes()
          .has(ObjcProtoLibraryRule.USE_OBJC_HEADER_NAMES_ATTR, Type.BOOLEAN)) {
        return ruleContext
            .attributes()
            .get(ObjcProtoLibraryRule.USE_OBJC_HEADER_NAMES_ATTR, Type.BOOLEAN);
      }
      return false;
    }

    /**
     * Returns whether the includes should include each of the proto generated headers.
     */
    boolean needsPerProtoIncludes() {
      if (ruleContext
          .attributes()
          .has(ObjcProtoLibraryRule.PER_PROTO_INCLUDES_ATTR, Type.BOOLEAN)) {
        return ruleContext
            .attributes()
            .get(ObjcProtoLibraryRule.PER_PROTO_INCLUDES_ATTR, Type.BOOLEAN);
      }
      return false;
    }

    /**
     * Returns whether to use the protobuf library instead of the PB2 library.
     */
    boolean hasPortableProtoFilters() {
      return ruleContext
          .attributes()
          .isAttributeValueExplicitlySpecified(ObjcProtoLibraryRule.PORTABLE_PROTO_FILTERS_ATTR);
    }

    /**
     * Returns the list of portable proto filters.
     */
    ImmutableList<Artifact> getPortableProtoFilters() {
      if (ruleContext
          .attributes()
          .has(ObjcProtoLibraryRule.PORTABLE_PROTO_FILTERS_ATTR, LABEL_LIST)) {
        return ruleContext
            .getPrerequisiteArtifacts(ObjcProtoLibraryRule.PORTABLE_PROTO_FILTERS_ATTR, Mode.HOST)
            .list();
      }
      return ImmutableList.of();
    }

    /**
     * Returns the list of well known type protos.
     */
    ImmutableList<Artifact> getWellKnownTypeProtos() {
      return ruleContext
          .getPrerequisiteArtifacts(ObjcRuleClasses.PROTOBUF_WELL_KNOWN_TYPES, Mode.HOST)
          .list();
    }

    /**
     * Returns the options file, or null if it was not specified.
     */
    @Nullable
    Artifact getOptionsFile() {
      if (ruleContext.attributes().has(ObjcProtoLibraryRule.OPTIONS_FILE_ATTR, LABEL)) {
        return ruleContext.getPrerequisiteArtifact(
            ObjcProtoLibraryRule.OPTIONS_FILE_ATTR, Mode.HOST);
      }
      return null;
    }

    /**
     * Returns the proto compiler to be used.
     */
    Artifact getProtoCompiler() {
      return ruleContext.getPrerequisiteArtifact(ObjcRuleClasses.PROTO_COMPILER_ATTR, Mode.HOST);
    }

    /**
     * Returns the list of files needed by the proto compiler.
     */
    ImmutableList<Artifact> getProtoCompilerSupport() {
      return ruleContext
          .getPrerequisiteArtifacts(ObjcRuleClasses.PROTO_COMPILER_SUPPORT_ATTR, Mode.HOST)
          .list();
    }

    /**
     * Returns the list of proto files to compile.
     */
    NestedSet<Artifact> getProtoFiles() {
      return NestedSetBuilder.<Artifact>stableOrder()
          .addAll(getProtoDepsFiles())
          .addTransitive(getProtoDepsSources())
          .build();
    }

    /**
     * Returns the list of proto files that were added directly into the deps attributes. This way
     * of specifying the protos is deprecated, and displays a warning when the target does so.
     */
    private ImmutableList<Artifact> getProtoDepsFiles() {
      PrerequisiteArtifacts prerequisiteArtifacts =
          ruleContext.getPrerequisiteArtifacts("deps", Mode.TARGET);
      ImmutableList<Artifact> protos = prerequisiteArtifacts.filter(FileType.of(".proto")).list();
      if (!protos.isEmpty()) {
        ruleContext.attributeWarning("deps", FILES_DEPRECATED_WARNING);
      }
      return protos;
    }

    /**
     * Returns the list of proto files that were added using proto_library dependencies.
     */
    private NestedSet<Artifact> getProtoDepsSources() {
      NestedSetBuilder<Artifact> artifacts = NestedSetBuilder.stableOrder();
      Iterable<ProtoSourcesProvider> providers =
          ruleContext.getPrerequisites("deps", Mode.TARGET, ProtoSourcesProvider.class);
      for (ProtoSourcesProvider provider : providers) {
        artifacts.addTransitive(provider.getTransitiveProtoSources());
      }
      return artifacts.build();
    }
  }
}