Framework์™€ library์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋‘ ์•Œ์•˜๋‹ค. ๊ทธ๋Ÿผ XCFramework๋Š” ๋ฌด์—‡์ผ๊นŒ?

XCFramework

framework์˜ ๋‹ค์–‘ํ•œ ๋ณ€ํ˜•๋“ค์„ Bundleํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•, Framework๋ฅผ ํ•œ๋ฒˆ๋” Bundling ํ•œ๋‹ค.

Xcode 11๊ณผ ํ•จ๊ป˜ 2019๋…„์— ์ถœ์‹œ๋œ ๊ธฐ๋Šฅ์ด๋‹ค. ๊ธฐ์กด์— Framework๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ AppStore์— ์˜ฌ๋ฆฌ๋Š” ๊ฒฝ์šฐ, simulator architecture๊นŒ์ง€ ๊ฐ™์ด ๋‹ด๊ณ  ์žˆ์–ด์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์—ˆ๋‹ค. strip ์ž‘์—…์„ ํ†ตํ•ด ์›ํ•˜์ง€ ์•Š๋Š” ์•„ํ‚คํ…์ณ๋ฅผ stripํ•˜์—ฌ ๋‹ค์‹œ ์˜ฌ๋ ค์•ผ ํ•œ๋‹ค. (lipo command๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.) ์ด ์ด์œ ๋Š” Universal Framework๊ฐ€ simulator architecture๊นŒ์ง€ ๋‹ด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž˜ ๋ชจ๋ฅธ๋‹ค๋ฉด ํ•ด๋‹น ๊ธ€์„ ์ฝ๊ณ  ์˜ค์ž. ์ด ์ƒํ™ฉ์—์„œ XCFramework๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ์›์ฒœ ๋ด‰์‡„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋จผ์ €, ํ•˜๋‚˜์˜ XCFramework์•ˆ์— simulator target๊ณผ device target์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.

์‹ฌ์ง€์–ด iOS, maciOS, tvOS, watchOS์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋„ ํ•˜๋‚˜๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ Mac App์„ ์œ„ํ•œ Framework์ธ AppKit๊ณผ iOS๋ฅผ ์œ„ํ•œ UIKit๋„ ํ•œ๋ฒˆ์— ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. Client๊ฐ€ ์–ด๋–ค API๋ฅผ ์›ํ•˜๋“  ์ด ํ•˜๋‚˜์˜ XCFramework์•ˆ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋˜ํ•œ ๊ธฐ์กด์—๋Š” Framework๋งŒ Bundle up ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ(์™ผ์ชฝ), ์ด์ œ๋Š” Static Library๋„ header์™€ ํ•จ๊ป˜ ๋ฌถ์–ด Bundlingํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹น์—ฐํžˆ, XCFramwork๋Š” Swift, C-Based code์— ๋Œ€ํ•ด Binary Distribution์„ ์ง€์›ํ•œ๋‹ค.

์ •๋ฆฌ

  • Muliple Architectures and Platforms๋ฅผ ์œ„ํ•ด ๋‚˜์˜จ ๊ฐœ๋…
  • AppKit๊ณผ UIKit, Simulator์šฉ์„ ํ•œ๋ฒˆ์— ์ง€์› ๊ฐ€๋Šฅ
    • ์—ฌ๋Ÿฌ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ๊ฐ์‹ธ๋Š” ํ˜•ํƒœ
  • ๋”์ด์ƒ simulator architecture strip ๊ณผ์ • ํ•„์š” ์—†์Œ
  • Static library๋„ header์™€ ํ•จ๊ป˜ ๋ฌถ์„ ์ˆ˜ ์žˆ์Œ
    • ์ด ๊ฒฝ์šฐ Xcode๊ฐ€ Project์˜ Header search path๋ฅผ ์ž๋™์œผ๋กœ update ํ•ด์คŒ

Archiving Framework

XCFramework๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ผ๋‹จ binary๋กœ ๋‚˜์˜จ ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋จผ์ €, Project์˜ Build Option์—์„œ Build Libraries for Distribution ์˜ต์…˜์„ ์ผœ์ฃผ์ž. ๋‚ด๊ฐ€ ์ž‘์—…ํ•˜๋Š” ๋…€์„์ด library๋กœ ๋ฐฐํฌ๋  ๊ฒƒ์ธ์ง€ ํ•ญ์ƒ ์ผœ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์žŠ์ง€ ๋ง์ž.

