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

Embed framework #62

Open
hartman opened this issue Dec 3, 2015 · 9 comments
Open

Embed framework #62

hartman opened this issue Dec 3, 2015 · 9 comments

Comments

@hartman
Copy link

hartman commented Dec 3, 2015

Anyone figured out a way to add a framework to a target and add it to the embed binaries build phase ?

@akame
Copy link

akame commented Dec 29, 2015

@hartman Have you fix this problem?I add some code,and it works.

@hartman
Copy link
Author

hartman commented Dec 29, 2015

@akame no, not really. The issues I encountered were:
1: An embedded framework is a source file that is in TWO buildphases.
2: Because of this, the XSourceFile also has to have TWO build file entries, instead of what the framework assumes (one)
3: An embedded framework has specific build settings in only one of the build phases.

@jasperblues
Copy link
Member

@hartman Did you use XCSourceFile or another approach? We can either update docs with working solution or provide a dedicated method.

@hartman
Copy link
Author

hartman commented Dec 30, 2015

I hacked something up with hardcoded modifiers etc, but it's not really usable for anyone else due to the hardcoded assumptions I make and unfortunately it doesn't seem like I have any time available soon to fix it 'properly'.

The proper way would be to move the 'build file' identifier logic that is in XSourceFile (see the buildFileKey property) into the buildPhases logic of XCTarget instead, or even to make build phases and their keys a separate class.

The build fileref entries also need their own build attribute properties, since embedded frameworks carry the attributes CodeSignOnCopy, RemoveHeadersOnCopy. (This is similar to where weak linking attribute and other build settings for other types of build files usually reside).

Some relevant fragments from an actual project file:

Sourcefile entry:

077F993C1B1C42ED0096BDE8 /* MyEmbedded.framework */ = {
    isa = PBXFileReference;
    fileEncoding = 4;
    lastKnownFileType = wrapper.framework;
    path = MyEmbedded.framework;
    sourceTree = "<group>";
};

Buildkey entries:

077F99401B1C43610096BDE8 /* MyEmbedded.framework in Frameworks */ = {
    isa = PBXBuildFile;
    fileRef = 077F993C1B1C42ED0096BDE8 /* MyEmbedded.framework */;
};
077F99411B1C43610096BDE8 /* MyEmbedded.framework in Embed Frameworks */ = {
    isa = PBXBuildFile;
    fileRef = 077F993C1B1C42ED0096BDE8 /* MyEmbedded.framework */;
    settings = {
        ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, );
    };
};

These are then referenced from two buildphases

/* Begin PBXFrameworksBuildPhase section */
    4500D2DD132F708D00C21112 /* Frameworks */ = {
            isa = PBXFrameworksBuildPhase;
            buildActionMask = 2147483647;
            files = (
                    45A4897114C8486100450DF1 /* AudioToolbox.framework in Frameworks */,
[...]
                    58243472b6c71df1eb70ab08 /* libc++.dylib in Frameworks */,
                    889e5236762d6dd5969d7d7d /* libiconv.dylib in Frameworks */,
                    077F99401B1C43610096BDE8 /* MyEmbedded.framework in Frameworks */,
            );
            runOnlyForDeploymentPostprocessing = 0;
    };
/* End PBXFrameworksBuildPhase section */

/* Begin PBXCopyFilesBuildPhase section */
    077F99441B1C43610096BDE8 /* Embed Frameworks */ = {
            isa = PBXCopyFilesBuildPhase;
            buildActionMask = 2147483647;
            dstPath = "";
            dstSubfolderSpec = 10;
            files = (
                    077F99411B1C43610096BDE8 /* MyEmbedded.framework in Embed Frameworks */,
            );
            name = "Embed Frameworks";
            runOnlyForDeploymentPostprocessing = 0;
    };
/* End PBXCopyFilesBuildPhase section */

Note how EmbedFrameworks is a rather generic CopyFilesBuildPhase, with the distinguishing feature of it being named "Embed Frameworks", which is... well if I were Apple, I think I'd simply introduced a new PBX build phase class for that, instead of relying on a strcmp to a modifiable name... Also this build phase is not present by default in Xcode projects, so you need to create it manually, which is another problem, since that is another thing that isn't implemented in XcodeEditor yet.

Oh, and you have to add this to your build configuration.

FRAMEWORK_SEARCH_PATHS = (
    "$(PROJECT_DIR)/**",
);

assuming your framework is 'somewhere' inside your project dir.

@hartman
Copy link
Author

hartman commented Dec 30, 2015

Oh, and you need to figure out how you would be able to distinguish between an old style static framework and a embedded framework, because they require different 'addMember' strategies and I haven't really been able to find a good/reliable/efficient way to (on the fly) distinguish the two. If anyone has any good tips for that, love to hear them.

@akame
Copy link

akame commented Dec 31, 2015

I add some Code
_in XCGroup.m_*

  • (void)addEmbeddedFramework:(XCFrameworkDefinition *)framework toTargets:(NSArray<XCTarget *> *)targets{
    [self addFramework:framework];
    XCSourceFile *embedded = (XCSourceFile *) [self memberWithDisplayName:[framework fileName]];
    [self addEmbeddedSourceFile:embedded toTargets:targets];
    }
  • (void)addEmbeddedSourceFile:(XCSourceFile *)sourceFile toTargets:(NSArray *)targets
    {
    for (XCTarget *target in targets) {
    [target addMember:sourceFile];
    [target addEmbedded:sourceFile];
    }
    }

