Skip to content

Commit c0abe0b

Browse files
Merge pull request #61713 from AnthonyLatsis/ninja-with-xcode
[docs] Improve instructions for using Ninja with Xcode & move them to GettingStarted.md
2 parents e98380c + 712dc7b commit c0abe0b

File tree

2 files changed

+78
-45
lines changed

2 files changed

+78
-45
lines changed

docs/DevelopmentTips.md

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,5 @@ By default, `build-script` will spawn as many parallel compile / link jobs as th
9797
For example, to have `build-script` spawn only one link job at a time, we can invoke it as:
9898

9999
```
100-
build-script --llvm-cmake-options==-DLLVM_PARALLEL_LINK_JOBS=1 --swift-cmake-options=-DSWIFT_PARALLEL_LINK_JOBS=1
100+
build-script --llvm-cmake-options=-DLLVM_PARALLEL_LINK_JOBS=1 --swift-cmake-options=-DSWIFT_PARALLEL_LINK_JOBS=1
101101
```
102-
103-
## Using ninja with Xcode
104-
105-
Although it's possible to build the swift compiler entirely with Xcode (`--xcode`), often it's better to build with _ninja_ and use Xcode for editing and debugging.
106-
This is very convenient because you get the benefits of the ninja build system and all the benefits of the Xcode IDE, like code completion, refactoring, debugging, etc.
107-
108-
To setup this environment a few steps are necessary:
109-
* Create a new workspace.
110-
* Create Xcode projects for LLVM and Swift with `utils/build-script --skip-build --xcode --skip-early-swift-driver`. Beside configuring, this needs to build a few LLVM files which are needed to configure the swift project.
111-
* Add the generated LLVM and Swift projects to your workspace. They can be found in the build directories `build/Xcode-DebugAssert/llvm-macosx-x86_64/LLVM.xcodeproj` and `build/Xcode-DebugAssert/swift-macosx-x86_64/Swift.xcodeproj`.
112-
* Add the `swift/SwiftCompilerSources` package to the workspace.
113-
* Create a new empty project `build-targets` (or however you want to name it) in the workspace, using the "External Build System" template.
114-
* For each compiler tool you want to build (`swift-frontend`, `sil-opt`, etc.), add an "External Build System" target to the `build-targets` project.
115-
* In the "Info" section of the target configuration, set:
116-
* the _Build Tool_ to the full path of the `ninja` command
117-
* the _Argument_ to the tool name (e.g. `swift-frontend`)
118-
* the _Directory_ to the ninja swift build directory, e.g. `/absolute/path/to/build/Ninja-DebugAssert/swift-macosx-x86_64`. For debugging to work, this has to be a debug build of course.
119-
* For each target, create a new scheme:
120-
* In the _Build_ section add the corresponding build target that you created before.
121-
* In the _Run/Info_ section select the built _Executable_ in the build directory (e.g. `/absolute/path/to/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift-frontend`).
122-
* In the _Run/Arguments_ section you can set the command line arguments with which you want to run the compiler tool.
123-
* In the _Run/Options_ section you can set the working directory for debugging.
124-
125-
Now you are all set. You can build and debug like with a native Xcode project.
126-
127-
If the project structure changes, e.g. new source files are added or deleted, you just have to re-create the LLVM and Swift projects with `utils/build-script --skip-build --xcode --skip-early-swift-driver`.

