Skip to content

Commit e340f29

Browse files
authored
Merge pull request #1 from p-ranav/feature/cmake_cleanup
Feature/cmake cleanup
2 parents 33d3fcc + e18983e commit e340f29

File tree

11 files changed

+271
-130
lines changed

11 files changed

+271
-130
lines changed

CMakeLists.txt

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,75 @@
1-
cmake_minimum_required(VERSION 3.6)
2-
project(GLOB)
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
32

4-
if(NOT CMAKE_BUILD_TYPE)
5-
set(CMAKE_BUILD_TYPE Release)
6-
endif()
3+
# ---- Project ----
4+
5+
# Note: update this to your new project's name and version
6+
project(
7+
Glob
8+
VERSION 1.0
9+
LANGUAGES CXX
10+
)
711

8-
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
9-
set(CMAKE_CXX_FLAGS_DEBUG "-g")
10-
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
12+
# ---- Include guards ----
1113

12-
# Disable deprecation for windows
13-
if (WIN32)
14-
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
14+
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
15+
message(
16+
FATAL_ERROR
17+
"In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
18+
)
1519
endif()
1620

17-
option(GLOB_BUILD_SHARED_LIBS "Build shared library" ON)
18-
option(GLOB_BUILD_STATIC_LIBS "Build static library" ON)
19-
option(GLOB_BUILD_SAMPLES "Build samples" ON)
21+
# ---- Add dependencies via CPM ----
22+
# see https://github.com/TheLartians/CPM.cmake for more info
2023

21-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
22-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
23-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
24+
include(cmake/CPM.cmake)
2425

25-
FILE(GLOB GLOB_SOURCES "src/glob.cpp")
26-
INCLUDE_DIRECTORIES("include")
26+
# PackageProject.cmake will be used to make our target installable
27+
CPMAddPackage(
28+
NAME PackageProject.cmake
29+
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
30+
VERSION 1.3
31+
)
2732

28-
if(GLOB_BUILD_SHARED_LIBS)
29-
ADD_LIBRARY(GLOB SHARED ${GLOB_SOURCES})
30-
SET_TARGET_PROPERTIES(GLOB PROPERTIES OUTPUT_NAME glob)
31-
TARGET_LINK_LIBRARIES(GLOB)
32-
SET_PROPERTY(TARGET GLOB PROPERTY CXX_STANDARD 17)
33-
endif()
33+
# ---- Add source files ----
3434

35-
if(GLOB_BUILD_STATIC_LIBS)
36-
ADD_LIBRARY(GLOB_STATIC STATIC ${GLOB_SOURCES})
37-
SET_TARGET_PROPERTIES(GLOB_STATIC PROPERTIES OUTPUT_NAME glob_static)
38-
TARGET_LINK_LIBRARIES(GLOB_STATIC)
39-
SET_PROPERTY(TARGET GLOB_STATIC PROPERTY CXX_STANDARD 17)
40-
endif()
35+
# Note: globbing sources is considered bad practice as CMake's generators may not detect new files
36+
# automatically. Keep that in mind when changing files, or explicitly mention them here.
37+
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
38+
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
4139

