Commit 7687948ac129fb097d822a3b6a8787fa037cb240

Edward Thomson 2021-11-11T16:13:38

Merge pull request #6112 from libgit2/ethomson/cmake3 cmake refactorings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index e805b1b..6e342d1 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -91,7 +91,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DDEBUG_STRICT_ALLOC=ON -DDEBUG_STRICT_OPEN=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON -DDEBUG_STRICT_ALLOC=ON -DDEBUG_STRICT_OPEN=ON
           os: ubuntu-latest
         - # Xenial, GCC, mbedTLS
           container:
@@ -99,7 +99,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
           os: ubuntu-latest
         - # Xenial, Clang, OpenSSL
           container:
@@ -107,14 +107,14 @@ jobs:
           env:
             CC: clang
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
           os: ubuntu-latest
         - # Xenial, Clang, mbedTLS
           container:
             name: xenial
           env:
             CC: clang
-            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
           os: ubuntu-latest
         - # Focal, Clang 10, mbedTLS, MemorySanitizer
@@ -123,7 +123,7 @@ jobs:
           env:
             CC: clang-10
             CFLAGS: -fsanitize=memory -fsanitize-memory-track-origins=2 -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer
-            CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local/msan -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
+            CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local/msan -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
             SKIP_SSH_TESTS: true
             SKIP_NEGOTIATE_TESTS: true
@@ -136,7 +136,7 @@ jobs:
           env:
             CC: clang-10
             CFLAGS: -fsanitize=undefined,nullability -fno-sanitize-recover=undefined,nullability -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer
-            CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
+            CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
             SKIP_SSH_TESTS: true
             SKIP_NEGOTIATE_TESTS: true
@@ -149,7 +149,7 @@ jobs:
           env:
             CC: clang-10
             CFLAGS: -fsanitize=thread -fno-optimize-sibling-calls -fno-omit-frame-pointer
-            CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
+            CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=OpenSSL -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
             SKIP_SSH_TESTS: true
             SKIP_NEGOTIATE_TESTS: true
diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml
index 47ebf46..789f268 100644
--- a/.github/workflows/nightly.yml
+++ b/.github/workflows/nightly.yml
@@ -25,7 +25,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DREGEX_BACKEND=builtin -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
           os: ubuntu-latest
         - # Xenial, GCC, mbedTLS
           container:
@@ -33,7 +33,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
           os: ubuntu-latest
         - # Xenial, Clang, OpenSSL
           container:
@@ -41,14 +41,14 @@ jobs:
           env:
             CC: clang
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
           os: ubuntu-latest
         - # Xenial, Clang, mbedTLS
           container:
             name: xenial
           env:
             CC: clang
-            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
           os: ubuntu-latest
         - # Xenial, GCC, thread-free
@@ -56,7 +56,7 @@ jobs:
             name: xenial
           env:
             CC: gcc
-            CMAKE_OPTIONS: -DTHREADSAFE=OFF -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DTHREADSAFE=OFF -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
           os: ubuntu-latest
         - # Xenial, Clang, OpenSSL (dynamically loaded)
@@ -64,7 +64,7 @@ jobs:
             name: xenial
           env:
             CC: clang
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             CMAKE_GENERATOR: Ninja
           os: ubuntu-latest
         - # Focal, Clang 10, mbedTLS, MemorySanitizer
@@ -119,7 +119,7 @@ jobs:
           container:
             name: centos7
           env:
-            CMAKE_OPTIONS: -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             PKG_CONFIG_PATH: /usr/local/lib/pkgconfig
             SKIP_NEGOTIATE_TESTS: true
           os: ubuntu-latest
@@ -127,7 +127,7 @@ jobs:
           container:
             name: centos7
           env:
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             PKG_CONFIG_PATH: /usr/local/lib/pkgconfig
             SKIP_NEGOTIATE_TESTS: true
           os: ubuntu-latest
@@ -212,7 +212,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL-Dynamic -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             RUN_INVASIVE_TESTS: true
           os: ubuntu-latest
         - # Bionic, x86, Clang, OpenSSL
@@ -223,7 +223,7 @@ jobs:
           env:
             CC: clang
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             RUN_INVASIVE_TESTS: true
           os: ubuntu-latest
         - # Bionic, x86, GCC, OpenSSL
@@ -233,7 +233,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_LEAK_CHECKER=valgrind -DUSE_GSSAPI=ON -DUSE_SSH=ON
             RUN_INVASIVE_TESTS: true
           os: ubuntu-latest
         - # Bionic, arm32, GCC, OpenSSL
@@ -244,7 +244,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON -DUSE_SSH=ON
             RUN_INVASIVE_TESTS: true
             SKIP_PROXY_TESTS: true
           os: ubuntu-latest
@@ -256,7 +256,7 @@ jobs:
           env:
             CC: gcc
             CMAKE_GENERATOR: Ninja
-            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON
+            CMAKE_OPTIONS: -DUSE_HTTPS=OpenSSL -DDEPRECATE_HARD=ON -DUSE_GSSAPI=ON -DUSE_SSH=ON
             RUN_INVASIVE_TESTS: true
             SKIP_PROXY_TESTS: true
           os: ubuntu-latest
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f7b2e71..ae0a5d6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,38 +6,24 @@ cmake_minimum_required(VERSION 3.5.1)
 project(libgit2 VERSION "1.3.0" LANGUAGES C)
 
 # Add find modules to the path
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake/")
-
-# Modules
-
-include(CheckLibraryExists)
-include(CheckFunctionExists)
-include(CheckSymbolExists)
-include(CheckStructHasMember)
-include(CheckPrototypeDefinition)
-include(AddCFlagIfSupported)
-include(FindPkgLibraries)
-include(FindThreads)
-include(FindStatNsec)
-include(Findfutimens)
-include(GNUInstallDirs)
-include(IdeSplitSources)
-include(FeatureSummary)
-include(EnableWarnings)
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake")
 
 #
 # Build options
 #
 
 # Optional subsystems
-option(THREADSAFE              "Build libgit2 as threadsafe"                            ON)
 option(BUILD_SHARED_LIBS       "Build Shared Library (OFF for Static)"                  ON)
 option(BUILD_TESTS             "Build Tests using the Clar suite"                       ON)
 option(BUILD_EXAMPLES          "Build library usage example apps"                      OFF)
 option(BUILD_FUZZERS           "Build the fuzz targets"                                OFF)
 
+# Suggested functionality that may not be available on a per-platform basis
+option(USE_THREADS             "Use threads for parallel processing when possible"      ON)
+option(USE_NSEC                "Support nanosecond precision file mtimes and ctimes"    ON)
+
 # Backend selection
-option(USE_SSH                 "Link with libssh2 to enable SSH support"                ON)
+option(USE_SSH                 "Link with libssh2 to enable SSH support"               OFF)
 option(USE_HTTPS               "Enable HTTPS support. Can be set to a specific backend" ON)
 option(USE_SHA1                "Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS/Generic" ON)
 option(USE_GSSAPI              "Link with libgssapi for SPNEGO auth"      OFF)
