Saturday, October 14, 2023
HomeiOS DevelopmentDynamic libraries and code replacements in Swift

Dynamic libraries and code replacements in Swift


How one can load a dynamic library and use native methodology swizzling in Swift? This text is all concerning the magic behind SwiftUI previews.

Swift

Dynamic library packages

I’ve already revealed an article about constructing static and dynamic libraries utilizing the Swift compiler, if you do not know what’s a dynamic library or you’re merely a bit extra about how the Swift compiler works, you must undoubtedly check out that put up first.

This time we will focus a bit extra on using the Swift Package deal Supervisor to create our dynamic library merchandise. The setup goes to be similar to the one I’ve created within the loading dynamic libraries at runtime article. First we will create a shared library utilizing SPM.



import PackageDescription

let bundle = Package deal(
    identify: "TextUI",
    merchandise: [
        .library(name: "TextUI", type: .dynamic, targets: ["TextUI"]),
    ],
    dependencies: [
        
    ],
    targets: [
        .target(name: "TextUI", swiftSettings: [
            .unsafeFlags(["-emit-module", "-emit-library"])
        ]),
    ]
)


The bundle manifest is sort of easy, though there are just a few particular issues that we had so as to add. The very very first thing is that we outlined the product kind as a dynamic library. It will be certain that the precise .dylib (or .so / .dll) binary shall be created while you construct the goal. 🎯

The second factor is that we would prefer to emit our Swift module data alongside the library, we are able to inform this to the compiler by means of some unsafe flags. Do not be afraid, these are literally not so harmful to make use of, these flags shall be immediately handed to the Swift compiler, however that is it.



Now the supply code for our TextUI library goes to be quite simple.


public struct TextUI {

    public static dynamic func construct() -> String {
        "Hi there, World!"
    }
}


It is only a struct with one static operate that returns a String worth. Fairly easy, besides one factor: the dynamic key phrase. By including the dynamic modifier to a operate (or methodology) you inform the compiler that it ought to use dynamic dispatch to “resolve” the implementation when calling it.

We will benefit from the dynamic dispatch afterward, however earlier than we might transfer onto that half, now we have to construct our dynamic library and make it accessible for others to make use of. 🔨

Should you run swift construct (or run the undertaking through Xcode) it will construct all of the required information and place them below the right construct folder. You can too print the construct folder by working the swift construct -c launch --show-bin-path (-c launch is for launch builds, we will construct the library utilizing the discharge configuration for apparent causes… we’re releasing them). Should you checklist the contents of the output listing, you must discover the next information there:

  • TextUI.swiftdoc
  • TextUI.swiftmodule
  • TextUI.swiftsourceinfo
  • libTextUI.dylib
  • libTextUI.dylib.dSYM

So, what can we do with this construct folder and the output information? We will want them below a location the place the construct instruments can entry the associated information, for the sake of simplicity we will put the whole lot into the /usr/native/lib folder utilizing a Makefile.

PRODUCT_NAME := "TextUI"
DEST_DIR := "/usr/native/lib/"
BUILD_DIR := $(shell swift construct -c launch --show-bin-path)

set up: clear
    @swift construct -c launch
    @set up "$(BUILD_DIR)/lib$(PRODUCT_NAME).dylib" $(DEST_DIR)
    @cp -R "$(BUILD_DIR)/lib$(PRODUCT_NAME).dylib.dSYM" $(DEST_DIR)
    @set up "$(BUILD_DIR)/$(PRODUCT_NAME).swiftdoc" $(DEST_DIR)
    @set up "$(BUILD_DIR)/$(PRODUCT_NAME).swiftmodule" $(DEST_DIR)
    @set up "$(BUILD_DIR)/$(PRODUCT_NAME).swiftsourceinfo" $(DEST_DIR)
    @rm ./lib$(PRODUCT_NAME).dylib
    @rm -r ./lib$(PRODUCT_NAME).dylib.dSYM