42-
if(GLOB_BUILD_SAMPLES)
43-
# GLOB_PRINT_MATCHES sample
44-
ADD_EXECUTABLE(GLOB_PRINT_MATCHES "samples/glob_and_print_matches.cpp")
45-
SET_TARGET_PROPERTIES(GLOB_PRINT_MATCHES PROPERTIES OUTPUT_NAME glob)
46-
TARGET_LINK_LIBRARIES(GLOB_PRINT_MATCHES GLOB)
47-
SET_PROPERTY(TARGET GLOB_PRINT_MATCHES PROPERTY CXX_STANDARD 17)
48-
49-
# GLOB_PRINT_MATCHES_RECURSIVE sample
50-
ADD_EXECUTABLE(GLOB_PRINT_MATCHES_RECURSIVE "samples/rglob_and_print_matches.cpp")
51-
SET_TARGET_PROPERTIES(GLOB_PRINT_MATCHES_RECURSIVE PROPERTIES OUTPUT_NAME rglob)
52-
TARGET_LINK_LIBRARIES(GLOB_PRINT_MATCHES_RECURSIVE GLOB)
53-
SET_PROPERTY(TARGET GLOB_PRINT_MATCHES_RECURSIVE PROPERTY CXX_STANDARD 17)
54-
endif()
40+
# ---- Create library ----
41+
42+
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
43+
# target: add_library(Glob INTERFACE) set_target_properties(Glob PROPERTIES
44+
# INTERFACE_COMPILE_FEATURES cxx_std_17)
45+
46+
add_library(Glob ${headers} ${sources})
47+
SET_TARGET_PROPERTIES(Glob PROPERTIES OUTPUT_NAME glob)
48+
set_target_properties(Glob PROPERTIES CXX_STANDARD 17)
49+
50+
# being a cross-platform target, we enforce standards conformance on MSVC
51+
target_compile_options(Glob PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")
52+
53+
# Link dependencies (if required) target_link_libraries(Glob PUBLIC cxxopts)
54+
55+
target_include_directories(
56+
Glob PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
57+
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
58+
)
59+
60+
# ---- Create an installable target ----
61+
# this allows users to install and find the library via `find_package()`.
62+
63+
# the location where the project's version header will be placed should match the project's regular
64+
# header paths
65+
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
5566

56-
# make check - runs unit tests
57-
if (UNIX)
58-
add_custom_target(check COMMAND ./tests)
59-
endif(UNIX)
67+
packageProject(
68+
NAME ${PROJECT_NAME}
69+
VERSION ${PROJECT_VERSION}
70+
BINARY_DIR ${PROJECT_BINARY_DIR}
71+
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
72+
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
73+
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
74+
DEPENDENCIES ""
75+
)

README.md

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
## Table of Contents
1010

1111
- [Quick Start](#quick-start)
12+
* [Build Library and Standalone Sample](#build-library-and-standalone-sample)
1213
- [API](#api)
1314
- [Wildcards](#wildcards)
14-
- [Example](#example)
15+
- [Examples](#examples)
1516
* [Match file extensions](#match-file-extensions)
1617
* [Match files in absolute pathnames](#match-files-in-absolute-pathnames)
1718
* [Wildcards: Match a range of characters listed in brackets ('[]')](#wildcards-match-a-range-of-characters-listed-in-brackets-)
1819
* [Exclude files from the matching](#exclude-files-from-the-matching)
1920
* [Wildcards: Match any one character with question mark ('?')](#wildcards-match-any-one-character-with-question-mark-)
2021
* [Case sensitivity](#case-sensitivity)
2122
- [Contributing](#contributing)
22-
- [License)(#license)
23+
- [License](#license)
2324

2425
## Quick Start
2526

@@ -29,11 +30,14 @@
2930
- If you can't use `C++17`, you can integrate [gulrak/filesystem](https://github.com/gulrak/filesystem) with minimal effort.
3031
* MIT License
3132

33+
### Build Library and Standalone Sample
34+
3235
```bash
33-
git clone https://github.com/p-ranav/glob
34-
cd glob
35-
mkdir build && cd build
36-
cmake .. && make
36+
cmake -Hall -Bbuild
37+
cmake --build build
38+
39+
# run standalone `glob` sample
40+
./build/standalone/glob --help
3741
```
3842

3943
## API
@@ -77,28 +81,7 @@ vector<filesystem::path> rglob(vector<string> pathnames);
7781
| `[-]` | any character in the range listed in brackets | `[A-Z]*` matches files starting with capital letters |
7882
| `[!]` | any character listed in the brackets | `[!ABC]*` matches files that do not start with A,B or C |
7983

80-
## Example
81-
82-
Below is a short program that runs `glob` and prints matching path names.
83-
84-
***NOTE*** Replace `glob` with `rglob` if you want to glob recursively.
85-
86-
```cpp
87-
#include <glob/glob.h>
88-
#include <iostream>
89-
90-
int main(int argc, char *argv[]) {
91-
if (argc != 2) {
92-
std::cerr << "Usage: ./exe <pattern>\n";
93-
return EXIT_FAILURE;
94-
}
95-
96-
// Run glob on the vector of patterns
97-
for (auto &f : glob::glob(argv[1])) {
98-
std::cout << f << "\n";
99-
}
100-
}
101-
```
84+
## Examples
10285

10386
### Match file extensions
10487

@@ -118,19 +101,19 @@ foo@bar:~$ tree
118101

119102
3 directories, 7 files
120103

121-
foo@bar:~$ ./main "**/*.hpp"
104+
foo@bar:~$ ./glob -i "**/*.hpp"
122105
"test/doctest.hpp"
123106

124-
foo@bar:~$ ./main "**/**/*.hpp"
107+
foo@bar:~$ ./glob -i "**/**/*.hpp"
125108
"include/foo/baz.hpp"
126109
"include/foo/foo.hpp"
127110
"include/foo/bar.hpp"
128111
```
129112

130-
***NOTE*** If you use `rglob` instead of `glob`:
113+
***NOTE*** If you run glob recursively, i.e., using `rglob`:
131114

132115
```console
133-
foo@bar:~$ ./main "**/*.hpp"
116+
foo@bar:~$ ./glob -r -i "**/*.hpp"
134117
"test/doctest.hpp"
135118
"include/foo/baz.hpp"
136119
"include/foo/foo.hpp"
@@ -140,7 +123,7 @@ foo@bar:~$ ./main "**/*.hpp"
140123
### Match files in absolute pathnames
141124

142125
```console
143-
foo@bar:~$ ./main '/usr/local/include/nc*.h'
126+
foo@bar:~$ ./glob -i '/usr/local/include/nc*.h'
144127
"/usr/local/include/ncCheck.h"
145128
"/usr/local/include/ncGroupAtt.h"
146129
"/usr/local/include/ncUshort.h"
@@ -162,13 +145,13 @@ foo@bar:~$ ./main '/usr/local/include/nc*.h'
162145
foo@bar:~$ ls test_files_02
163146
1.txt 2.txt 3.txt 4.txt
164147

165-
foo@bar:~$ ./main 'test_files_02/[0-9].txt'
148+
foo@bar:~$ ./glob -i 'test_files_02/[0-9].txt'
166149
"test_files_02/4.txt"
167150
"test_files_02/3.txt"
168151
"test_files_02/2.txt"
169152
"test_files_02/1.txt"
170153

171-
foo@bar:~$ ./main 'test_files_02/[1-2]*'
154+
foo@bar:~$ ./glob -i 'test_files_02/[1-2]*'
172155
"test_files_02/2.txt"
173156
"test_files_02/1.txt"
174157
```
@@ -177,7 +160,7 @@ foo@bar:~$ ./main 'test_files_02/[1-2]*'
177160
foo@bar:~$ ls test_files_03
178161
file1.txt file2.txt file3.txt file4.txt
179162

180-
foo@bar:~$ ./main 'test_files_03/file[0-9].*'
163+
foo@bar:~$ ./glob -i 'test_files_03/file[0-9].*'
181164
"test_files_03/file2.txt"
182165
"test_files_03/file3.txt"
183166
"test_files_03/file1.txt"
@@ -190,14 +173,14 @@ foo@bar:~$ ./main 'test_files_03/file[0-9].*'
190173
foo@bar:~$ ls test_files_01
191174
__init__.py bar.py foo.py
192175

193-
foo@bar:~$ ./main 'test_files_01/*[!__init__].py'
176+
foo@bar:~$ ./glob -i 'test_files_01/*[!__init__].py'
194177
"test_files_01/bar.py"
195178
"test_files_01/foo.py"
196179

197-
foo@bar:~$ ./main 'test_files_01/*[!__init__][!bar].py'
180+
foo@bar:~$ ./glob -i 'test_files_01/*[!__init__][!bar].py'
198181
"test_files_01/foo.py"
199182

200-
foo@bar:~$ ./main 'test_files_01/[!_]*.py'
183+
foo@bar:~$ ./glob -i 'test_files_01/[!_]*.py'
201184
"test_files_01/bar.py"
202185
"test_files_01/foo.py"
203186
```
@@ -208,7 +191,7 @@ foo@bar:~$ ./main 'test_files_01/[!_]*.py'
208191
foo@bar:~$ ls test_files_02
209192
1.txt 2.txt 3.txt 4.txt
210193

211-
foo@bar:~$ ./main 'test_files_02/?.txt'
194+
foo@bar:~$ ./glob -i 'test_files_02/?.txt'
212195
"test_files_02/4.txt"
213196
"test_files_02/3.txt"
214197
"test_files_02/2.txt"
@@ -219,7 +202,7 @@ foo@bar:~$ ./main 'test_files_02/?.txt'
219202
foo@bar:~$ ls test_files_03
220203
file1.txt file2.txt file3.txt file4.txt
221204

222-
foo@bar:~$ ./main 'test_files_03/????[3-4].txt'
205+
foo@bar:~$ ./glob -i 'test_files_03/????[3-4].txt'
223206
"test_files_03/file3.txt"
224207
"test_files_03/file4.txt"
225208
```
@@ -232,11 +215,17 @@ foo@bar:~$ ./main 'test_files_03/????[3-4].txt'
232215
foo@bar:~$ ls test_files_05
233216
file1.png file2.png file3.PNG file4.PNG
234217

235-
foo@bar:~$ ./main 'test_files_05/*.png'
218+
foo@bar:~$ ./glob -i 'test_files_05/*.png'
236219
"test_files_05/file2.png"
237220
"test_files_05/file1.png"
238221

239-
foo@bar:~$ ./main 'test_files_05/*.PNG'
222+
foo@bar:~$ ./glob -i 'test_files_05/*.PNG'
223+
"test_files_05/file3.PNG"
224+
"test_files_05/file4.PNG"
225+
226+
foo@bar:~$ ./glob -i "test_files_05/*.png","test_files_05/*.PNG"
227+
"test_files_05/file2.png"
228+
"test_files_05/file1.png"
240229
"test_files_05/file3.PNG"
241230
"test_files_05/file4.PNG"
242231
```

all/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# this script adds all subprojects to a single build to allow IDEs understand the full project
2+
# structure.
3+
4+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
5+
6+
project(BuildAll LANGUAGES CXX)
7+
8+
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../standalone ${CMAKE_BINARY_DIR}/standalone)

clang-format.bash

Lines changed: 0 additions & 2 deletions
This file was deleted.

cmake/CPM.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
set(CPM_DOWNLOAD_VERSION 0.27.2)
2+
3+
if(CPM_SOURCE_CACHE)
4+
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
5+
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
6+
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
7+
else()
8+
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
9+
endif()
10+
11+
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
12+
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
13+
file(DOWNLOAD
14+
https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
15+
${CPM_DOWNLOAD_LOCATION}
16+
)
17+
endif()
18+
19+
include(${CPM_DOWNLOAD_LOCATION})

0 commit comments

Comments
 (0)