xcodebuild archive command๋ฅผ ํ†ตํ•ด ์•„์นด์ด๋น™ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ ๋‚ด์— ์›ํ•˜๋Š” Scheme์„ ์„ ํƒํ•˜๊ณ , Compileํ•˜๊ณ  ์‹ถ์€ destination์„ ์„ค์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, iOS ๋””๋ฐ”์ด์Šค์™€ simulator, mac์— ๋Œ€์‘๋˜๋Š” ๋นŒ๋“œ product๋ฅผ ์›ํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ๋ถ€๊ทผ์— argument๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ Skip Install ๋นŒ๋“œ ์„ธํŒ…์„ No๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ๊ฒฐ๊ณผ Archive์— Framework๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

์ด ์ž‘์—…์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๋ณ€ํ˜•๋œ Archive๋ฅผ ๋ฝ‘์•„๋‚ด์—ˆ๋‹ค. ์ฆ‰, ๋‹ค์–‘ํ•œ architecture, device์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” binary๋ฅผ ๋ฝ‘์•„๋‚ด์—ˆ๋‹ค.

Building an XCFramework

์ด์ œ ๊ฐ๊ฐ ๋‹ค๋ฅธ binary framework๊ฐ€ ์žˆ์œผ๋‹ˆ, ์ด๋ฅผ XCFramework๋กœ ๋ฌถ์–ด๋ณด์ž.

xcodebuild -create-cxframework command๋กœ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. framework์—๋Š” archive๋œ ํŒŒ์ผ๋“ค์˜ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋˜๊ณ , output์€ ์ตœ์ข…์ ์œผ๋กœ bundling๋œ XCFramework์˜ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

์ •๋ฆฌํ•˜๋ฉด,

  1. Build Library for Distribution: YES
  2. xcodebuild archive
  3. xcodebuild -xreate-xcframework

Deep inside

์‹ค์ œ๋กœ ๋งŒ๋“ค์–ด์ง„ XCFramework๋ฅผ ์—ด์–ด๋ณด๋ฉด, ๊ฐ๊ฐ์˜ architecture์— ๋”ฐ๋ฅธ framework๋ฅผ nestingํ•œ ํ˜•ํƒœ๋กœ ๋‹ด๊ณ  ์žˆ๋‹ค.

์ •๋ฆฌ

์ง€๊ธˆ๊นŒ์ง€ ์•ฝ 8๊ฐœ ์ •๋„์˜ ๊ธ€์—์„œ binary, framework, bundle ๋“ฑ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ณ  ๋งˆ๋ฌด๋ฆฌ ํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

  • Library vs Framework
    • Libary: Linkable binary
    • Framework: Library with directory for resources and headers
  • Static vs Dynamic
    • Static: build-time linkable binary
      • linkํ›„ output binary์— ํฌํ•จ๋˜์–ด ํ•˜๋‚˜์˜ ์‹คํ–‰ํŒŒ์ผ์ด ๋จ.
    • Dynamic: run-time linkable binary
      • output binary์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ binary์—์„œ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ
      • ์•ฑ์—์„œ๋Š” Dynamic Framework์ธ ๊ฒฝ์šฐ App Package์— embedํ•˜์—ฌ ์‚ฌ์šฉ (@rpath)
  • Universal(fat) binary
    • Multi-Architecture Binary format
    • App store์— uploadํ•˜๋Š” binary์—๋Š” simulator์šฉ architecture๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์•„์•ผ ํ•จ (strip ํ•„์š”)
  • Swift Compatibility
    • ABI Stability: Swift 5.0์ด์ƒ (+ deployment target 13.0์ด์ƒ)
    • Module Stability: Swift 5.1์ด์ƒ (+ Build Libraries For Distribution = Yes)
  • Useful shell commands
    • man(manual) : format and display the on-line manual pages
    • file : determine file type
    • lipo : create or operate on universal files
    • otool : command line parser for llvm-objdump
  • Framework ์ƒ์„ฑ ๋ชฉ์ 
    • ํƒ€ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฐํฌ (3rd-party library)
    • ์•ฑ ์ต์Šคํ…์…˜์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•  ์ฝ”๋“œ ๋ฐ ๋ฆฌ์†Œ์Šค
    • ๋นŒ๋“œ ์†๋„ ๊ฐœ์„  (์•ฑ ๋Ÿฐ์นญ ์†๋„์™€ trade-off ๊ด€๊ณ„์ž„)
    • ๋งˆ์ดํฌ๋กœ ํ”ผ์ณ ์•„ํ‚คํ…์ณ ์ ์šฉ ์‹œ ์ƒ์„ฑ ํ•„์š”

Reference