Skip to content

Support for embedding resources in an executable#6067

Merged
neonichu merged 1 commit into
mainfrom
embed-resources
Feb 8, 2023
Merged

Support for embedding resources in an executable#6067
neonichu merged 1 commit into
mainfrom
embed-resources

Conversation

@neonichu

@neonichu neonichu commented Jan 20, 2023

Copy link
Copy Markdown
Contributor

Basic support for a new .embed resource rule which will allow embedding the contents of the resource into the executable code by generating a byte array, e.g.

@_implementationOnly import struct Foundation.Data
    
struct PackageResources {
static let best_txt = Data([104,101,108,108,111,32,119,111,114,108,100,10])
}

Note that the current naïve implementaton will not work well for larger resources as it is pretty memory inefficient.

@neonichu neonichu self-assigned this Jan 20, 2023
@MaxDesiatov

MaxDesiatov commented Jan 20, 2023

Copy link
Copy Markdown
Contributor

Would it be better for performance if it was embedded as a [UInt8] array? That way you don't need to decode base64 at run time. You also don't need to rely on Foundation.Data then.

@neonichu

Copy link
Copy Markdown
Contributor Author

I updated the PR, but I think we'd still want to rely on Data instead of offering just a byte array?

Comment thread Sources/PackageLoading/ManifestJSONParser.swift Outdated
@abertelrud

Copy link
Copy Markdown
Contributor

I updated the PR, but I think we'd still want to rely on Data instead of offering just a byte array?

Although the bundle form of this code generation does rely on Foundation, I think it makes sense for the byte array form to not do so. That makes it useful on, say, new platforms that doesn't have Foundation yet (or is having Foundation now a requirement for any new platform that claims to support Swift? — I've lost track of how core it is now considered to the Swift standard libraries).

Comment thread Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift Outdated
Comment thread Sources/PackageDescription/Resource.swift
@neonichu

Copy link
Copy Markdown
Contributor Author

Updated naming to embedInCode and changed the generated code to bare [UInt8].

@tomerd tomerd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cool 👍

Comment thread Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
Comment thread Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
Comment thread Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
@tomerd

tomerd commented Jan 20, 2023

Copy link
Copy Markdown
Contributor

looks good. I guess its draft since we need some tests?

Basic support for a new `.embed` resource rule which will allow embedding the contents of the resource into the executable code by generating a byte array, e.g.

```
struct PackageResources {
static let best_txt: [UInt8] = [104,101,108,108,111,32,119,111,114,108,100,10]
}
```

Note that the current naïve implementaton will not work well for larger resources as it is pretty memory inefficient.
@neonichu neonichu marked this pull request as ready for review February 8, 2023 01:42
@neonichu neonichu requested a review from elsh as a code owner February 8, 2023 01:42
@neonichu

neonichu commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

Added a test now, so this is ready for review. Note that the test is part of FunctionalTests which is currently disabled so it won't run on CI at this time.

@neonichu

neonichu commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

@swift-ci please smoke test

@neonichu neonichu changed the title WIP: Support for embedding resources in an executable Support for embedding resources in an executable Feb 8, 2023
@neonichu

neonichu commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

Seeing another immediate failure for Windows here, cc @shahmishal

@shahmishal

Copy link
Copy Markdown
Member

@swift-ci test Windows

@neonichu

neonichu commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

@swift-ci smoke test Windows

@neonichu

neonichu commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

@swift-ci smoke test macOS

@neonichu neonichu merged commit 9fbd3fb into main Feb 8, 2023
@neonichu neonichu deleted the embed-resources branch February 8, 2023 16:31
@tomerd

tomerd commented Feb 8, 2023

Copy link
Copy Markdown
Contributor

thanks for adding this great feature @neonichu , should we add an entry in the change log / release note? seems important to communicate this one

@neonichu

neonichu commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

Added in #6132

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.

5 participants