forked from boazsegev/facil.io
-
Notifications
You must be signed in to change notification settings - Fork 5
/
makefile
818 lines (683 loc) · 26.6 KB
/
makefile
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
#############################################################################
# This makefile was composed for facil.io
#
# Copyright (c) 2016-2019 Boaz Segev
# License MIT or ISC
#
# This makefile should be easilty portable on
# X-nix systems for different projects.
#
#############################################################################
#############################################################################
# Compliation Output Settings
#############################################################################
# binary name and location
ifndef NAME
NAME=fioapp
endif
# a temporary folder that will be cleared out and deleted between fresh builds
# All object files will be placed in this folder
TMP_ROOT=tmp
# destination folder for the final compiled output
ifndef DEST
DEST=$(TMP_ROOT)
endif
# output folder for `make libdump` - dumps all library files (not source files) in one place.
DUMP_LIB=libdump
# The library details for CMake incorporation. Can be safely removed.
CMAKE_LIBFILE_NAME=CMakeLists.txt
#############################################################################
# Source Code Folder Settings
#############################################################################
# The development, non-library .c file(s) (i.e., the one with `int main(void)`).
MAIN_ROOT=src
# Development subfolders under the main development root
MAIN_SUBFOLDERS=
#############################################################################
# Library Folder Settings
#############################################################################
# the .c and .cpp source files root folder
LIB_ROOT=lib
# publicly used subfolders in the lib root
LIB_PUBLIC_SUBFOLDERS=facil facil/tls facil/fiobj facil/cli facil/http facil/http/parsers facil/redis
# privately used subfolders in the lib root (this distinction is only relevant for CMake)
LIB_PRIVATE_SUBFOLDERS=
#############################################################################
# Compiler / Linker Settings
#############################################################################
# any libraries required (only names, ommit the "-l" at the begining)
LINKER_LIBS=pthread m
# optimization level.
OPTIMIZATION=-O2 -march=native
# Warnings... i.e. -Wpedantic -Weverything -Wno-format-pedantic
WARNINGS= -Wshadow -Wall -Wextra -Wno-missing-field-initializers -Wpedantic
# any extra include folders, space seperated list. (i.e. `pg_config --includedir`)
INCLUDE= ./
# any preprocessosr defined flags we want, space seperated list (i.e. DEBUG )
FLAGS:=
# c compiler
ifndef CC
CC=gcc
endif
# c++ compiler
ifndef CPP
CPP=g++
endif
# c standard
ifndef CSTD
CSTD:=c11
endif
# c++ standard
ifndef CPPSTD
CPPSTD:=gnu++11
endif
PKG_CONFIG ?= pkg-config
# for internal use - don't change
LINKER_LIBS_EXT:=
#############################################################################
# Debug Settings
#############################################################################
# add DEBUG flag if requested
ifdef DEBUG
$(info * Detected DEBUG environment flag, enforcing debug mode compilation)
FLAGS:=$(FLAGS) DEBUG
# # comment the following line if you want to use a different address sanitizer or a profiling tool.
OPTIMIZATION:=-O0 -march=native -fsanitize=address -fno-omit-frame-pointer
# possibly useful: -Wconversion -Wcomma -fsanitize=undefined -Wshadow
# go crazy with clang: -Weverything -Wno-cast-qual -Wno-used-but-marked-unused -Wno-reserved-id-macro -Wno-padded -Wno-disabled-macro-expansion -Wno-documentation-unknown-command -Wno-bad-function-cast -Wno-missing-prototypes
else
FLAGS:=$(FLAGS) NDEBUG NODEBUG
endif
#############################################################################
# facil.io compilation flag helpers
#############################################################################
# add FIO_PRINT_STATE flag if requested
ifdef FIO_PRINT
$(warning FIO_PRINT_STATE is deprecated. FIO_PRINT support will be removed soon.)
FLAGS:=$(FLAGS) FIO_PRINT_STATE=$(FIO_PRINT)
endif
# add FIO_PUBSUB_SUPPORT flag if requested
ifdef FIO_PUBSUB_SUPPORT
FLAGS:=$(FLAGS) FIO_PUBSUB_SUPPORT=$(FIO_PUBSUB_SUPPORT)
endif
#############################################################################
# OS Specific Settings (debugger, disassembler, etc')
#############################################################################
ifneq ($(OS),Windows_NT)
OS := $(shell uname)
else
$(warning *** Windows systems might not work with this makefile / library.)
endif
ifeq ($(OS),Darwin) # Run MacOS commands
# debugger
DB=lldb
# disassemble tool. Use stub to disable.
# DISAMS=otool -dtVGX
# documentation commands
# DOCUMENTATION=cldoc generate $(INCLUDE_STR) -- --output ./html $(foreach dir, $(LIB_PUBLIC_SUBFOLDERS), $(wildcard $(addsuffix /, $(basename $(dir)))*.h*))
$(DEST)/libfacil.so: LDFLAGS += -dynamiclib -install_name $(realpath $(DEST))/libfacil.so
else
# debugger
DB=gdb
# disassemble tool, leave undefined.
# DISAMS=otool -tVX
DOCUMENTATION=
endif
# GCC (at least) >= 7 triggers some bug when -fipa-icf is enabled
# (as in our default: -O2)
ifeq ($(shell $(CC) -v 2>&1 | grep -o "^gcc version"),gcc version)
OPTIMIZATION += -fno-ipa-icf
endif
#############################################################################
# Automatic Setting Expansion
# (don't edit)
#############################################################################
BIN = $(DEST)/$(NAME)
LIBDIR_PUB = $(LIB_ROOT) $(foreach dir, $(LIB_PUBLIC_SUBFOLDERS), $(addsuffix /,$(basename $(LIB_ROOT)))$(dir))
LIBDIR_PRIV = $(foreach dir, $(LIB_PRIVATE_SUBFOLDERS), $(addsuffix /,$(basename $(LIB_ROOT)))$(dir))
LIBDIR = $(LIBDIR_PUB) $(LIBDIR_PRIV)
LIBSRC = $(foreach dir, $(LIBDIR), $(wildcard $(addsuffix /, $(basename $(dir)))*.c*))
MAINDIR = $(MAIN_ROOT) $(foreach main_root, $(MAIN_ROOT) , $(foreach dir, $(MAIN_SUBFOLDERS), $(addsuffix /,$(basename $(main_root)))$(dir)))
MAINSRC = $(foreach dir, $(MAINDIR), $(wildcard $(addsuffix /, $(basename $(dir)))*.c*))
FOLDERS = $(LIBDIR) $(MAINDIR)
SOURCES = $(LIBSRC) $(MAINSRC)
BUILDTREE =$(foreach dir, $(FOLDERS), $(addsuffix /, $(basename $(TMP_ROOT)))$(basename $(dir)))
CCL = $(CC)
INCLUDE_STR = $(foreach dir,$(INCLUDE),$(addprefix -I, $(dir))) $(foreach dir,$(FOLDERS),$(addprefix -I, $(dir)))
MAIN_OBJS = $(foreach source, $(MAINSRC), $(addprefix $(TMP_ROOT)/, $(addsuffix .o, $(basename $(source)))))
LIB_OBJS = $(foreach source, $(LIBSRC), $(addprefix $(TMP_ROOT)/, $(addsuffix .o, $(basename $(source)))))
OBJS_DEPENDENCY:=$(LIB_OBJS:.o=.d) $(MAIN_OBJS:.o=.d)
#############################################################################
# TRY_COMPILE and TRY_COMPILE_AND_RUN functions
#
# Call using $(call TRY_COMPILE, code, compiler_flags)
#
# Returns shell code as string: "0" (success) or non-0 (failure)
#
# TRY_COMPILE_AND_RUN returns the program's shell code as string.
#############################################################################
TRY_COMPILE=$(shell printf $(1) | $(CC) $(INCLUDE_STR) $(LDFLAGS) $(2) -xc -o /dev/null - >> /dev/null 2> /dev/null ; echo $$? 2> /dev/null)
TRY_COMPILE_AND_RUN=$(shell printf $(1) | $(CC) $(2) -xc -o ./___fio_tmp_test_ - 2> /dev/null ; ./___fio_tmp_test_ >> /dev/null 2> /dev/null; echo $$?; rm ./___fio_tmp_test_ 2> /dev/null)
EMPTY:=
#############################################################################
# kqueue / epoll / poll Selection / Detection
# (no need to edit)
#############################################################################
FIO_POLL_TEST_KQUEUE := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <sys/event.h>\\n\
int main(void) {\\n\
int fd = kqueue();\\n\
}\\n\
"
FIO_POLL_TEST_EPOLL := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <stdio.h>\\n\
\#include <sys/types.h>\\n\
\#include <sys/stat.h>\\n\
\#include <fcntl.h>\\n\
\#include <sys/epoll.h>\\n\
int main(void) {\\n\
int fd = epoll_create1(EPOLL_CLOEXEC);\\n\
}\\n\
"
FIO_POLL_TEST_POLL := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <poll.h>\\n\
int main(void) {\\n\
struct pollfd plist[18];\\n\
memset(plist, 0, sizeof(plist[0]) * 18);\\n\
poll(plist, 1, 1);\\n\
}\\n\
"
# Test for manual selection and then TRY_COMPILE with each polling engine
ifdef FIO_POLL
$(info * Skipping polling tests, enforcing manual selection of: poll)
FLAGS:=$(FLAGS) FIO_ENGINE_POLL
else ifdef FIO_FORCE_POLL
$(info * Skipping polling tests, enforcing manual selection of: poll)
FLAGS:=$(FLAGS) FIO_ENGINE_POLL
else ifdef FIO_FORCE_EPOLL
$(info * Skipping polling tests, enforcing manual selection of: epoll)
FLAGS:=$(FLAGS) FIO_ENGINE_EPOLL
else ifdef FIO_FORCE_KQUEUE
$(info * Skipping polling tests, enforcing manual selection of: kqueue)
FLAGS:=$(FLAGS) FIO_ENGINE_KQUEUE
else ifeq ($(call TRY_COMPILE, $(FIO_POLL_TEST_EPOLL), $(EMPTY)), 0)
$(info * Detected `epoll`)
FLAGS:=$(FLAGS) FIO_ENGINE_EPOLL
else ifeq ($(call TRY_COMPILE, $(FIO_POLL_TEST_KQUEUE), $(EMPTY)), 0)
$(info * Detected `kqueue`)
FLAGS:=$(FLAGS) FIO_ENGINE_KQUEUE
else ifeq ($(call TRY_COMPILE, $(FIO_POLL_TEST_POLL), $(EMPTY)), 0)
$(info * Detected `poll` - this is suboptimal fallback!)
FLAGS:=$(FLAGS) FIO_ENGINE_POLL
else
$(warning No supported polling engine! won't be able to compile facil.io)
endif
#############################################################################
# Detecting The `sendfile` System Call
# (no need to edit)
#############################################################################
# Linux variation
FIO_SENDFILE_TEST_LINUX := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <stdio.h>\\n\
\#include <sys/sendfile.h>\\n\
int main(void) {\\n\
off_t offset = 0;\\n\
ssize_t result = sendfile(2, 1, (off_t *)&offset, 300);\\n\
}\\n\
"
# BSD variation
FIO_SENDFILE_TEST_BSD := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <stdio.h>\\n\
\#include <sys/types.h>\\n\
\#include <sys/socket.h>\\n\
\#include <sys/uio.h>\\n\
int main(void) {\\n\
off_t sent = 0;\\n\
off_t offset = 0;\\n\
ssize_t result = sendfile(2, 1, offset, (size_t)sent, NULL, &sent, 0);\\n\
}\\n\
"
# Apple variation
FIO_SENDFILE_TEST_APPLE := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <stdio.h>\\n\
\#include <sys/types.h>\\n\
\#include <sys/socket.h>\\n\
\#include <sys/uio.h>\\n\
int main(void) {\\n\
off_t sent = 0;\\n\
off_t offset = 0;\\n\
ssize_t result = sendfile(2, 1, offset, &sent, NULL, 0);\\n\
}\\n\
"
ifeq ($(call TRY_COMPILE, $(FIO_SENDFILE_TEST_LINUX), $(EMPTY)), 0)
$(info * Detected `sendfile` (Linux))
FLAGS:=$(FLAGS) USE_SENDFILE_LINUX
else ifeq ($(call TRY_COMPILE, $(FIO_SENDFILE_TEST_BSD), $(EMPTY)), 0)
$(info * Detected `sendfile` (BSD))
FLAGS:=$(FLAGS) USE_SENDFILE_BSD
else ifeq ($(call TRY_COMPILE, $(FIO_SENDFILE_TEST_APPLE), $(EMPTY)), 0)
$(info * Detected `sendfile` (Apple))
FLAGS:=$(FLAGS) USE_SENDFILE_APPLE
else
$(info * No `sendfile` support detected.)
FLAGS:=$(FLAGS) USE_SENDFILE=0
endif
#############################################################################
# Detecting 'struct tm' fields
# (no need to edit)
#############################################################################
FIO_TEST_STRUCT_TM_TM_ZONE := "\\n\
\#define _GNU_SOURCE\\n\
\#include <time.h>\\n\
int main(void) {\\n\
struct tm tm;\\n\
tm.tm_zone = \"UTC\";\\n\
return 0;\\n\
}\\n\
"
ifeq ($(call TRY_COMPILE, $(FIO_TEST_STRUCT_TM_TM_ZONE), $(EMPTY)), 0)
$(info * Detected 'tm_zone' field in 'struct tm')
FLAGS:=$(FLAGS) HAVE_TM_TM_ZONE=1
endif
#############################################################################
# Detecting SystemV socket libraries
# (no need to edit)
#############################################################################
FIO_TEST_SOCKET_AND_NETWORK_SERVICE := "\\n\
\#include <sys/types.h>\\n\
\#include <sys/socket.h>\\n\
\#include <netinet/in.h>\\n\
\#include <arpa/inet.h>\\n\
int main(void) {\\n\
struct sockaddr_in addr = { .sin_port = 0 };\\n\
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\\n\
if(fd == -1) return 1;\\n\
if(inet_pton(AF_INET, \"127.0.0.1\", &addr.sin_addr) < 1) return 1;\\n\
return connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0 ? 1 : 0;\\n\
}\\n\
"
ifeq ($(call TRY_COMPILE, $(FIO_TEST_SOCKET_AND_NETWORK_SERVICE), $(EMPTY)), 0)
$(info * Detected socket API without additional libraries)
else ifeq ($(call TRY_COMPILE, $(FIO_TEST_SOCKET_AND_NETWORK_SERVICE), "-lsocket" "-lnsl"), 0)
$(info * Detected socket API from libsocket and libnsl)
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) socket nsl
else
$(error No socket API available)
endif
#############################################################################
# SSL/ TLS Library Detection
# (no need to edit)
#############################################################################
# BearSSL requirement C application code
# (source code variation)
FIO_TLS_TEST_BEARSSL_SOURCE := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <bearssl.h>\\n\
int main(void) {\\n\
}\\n\
"
# BearSSL requirement C application code
# (linked library variation)
FIO_TLS_TEST_BEARSSL_EXT := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <bearssl.h>\\n\
int main(void) {\\n\
}\\n\
"
# OpenSSL requirement C application code
FIO_TLS_TEST_OPENSSL := "\\n\
\#define _GNU_SOURCE\\n\
\#include <stdlib.h>\\n\
\#include <openssl/bio.h> \\n\
\#include <openssl/err.h> \\n\
\#include <openssl/ssl.h> \\n\
\#if OPENSSL_VERSION_NUMBER < 0x10100000L \\n\
\#error \"OpenSSL version too small\" \\n\
\#endif \\n\
int main(void) { \\n\
SSL_library_init(); \\n\
SSL_CTX *ctx = SSL_CTX_new(TLS_method()); \\n\
SSL *ssl = SSL_new(ctx); \\n\
BIO *bio = BIO_new_socket(3, 0); \\n\
BIO_up_ref(bio); \\n\
SSL_set0_rbio(ssl, bio); \\n\
SSL_set0_wbio(ssl, bio); \\n\
}\\n\
"
# automatic library adjustments for possible BearSSL library
LIB_PRIVATE_SUBFOLDERS:=$(LIB_PRIVATE_SUBFOLDERS) $(if $(wildcard lib/bearssl),bearssl)
ifeq ($(shell $(PKG_CONFIG) -- openssl >/dev/null 2>&1; echo $$?), 0)
OPENSSL_CFLAGS = $(shell $(PKG_CONFIG) --cflags openssl)
OPENSSL_LDFLAGS = $(shell $(PKG_CONFIG) --libs openssl)
OPENSSL_LIBS =
endif
OPENSSL_LDFLAGS ?= "-lssl" "-lcrypto"
# add BearSSL/OpenSSL library flags (exclusive)
ifdef FIO_NO_TLS
else ifeq ($(call TRY_COMPILE, $(FIO_TLS_TEST_BEARSSL_SOURCE), $(EMPTY)), 0)
$(info * Detected the BearSSL source code library, setting HAVE_BEARSSL)
# TODO: when BearSSL support arrived, set the FIO_TLS_FOUND flag as well
FLAGS:=$(FLAGS) HAVE_BEARSSL
else ifeq ($(call TRY_COMPILE, $(FIO_TLS_TEST_BEARSSL_EXT), "-lbearssl"), 0)
$(info * Detected the BearSSL library, setting HAVE_BEARSSL)
# TODO: when BearSSL support arrived, set the FIO_TLS_FOUND flag as well
FLAGS:=$(FLAGS) HAVE_BEARSSL
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) bearssl
else ifeq ($(call TRY_COMPILE, $(FIO_TLS_TEST_OPENSSL), $(OPENSSL_CFLAGS) $(OPENSSL_LDFLAGS)), 0)
$(info * Detected the OpenSSL library, setting HAVE_OPENSSL)
FLAGS:=$(FLAGS) HAVE_OPENSSL FIO_TLS_FOUND
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) $(OPENSSL_LIBS)
LDFLAGS += $(OPENSSL_LDFLAGS)
CFLAGS += $(OPENSSL_CFLAGS)
PKGC_REQ_OPENSSL = openssl >= 1.1, openssl < 1.2
PKGC_REQ += $$(PKGC_REQ_OPENSSL)
else
$(info * No compatible SSL/TLS library detected.)
endif
# S2N TLS/SSL library: https://github.com/awslabs/s2n
ifeq ($(call TRY_COMPILE, "\#include <s2n.h>\\n int main(void) {}", "-ls2n") , 0)
$(info * Detected the s2n library, setting HAVE_S2N)
# TODO: when S2N support arrived, set the FIO_TLS_FOUND flag as well
FLAGS:=$(FLAGS) HAVE_S2N
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) s2n
endif
#############################################################################
# ZLib Library Detection
# (no need to edit)
#############################################################################
ifeq ($(call TRY_COMPILE, "\#include <zlib.h>\\nint main(void) {}", "-lz") , 0)
$(info * Detected the zlib library, setting HAVE_ZLIB)
FLAGS:=$(FLAGS) HAVE_ZLIB
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) z
PKGC_REQ_ZLIB = zlib
PKGC_REQ += $$(PKGC_REQ_ZLIB)
endif
#############################################################################
# PostgreSQL Library Detection
# (no need to edit)
#############################################################################
ifeq ($(call TRY_COMPILE, "\#include <libpq-fe.h>\\n int main(void) {}", "-lpg") , 0)
$(info * Detected the PostgreSQL library, setting HAVE_POSTGRESQL)
FLAGS:=$(FLAGS) HAVE_POSTGRESQL
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) pg
else ifeq ($(call TRY_COMPILE, "\#include </usr/include/postgresql/libpq-fe.h>\\nint main(void) {}", "-lpg") , 0)
$(info * Detected the PostgreSQL library, setting HAVE_POSTGRESQL)
FLAGS:=$(FLAGS) HAVE_POSTGRESQL
INCLUDE_STR:=$(INCLUDE_STR) -I/usr/include/postgresql
LINKER_LIBS_EXT:=$(LINKER_LIBS_EXT) pg
endif
#############################################################################
# Endian Detection
# (no need to edit)
#############################################################################
ifeq ($(call TRY_COMPILE_AND_RUN, "int main(void) {int i = 1; return (int)(i & ((unsigned char *)&i)[sizeof(i)-1]);}\n",$(EMPTY)), 1)
$(info * Detected Big Endian byte order.)
FLAGS:=$(FLAGS) __BIG_ENDIAN__
else ifeq ($(call TRY_COMPILE_AND_RUN, "int main(void) {int i = 1; return (int)(i & ((unsigned char *)&i)[0]);}\n",$(EMPTY)), 1)
$(info * Detected Little Endian byte order.)
FLAGS:=$(FLAGS) __BIG_ENDIAN__=0
else
$(info * Byte ordering (endianness) detection failed)
endif
#############################################################################
# Updated flags and final values
# (don't edit)
#############################################################################
FLAGS_STR = $(foreach flag,$(FLAGS),$(addprefix -D, $(flag)))
CFLAGS:= $(CFLAGS) -g -std=$(CSTD) -fpic $(FLAGS_STR) $(WARNINGS) $(OPTIMIZATION) $(INCLUDE_STR)
CPPFLAGS:= $(CPPFLAGS) -std=$(CPPSTD) -fpic $(FLAGS_STR) $(WARNINGS) $(OPTIMIZATION) $(INCLUDE_STR)
LINKER_FLAGS= $(LDFLAGS) $(foreach lib,$(LINKER_LIBS),$(addprefix -l,$(lib))) $(foreach lib,$(LINKER_LIBS_EXT),$(addprefix -l,$(lib)))
CFLAGS_DEPENDENCY=-MT $@ -MMD -MP
# Build a "Requires:" string for the pkgconfig/facil.pc file
# unfortunately, leading or trailing commas are interpreted as
# "empty package name" by pkg-config, therefore we work around this by using
# $(strip ..).
# The following 2 lines are from the manual of GNU make
nullstring :=
space := $(nullstring) # end of line
comma := ,
$(eval PKGC_REQ_EVAL := $(subst $(space),$(comma) ,$(strip $(PKGC_REQ))))
#############################################################################
# Tasks - Building
#############################################################################
$(NAME): build
build: | create_tree build_objects
build_objects: $(LIB_OBJS) $(MAIN_OBJS)
@$(CCL) -o $(BIN) $^ $(OPTIMIZATION) $(LINKER_FLAGS)
@$(DOCUMENTATION)
lib: | create_tree lib_build
$(DEST)/pkgconfig/facil.pc: makefile | libdump
@mkdir -p $(DEST)/pkgconfig && \
printf "\
Name: facil.io\\n\
Description: facil.io\\n\
Cflags: -I%s\\n\
Libs: -L%s -lfacil\\n\
Version: %s\\n\
Requires.private: %s\\n\
" $(realpath $(DEST)/../libdump/include) $(realpath $(DEST)) 0.7.x "$(PKGC_REQ_EVAL)" > $@
$(DEST)/libfacil.so: $(LIB_OBJS) | $(DEST)/pkgconfig/facil.pc
@$(CCL) -shared -o $@ $^ $(OPTIMIZATION) $(LINKER_FLAGS)
lib_build: $(DEST)/libfacil.so
@$(DOCUMENTATION)
%.o : %.c
#### no disassembler (normal / expected state)
ifndef DISAMS
$(TMP_ROOT)/%.o: %.c $(TMP_ROOT)/%.d
@$(CC) -c $< -o $@ $(CFLAGS_DEPENDENCY) $(CFLAGS)
$(TMP_ROOT)/%.o: %.cpp $(TMP_ROOT)/%.d
@$(CC) -c $< -o $@ $(CFLAGS_DEPENDENCY) $(CPPFLAGS)
$(eval CCL = $(CPP))
$(TMP_ROOT)/%.o: %.c++ $(TMP_ROOT)/%.d
@$(CC) -c $< -o $@ $(CFLAGS_DEPENDENCY) $(CPPFLAGS)
$(eval CCL = $(CPP))
#### add diassembling stage (testing / slower)
else
$(TMP_ROOT)/%.o: %.c $(TMP_ROOT)/%.d
@$(CC) -c $< -o $@ $(CFLAGS_DEPENDENCY) $(CFLAGS)
@$(DISAMS) $@ > [email protected]
$(TMP_ROOT)/%.o: %.cpp $(TMP_ROOT)/%.d
@$(CPP) -o $@ -c $< $(CFLAGS_DEPENDENCY) $(CPPFLAGS)
$(eval CCL = $(CPP))
@$(DISAMS) $@ > [email protected]
$(TMP_ROOT)/%.o: %.c++ $(TMP_ROOT)/%.d
@$(CPP) -o $@ -c $< $(CFLAGS_DEPENDENCY) $(CPPFLAGS)
$(eval CCL = $(CPP))
@$(DISAMS) $@ > [email protected]
endif
$(TMP_ROOT)/%.d: ;
-include $(OBJS_DEPENDENCY)
#############################################################################
# Tasks - Testing
#############################################################################
.PHONY : test
test: | clean
@DEBUG=1 $(MAKE) test_build_and_run
-@rm $(BIN) 2> /dev/null
-@rm -R $(TMP_ROOT) 2> /dev/null
.PHONY : test/speed
test/speed: | test_add_speed_flags $(LIB_OBJS)
@$(CC) -c ./tests/speeds.c -o $(TMP_ROOT)/speeds.o $(CFLAGS_DEPENDENCY) $(CFLAGS)
@$(CCL) -o $(BIN) $(LIB_OBJS) $(TMP_ROOT)/speeds.o $(OPTIMIZATION) $(LINKER_FLAGS)
@$(BIN)
.PHONY : test/optimized
test/optimized: | clean test_add_speed_flags create_tree $(LIB_OBJS)
@$(CC) -c ./tests/tests.c -o $(TMP_ROOT)/tests.o $(CFLAGS_DEPENDENCY) $(CFLAGS)
@$(CCL) -o $(BIN) $(LIB_OBJS) $(TMP_ROOT)/tests.o $(OPTIMIZATION) $(LINKER_FLAGS)
@$(BIN)
-@rm $(BIN) 2> /dev/null
-@rm -R $(TMP_ROOT) 2> /dev/null
.PHONY : test/ci
test/ci:| clean
@DEBUG=1 $(MAKE) test_build_and_run
.PHONY : test/c99
test/c99:| clean
@CSTD=c99 DEBUG=1 $(MAKE) test_build_and_run
.PHONY : test/poll
test/poll:| clean
@CSTD=c99 DEBUG=1 FIO_FORCE_POLL=1 $(MAKE) test_build_and_run
.PHONY : test_build_and_run
test_build_and_run: | create_tree test_add_flags test/build
@$(BIN)
.PHONY : test_add_flags
test_add_flags:
$(eval CFLAGS:=-coverage $(CFLAGS) -DDEBUG=1 -Werror)
$(eval LINKER_FLAGS:=-coverage -DDEBUG=1 $(LINKER_FLAGS))
.PHONY : test_add_speed_flags
test_add_speed_flags:
$(eval CFLAGS:=$(CFLAGS) -DDEBUG=1)
$(eval LINKER_FLAGS:=-DDEBUG=1 $(LINKER_FLAGS))
.PHONY : test/build
test/build: $(LIB_OBJS)
@$(CC) -c ./tests/tests.c -o $(TMP_ROOT)/tests.o $(CFLAGS_DEPENDENCY) $(CFLAGS)
@$(CCL) -o $(BIN) $(LIB_OBJS) $(TMP_ROOT)/tests.o $(OPTIMIZATION) $(LINKER_FLAGS)
.PHONY : clean
clean:
-@rm -f $(BIN) 2> /dev/null || echo "" >> /dev/null
-@rm -R -f $(TMP_ROOT) 2> /dev/null || echo "" >> /dev/null
-@mkdir -p $(BUILDTREE)
.PHONY : run
run: | build
@$(BIN)
.PHONY : db
db: | clean
DEBUG=1 $(MAKE) build
$(DB) $(BIN)
.PHONY : create_tree
create_tree:
-@mkdir -p $(BUILDTREE) 2> /dev/null
#############################################################################
# Tasks - Installers
#############################################################################
.PHONY : install/bearssl
install/bearssl: | remove/bearssl add/bearssl ;
.PHONY : add/bearssl
add/bearssl: | remove/bearssl
-@echo " "
-@echo "* Cloning BearSSL and copying source files to lib/bearssl."
-@echo " Please review the BearSSL license."
@git clone https://www.bearssl.org/git/BearSSL tmp/bearssl
@mkdir lib/bearssl
-@find tmp/bearssl/src -name "*.*" -exec mv "{}" lib/bearssl \;
-@find tmp/bearssl/inc -name "*.*" -exec mv "{}" lib/bearssl \;
-@make clean
.PHONY : remove/bearssl
remove/bearssl:
-@echo "* Removing existing BearSSL source files."
-@rm -R -f lib/bearssl 2> /dev/null || echo "" >> /dev/null
-@make clean
#############################################################################
# Tasks - library code dumping & CMake
#############################################################################
ifndef DUMP_LIB
.PHONY : libdump
libdump: cmake
else
ifeq ($(LIBDIR_PRIV),)
.PHONY : libdump
libdump: cmake
-@rm -R $(DUMP_LIB) 2> /dev/null
-@mkdir $(DUMP_LIB)
-@mkdir $(DUMP_LIB)/src
-@mkdir $(DUMP_LIB)/include
-@mkdir $(DUMP_LIB)/all # except README.md files
-@cp -n $(foreach dir,$(LIBDIR_PUB), $(wildcard $(addsuffix /, $(basename $(dir)))*.[^m]*)) $(DUMP_LIB)/all 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PUB), $(wildcard $(addsuffix /, $(basename $(dir)))*.h*)) $(DUMP_LIB)/include 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PUB), $(wildcard $(addsuffix /, $(basename $(dir)))*.[^hm]*)) $(DUMP_LIB)/src 2> /dev/null
else
.PHONY : libdump
libdump: cmake
-@rm -R $(DUMP_LIB) 2> /dev/null
-@mkdir $(DUMP_LIB)
-@mkdir $(DUMP_LIB)/src
-@mkdir $(DUMP_LIB)/include
-@mkdir $(DUMP_LIB)/all # except README.md files
-@cp -n $(foreach dir,$(LIBDIR_PUB), $(wildcard $(addsuffix /, $(basename $(dir)))*.[^m]*)) $(DUMP_LIB)/all 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PUB), $(wildcard $(addsuffix /, $(basename $(dir)))*.h*)) $(DUMP_LIB)/include 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PUB), $(wildcard $(addsuffix /, $(basename $(dir)))*.[^hm]*)) $(DUMP_LIB)/src 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PRIV), $(wildcard $(addsuffix /, $(basename $(dir)))*.[^m]*)) $(DUMP_LIB)/all 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PRIV), $(wildcard $(addsuffix /, $(basename $(dir)))*.h*)) $(DUMP_LIB)/include 2> /dev/null
-@cp -n $(foreach dir,$(LIBDIR_PRIV), $(wildcard $(addsuffix /, $(basename $(dir)))*.[^hm]*)) $(DUMP_LIB)/src 2> /dev/null
endif
endif
ifndef CMAKE_LIBFILE_NAME
.PHONY : cmake
cmake:
else
.PHONY : cmake
cmake:
-@rm $(CMAKE_LIBFILE_NAME) 2> /dev/null
@touch $(CMAKE_LIBFILE_NAME)
@echo 'project(facil.io C)' >> $(CMAKE_LIBFILE_NAME)
@echo 'cmake_minimum_required(VERSION 2.4)' >> $(CMAKE_LIBFILE_NAME)
@echo '' >> $(CMAKE_LIBFILE_NAME)
@echo 'find_package(Threads REQUIRED)' >> $(CMAKE_LIBFILE_NAME)
@echo '' >> $(CMAKE_LIBFILE_NAME)
@echo 'set(facil.io_SOURCES' >> $(CMAKE_LIBFILE_NAME)
@$(foreach src,$(LIBSRC),echo ' $(src)' >> $(CMAKE_LIBFILE_NAME);)
@echo ')' >> $(CMAKE_LIBFILE_NAME)
@echo '' >> $(CMAKE_LIBFILE_NAME)
@echo 'add_library(facil.io $${facil.io_SOURCES})' >> $(CMAKE_LIBFILE_NAME)
@echo 'target_link_libraries(facil.io' >> $(CMAKE_LIBFILE_NAME)
@echo ' PRIVATE Threads::Threads' >> $(CMAKE_LIBFILE_NAME)
@$(foreach src,$(LINKER_LIBS),echo ' PUBLIC $(src)' >> $(CMAKE_LIBFILE_NAME);)
@echo ' )' >> $(CMAKE_LIBFILE_NAME)
@echo 'target_include_directories(facil.io' >> $(CMAKE_LIBFILE_NAME)
@$(foreach src,$(LIBDIR_PUB),echo ' PUBLIC $(src)' >> $(CMAKE_LIBFILE_NAME);)
@$(foreach src,$(LIBDIR_PRIV),echo ' PRIVATE $(src)' >> $(CMAKE_LIBFILE_NAME);)
@echo ')' >> $(CMAKE_LIBFILE_NAME)
@echo '' >> $(CMAKE_LIBFILE_NAME)
endif
#############################################################################
# Tasks - make variable printout (test)
#############################################################################
# Prints the make variables, used for debugging the makefile
.PHONY : vars
vars:
@echo "CC: $(CC)"
@echo ""
@echo "BIN: $(BIN)"
@echo ""
@echo "LIBDIR_PUB: $(LIBDIR_PUB)"
@echo ""
@echo "LIBDIR_PRIV: $(LIBDIR_PRIV)"
@echo ""
@echo "MAINDIR: $(MAINDIR)"
@echo ""
@echo "FOLDERS: $(FOLDERS)"
@echo ""
@echo "BUILDTREE: $(BUILDTREE)"
@echo ""
@echo "LIBSRC: $(LIBSRC)"
@echo ""
@echo "MAINSRC: $(MAINSRC)"
@echo ""
@echo "LIB_OBJS: $(LIB_OBJS)"
@echo ""
@echo "MAIN_OBJS: $(MAIN_OBJS)"
@echo ""
@echo "OBJS_DEPENDENCY: $(OBJS_DEPENDENCY)"
@echo ""
@echo "CFLAGS: $(CFLAGS)"
@echo ""
@echo "CPPFLAGS: $(CPPFLAGS)"
@echo ""
@echo "LINKER_LIBS: $(LINKER_LIBS)"
@echo ""
@echo "LINKER_LIBS_EXT: $(LINKER_LIBS_EXT)"
@echo ""
@echo "LINKER_FLAGS: $(LINKER_FLAGS)"