Giter VIP home page Giter VIP logo

swift-staticlibs's Introduction

swift-staticlibs

This repo contains scripts for building iOS static frameworks that contain Swift sources from within Xcode

Usage

For each dynamic framework target in Xcode that you would like to be build statically:

  1. Set the undocumented LD and LIPO Xcode build settings to point to these scripts
  2. Add the target to your main target's Link Binary With Libraries build phase.

You can set these settings through a user defined build setting in Xcode, or with a xcconfig file like this:

LD = $(PROJECT_DIR)/path/to/ld.py
LIPO = $(PROJECT_DIR)/path/to/lipo.py

CocoaPods

There are a few issues with using these scripts alongside CocoaPods:

  • With the below configuration, there is no differentiation between targets, if for some reason you didn't want to use these scripts for all targets, you would have to handle that
  • You must delete the [CP] Embed Pods Frameworks build phase from all targets that depend on CocoaPods. Otherwise your final .app bundle will contain frameworks that are not referenced and just bloating your app size
  • If you have any pre-compiled dynamic frameworks that are included with CocoaPods, deleting the Embed Pods Frameworks phase will mean these are no longer included
  • Resources that pods vendor are not handled by these scripts, there are 2 different ways CocoaPods handles resources:
    • If your pods use the resources directive (which is not recommended), your must handle copying the resources, and any conflicts caused by duplicate naming
    • If your pods are using the recommended resource_bundles directive, you still have to handle copying the resource bundle into your targets, otherwise it is ignored

If you feel comfortable working around these issues, you can add something like this to your Podfile:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['LD'] = '$(PROJECT_DIR)/path/to/ld.py'
      config.build_settings['LIPO'] = '$(PROJECT_DIR)/path/to/lipo.py'
    end
  end
end

If you need a more elaborate configuration in CocoaPods, you can use the xcodeproj gem in order to make decisions based on project configuration.

Why?

  1. Xcode has never supported building static libraries (and definitely not static frameworks) containing Swift sources (although this has always been supported by Swift Package Manager)

  2. Dynamic frameworks cause an impact on launch times. While this has improved since the original issues, there's still significant overhead when you have a large amount of modules

  3. By using static libraries (or, in this case, static frameworks), you don't have to deal with all the overhead of dynamic framework loading. See this session from WWDC 2016 for more details around this.

How?

By replacing the libtool and lipo invocations from Xcode, these scripts hijack the passed arguments, and transform them into the arguments necessary for building static frameworks, instead of dynamic frameworks. Then since the product ends up existing in the same place as the dynamic framework that would have otherwise been included, Xcode happily links the static framework instead.

Static frameworks are very similar to dynamic frameworks, except the binary contained within the framework ends up being linked statically, instead of dynamically (see this article for more details). They differ from traditional static libraries in that they have the .framework directory structure identical to dynamic frameworks. This makes it easier to integrate from within Xcode since Xcode already expects the products from dynamic framework targets to follow this structure.

swift-staticlibs's People

Contributors

keith avatar mxcl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.