Skip to content

Commit 98c5440

Browse files
authored
Initial commit
0 parents  commit 98c5440

21 files changed

Lines changed: 531 additions & 0 deletions

.github/workflows/build-linux.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Template: https://github.com/actions/starter-workflows/blob/main/ci/cmake-multi-platform.yml
2+
name: Build On Linux
3+
4+
on:
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- master
9+
tags:
10+
- "v*"
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Install Conan
20+
run: |
21+
22+
pip install conan==2.6.0
23+
24+
conan profile detect
25+
26+
- name: Configure CMake
27+
working-directory: ${{github.workspace}}
28+
run: |
29+
30+
conan install . --build=missing
31+
32+
cmake --preset cmake-project-release
33+
34+
- name: Build Targets
35+
working-directory: ${{github.workspace}}
36+
run: cmake --build --preset cmake-project-release
37+
38+
- name: Install Targets
39+
working-directory: ${{github.workspace}}
40+
run: |
41+
42+
cmake --build --preset cmake-project-release -t install
43+
44+
- name: Upload artifacts
45+
uses: actions/upload-artifact@v4
46+
with:
47+
name: install
48+
path: build/Release/install
49+
include-hidden-files: true
50+
if-no-files-found: error

.github/workflows/build-win32.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Build On Win32
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
tags:
9+
- "v*"
10+
11+
jobs:
12+
build:
13+
runs-on: windows-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Install Conan
19+
run: |
20+
21+
pip install conan==2.6.0
22+
23+
conan profile detect
24+
25+
- name: Configure CMake
26+
working-directory: ${{github.workspace}}
27+
run: |
28+
29+
conan install . --build=missing
30+
31+
cmake --preset cmake-project-default
32+
33+
- name: Build Targets
34+
working-directory: ${{github.workspace}}
35+
run: cmake --build --preset cmake-project-release
36+
37+
- name: Install Targets
38+
working-directory: ${{github.workspace}}
39+
run: |
40+
41+
cmake --build --preset cmake-project-release -t install
42+
43+
- name: Upload artifacts
44+
uses: actions/upload-artifact@v4
45+
with:
46+
name: install
47+
path: build/install
48+
include-hidden-files: true
49+
if-no-files-found: error
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Unit Test On Linux
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- "**"
8+
tags:
9+
- "v*"
10+
11+
jobs:
12+
unit-test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Install Conan
19+
run: |
20+
21+
pip install conan==2.6.0
22+
23+
conan profile detect
24+
25+
- name: Configure CMake
26+
working-directory: ${{github.workspace}}
27+
run: |
28+
29+
conan install . --build=missing
30+
31+
cmake --preset cmake-project-release -DWITH_GTEST=ON
32+
33+
- name: Build Targets
34+
working-directory: ${{github.workspace}}
35+
run: cmake --build --preset cmake-project-release
36+
37+
- name: Run Unit Test
38+
working-directory: ${{github.workspace}}
39+
run: |
40+
41+
cmake --build --preset cmake-project-release -t tests
42+
43+
./build/Release/test/gtest-testrun
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Unit Test On Windows
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- "**"
8+
tags:
9+
- "v*"
10+
11+
jobs:
12+
unit-test:
13+
runs-on: windows-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Install Conan
19+
run: |
20+
21+
pip install conan==2.6.0
22+
23+
conan profile detect
24+
25+
- name: Configure CMake
26+
working-directory: ${{github.workspace}}
27+
run: |
28+
29+
conan install . --build=missing
30+
31+
cmake --preset cmake-project-default -DWITH_GTEST=ON
32+
33+
- name: Build Targets
34+
working-directory: ${{github.workspace}}
35+
run: cmake --build --preset cmake-project-release
36+
37+
- name: Run Unit Test
38+
working-directory: ${{github.workspace}}
39+
run: |
40+
41+
cmake --build --preset cmake-project-release -t tests
42+
43+
.\build\test\Release\gtest-testrun.exe

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode/
2+
build/
3+
CMakeUserPresets.json