@@ -55,7 +41,7 @@ option(DEBUG_STRICT_OPEN       "Enable path validation in open"                 
 # Output options
 option(SONAME                  "Set the (SO)VERSION of the target"                      ON)
 option(LIBGIT2_FILENAME        "Name of the produced binary"                           OFF)
-option(DEPRECATE_HARD          "Do not include deprecated functions in the library"	   OFF)
+option(DEPRECATE_HARD          "Do not include deprecated functions in the library"    OFF)
 
 # Compilation options
 option(ENABLE_WERROR           "Enable compilation with -Werror"                       OFF)
@@ -63,217 +49,60 @@ option(ENABLE_WERROR           "Enable compilation with -Werror"                
 if(UNIX)
 	# NTLM client requires crypto libraries from the system HTTPS stack
 	if(NOT USE_HTTPS)
-	    option(USE_NTLMCLIENT  "Enable NTLM support on Unix."                          OFF)
+		option(USE_NTLMCLIENT  "Enable NTLM support on Unix."                  OFF)
 	else()
-	    option(USE_NTLMCLIENT  "Enable NTLM support on Unix."                           ON)
+		option(USE_NTLMCLIENT  "Enable NTLM support on Unix."                   ON)
 	endif()
 
-	option(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds"                     OFF)
+	option(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds"                 OFF)
 endif()
 
 if(APPLE)
-	option(USE_ICONV           "Link with and use iconv library"                        ON)
+	option(USE_ICONV           "Link with and use iconv library"                    ON)
 endif()
 
 if(MSVC)
 	# This option must match the settings used in your program, in particular if you
 	# are linking statically
-	option(STATIC_CRT          "Link the static CRT libraries"                          ON)
+	option(STATIC_CRT          "Link the static CRT libraries"                      ON)
 
 	# If you want to embed a copy of libssh2 into libgit2, pass a
 	# path to libssh2
-	option(EMBED_SSH_PATH      "Path to libssh2 to embed (Windows)"                    OFF)
+	option(EMBED_SSH_PATH      "Path to libssh2 to embed (Windows)"                OFF)
 
 	# Enable leak checking using the debugging C runtime.
-	option(WIN32_LEAKCHECK     "Enable leak reporting via crtdbg"                      OFF)
+	option(WIN32_LEAKCHECK     "Enable leak reporting via crtdbg"                  OFF)
 endif()
 
 if(WIN32)
 	# By default, libgit2 is built with WinHTTP.  To use the built-in
-	# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
-	option(WINHTTP             "Use Win32 WinHTTP routines"                             ON)
+	# HTTP transport, invoke CMake with the "-DUSE_WINHTTP=OFF" argument.
+	option(USE_WINHTTP         "Use Win32 WinHTTP routines"                         ON)
 endif()
 
-
-#
-# Compiler / linker flags
-#
-
-if(DEPRECATE_HARD)
-	add_definitions(-DGIT_DEPRECATE_HARD)
+if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
+	set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
 endif()
 
-# Platform specific compilation flags
-if(MSVC)
-	add_definitions(-D_SCL_SECURE_NO_WARNINGS)
-	add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
-	add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
-
-	string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
-
-	# /GF - String pooling
-	# /MP - Parallel build
-	set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
-
-	# /Gd - explicitly set cdecl calling convention
-	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
-
-	if(NOT (MSVC_VERSION LESS 1900))
-		# /guard:cf - Enable Control Flow Guard
-		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
-	endif()
-
-	if(STATIC_CRT)
-		set(CRT_FLAG_DEBUG "/MTd")
-		set(CRT_FLAG_RELEASE "/MT")
-	else()
-		set(CRT_FLAG_DEBUG "/MDd")
-		set(CRT_FLAG_RELEASE "/MD")
-	endif()
-
-	if(WIN32_LEAKCHECK)
-		set(GIT_WIN32_LEAKCHECK 1)
-		set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
-		set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
-	endif()
-
-	# /Zi - Create debugging information
-	# /Od - Disable optimization
-	# /D_DEBUG - #define _DEBUG
-	# /MTd - Statically link the multithreaded debug version of the CRT
-	# /MDd - Dynamically link the multithreaded debug version of the CRT
-	# /RTC1 - Run time checks
-	set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
-
-	# /DNDEBUG - Disables asserts
-	# /MT - Statically link the multithreaded release version of the CRT
-	# /MD - Dynamically link the multithreaded release version of the CRT
-	# /O2 - Optimize for speed
-	# /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
-	# /GL - Link time code generation (whole program optimization)
-	# /Gy - Function-level linking
-	set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
-
-	# /Oy- - Disable frame pointer omission (FPO)
-	set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
-
-	# /O1 - Optimize for size
-	set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
-
-	# /IGNORE:4221 - Ignore empty compilation units
-	set(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
-
-	# /DYNAMICBASE - Address space load randomization (ASLR)
-	# /NXCOMPAT - Data execution prevention (DEP)
-	# /LARGEADDRESSAWARE - >2GB user address space on x86
-	# /VERSION - Embed version information in PE header
-	set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
-
-	if(NOT (MSVC_VERSION LESS 1900))
-		# /GUARD:CF - Enable Control Flow Guard
-		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
-	endif()
 
-	# /DEBUG - Create a PDB
-	# /LTCG - Link time code generation (whole program optimization)
-	# /OPT:REF /OPT:ICF - Fold out duplicate code at link step
-	# /INCREMENTAL:NO - Required to use /LTCG
-	# /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
-	set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
-	set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
-	set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
-	set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
-
-	# Same linker settings for DLL as EXE
-	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
-	set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
-	set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
-	set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
-	set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
-else()
-	if(ENABLE_REPRODUCIBLE_BUILDS)
-		set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
-		set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq  <TARGET> <LINK_FLAGS> <OBJECTS>")
-		set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
-	endif()
-
-	if(NOT BUILD_SHARED_LIBS)
-		set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
-	endif()
-
-	set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
-
-	enable_warnings(all)
-	enable_warnings(extra)
-
-	if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
-		set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
-	endif()
-
-	set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
-
-	if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
-		string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
-	elseif(BUILD_SHARED_LIBS)
-		add_c_flag_IF_SUPPORTED(-fvisibility=hidden)
-
-		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
-	endif()
-
-	if(MINGW)
-		# MinGW >= 3.14 uses the C99-style stdio functions
-		# automatically, but forks like mingw-w64 still want
-		# us to define this in order to use them
-		add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
-	endif()
-
-	enable_warnings(documentation)
-	disable_warnings(documentation-deprecated-sync)
-	disable_warnings(missing-field-initializers)
-	enable_warnings(strict-aliasing)
-	enable_warnings(strict-prototypes)
-	enable_warnings(declaration-after-statement)
-	enable_warnings(shift-count-overflow)
-	enable_warnings(unused-const-variable)
-	enable_warnings(unused-function)
-	enable_warnings(int-conversion)
-	enable_warnings(c11-extensions)
-	enable_warnings(c99-c11-compat)
-
-	# MinGW uses gcc, which expects POSIX formatting for printf, but
-	# uses the Windows C library, which uses its own format specifiers.
-	# Disable format specifier warnings.
-	if(MINGW)
-		disable_warnings(format)
-		disable_warnings(format-security)
-	else()
-		enable_warnings(format)
-		enable_warnings(format-security)
-	endif()
-endif()
-
-# Ensure that MinGW provides the correct header files.
-if(WIN32 AND NOT CYGWIN)
-	add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600)
-endif()
+# Modules
 
-if(NOT CMAKE_CONFIGURATION_TYPES)
-	# Build Debug by default
-	if(NOT CMAKE_BUILD_TYPE)
-		set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
-	endif()
-else()
-	# Using a multi-configuration generator eg MSVC or Xcode
-	# that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
-endif()
+include(CheckLibraryExists)
+include(CheckFunctionExists)
+include(CheckSymbolExists)
+include(CheckStructHasMember)
+include(CheckPrototypeDefinition)
+include(AddCFlagIfSupported)
+include(FindPkgLibraries)
+include(FindThreads)
+include(FindStatNsec)
+include(Findfutimens)
+include(GNUInstallDirs)
+include(IdeSplitSources)
+include(FeatureSummary)
+include(EnableWarnings)
+include(DefaultCFlags)
 
-IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
-	# The actual sanitizer link target will be added when linking the fuzz
-	# targets.
-	set(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
-	add_c_flag(-fsanitize=fuzzer-no-link)
-	unset(CMAKE_REQUIRED_FLAGS)
-endif()
 
 #
 # Subdirectories
@@ -282,7 +111,6 @@ endif()
 add_subdirectory(src)
 
 if(BUILD_TESTS)
-	enable_testing()
 	add_subdirectory(tests)
 endif()
 
@@ -291,17 +119,13 @@ if(BUILD_EXAMPLES)
 endif()
 
 if(BUILD_FUZZERS)
-	if(NOT USE_STANDALONE_FUZZERS)
-		if(BUILD_EXAMPLES)
-			message(FATAL_ERROR "Cannot build the fuzzer targets and the examples together")
-		endif()
-		if(BUILD_TESTS)
-			message(FATAL_ERROR "Cannot build the fuzzer targets and the tests together")
-		endif()
+	if((BUILD_TESTS OR BUILD_EXAMPLES) AND NOT USE_STANDALONE_FUZZERS)
+		message(FATAL_ERROR "Cannot build the fuzzer and the tests or examples together")
 	endif()
 	add_subdirectory(fuzzers)
 endif()
 
+
 # Summary
 
 feature_summary(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
diff --git a/README.md b/README.md
index 2a365df..7ed5f6c 100644
--- a/README.md
+++ b/README.md
@@ -279,7 +279,7 @@ The following CMake variables are declared:
 - `CMAKE_INSTALL_INCLUDEDIR`: Where to install headers to.
 - `BUILD_SHARED_LIBS`: Build libgit2 as a Shared Library (defaults to ON)
 - `BUILD_TESTS`: Build the unit and integration test suites (defaults to ON)
-- `THREADSAFE`: Build libgit2 with threading support (defaults to ON)
+- `USE_THREADS`: Build libgit2 with threading support (defaults to ON)
 
 To list all build options and their current value, you can do the
 following:
diff --git a/cmake/DefaultCFlags.cmake b/cmake/DefaultCFlags.cmake
new file mode 100644
index 0000000..d122f6c
--- /dev/null
+++ b/cmake/DefaultCFlags.cmake
@@ -0,0 +1,153 @@
+# Platform specific compilation flags
+if(MSVC)
+	add_definitions(-D_SCL_SECURE_NO_WARNINGS)
+	add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
+	add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
+
+	string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
+
+	# /GF - String pooling
+	# /MP - Parallel build
+	set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
+
+	# /Gd - explicitly set cdecl calling convention
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
+
+	if(NOT (MSVC_VERSION LESS 1900))
+		# /guard:cf - Enable Control Flow Guard
+		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
+	endif()
+
+	if(STATIC_CRT)
+		set(CRT_FLAG_DEBUG "/MTd")
+		set(CRT_FLAG_RELEASE "/MT")
+	else()
+		set(CRT_FLAG_DEBUG "/MDd")
+		set(CRT_FLAG_RELEASE "/MD")
+	endif()
+
+	if(WIN32_LEAKCHECK)
+		set(GIT_WIN32_LEAKCHECK 1)
+		set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
+		set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
+	endif()
+
+	# /Zi - Create debugging information
+	# /Od - Disable optimization
+	# /D_DEBUG - #define _DEBUG
+	# /MTd - Statically link the multithreaded debug version of the CRT
+	# /MDd - Dynamically link the multithreaded debug version of the CRT
+	# /RTC1 - Run time checks
+	set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
+
+	# /DNDEBUG - Disables asserts
+	# /MT - Statically link the multithreaded release version of the CRT
+	# /MD - Dynamically link the multithreaded release version of the CRT
+	# /O2 - Optimize for speed
+	# /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
+	# /GL - Link time code generation (whole program optimization)
+	# /Gy - Function-level linking
+	set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
+
+	# /Oy- - Disable frame pointer omission (FPO)
+	set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
+
+	# /O1 - Optimize for size
+	set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
+
+	# /IGNORE:4221 - Ignore empty compilation units
+	set(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
+
+	# /DYNAMICBASE - Address space load randomization (ASLR)
+	# /NXCOMPAT - Data execution prevention (DEP)
+	# /LARGEADDRESSAWARE - >2GB user address space on x86
+	# /VERSION - Embed version information in PE header
+	set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
+
+	if(NOT (MSVC_VERSION LESS 1900))
+		# /GUARD:CF - Enable Control Flow Guard
+		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
+	endif()
+
+	# /DEBUG - Create a PDB
+	# /LTCG - Link time code generation (whole program optimization)
+	# /OPT:REF /OPT:ICF - Fold out duplicate code at link step
+	# /INCREMENTAL:NO - Required to use /LTCG
+	# /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
+	set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
+	set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
+	set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
+	set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
+
+	# Same linker settings for DLL as EXE
+	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
+	set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
+	set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
+	set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
+	set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
+else()
+	if(ENABLE_REPRODUCIBLE_BUILDS)
+		set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
+		set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq  <TARGET> <LINK_FLAGS> <OBJECTS>")
+		set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
+	endif()
+
+	if(NOT BUILD_SHARED_LIBS)
+		set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+	endif()
+
+	set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
+
+	enable_warnings(all)
+	enable_warnings(extra)
+
+	if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
+		set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
+	endif()
+
+	set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
+
+	if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
+		string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
+	elseif(BUILD_SHARED_LIBS)
+		add_c_flag_IF_SUPPORTED(-fvisibility=hidden)
+
+		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
+	endif()
+
+	if(MINGW)
+		# MinGW >= 3.14 uses the C99-style stdio functions
+		# automatically, but forks like mingw-w64 still want
+		# us to define this in order to use them
+		add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
+	endif()
+
+	enable_warnings(documentation)
+	disable_warnings(documentation-deprecated-sync)
+	disable_warnings(missing-field-initializers)
+	enable_warnings(strict-aliasing)
+	enable_warnings(strict-prototypes)
+	enable_warnings(declaration-after-statement)
+	enable_warnings(shift-count-overflow)
+	enable_warnings(unused-const-variable)
+	enable_warnings(unused-function)
+	enable_warnings(int-conversion)
+	enable_warnings(c11-extensions)
+	enable_warnings(c99-c11-compat)
+
+	# MinGW uses gcc, which expects POSIX formatting for printf, but
+	# uses the Windows C library, which uses its own format specifiers.
+	# Disable format specifier warnings.
+	if(MINGW)
+		disable_warnings(format)
+		disable_warnings(format-security)
+	else()
+		enable_warnings(format)
+		enable_warnings(format-security)
+	endif()
+endif()
+
+# Ensure that MinGW provides the correct header files.
+if(WIN32 AND NOT CYGWIN)
+	add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600)
+endif()
diff --git a/cmake/FindHTTPParser.cmake b/cmake/FindHTTPParser.cmake
new file mode 100644
index 0000000..d92bf75
--- /dev/null
+++ b/cmake/FindHTTPParser.cmake
@@ -0,0 +1,39 @@
+# - Try to find http-parser
+#
+# Defines the following variables:
+#
+# HTTP_PARSER_FOUND - system has http-parser
+# HTTP_PARSER_INCLUDE_DIR - the http-parser include directory
+# HTTP_PARSER_LIBRARIES - Link these to use http-parser
+# HTTP_PARSER_VERSION_MAJOR - major version
+# HTTP_PARSER_VERSION_MINOR - minor version
+# HTTP_PARSER_VERSION_STRING - the version of http-parser found
+
+# Find the header and library
+FIND_PATH(HTTP_PARSER_INCLUDE_DIR NAMES http_parser.h)
+FIND_LIBRARY(HTTP_PARSER_LIBRARY NAMES http_parser libhttp_parser)
+
+# Found the header, read version
+if (HTTP_PARSER_INCLUDE_DIR AND EXISTS "${HTTP_PARSER_INCLUDE_DIR}/http_parser.h")
+	FILE(READ "${HTTP_PARSER_INCLUDE_DIR}/http_parser.h" HTTP_PARSER_H)
+	IF (HTTP_PARSER_H)
+		STRING(REGEX REPLACE ".*#define[\t ]+HTTP_PARSER_VERSION_MAJOR[\t ]+([0-9]+).*" "\\1" HTTP_PARSER_VERSION_MAJOR "${HTTP_PARSER_H}")
+		STRING(REGEX REPLACE ".*#define[\t ]+HTTP_PARSER_VERSION_MINOR[\t ]+([0-9]+).*" "\\1" HTTP_PARSER_VERSION_MINOR "${HTTP_PARSER_H}")
+		SET(HTTP_PARSER_VERSION_STRING "${HTTP_PARSER_VERSION_MAJOR}.${HTTP_PARSER_VERSION_MINOR}")
+	ENDIF()
+	UNSET(HTTP_PARSER_H)
+ENDIF()
+
+# Handle the QUIETLY and REQUIRED arguments and set HTTP_PARSER_FOUND
+# to TRUE if all listed variables are TRUE
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(HTTP_Parser REQUIRED_VARS HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY)
+
+# Hide advanced variables
+MARK_AS_ADVANCED(HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY)
+
+# Set standard variables
+IF (HTTP_PARSER_FOUND)
+	SET(HTTP_PARSER_LIBRARIES ${HTTP_PARSER_LIBRARY})
+	set(HTTP_PARSER_INCLUDE_DIRS ${HTTP_PARSER_INCLUDE_DIR})
+ENDIF()
diff --git a/cmake/FindHTTP_Parser.cmake b/cmake/FindHTTP_Parser.cmake
deleted file mode 100644
index d92bf75..0000000
--- a/cmake/FindHTTP_Parser.cmake
+++ /dev/null
@@ -1,39 +0,0 @@
-# - Try to find http-parser
-#
-# Defines the following variables:
-#
-# HTTP_PARSER_FOUND - system has http-parser
-# HTTP_PARSER_INCLUDE_DIR - the http-parser include directory
-# HTTP_PARSER_LIBRARIES - Link these to use http-parser
-# HTTP_PARSER_VERSION_MAJOR - major version
-# HTTP_PARSER_VERSION_MINOR - minor version
-# HTTP_PARSER_VERSION_STRING - the version of http-parser found
-
-# Find the header and library
-FIND_PATH(HTTP_PARSER_INCLUDE_DIR NAMES http_parser.h)
-FIND_LIBRARY(HTTP_PARSER_LIBRARY NAMES http_parser libhttp_parser)
-
-# Found the header, read version
-if (HTTP_PARSER_INCLUDE_DIR AND EXISTS "${HTTP_PARSER_INCLUDE_DIR}/http_parser.h")
-	FILE(READ "${HTTP_PARSER_INCLUDE_DIR}/http_parser.h" HTTP_PARSER_H)
-	IF (HTTP_PARSER_H)
-		STRING(REGEX REPLACE ".*#define[\t ]+HTTP_PARSER_VERSION_MAJOR[\t ]+([0-9]+).*" "\\1" HTTP_PARSER_VERSION_MAJOR "${HTTP_PARSER_H}")
-		STRING(REGEX REPLACE ".*#define[\t ]+HTTP_PARSER_VERSION_MINOR[\t ]+([0-9]+).*" "\\1" HTTP_PARSER_VERSION_MINOR "${HTTP_PARSER_H}")
-		SET(HTTP_PARSER_VERSION_STRING "${HTTP_PARSER_VERSION_MAJOR}.${HTTP_PARSER_VERSION_MINOR}")
-	ENDIF()
-	UNSET(HTTP_PARSER_H)
-ENDIF()
-
-# Handle the QUIETLY and REQUIRED arguments and set HTTP_PARSER_FOUND
-# to TRUE if all listed variables are TRUE
-INCLUDE(FindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(HTTP_Parser REQUIRED_VARS HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY)
-
-# Hide advanced variables
-MARK_AS_ADVANCED(HTTP_PARSER_INCLUDE_DIR HTTP_PARSER_LIBRARY)
-
-# Set standard variables
-IF (HTTP_PARSER_FOUND)
-	SET(HTTP_PARSER_LIBRARIES ${HTTP_PARSER_LIBRARY})
-	set(HTTP_PARSER_INCLUDE_DIRS ${HTTP_PARSER_INCLUDE_DIR})
-ENDIF()
diff --git a/cmake/FindStatNsec.cmake b/cmake/FindStatNsec.cmake
index a4a09fa..9dfdf51 100644
--- a/cmake/FindStatNsec.cmake
+++ b/cmake/FindStatNsec.cmake
@@ -1,26 +1,20 @@
-INCLUDE(FeatureSummary)
+include(FeatureSummary)
 
-CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtim "sys/types.h;sys/stat.h"
+check_struct_has_member("struct stat" st_mtim "sys/types.h;sys/stat.h"
 	HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C)
-CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtimespec "sys/types.h;sys/stat.h"
+check_struct_has_member("struct stat" st_mtimespec "sys/types.h;sys/stat.h"
 	HAVE_STRUCT_STAT_ST_MTIMESPEC LANGUAGE C)
-CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtime_nsec sys/stat.h
+check_struct_has_member("struct stat" st_mtime_nsec sys/stat.h
 	HAVE_STRUCT_STAT_MTIME_NSEC LANGUAGE C)
 
-IF (HAVE_STRUCT_STAT_ST_MTIM)
-	CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtim.tv_nsec sys/stat.h
+if(HAVE_STRUCT_STAT_ST_MTIM)
+	check_struct_has_member("struct stat" st_mtim.tv_nsec sys/stat.h
 		HAVE_STRUCT_STAT_NSEC LANGUAGE C)
-ELSEIF (HAVE_STRUCT_STAT_ST_MTIMESPEC)
-	CHECK_STRUCT_HAS_MEMBER("struct stat" st_mtimespec.tv_nsec sys/stat.h
+elseif(HAVE_STRUCT_STAT_ST_MTIMESPEC)
+	check_struct_has_member("struct stat" st_mtimespec.tv_nsec sys/stat.h
 		HAVE_STRUCT_STAT_NSEC LANGUAGE C)
-ELSE ()
-	SET( HAVE_STRUCT_STAT_NSEC ON )
-ENDIF()
+else()
+	set(HAVE_STRUCT_STAT_NSEC ON )
+endif()
 
-IF (HAVE_STRUCT_STAT_NSEC OR WIN32)
-	OPTION( USE_NSEC		"Care about sub-second file mtimes and ctimes"	ON  )
-ELSE()
-	SET(USE_NSEC OFF)
-ENDIF()
-
-ADD_FEATURE_INFO(nanoseconds USE_NSEC "whether to use sub-second file mtimes and ctimes")
+add_feature_info(nanoseconds USE_NSEC "support nanosecond precision file mtimes and ctimes")
diff --git a/cmake/SelectHTTPParser.cmake b/cmake/SelectHTTPParser.cmake
new file mode 100644
index 0000000..a1724a7
--- /dev/null
+++ b/cmake/SelectHTTPParser.cmake
@@ -0,0 +1,19 @@
+# Optional external dependency: http-parser
+if(USE_HTTP_PARSER STREQUAL "system")
+	find_package(HTTP_Parser)
+
+	if(HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
+		list(APPEND LIBGIT2_SYSTEM_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
+		list(APPEND LIBGIT2_LIBS ${HTTP_PARSER_LIBRARIES})
+		list(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
+		add_feature_info(http-parser ON "http-parser support (system)")
+	else()
+		message(FATAL_ERROR "http-parser support was requested but not found")
+	endif()
+else()
+	message(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
+	add_subdirectory("${libgit2_SOURCE_DIR}/deps/http-parser" "${libgit2_BINARY_DIR}/deps/http-parser")
+	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
+	list(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
+	add_feature_info(http-parser ON "http-parser support (bundled)")
+endif()
diff --git a/cmake/SelectHTTPSBackend.cmake b/cmake/SelectHTTPSBackend.cmake
index 4998f0f..4af7474 100644
--- a/cmake/SelectHTTPSBackend.cmake
+++ b/cmake/SelectHTTPSBackend.cmake
@@ -19,7 +19,7 @@ IF(USE_HTTPS)
 				MESSAGE(STATUS "Security framework is too old, falling back to OpenSSL")
 				SET(USE_HTTPS "OpenSSL")
 			ENDIF()
-		ELSEIF (WINHTTP)
+		ELSEIF (USE_WINHTTP)
 			SET(USE_HTTPS "WinHTTP")
 		ELSEIF(OPENSSL_FOUND)
 			SET(USE_HTTPS "OpenSSL")
diff --git a/cmake/SelectRegex.cmake b/cmake/SelectRegex.cmake
new file mode 100644
index 0000000..900d7f1
--- /dev/null
+++ b/cmake/SelectRegex.cmake
@@ -0,0 +1,51 @@
+# Specify regular expression implementation
+find_package(PCRE)
+
+if(REGEX_BACKEND STREQUAL "")
+	check_symbol_exists(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)
+
+	if(HAVE_REGCOMP_L)
+		set(REGEX_BACKEND "regcomp_l")
+	elseif(PCRE_FOUND)
+		set(REGEX_BACKEND "pcre")
+	else()
+		set(REGEX_BACKEND "builtin")
+	endif()
+endif()
+
+if(REGEX_BACKEND STREQUAL "regcomp_l")
+	add_feature_info(regex ON "using system regcomp_l")
+	set(GIT_REGEX_REGCOMP_L 1)
+elseif(REGEX_BACKEND STREQUAL "pcre2")
+	find_package(PCRE2)
+
+	if(NOT PCRE2_FOUND)
+		MESSAGE(FATAL_ERROR "PCRE2 support was requested but not found")
+	endif()
+
+	add_feature_info(regex ON "using system PCRE2")
+	set(GIT_REGEX_PCRE2 1)
+
+	list(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE2_INCLUDE_DIRS})
+	list(APPEND LIBGIT2_LIBS ${PCRE2_LIBRARIES})
+	list(APPEND LIBGIT2_PC_REQUIRES "libpcre2-8")
+elseif(REGEX_BACKEND STREQUAL "pcre")
+	add_feature_info(regex ON "using system PCRE")
+	set(GIT_REGEX_PCRE 1)
+
+	list(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE_INCLUDE_DIRS})
+	list(APPEND LIBGIT2_LIBS ${PCRE_LIBRARIES})
+	list(APPEND LIBGIT2_PC_REQUIRES "libpcre")
+elseif(REGEX_BACKEND STREQUAL "regcomp")
+	add_feature_info(regex ON "using system regcomp")
+	set(GIT_REGEX_REGCOMP 1)
+elseif(REGEX_BACKEND STREQUAL "builtin")
+	add_feature_info(regex ON "using bundled PCRE")
+	set(GIT_REGEX_BUILTIN 1)
+
+	add_subdirectory("${libgit2_SOURCE_DIR}/deps/pcre" "${libgit2_BINARY_DIR}/deps/pcre")
+	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/pcre")
+	list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:pcre>)
+else()
+	message(FATAL_ERROR "The REGEX_BACKEND option provided is not supported")
+endif()
diff --git a/cmake/SelectSSH.cmake b/cmake/SelectSSH.cmake
new file mode 100644
index 0000000..0356ee3
--- /dev/null
+++ b/cmake/SelectSSH.cmake
@@ -0,0 +1,41 @@
+# Optional external dependency: libssh2
+if(USE_SSH)
+	find_pkglibraries(LIBSSH2 libssh2)
+	if(NOT LIBSSH2_FOUND)
+		find_package(LibSSH2)
+		set(LIBSSH2_INCLUDE_DIRS ${LIBSSH2_INCLUDE_DIR})
+		get_filename_component(LIBSSH2_LIBRARY_DIRS "${LIBSSH2_LIBRARY}" DIRECTORY)
+		set(LIBSSH2_LIBRARIES ${LIBSSH2_LIBRARY})
+		set(LIBSSH2_LDFLAGS "-lssh2")
+	endif()
+
+	if(NOT LIBSSH2_FOUND)
+		message(FATAL_ERROR "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
+	endif()
+endif()
+
+if(LIBSSH2_FOUND)
+	set(GIT_SSH 1)
+	list(APPEND LIBGIT2_SYSTEM_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
+	list(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
+	list(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
+
+	check_library_exists("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
+	if(HAVE_LIBSSH2_MEMORY_CREDENTIALS)
+		set(GIT_SSH_MEMORY_CREDENTIALS 1)
+	endif()
+else()
+	message(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
+endif()
+
+if(WIN32 AND EMBED_SSH_PATH)
+	file(GLOB SSH_SRC "${EMBED_SSH_PATH}/src/*.c")
+	list(SORT SSH_SRC)
+	list(APPEND LIBGIT2_OBJECTS ${SSH_SRC})
+
+	list(APPEND LIBGIT2_INCLUDES "${EMBED_SSH_PATH}/include")
+	file(WRITE "${EMBED_SSH_PATH}/src/libssh2_config.h" "#define HAVE_WINCNG\n#define LIBSSH2_WINCNG\n#include \"../win32/libssh2_config.h\"")
+	set(GIT_SSH 1)
+endif()
+
+add_feature_info(SSH GIT_SSH "SSH transport support")
diff --git a/cmake/SelectWinHTTP.cmake b/cmake/SelectWinHTTP.cmake
new file mode 100644
index 0000000..a411004
--- /dev/null
+++ b/cmake/SelectWinHTTP.cmake
@@ -0,0 +1,17 @@
+if(WIN32 AND WINHTTP)
+	set(GIT_WINHTTP 1)
+
+	# Since MinGW does not come with headers or an import library for winhttp,
+	# we have to include a private header and generate our own import library
+	if(MINGW)
+		add_subdirectory("${libgit2_SOURCE_DIR}/deps/winhttp" "${libgit2_BINARY_DIR}/deps/winhttp")
+		list(APPEND LIBGIT2_LIBS winhttp)
+		list(APPEND LIBGIT2_DEPENDENCY_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
+	else()
+		list(APPEND LIBGIT2_LIBS "winhttp")
+		list(APPEND LIBGIT2_PC_LIBS "-lwinhttp")
+	endif()
+
+	list(APPEND LIBGIT2_LIBS "rpcrt4" "crypt32" "ole32")
+	list(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32")
+endif()
diff --git a/cmake/SelectZlib.cmake b/cmake/SelectZlib.cmake
new file mode 100644
index 0000000..2dfc6c8
--- /dev/null
+++ b/cmake/SelectZlib.cmake
@@ -0,0 +1,34 @@
+# Optional external dependency: zlib
+include(SanitizeBool)
+
+SanitizeBool(USE_BUNDLED_ZLIB)
+if(USE_BUNDLED_ZLIB STREQUAL ON)
+	set(USE_BUNDLED_ZLIB "Bundled")
+endif()
+
+if(USE_BUNDLED_ZLIB STREQUAL "OFF")
+	find_package(ZLIB)
+	if(ZLIB_FOUND)
+		list(APPEND LIBGIT2_SYSTEM_INCLUDES ${ZLIB_INCLUDE_DIRS})
+		list(APPEND LIBGIT2_LIBS ${ZLIB_LIBRARIES})
+		if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
+			list(APPEND LIBGIT2_PC_LIBS "-lz")
+		else()
+			list(APPEND LIBGIT2_PC_REQUIRES "zlib")
+		endif()
+		add_feature_info(zlib ON "using system zlib")
+	else()
+		message(STATUS "zlib was not found; using bundled 3rd-party sources." )
+	endif()
+endif()
+if(USE_BUNDLED_ZLIB STREQUAL "Chromium")
+	add_subdirectory("${libgit2_SOURCE_DIR}/deps/chromium-zlib" "${libgit2_BINARY_DIR}/deps/chromium-zlib")
+	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/chromium-zlib")
+	list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:chromium_zlib>)
+	add_feature_info(zlib ON "using (Chromium) bundled zlib")
+elseif(USE_BUNDLED_ZLIB OR NOT ZLIB_FOUND)
+	add_subdirectory("${libgit2_SOURCE_DIR}/deps/zlib" "${libgit2_BINARY_DIR}/deps/zlib")
+	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
+	list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
+	add_feature_info(zlib ON "using bundled zlib")
+endif()
diff --git a/docs/error-handling.md b/docs/error-handling.md
index 05725f2..13ce78f 100644
--- a/docs/error-handling.md
+++ b/docs/error-handling.md
@@ -21,7 +21,7 @@ critical failures (such as a packfile being corrupted, a loose object
 having the wrong access permissions, etc.) all of which will return -1.
 When the object lookup is successful, it will return 0.
 
-If libgit2 was compiled with threads enabled (`-DTHREADSAFE=ON` when using
+If libgit2 was compiled with threads enabled (`-DUSE_THREADS=ON` when using
 CMake), then the error message will be kept in thread-local storage, so it
 will not be modified by other threads.  If threads are not enabled, then
 the error message is in global data.
diff --git a/fuzzers/CMakeLists.txt b/fuzzers/CMakeLists.txt
index 8cc61b7..1479a2a 100644
--- a/fuzzers/CMakeLists.txt
+++ b/fuzzers/CMakeLists.txt
@@ -3,7 +3,10 @@ include_directories(${LIBGIT2_INCLUDES})
 include_directories(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
 
 if(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
+	set(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
 	add_c_flag(-fsanitize=fuzzer)
+	add_c_flag(-fsanitize=fuzzer-no-link)
+	unset(CMAKE_REQUIRED_FLAGS)
 endif()
 
 file(GLOB SRC_FUZZ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *_fuzzer.c)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9812fc0..ae02ef9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,6 +2,10 @@ add_library(git2internal OBJECT)
 set_target_properties(git2internal PROPERTIES C_STANDARD 90)
 
 
+if(DEPRECATE_HARD)
+        add_definitions(-DGIT_DEPRECATE_HARD)
+endif()
+
 if(DEBUG_POOL)
 	set(GIT_DEBUG_POOL 1)
 endif()
@@ -46,22 +50,13 @@ add_feature_info(futimens GIT_USE_FUTIMENS "futimens support")
 
 check_prototype_definition(qsort_r
 	"void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *))"
-	"" "stdlib.h" HAVE_QSORT_R_BSD)
-if(HAVE_QSORT_R_BSD)
-	target_compile_definitions(git2internal PRIVATE HAVE_QSORT_R_BSD)
-endif()
+	"" "stdlib.h" GIT_QSORT_R_BSD)
 
 check_prototype_definition(qsort_r
 	"void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg)"
-	"" "stdlib.h" HAVE_QSORT_R_GNU)
-if(HAVE_QSORT_R_GNU)
-	target_compile_definitions(git2internal PRIVATE HAVE_QSORT_R_GNU)
-endif()
+	"" "stdlib.h" GIT_QSORT_R_GNU)
 
-check_function_exists(qsort_s HAVE_QSORT_S)
-if(HAVE_QSORT_S)
-	target_compile_definitions(git2internal PRIVATE HAVE_QSORT_S)
-endif()
+check_function_exists(qsort_s GIT_QSORT_S)
 
 # Find required dependencies
 
@@ -81,11 +76,11 @@ if(NEED_LIBRT)
 	list(APPEND LIBGIT2_PC_LIBS "-lrt")
 endif()
 
-if(THREADSAFE)
+if(USE_THREADS)
 	list(APPEND LIBGIT2_LIBS ${CMAKE_THREAD_LIBS_INIT})
 	list(APPEND LIBGIT2_PC_LIBS ${CMAKE_THREAD_LIBS_INIT})
 endif()
-add_feature_info(threadsafe THREADSAFE "threadsafe support")
+add_feature_info(threadsafe USE_THREADS "threadsafe support")
 
 
 if(WIN32 AND EMBED_SSH_PATH)
@@ -98,158 +93,15 @@ if(WIN32 AND EMBED_SSH_PATH)
 	set(GIT_SSH 1)
 endif()
 
-if(WIN32 AND WINHTTP)
-	set(GIT_WINHTTP 1)
-
-	# Since MinGW does not come with headers or an import library for winhttp,
-	# we have to include a private header and generate our own import library
-	if(MINGW)
-		add_subdirectory("${libgit2_SOURCE_DIR}/deps/winhttp" "${libgit2_BINARY_DIR}/deps/winhttp")
-		list(APPEND LIBGIT2_LIBS winhttp)
-		list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/winhttp")
-	else()
-		list(APPEND LIBGIT2_LIBS "winhttp")
-		list(APPEND LIBGIT2_PC_LIBS "-lwinhttp")
-	endif()
-
-	list(APPEND LIBGIT2_LIBS "rpcrt4" "crypt32" "ole32")
-	list(APPEND LIBGIT2_PC_LIBS "-lrpcrt4" "-lcrypt32" "-lole32")
-endif()
-
 include(SelectHTTPSBackend)
 include(SelectHashes)
-target_sources(git2internal PRIVATE ${SRC_SHA1})
+include(SelectHTTPParser)
+include(SelectRegex)
+include(SelectSSH)
+include(SelectWinHTTP)
+include(SelectZlib)
 
-# Specify regular expression implementation
-find_package(PCRE)
-
-if(REGEX_BACKEND STREQUAL "")
-	check_symbol_exists(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)
-
-	if(HAVE_REGCOMP_L)
-		set(REGEX_BACKEND "regcomp_l")
-	elseif(PCRE_FOUND)
-		set(REGEX_BACKEND "pcre")
-	else()
-		set(REGEX_BACKEND "builtin")
-	endif()
-endif()
-
-if(REGEX_BACKEND STREQUAL "regcomp_l")
-	add_feature_info(regex ON "using system regcomp_l")
-	set(GIT_REGEX_REGCOMP_L 1)
-elseif(REGEX_BACKEND STREQUAL "pcre2")
-	find_package(PCRE2)
-
-	if(NOT PCRE2_FOUND)
-		MESSAGE(FATAL_ERROR "PCRE2 support was requested but not found")
-	endif()
-
-	add_feature_info(regex ON "using system PCRE2")
-	set(GIT_REGEX_PCRE2 1)
-
-	list(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE2_INCLUDE_DIRS})
-	list(APPEND LIBGIT2_LIBS ${PCRE2_LIBRARIES})
-	list(APPEND LIBGIT2_PC_REQUIRES "libpcre2-8")
-elseif(REGEX_BACKEND STREQUAL "pcre")
-	add_feature_info(regex ON "using system PCRE")
-	set(GIT_REGEX_PCRE 1)
-
-	list(APPEND LIBGIT2_SYSTEM_INCLUDES ${PCRE_INCLUDE_DIRS})
-	list(APPEND LIBGIT2_LIBS ${PCRE_LIBRARIES})
-	list(APPEND LIBGIT2_PC_REQUIRES "libpcre")
-elseif(REGEX_BACKEND STREQUAL "regcomp")
-	add_feature_info(regex ON "using system regcomp")
-	set(GIT_REGEX_REGCOMP 1)
-elseif(REGEX_BACKEND STREQUAL "builtin")
-	add_feature_info(regex ON "using bundled PCRE")
-	set(GIT_REGEX_BUILTIN 1)
-
-	add_subdirectory("${libgit2_SOURCE_DIR}/deps/pcre" "${libgit2_BINARY_DIR}/deps/pcre")
-	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/pcre")
-	list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:pcre>)
-else()
-	message(FATAL_ERROR "The REGEX_BACKEND option provided is not supported")
-endif()
-
-# Optional external dependency: http-parser
-if(USE_HTTP_PARSER STREQUAL "system")
-	find_package(HTTP_Parser)
-
-	if(HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
-		list(APPEND LIBGIT2_SYSTEM_INCLUDES ${HTTP_PARSER_INCLUDE_DIRS})
-		list(APPEND LIBGIT2_LIBS ${HTTP_PARSER_LIBRARIES})
-		list(APPEND LIBGIT2_PC_LIBS "-lhttp_parser")
-		add_feature_info(http-parser ON "http-parser support (system)")
-	else()
-		message(FATAL_ERROR "http-parser support was requested but not found")
-	endif()
-else()
-	message(STATUS "http-parser version 2 was not found or disabled; using bundled 3rd-party sources.")
-	add_subdirectory("${libgit2_SOURCE_DIR}/deps/http-parser" "${libgit2_BINARY_DIR}/deps/http-parser")
-	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser")
-	list(APPEND LIBGIT2_OBJECTS "$<TARGET_OBJECTS:http-parser>")
-	add_feature_info(http-parser ON "http-parser support (bundled)")
-endif()
-
-# Optional external dependency: zlib
-SanitizeBool(USE_BUNDLED_ZLIB)
-if(USE_BUNDLED_ZLIB STREQUAL ON)
-	set(USE_BUNDLED_ZLIB "Bundled")
-endif()
-
-if(USE_BUNDLED_ZLIB STREQUAL "OFF")
-	find_package(ZLIB)
-	if(ZLIB_FOUND)
-		list(APPEND LIBGIT2_SYSTEM_INCLUDES ${ZLIB_INCLUDE_DIRS})
-		list(APPEND LIBGIT2_LIBS ${ZLIB_LIBRARIES})
-		if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
-			list(APPEND LIBGIT2_PC_LIBS "-lz")
-		else()
-			list(APPEND LIBGIT2_PC_REQUIRES "zlib")
-		endif()
-		add_feature_info(zlib ON "using system zlib")
-	else()
-		message(STATUS "zlib was not found; using bundled 3rd-party sources." )
-	endif()
-endif()
-if(USE_BUNDLED_ZLIB STREQUAL "Chromium")
-	add_subdirectory("${libgit2_SOURCE_DIR}/deps/chromium-zlib" "${libgit2_BINARY_DIR}/deps/chromium-zlib")
-	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/chromium-zlib")
-	list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:chromium_zlib>)
-	add_feature_info(zlib ON "using (Chromium) bundled zlib")
-elseif(USE_BUNDLED_ZLIB OR NOT ZLIB_FOUND)
-	add_subdirectory("${libgit2_SOURCE_DIR}/deps/zlib" "${libgit2_BINARY_DIR}/deps/zlib")
-	list(APPEND LIBGIT2_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib")
-	list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:zlib>)
-	add_feature_info(zlib ON "using bundled zlib")
-endif()
-
-# Optional external dependency: libssh2
-if(USE_SSH)
-	find_pkglibraries(LIBSSH2 libssh2)
-	if(NOT LIBSSH2_FOUND)
-		find_package(LibSSH2)
-		set(LIBSSH2_INCLUDE_DIRS ${LIBSSH2_INCLUDE_DIR})
-		get_filename_component(LIBSSH2_LIBRARY_DIRS "${LIBSSH2_LIBRARY}" DIRECTORY)
-		set(LIBSSH2_LIBRARIES ${LIBSSH2_LIBRARY})
-		set(LIBSSH2_LDFLAGS "-lssh2")
-	endif()
-endif()
-if(LIBSSH2_FOUND)
-	set(GIT_SSH 1)
-	list(APPEND LIBGIT2_SYSTEM_INCLUDES ${LIBSSH2_INCLUDE_DIRS})
-	list(APPEND LIBGIT2_LIBS ${LIBSSH2_LIBRARIES})
-	list(APPEND LIBGIT2_PC_LIBS ${LIBSSH2_LDFLAGS})
-
-	check_library_exists("${LIBSSH2_LIBRARIES}" libssh2_userauth_publickey_frommemory "${LIBSSH2_LIBRARY_DIRS}" HAVE_LIBSSH2_MEMORY_CREDENTIALS)
-	if(HAVE_LIBSSH2_MEMORY_CREDENTIALS)
-		set(GIT_SSH_MEMORY_CREDENTIALS 1)
-	endif()
-else()
-	message(STATUS "LIBSSH2 not found. Set CMAKE_PREFIX_PATH if it is installed outside of the default search path.")
-endif()
-add_feature_info(SSH GIT_SSH "SSH transport support")
+target_sources(git2internal PRIVATE ${SRC_SHA1})
 
 # Optional external dependency: ntlmclient
 if(USE_NTLMCLIENT)
@@ -277,7 +129,7 @@ endif()
 add_feature_info(iconv GIT_USE_ICONV "iconv encoding conversion support")
 
 
-if(THREADSAFE)
+if(USE_THREADS)
 	if(NOT WIN32)
 		find_package(Threads REQUIRED)
 	endif()
diff --git a/src/features.h.in b/src/features.h.in
index a40b608..81a8ae0 100644
--- a/src/features.h.in
+++ b/src/features.h.in
@@ -24,6 +24,10 @@
 #cmakedefine GIT_REGEX_PCRE2
 #cmakedefine GIT_REGEX_BUILTIN 1
 
+#cmakedefine GIT_QSORT_R_BSD
+#cmakedefine GIT_QSORT_R_GNU
+#cmakedefine GIT_QSORT_S
+
 #cmakedefine GIT_SSH 1
 #cmakedefine GIT_SSH_MEMORY_CREDENTIALS 1
 
diff --git a/src/thread.h b/src/thread.h
index 4b091c0..82eb7fc 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -12,7 +12,7 @@
 #if defined(__clang__)
 
 # if (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 1))
-#  error Atomic primitives do not exist on this version of clang; configure libgit2 with -DTHREADSAFE=OFF
+#  error Atomic primitives do not exist on this version of clang; configure libgit2 with -DUSE_THREADS=OFF
 # else
 #  define GIT_BUILTIN_ATOMIC
 # endif
@@ -20,7 +20,7 @@
 #elif defined(__GNUC__)
 
 # if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1))
-#  error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DTHREADSAFE=OFF
+#  error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DUSE_THREADS=OFF
 # elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
 #  define GIT_BUILTIN_ATOMIC
 # else
diff --git a/src/util.c b/src/util.c
index 2b1dadf..e06d4ca 100644
--- a/src/util.c
+++ b/src/util.c
@@ -18,7 +18,7 @@
 # endif
 # include <windows.h>
 
-# ifdef HAVE_QSORT_S
+# ifdef GIT_QSORT_S
 #  include <search.h>
 # endif
 #endif
@@ -673,7 +673,7 @@ size_t git__unescape(char *str)
 	return (pos - str);
 }
 
-#if defined(HAVE_QSORT_S) || defined(HAVE_QSORT_R_BSD)
+#if defined(GIT_QSORT_S) || defined(GIT_QSORT_R_BSD)
 typedef struct {
 	git__sort_r_cmp cmp;
 	void *payload;
@@ -688,9 +688,9 @@ static int GIT_LIBGIT2_CALL git__qsort_r_glue_cmp(
 #endif
 
 
-#if !defined(HAVE_QSORT_R_BSD) && \
-	!defined(HAVE_QSORT_R_GNU) && \
-	!defined(HAVE_QSORT_S)
+#if !defined(GIT_QSORT_R_BSD) && \
+	!defined(GIT_QSORT_R_GNU) && \
+	!defined(GIT_QSORT_S)
 static void swap(uint8_t *a, uint8_t *b, size_t elsize)
 {
 	char tmp[256];
@@ -721,12 +721,12 @@ static void insertsort(
 void git__qsort_r(
 	void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload)
 {
-#if defined(HAVE_QSORT_R_BSD)
+#if defined(GIT_QSORT_R_BSD)
 	git__qsort_r_glue glue = { cmp, payload };
 	qsort_r(els, nel, elsize, &glue, git__qsort_r_glue_cmp);
-#elif defined(HAVE_QSORT_R_GNU)
+#elif defined(GIT_QSORT_R_GNU)
 	qsort_r(els, nel, elsize, cmp, payload);
-#elif defined(HAVE_QSORT_S)
+#elif defined(GIT_QSORT_S)
 	git__qsort_r_glue glue = { cmp, payload };
 	qsort_s(els, nel, elsize, git__qsort_r_glue_cmp, &glue);
 #else
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b85cd6e..760925f 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -62,6 +62,8 @@ function(ADD_CLAR_TEST name)
 	endif()
 endfunction(ADD_CLAR_TEST)
 
+enable_testing()
+
 add_clar_test(offline             -v -xonline)
 add_clar_test(invasive            -v -score::ftruncate -sfilter::stream::bigfile -sodb::largefiles -siterator::workdir::filesystem_gunk -srepo::init -srepo::init::at_filesystem_root)
 add_clar_test(online              -v -sonline -xonline::customcert)