swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.12k stars 100 forks source link

Archive plugin to generate libraries (*.so) #293

Closed fitomad closed 10 months ago

fitomad commented 1 year ago

I've modified the actual archive plugin in order to generate a library from a Swift Package that defines a dynamic library.

There are only three changes

Line 113 - Remove the --static-swift-stdlib compilation parameter

let buildCommand = "swift build -c \(buildConfiguration.rawValue) --product \(product.name)"

Line 119 - Create the appropiate library name: lib[Name].so

let productPath = buildOutputPath.appending("lib\(product.name).so")

330 - Change the filter policy, LibraryProduct instead of ExecutableProduct

self.products = context.package.products.filter { $0 is LibraryProduct }

After those changes the archive plugin generates a library

Captura de pantalla 2023-03-14 a las 12 29 13

Why an issue instead of a PR?

Because modify or add parameters to the current archive command could affect the developers' generation experience and even current CI/CD flows using this plugin.

I think that could a good idea bring both options to developers, generate an executable Lamba or a library that could be deployed in an AWS Lambda Layer and shared between other Lambdas.

Now we use the following command

swift package archive

Maybe we can add an additional parameter that indicates the type of product we want to deploy. The parameter is optional and if it's not presented the archive command presents the current behavior.

swift package archive --type binary
swift package archive --type library

And at last, I don't know if the library generation is under the scope of this project 😀

sebsto commented 10 months ago

Hello @fitomad

The reason why the plugin uses --static-swift-stdlib is because the Swift binary interface on Linux is not stable. Meaning that the binary executable or library produced is not guaranteed to work with a future Swift version.

What is your use case to generate a .so instead of an executable ?

More reading about Swift ABI : https://www.swift.org/blog/abi-stability-and-more/ https://github.com/apple/swift/blob/main/docs/ABIStabilityManifesto.md

fitomad commented 10 months ago

Hi @sebsto

That was just a PoC to generate libraries that could be included in a Lambda Layer.

Just a test 😜