-
Notifications
You must be signed in to change notification settings - Fork 12
/
Makefile
230 lines (196 loc) · 6.24 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
# MIT License
#
# Copyright (c) 2021 Daniel Robertson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
CXX := g++
AR := ar
INCDIR := include
SRCDIR := src
BUILDDIR := build
BINDIR := bin
SRCEXT := cpp
LIBS := -llgpio -pthread
INC := -I $(INCDIR)
CFLAGS := -O2 \
-fomit-frame-pointer \
-pipe \
-Wall \
-Wextra \
-fstack-clash-protection \
-Wfatal-errors \
-Werror=format-security \
-Wl,-z,relro \
-Wl,-z,now \
-Wl,-z,defs \
-Wl,--hash-style=gnu \
-Wl,--as-needed \
-D_FORTIFY_SOURCE=2 \
-DNDEBUG=1
########################################################################
# https://stackoverflow.com/a/39895302/570787
ifeq ($(PREFIX),)
PREFIX := /usr/local
endif
ifeq ($(OS),Windows_NT)
IS_WIN := 1
endif
ifeq ($(GITHUB_ACTIONS),true)
IS_GHA := 1
endif
ifneq ($(IS_WIN),1)
IS_PI := $(shell test -f /proc/device-tree/model && grep -qi "raspberry pi" /proc/device-tree/model)
endif
ifeq ($(VIRTUAL_PI),1)
IS_PI := 1
endif
########################################################################
ifeq ($(IS_WIN),1)
# overwrite binaries for dev
# NOTE: toolchain available here: https://gnutoolchains.com/raspberry/
# set RPI_TOOLCHAIN environment variable to directory of toolchain
CXX = $(RPI_TOOLCHAIN)/bin/arm-linux-gnueabihf-g++.exe
AR = $(RPI_TOOLCHAIN)/bin/arm-linux-gnueabihf-ar.exe
endif
ifeq ($(IS_PI),1)
# only include these flags on rpi
CFLAGS := -march=native \
-mfpu=vfp \
-mfloat-abi=hard \
$(CFLAGS)
endif
ifeq ($(IS_GHA),1)
# gha needs these additional libs
LIBS := $(LIBS) -lrt -lcrypt
endif
CXXFLAGS := -std=c++11 \
-fexceptions \
$(CFLAGS)
########################################################################
.PHONY: all
all: dirs \
build \
execs
.PHONY: build
build: $(BUILDDIR)/static/libhx711.a $(BUILDDIR)/shared/libhx711.so
.PHONY execs:
execs: hx711calibration test
.PHONY: clean
clean:
ifeq ($(IS_WIN),1)
del /S /Q $(BUILDDIR)\*
del /S /Q $(BINDIR)\*
else
rm -r $(BUILDDIR)/*
rm -r $(BINDIR)/*
endif
.PHONY: dirs
dirs:
ifeq ($(IS_WIN),1)
if not exist $(BINDIR) mkdir $(BINDIR)
if not exist $(BUILDDIR) mkdir $(BUILDDIR)
if not exist $(BUILDDIR)\static mkdir $(BUILDDIR)\static
if not exist $(BUILDDIR)\shared mkdir $(BUILDDIR)\shared
else
mkdir -p $(BINDIR)
mkdir -p $(BUILDDIR)/static
mkdir -p $(BUILDDIR)/shared
endif
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
$(CXX) $(CXXFLAGS) $(INC) -c -o $@ $<
$(BUILDDIR)/static/%.o: $(SRCDIR)/%.$(SRCEXT)
$(CXX) $(CXXFLAGS) $(INC) -c -o $@ $<
$(BUILDDIR)/shared/%.o: $(SRCDIR)/%.$(SRCEXT)
$(CXX) $(CXXFLAGS) -fPIC $(INC) -c -o $@ $<
# Build static library
$(BUILDDIR)/static/libhx711.a: $(BUILDDIR)/static/AbstractScale.o \
$(BUILDDIR)/static/AdvancedHX711.o \
$(BUILDDIR)/static/HX711.o \
$(BUILDDIR)/static/Mass.o \
$(BUILDDIR)/static/SimpleHX711.o \
$(BUILDDIR)/static/Utility.o \
$(BUILDDIR)/static/Value.o \
$(BUILDDIR)/static/ValueStack.o \
$(BUILDDIR)/static/Watcher.o
$(AR) rcs $(BUILDDIR)/static/libhx711.a \
$(BUILDDIR)/static/AbstractScale.o \
$(BUILDDIR)/static/AdvancedHX711.o \
$(BUILDDIR)/static/HX711.o \
$(BUILDDIR)/static/Mass.o \
$(BUILDDIR)/static/SimpleHX711.o \
$(BUILDDIR)/static/Utility.o \
$(BUILDDIR)/static/Value.o \
$(BUILDDIR)/static/ValueStack.o \
$(BUILDDIR)/static/Watcher.o
# Build shared library
$(BUILDDIR)/shared/libhx711.so: $(BUILDDIR)/shared/AbstractScale.o \
$(BUILDDIR)/shared/AdvancedHX711.o \
$(BUILDDIR)/shared/HX711.o \
$(BUILDDIR)/shared/Mass.o \
$(BUILDDIR)/shared/SimpleHX711.o \
$(BUILDDIR)/shared/Utility.o \
$(BUILDDIR)/shared/Value.o \
$(BUILDDIR)/shared/ValueStack.o \
$(BUILDDIR)/shared/Watcher.o
$(CXX) -shared \
$(CXXFLAGS) \
$(INC) \
-o $(BUILDDIR)/shared/libhx711.so \
$(BUILDDIR)/shared/AbstractScale.o \
$(BUILDDIR)/shared/AdvancedHX711.o \
$(BUILDDIR)/shared/HX711.o \
$(BUILDDIR)/shared/Mass.o \
$(BUILDDIR)/shared/SimpleHX711.o \
$(BUILDDIR)/shared/Utility.o \
$(BUILDDIR)/shared/Value.o \
$(BUILDDIR)/shared/ValueStack.o \
$(BUILDDIR)/shared/Watcher.o \
$(LIBS)
.PHONY: hx711calibration
hx711calibration: $(BUILDDIR)/Calibration.o
$(CXX) $(CXXFLAGS) $(INC) \
-o $(BINDIR)/hx711calibration \
$(BUILDDIR)/Calibration.o \
-L $(BUILDDIR)/static \
-lhx711 $(LIBS)
.PHONY: test
test: $(BUILDDIR)/SimpleHX711Test.o $(BUILDDIR)/AdvancedHX711Test.o
$(CXX) $(CXXFLAGS) $(INC) \
-o $(BINDIR)/simplehx711test \
$(BUILDDIR)/SimpleHX711Test.o \
-L $(BUILDDIR)/static \
-lhx711 $(LIBS)
$(CXX) $(CXXFLAGS) $(INC) \
-o $(BINDIR)/advancedhx711test \
$(BUILDDIR)/AdvancedHX711Test.o \
-L $(BUILDDIR)/static \
-lhx711 $(LIBS)
.PHONY: install
install: $(BUILDDIR)/static/libhx711.a $(BUILDDIR)/shared/libhx711.so
install -d $(DESTDIR)$(PREFIX)/lib/
install -m 644 $(BUILDDIR)/static/libhx711.a $(DESTDIR)$(PREFIX)/lib/
install -m 644 $(BUILDDIR)/shared/libhx711.so $(DESTDIR)$(PREFIX)/lib/
install -d $(DESTDIR)$(PREFIX)/include/hx711
install -m 644 $(INCDIR)/*.h $(DESTDIR)$(PREFIX)/include/hx711
$(info install: you may need to run ldconfig)
.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(PREFIX)/lib/libhx711.*
rm -rf $(DESTDIR)$(PREFIX)/include/hx711
$(info uninstall: you may need to run ldconfig)