uninstall: clear
    
    @rm $(DEST_DIR)lib$(PRODUCT_NAME).dylib
    @rm -r $(DEST_DIR)lib$(PRODUCT_NAME).dylib.dSYM
    @rm $(DEST_DIR)$(PRODUCT_NAME).swiftdoc
    @rm $(DEST_DIR)$(PRODUCT_NAME).swiftmodule
    @rm $(DEST_DIR)$(PRODUCT_NAME).swiftsourceinfo

clear:
    @swift bundle clear


Now when you run make or make set up all of the required information shall be positioned below the precise location. Our dynamic library bundle is now prepared to make use of. The one query is how will we devour this shared binary library utilizing one other Swift Package deal goal? 🤔



Linking towards shared libraries

We will construct a model new executable software known as TextApp utilizing the Swift Package deal Supervisor. This bundle will use our beforehand created and put in shared dynamic library.



import PackageDescription

let bundle = Package deal(
    identify: "TextApp",
    targets: [
        .target(name: "TextApp", swiftSettings: [
            .unsafeFlags(["-L", "/usr/local/lib/"]),
            .unsafeFlags(["-I", "/usr/local/lib/"]),
            .unsafeFlags(["-lTextUI"]),
        ], linkerSettings: [
            .unsafeFlags(["-L", "/usr/local/lib/"]),
            .unsafeFlags(["-I", "/usr/local/lib/"]),
            .unsafeFlags(["-lTextUI"]),
        ]),
    ]
)


The trick is that we are able to add some flags to the Swift compiler and the linker, in order that they’ll know that we have ready some particular library and header (modulemap) information below the /usr/native/lib/ folder. We might additionally prefer to hyperlink the TextUI framework with our software, with a purpose to do that now we have to move the identify of the module as a flag. I’ve already defined these flags (-L, -I, -l) in my earlier posts so I suppose you are conversant in them, if not please learn the linked articles. 🤓


import TextUI

print(TextUI.construct())

Our most important.swift file is fairly easy, we simply print the results of the construct methodology, the default implementation ought to return the well-known “Hi there, World!” textual content.

Are you prepared to exchange the construct operate utilizing native methodology swizzling in Swift?



Dynamic methodology alternative

After publishing my unique plugin system associated article, I’ve bought an electronic mail from one in every of my readers. Initially thanks for letting me know concerning the @_dynamicReplacement attribute Corey. 🙏

The factor is that Swift helps dynamic methodology swizzling out of the field, though it’s by means of a non-public attribute (begins with an underscore), which implies it isn’t prepared for public use but (yeah… similar to @_exported, @_functionBuilder and the others), however ultimately it will likely be finalized.

You possibly can learn the unique dynamic methodology alternative pitch on the Swift boards, there’s additionally this nice little snippet that comprises a minimal showcase concerning the @_dynamicReplacement attribute.

Lengthy story brief, you need to use this attribute to override a customized dynamic methodology with your individual implementation (even when it comes from a dynamically loaded library). In our case we have already ready a dynamic construct methodology, so if we attempt we are able to override that the next snippet.


import TextUI

extension TextUI {

    @_dynamicReplacement(for: construct())
    static func _customBuild() -> String {
        "It simply works."
    }
}

print(TextUI.construct()) 


Should you alter the most important.swift file and run the undertaking you must see that even we’re calling the construct methodology, it’ll be dispatched dynamically and our _customBuild() methodology shall be known as below the hood, therefore the brand new return worth.

It really works like a allure, however can we make this much more dynamic? Is it attainable to construct yet another dynamic library and cargo that at runtime, then substitute the unique construct implementation with the dynamically loaded lib code? The reply is sure, let me present you ways to do that. 🤩



import PackageDescription

let bundle = Package deal(
    identify: "TextView",
    merchandise: [
        .library(name: "TextView", type: .dynamic, targets: ["TextView"]),
    ],
    targets: [
        .target(name: "TextView", swiftSettings: [
            .unsafeFlags(["-L", "/usr/local/lib/"]),
            .unsafeFlags(["-I", "/usr/local/lib/"]),
            .unsafeFlags(["-lTextUI"]),
        ], linkerSettings: [
            .unsafeFlags(["-L", "/usr/local/lib/"]),
            .unsafeFlags(["-I", "/usr/local/lib/"]),
            .unsafeFlags(["-lTextUI"]),
        ]),
    ]
)

