use of com.facebook.buck.apple.AppleConfig in project buck by facebook.
the class InstallCommand method installAppleBundleForSimulator.
private InstallResult installAppleBundleForSimulator(CommandRunnerParams params, AppleBundle appleBundle, SourcePathResolver pathResolver, ProjectFilesystem projectFilesystem, ProcessExecutor processExecutor) throws IOException, InterruptedException {
// TODO(bhamiltoncx): This should be shared with the build and passed down.
AppleConfig appleConfig = new AppleConfig(params.getBuckConfig());
Optional<Path> xcodeDeveloperPath = appleConfig.getAppleDeveloperDirectorySupplier(processExecutor).get();
if (!xcodeDeveloperPath.isPresent()) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (Xcode not found)", appleBundle.getFullyQualifiedName()));
return FAILURE;
}
UnixUserIdFetcher userIdFetcher = new UnixUserIdFetcher();
AppleCoreSimulatorServiceController appleCoreSimulatorServiceController = new AppleCoreSimulatorServiceController(processExecutor);
Optional<Path> coreSimulatorServicePath = appleCoreSimulatorServiceController.getCoreSimulatorServicePath(userIdFetcher);
boolean shouldWaitForSimulatorsToShutdown = false;
if (!coreSimulatorServicePath.isPresent() || !coreSimulatorServicePath.get().toRealPath().startsWith(xcodeDeveloperPath.get().toRealPath())) {
LOG.warn("Core simulator service path %s does not match developer directory %s, " + "killing all simulators.", coreSimulatorServicePath, xcodeDeveloperPath.get());
if (!appleCoreSimulatorServiceController.killSimulatorProcesses()) {
params.getConsole().printBuildFailure("Could not kill running simulator processes.");
return FAILURE;
}
shouldWaitForSimulatorsToShutdown = true;
}
Path simctlPath = xcodeDeveloperPath.get().resolve("usr/bin/simctl");
Optional<AppleSimulator> appleSimulator = getAppleSimulatorForBundle(appleBundle, processExecutor, simctlPath);
if (!appleSimulator.isPresent()) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (no appropriate simulator found)", appleBundle.getFullyQualifiedName()));
return FAILURE;
}
Path iosSimulatorPath = null;
Path xcodeApplicationsPath = xcodeDeveloperPath.get().resolve("Applications");
for (String simulatorApp : APPLE_SIMULATOR_APPS) {
Path resolvedSimulatorPath = xcodeApplicationsPath.resolve(simulatorApp);
if (projectFilesystem.isDirectory(resolvedSimulatorPath)) {
iosSimulatorPath = resolvedSimulatorPath;
break;
}
}
if (iosSimulatorPath == null) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (could not find simulator under %s, checked %s)", appleBundle.getFullyQualifiedName(), xcodeApplicationsPath, APPLE_SIMULATOR_APPS));
return FAILURE;
}
AppleSimulatorController appleSimulatorController = new AppleSimulatorController(processExecutor, simctlPath, iosSimulatorPath);
if (!appleSimulatorController.canStartSimulator(appleSimulator.get().getUdid())) {
LOG.warn("Cannot start simulator %s, killing simulators and trying again.");
if (!appleCoreSimulatorServiceController.killSimulatorProcesses()) {
params.getConsole().printBuildFailure("Could not kill running simulator processes.");
return FAILURE;
}
shouldWaitForSimulatorsToShutdown = true;
// Killing the simulator can cause the UDIDs to change, so we need to fetch them again.
appleSimulator = getAppleSimulatorForBundle(appleBundle, processExecutor, simctlPath);
if (!appleSimulator.isPresent()) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (no appropriate simulator found)", appleBundle.getFullyQualifiedName()));
return FAILURE;
}
}
long remainingMillis = APPLE_SIMULATOR_WAIT_MILLIS;
if (shouldWaitForSimulatorsToShutdown) {
Optional<Long> shutdownMillis = appleSimulatorController.waitForSimulatorsToShutdown(remainingMillis);
if (!shutdownMillis.isPresent()) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (simulators did not shut down within %d ms).", appleBundle.getFullyQualifiedName(), APPLE_SIMULATOR_WAIT_MILLIS));
return FAILURE;
}
LOG.debug("Simulators shut down in %d millis.", shutdownMillis.get());
remainingMillis -= shutdownMillis.get();
}
LOG.debug("Starting up simulator %s", appleSimulator.get());
Optional<Long> startMillis = appleSimulatorController.startSimulator(appleSimulator.get().getUdid(), remainingMillis);
if (!startMillis.isPresent()) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (could not start simulator %s within %d ms)", appleBundle.getFullyQualifiedName(), appleSimulator.get().getName(), APPLE_SIMULATOR_WAIT_MILLIS));
return FAILURE;
}
LOG.debug("Simulator started in %d ms. Installing Apple bundle %s in simulator %s", startMillis.get(), appleBundle, appleSimulator.get());
if (!appleSimulatorController.installBundleInSimulator(appleSimulator.get().getUdid(), pathResolver.getAbsolutePath(Preconditions.checkNotNull(appleBundle.getSourcePathToOutput())))) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (could not install bundle %s in simulator %s)", appleBundle.getFullyQualifiedName(), pathResolver.getAbsolutePath(appleBundle.getSourcePathToOutput()), appleSimulator.get().getName()));
return FAILURE;
}
if (run) {
return launchAppleBundle(params, appleBundle, appleSimulatorController, projectFilesystem, appleSimulator.get());
} else {
params.getBuckEventBus().post(ConsoleEvent.info(params.getConsole().getAnsi().asHighlightedSuccessText("Successfully installed %s. (Use `buck install -r %s` to run.)"), getArguments().get(0), getArguments().get(0)));
return InstallResult.builder().setExitCode(0).build();
}
}
use of com.facebook.buck.apple.AppleConfig in project buck by facebook.
the class ProjectCommand method generateWorkspacesForTargets.
@VisibleForTesting
static ImmutableSet<BuildTarget> generateWorkspacesForTargets(final CommandRunnerParams params, final TargetGraphAndTargets targetGraphAndTargets, ImmutableSet<BuildTarget> passedInTargetsSet, ImmutableSet<ProjectGenerator.Option> options, ImmutableList<String> buildWithBuckFlags, Optional<ImmutableSet<UnflavoredBuildTarget>> focusModules, Map<Path, ProjectGenerator> projectGenerators, boolean combinedProject, boolean buildWithBuck) throws IOException, InterruptedException {
ImmutableSet<BuildTarget> targets;
if (passedInTargetsSet.isEmpty()) {
targets = targetGraphAndTargets.getProjectRoots().stream().map(TargetNode::getBuildTarget).collect(MoreCollectors.toImmutableSet());
} else {
targets = passedInTargetsSet;
}
LOG.debug("Generating workspace for config targets %s", targets);
ImmutableSet.Builder<BuildTarget> requiredBuildTargetsBuilder = ImmutableSet.builder();
for (final BuildTarget inputTarget : targets) {
TargetNode<?, ?> inputNode = targetGraphAndTargets.getTargetGraph().get(inputTarget);
XcodeWorkspaceConfigDescription.Arg workspaceArgs;
if (inputNode.getDescription() instanceof XcodeWorkspaceConfigDescription) {
TargetNode<XcodeWorkspaceConfigDescription.Arg, ?> castedWorkspaceNode = castToXcodeWorkspaceTargetNode(inputNode);
workspaceArgs = castedWorkspaceNode.getConstructorArg();
} else if (canGenerateImplicitWorkspaceForDescription(inputNode.getDescription())) {
workspaceArgs = createImplicitWorkspaceArgs(inputNode);
} else {
throw new HumanReadableException("%s must be a xcode_workspace_config, apple_binary, apple_bundle, or apple_library", inputNode);
}
BuckConfig buckConfig = params.getBuckConfig();
AppleConfig appleConfig = new AppleConfig(buckConfig);
HalideBuckConfig halideBuckConfig = new HalideBuckConfig(buckConfig);
CxxBuckConfig cxxBuckConfig = new CxxBuckConfig(buckConfig);
SwiftBuckConfig swiftBuckConfig = new SwiftBuckConfig(buckConfig);
CxxPlatform defaultCxxPlatform = params.getCell().getKnownBuildRuleTypes().getDefaultCxxPlatforms();
WorkspaceAndProjectGenerator generator = new WorkspaceAndProjectGenerator(params.getCell(), targetGraphAndTargets.getTargetGraph(), workspaceArgs, inputTarget, options, combinedProject, buildWithBuck, buildWithBuckFlags, focusModules, !appleConfig.getXcodeDisableParallelizeBuild(), new ExecutableFinder(), params.getEnvironment(), params.getCell().getKnownBuildRuleTypes().getCxxPlatforms(), defaultCxxPlatform, params.getBuckConfig().getView(ParserConfig.class).getBuildFileName(), input -> ActionGraphCache.getFreshActionGraph(params.getBuckEventBus(), targetGraphAndTargets.getTargetGraph().getSubgraph(ImmutableSet.of(input))).getResolver(), params.getBuckEventBus(), halideBuckConfig, cxxBuckConfig, appleConfig, swiftBuckConfig);
ListeningExecutorService executorService = params.getExecutors().get(ExecutorPool.PROJECT);
Preconditions.checkNotNull(executorService, "CommandRunnerParams does not have executor for PROJECT pool");
generator.generateWorkspaceAndDependentProjects(projectGenerators, executorService);
ImmutableSet<BuildTarget> requiredBuildTargetsForWorkspace = generator.getRequiredBuildTargets();
LOG.debug("Required build targets for workspace %s: %s", inputTarget, requiredBuildTargetsForWorkspace);
requiredBuildTargetsBuilder.addAll(requiredBuildTargetsForWorkspace);
}
return requiredBuildTargetsBuilder.build();
}
use of com.facebook.buck.apple.AppleConfig in project buck by facebook.
the class ProjectGeneratorTest method setUp.
@Before
public void setUp() throws InterruptedException, IOException {
assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
clock = new SettableFakeClock(0, 0);
fakeProjectFilesystem = new FakeProjectFilesystem(clock);
projectCell = (new TestCellBuilder()).setFilesystem(fakeProjectFilesystem).build();
projectFilesystem = projectCell.getFilesystem();
rootPath = projectFilesystem.getRootPath();
// Add files and directories used to test resources.
projectFilesystem.createParentDirs(Paths.get("foodir", "foo.png"));
projectFilesystem.writeContentsToPath("", Paths.get("foodir", "foo.png"));
projectFilesystem.writeContentsToPath("", Paths.get("bar.png"));
fakeProjectFilesystem.touch(Paths.get("Base.lproj", "Bar.storyboard"));
halideBuckConfig = HalideLibraryBuilder.createDefaultHalideConfig(fakeProjectFilesystem);
ImmutableMap<String, ImmutableMap<String, String>> sections = ImmutableMap.of("cxx", ImmutableMap.of("cflags", "-Wno-deprecated -Wno-conversion", "cxxflags", "-Wundeclared-selector -Wno-objc-designated-initializers"), "apple", ImmutableMap.of("force_dsym_mode_in_build_with_buck", "false"), "swift", ImmutableMap.of("version", "1.23"));
BuckConfig config = FakeBuckConfig.builder().setSections(sections).build();
cxxBuckConfig = new CxxBuckConfig(config);
appleConfig = new AppleConfig(config);
swiftBuckConfig = new SwiftBuckConfig(config);
}
use of com.facebook.buck.apple.AppleConfig in project buck by facebook.
the class InstallCommand method getInstallHelperTargets.
private ImmutableSet<String> getInstallHelperTargets(CommandRunnerParams params, ListeningExecutorService executor) throws IOException, InterruptedException, BuildTargetException, BuildFileParseException {
ParserConfig parserConfig = params.getBuckConfig().getView(ParserConfig.class);
ImmutableSet.Builder<String> installHelperTargets = ImmutableSet.builder();
for (int index = 0; index < getArguments().size(); index++) {
// TODO(ryu2): Cache argument parsing
TargetNodeSpec spec = parseArgumentsAsTargetNodeSpecs(params.getBuckConfig(), getArguments()).get(index);
BuildTarget target = FluentIterable.from(params.getParser().resolveTargetSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), executor, ImmutableList.of(spec), SpeculativeParsing.of(false), parserConfig.getDefaultFlavorsMode())).transformAndConcat(Functions.identity()).first().get();
TargetNode<?, ?> node = params.getParser().getTargetNode(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), executor, target);
if (node != null && Description.getBuildRuleType(node.getDescription()).equals(Description.getBuildRuleType(AppleBundleDescription.class))) {
for (Flavor flavor : node.getBuildTarget().getFlavors()) {
if (ApplePlatform.needsInstallHelper(flavor.getName())) {
AppleConfig appleConfig = new AppleConfig(params.getBuckConfig());
Optional<BuildTarget> deviceHelperTarget = appleConfig.getAppleDeviceHelperTarget();
Optionals.addIfPresent(Optionals.bind(deviceHelperTarget, input -> !input.toString().isEmpty() ? Optional.of(input.toString()) : Optional.empty()), installHelperTargets);
}
}
}
}
return installHelperTargets.build();
}
use of com.facebook.buck.apple.AppleConfig in project buck by facebook.
the class InstallCommand method installAppleBundleForDevice.
private InstallResult installAppleBundleForDevice(CommandRunnerParams params, AppleBundle appleBundle, ProjectFilesystem projectFilesystem, ProcessExecutor processExecutor, SourcePathResolver pathResolver) throws IOException, NoSuchBuildTargetException {
// TODO(bhamiltoncx): This should be shared with the build and passed down.
AppleConfig appleConfig = new AppleConfig(params.getBuckConfig());
final Path helperPath;
Optional<BuildTarget> helperTarget = appleConfig.getAppleDeviceHelperTarget();
if (helperTarget.isPresent()) {
BuildRuleResolver resolver = super.getBuild().getRuleResolver();
BuildRule buildRule = resolver.requireRule(helperTarget.get());
if (buildRule == null) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (could not resolve build rule for device helper target %s)", appleBundle.getFullyQualifiedName(), helperTarget.get().getBaseName()));
return FAILURE;
}
SourcePath buildRuleOutputPath = buildRule.getSourcePathToOutput();
if (buildRuleOutputPath == null) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (device helper target %s does not specify an output)", appleBundle.getFullyQualifiedName(), helperTarget.get().getBaseName()));
return FAILURE;
}
helperPath = pathResolver.getAbsolutePath(buildRuleOutputPath);
} else {
Optional<Path> helperOverridePath = appleConfig.getAppleDeviceHelperAbsolutePath();
if (helperOverridePath.isPresent()) {
helperPath = helperOverridePath.get();
} else {
params.getConsole().printBuildFailure(String.format("Cannot install %s (could not find path to device install helper tool)", appleBundle.getFullyQualifiedName()));
return FAILURE;
}
}
AppleDeviceHelper helper = new AppleDeviceHelper(processExecutor, helperPath);
ImmutableMap<String, String> connectedDevices = helper.getConnectedDevices();
if (connectedDevices.size() == 0) {
params.getConsole().printBuildFailure(String.format("Cannot install %s (no connected devices found)", appleBundle.getFullyQualifiedName()));
return FAILURE;
}
String selectedUdid = null;
if (targetDeviceOptions().getSerialNumber().isPresent()) {
String udidPrefix = Assertions.assertNotNull(targetDeviceOptions().getSerialNumber().get()).toLowerCase();
for (String udid : connectedDevices.keySet()) {
if (udid.startsWith(udidPrefix)) {
selectedUdid = udid;
break;
}
}
if (selectedUdid == null) {
params.getConsole().printBuildFailure(String.format("Cannot install %s to the device %s (no connected devices with that UDID/prefix)", appleBundle.getFullyQualifiedName(), udidPrefix));
return FAILURE;
}
} else {
if (connectedDevices.size() > 1) {
LOG.warn("More than one connected device found, and no device ID specified. A device will be" + " arbitrarily picked.");
}
selectedUdid = connectedDevices.keySet().iterator().next();
}
LOG.info("Installing " + appleBundle.getFullyQualifiedName() + " to device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
if (helper.installBundleOnDevice(selectedUdid, pathResolver.getAbsolutePath(Preconditions.checkNotNull(appleBundle.getSourcePathToOutput())))) {
params.getConsole().printSuccess("Installed " + appleBundle.getFullyQualifiedName() + " to device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
if (run) {
Optional<String> appleBundleId;
try (InputStream bundlePlistStream = projectFilesystem.getInputStreamForRelativePath(appleBundle.getInfoPlistPath())) {
appleBundleId = AppleInfoPlistParsing.getBundleIdFromPlistStream(bundlePlistStream);
}
if (!appleBundleId.isPresent()) {
params.getConsole().printBuildFailure(String.format("Cannot run %s (could not get bundle ID from %s)", appleBundle.getFullyQualifiedName(), appleBundle.getInfoPlistPath()));
return FAILURE;
}
if (waitForDebugger) {
LOG.warn(WAIT_FOR_DEBUGGER_LONG_ARG + " not yet implemented for devices.");
}
if (helper.runBundleOnDevice(selectedUdid, appleBundleId.get())) {
return InstallResult.builder().setExitCode(0).build();
} else {
params.getConsole().printBuildFailure("Failed to run " + appleBundle.getFullyQualifiedName() + " on device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
return FAILURE;
}
} else {
return InstallResult.builder().setExitCode(0).build();
}
} else {
params.getConsole().printBuildFailure("Failed to install " + appleBundle.getFullyQualifiedName() + " to device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
return FAILURE;
}
}
Aggregations