Skip to content

Commit

Permalink
Add CMake support and CI (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrahams authored Mar 29, 2024
1 parent 3d802c0 commit 16e6096
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ignore:
- Tests
- Package.swift

comment:
require_changes: true

coverage:
status:

project:
default:
threshold: 0%

patch:
default:
# basic
target: auto
threshold: 0%
base: auto
121 changes: 121 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
name: Build and test

on:
push:
branches: [ main ]
paths-ignore:
- "**.md"
- "LICENSE"
- ".gitignore"
- ".editorconfig"
pull_request:
branches: [ "**" ]
paths-ignore:
- "**.md"
- "LICENSE"
- ".gitignore"
- ".editorconfig"

env:
swift-version: '5.9'

jobs:
build-and-test:
name: "${{ matrix.os }}/${{ matrix.spm_configuration }}"
strategy:
fail-fast: false
matrix:
# macos-latest is apparently not the latest.
os: [macos-13, ubuntu-latest, windows-latest]
spm_configuration: [debug, release]

include:
- swift_test_options:

- spm_configuration: debug
cmake_build_type: Debug

- spm_configuration: release
cmake_build_type: Release

- os: ubuntu-latest
spm_configuration: debug

swift_test_options: --enable-code-coverage

runs-on: ${{ matrix.os }}

steps:
- uses: maxim-lobanov/setup-xcode@v1
if: ${{ matrix.os == 'macos-13' }}
with:
xcode-version: latest-stable

- name: 'Set up swift'
uses: SwiftyLab/setup-swift@latest
with:
swift-version: ${{ env.swift-version }}

- name: Verify swift version
run: swift --version && swift --version | grep -q ${{ env.swift-version }}
shell: bash

# Setup the build machine with the most recent versions of CMake and Ninja. Both are cached if
# not already: on subsequent runs both will be quickly restored from GitHub cache service.
- uses: lukka/get-cmake@latest

- name: Checkout (GitHub)
uses: actions/checkout@v4
with:
submodules: true
show-progress: false

- name: Configure (CMake)
# We explicitly point to swiftc in the PATH because otherwise CMake picks up the one in XCode.
run: >-
cmake -GNinja -S . -B .cmake-build
-DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }}
-DBUILD_TESTING=YES
${{ matrix.os == 'macos-13' && '-D CMAKE_Swift_COMPILER=swiftc' || '' }}
- name: Build (CMake)
run: cmake --build .cmake-build

- name: Test (CMake)
run: ctest -V --test-dir .cmake-build

- name: CMake => Xcode
if: ${{ matrix.os == 'macos-13' }}
# We explicitly point to swiftc in the PATH because otherwise CMake picks up the one in XCode.
run: >-
cmake -GXcode -S . -B .xcode-build
-DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }}
-DBUILD_TESTING=YES
${{ matrix.os == 'macos-13' && '-D CMAKE_Swift_COMPILER=swiftc' || '' }}
cd .xcode-build
xcrun xcodebuild -configuration ${{ matrix.spm_configuration }} -scheme Durian -destination 'platform=macOS' test
- name: Build and Test via SPM
run: swift test -v ${{ matrix.swift_test_options }} -c ${{ matrix.spm_configuration }}

- name: Export Coverage
if: ${{ contains(matrix.swift_test_options, '--enable-code-coverage') }}
run: |
shopt -s nullglob
dot_os=(.build/${{ matrix.spm_configuration }}/*.build/*.o .build/${{ matrix.spm_configuration }}/*.build/**/*.o)
bin_params=("${dot_os[0]}")
for o in "${dot_os[@]:1}"; do
bin_params+=("-object" "${o}")
done
# Note: on mac this command might require a leading xcrun.
llvm-cov export -format="lcov" -instr-profile "$(swift test -c ${{ matrix.spm_configuration }} --show-codecov-path | xargs dirname)"/default.profdata "${bin_params[@]}" > info.lcov
- name: Upload coverage reports to Codecov
if: ${{ contains(matrix.swift_test_options, '--enable-code-coverage') }}
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Standard project boilerplate
#
cmake_minimum_required(VERSION 3.26)
project(Swifty-LLVM
VERSION 0.1.0
DESCRIPTION "A tool that finds XCTestCases and their testXXX methods and generates a main.swift to run them."
HOMEPAGE_URL "https://github.com/hylo-lang/GenerateSwiftXCTestMain"
LANGUAGES C CXX Swift
)
enable_testing()
list(PREPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
if (PROJECT_IS_TOP_LEVEL)
include(cmake/TopLevelDefaults.cmake)
endif()

add_subdirectory(Sources)

if(BUILD_TESTING)
add_subdirectory(Tests)
endif()
13 changes: 13 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# The Swift module our clients depend on.
#
file(GLOB_RECURSE source_files FOLLOW_SYMLINKS LIST_DIRECTORIES false CONFIGURE_DEPENDS Durian/*.swift)
add_library(Durian ${source_files})

# This is required in order to be a testee.
set_target_properties(Durian
PROPERTIES FRAMEWORK TRUE
)

target_compile_options(Durian
PRIVATE $<$<BOOL:${BUILD_TESTING}>:-enable-testing>)
12 changes: 12 additions & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(NOT APPLE)
find_package(GenerateSwiftXCTestMain)
endif()

find_package(SwiftXCTest)

file(GLOB_RECURSE test_files
FOLLOW_SYMLINKS
LIST_DIRECTORIES false
CONFIGURE_DEPENDS DurianTests/*.swift)

add_swift_xctest(DurianTests Durian ${test_files})
24 changes: 24 additions & 0 deletions cmake/TopLevelDefaults.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Commands used to create an easy development environment when
# building this project directly (not as a dependency).

# Without this, generated Xcode projects aren't debuggable.
set(CMAKE_XCODE_GENERATE_SCHEME YES)

#
# Hylo-standard dependency resolution.
#
include(FetchContent)

block()

set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
FetchContent_Declare(Hylo-CMakeModules
GIT_REPOSITORY https://github.com/hylo-lang/CMakeModules.git
GIT_TAG main
OVERRIDE_FIND_PACKAGE
)

endblock()
FetchContent_MakeAvailable(Hylo-CMakeModules)

list(PREPEND CMAKE_MODULE_PATH ${hylo-cmakemodules_SOURCE_DIR})

0 comments on commit 16e6096

Please sign in to comment.