Skip to content

Add --ios_device flag for running iOS application on a physical device #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public class ObjcCommandLineOptions extends FragmentOptions {
+ "devicetypes' on the machine the simulator will be run on.")
public String iosSimulatorDevice;

@Option(
name = "ios_device",
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.TESTING,
effectTags = {OptionEffectTag.TEST_RUNNER},
help =
"The identifier, ECID, serial number, UDID, user-provided name, or DNS name of "
+ "the device for running an iOS application. "
+ "You can get a list of devices by running 'xcrun devicectl list "
+ "devices'.")
Comment on lines +56 to +60

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The help text is good, but it could be improved by mentioning that the identifier can be obtained from Xcode as well, and that the tool requires Xcode 15 or later. Also, consider adding a note about potential issues with device access or permissions.

For example, the help text could be reworded as follows:

"The identifier, ECID, serial number, UDID, user-provided name, or DNS name of the device for running an iOS application. You can get a list of devices by running 'xcrun devicectl list devices' (requires Xcode 15 or later) or from Xcode's Devices and Simulators window. Ensure that the device is properly connected and authorized for development."

        "The identifier, ECID, serial number, UDID, user-provided name, or DNS name of "
            + "the device for running an iOS application. "
            + "You can get a list of devices by running 'xcrun devicectl list "
            + "devices' (requires Xcode 15 or later) or from Xcode's Devices and Simulators "
            + "window. Ensure that the device is properly connected and authorized for development."

public String iosDevice;

@Option(
name = "ios_memleaks",
defaultValue = "false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class ObjcConfiguration extends Fragment implements ObjcConfigurationApi

private final DottedVersion iosSimulatorVersion;
private final String iosSimulatorDevice;
private final String iosDevice;
private final boolean runMemleaks;
private final CompilationMode compilationMode;
private final ImmutableList<String> fastbuildOptions;
Expand All @@ -70,6 +71,7 @@ public ObjcConfiguration(BuildOptions buildOptions) {

this.iosSimulatorDevice = objcOptions.iosSimulatorDevice;
this.iosSimulatorVersion = DottedVersion.maybeUnwrap(objcOptions.iosSimulatorVersion);
this.iosDevice = objcOptions.iosDevice;
this.runMemleaks = objcOptions.runMemleaks;
this.compilationMode = Preconditions.checkNotNull(options.compilationMode, "compilationMode");
this.fastbuildOptions = ImmutableList.copyOf(objcOptions.fastbuildOptions);
Expand Down Expand Up @@ -98,6 +100,14 @@ public DottedVersion getIosSimulatorVersion() {
return iosSimulatorVersion;
}

/**
* Returns the device when running an application on a physical device.
*/
@Override
public String getIosDevice() {
return iosDevice;
}

@Override
public boolean runMemleaks() {
return runMemleaks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public interface ObjcConfigurationApi extends StarlarkValue {
@Nullable
DottedVersionApi<?> getIosSimulatorVersion();

@StarlarkMethod(
name = "ios_device",
structField = true,
allowReturnNones = true,
doc = "The device identifier to use when running an iOS application.")
@Nullable
String getIosDevice();

@StarlarkMethod(
name = "run_memleaks",
structField = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,61 @@ def swift_binary_impl(ctx):
assertThat(signingCertificateName).isEqualTo("'Apple Developer'");
}

@Test
public void testStarlarkCanAccessObjcConfigurationForDevice() throws Exception {
scratch.file("examples/rule/BUILD");
scratch.file(
"examples/rule/objc_rules.bzl",
"""
load("//myinfo:myinfo.bzl", "MyInfo")

def swift_binary_impl(ctx):
compilation_mode_copts = ctx.fragments.objc.copts_for_current_compilation_mode
ios_device = ctx.fragments.objc.ios_device
signing_certificate_name = ctx.fragments.objc.signing_certificate_name
return MyInfo(
compilation_mode_copts = compilation_mode_copts,
ios_device = ios_device,
signing_certificate_name = signing_certificate_name,
)

swift_binary = rule(
implementation = swift_binary_impl,
fragments = ["objc"],
)
""");

scratch.file("examples/objc_starlark/a.m");
scratch.file(
"examples/objc_starlark/BUILD",
"""
load("//examples/rule:objc_rules.bzl", "swift_binary")

package(default_visibility = ["//visibility:public"])

swift_binary(
name = "my_target",
)
""");

useConfiguration(
"--compilation_mode=opt",
"--ios_multi_cpus=arm64",
"--ios_device='11111111-1111-1111-1111-11111111111'",
"--ios_signing_cert_name='Apple Developer'");
ConfiguredTarget starlarkTarget = getConfiguredTarget("//examples/objc_starlark:my_target");
StructImpl myInfo = getMyInfoFromTarget(starlarkTarget);

@SuppressWarnings("unchecked")
List<String> compilationModeCopts = (List<String>) myInfo.getValue("compilation_mode_copts");
Object iosDevice = myInfo.getValue("ios_device");
Object signingCertificateName = myInfo.getValue("signing_certificate_name");

assertThat(compilationModeCopts).containsExactlyElementsIn(ObjcConfiguration.OPT_COPTS);
assertThat(iosDevice).isEqualTo("'11111111-1111-1111-1111-11111111111'");
assertThat(signingCertificateName).isEqualTo("'Apple Developer'");
}

@Test
public void testSigningCertificateNameCanReturnNone() throws Exception {
scratch.file("examples/rule/BUILD");
Expand Down
Loading