Similar SPM sample, we have simply created a dynamic library and we have used the TextUI as a shared library so we are able to place our TextUI extension into this library as a substitute of the TextApp goal.

Up to now we have created 3 separated Swift packages shared the TextUI module between the TextApp and the TextView packages as a pre-built dynamic library (utilizing unsafe construct flags). Now we will prolong the TextUI struct inside our TextView bundle and construct it as a dynamic library.


import TextUI

extension TextUI {

    @_dynamicReplacement(for: construct())
    static func _customBuild() -> String {
        "It simply works."
    }
}


We are able to use an analogous makefile (to the earlier one) or just run the swift construct -c launch command and duplicate the libTextView.dylib file from the construct listing by hand.

Should you run this code utilizing Linux or Home windows, the dynamic library file shall be known as libTextView.so below Linux and libTextView.dll on Home windows.

So simply place this file below your property listing we will want the complete path to entry it utilizing the TextApp’s most important file. We will use the dlopen name to load the dylib, this may substitute our construct methodology, then we shut it utilizing dlclose (on the supported platforms, extra on this later…).


import Basis
import TextUI


print(TextUI.construct())


let dylibPath = "/Customers/tib/libTextView.dylib"
guard let dylibReference = dlopen(dylibPath, RTLD_LAZY) else {
    if let err = dlerror() {
        fatalError(String(format: "dlopen error - %s", err))
    }
    else {
        fatalError("unknown dlopen error")
    }
}
defer {
    dlclose(dylibReference)
}


print(TextUI.construct())


The wonderful thing about this method is that you do not have to fiddle with further dlsym calls and unsafe C pointers. There may be additionally a pleasant and detailed article about Swift and native methodology swizzling, this focuses a bit extra on the emitted replacements code, however I discovered it a really nice learn.

Sadly there may be yet another factor that now we have to speak about…



Drawbacks & conclusion

Dynamic methodology alternative works good, this method is behind SwiftUI stay previews (or dlsym with some pointer magic, however who is aware of this for positive..). Anyway, the whole lot appears nice, till you begin involving Swift courses below macOS. What’s fallacious with courses?

Seems that the Goal-C runtime will get concerned below macOS when you compile a local Swift class. Simply compile the next instance supply and check out it utilizing the nm device.



class A {}



Below macOS the output of nm will comprise traces of the Goal-C runtime and that’s greater than sufficient to trigger some troubles through the dylib shut course of. Seems in case your library comprises the ObjC runtime you will not have the ability to really shut the dylib, it doesn’t matter what. ⚠️


Previous to Mac OS X 10.5, solely bundles might be unloaded. Beginning in Mac OS X 10.5, dynamic libraries can also be unloaded. There are a
couple of instances by which a dynamic library won’t ever be unloaded: 1) the principle executable hyperlinks towards it, 2) an API that doesn’t assist
unloading (e.g. NSAddImage()) was used to load it or another dynamic library that depends upon it, 3) the dynamic library is in dyld’s
shared cache.


Should you check out man 3 dlclose you will get just a few extra hints concerning the causes, plus you too can verify the supply code of the Goal-C runtime, if you wish to see extra particulars.


Anyway I believed this needs to be talked about, as a result of it could actually trigger some hassle (solely on macOS), however the whole lot works simply nice below Linux, so in case you are planning to make use of this method on the server facet, then I might say it will work simply nice. It is not secure, nevertheless it ought to work. 😈


Oh, I nearly overlook the hot-reload performance. Properly, you possibly can add a listing or file watcher that may monitor your supply codes and if one thing adjustments you possibly can re-build the TextView dynamic library then load the dylib once more and name the construct methodology if wanted. It is comparatively simple after you’ve got tackled the dylib half, as soon as you determine the smaller particulars, it really works like magic. 🥳







Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments