Skip to content

Commit 6fd26ff

Browse files
author
Christopher Crouzet
committed
Bring on the initial commit
0 parents  commit 6fd26ff

File tree

13 files changed

+2185
-0
lines changed

13 files changed

+2185
-0
lines changed

.clang-format

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
# Minimum version required for clang-format: 5.0
3+
Language: Cpp
4+
AccessModifierOffset: -4
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: false
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlines: Right
9+
AlignOperands: true
10+
AlignTrailingComments: false
11+
AllowAllParametersOfDeclarationOnNextLine: false
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: None
15+
AllowShortIfStatementsOnASingleLine: false
16+
AllowShortLoopsOnASingleLine: false
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: All
19+
AlwaysBreakBeforeMultilineStrings: false
20+
AlwaysBreakTemplateDeclarations: true
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
BraceWrapping:
24+
AfterClass: false
25+
AfterControlStatement: false
26+
AfterEnum: false
27+
AfterFunction: true
28+
AfterNamespace: true
29+
AfterObjCDeclaration: false
30+
AfterStruct: false
31+
AfterUnion: false
32+
BeforeCatch: true
33+
BeforeElse: false
34+
IndentBraces: false
35+
BreakAfterJavaFieldAnnotations: false
36+
BreakBeforeBinaryOperators: All
37+
BreakBeforeBraces: Custom
38+
BreakBeforeInheritanceComma: true
39+
BreakBeforeTernaryOperators: true
40+
BreakConstructorInitializers: BeforeComma
41+
BreakStringLiterals: true
42+
ColumnLimit: 80
43+
CommentPragmas: '^ IWYU pragma:'
44+
CompactNamespaces: false
45+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
46+
ConstructorInitializerIndentWidth: 4
47+
ContinuationIndentWidth: 4
48+
Cpp11BracedListStyle: true
49+
DerivePointerAlignment: false
50+
DisableFormat: false
51+
FixNamespaceComments: true
52+
ForEachMacros: []
53+
IncludeCategories:
54+
- Regex: '.*'
55+
Priority: 1
56+
IncludeIsMainRegex: '$'
57+
IndentCaseLabels: true
58+
IndentWidth: 4
59+
IndentWrappedFunctionNames: false
60+
JavaScriptQuotes: Leave
61+
JavaScriptWrapImports: true
62+
KeepEmptyLinesAtTheStartOfBlocks: false
63+
MacroBlockBegin: ''
64+
MacroBlockEnd: ''
65+
MaxEmptyLinesToKeep: 1
66+
NamespaceIndentation: None
67+
ObjCBlockIndentWidth: 4
68+
ObjCSpaceAfterProperty: true
69+
ObjCSpaceBeforeProtocolList: true
70+
PenaltyBreakAssignment: 10
71+
PenaltyBreakBeforeFirstCallParameter: 1
72+
PenaltyBreakComment: 10
73+
PenaltyBreakFirstLessLess: 10
74+
PenaltyBreakString: 20
75+
PenaltyExcessCharacter: 1000
76+
PenaltyReturnTypeOnItsOwnLine: 0
77+
PointerAlignment: Right
78+
ReflowComments: true
79+
SortIncludes: false
80+
SortUsingDeclarations: true
81+
SpaceAfterCStyleCast: false
82+
SpaceAfterTemplateKeyword: false
83+
SpaceBeforeAssignmentOperators: true
84+
SpaceBeforeParens: ControlStatements
85+
SpaceInEmptyParentheses: false
86+
SpacesBeforeTrailingComments: 1
87+
SpacesInAngles: false
88+
SpacesInCStyleCastParentheses: false
89+
SpacesInContainerLiterals: true
90+
SpacesInParentheses: false
91+
SpacesInSquareBrackets: false
92+
Standard: Cpp11
93+
TabWidth: 4
94+
UseTab: Never
95+
...

