Xcode의 Target과 Scheme에 대해 자세히 알아봅시다.
Target과 Scheme을 통해서 Xcode의 빌드 및 실행 과정을 관리할 수 있습니다.
각각 무엇을 세팅할 수 있고 어떻게 사용할 수 있는지 알아봅시다.
Target
Build System
Target을 이해하기 위해서는 Xcode의 Build System에 대해 먼저 알면 좋습니다.
Build System은 코드와 리소스 파일을 App, Framework, Library 등의 제품으로 변환시키는 도구를 관리합니다.
간단하게는 Compiler를 관리한다고 생각해도 좋습니다.
조금 더 자세하게 들여다보면 Compiler를 포함하여 Linking Module, Packaging 등 제품 Build Process 전체를 관리합니다.

Target은 Build System에 제품을 빌드하는 방법과 지침(Instructions)을 정의하는 요소입니다.
Target은 App, Framework, Library 같은 하나의 제품을 빌드하는 데 필요한 코드, 리소스, 빌드 설정 등의 지침(Instructions)으로 구성됩니다. Unit Test, UI Test를 위한 Bundle도 Target에 포함됩니다.
Target = Single Product(단일 제품)이며 하나의 프로젝트에 여러 개의 Target이 존재할 수 있습니다.


Target에 포함된 Instructions는 Build Settings, Build Phases, Build Rules가 있습니다.

Build Settings

기본적으로 Target은 Project의 Build Settings 값을 상속합니다.
하지만 특정 Target의 Build Settings에서 별도로 값을 지정할 수도 있으며, 여러 Target이 공유하는 값을 지정할 수도 있습니다.
Target은 다른 Target과 관련이 있을 수 있습니다.
예를 들어 UnitTest Target을 빌드하기 위해서는 테스트할 코드가 포함된 Target(App, Framework 등)의 출력이 반드시 필요합니다.
이러한 경우를 UnitTest Target이 테스트할 코드가 포함된 Target에 종속된다고 표현합니다.
Target이 모두 같은 Workspace에 존재하는 경우, Xcode는 Target 간의 종속성을 탐색할 수 있기 때문에 필요한 순서대로 Target을 빌드합니다. 이러한 관계를 Implicit Dependency(암시적 종속성)이라고 합니다.
물론 Build Settings에서 명시적(Explicit) 종속성을 지정할 수도 있습니다.
Build Phases
Build System의 Process는 기본적으로 소스 코드 컴파일, 바이너리 링크, 리소스 복사 등의 단계로 나뉩니다.
각 Phases별 소스를 설정하거나, Phases를 추가할 수 있습니다.

Build Phases에는 Dependyncies, Compile Sources, Link Binary With Libraries, Copy Bundle Resources, Headers, Copy Files, Run Script가 있으며, 반드시 나열된 순서대로 실행되는 것은 아닙니다.
Dependyncies: 현재 Target을 빌드하기 전에 종속성이 있는 Target들이 먼저 빌드되도록 합니다.
Compile Sources: 컴파일될 소스 코드 파일을 지정하고, Object file로 컴파일 합니다.
Link Binary With Libraries: Object file을 외부 Library나 Framework와 연결합니다.
Copy Bundle Resources: 이미지, XIB, Storyboard 같은 리소스를 Bundle에 복사합니다.
Headers: Public 또는 Private Header File을 지정합니다. 특정 파일을 Target 간 공유하거나, 특정 Target 내에서만 사용되도록 합니다.
Copy Files: Configuration File들을 지정된 Directory에 복사합니다.
Run Script: Build Phases 중간중간에 필요에 따라 Custom Shell Script나 Command를 실행하여 코드 포맷팅이나 파일 생성 등의 작업을 할 수 있습니다.
Build Rules
특정 타입의 파일을 빌드할 때, 어떻게 처리될지를 정의합니다.
단순한 예시로 Swift 파일은 Swift 컴파일러를 사용해야합니다.
Xcode는 기본적으로 Swift, Objective-C, C 등의 언어를 파악하고 컴파일할 수 있지만, 그외 독립적으로 처리해야하는 파일이 있는 경우에 Build Rules를 활용합니다.

외부 Shell Script, 환경 변수 등을 사용하거나, 외부 파일에 접근해야하는 경우에도 사용됩니다.
Scheme
Scheme은 빌드할 Target Collection, 빌드 시 사용할 Configuration, 실행할 Test Collection을 정의합니다.
따라서 Scheme 설정은 Target을 빌드하고 실행하는 과정을 제어합니다.
이를 통해 Development, Production등의 환경에 따라 빌드 환경 요소를 설정하여 사용할 수 있습니다.
여러개의 Scheme을 생성할 수 있지만, 한번에 하나의 Scheme만 활성화할 수 있습니다.

