Skip to content

Commit

Permalink
Add xcrunwrapper to deal with DEVELOPER_DIR and SDKROOT.
Browse files Browse the repository at this point in the history
Replace uses of $SDKROOT and $DEVELOPER_DIR values in compile paths with __DEVELOPER_DIR__ and __SDKROOT__ to that xcrunwrapper can deal with them appropriately.

RELNOTES:none

--
MOS_MIGRATED_REVID=107259512
  • Loading branch information
Dave MacLachlan authored and fweikert committed Nov 6, 2015
1 parent 69a3f36 commit 7fdbd78
Show file tree
Hide file tree
Showing 21 changed files with 227 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
/tools/objc/realpath
/tools/objc/StdRedirect.dylib
/tools/objc/swiftstdlibtoolwrapper.sh
/tools/objc/xcrunwrapper.sh
1 change: 1 addition & 0 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ if [ $DO_TOOLS_COMPILATION ]; then
bazel_bootstrap //src/tools/xcode/ibtoolwrapper:ibtoolwrapper tools/objc/ibtoolwrapper.sh 0755
bazel_bootstrap //src/tools/xcode/momcwrapper:momcwrapper tools/objc/momcwrapper.sh 0755
bazel_bootstrap //src/tools/xcode/swiftstdlibtoolwrapper:swiftstdlibtoolwrapper tools/objc/swiftstdlibtoolzip.sh 0755
bazel_bootstrap //src/tools/xcode/xcrunwrapper:xcrunwrapper tools/objc/xcrunwrapper.sh 0755
bazel_bootstrap //src/objc_tools/bundlemerge:bundlemerge_deploy.jar \
tools/objc/precomp_bundlemerge_deploy.jar
bazel_bootstrap //src/objc_tools/plmerge:plmerge_deploy.jar \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public static void setup(ConfiguredRuleClassProvider.Builder builder) {
builder.addRuleDefinition(new ObjcRuleClasses.SdkFrameworksDependerRule());
builder.addRuleDefinition(new ObjcRuleClasses.CompileDependencyRule());
builder.addRuleDefinition(new ObjcRuleClasses.ResourceToolsRule());
builder.addRuleDefinition(new ObjcRuleClasses.XcrunRule());
builder.addRuleDefinition(new IosApplicationRule());
builder.addRuleDefinition(new IosExtensionBinaryRule());
builder.addRuleDefinition(new IosExtensionRule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.BUNDLE_FILE;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.STRINGS;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.XCASSETS_DIR;
import static com.google.devtools.build.lib.rules.objc.ObjcRuleClasses.XCRUN;

import com.google.common.base.Optional;
import com.google.common.base.Verify;
Expand Down Expand Up @@ -231,9 +230,10 @@ private void registerInterfaceBuilderActions(ObjcProvider objcProvider) {
.setCommandLine(ibActionsCommandLine(archiveRoot, zipOutput, storyboardInput))
.addOutput(zipOutput)
.addInput(storyboardInput)
// TODO(dmaclach): Adding realpath here should not be required once
// TODO(dmaclach): Adding realpath and xcrunwrapper should not be required once
// https://github.com/bazelbuild/bazel/issues/285 is fixed.
.addInput(attributes.realpath())
.addInput(CompilationSupport.xcrunwrapper(ruleContext).getExecutable())
.build(ruleContext));
}
}
Expand Down Expand Up @@ -270,13 +270,14 @@ private void registerMomczipActions(ObjcProvider objcProvider) {
.setExecutable(attributes.momcWrapper())
.addOutput(outputZip)
.addInputs(datamodel.getInputs())
// TODO(dmaclach): Adding realpath here should not be required once
// TODO(dmaclach): Adding realpath and xcrunwrapper should not be required once
// https://github.com/google/bazel/issues/285 is fixed.
.addInput(attributes.realpath())
.setCommandLine(CustomCommandLine.builder()
.addInput(CompilationSupport.xcrunwrapper(ruleContext).getExecutable())
.setCommandLine(CustomCommandLine.builder()
.addPath(outputZip.getExecPath())
.add(datamodel.archiveRootForMomczip())
.add("-XD_MOMC_SDKROOT=" + IosSdkCommands.sdkDir(objcConfiguration))
.add("-XD_MOMC_SDKROOT=" + IosSdkCommands.sdkDir())
.add("-XD_MOMC_IOS_TARGET_VERSION=" + bundling.getMinimumOsVersion())
.add("-MOMC_PLATFORMS")
.add(objcConfiguration.getBundlingPlatform().getLowerCaseNameInPlist())
Expand All @@ -302,9 +303,10 @@ private void registerConvertXibsActions(ObjcProvider objcProvider) {
.setCommandLine(ibActionsCommandLine(archiveRoot, zipOutput, original))
.addOutput(zipOutput)
.addInput(original)
// TODO(dmaclach): Adding realpath here should not be required once
// TODO(dmaclach): Adding realpath and xcrunwrapper should not be required once
// https://github.com/bazelbuild/bazel/issues/285 is fixed.
.addInput(attributes.realpath())
.addInput(CompilationSupport.xcrunwrapper(ruleContext).getExecutable())
.build(ruleContext));
}
}
Expand All @@ -324,6 +326,7 @@ private void registerConvertStringsActions(ObjcProvider objcProvider) {
.addPath(strings.getExecPath())
.build())
.addInput(strings)
.addInput(CompilationSupport.xcrunwrapper(ruleContext).getExecutable())
.addOutput(bundled)
.build(ruleContext));
}
Expand Down Expand Up @@ -385,9 +388,10 @@ private void registerActoolActionIfNecessary(ObjcProvider objcProvider) {
.addTransitiveInputs(objcProvider.get(ASSET_CATALOG))
.addOutput(zipOutput)
.addOutput(actoolPartialInfoplist)
// TODO(dmaclach): Adding realpath here should not be required once
// TODO(dmaclach): Adding realpath and xcrunwrapper should not be required once
// https://github.com/google/bazel/issues/285 is fixed.
.addInput(attributes.realpath())
.addInput(CompilationSupport.xcrunwrapper(ruleContext).getExecutable())
.setCommandLine(actoolzipCommandLine(
objcProvider,
zipOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import static com.google.devtools.build.lib.rules.objc.ObjcRuleClasses.SRCS_TYPE;
import static com.google.devtools.build.lib.rules.objc.ObjcRuleClasses.STRIP;
import static com.google.devtools.build.lib.rules.objc.ObjcRuleClasses.SWIFT;
import static com.google.devtools.build.lib.rules.objc.ObjcRuleClasses.XCRUN;
import static com.google.devtools.build.lib.rules.objc.ObjcRuleClasses.intermediateArtifacts;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

Expand All @@ -60,6 +59,7 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ParameterFile;
import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
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;
Expand Down Expand Up @@ -116,6 +116,13 @@ public final class CompilationSupport {
static final ImmutableList<String> CLANG_COVERAGE_FLAGS =
ImmutableList.of("-fprofile-arcs", "-ftest-coverage");

/**
* Returns the location of the xcrunwrapper tool.
*/
public static final FilesToRunProvider xcrunwrapper(RuleContext ruleContext) {
return ruleContext.getExecutablePrerequisite("$xcrunwrapper", Mode.HOST);
}

/**
* Files which can be instrumented along with the attributes in which they may occur and the
* attributes along which they are propagated from dependencies (via
Expand Down Expand Up @@ -397,7 +404,7 @@ private void registerCompileAction(
// TODO(bazel-team): Remote private headers from inputs once they're added to the provider.
ruleContext.registerAction(ObjcRuleClasses.spawnOnDarwinActionBuilder(ruleContext)
.setMnemonic("ObjcCompile")
.setExecutable(XCRUN)
.setExecutable(xcrunwrapper(ruleContext))
.setCommandLine(commandLine.build())
.addInput(sourceFile)
.addInputs(additionalInputs.build())
Expand Down Expand Up @@ -445,7 +452,7 @@ private void registerSwiftCompileAction(
.add("-frontend")
.add("-emit-object")
.add("-target").add(IosSdkCommands.swiftTarget(objcConfiguration))
.add("-sdk").add(IosSdkCommands.sdkDir(objcConfiguration))
.add("-sdk").add(IosSdkCommands.sdkDir())
.add("-enable-objc-interop");

if (objcConfiguration.generateDebugSymbols()) {
Expand Down Expand Up @@ -494,7 +501,7 @@ private void registerSwiftCompileAction(
ruleContext.registerAction(
ObjcRuleClasses.spawnOnDarwinActionBuilder(ruleContext)
.setMnemonic("SwiftCompile")
.setExecutable(XCRUN)
.setExecutable(xcrunwrapper(ruleContext))
.setCommandLine(commandLine.build())
.addInput(sourceFile)
.addInputs(otherSwiftSources)
Expand Down Expand Up @@ -527,7 +534,7 @@ private void registerSwiftModuleMergeAction(
.add(SWIFT)
.add("-frontend")
.add("-emit-module")
.add("-sdk").add(IosSdkCommands.sdkDir(objcConfiguration))
.add("-sdk").add(IosSdkCommands.sdkDir())
.add("-target").add(IosSdkCommands.swiftTarget(objcConfiguration));

if (objcConfiguration.generateDebugSymbols()) {
Expand Down Expand Up @@ -563,7 +570,7 @@ private void registerSwiftModuleMergeAction(

ruleContext.registerAction(ObjcRuleClasses.spawnOnDarwinActionBuilder(ruleContext)
.setMnemonic("SwiftModuleMerge")
.setExecutable(XCRUN)
.setExecutable(xcrunwrapper(ruleContext))
.setCommandLine(commandLine.build())
.addInputs(moduleFiles.build())
.addTransitiveInputs(objcProvider.get(HEADER))
Expand Down Expand Up @@ -599,13 +606,13 @@ private Iterable<Action> archiveActions(

actions.add(ObjcRuleClasses.spawnOnDarwinActionBuilder(ruleContext)
.setMnemonic("ObjcLink")
.setExecutable(XCRUN)
.setExecutable(xcrunwrapper(ruleContext))
.setCommandLine(new CustomCommandLine.Builder()
.add(LIBTOOL)
.add("-static")
.add("-filelist").add(objList.getExecPathString())
.add("-arch_only").add(objcConfiguration.getIosCpu())
.add("-syslibroot").add(IosSdkCommands.sdkDir(objcConfiguration))
.add("-syslibroot").add(IosSdkCommands.sdkDir())
.add("-o").add(archive.getExecPathString())
.build())
.addInputs(objFiles)
Expand All @@ -623,12 +630,12 @@ private void registerFullyLinkAction(ObjcProvider objcProvider) throws Interrupt
ImmutableList<Artifact> ccLibraries = ccLibraries(objcProvider);
ruleContext.registerAction(ObjcRuleClasses.spawnOnDarwinActionBuilder(ruleContext)
.setMnemonic("ObjcLink")
.setExecutable(XCRUN)
.setExecutable(xcrunwrapper(ruleContext))
.setCommandLine(new CustomCommandLine.Builder()
.add(LIBTOOL)
.add("-static")
.add("-arch_only").add(objcConfiguration.getIosCpu())
.add("-syslibroot").add(IosSdkCommands.sdkDir(objcConfiguration))
.add("-syslibroot").add(IosSdkCommands.sdkDir())
.add("-o").add(archive.getExecPathString())
.addExecPaths(objcProvider.get(LIBRARY))
.addExecPaths(objcProvider.get(IMPORTED_LIBRARY))
Expand Down Expand Up @@ -780,6 +787,7 @@ private void registerLinkAction(ObjcProvider objcProvider, ExtraLinkArgs extraLi
.addTransitiveInputs(objcProvider.get(FRAMEWORK_FILE))
.addInputs(ccLibraries)
.addInputs(extraLinkInputs)
.addInput(xcrunwrapper(ruleContext).getExecutable())
.build(ruleContext));

if (objcConfiguration.shouldStripBinary()) {
Expand All @@ -793,7 +801,7 @@ private void registerLinkAction(ObjcProvider objcProvider, ExtraLinkArgs extraLi
ruleContext.registerAction(
ObjcRuleClasses.spawnOnDarwinActionBuilder(ruleContext)
.setMnemonic("ObjcBinarySymbolStrip")
.setExecutable(XCRUN)
.setExecutable(xcrunwrapper(ruleContext))
.setCommandLine(symbolStripCommandLine(stripArgs, binaryToLink, strippedBinary))
.addOutput(strippedBinary)
.addInput(binaryToLink)
Expand Down Expand Up @@ -825,7 +833,7 @@ private CommandLine linkCommandLine(ExtraLinkArgs extraLinkArgs,
ObjcConfiguration objcConfiguration = ObjcRuleClasses.objcConfiguration(ruleContext);

CustomCommandLine.Builder commandLine = CustomCommandLine.builder()
.addPath(XCRUN);
.addPath(xcrunwrapper(ruleContext).getExecutable().getExecPath());

if (objcProvider.is(USES_CPP)) {
commandLine
Expand Down Expand Up @@ -885,7 +893,7 @@ private CommandLine linkCommandLine(ExtraLinkArgs extraLinkArgs,
PathFragment dsymPath = FileSystemUtils.removeExtension(dsymBundle.get().getExecPath());
commandLine
.add("&&")
.addPath(XCRUN)
.addPath(xcrunwrapper(ruleContext).getExecutable().getExecPath())
.add(DSYMUTIL)
.add(linkedBinary.getExecPathString())
.add("-o " + dsymPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
* Utility code for use when generating iOS SDK commands.
*/
public class IosSdkCommands {
public static final String DEVELOPER_DIR = "/Applications/Xcode.app/Contents/Developer";

// These next two strings are shared secrets with the xcrunwrapper.sh to allow
// expansion of DeveloperDir and SDKRoot and runtime, since they aren't known
// until compile time on any given build machine.
private static final String DEVELOPER_DIR = "__BAZEL_XCODE_DEVELOPER_DIR__";
private static final String SDKROOT_DIR = "__BAZEL_XCODE_SDKROOT__";

// There is a handy reference to many clang warning flags at
// http://nshipster.com/clang-diagnostics/
Expand Down Expand Up @@ -73,19 +78,41 @@ public static String getPlatformPlistName(ObjcConfiguration configuration) {
return Platform.forArch(configuration.getIosCpu()).getNameInPlist();
}

/**
* Returns the platform directory inside of Xcode for a given configuration.
*/
public static String platformDir(ObjcConfiguration configuration) {
return DEVELOPER_DIR + "/Platforms/" + getPlatformPlistName(configuration) + ".platform";
return platformDir(getPlatformPlistName(configuration));
}

public static String sdkDir(ObjcConfiguration configuration) {
return platformDir(configuration) + "/Developer/SDKs/"
+ getPlatformPlistName(configuration) + configuration.getIosSdkVersion() + ".sdk";
/**
* Returns the platform directory inside of Xcode for a given platform name (e.g. iphoneos).
*/
public static String platformDir(String platformName) {
return DEVELOPER_DIR + "/Platforms/" + platformName + ".platform";
}

public static String frameworkDir(ObjcConfiguration configuration) {
/**
* Returns the platform directory inside of Xcode for a given configuration.
*/
public static String sdkDir() {
return SDKROOT_DIR;
}

/**
* Returns the platform frameworks directory inside of Xcode for a given configuration.
*/
public static String platformDeveloperFrameworkDir(ObjcConfiguration configuration) {
return platformDir(configuration) + "/Developer/Library/Frameworks";
}

/**
* Returns the SDK frameworks directory inside of Xcode for a given configuration.
*/
public static String sdkDeveloperFrameworkDir() {
return sdkDir() + "/Developer/Library/Frameworks";
}

/**
* Returns swift libraries path.
*/
Expand Down Expand Up @@ -131,11 +158,11 @@ public static List<String> commonLinkAndCompileFlagsForClang(

return builder
.add("-arch", configuration.getIosCpu())
.add("-isysroot", sdkDir(configuration))
.add("-isysroot", sdkDir())
// TODO(bazel-team): Pass framework search paths to Xcodegen.
.add("-F", sdkDir(configuration) + "/Developer/Library/Frameworks")
.add("-F", sdkDeveloperFrameworkDir())
// As of sdk8.1, XCTest is in a base Framework dir
.add("-F", frameworkDir(configuration))
.add("-F", platformDeveloperFrameworkDir(configuration))
// Add custom (non-SDK) framework search paths. For each framework foo/bar.framework,
// include "foo" as a search path.
.addAll(Interspersing.beforeEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,9 @@ ObjcCommon build() {

if (compilationAttributes.isPresent()) {
CompilationAttributes attributes = compilationAttributes.get();
ObjcConfiguration objcConfiguration = ObjcRuleClasses.objcConfiguration(context);
Iterable<PathFragment> sdkIncludes = Iterables.transform(
Interspersing.prependEach(
IosSdkCommands.sdkDir(objcConfiguration) + "/usr/include/",
IosSdkCommands.sdkDir() + "/usr/include/",
PathFragment.safePathStrings(attributes.sdkIncludes())),
TO_PATH_FRAGMENT);
objcProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("objc_proto_library")
.factoryClass(ObjcProtoLibrary.class)
.ancestors(BaseRuleClasses.RuleBase.class)
.ancestors(BaseRuleClasses.RuleBase.class, ObjcRuleClasses.XcrunRule.class)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public class ObjcRuleClasses {
static final String DSYMUTIL = "dsymutil";
static final String LIPO = "lipo";
static final String STRIP = "strip";
static final PathFragment XCRUN = new PathFragment("/usr/bin/xcrun");

private static final PathFragment JAVA = new PathFragment("/usr/bin/java");

Expand Down Expand Up @@ -482,7 +481,7 @@ public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("$objc_resources_rule")
.type(RuleClassType.ABSTRACT)
.ancestors(ResourceToolsRule.class)
.ancestors(ResourceToolsRule.class, XcrunRule.class)
.build();
}
}
Expand Down Expand Up @@ -711,7 +710,8 @@ public Metadata getMetadata() {
BaseRuleClasses.RuleBase.class,
CompileDependencyRule.class,
OptionsRule.class,
CoptsRule.class)
CoptsRule.class,
XcrunRule.class)
.build();
}
}
Expand Down Expand Up @@ -907,7 +907,7 @@ public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("$objc_bundling_rule")
.type(RuleClassType.ABSTRACT)
.ancestors(OptionsRule.class, ResourceToolsRule.class)
.ancestors(OptionsRule.class, ResourceToolsRule.class, XcrunRule.class)
.build();
}
}
Expand Down Expand Up @@ -1042,5 +1042,25 @@ public Metadata getMetadata() {
.build();
}
}

/**
* Common attributes for {@code objc_*} rules that need to call xcrun.
*/
public static class XcrunRule implements RuleDefinition {
@Override
public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
return builder
.add(attr("$xcrunwrapper", LABEL).cfg(HOST).exec()
.value(env.getLabel(Constants.TOOLS_REPOSITORY + "//tools/objc:xcrunwrapper")))
.build();
}
@Override
public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("$objc_xcrun_rule")
.type(RuleClassType.ABSTRACT)
.build();
}
}
}

Loading

0 comments on commit 7fdbd78

Please sign in to comment.