Skip to content

Commit 4f5300d

Browse files
author
Anton Pantyukhin
committed
Add common files
1 parent 092e480 commit 4f5300d

14 files changed

+429
-0
lines changed

.clang-format

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
Language: Cpp
3+
Standard: c++17
4+
UseTab: Never
5+
TabWidth: 4
6+
IndentWidth: 4
7+
ColumnLimit: 120
8+
9+
BreakBeforeBraces: Custom
10+
BraceWrapping:
11+
AfterCaseLabel: false
12+
AfterClass: false
13+
AfterControlStatement: Never
14+
AfterEnum: false
15+
AfterFunction: true
16+
AfterNamespace: false
17+
AfterStruct: false
18+
AfterUnion: false
19+
AfterExternBlock: false
20+
BeforeCatch: false
21+
BeforeElse: false
22+
BeforeLambdaBody: false
23+
BeforeWhile: false
24+
SplitEmptyFunction: false
25+
SplitEmptyRecord: false
26+
SplitEmptyNamespace: false
27+
BreakBeforeBinaryOperators: None
28+
BreakConstructorInitializers: AfterColon
29+
PackConstructorInitializers: CurrentLine
30+
AlwaysBreakAfterReturnType: None
31+
AlwaysBreakBeforeMultilineStrings: false
32+
AlwaysBreakTemplateDeclarations: Yes
33+
34+
AlignAfterOpenBracket: true
35+
AlignEscapedNewlines: Left
36+
AlignTrailingComments: true
37+
AlignOperands: Align
38+
39+
IndentCaseBlocks: true
40+
IndentCaseLabels: false
41+
IndentExternBlock: Indent
42+
IndentPPDirectives: AfterHash
43+
IndentWrappedFunctionNames: true
44+
AccessModifierOffset: -4
45+
NamespaceIndentation: None
46+
47+
SpaceAfterCStyleCast: false
48+
SpaceAfterLogicalNot: false
49+
SpaceAfterTemplateKeyword: false
50+
SpaceBeforeAssignmentOperators: true
51+
SpaceBeforeCaseColon: false
52+
SpaceBeforeCpp11BracedList: false
53+
SpaceBeforeCtorInitializerColon: false
54+
SpaceBeforeInheritanceColon: false
55+
SpaceBeforeParens: ControlStatements
56+
SpaceBeforeRangeBasedForLoopColon: false
57+
SpaceBeforeSquareBrackets: false
58+
SpaceInEmptyBlock: true
59+
SpaceInEmptyParentheses: false
60+
SpacesInCStyleCastParentheses: false
61+
SpacesInConditionalStatement: false
62+
SpacesInContainerLiterals: false
63+
SpacesInParentheses: false
64+
SpacesInSquareBrackets: false
65+
66+
AllowAllArgumentsOnNextLine: true
67+
BinPackArguments: false
68+
AllowAllParametersOfDeclarationOnNextLine: false
69+
BinPackParameters: false
70+
AllowShortBlocksOnASingleLine: Empty
71+
AllowShortEnumsOnASingleLine: true
72+
AllowShortCaseLabelsOnASingleLine: true
73+
AllowShortFunctionsOnASingleLine: Inline
74+
AllowShortIfStatementsOnASingleLine: Never
75+
AllowShortLoopsOnASingleLine: false
76+
AllowShortLambdasOnASingleLine: All
77+
78+
CompactNamespaces: true
79+
Cpp11BracedListStyle: true
80+
EmptyLineBeforeAccessModifier: Always
81+
KeepEmptyLinesAtTheStartOfBlocks: true
82+
MaxEmptyLinesToKeep: 1
83+
PenaltyReturnTypeOnItsOwnLine: 1000
84+
PointerAlignment: Left
85+
SortUsingDeclarations: false

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Build folders
2+
build*/
3+
_build*/
4+
cmake-build*/
5+
6+
# Mac
7+
.DS_Store

