Changes dylibs from being part of the xcodeproject file references to being arguments to OTHER_LDFLAGS. Command lines now use -l"name" to link libraries. This solves the problem that libraries in Xcode 7 now have .tbd files instead of dylibs in device builds and maintains backwards compatibility with Xcode 6.

Also modifies Bazel Objclink action to pass in libraries as -l"name" arguments.

RELNOTES:Adds support for dylibs on devices for Xcode 7.

--
MOS_MIGRATED_REVID=103589448
diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
index 57de1fd..1ea050a77 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java
@@ -722,11 +722,11 @@
         .add(IosSdkCommands.DEFAULT_LINKER_FLAGS)
         .addBeforeEach("-framework", frameworkNames(objcProvider))
         .addBeforeEach("-weak_framework", SdkFramework.names(objcProvider.get(WEAK_SDK_FRAMEWORK)))
+        .addFormatEach("-l%s", libraryNames(objcProvider))
         .addExecPath("-o", linkedBinary)
         .addExecPaths(objcProvider.get(LIBRARY))
         .addExecPaths(objcProvider.get(IMPORTED_LIBRARY))
         .addExecPaths(ccLibraries)
-        .add(dylibPaths(objcProvider))
         .addBeforeEach("-force_load", Artifact.toExecPaths(objcProvider.get(FORCE_LOAD_LIBRARY)))
         .add(extraLinkArgs)
         .build();
@@ -785,12 +785,15 @@
     }
   }
 
-  private Iterable<String> dylibPaths(ObjcProvider objcProvider) {
-    ObjcConfiguration objcConfiguration = ObjcRuleClasses.objcConfiguration(ruleContext);
+  private Iterable<String> libraryNames(ObjcProvider objcProvider) {
     ImmutableList.Builder<String> args = new ImmutableList.Builder<>();
     for (String dylib : objcProvider.get(SDK_DYLIB)) {
-      args.add(String.format(
-          "%s/usr/lib/%s.dylib", IosSdkCommands.sdkDir(objcConfiguration), dylib));
+      if (dylib.startsWith("lib")) {
+        // remove lib prefix if it exists which is standard
+        // for libraries (libxml.dylib -> -lxml).
+        dylib = dylib.substring(3);
+      }
+      args.add(dylib);
     }
     return args.build();
   }
diff --git a/src/main/protobuf/xcodegen.proto b/src/main/protobuf/xcodegen.proto
index 23d3362..5aa4c59 100644
--- a/src/main/protobuf/xcodegen.proto
+++ b/src/main/protobuf/xcodegen.proto
@@ -172,8 +172,8 @@
   // SDK .dylib files to link with this target. Each name should not include the
   // the path or the .dylib extension, e.g. "libz" to link in
   // "SDKROOT/usr/lib/libz.dylib". For all targets, this causes the library to
-  // appear in the Project Navigator. For binary targets, this causes the
-  // library to be linked with the final binary.
+  // appear in the Project Build Settings under OTHER_LDFLAGS. For binary
+  // targets, this causes the library to be linked with the final binary.
   repeated string sdk_dylib = 19;
 }
 
diff --git a/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/LibraryObjects.java b/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/LibraryObjects.java
index f751d9d..11c738a 100644
--- a/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/LibraryObjects.java
+++ b/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/LibraryObjects.java
@@ -43,7 +43,6 @@
 public final class LibraryObjects implements HasProjectNavigatorFiles {
 
   @VisibleForTesting static final String FRAMEWORK_FILE_TYPE = "wrapper.framework";
-  @VisibleForTesting static final String DYLIB_FILE_TYPE = "compiled.mach-o.dylib";
 
   private final LinkedHashMap<FileReference, PBXReference> fileToMainGroupReferences =
       new LinkedHashMap<>();
@@ -67,19 +66,6 @@
     private BuildPhaseBuilder() {} // Don't allow instantiation from outside the enclosing class.
 
     /**
-     * Creates a new dylib library based on the passed name.
-     *
-     * @param name simple dylib without ".dylib" suffix, e.g. "libz"
-     */
-    public BuildPhaseBuilder addDylib(String name) {
-      FileReference reference =
-          FileReference.of(String.format("usr/lib/%s.dylib", name), SourceTree.SDKROOT)
-              .withExplicitFileType(DYLIB_FILE_TYPE);
-      fileReferences.add(reference);
-      return this;
-    }
-
-    /**
      * Creates a new SDK framework based on the passed name.
      *
      * @param name simple framework name without ".framework" suffix, e.g. "Foundation"
diff --git a/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/XcodeprojGeneration.java b/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/XcodeprojGeneration.java
index 2fae52f..6e69f6f 100644
--- a/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/XcodeprojGeneration.java
+++ b/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/XcodeprojGeneration.java
@@ -348,14 +348,9 @@
     }
   }
 
-  private static PBXFrameworksBuildPhase buildLibraryInfo(
+  private static PBXFrameworksBuildPhase buildFrameworksInfo(
       LibraryObjects libraryObjects, TargetControl target) {
     BuildPhaseBuilder builder = libraryObjects.newBuildPhase();
-    if (Containing.item(PRODUCT_TYPES_THAT_HAVE_A_BINARY, productType(target))) {
-      for (String dylib : target.getSdkDylibList()) {
-        builder.addDylib(dylib);
-      }
-    }
     for (String sdkFramework : target.getSdkFrameworkList()) {
       builder.addSdkFramework(sdkFramework);
     }
@@ -374,6 +369,15 @@
         flags.add("$(WORKSPACE_ROOT)/" + importedLibrary);
       }
     }
+    if (Containing.item(PRODUCT_TYPES_THAT_HAVE_A_BINARY, productType(targetControl))) {
+      for (String dylib : targetControl.getSdkDylibList()) {
+        if (dylib.startsWith("lib")) {
+          dylib = dylib.substring(3);
+        }
+        flags.add("-l" + dylib);
+      }
+    }
+
     return flags.build();
   }
 
@@ -530,7 +534,10 @@
       }
       target.setProductReference(productReference);
 
-      PBXFrameworksBuildPhase frameworksPhase = buildLibraryInfo(libraryObjects, targetControl);
+      // We only add frameworks here and not dylibs because of differences in how
+      // Xcode 6 and Xcode 7 specify dylibs in the project organizer.
+      // (Xcode 6 -> *.dylib, Xcode 7 -> *.tbd)
+      PBXFrameworksBuildPhase frameworksPhase = buildFrameworksInfo(libraryObjects, targetControl);
       PBXResourcesBuildPhase resourcesPhase = resources.resourcesBuildPhase(targetControl);
 
       for (String importedArchive : targetControl.getImportedLibraryList()) {