Search code examples
swiftoptimizationswift-package-manager

How to build optimized version of Swift Package using the Swift Package Manager `swift build` command


I want to compile my swift code written in the new Swift Package Manager format into highly optimized binary code. This is possible currently using swiftc -O somefile.swift But since the swift packages are built using swift build command, I cannot pass -O option as it does not accept it. So is there a way to specify to optimize the code during the compile process?


Solution

  • You can build in a release configuration with: swift build --configuration release.

    You can also see the tool usage via:

    $ swift build --help
    OVERVIEW: Build sources into binary products
    
    USAGE: swift build [mode] [options]
    
    MODES:
      -c, --configuration <value>   Build with configuration (debug|release) [default: debug]
      --clean [<mode>]              Delete artifacts (build|dist) [default: build]
    
    OPTIONS:
          -C, --chdir <path>       Change working directory before any other operation
      --build-path <path>      Specify build/cache directory [default: ./.build]
      --color <mode>           Specify color mode (auto|always|never) [default: auto]
      -v, --verbose            Increase verbosity of informational output
      -Xcc <flag>              Pass flag through to all C compiler invocations
      -Xlinker <flag>          Pass flag through to all linker invocations
      -Xswiftc <flag>          Pass flag through to all Swift compiler invocations
    
    NOTE: Use `swift package` to perform other functions on packages
    

    which lists this option.

    See the package manager reference for more information.