Scheme은 Project 또는 Workspace 중 한 곳을 선택하여 생성할 수 있습니다.

활성화된 Scheme을 선택하고 빌드될 기기(Device, 플랫폼) 및 Architecture도 선택합니다.

Scheme은 Build, Run, Test, Profile, Analyze, Archive의 Action을 포함합니다.
각 Action마다 Pre- 또는 Post- Action을 설정할 수 있습니다.

각 Action은 Build Configuration을 지정할 수 있습니다. Build Configuration은 Debug와 Release로 구성됩니다.
Debug는 일반적으로 더 빠른 빌드를 위해 코드 최적화를 비활성화하고, Debugging 정보를 생성합니다.
Release는 런타임 성능을 중시하여 코드 최적화를 활성화하고, Debugging 정보 생성을 비활성화하여 제품의 크기를 줄입니다.
Scheme Actions
Build: 빌드할 Target을 선택하고, 해당 Target이 빌드될 때 함께 빌드될 Target과 종속성에 따른 빌드 순서를 설정합니다. 또한 Target 마다 Build 시에 작동할 Action을 선택할 수 있습니다.
Run: Build Configuration을 지정하고, 제품 실행 시 사용될 Arguments나 환경 변수를 설정합니다.
Test: Build Configuration을 지정하고, 실행할 Test Plan(Unit Test, UI Test)을 지정합니다.
Profile: Build Configuration을 지정하고,
Analyze: Instruments 도구에서 수집할 정보를 설정합니다.
Archive: 배포를 위한 최종 빌드를 생성하여 저장합니다. 제품(Target)에 따라 IPA, XCFramework 등을 생성하거나 Archive를 만듭니다.
Conclusion
정리해보자면 Target은 빌드 후 최종 산출될 Bundle(Package)을 정의하는 개념으로 Build System에 전달될 소스 코드(바이너리), 리소스, 메타데이터, 외부 Framework와 Library을 설정합니다.
Scheme은 Target의 빌드, 실행, 테스트, 배포 등 프로세스에 대한 설정을 정의하는 개념입니다.
두 개념을 이해하고, 적절히 활용하면 개인 혹은 조직에 맞는 개발 플로우를 설정할 수 있습니다.
Reference
https://developer.apple.com/documentation/xcode/build-system
Build system | Apple Developer Documentation
Compile your code into a binary format, and customize your project settings to build your code.
developer.apple.com
https://developer.apple.com/library/archive/featuredarticles/XcodeConcepts/Concept-Targets.html
Xcode Target
Xcode Target A target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. A target defines a single product; it organizes the inputs into the build system—the source files and
developer.apple.com
https://developer.apple.com/documentation/xcode/configuring-the-build-settings-of-a-target
Configuring the build settings of a target | Apple Developer Documentation
Specify the options you use to compile, link, and produce a product from a target, and identify settings inherited from your project or the system.
developer.apple.com
https://developer.apple.com/documentation/xcode/customizing-the-build-phases-of-a-target
Customizing the build phases of a target | Apple Developer Documentation
Specify the tasks to perform during a build, including the source files to compile, the scripts to run, and the resources to include in the final product.
developer.apple.com
https://developer.apple.com/documentation/xcode/creating-build-rules-for-custom-file-types
Creating build rules for custom file types | Apple Developer Documentation
Tell Xcode how to build your project’s custom file types, and provide dependency information to optimize the build process for each file.
developer.apple.com
https://developer.apple.com/library/archive/featuredarticles/XcodeConcepts/Concept-Schemes.html
Xcode Scheme
Xcode Scheme An Xcode scheme defines a collection of targets to build, a configuration to use when building, and a collection of tests to execute. You can have as many schemes as you want, but only one can be active at a time. You can specify whether a sch
developer.apple.com
https://developer.apple.com/documentation/xcode/customizing-the-build-schemes-for-a-project
Customizing the build schemes for a project | Apple Developer Documentation
Specify which targets to build, and customize the settings Xcode uses to build, run, test, and profile those targets.
developer.apple.com
https://developer.apple.com/documentation/xcode/building-and-running-an-app
Building and running an app | Apple Developer Documentation
Compile your source files and assemble an app bundle to run on a device or simulator.
developer.apple.com
'iOS > iOS' 카테고리의 다른 글
[iOS] Swift Static Framework 만들기(Universal) - 2. XCFramework (1) | 2024.10.22 |
---|---|
[iOS] Project & Workspace (1) | 2024.10.07 |
[iOS] Xcode 15 Rosetta (0) | 2024.01.30 |
[iOS] CI/CD 환경 구축하기 - 2. fastlane match & CI/CD in Git Actions (0) | 2024.01.12 |
[iOS] CI/CD 환경 구축하기 - 1. Tuist in Git Actions & fastlane (0) | 2024.01.09 |