-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile.md
288 lines (240 loc) · 7.6 KB
/
makefile.md
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
# Makefile
## 使用方法
1、编写 Makefile(参考 <https://seisman.github.io/how-to-write-makefile>)
```makefile
CXX := g++
CXXFLAGS := -c -Wall
LDFLAGS :=
TARGET := hello
SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp, %.o, $(SRCS))
# 声明 `all` 为伪目标,防止与系统中的同名目标冲突
.PHONY: all
all: $(TARGET)
# 目标规则: 生成可执行文件
$(TARGET): $(OBJS)
$(CXX) -o $@ $^ $(LDFLAGS)
# 编译规则: 生成目标文件
$(OBJS): %.o: %.cpp
$(CXX) -o $@ -c $< $(CXXFLAGS)
.PHONY: clean
clean:
rm -f *.o $(TARGET)
```
2、配置编译选项
```bash
cat <<\EOF | tee sdk.mk
CURR_DIR := $(shell pwd)
BUILDROOT_DIR := $(CURR_DIR)/../buildroot-2023.02.9/output/host
SYSROOT_DIR := $(BUILDROOT_DIR)/arm-buildroot-linux-gnueabihf/sysroot
TOOLCHAIN_DIR := $(BUILDROOT_DIR)/bin
export PATH := $(TOOLCHAIN_DIR):$(PATH)
# cross compile options
export ARCH := arm
export CROSS_COMPILE := arm-buildroot-linux-gnueabihf-
export CC := $(CROSS_COMPILE)gcc
# export CXX := $(CROSS_COMPILE)g++
export AS := $(CROSS_COMPILE)as
export LD := $(CROSS_COMPILE)ld
export STRIP := $(CROSS_COMPILE)strip
export RANLIB := $(CROSS_COMPILE)ranlib
export OBJCOPY := $(CROSS_COMPILE)objcopy
export OBJDUMP := $(CROSS_COMPILE)objdump
export AR := $(CROSS_COMPILE)ar
export NM := $(CROSS_COMPILE)nm
export CFLAGS := --sysroot=$(SYSROOT_DIR) -I$(SYSROOT_DIR)/usr/include -g -Wall
# export CXXFLAGS := --sysroot=$(SYSROOT_DIR) -I$(SYSROOT_DIR)/usr/include -g -Wall
export LDFLAGS := -L$(SYSROOT_DIR)/lib -L$(SYSROOT_DIR)/usr/lib
export PKG_CONFIG_DIR := "$(SYSROOT_DIR)/usr/lib/pkgconfig"
export PKG_CONFIG_PATH := "$(PKG_CONFIG_DIR):$(SYSROOT_DIR)/usr/share/pkgconfig"
export PKG_CONFIG_LIBDIR := "$(PKG_CONFIG_DIR)"
export PKG_CONFIG_SYSROOT_DIR := "$(SYSROOT_DIR)"
export PKG_CONFIG_DISABLE_UNINSTALLED := "yes"
EOF
```
````{dropdown} 编译 Python 需要的选项
```bash
cat <<EOF | tee config.site
ac_cv_file__dev_ptmx=no
ac_cv_file__dev_ptc=no
EOF
cd $(SRC_DIR) && ./configure \
--prefix=$(DESTDIR) \
--build=i686-pc-linux-gnu \
--target=aarch64-linux \
--host=aarch64-linux \
--disable-test-modules \
--enable-optimizations \
--with-openssl=$(SYSROOT_DIR)/usr \
--with-openssl-rpath=auto \
--disable-ipv6 \
--with-config-site=CONFIG_SITE
```
````
3、开始编译
出于跨平台的目的,你可能会用到一些比 Makefile 更自动化的构建工具,比如 CMake、Autoconf、Meson 等等。为此,我们不得不做一些相关的配置信息,如下所示:
::::{tab-set}
:::{tab-item} CMakeLists
```bash
cat <<EOF | tee CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(persondemo)
ADD_EXECUTABLE(persondemo main.cpp student.cpp)
EOF
mkdir $(SRC_DIR)/build
cd build && cmake ..
make
make install
```
:::
:::{tab-item} Autoconf
```bash
cd $(SRC_DIR) && autoreconf -vi
cd $(SRC_DIR) && ./configure \
--build=x86_64-pc-linux-gnu \
--target=aarch64-linux \
--host=aarch64-linux
make -C $(SRC_DIR)
make -C $(SRC_DIR) install
```
:::
:::{tab-item} Meson
```bash
cat <<EOF | tee aarch64-linux.ini
[constants]
sysroot_dir = '/arm-buildroot-linux-gnueabihf_sdk-buildroot/sysroot'
toochain_dir = sysroot_dir + '/usr/bin'
crosstools_prefix = toolchain_dir + '/aarch64-linux-'
[binaries]
c = crosstools_prefix + 'gcc'
cpp = crosstools_prefix + 'g++'
strip = crosstools_prefix + 'strip'
pkgconfig = 'pkg-config'
[build-in options]
has_function_print = true
has_function_hfkerhisadf = false
allow_default_for_cross = true
[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'cortex-a9'
endian = 'little'
[build_machine]
system = 'linux'
cpu_family = 'x86_64'
cpu = 'i686'
EOF
meson build_dir \
--prefix=$(CURR_DIR) \
--build-type=plain \
--cross-file $(CURR_DIR)/aarch64-linux.ini
cd build_dir && meson compile -C output_dir
meson install -C output_dir
```
:::
::::
这些工具本质上还是会生成 Makefile,因此,不管你用的什么构建工具,下面的步骤是通用的:
```makefile
CURR_DIR := $(shell pwd)
-include sdk.mk
# where is source code?
PROJECT_NAME := valgrind
VERSION := 3.23.0
TAR_BALL := $(PROJECT_NAME)-$(VERSION).tar.bz2
SRC_DIR := $(CURR_DIR)/$(PROJECT_NAME)-$(VERSION)
# where is the build results?
export DESTDIR := $(CURR_DIR)/build
# make
.PHONY: all
all:
@echo "SYSROOT=$(SYSROOT_DIR)"
@echo "$(CC) --print-file-name=include"
@echo "CC=`which $(CC)`"
@echo "CXX=`which $(CXX)`"
@echo "CMake=`which cmake`"
@echo "Clang=`which clang`"
@cd $(SRC_DIR) && ./configure \
--prefix=$(DESTDIR) \
--build=x86_64-pc-linux-gnu \
--target=aarch64-linux \
--host=aarch64-linux
# {x86,amd64,arm32,arm64,ppc32,ppc64le,ppc64be,s390x,mips32,mips64}-linux,
# {arm32,arm64,x86,mips32}-android, {x86,amd64}-solaris,
# {x86,amd64,arm64}-FreeBSD and {x86,amd64}-darwin
@cd $(SRC_DIR) && make -C $(SRC_DIR) -j8
@cd $(SRC_DIR) && make -C $(SRC_DIR) install
# make clean
.PHONY: clean
clean:
rm -rf $(SRC_DIR)
rm -rf $(DESTDIR)
# make patch
.PHONY: patch
patch:
tar xf $(TAR_BALL)
@if [ ! -d "patches" ]; then mkdir -p $(CURR_DIR)/patches; fi
@rm -rf $(DESTDIR) && mkdir -p $(DESTDIR)
# make repo
.PHONY: repo
repo:
@cd $(SRC_DIR) && if [ ! -d ".git" ]; then git init; fi
@cd $(SRC_DIR) && git config --add core.filemode false
@cd $(SRC_DIR) && git config --global core.autocrlf false
@cd $(SRC_DIR) && git add .
@cd $(SRC_DIR) && git commit -m "commit before make patch"
@echo "$(SRC_DIR) is already up to date"
# make diff
.PHONY: diff
diff:
@cd $(SRC_DIR) && git config --add core.filemode false
@cd $(SRC_DIR) && git config --global core.autocrlf false
@cd $(SRC_DIR) && git add .
@cd $(SRC_DIR) && git diff --cached > $(CURR_DIR)/patches/0000-undefined.patch
@echo "patch file is saved to $(CURR_DIR)/patches/0000-undefined.patch"
# make help
.PHONY: help
help:
@echo ""
@echo "Step 1:\033[35m make patch \033[0m Apply patches"
@echo "Step 2:\033[35m make repo \033[0m Initilize git repository and commit"
@echo "Step 3:\033[35m make \033[0m Check if the modifications are valid"
@echo "Step 4:\033[35m TODO: edit \033[0m Create, edit and save modifications"
@echo "Step 5:\033[35m make diff \033[0m Generate a patch file"
@echo ""
```
## 赋值操作
|运算符|行为描述|
|---|---|
|`=`|定义变量 |
|`:=`|重新定义变量,覆盖之前的值 |
|`?=`|如果变量未定义,则赋予默认值 |
|`+=`|在变量后追加值 |
```makefile
var = "hello world"
$(info $(var)) # "hello world"
var ?= "update or not"
$(info $(var)) # "hello world"
var += "append"
$(info $(var)) # "hello world" "append"
var := "always update"
$(info $(var)) # "always update"
# 默认目标
all:
@echo "All done" # All done
# 空目标,确保每个语句都会执行
.PHONY: all
```
## 通配符
| 通配符 | 作用 |
| ------ | ----------------------------------- |
| `$@` | 代表目标文件,也就是 `:` 左侧的文件 |
| `$^` | 代表依赖文件,也就是 `:` 右侧的文件 |
| `$<` | 代表第一个依赖文件 |
| `%` | 匹配字符串中任意个字符 |
## 常用函数
| 函数 | 作用 |
| -------------------------------------- | ------------------------------------------------------- |
| `$(subst from,to,text)` | 将 `text` 中的 `from` 替换为 `to` |
| `$(patsubst pattern,replacement,text)` | 将 `text` 中符合格式 `pattern` 的字替换为 `replacement` |
| `$(strip string)` | 去掉前导和结尾空格,并将中间的多个空格压缩为单个空格 |
| `$(wildcard pattern)` | 匹配 `pattern`,文件名之间用一个空格隔开 |