Skip to content

Commit e5daf70

Browse files
committed
[Documentation] Add C language targets documentation
- <rdar://problem/28318112> SR-2638 [SwiftPM] Document C language targets in actual docs
1 parent 44f25f9 commit e5daf70

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Documentation/Reference.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [Debug](#debug)
1919
* [Release](#release)
2020
* [Depending on Apple Modules](#depending-on-apple-modules)
21+
* [C language targets](#c-language-targets)
2122
* [Resources](Resources.md)
2223

2324
---
@@ -53,6 +54,22 @@ and `swift build` will now produce an:
5354

5455
executable output file.
5556

57+
The C language modules are laid out in a similar format. A C language library named `Baz` can be created in following format:
58+
59+
example/Sources/Baz/Baz.c
60+
example/Sources/Baz/include/Baz.h
61+
62+
The public headers for this library go in the directory named `include`.
63+
64+
Similarly, an executable C language module named `Baz` would look like this:
65+
66+
example/Sources/Baz/main.c
67+
68+
Note: It is possible to have C, C++, Objective-C and Objective-C++ sources as part of a C language target. Swift modules can
69+
import C language targets but not vice versa.
70+
71+
Read more on C language targets [here](#c-language-targets).
72+
5673
### Test Suite Layouts
5774

5875
The package manager supports laying out test sources following a similar convention as primary sources:
@@ -341,3 +358,18 @@ A C language target is build with following flags in release mode:
341358
### Depending on Apple Modules
342359

343360
At this time there is no explicit support for depending on Foundation, AppKit, etc, though importing these modules should work if they are present in the proper system location. We will add explicit support for system dependencies in the future. Note that at this time the Package Manager has no support for iOS, watchOS, or tvOS platforms.
361+
362+
## C language targets
363+
364+
The C language targets are laid out similar to Swift targets execept that the C langauge libraries should contain a directory named `include` to hold the public headers.
365+
To allow a Swift module to import a C language module, add a [target dependency](#targets) in the manifest file. Swift Package Manager will automatically generate a modulemap for each C language library module for these 3 cases:
366+
367+
* If `include/Foo/Foo.h` exists and `Foo` is the only directory under the include directory then `include/Foo/Foo.h` becomes the umbrella header.
368+
369+
* If `include/Foo.h` exists and `include` contains no other subdirectory then `include/Foo.h` becomes the umbrella header.
370+
371+
* Otherwise if the `include` directory only contains header files and no other subdirectory, it becomes the umbrella directory.
372+
373+
In case of complicated `include` layouts, a custom `module.modulemap` can be provided inside `include`. SwiftPM will error out if it can not generate a modulemap w.r.t the above rules.
374+
375+
For executable modules, only one valid C language main file is allowed i.e. it is invalid to have `main.c` and `main.cpp` in the same module.

0 commit comments

Comments
 (0)