Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake support and tests #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 140
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

28 changes: 28 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*'
WarningsAsErrors: ''
HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h'
AnalyzeTemporaryDtors: false
FormatStyle: none

CheckOptions:
- key: google-readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-function-size.StatementThreshold
value: '800'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: modernize-loop-convert.MaxCopySize
value: '16'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: modernize-loop-convert.NamingStyle
value: CamelCase
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: modernize-replace-auto-ptr.IncludeStyle
value: llvm
- key: modernize-use-nullptr.NullMacros
value: 'NULL'

5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test-linux
test-win
build/
.vscode/
.DS_Store
101 changes: 101 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
cmake_minimum_required(VERSION 3.13)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

project(cfgpath LANGUAGES C CXX)

include(string_utils)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/)

if (MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif ()

# Get version from cfgpath.core.h
file(READ include/cfgpath/cfgpath.core.h core_h)
if (NOT core_h MATCHES "CFGPATH_VERSION ([0-9]+)([0-9][0-9])([0-9][0-9])")
message(FATAL_ERROR "Cannot get CFGPATH_VERSION from core.h.")
endif ()

# Use math to skip leading zeros if any.
math(EXPR CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_MATCH_1})
math(EXPR CPACK_PACKAGE_VERSION_MINOR ${CMAKE_MATCH_2})
math(EXPR CPACK_PACKAGE_VERSION_PATCH ${CMAKE_MATCH_3})
join(CFGPATH_VERSION
${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})

message(STATUS "Version: ${CFGPATH_VERSION}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

include(CheckCXXCompilerFlag)

# Generate 'compile_commands.json' for clang_complete
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CXX_STD 14 CACHE STRING "Set to 11, 14, 17 or 20 to enable C++11, C++14, C++17 or C++20 builds, respectively." FORCE)
set(C_STD 11 CACHE STRING "Set to 99 or 11 to enable C99 or C11 builds, respectively." FORCE)

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options("-Wno-c++11-extensions" "-Wno-c++11-long-long" "-Wno-deprecated-declarations")
endif()
endif()

option(CFGPATH_BUILD_TESTS "Build with tests enabled." ON)

include_directories(${CMAKE_CURRENT_LIST_DIR}/include)

# Define the cfgpath library, its includes and the needed defines.
file(GLOB CFGPATH_HEADER
${CMAKE_CURRENT_LIST_DIR}/include/cfgpath/*.h
${CMAKE_CURRENT_LIST_DIR}/include/cfgpath/detail/*.h
${CMAKE_CURRENT_LIST_DIR}/include/cfgpath/detail/polyfills/*.h)

file(GLOB CFGPATH_SRC
${CMAKE_CURRENT_LIST_DIR}/src/*.c
${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)

add_library(cfgpath ${CFGPATH_SRC} ${CFGPATH_HEADER} README.md)

if(APPLE)
target_link_libraries(cfgpath "-framework Carbon")
endif()

add_library(cfgpath::cfgpath ALIAS cfgpath)

target_include_directories(cfgpath PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

set_target_properties(cfgpath PROPERTIES
VERSION ${CFGPATH_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}
DEBUG_POSTFIX d)

if (BUILD_SHARED_LIBS)
if (UNIX AND NOT APPLE)
# Fix rpmlint warning:
# unused-direct-shlib-dependency /usr/lib/libcfgpath.so.1.1.0 /lib/libm.so.6.
target_link_libraries(cfgpath -Wl,--as-needed)
endif ()
target_compile_definitions(cfgpath PRIVATE CFGPATH_EXPORT INTERFACE CFGPATH_SHARED)
endif ()

add_library(cfgpath-header-only INTERFACE)
add_library(cfgpath::cfgpath-header-only ALIAS cfgpath-header-only)

target_compile_definitions(cfgpath-header-only INTERFACE CFGPATH_HEADER_ONLY=1)

target_include_directories(cfgpath-header-only INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

if(CFGPATH_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()

# installer
include(CPack)
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
13 changes: 0 additions & 13 deletions Makefile

This file was deleted.

62 changes: 46 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cfgpath.h: Cross platform methods for obtaining paths to configuration files
============================================================================
Copyright 2013 Adam Nielsen <[email protected]>

Copyright (C) 2013 Adam Nielsen <[email protected]>

Copyright (C) 2019 LonghronShen <[email protected]>

---

Expand All @@ -15,24 +18,51 @@ purpose. If you add new platform support, please contribute a patch!

Basic use:

#include "cfgpath.h"

char cfgdir[MAX_PATH];
get_user_config_file(cfgdir, sizeof(cfgdir), "myapp");
if (cfgdir[0] == 0) {
printf("Unable to find home directory.\n");
return 1;
}
printf("Saving configuration file to %s\n", cfgdir);
```C

#include <cfgpath/cfgpath.h>

char cfgdir[MAX_PATH];
get_user_config_file(cfgdir, sizeof(cfgdir), "myapp");
if (cfgdir[0] == 0)
{
printf("Unable to find home directory.\n");
return 1;
}
printf("Saving configuration file to %s\n", cfgdir);

```
If you are using C++, you can also use it like this:

```C++

#include <exception>
#include <cfgpath/cfgpath.h>

auto config_folder_path = libcfgpath::cfgpath::get_folder_path(libcfgpath::known_spefial_folder::CONFIG, "myapp");

if (config_folder_path == "")
{
throw std::exception("Unable to find config directory.");
}

```

To integrate it into your own project:

* Just copy the **include** folder into your own project.

* Or clone the project as a submodule of your project
1. Add the **include** folder as an 'additional include directory' of your project;

2. If you are using CMake, you can use **add_subdirectory** to add the cloned submodule folder as an externl dependency, and link your project with the **cfgpath** library. Of course, you can also choose to use the **header only mode** by setting the **CFGPATH_HEADER_ONLY** variable.

To integrate it into your own project, just copy cfgpath.h. All the other
files are for testing to make sure it works correctly, so you don't need them
unless you intend to make changes and send me a patch.
All the other files are for testing to make sure it works correctly, so you don't need them unless you intend to make changes and send me a patch.

Supported platforms are currently:

* Linux
* Mac OS X
* Windows
* Linux
* Mac OS X
* Windows

Patches adding support for more platforms would be greatly appreciated.
Loading