Skip to content

Commit

Permalink
Update cmkr and showcase latest features
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexodia committed Dec 6, 2024
1 parent aeadb62 commit 43846a8
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 131 deletions.
48 changes: 35 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
name: CMake

on: [push, pull_request]
on:
push:
pull_request:
schedule:
# Build every 6 days to avoid costly cache rebuilds
- cron: 0 0 */6 * * # https://crontab.guru/#0_0_*/6_*_*

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
fail-fast: false
matrix:
os: [windows-2019, macos-10.15, ubuntu-20.04]
os: [windows-latest, macos-latest, ubuntu-latest]
env:
BUILD_TYPE: Release
BUILD_TYPE: "Release"
CMAKE_GENERATOR: "Ninja"
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"

steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Compile with gcc 10 on ubuntu
if: ${{ matrix.os == 'ubuntu-20.04' }}
run: |
echo "CC=gcc-10" >> $GITHUB_ENV
echo "CXX=g++-10" >> $GITHUB_ENV
- name: Build
- name: Checkout
uses: actions/checkout@v4

# Reference: https://learn.microsoft.com/en-us/vcpkg/consume/binary-caching-github-actions-cache
- name: Export GitHub Actions cache environment variables
uses: actions/github-script@v7
with:
script: |
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
- name: Install Ninja
uses: seanmiddleditch/gha-setup-ninja@96bed6edff20d1dd61ecff9b75cc519d516e6401 # v5

- name: Visual Studio Development Environment
uses: ilammy/msvc-dev-cmd@cec98b9d092141f74527d0afa6feb2af698cfe89 # v1.12.1

- name: CMake Build
run: |
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}
cmake -B build "-DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}"
cmake --build build --config ${{ env.BUILD_TYPE }} --parallel
- name: Run Tests
run: |
build/example
26 changes: 0 additions & 26 deletions .gitpod.yml

This file was deleted.

78 changes: 46 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,86 @@

cmake_minimum_required(VERSION 3.15)

# Regenerate CMakeLists.txt automatically in the root project
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "In-tree builds are not supported. Run CMake from a separate directory: cmake -B build")
endif()

set(CMKR_ROOT_PROJECT OFF)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(CMKR_ROOT_PROJECT ON)

# Bootstrap cmkr
include("cmake/cmkr.cmake" OPTIONAL RESULT_VARIABLE CMKR_INCLUDE_RESULT)
# Bootstrap cmkr and automatically regenerate CMakeLists.txt
include(cmkr.cmake OPTIONAL RESULT_VARIABLE CMKR_INCLUDE_RESULT)
if(CMKR_INCLUDE_RESULT)
cmkr()
endif()

# Enable folder support
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
endif()

# Create a configure-time dependency on cmake.toml to improve IDE support
if(CMKR_ROOT_PROJECT)
configure_file(cmake.toml cmake.toml COPYONLY)
# Create a configure-time dependency on cmake.toml to improve IDE support
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS cmake.toml)
endif()

# vcpkg settings
set(VCPKG_OVERLAY_PORTS
"${CMAKE_CURRENT_SOURCE_DIR}/vcpkg-overlay"
)
set(VCPKG_OVERLAY_TRIPLETS
"${CMAKE_CURRENT_SOURCE_DIR}/vcpkg-overlay"
)
project(vcpkg_template)

if(CMKR_ROOT_PROJECT AND NOT CMKR_DISABLE_VCPKG)
include(FetchContent)
message(STATUS "Fetching vcpkg...")
FetchContent_Declare(vcpkg URL "https://github.com/microsoft/vcpkg/archive/refs/tags/2021.05.12.tar.gz")
# Fix warnings about DOWNLOAD_EXTRACT_TIMESTAMP
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()
message(STATUS "Fetching vcpkg (2024.11.16)...")
FetchContent_Declare(vcpkg
URL
"https://github.com/microsoft/vcpkg/archive/refs/tags/2024.11.16.tar.gz"
SUBBUILD_DIR
"CMakeFiles/vcpkg-subbuild"
SOURCE_DIR
vcpkg
BINARY_DIR
"CMakeFiles/vcpkg-build"
)
FetchContent_MakeAvailable(vcpkg)

if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin AND CMAKE_OSX_ARCHITECTURES STREQUAL "")
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_HOST_SYSTEM_PROCESSOR} CACHE STRING "" FORCE)
endif()
include("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake")
endif()

# Packages
find_package(fmt REQUIRED CONFIG)

find_package(unofficial-sqlite3 REQUIRED CONFIG)
find_package(fmt REQUIRED)

# Target example
set(CMKR_TARGET example)
set(example_SOURCES "")
find_package(unofficial-sqlite3 REQUIRED)