****in XCTarget.m ***

  • (void)addEmbedded:(XCSourceFile*)member
    {
    [member becomeEmbedded];

    NSMutableDictionary* target = [NSMutableDictionary dictionaryWithDictionary:[[_project objects] objectForKey:_key]];

    BOOL hasEmbeddedConfiguration = NO;

    for (NSString* buildPhaseKey in [target objectForKey:@"buildPhases"])
    {
    NSMutableDictionary* buildPhase = [[_project objects] objectForKey:buildPhaseKey];
    if ([[buildPhase valueForKey:@"isa"] xce_asMemberType] == PBXCopyFilesBuildPhaseType) //targer have already use embedded
    {

        NSMutableArray* files = [buildPhase objectForKey:@"files"];
        if (![files containsObject:[member embeddedKey]])
        {
            [files addObject:[member embeddedKey]];
        }
    
        [buildPhase setObject:files forKey:@"files"];
    
        hasEmbeddedConfiguration = YES;
    }
    

    }

    if (!hasEmbeddedConfiguration) { // target never use embedded before
    NSMutableDictionary *embeddedConfig = [NSMutableDictionary dictionary];
    [embeddedConfig setObject:@"PBXCopyFilesBuildPhase" forKey:@"isa"];
    [embeddedConfig setObject:@"2147483647" forKey:@"buildActionMask"];
    [embeddedConfig setObject:@"" forKey:@"dstPath"];
    [embeddedConfig setObject:@"10" forKey:@"dstSubfolderSpec"];
    [embeddedConfig setObject:@"Embed Frameworks" forKey:@"name"];
    [embeddedConfig setObject:@"0" forKey:@"runOnlyForDeploymentPostprocessing"];
    [embeddedConfig setObject:@[[member embeddedKey]] forKey:@"files"];

    NSString *embeddedKey = [[XCKeyBuilder forItemNamed:[_name stringByAppendingString:@".EmbedFrameworks"]] build];
    
    NSMutableArray *buildPhases = [NSMutableArray arrayWithArray:[target objectForKey:@"buildPhases"]];
    [buildPhases addObject:embeddedKey];
    
    [target setObject:buildPhases forKey:@"buildPhases"];
    
    [[_project objects] setObject:target forKey:_key];
    
    [[_project objects] setObject:embeddedConfig forKey:embeddedKey];
    

    }
    [self flagMembersAsDirty];
    }

***_in XCSourceFile.m_

  • (void)becomeEmbedded
    {
    if (_type == Framework) {
    NSMutableDictionary *sourceBuildFile = [NSMutableDictionary dictionary];
    sourceBuildFile[@"isa"] = [NSString xce_stringFromMemberType:PBXBuildFileType];
    sourceBuildFile[@"fileRef"] = _key;

    NSDictionary *dict = [NSDictionary dictionaryWithObject:@[@"CodeSignOnCopy",@"RemoveHeadersOnCopy"] forKey:@"ATTRIBUTES"];
    
    sourceBuildFile[@"settings"] = dict;
    
    NSString *buildFileKey = [[XCKeyBuilder forItemNamed:[_name stringByAppendingString:@".embedded"]] build];
    [_project objects][buildFileKey] = sourceBuildFile;
    

    }
    }

  • (NSString *)embeddedKey
    {
    if (_embeddedKey == nil) {
    [[_project objects] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
    if ([[obj valueForKey:@"isa"] xce_hasBuildFileType]) {

            if ([[obj valueForKey:@"fileRef"] isEqualToString:_key]) {
    
                if ([[obj valueForKey:@"settings"] allKeys].count > 0) {
                    NSDictionary *settings = [obj valueForKey:@"settings"];
    
                    [settings enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull settingKey, id  _Nonnull settingobj, BOOL * _Nonnull settingStop) {
    
                        if (([settingobj containsObject:@"CodeSignOnCopy"] && [settingobj containsObject:@"RemoveHeadersOnCopy"])) {
    
                            _embeddedKey = [key copy];
    
                            NSLog(@"there is a embedded framework");
                        }
                    }];
                }
            }
        }
    }];
    

    }
    return [_embeddedKey copy];
    }

@akame
Copy link

akame commented Dec 31, 2015

Of course we should change buildFileKey function in XCSourceFile.m
_XCSourceFile.m_

  • (NSString *)buildFileKey
    {
    if (_buildFileKey == nil) {
    [[_project objects] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
    if ([[obj valueForKey:@"isa"] xce_hasBuildFileType]) {
    if ([[obj valueForKey:@"fileRef"] isEqualToString:_key]) {

                if ([[obj valueForKey:@"settings"] allKeys].count > 0) {
                    NSDictionary *settings = [obj valueForKey:@"settings"];
    
                    [settings enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull settingKey, id  _Nonnull settingobj, BOOL * _Nonnull settingStop) {
    
                        if (!([settingobj containsObject:@"CodeSignOnCopy"] && [settingobj containsObject:@"RemoveHeadersOnCopy"])) {
    
                           _buildFileKey = [key copy];
                        }
    
                    }];
                }else{
                     _buildFileKey = [key copy];
                }
            }
        }
    }];
    

    }
    return [_buildFileKey copy];
    }

_XCSourceFile.m_

@akame
Copy link

akame commented Dec 31, 2015

At last I change XcodeBuildConfiguration:

for (NSString *configName in [target configurations]) { //Debug Release

XCProjectBuildConfig *config = [target configurationWithName:configName];

NSString *ldRunPath = @"$(inherited) @executable_path/Frameworks";

[config addOrReplaceSetting:ldRunPath forKey:@"LD_RUNPATH_SEARCH_PATHS"];

}

@hartman

@a83988029
Copy link

@akame thanks! it works.
why not commit to the project to make it better.

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

4 participants