Skip to content

Commit

Permalink
Add code formatting (#17)
Browse files Browse the repository at this point in the history
* add style config and enforce formatting

* remove leading underscore from var name
  • Loading branch information
TheLartians authored Feb 15, 2021
1 parent bb6a707 commit 16b9cd5
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 68 deletions.
16 changes: 16 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
BasedOnStyle: Google
AccessModifierOffset: '-2'
AlignTrailingComments: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AlwaysBreakTemplateDeclarations: 'No'
BreakBeforeBraces: Attach
ColumnLimit: '100'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
IncludeBlocks: Regroup
IndentPPDirectives: AfterHash
IndentWidth: '2'
NamespaceIndentation: All
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: 'true'
...
57 changes: 57 additions & 0 deletions .cmake-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
format:
tab_size: 2
line_width: 100
dangle_parens: true

parse:
additional_commands:
cpmaddpackage:
pargs:
nargs: '*'
flags: []
spelling: CPMAddPackage
kwargs: &cpmaddpackagekwargs
NAME: 1
FORCE: 1
VERSION: 1
GIT_TAG: 1
DOWNLOAD_ONLY: 1
GITHUB_REPOSITORY: 1
GITLAB_REPOSITORY: 1
GIT_REPOSITORY: 1
SVN_REPOSITORY: 1
SVN_REVISION: 1
SOURCE_DIR: 1
DOWNLOAD_COMMAND: 1
FIND_PACKAGE_ARGUMENTS: 1
NO_CACHE: 1
GIT_SHALLOW: 1
URL: 1
URL_HASH: 1
URL_MD5: 1
DOWNLOAD_NAME: 1
DOWNLOAD_NO_EXTRACT: 1
HTTP_USERNAME: 1
HTTP_PASSWORD: 1
OPTIONS: +
cpmfindpackage:
pargs:
nargs: '*'
flags: []
spelling: CPMFindPackage
kwargs: *cpmaddpackagekwargs
packageproject:
pargs:
nargs: '*'
flags: []
spelling: packageProject
kwargs:
NAME: 1
VERSION: 1
NAMESPACE: 1
INCLUDE_DIR: 1
INCLUDE_DESTINATION: 1
BINARY_DIR: 1
COMPATIBILITY: 1
VERSION_HEADER: 1
DEPENDENCIES: +
28 changes: 28 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Style

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
style:

runs-on: macos-latest

steps:
- uses: actions/checkout@v1

- name: Install format dependencies
run: |
brew install clang-format
pip3 install cmake_format==0.6.11 pyyaml
- name: configure
run: cmake -Stest/style -Bbuild/style

- name: check style
run: cmake --build build/style --target check-format
72 changes: 33 additions & 39 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@

set(
PACKAGE_PROJECT_ROOT_PATH
"${CMAKE_CURRENT_LIST_DIR}"
CACHE INTERNAL
"The path to the PackageProject directory"
set(PACKAGE_PROJECT_ROOT_PATH
"${CMAKE_CURRENT_LIST_DIR}"
CACHE INTERNAL "The path to the PackageProject directory"
)

function(packageProject)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

cmake_parse_arguments(
PROJECT
"NO_VERSION_SUFFIX"
PROJECT "NO_VERSION_SUFFIX"
"NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;VERSION_HEADER;NAMESPACE"
"DEPENDENCIES"
${ARGN}
"DEPENDENCIES" ${ARGN}
)

if(DEFINED PROJECT_NO_VERSION_SUFFIX)
unset(_PROJECT_VERSION_SUFFIX)
unset(PROJECT_VERSION_SUFFIX)
else()
set(_PROJECT_VERSION_SUFFIX -${PROJECT_VERSION})
set(PROJECT_VERSION_SUFFIX -${PROJECT_VERSION})
endif()

# handle default arguments
if(NOT DEFINED PROJECT_COMPATIBILITY)
set(PROJECT_COMPATIBILITY AnyNewerVersion)
endif()

# we wanto to automatically add :: to our namespace, so only append if a namespace was given in the first place
# we also provide an alias to ensure that local and installed versions have the same name
# we wanto to automatically add :: to our namespace, so only append if a namespace was given in
# the first place we also provide an alias to ensure that local and installed versions have the
# same name
if(DEFINED PROJECT_NAMESPACE)
set(PROJECT_NAMESPACE ${PROJECT_NAMESPACE}::)
add_library(${PROJECT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME})
Expand All @@ -41,8 +37,7 @@ function(packageProject)
string(TOUPPER ${PROJECT_NAME} UPPERCASE_PROJECT_NAME)
configure_file(
${PACKAGE_PROJECT_ROOT_PATH}/version.h.in
${PROJECT_VERSION_INCLUDE_DIR}/${PROJECT_VERSION_HEADER}
@ONLY
${PROJECT_VERSION_INCLUDE_DIR}/${PROJECT_VERSION_HEADER} @ONLY
)

get_target_property(target_type ${PROJECT_NAME} TYPE)
Expand All @@ -51,11 +46,10 @@ function(packageProject)
else()
set(VISIBILITY PUBLIC)
endif()
target_include_directories(${PROJECT_NAME} ${VISIBILITY} "$<BUILD_INTERFACE:${PROJECT_VERSION_INCLUDE_DIR}>")
install(
DIRECTORY ${PROJECT_VERSION_INCLUDE_DIR}/
DESTINATION ${PROJECT_INCLUDE_DESTINATION}
target_include_directories(
${PROJECT_NAME} ${VISIBILITY} "$<BUILD_INTERFACE:${PROJECT_VERSION_INCLUDE_DIR}>"
)
install(DIRECTORY ${PROJECT_VERSION_INCLUDE_DIR}/ DESTINATION ${PROJECT_INCLUDE_DESTINATION})
endif()

write_basic_package_version_file(
Expand All @@ -67,37 +61,37 @@ function(packageProject)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX} COMPONENT Runtime
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX} COMPONENT Development
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX} COMPONENT Runtime
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX} COMPONENT Runtime
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
COMPONENT Runtime
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
COMPONENT Development
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
COMPONENT Runtime
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
COMPONENT Runtime
PUBLIC_HEADER DESTINATION ${PROJECT_INCLUDE_DESTINATION} COMPONENT Development
)