CMakeLists.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
cmake_minimum_required (VERSION 3.23)
2+
3+
set (CMAKE_CXX_STANDARD 11)
4+
set (CMAKE_CXX_STANDARD_REQUIRED true)
5+
6+
7+
if (NOT DEFINED PACKAGE_VERSION)
8+
message ("-- [CMake Project] WARNING: Package version is not defined, set as 0.0.0")
9+
set (PACKAGE_VERSION "0.0.0")
10+
endif ()
11+
12+
message ("-- [CMake Project] Package version: ${PACKAGE_VERSION}")
13+
14+
project ("CMake Project"
15+
VERSION ${PACKAGE_VERSION}
16+
DESCRIPTION "C/C++ 项目的 CMake 模板。"
17+
)
18+
19+
# write version file
20+
file (WRITE "${PROJECT_BINARY_DIR}/.version" "${PACKAGE_VERSION}")
21+
22+
23+
# configure unit test
24+
option (WITH_GTEST "Enable unit tests by GoogleTest" OFF)
25+
26+
if (NOT with_gtest)
27+
unset (WITH_GTEST CACHE)
28+
29+
endif ()
30+
31+
if (WITH_GTEST)
32+
message ("-- [CMake Project] Unit test by GoogleTest is enabled")
33+
34+
else ()
35+
message ("-- [CMake Project] Unit test by GoogleTest is disabled")
36+
37+
endif ()
38+
39+
if (WITH_GTEST)
40+
find_package (GTest REQUIRED)
41+
42+
endif ()
43+
44+
45+
# configue included headers
46+
set (DIR_SRC_ROOT "${PROJECT_SOURCE_DIR}/src")
47+
include_directories (${DIR_SRC_ROOT})
48+
49+
# configure compiler cross platform
50+
if (CMAKE_HOST_WIN32)
51+
include (config-win32.cmake)
52+
53+
elseif (CMAKE_HOST_UNIX)
54+
include (config-unix.cmake)
55+
56+
else ()
57+
message (FATAL_ERROR "Unknown platform")
58+
59+
endif ()
60+
61+
62+
# configure output dir of static and shared library
63+
include (GNUInstallDirs)
64+
65+
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
66+
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
67+
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
68+
69+
# configure targets
70+
aux_source_directory (${DIR_SRC_ROOT} PROJECT_SOURCE_FILES)
71+
add_library (cmake-project ${PROJECT_SOURCE_FILES})
72+
73+
unset (CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
74+
unset (CMAKE_LIBRARY_OUTPUT_DIRECTORY)
75+
unset (CMAKE_RUNTIME_OUTPUT_DIRECTORY)
76+
77+
78+
add_subdirectory ("snippet")
79+
80+
81+
if (WITH_GTEST)
82+
add_subdirectory (test)
83+
84+
endif ()
85+
86+
87+
# configure install targets
88+
option (INSTALL_IN_PLACE "Set CMAKE_INSTALL_PREFIX inside PROJECT_BINARY_DIR" ON)
89+
90+
if (INSTALL_IN_PLACE)
91+
set (CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/install")
92+
93+
endif ()
94+
95+
message ("-- [CMake Project] Install destination would be: ${CMAKE_INSTALL_PREFIX}")
96+
97+
98+
include (public-headers.cmake)
99+
set_target_properties (cmake-project PROPERTIES PUBLIC_HEADER ${CMAKE_PROJECT_PUBLIC_HEADERS})
100+
101+
install (TARGETS cmake-project
102+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
103+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
104+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
105+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
106+
)
107+
108+
install (FILES ${PROJECT_BINARY_DIR}/.version DESTINATION .)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# cmake-project-template
2+
3+
C/C++ 项目的 CMake 模板。
4+

conanfile.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from conan import ConanFile
2+
from conan.tools.cmake import cmake_layout, CMakeToolchain
3+
4+
5+
class ExampleRecipe(ConanFile):
6+
name = "cmake-project"
7+
version = "1.1.1"
8+
description = "C/C++ 项目的 CMake 模板。"
9+
languages = "C++"
10+
author = "DavidingPlus"
11+
homepage = "https://github.com/DavidingPlus/cmake-project-template"
12+
13+
settings = "os", "compiler", "build_type", "arch"
14+
generators = "CMakeDeps"
15+
16+
options = {
17+
"with_gtest": [True, False]
18+
}
19+
default_options = {
20+
"with_gtest": True
21+
}
22+
23+
def requirements(self):
24+
if self.options.with_gtest:
25+
self.requires("gtest/1.12.1")
26+
27+
def layout(self):
28+
cmake_layout(self)
29+
30+
def generate(self):
31+
tc = CMakeToolchain(self)
32+
tc.presets_prefix = "cmake-project"
33+
tc.cache_variables["PACKAGE_VERSION"] = self.version
34+
tc.cache_variables["with_gtest"] = self.options.with_gtest
35+
tc.generate()

config-unix.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
message ("-- [CMake Project] Building on Linux ...")
2+
3+
# configure compiler gcc with CCache
4+
set (CMAKE_C_COMPILER "gcc")
5+
set (CMAKE_CXX_COMPILER "g++")
6+
7+
message ("-- [CMake Project] Using C Compiler: ${CMAKE_C_COMPILER} (${CMAKE_C_COMPILER_VERSION})")
8+
message ("-- [CMake Project] Using C++ Compiler: ${CMAKE_CXX_COMPILER} (${CMAKE_CXX_COMPILER_VERSION})")
9+
10+
11+
option (BUILD_SHARED_LIBS "Build using shared libraries" ON)

config-win32.cmake

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
message ("-- [CMake Project] Building on Win32 ...")
2+
3+
message ("-- [CMake Project] Using C Compiler: ${CMAKE_C_COMPILER} (${CMAKE_C_COMPILER_VERSION})")
4+
message ("-- [CMake Project] Using C++ Compiler: ${CMAKE_CXX_COMPILER} (${CMAKE_CXX_COMPILER_VERSION})")
5+
6+
# using warning options W2
7+
option (SUPRESS_W3_WARNINGS "Configure warning level to /W2" ON)
8+
if (SUPRESS_W3_WARNINGS)
9+
add_compile_options ("/W2")
10+
endif ()
11+
12+
# configure compiler MSVC with Ccache
13+
# This is for MSVC to satisfy Ccache, not other compilers.
14+
add_compile_options ("/Z7")
15+
16+
find_program (CCACHE_EXECUTABLE ccache)
17+
if (CCACHE_EXECUTABLE)
18+
message ("-- [CMake Project] Ccache executable found at: ${CCACHE_EXECUTABLE}")
19+
file (COPY "${CCACHE_EXECUTABLE}" DESTINATION "${PROJECT_BINARY_DIR}")
20+
file (RENAME "${PROJECT_BINARY_DIR}/ccache.exe" "${PROJECT_BINARY_DIR}/cl.exe")
21+
set (CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug,RelWithDebInfo>:Embedded>")
22+
set (CMAKE_VS_GLOBALS
23+
"CLToolExe=cl.exe"
24+
"CLToolPath=${PROJECT_BINARY_DIR}"
25+
"TrackFileAccess=false"
26+
"UseMultiToolTask=true"
27+
"DebugInformationFormat=OldStyle"
28+
)
29+
30+
else ()
31+
message ("-- [CMake Project] CCache not found.")
32+
33+
endif ()
34+
35+
36+
option (BUILD_SHARED_LIBS "Build using shared libraries" OFF)

0 commit comments

Comments
 (0)