Swift & CMake "hello world".

Code:
pkg install swift510 cmake ninja

Swift source file ; cat helloworld.swift
Code:
import Foundation
//----------------------------------------------------------------------
print("Hello, FreeBSD!")
let fruits: Array<String> = ["Apple", "Banana", "Cherry"]
typealias Vector<T> = Array<T>
let numbers: Vector<Int> = [10, 20, 30, 40, 50]
print("\nIterating through the Array:")
for fruit in fruits {
    print(" - \(fruit)")
}
print("\nAccessing the Vector (Array alias):")
print(" The second number is: \(numbers[1])")
print(" Vector count: \(numbers.count)")
//----------------------------------------------------------------------
class Calculator {
    private var NUM: Int
    init(startingValue: Int) {
        self.NUM = startingValue
        print("Calculator initialized with: \(self.NUM)")
    }
    func add(inputNumber: Int) -> Int {
        return inputNumber + self.NUM
    }
    deinit {
        print("Calculator is being deallocated from memory")
    }
}
var myCalc: Calculator? = Calculator(startingValue: 10)
if let result = myCalc?.add(inputNumber: 5) {
    print("Result of 5 + 10 is: \(result)")
}
print("Setting myCalc to nil...")
myCalc = nil
//----------------------------------------------------------------------
print("Main Thread: Starting...")
let backgroundTask = Task {
    print("Background Task: Hello World from a new thread!")
    try? await Task.sleep(nanoseconds: 1_000_000_000) // Sleep 1 second
    print("Background Task: Finished work.")
    return "Task Success"
}
print("Main Thread: Waiting for the background task to finish...")
let result = await backgroundTask.value
print("Main Thread: Received result: '\(result)'.")
print("Main Thread: Exiting.")
Cmake config file , cat CMakeLists.txt
Code:
cmake_minimum_required(VERSION 3.26)
project(HelloWorldSwift LANGUAGES Swift)
set(CMAKE_Swift_SDK "")
add_executable(HelloWorld helloworld.swift)
set_target_properties(HelloWorld PROPERTIES
    Swift_LANGUAGE_VERSION 5
)

A script to configure, build ,execute ; cat doit
Code:
export PATH="/usr/local/swift510/bin:$PATH"
cmake --build build --target clean
mkdir build
cd build
cmake -G Ninja \
      -DCMAKE_Swift_COMPILER=/usr/local/swift510/bin/swiftc \
      -DCMAKE_Swift_FLAGS="-sdk /" \
      ..
ninja 2>&1 | grep -v "SDKSettings.json"
./HelloWorld

Code:
./doit
 
Back
Top