docs/HowToGuides/GettingStarted.md

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ toolchain as a one-off, there are a couple of differences:
2525
- [Editing code](#editing-code)
2626
- [Setting up your fork](#setting-up-your-fork)
2727
- [First time Xcode setup](#first-time-xcode-setup)
28+
- [Using Ninja with Xcode](#using-ninja-with-xcode)
2829
- [Other IDEs setup](#other-ides-setup)
2930
- [Editing](#editing)
3031
- [Incremental builds with Ninja](#incremental-builds-with-ninja)
@@ -274,24 +275,6 @@ In the following sections, for simplicity, we will assume that you are using a
274275
unless explicitly mentioned otherwise. You will need to slightly tweak the paths
275276
for other build configurations.
276277

277-
### Using both Ninja and Xcode
278-
279-
Some contributors find it more convenient to use both Ninja and Xcode.
280-
Typically this configuration consists of:
281-
282-
1. A Ninja build created with `--release-debuginfo`.
283-
2. An Xcode build created with `--release-debuginfo --debug-swift`.
284-
285-
The Ninja build can be used for fast incremental compilation and running tests
286-
quickly. The Xcode build can be used for debugging with high fidelity.
287-
288-
The additional flexibility comes with two issues: (1) consuming much more disk
289-
space and (2) you need to maintain the two builds in sync, which needs extra
290-
care when moving across branches.
291-
292-
### Integrate a Ninja build with Xcode
293-
It is possible to integrate the Ninja build into Xcode. For details on how to set this up see [Using Ninja with Xcode in DevelopmentTips.md](/docs/DevelopmentTips.md#using-ninja-with-xcode).
294-
295278
### Troubleshooting build issues
296279

297280
- Double-check that all projects are checked out at the right branches.
@@ -372,6 +355,82 @@ select the following schemes:
372355
run.
373356
<!-- TODO: Insert SourceKit/stdlib specific instructions? -->
374357
358+
### Using Ninja with Xcode
359+
360+
Although it's possible to build the toolchain entirely with Xcode via `--xcode`,
361+
a more efficient and robust option is to integrate a Ninja build with Xcode.
362+
This is also convenient in that you can navigate, build, run, edit, and debug in
363+
Xcode while retaining the option of using Ninja on the command line.
364+
365+
Assuming that you have already [built the toolchain via Ninja](#the-actual-build),
366+
several more steps are necessary to set up this environment:
367+
* Generate Xcode projects with `utils/build-script --skip-build --xcode --skip-early-swift-driver`.
368+
This will first build a few LLVM files that are needed to configure the
369+
projects.
370+
* Create a new Xcode workspace.
371+
* Add the generated Xcode projects or Swift packages that are relevant to your
372+
tasks to your workspace. All the Xcode projects can be found among the
373+
build artifacts in `build/Xcode-DebugAssert`. For example:
374+
* If you are aiming for the compiler, add `build/Xcode-DebugAssert/swift-macosx-*/Swift.xcodeproj`.
375+
This project also includes the standard library and runtime sources. If you
376+
need the parts of the compiler that are implemented in Swift itself, add the
377+
`swift/SwiftCompilerSources/Package.swift` package as well.
378+
* If you are aiming for just the standard library or runtime, add
379+
`build/Xcode-DebugAssert/swift-macosx-*/stdlib/Swift-stdlib.xcodeproj`.
380+
<!-- FIXME: Without this "hard" line break, the note doesn’t get properly spaced from the bullet -->
381+
<br />
382+
383+
> **Warning**
384+
> Adding both `Swift.xcodeproj` and `LLVM.xcodeproj` *might* slow down the IDE
385+
and is not recommended unless you know what you're doing.
386+
387+
In general, we encourage you to add only what you need. Keep in mind that none
388+
of the generated Xcode projects are required to build or run with this setup
389+
because we are using Ninja—an *external* build system; rather, they should be
390+
viewed as a means of leveraging the navigation, editing and debugging features
391+
of the IDE in relation to the source code they wrap.
392+
393+
* Create an empty Xcode project in the workspace, using the
394+
_External Build System_ template.
395+
* For a Ninja target that you want to build (e.g. `swift-frontend`), add a
396+
target to the empty project, using the _External Build System_ template.
397+
* In the _Info_ pane of the target settings, set
398+
* _Build Tool_ to the path of the `ninja` executable (the output of
399+
`which ninja` on the command line)
400+
* _Arguments_ to the Ninja target name (e.g. `swift-frontend`)
401+
* _Directory_ to the path of the build directory associated with the Ninja
402+
target. For Swift targets, including the standard library and runtime, you
403+
want `path/to/swift-project/build/Ninja-*/swift-macosx-*`
404+
* Add a scheme for the target. Be sure not to select a target from one the
405+
generated Xcode projects.
406+
* > **Note**
407+
> Ignore this step if the target associates to a non-executable Ninja target
408+
like `swift-stdlib`.
409+
410+
Adjust the _Run_ action settings of the scheme:
411+
* In the _Info_ pane, select the _Executable_ built by the Ninja target from
412+
the appropriate `bin` directory (e.g. `build/Ninja-*/swift-macosx-*/bin/swift-frontend`).
413+
* In the _Arguments_ pane, add the command line arguments that you want to
414+
pass to the executable on launch (e.g. `path/to/file.swift -typecheck` for
415+
`swift-frontend`).
416+
* You can optionally set the working directory for debugging in the
417+
_Options_ pane.
418+
* Configure as many more target-scheme pairs as you need.
419+
420+
Now you are all set! You can build, run and debug as with a native Xcode
421+
project. If an `update-checkout` routine or a structural change—such as when
422+
source files are added or deleted—happens to impact your editing experience,
423+
simply regenerate the Xcode projects.
424+
425+
> **Note**
426+
> * For debugging to *fully* work for a given component—say, the compiler—the
427+
`build-script` invocation for the Ninja build must be arranged to
428+
[build a debug variant of that component](#debugging-issues).
429+
> * Xcode's indexing can occasionally start slipping after switching to and back
430+
from a distant branch, resulting in a noticeable slowdown. To sort things
431+
out, close the workspace and delete the _Index_ directory from the
432+
workspace's derived data before reopening.
433+
375434
### Other IDEs setup
376435
377436
You can also use other editors and IDEs to work on Swift.

0 commit comments

Comments
 (0)