Detecting XPM from Package.swift

Does anyone have a reliable method of detecting if a Package.swift file is being compiled from or running in XPM (Xcode's swift package manager support). I have a set up that requires my targets names to be different when running under Xcode and producing a framework.

I'd like to add a conditional to package manifest that would allow me to change the package's target's names iff its being used from XPM but not when being used by SPM.

Any help and ideas are appreciated :)

In your Package.swift:

import Foundation

if ProcessInfo.processInfo.environment["__CFBundleIdentifier"] == "" {
  print("ℹī¸ Building from XCode")
} else {
  print("ℹī¸ Not building from XCode")

I don't know how stable that is, but it appears to work in Xcode 13.2.1.


@Karl is correct. #if Xcode may work for other users who come to this thread, but it does not look like it will in your case. Here is a post with more information about both. I have been using it in that form for over two years now.