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

Generated XCFrameworks missing Swift Macros #117

Open
darrarski opened this issue May 7, 2024 · 3 comments
Open

Generated XCFrameworks missing Swift Macros #117

darrarski opened this issue May 7, 2024 · 3 comments

Comments

@darrarski
Copy link

I'm trying to use Scipio to make XCFramework for Composable Architecture. At first, it seemed to be working fine, as I was able to use the generated XCFramework in Xcode project. However, I can't use Swift Macros that Composable Architecture exposes. When trying to do so, I get the following error:

External macro implementation type 'ComposableArchitectureMacros.ReducerMacro' could not be found for macro 'Reducer()'

I'm unsure how to debug the issue, but I found that Tuist does some extra work when creating Xcode targets from swift packages. It adds an additional build phase for each swift package that contains macros. The build scheme looks like this:

Copy Swift Macro executable into $BUILT_PRODUCT_DIR

#  This build phase serves two purposes:
#  - Force Xcode build system to compile the macOS executable transitively when compiling for non-macOS destinations
#  - Place the artifacts in the "Debug" directory where the built artifacts for the active destination live. We default to "Debug" because otherwise the Xcode editor fails to resolve the macro references.
if [[ -f "$BUILD_DIR/$CONFIGURATION/ComposableArchitectureMacros" && ! -f "$BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/ComposableArchitectureMacros" ]]; then
    mkdir -p "$BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/"
    cp "$BUILD_DIR/$CONFIGURATION/ComposableArchitectureMacros" "$BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/ComposableArchitectureMacros"
fi

Input Files: $BUILD_DIR/$CONFIGURATION/ComposableArchitectureMacros
Output Files: $BUILD_DIR/Debug-$EFFECTIVE_PLATFORM_NAME/ComposableArchitectureMacros

I think the above build phase is crucial for the Swift Macros to work, when integrated in a different way than through SPM.

It would be great to use Scipio for generating XCFrameworks, and being able to use Swift Macros. I'm happy to help with further investigation and research possible solutions, but I'm not even sure where to start at the moment.

@darrarski
Copy link
Author

I made some progress. It's nowhere close to a proper solution, but I made it work with some manual hacking, so I guess it's worth sharing. Here are the steps I take to use the XCFramework generated from the Composable Architecture package, along with contained Swift Macros:

  1. I built the Composable Architecture package.
  2. I copied these binary files from the build / derived data directory into some directory, so I can find them later easily:
    • CasePathsMacros
    • ComposableArchitectureMacros
    • DependenciesMacrosPlugin
    • PerceptionMacros
  3. I switched to my Xcode project (the one that uses Composable Architecture XCFramework).
  4. I've added the following script as a build phase to my target. It copies the binary files from the directory mentioned in point 2. into the project's build directory:
    cp "path/to/some/directory/*Macros" "$BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/"
  5. I've added the following "Other Swift Flags" (in the build settings) to my target:

    -load-plugin-executable
    $BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/ComposableArchitectureMacros#ComposableArchitectureMacros
    -load-plugin-executable
    $BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/DependenciesMacrosPlugin#DependenciesMacrosPlugin
    -load-plugin-executable
    $BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/PerceptionMacros#PerceptionMacros
    -load-plugin-executable
    $BUILD_DIR/Debug$EFFECTIVE_PLATFORM_NAME/CasePathsMacros#CasePathsMacros

Now, I'm able to use Composable Architecture XCFramework and use Swift Macros like @Reducer when running my app in iOS Simulator.

The only issue I found so far, is that when I use the macros in my code, Xcode previews are no longer working - fail to build due to the following error:

external macro implementation type 'ComposableArchitectureMacros.ReducerMacro' could not be found for macro 'Reducer()'

The broken previews need investigation, it may be a matter of telling Xcode where to find the macro binaries when building for previews.

I'm not sure if the above process could be fully automated with Scipio. It may require some manual work when integrating generated XCFramework with an Xcode project (like adding a build phase and updating swift compiler flags manually). I think that at least some parts of the process could be automated, like extracting the included macro binaries when generating XCFramework.

@giginet
Copy link
Owner

giginet commented May 8, 2024

Hi. Thank you for opening this issue.

As you said, I've noticed the problem that Scipio couldn't build the package it requires a macro.

Adding -load-plugin-executable options will help us. I'm planning the following steps.

  1. Build macro target to the host OS as a regular executable
  2. Pass -load-plugin-executable as OTHER_LINKER_FLAGS

Scipio uses PIF; the format to represent the Xcode project generated by SwiftPM.
We have to investigate the behavior to build such packages with Xcode build.

swift build --build-system xcode

I'm going to support the Swift Macro. I'll use TCA to test the feature.

@darrarski
Copy link
Author

I did some research and put my findings into a repository. It contains a simple bash script that uses Scipio to build TCA and output XCFrameworks with Swift Macros binaries. There's also an example swift package where I try to integrate the generated frameworks and macros. It mostly works, but there are some issues. I may be missing some steps when building the frameworks, or when integrating with the example swift package.

https://github.com/darrarski/tca-frameworks

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

2 participants