CMakeLists.txt

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(mylib
3+
VERSION 1.0.0
4+
DESCRIPTION "Template for C++ library built with CMake"
5+
LANGUAGES CXX)
6+
7+
#----------------------------------------------------------------------------------------------------------------------
8+
# general settings and options
9+
#----------------------------------------------------------------------------------------------------------------------
10+
11+
include(cmake/utils.cmake)
12+
include(GNUInstallDirs)
13+
14+
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
15+
16+
# MYLIB_SHARED_LIBS option (undefined by default) can be used to force shared/static build
17+
option(MYLIB_BUILD_TESTS "Build mylib tests" OFF)
18+
option(MYLIB_BUILD_EXAMPLES "Build mylib examples" OFF)
19+
option(MYLIB_BUILD_DOCS "Build mylib documentation" OFF)
20+
option(MYLIB_INSTALL "Generate target for installing mylib" ${is_top_level})
21+
22+
if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
23+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
24+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
25+
endif()
26+
27+
if(DEFINED MYLIB_SHARED_LIBS)
28+
set(BUILD_SHARED_LIBS ${MYLIB_SHARED_LIBS})
29+
endif()
30+
31+
set_if_undefined(CMAKE_CXX_VISIBILITY_PRESET hidden)
32+
set_if_undefined(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
33+
34+
set_if_undefined(MYLIB_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/mylib-${PROJECT_VERSION}" CACHE
35+
STRING "Install path for mylib package-related CMake files")
36+
37+
add_library(mylib) # initialized below
38+
add_library(mylib::mylib ALIAS mylib)
39+
40+
# make 'find_package(mylib)' do nothing in subprojects because target is already defined
41+
list(APPEND defined_targets mylib)
42+
43+
#----------------------------------------------------------------------------------------------------------------------
44+
# mylib dependencies
45+
#----------------------------------------------------------------------------------------------------------------------
46+
47+
#find_package(Boost 1.77.0 REQUIRED COMPONENTS system)
48+
49+
#----------------------------------------------------------------------------------------------------------------------
50+
# mylib sources
51+
#----------------------------------------------------------------------------------------------------------------------
52+
53+
include(GenerateExportHeader)
54+
set(export_file_name "export_shared.h")
55+
56+
if(NOT BUILD_SHARED_LIBS)
57+
set(export_file_name "export_static.h")
58+
endif()
59+
60+
generate_export_header(mylib EXPORT_FILE_NAME include/mylib/${export_file_name})
61+
62+
set(public_headers
63+
include/mylib/export.h
64+
include/mylib/mylib.h)
65+
set(sources
66+
${public_headers}
67+
src/mylib.cpp)
68+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
69+
70+
list(APPEND public_headers "${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}")
71+
list(APPEND sources "${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}")
72+
73+
#----------------------------------------------------------------------------------------------------------------------
74+
# mylib target
75+
#----------------------------------------------------------------------------------------------------------------------
76+
77+
include(CMakePackageConfigHelpers)
78+
79+
target_sources(mylib PRIVATE ${sources})
80+
target_compile_definitions(mylib PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:MYLIB_STATIC_DEFINE>")
81+
82+
target_include_directories(mylib
83+
PUBLIC
84+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
85+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
86+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
87+
PRIVATE
88+
"${CMAKE_CURRENT_SOURCE_DIR}/src")
89+
90+
set_target_properties(mylib PROPERTIES
91+
PUBLIC_HEADER "${public_headers}"
92+
SOVERSION ${PROJECT_VERSION_MAJOR}
93+
VERSION ${PROJECT_VERSION})
94+
95+
if(MYLIB_INSTALL)
96+
configure_package_config_file(cmake/mylib-config.cmake.in mylib-config.cmake
97+
INSTALL_DESTINATION "${MYLIB_INSTALL_CMAKEDIR}")
98+
99+
write_basic_package_version_file(mylib-config-version.cmake
100+
COMPATIBILITY SameMajorVersion)
101+
102+
install(TARGETS mylib EXPORT mylib_export
103+
RUNTIME COMPONENT mylib
104+
LIBRARY COMPONENT mylib NAMELINK_COMPONENT mylib-dev
105+
ARCHIVE COMPONENT mylib-dev
106+
PUBLIC_HEADER COMPONENT mylib-dev DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mylib")
107+
108+
set(export_file "mylib-shared-export.cmake")
109+
110+
if(NOT BUILD_SHARED_LIBS)
111+
set(export_file "mylib-static-export.cmake")
112+
endif()
113+
114+
install(EXPORT mylib_export
115+
COMPONENT mylib-dev
116+
FILE "${export_file}"
117+
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}"
118+
NAMESPACE mylib::)
119+
120+
install(FILES
121+
"${CMAKE_CURRENT_BINARY_DIR}/mylib-config.cmake"
122+
"${CMAKE_CURRENT_BINARY_DIR}/mylib-config-version.cmake"
123+
COMPONENT mylib-dev
124+
DESTINATION "${MYLIB_INSTALL_CMAKEDIR}")
125+
126+
if(MSVC)
127+
set(pdb_file "")
128+
set(pdb_file_destination "")
129+
130+
if(BUILD_SHARED_LIBS)
131+
set(pdb_file "$<TARGET_PDB_FILE:mylib>")
132+
set(pdb_file_destination "${CMAKE_INSTALL_BINDIR}")
133+
else()
134+
# TARGET_PDB_FILE does not work for pdb file generated for static library build, determining it manually
135+
set(pdb_file "$<TARGET_FILE_DIR:mylib>/$<TARGET_FILE_PREFIX:mylib>$<TARGET_FILE_BASE_NAME:mylib>.pdb")
136+
set(pdb_file_destination "${CMAKE_INSTALL_LIBDIR}")
137+
endif()
138+
139+
install(FILES "${pdb_file}"
140+
COMPONENT mylib-dev
141+
CONFIGURATIONS Debug RelWithDebInfo
142+
DESTINATION "${pdb_file_destination}"
143+
OPTIONAL)
144+
endif()
145+
endif()
146+
147+
#----------------------------------------------------------------------------------------------------------------------
148+
# other targets
149+
#----------------------------------------------------------------------------------------------------------------------
150+
151+
# makes 'find_package' skip search for already defined targets in subdirectories
152+
macro(find_package)
153+
string(TOLOWER "${ARG0}" name)
154+
155+
if(NOT ${name} IN_LIST ${defined_targets})
156+
_find_package(${ARGV})
157+
endif()
158+
endmacro()
159+
160+
if(MYLIB_BUILD_TESTS)
161+
if(${is_top_level})
162+
enable_testing()
163+
endif()
164+
165+
add_subdirectory(tests)
166+
endif()
167+
168+
if(MYLIB_BUILD_EXAMPLES)
169+
add_subdirectory(examples)
170+
endif()
171+
172+
if(MYLIB_BUILD_DOCS)
173+
find_package(Doxygen REQUIRED)
174+
doxygen_add_docs(docs include)
175+
endif()

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Overview

cmake/mylib-config.cmake.in

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# CMake package config file for mylib library.
2+
#
3+
# The following target is imported:
4+
#
5+
# mylib::mylib
6+
#
7+
# Type of target to import (static or shared) is determined by the following algorithm:
8+
#
9+
# 1. if MYLIB_SHARED_LIBS is defined and:
10+
# 1.1 is true, then import shared library target or issue error
11+
# 1.2 is false, then import static library target or issue error
12+
#
13+
# 2. if BUILD_SHARED_LIBS is true and file containing shared library target definition exists,
14+
# then import shared library target
15+
#
16+
# 3. if file containing static library target definition exists, then import static library target
17+
#
18+
# 4. import shared library target or issue error
19+
20+
@PACKAGE_INIT@
21+
22+
macro(import_targets type)
23+
if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/mylib-${type}-export.cmake")
24+
set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "mylib ${type} libraries were requested but not found")
25+
set(${CMAKE_FIND_PACKAGE_NAME}_FOUND OFF)
26+
return()
27+
endif()
28+
29+
include("${CMAKE_CURRENT_LIST_DIR}/mylib-${type}-export.cmake")
30+
endmacro()
31+
32+
if(NOT TARGET mylib::mylib)
33+
set(type "")
34+
35+
if(DEFINED MYLIB_SHARED_LIBS)
36+
if(MYLIB_SHARED_LIBS)
37+
set(type "shared")
38+
else()
39+
set(type "static")
40+
endif()
41+
elseif(BUILD_SHARED_LIBS AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/mylib-shared-export.cmake")
42+
set(type "shared")
43+
elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/mylib-static-export.cmake")
44+
set(type "static")
45+
else()
46+
set(type "shared")
47+
endif()
48+
49+
import_targets(${type})
50+
check_required_components(mylib)
51+
message("-- Found ${type} mylib (version ${${CMAKE_FIND_PACKAGE_NAME}_VERSION})")
52+
endif()

cmake/utils.cmake

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# set_if_undefined(<variable> [<value>]...) - set variable if it is not defined
2+
macro(set_if_undefined variable)
3+
if(NOT DEFINED ${variable})
4+
set(${variable} ${ARGN})
5+
endif()
6+
endmacro()

examples/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(add)

examples/add/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(mylib-add LANGUAGES CXX)
3+
4+
find_package(mylib REQUIRED)
5+
6+
set(sources main.cpp)
7+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
8+
9+
add_executable(mylib-add)
10+
target_sources(mylib-add PRIVATE ${sources})
11+
target_link_libraries(mylib-add PRIVATE mylib::mylib)

examples/add/main.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <mylib/mylib.h>
2+
3+
int main(int argc, char* argv[])
4+
{
5+
return 0;
6+
}

include/mylib/export.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#ifndef MYLIB_STATIC_DEFINE
4+
# include <mylib/export_shared.h>
5+
#else
6+
# include <mylib/export_static.h>
7+
#endif

include/mylib/mylib.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include <mylib/export.h>
4+
5+
namespace mylib {
6+
7+
MYLIB_EXPORT int add(int a, int b);
8+
}

src/mylib.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <mylib/mylib.h>
2+
3+
namespace mylib {
4+
5+
int add(int a, int b)
6+
{
7+
return a + b;
8+
}
9+
} // namespace mylib

0 commit comments

Comments
 (0)