Add imported static libraries to the list of libraries to link, instead of specifying them as linker flags, and also add their associated library search paths. This makes sure the link order for libraries is consistent with Bazel build.
--
MOS_MIGRATED_REVID=104524532
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 198d4f9..b0ed080 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
@@ -332,6 +332,18 @@
}
/**
+ * Returns the {@code LIBRARY_SEARCH_PATHS} array for a target's imported static libraries.
+ */
+ private static NSArray librarySearchPaths(Iterable<String> importedLibraries) {
+ ImmutableSet.Builder<NSString> result = new ImmutableSet.Builder<>();
+ for (String importedLibrary : importedLibraries) {
+ result.add(new NSString("$(WORKSPACE_ROOT)/" + Paths.get(importedLibrary).getParent()));
+ }
+
+ return (NSArray) NSObject.wrap(result.build().asList());
+ }
+
+ /**
* Returns the {@code ARCHS} array for a target's build config given the list of architecture
* strings. If none is given, an array with default architectures "armv7" and "arm64" will be
* returned.
@@ -364,11 +376,6 @@
Iterable<String> givenFlags = targetControl.getLinkoptList();
ImmutableList.Builder<String> flags = new ImmutableList.Builder<>();
flags.addAll(givenFlags);
- if (!Equaling.of(ProductType.STATIC_LIBRARY, productType(targetControl))) {
- for (String importedLibrary : targetControl.getImportedLibraryList()) {
- 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")) {
@@ -513,6 +520,11 @@
targetBuildConfigMap.put(name, value);
}
+ if (!Equaling.of(ProductType.STATIC_LIBRARY, productType(targetControl))) {
+ targetBuildConfigMap.put("LIBRARY_SEARCH_PATHS",
+ librarySearchPaths(targetControl.getImportedLibraryList()));
+ }
+
// Note that HFS+ (the Mac filesystem) is usually case insensitive, so we cast all target
// names to lower case before checking for duplication because otherwise users may end up
// having duplicated intermediate build directories that can interfere with the build.
@@ -586,9 +598,18 @@
Iterables.addAll(project.getMainGroup().getChildren(), processedProjectFiles);
for (TargetInfo targetInfo : targetInfoByLabel.values()) {
- for (DependencyControl dependency : targetInfo.control.getDependencyList()) {
+ TargetControl targetControl = targetInfo.control;
+ for (DependencyControl dependency : targetControl.getDependencyList()) {
targetInfo.addDependencyInfo(dependency, targetInfoByLabel);
}
+
+ if (!Equaling.of(ProductType.STATIC_LIBRARY, productType(targetControl))) {
+ for (String importedLibrary : targetControl.getImportedLibraryList()) {
+ FileReference fileReference = FileReference.of(importedLibrary, SourceTree.GROUP)
+ .withExplicitFileType(FILE_TYPE_ARCHIVE_LIBRARY);
+ targetInfo.frameworksPhase.getFiles().add(pbxBuildFiles.getStandalone(fileReference));
+ }
+ }
}
return project;