.clang-tidy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Minimum version required for clang-tidy: 5.0
3+
Checks: '*,-clang-analyzer-valist.Uninitialized,-llvm-header-guard,-llvm-include-order'
4+
WarningsAsErrors: ''
5+
HeaderFilterRegex: ''
6+
AnalyzeTemporaryDtors: false
7+
FormatStyle: file
8+
...

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Christopher Crouzet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
ifndef outdir
2+
OUTDIR := build
3+
else
4+
OUTDIR := $(outdir)
5+
endif
6+
7+
# ------------------------------------------------------------------------------
8+
9+
PROJECT := zero
10+
11+
OBJECTDIR := $(OUTDIR)/obj
12+
BINARYDIR := $(OUTDIR)/bin
13+
14+
CFLAGS := -std=c99
15+
CPPFLAGS := -DNDEBUG -O3 \
16+
-Wpedantic -Wall -Wextra -Waggregate-return -Wcast-align \
17+
-Wcast-qual -Wconversion -Wfloat-equal -Wpointer-arith -Wshadow \
18+
-Wstrict-overflow=5 -Wswitch -Wswitch-default -Wundef \
19+
-Wunreachable-code -Wwrite-strings
20+
PREREQFLAGS := -MMD -MP
21+
LDFLAGS :=
22+
LDLIBS :=
23+
24+
COMPILE.c = $(CC) $(PREREQFLAGS) $(CPPFLAGS) $(CFLAGS) $(TARGET_ARCH) -c
25+
LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)
26+
27+
# ------------------------------------------------------------------------------
28+
29+
ALL_SOURCES :=
30+
ALL_HEADERS :=
31+
32+
# Create build rules for object targets.
33+
# $(1): source path, e.g.: src, tools/build.
34+
# $(2): target path, e.g.: zero, tools/build.
35+
# $(3): prefix for variable names.
36+
define CREATE_OBJECT_RULES =
37+
$(3)_SOURCES := $$(wildcard $(1)/*.c)
38+
$(3)_HEADERS := $$(wildcard $(1)/*.h)
39+
$(3)_OBJECTS := $$($(3)_SOURCES:$(1)/%.c=$$(OBJECTDIR)/$(2)/%.o)
40+
$(3)_PREREQS := $$($(3)_OBJECTS:.o=.d)
41+
42+
$$($(3)_OBJECTS): $$(OBJECTDIR)/$(2)/%.o: $(1)/%.c
43+
@ mkdir -p $$(@D)
44+
@ $$(COMPILE.c) -o $$@ $$<
45+
46+
-include $$($(3)_PREREQS)
47+
48+
ALL_SOURCES += $$($(3)_SOURCES)
49+
ALL_HEADERS += $$($(3)_HEADERS)
50+
endef
51+
52+
# Create build rules for binary targets.
53+
# $(1): source path, e.g.: src, tools/build.
54+
# $(2): target path, e.g.: zero, tools/build.
55+
# $(3): prefix for variable names.
56+
define CREATE_BINARY_RULES =
57+
$(3)_SOURCES := $$(wildcard $(1)/*.c)
58+
$(3)_OBJECTS := $$($(3)_SOURCES:$(1)/%.c=$$(OBJECTDIR)/$(2)/%.o)
59+
$(3)_TARGET := $$(BINARYDIR)/$(2)
60+
61+
$$(eval $$(call \
62+
CREATE_OBJECT_RULES,$(1),$(2),$(3)))
63+
64+
$$($(3)_TARGET): $$($(3)_OBJECTS)
65+
@ mkdir -p $$(@D)
66+
@ $$(LINK.o) $$^ $$(LDLIBS) -o $$@
67+
endef
68+
69+
# ------------------------------------------------------------------------------
70+
71+
TOOLS_TARGETS :=
72+
TOOLS_PHONYTARGETS :=
73+
74+
# Create the rules to build a tool target.
75+
# $(1): target name.
76+
# $(2): path.
77+
# $(3): prefix for variable names.
78+
define CREATE_TOOL_RULES =
79+
$$(eval $$(call \
80+
CREATE_BINARY_RULES,$(2),$(2),$(3)_$(1)))
81+
82+
$(3)-$(1): $$($(3)_$(1)_TARGET)
83+
84+
.PHONY: $(3)-$(1)
85+
86+
TOOLS_TARGETS += $$($(3)_$(1)_TARGET)
87+
TOOLS_PHONYTARGETS += $(3)-$(1)
88+
endef
89+
90+
# Create the rules for all the tool targets.
91+
# $(1): path.
92+
# $(2): prefix for variable names.
93+
define CREATE_TOOLS_RULES =
94+
TOOLS := $$(notdir $$(wildcard $(1)/*))
95+
$$(foreach _i,$$(TOOLS),$$(eval $$(call \
96+
CREATE_TOOL_RULES,$$(_i),$(1)/$$(_i),$(2))))
97+
98+
tools: $$(TOOLS_PHONYTARGETS)
99+
100+
.PHONY: tools
101+
endef
102+
103+
$(eval $(call \
104+
CREATE_TOOLS_RULES,tools,tool))
105+
106+
# ------------------------------------------------------------------------------
107+
108+
TEMPLATES := $(wildcard src/*.tpl)
109+
INCLUDES := $(TEMPLATES:src/%.h.tpl=include/$(PROJECT)/%.h)
110+
111+
$(INCLUDES): include/$(PROJECT)/%.h: src/%.h.tpl
112+
@ mkdir -p $(@D)
113+
@ $(tool_build_TARGET) $< $@
114+
@ clang-format -i -style=file $@
115+
116+
$(INCLUDES): tool-build
117+
118+
includes: $(INCLUDES)
119+
120+
.PHONY: includes
121+
122+
# ------------------------------------------------------------------------------
123+
124+
ALL_HEADERS += $(TEMPLATES) $(wildcard src/partials/*.h)
125+
126+
CLANGVERSION := $(shell clang --version \
127+
| grep version \
128+
| sed 's/^.*version \([0-9]*\.[0-9]*\.[0-9]*\).*$$/\1/')
129+
CLANGDIR := $(shell dirname $(shell which clang))
130+
CLANGINCLUDE := $(CLANGDIR)/../lib/clang/$(CLANGVERSION)/include
131+
132+
format:
133+
@ clang-format -i -style=file $(ALL_SOURCES) $(ALL_HEADERS)
134+
135+
tidy:
136+
clang-tidy -fix $(INCLUDES) -- $(CPPFLAGS) $(CFLAGS) -I$(CLANGINCLUDE)
137+
138+
.PHONY: format tidy
139+
140+
# ------------------------------------------------------------------------------
141+
142+
clean:
143+
@- rm -rf $(OUTDIR)
144+
145+
.PHONY: clean
146+
147+
# ------------------------------------------------------------------------------
148+
149+
all: includes;
150+
151+
.PHONY: all
152+
153+
.DEFAULT_GOAL := all

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Zero
2+
====
3+
4+
Zero is a bunch of single-files libraries for C/C++.
5+
6+
It is written in C89 at the exception of a couple of features borrowed from C99,
7+
namely fixed-width integer types and variadic macros.
8+
9+
10+
## Features
11+
12+
* mostly C89-compliant
13+
* headers don't include anything by default <sup>[1]</sup>
14+
* implementations are included upon defining the `ZR_IMPLEMENTATION` macro
15+
* each library is a standalone single file to ease integration into projects
16+
* simple
17+
18+
1. The only exception being `<stdarg.h>` for headers defining functions with a
19+
`va_list` object as parameter.
20+
21+
22+
## Libraries
23+
24+
25+
| library | description | latest version |
26+
|---------|-------------|----------------|
27+
**[allocator.h](include/zero/allocator.h)** | Aligned and non-aligned wrappers of malloc/realloc/free | 0.1.0
28+
**[logger.h](include/zero/logger.h)** | Simple logger with different log levels and colouring | 0.1.0
29+
30+
31+
## FAQ
32+
33+
### Why defining custom fixed-width integer types and even `size_t`?
34+
35+
Because most projects target common platforms (Windows, Linux, macOS), thus
36+
using either the ILP32, LP64, or LLP64 data models, which all guarantee the
37+
`char` type to be 8 bits, `short` to be 16 bits, `int` to be 32 bits, and
38+
`long long` to be 64 bits. If such types can be accurately defined in a few
39+
lines for most of the projects, then why including a standard header that
40+
resolves to _thousands_ of lines of code with its dependencies? For the exotic
41+
platforms, the macro `ZR_USE_STD_FIXED_TYPES` can be defined, or each type can
42+
be overrided individually.
43+
44+
The same applies to redefining `size_t`—on almost all platforms the size of
45+
`size_t` equals the targetted architecture, that is either 32 or 64 bits. Here
46+
again, if that's not enough then it's still possible to define
47+
the macro `ZR_USE_STD_BASIC_TYPES`.
48+
49+
Note that these custom types are only used for the public interface defined in
50+
the headers, to avoid cluttering project headers including them. But the
51+
implementation sections make free use of standard headers as needed
52+
(including standard fixed-width integer types and `size_t`).

0 commit comments

Comments
 (0)