list(APPEND example_SOURCES
"src/main.cpp"
)
find_package(unofficial-mylib REQUIRED)

list(APPEND example_SOURCES
# Target: example
set(example_SOURCES
cmake.toml
"src/main.cpp"
)

set(CMKR_SOURCES ${example_SOURCES})
add_executable(example)

if(example_SOURCES)
target_sources(example PRIVATE ${example_SOURCES})
endif()

get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT)
if(NOT CMKR_VS_STARTUP_PROJECT)
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example)
endif()

target_sources(example PRIVATE ${example_SOURCES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example_SOURCES})

target_link_libraries(example PRIVATE
fmt::fmt
unofficial::sqlite3::sqlite3
unofficial::mylib::mylib
)

unset(CMKR_TARGET)
unset(CMKR_SOURCES)

get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT)
if(NOT CMKR_VS_STARTUP_PROJECT)
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example)
endif()
59 changes: 39 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,64 @@
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/from-referrer/)

# vcpkg_template

This is a template showcasing [cmkr](https://github.com/build-cpp/cmkr) together with [vcpkg](https://github.com/microsoft/vcpkg) for frictionless cross platform dependency management with CMake.

## Building (IDE)

Clone this repository and open it in your favorite IDE with CMake support (Visual Studio, CLion, Qt Creator). Everything should work out of the box.
## Building

## Building (command line)
Use the following commands to build the project:

```
cmake -Bbuild
cmake -B build
cmake --build build
```

Then open the `.sln` (Windows) or run `make` (Unix) from the `build` directory.

## cmake.toml

Under the hood cmkr generates the `CMakeLists.txt` required to build this project from the `cmake.toml` file:

```toml
[cmake]
version = "3.15"
cmkr-include = "cmake/cmkr.cmake"

[project]
name = "vcpkg_template"

# See https://vcpkg.io/en/packages.html for available packages
# See https://vcpkg.link for available packages
# Chose a version from https://github.com/microsoft/vcpkg/releases
[vcpkg]
version = "2021.05.12"
packages = ["fmt", "sqlite3"]
version = "2024.11.16"
packages = [
"fmt",
"sqlite3",
"mylib",
]
overlay = "vcpkg-overlay"

[find-package]
fmt = { version = "*" }
unofficial-sqlite3 = { version = "*" }
# Make the packages available to CMake
[find-package.fmt]
[find-package.unofficial-sqlite3]
[find-package.unofficial-mylib]

[target.example]
type = "executable"
sources = ["src/main.cpp"]
link-libraries = ["fmt::fmt", "unofficial::sqlite3::sqlite3"]
link-libraries = [
"fmt::fmt",
"unofficial::sqlite3::sqlite3",
"unofficial::mylib::mylib",
]
```

## Vcpkg overlay

The `[vcpkg].overlay` key points to a local folder used as an overlay for vcpkg ports and triplets:

```sh
vcpkg-overlay
├── mylib # custom port
│ ├── CMakeLists.txt
│ ├── portfile.cmake
│ ├── usage
│ └── vcpkg.json
└── x64-windows.cmake # custom triplet
```

The `vcpkg-overlay/mylib` [overlay port](https://learn.microsoft.com/en-us/vcpkg/concepts/overlay-ports) is used to make the example [mylib](https://gitlab.com/mrexodia/mylib) available without having to fork the vcpkg repository or create a custom registry.

The `vcpkg-overlay/x64-windows.cmake` [overlay triplet](https://learn.microsoft.com/en-us/vcpkg/users/examples/overlay-triplets-linux-dynamic) is used to always build static libraries for Windows (instead of shared libraries, which is normally the default).
28 changes: 17 additions & 11 deletions cmake.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
[cmake]
version = "3.15"
cmkr-include = "cmake/cmkr.cmake"

[project]
name = "vcpkg_template"

# See https://vcpkg.io/en/packages.html for available packages
# See https://vcpkg.link for available packages
# Chose a version from https://github.com/microsoft/vcpkg/releases
[vcpkg]
version = "2021.05.12"
packages = ["fmt", "sqlite3"]
version = "2024.11.16"
packages = [
"fmt",
"sqlite3",
"mylib",
]
overlay = "vcpkg-overlay"

[find-package]
fmt = { version = "*" }
unofficial-sqlite3 = { version = "*" }
# Make the packages available to CMake
[find-package.fmt]
[find-package.unofficial-sqlite3]
[find-package.unofficial-mylib]

[target.example]
type = "executable"
sources = ["src/main.cpp"]
link-libraries = ["fmt::fmt", "unofficial::sqlite3::sqlite3"]
link-libraries = [
"fmt::fmt",
"unofficial::sqlite3::sqlite3",
"unofficial::mylib::mylib",
]
Loading

0 comments on commit 43846a8

Please sign in to comment.