MobileNativeFoundation / MobileNativeFoundation/rules_xcodeproj

Feature Request: Xcode SwiftUI Preview Support

Open
#3,201 12 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Swift
Stars
626
Forks
124
PR merge metrics
No merged PRs in 30d

Description

Custom Toolchain and Library Linking Implementation

The Problem

The generated projects had critical issues preventing Xcode Preview App (XOJIT) functionality because it depends on:

  1. Missing Library Dependencies: Preview App couldn't find required static libraries (.a, .dylib.. ect) during linking, causing build failures. It parses it from the build phase.
  2. Missing Search Paths: Library search paths weren't properly configured, leading to linker errors. After adding the above, we need to add the search paths for the PreviewApp to find the .a files.
  3. Toolchain Misalignment: SwiftUI Preview prevents us from injecting swiftc stub. We need to geenrate an entire new xctoolchain.

The Solution

Implement a comprehensive custom toolchain system and enhanced library linking through several interconnected components:

1. Custom Toolchain Generation System

Create a fully functional Xcode toolchain that mirrors the system toolchain but allows for specific tool overrides.

2. Enhanced Library Detection and Linking

Improve library discovery to properly detect and collect library dependencies from compilation providers.

3. Build Phase Enhancement

Ensure discovered libraries are properly included in Xcode targets' build phases.

4. Project-Level Configuration

Update project settings to use custom toolchains and proper search paths to find the libs.

Implementation Progress

Custom Toolchain System
  • Toolchain Discovery: Automatic Xcode version detection and version-specific toolchain identifiers (custom_toolchain.bzl)
  • Symlink Creation: Complete toolchain structure using symlinks to system toolchain for compatibility
  • Tool Override Mechanism: Flexible system allowing specific tools (swiftc, clang, etc.) to be replaced with custom implementations
  • Two-Stage Process: Symlinked base toolchain creation followed by override application
  • Template System: Shell script templates for toolchain creation (custom_toolchain_symlink.sh, custom_toolchain_override.sh)
Library Detection Enhancement
  • Comprehensive Library Collection: Proper identification of static libraries (.a), dynamic libraries (.dylib), and frameworks from both Objective-C and C++ compilation providers (linker_input_files.bzl)
  • Search Path Generation: Automatic generation of library search paths based on discovered library locations
  • Dependency Filtering: Intelligent filtering to avoid circular dependencies by excluding target's own libraries
  • Framework File Tracking: Detailed tracking of framework files and their paths for proper linking
Build Phase Integration
  • Link Binary Build Phase: Enhanced CreateLinkBinaryWithLibrariesBuildPhaseObject.swift to generate proper PBXFrameworksBuildPhase
  • Framework Object Creation: New CreateFrameworkObject.swift for framework references in Xcode project
  • Build File Objects: CreateFrameworkBuildFileObject.swift for managing build file references
Runtime Library Support
  • libclang_rt.iossim.a: Hardcoded inclusion of critical runtime library for iOS Simulator builds
  • Search Path Configuration: Automatic addition of runtime library location to search paths
  • Toolchain Integration: Proper integration into custom toolchain structure
Project Configuration
  • Build Settings: Enhanced PBXProjectBuildSettings.swift for custom toolchain usage
  • Toolchain Selection: Generated projects automatically point to custom toolchain
  • Environment Setup: Proper environment configuration for consistent custom toolchain usage
Testing and Validation
  • Unit Tests: Comprehensive test suite in test/internal/custom_toolchain/ for toolchain generation
  • Integration Tests: Updated fixture tests ensuring generated projects work correctly
  • Build Verification: Maintained compatibility with existing build processes
Project Size Optimization (High Priority)
  • Problem Analysis: Current implementation generates individual product/build file references per target dependency, causing bloated project files
  • Product Reference Deduplication: Implement global registry system where each unique library has exactly one product reference regardless of target usage
  • Shared Build File References: Update reference system to reuse existing references when multiple targets link same library
  • Search Path Consolidation: Move library search paths from target-level to project-level configuration
  • Reference Management System: Centralized tracking of target-dependency relationships without duplicating references
  • Testing with Large Projects: Validate improvements with complex project structures

