Skip to content

Make Host NSUnimplemented for Android #2439

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

Closed
wants to merge 1 commit into from

Conversation

ephemer
Copy link

@ephemer ephemer commented Jul 30, 2019

It is currently impossible to load Foundation on Android 5.0 because getifaddrs is not available there.

In the absence of #if available(Android 24, *) (which would be the ideal solution here), we need to avoid compiling any mention of getifaddrs, which is what this PR aims to achieve.

@spevans
Copy link
Contributor

spevans commented Jul 30, 2019

If ResolveType.current cant be supported on Android its probably better to mark the .current value as unavailable using @available. Then the call to _resolveCurrent() can be removed on Android as well. Might also be worth making _resolveCurrent() private since it isn't used outside of this file.

Also, if functionality cant be supported, it should call NSUnsupported() not NSUnimplemented()

@spevans
Copy link
Contributor

spevans commented Jul 30, 2019

@swift-ci test

@spevans
Copy link
Contributor

spevans commented Jul 30, 2019

cc @drodriguez

@spevans spevans requested a review from drodriguez July 30, 2019 18:06
@millenomi
Copy link
Contributor

I hate to regress. Can we provide a dynamic shim in CF for getifaddrs for Android use only that errors out?

@millenomi
Copy link
Contributor

By that I mean: that errors out if you're running on an old-enough Android OS.

@drodriguez
Copy link
Contributor

It would be better to not disable it completely. I was aware of this usage, and there should be a way of fixing it. Lily proposal might be a way (even if the alternative for pre-5.0 is just something like NSUnimplemented.

In any case, if this is all the implementation, it also should include disabling the tests, or they will fail. Host functionality is not that interesting in any case (also not being available in iOS makes it a less used type than others in Foundation).

@millenomi
Copy link
Contributor

For context, I've provided some guidance on what the preferred way to deal with these situations on the forums.

@ephemer
Copy link
Author

ephemer commented Jul 31, 2019

Hi @drodriguez and @millenomi, I agree that it doesn't feel good to regress on available functionality. But I don't see a better way around this. As I outlined on the forums here I don't think it's possible to make a shim for this on Android: I'm pretty sure we need a compile-time API Level check. Otherwise we have no Foundation at all on Android 5.0, which is a much worse regression than not having Host.

I have no idea how often this functionality would be used on Android (have never used the API myself) but if it's not available on iOS I think it's reasonable for it not to be available on Android either.

We'd need to disable the Host tests on Android, yes.

And I agree that NSUnsupported() would be better than NSUnimplemented(). Even better than either of those would be an @available annotation so people don't think it'd be a good idea to try it for Android. From what I understand @available isn't available for Android yet though. In addition, it alone wouldn't suffice to fix the error we're having here - we'd still need to ensure that getifaddrs doesn't enter the symbol table for Android by using a conditional compilation.

@ephemer
Copy link
Author

ephemer commented Jul 31, 2019

@spevans sorry, that last message about NSUnimplemented was in response to your message too. I appreciate your thoughts on this.

I agree, it'd probably be ideal to mark Host as not @available on Android, but I don't think we have this feature in Swift yet (for Android). Without that, I'd rather only hit NSUnsupported() in the cases where it really is not supported. That way we'd make it clear to other s-c-f devs where the problem lies, and decrease the amount of unavailable API to a minimum.

But I'm happy to go with your guidance on this if you have a strong opinion- I personally have never used the Host API- I just want to get Foundation working on Android 5.0 again.

@ephemer
Copy link
Author

ephemer commented Jul 31, 2019

@millenomi looks like I can take your suggestion after all using the approach from #2228.

What do you think the best way of implementing the NSUnsupported() behaviour would be? Is something available in CoreFoundation (in C) along these lines?

@spevans
Copy link
Contributor

spevans commented Jul 31, 2019

Could getifaddrs be implemented as a weak function that just returns failure? Then shouldn't it be replaced by the version in the SDK if it exists otherwise the weak version would be used as a fallback?

@millenomi
Copy link
Contributor

I'm not quite sure what the support for that is on Android. @ephemer, thank you for going a different route.

@ephemer
Copy link
Author

ephemer commented Aug 8, 2019

@millenomi I'm just getting to making a PR now (I have very little idea what I'm doing though- as I mentioned I've never used this API before). I am just dynamically checking for getifaddrs and freeifaddrs which I should be able to figure out.

What I'm missing is a way to fatal error out from C in the NSUnsupported case. Do I just NSLog and abort, or is there a preferred way to do this in CFFoundation?

Edit: it currently looks like this:

static void _CFIfaddrsUnsupported() {
    CFLog(__kCFLogAssertion, CFSTR("get/freeifaddrs is unsupported on Android < 7.0"));
    abort();
}

@spevans
Copy link
Contributor

spevans commented Aug 8, 2019

Could you make the shim function return errno = EOPNOTSUPPif the functions don't exist and check this result in the swift code?

@Molanda
Copy link
Contributor

Molanda commented Sep 27, 2019

I found an Android implementation of getifaddrs/freeifaddrs that might be helpful...

https://github.com/morristech/android-ifaddrs

However, for myself, I have just used the NSUnimplemented solution, as Android 5.0 support is much more important to me than a working address lookup in Host.

@shahmishal
Copy link
Member

The Swift project moved the default branch to main and deleted master branch, so GitHub automatically closed the PR. Please re-create pull request with main branch.

More detail about the branch update - https://forums.swift.org/t/updating-branch-names/40412

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants