Skip to content

Commit cbfe8d3

Browse files
committed
Initial Commit: Adding initial projects
- The first project is a single-file "Hello World" program written in Swift. - The second project is a library and executable, both are written in Swift. - The third project demonstrates Swift/C++ interop, where a library containing mixes C++ and Swift sources is linked into a C++ executable and a Swift executable.
0 parents  commit cbfe8d3

File tree

26 files changed

+801
-0
lines changed

26 files changed

+801
-0
lines changed

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

1_single_executable/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2023 Apple Inc. and the Swift project authors.
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
8+
cmake_minimum_required(VERSION 3.22)
9+
10+
project(hello LANGUAGES Swift)
11+
12+
add_executable(hello hello.swift)

1_single_executable/Readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Hello World
2+
3+
This is the simplest usage of Swift and CMake, building a single swift
4+
executable.
5+
6+
## Build Instructions
7+
8+
### macOS and Linux:
9+
10+
```sh
11+
$ mkdir build && cd build
12+
$ cmake -G 'Ninja' ../
13+
$ ninja hello
14+
$ ./hello
15+
Hello, world!
16+
```
17+
18+
## Build Results
19+
20+
Building this project results in a single executable, `hello` in the build
21+
directory.

1_single_executable/hello.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
print("Hello, world!")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2023 Apple Inc. and the Swift project authors.
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
8+
cmake_minimum_required(VERSION 3.22)
9+
project(Project2 LANGUAGES Swift)
10+
11+
add_subdirectory("lib/")
12+
add_subdirectory("src/")

2_executable_library/Readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Factorials
2+
3+
This program demonstrates using CMake to build a Swift executable that uses a
4+
Swift library.
5+
6+
## Build Instructions
7+
8+
### macOS and Linux:
9+
10+
```sh
11+
$ mkdir build && cd build
12+
$ cmake -G 'Ninja' ../
13+
$ ninja factorials
14+
$ ./src/factorials
15+
16+
factorial(0) = 1
17+
factorial(1) = 1
18+
factorial(2) = 2
19+
factorial(3) = 6
20+
factorial(4) = 24
21+
factorial(5) = 120
22+
factorial(6) = 720
23+
factorial(7) = 5040
24+
factorial(8) = 40320
25+
factorial(9) = 362880
26+
factorial(10) = 3628800
27+
```
28+
29+
## Build Results
30+
31+
This project results in two primary build products, the `factorial` library, and
32+
the `factorials` executable The library and the corresponding swiftmodule live
33+
under the `lib/` directory, while the executable lives under the `src/`
34+
directory.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2023 Apple Inc. and the Swift project authors.
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
8+
add_library(factorial factorial.swift)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
public func factorial(x: Int) -> Int {
13+
if x <= 0 {
14+
return 1
15+
}
16+
return x * factorial(x: x - 1)
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2023 Apple Inc. and the Swift project authors.
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
8+
add_executable(factorials main.swift)
9+
target_link_libraries(factorials PRIVATE factorial)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import factorial
13+
14+
for x in 0...10 {
15+
print("factorial(\(x)) = \(factorial(x: x))")
16+
}

0 commit comments

Comments
 (0)