Complexity: Medium (equivalent to 2-3 days focused development)

Dependency Detection Improvements (High Priority)
  • Root Cause Investigation: Analyze why certain dependency types aren't captured by current algorithm
  • Provider Analysis Enhancement: Expand logic to handle complex dependency scenarios, mixed language targets, and transitive dependencies
  • Filtering Logic Refinement: Make library filtering more precise to avoid excluding valid dependencies
  • Debugging Infrastructure: Add comprehensive logging and tracing for dependency discovery process
  • Fallback Mechanisms: Implement alternative discovery paths for edge cases where primary analysis fails
  • Validation System: Cross-reference discovered dependencies with actual linker requirements to identify gaps
  • Edge Case Testing: Create comprehensive test cases for problematic configurations

Complexity: High (equivalent to 3-4 days focused development)

Performance and Polish (Medium Priority)
  • Performance Profiling: Analyze toolchain generation performance and identify bottlenecks
  • File Operation Optimization: Optimize file operations and implement intelligent caching
  • Build Time Impact: Monitor and minimize impact on build times
  • Documentation Updates: Create comprehensive documentation for new features
  • Troubleshooting Guides: Develop guides for common issues and edge cases
  • Monitoring System: Establish monitoring for edge cases and validation failures

Implementation Strategy

Phase 1: Project Size Optimization (Priority: High)

Focus on architectural changes to reference generation system to improve developer experience with large project files.

Approach:

  • Implement shared product reference system across targets
  • Update build file reference generation to reuse existing references
  • Reorganize search paths to project-level configuration
  • Test with existing large projects to validate improvements
Phase 2: Dependency Detection Enhancement (Priority: High)

Systematic analysis and enhancement of dependency discovery to address core functionality gaps.

Approach:

  • Conduct deep investigation into dependency detection failures
  • Create comprehensive test cases for problematic edge cases
  • Implement enhanced dependency detection algorithms
  • Add debugging infrastructure for troubleshooting complex scenarios
Phase 3: Performance and Polish (Priority: Medium)

Performance and developer experience improvements.

Approach:

  • Profile toolchain generation performance and optimize bottlenecks
  • Optimize file operations and implement intelligent caching
  • Create comprehensive documentation and troubleshooting guides
  • Establish monitoring and validation for edge cases

Technical Details

Branch: karim/add-custom-toolchain-to-generated-project

Key Files:

  • xcodeproj/internal/custom_toolchain.bzl - Custom toolchain rule implementation
  • xcodeproj/internal/files/linker_input_files.bzl - Enhanced library detection
  • tools/generators/pbxnativetargets/src/Generator/CreateLinkBinaryWithLibrariesBuildPhaseObject.swift - Build phase generation
  • tools/generators/pbxnativetargets/src/Generator/CreateFrameworkObject.swift - Framework object creation
  • tools/generators/pbxproj_prefix/src/Generator/PBXProjectBuildSettings.swift - Project build settings

Current Status

This implementation provides foundational infrastructure that enables reliable Xcode Preview usage while maintaining Bazel build benefits. The system significantly improves Preview App functionality, with remaining work focused on optimization and edge case handling rather than fundamental architectural changes.

The completed work establishes a solid foundation for iOS development workflows, and the outstanding tasks will complete the feature set for production readiness.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading xcodeproj/internal/custom_toolchain.bzl and xcodeproj/internal/files/linker_input_files.bzl, then inspect the listed Swift generators and tests under test/internal/custom_toolchain/. The issue contains several unfinished workstreams, including reference deduplication and dependency detection; completion requires selecting and scoping one workstream, adding validation for it, and confirming generated Xcode projects support the intended Preview workflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
build-system, devtools, mobile-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.