Skip to content

Experiment of Interoperability between C++ & Swift

License

Notifications You must be signed in to change notification settings

luncliff/cpp-swift-experiment

Repository files navigation

CMake Swift

References

How To

Setup

pwsh setup-metal-cpp.ps1

Build

With Swift Package Manager(SwiftPM)

swift build --build-tests
swift build --configuration debug --target Baguette

With Xcodebuild

xcodebuild -showsdks
xcodebuild -list -workspace .
xcodebuild -workspace . -scheme "Baguette-Package" -showdestinations
xcodebuild -workspace . -scheme "Baguette-Package" \
    -destination "generic/platform=macOS" \
    -derivedDataPath DerivedData \
    -configuration Debug \
    build

With CMake, Vcpkg Toolchain

export VCPKG_FEATURE_FLAGS="manifests"
export VCPKG_ROOT="$HOME/../vcpkg"
cmake --preset x64-osx
cmake --build --preset x64-osx-debug

Test

With SwiftPM

swift test --list-tests
swift test --enable-code-coverage

With Xcodebuild

xcodebuild -workspace . -scheme "Baguette-Package" \
    -destination "platform=macOS" \
    -derivedDataPath DerivedData \
    -enableCodeCoverage YES \
    test

With CMake, Vcpkg Toolchain

cmake --build --preset x64-osx-debug --target baguette_test
ctest --preset x64-osx-debug

Analysis

Coverage With llvm-cov & lcov

If you used Swift Package Manager to build/test, the path can be used with configuration name.

llvm-cov export --format lcov -instr-profile .build/debug/codecov/default.profdata .build/debug/BaguettePackageTests.xctest/Contents/MacOS/BaguettePackageTests > lcov.info

Xcodebuild will generate more complicated output path.

profdata_path=$(find "DerivedData/Build/ProfileData" -name "Coverage.profdata" | head -n 1)
xctest_path="DerivedData/Build/Products/Debug/BaguetteBridgeTests.xctest/Contents/MacOS/BaguetteBridgeTests"
llvm-cov export -format lcov -instr-profile "$profdata_path" "$xctest_path" > lcov.info
$profdata_path = Get-ChildItem -File -Path "DerivedData/Build/ProfileData" -Recurse -Filter "Coverage.profdata" 
$xctest_path = "./DerivedData/Build/Products/Debug/BaguetteBridgeTests.xctest/Contents/MacOS/BaguetteBridgeTests"
llvm-cov export -format lcov -instr-profile "$profdata_path" "$xctest_path" > lcov.info

From the generated lcov.info, you can remove unnecessary files.

lcov --remove lcov.info "test/" \
     --output-file lcov-changed.info
lcov --remove lcov-changed.info "externals/" \
     --output-file lcov-changed.info --ignore-errors unused
lcov --list lcov-changed.info

From the file, you can generate HTML report.

genhtml lcov.info --output-directory docs
open docs/index.html

Use xcrun to generate coverage report.

xcrun --run llvm-cov show -instr-profile=$profdata_path $xctest_path > docs/coverage.report

Coverage With xccov

xcresult_path=$(find DerivedData/Logs/Test/ -maxdepth 1 -name "Test*.xcresult" | head -n 1)
$xcresult_path = Get-ChildItem -Directory  -Filter "DerivedData/Logs/Test/Test*.xcresult" | Select-Object -First 1
xcrun xccov view --report "$xcresult_path"
xcrun xccov view --report --json $xcresult_path | jq
New-Item -Type Directory -Name "scripts" -Force
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/SonarSource/sonar-scanning-examples/master/swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh" -OutFile "scripts/xccov-to-sonarqube-generic.sh"
bash ./scripts/xccov-to-sonarqube-generic.sh $xcresult_path > docs/coverage.xml

Reporting

(TBA)

Lint

Check https://github.com/realm/SwiftLint

swiftlint lint --autocorrect
swiftlint lint --output docs/lint.md --reporter markdown

Document

xcodebuild docbuild -workspace . -derivedDataPath DerivedData -scheme Baguette-Package -destination "platform=macOS"
find "$(pwd)/.build" -type d -name "*.doccarchive"