configure_package_config_file(
${PACKAGE_PROJECT_ROOT_PATH}/Config.cmake.in
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX}
${PACKAGE_PROJECT_ROOT_PATH}/Config.cmake.in "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
)

install(
EXPORT ${PROJECT_NAME}Targets
DESTINATION lib/cmake/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX}
DESTINATION lib/cmake/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
NAMESPACE ${PROJECT_NAMESPACE}
)

install(
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION
lib/cmake/${PROJECT_NAME}${_PROJECT_VERSION_SUFFIX}
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION lib/cmake/${PROJECT_NAME}${PROJECT_VERSION_SUFFIX}
)

install(
DIRECTORY ${PROJECT_INCLUDE_DIR}/
DESTINATION ${PROJECT_INCLUDE_DESTINATION}
)
install(DIRECTORY ${PROJECT_INCLUDE_DIR}/ DESTINATION ${PROJECT_INCLUDE_DESTINATION})

set(${PROJECT_NAME}_VERSION ${PROJECT_VERSION} CACHE INTERNAL "")
set(${PROJECT_NAME}_VERSION
${PROJECT_VERSION}
CACHE INTERNAL ""
)
endfunction()
12 changes: 5 additions & 7 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF)

project(PackageProjectTest
project(
PackageProjectTest
VERSION 1.0
LANGUAGES CXX
)

if (TEST_INSTALLED_VERSION)
if(TEST_INSTALLED_VERSION)
find_package(dependency 1.2.3 REQUIRED)
find_package(namespaced_dependency 4.5.6 REQUIRED)
find_package(transitive_dependency 7.8.9 REQUIRED)
Expand All @@ -19,9 +20,6 @@ endif()

add_executable(test main.cpp)

target_link_libraries(test
dependency
ns::namespaced_dependency
transitive_dependency::transitive_dependency
target_link_libraries(
test dependency ns::namespaced_dependency transitive_dependency::transitive_dependency
)

10 changes: 5 additions & 5 deletions test/dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(dependency
project(
dependency
VERSION 1.2.3
LANGUAGES CXX
)

add_library(dependency source/dependency.cpp)

target_include_directories(dependency
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
target_include_directories(
dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/PackageProject)
Expand Down
3 changes: 2 additions & 1 deletion test/dependency/source/dependency.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <dependency/version.h>

#include <iostream>

void dependencyFunction() {
std::cout << "Using dependency version " << DEPENDENCY_VERSION << std::endl;
}
1 change: 1 addition & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <namespaced_dependency/version.h>
#include <transitive_dependency/transitive_dependency.h>
#include <transitive_dependency/version.h>

#include <string>

int main() {
Expand Down
10 changes: 5 additions & 5 deletions test/namespaced_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(namespaced_dependency
project(
namespaced_dependency
VERSION 4.5.6
LANGUAGES CXX
)
Expand All @@ -10,10 +11,9 @@ add_library(${PROJECT_NAME} source/namespaced_dependency.cpp)
# the alias ${PROJECT_NAMESPACE}::${PROJECT_NAME} is automatically provided by PackageProject.cmake
# if we use the `NAMESPACE` parameter

target_include_directories(namespaced_dependency
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
target_include_directories(
namespaced_dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/PackageProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace ns {

void namespacedDependencyFunction();
void namespacedDependencyFunction();

}
12 changes: 7 additions & 5 deletions test/namespaced_dependency/source/namespaced_dependency.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <iostream>
#include <namespaced_dependency/version.h>

#include <iostream>

namespace ns {
void namespacedDependencyFunction() {
std::cout << "Using namespaced_dependency version " << NAMESPACED_DEPENDENCY_VERSION << std::endl;
}
}
void namespacedDependencyFunction() {
std::cout << "Using namespaced_dependency version " << NAMESPACED_DEPENDENCY_VERSION
<< std::endl;
}
} // namespace ns
23 changes: 23 additions & 0 deletions test/style/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(StyleCheck)

set(CPM_DOWNLOAD_VERSION 0.28.1)
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake v${CPM_DOWNLOAD_VERSION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endif()

include(${CPM_DOWNLOAD_LOCATION})

CPMAddPackage(
NAME Format.cmake
VERSION 1.6
GITHUB_REPOSITORY TheLartians/Format.cmake
OPTIONS "FORMAT_CHECK_CMAKE ON"
)
10 changes: 5 additions & 5 deletions test/transitive_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(transitive_dependency
project(
transitive_dependency
VERSION 7.8.9
LANGUAGES CXX
)
Expand All @@ -25,10 +26,9 @@ add_library(${PROJECT_NAME} source/transitive_dependency.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_link_libraries(${PROJECT_NAME} fmt cxxopts)

target_include_directories(transitive_dependency
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
target_include_directories(
transitive_dependency PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/PackageProject)
Expand Down

0 comments on commit 16b9cd5

Please sign in to comment.