Skip to content
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

[Question] Cannot import default iOS frameworks in Objective-C code #385

Closed
alex-swiftify opened this issue May 19, 2017 · 7 comments
Closed

Comments

@alex-swiftify
Copy link

alex-swiftify commented May 19, 2017

I'm trying to convert an Objective-C method declaration to Swift using the following command:

sourcekitten doc --objc test.h -- -target arm64-apple-ios10.3 -x objective-c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk -I $(pwd)

test.h:

 // If I pass declaration.h directly to sourcekitten, Swift declaration is not generated
#import "declaration.h"

declaration.h:

// Without Foundation, Swift 3 style declaration is not generated
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Test

/**
 Need this comment for Swift declaration to be generated
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@end

The returned output includes the following key.swift_declaration:
func tableView(_ tableView: Any!, cellForRowAt indexPath: IndexPath!) -> Any!

Note that tableView and return type are incorrectly declared as Any!.

If I copy UIKit.h to the same folder as test.h, and copy all files from .../iPhoneOS10.3.sdk/System/Library/Frameworks/UIKit.framework/Headers/
into a subfolder named UIKit, I get the expected output:
func tableView(_ tableView: UITableView!, cellForRowAt indexPath: IndexPath!) -> UITableViewCell!

It seems that MacOS frameworks from /System/Library/Frameworks/ are correctly imported using a statement like #import <Framework/Framework.h>, but I can't figure out how to get iOS frameworks imported the same way.

Already tried the following:

  • specifying path to .../iPhoneOS10.3.sdk/System/Library/Frameworks, or .../Frameworks/UIKit.framework/Headers using -I compiler argument;
  • specifying above paths using FRAMEWORK_SEARCH_PATHS argument;
  • specifying -sdk parameter with the same value as -isysroot;
  • specifying workspace and scheme parameters to load configuration from Xcode scheme:
    -workspace TestImports.xcodeproj/project.xcworkspace -scheme TestImports

Also interesting that if I change the define to something like #import <UIKit/UIKit2.h> I get an appropriate error message, which makes me think that the #import statement is recognized, but types like UITableView are not imported for some reason.

Any hints appreciated!

@norio-nomura
Copy link
Collaborator

This usage is not currently supported.
I'll open PR fixing this.

norio-nomura added a commit that referenced this issue May 20, 2017
This allows specifying `-isysroot` on CLI arguments.
Fixes #385
@alex-swiftify
Copy link
Author

Wow, thanks for promptness!
Will check it out.

@alex-swiftify
Copy link
Author

Related question.

  1. In the above sample, if test.h contains the following:
#import <UIKit/UITableView.h>

... i get an empty output.

  1. If test.h contains the following:
#import </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk/System/Library/Frameworks/UIKit.framework/Headers/UITableView.h>

... i get all declarations from UITableView.h.

Is there any filter that excludes any declarations from default iOS frameworks if I use a statement like (1)?

@jpsim
Copy link
Owner

jpsim commented May 23, 2017

What compiler arguments are you passing? I think you need to pass -sdk for it to find the SDK.

@alex-swiftify
Copy link
Author

alex-swiftify commented May 23, 2017

Exactly the same as suggested here: #387

sourcekitten doc --objc test.h -- -x objective-c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.3.sdk -I $(pwd)                                                                                                                                                 

Yet figuring out how to pass the -sdk argument. I.e.:

sourcekitten doc --objc test.h -- -x objective-c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.3.sdk -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.3.sdk -I $(pwd)

outputs Illegal instruction: 4 (as mentioned in #206).

@jpsim
Copy link
Owner

jpsim commented May 23, 2017

¯\_(ツ)_/¯

SourceKitten is pretty dumb when it comes to compiler arguments. It just passes it to libclang.

Xcode exists to translate GUI project settings into a complex soup of clang arguments. It's not exactly a process I fully understand or find intuitive either. Sorry.

@alex-swiftify
Copy link
Author

Well, I still have quite a few concerns.

What compiler arguments are you passing? I think you need to pass -sdk for it to find the SDK.
I think the SDK is found correctly. At least, if I change the command to #import <UIKit/invalid.h> it will fail.
I guess there may be some filter that does not include SDK declarations (but only the user code) as the output of sourcekitten doc command?

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

No branches or pull requests

3 participants