-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.arch_indep
353 lines (313 loc) · 13.5 KB
/
Makefile.arch_indep
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
# musl-cross-make meta makefile
# built to support these architectures:
# aarch64-linux-musl
# arm-linux-musleabihf
# i686-linux-musl
# riscv64-linux-musl
# x86_64-linux-musl
# prereqs:
# bash (gnu)
# gzip (busybox should be fine)
# curl (with ssl/tls support)
# gcc/g++
# ln (busybox should be fine)
# make (gnu)
# sort (busybox should be fine)
# tar (busybox should be fine)
# wget (with ssl/tls support)
# projects:
# musl-cross-make: https://github.com/richfelker/musl-cross-make
# crosware: https://github.com/ryanwoodsmall/crosware
# chralpine (if you can't use docker): https://github.com/ryanwoodsmall/shell-ish/blob/master/bin/chralpine.sh
# install alpine prereqs:
# XXX - need to figure out man page generation requirements, is groff enough?
# apk update ; apk add git make gcc g++ make curl wget bash groff texinfo gettext gettext-static gettext-libs gettext-dev
# example usage with bash fd substitution:
# git clone https://github.com/richfelker/musl-cross-make.git
# cd musl-cross-make
# make -f <(curl -kLs https://raw.githubusercontent.com/ryanwoodsmall/musl-misc/master/musl-cross-make-confs/Makefile.arch_indep)
# command i normally use:
# ( time ( make -f <(curl -kLs https://raw.githubusercontent.com/ryanwoodsmall/musl-misc/master/musl-cross-make-confs/Makefile.arch_indep) ) ) >/tmp/musl-cross-make.out 2>&1
# stages:
# clean everything up
# generate configs
# copy in shared config.mak
# build normal "shared" compiler with system compiler
# install shared compiler
# clean again
# copy in static config.mak
# build static compiler
# install static compiler
# create static compiler symlinks
# archive and compress static compiler
# XXX - error on unset var example
# ifndef TEST_VAR
# $(error TEST_VAR not set)
# endif
# XXX - look at .ONESHELL
# https://www.gnu.org/software/make/manual/html_node/One-Shell.html
# - add optional "test -e config.mak.custom" & append to config.mak
# XXX - need static and shared config
# SHARED_CONFIG
# STATIC_CONFIG
# - bundle into COMMON_CONFIG or GCC_CONFIG?
# - inherit from env with += $(...)
# - --with-boot-ldflags=... ?
# - C++ is still broken on building a shared .so with libstdc++.a
# - default rpath of $(CWCURRENT)/$(TRIPLET)/lib?
#
# tuning
# - i want these to be able to build/run code for the big four architectures
# - ... and elsewhere
#
# ARM 32-bit flags
# - mostly want to stay compatible with armv6/vfp/hard-float (Pi 1, Pi zero)
# - gcc flags will be something like "--with-arch=armv6 --with-float=hard --with-fpu=vfp"
# - vfp vs vfpv2?
# - rpi1 and pi0 arch are arm1176jzf-s, flags should possibly be:
# -march=armv6kz
# -mcpu=arm1176jzf-s
# -mtune=arm1176jzf-s
# -mfpu=vfp
# -mfloat-abi=hard
# - should work as expected on ARMv7-A/Cortex-A series
# - links:
# - https://wiki.musl-libc.org/getting-started.html#Notes_on_ARM_Float_Mode
# - https://raspberrypi.stackexchange.com/questions/2046/which-cpu-flags-are-suitable-for-gcc-on-raspberry-pi
# - https://www.raspberrypi.org/forums/viewtopic.php?t=11629
# - https://github.com/richfelker/musl-cross-make/issues/34
# - https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html
# ARM 64-bit
# - fewer options here
# - probably tune to cortex-a53 to be safe-ish
# - cortex-a35?
# -march=armv8{.1}-a{+[no]{crc,crypto,fp,simd,lse}}
# -mtune={generic,cortex-a{35,53,57,72}}
# intel 32-bit
# - lowest common denominator is geode, like an i486 w/mmx?
# - musl requires i486+ (or cmpxchg emulation)
# -march=... i486? i586/pentium? i686/pentium-pro? pentium-mmx? geode?
# -mtune=... generic (default, i686)? pentium-mmx? geode?
# intel 64-bit
# - lcd is probably atom
# - bonnell or silvermont?
# shared/static
# - need to figure out true static-only
# - libc.so is only shared lib, left in place
# - ld-musl-${arch}.so symlinks fixed to point to full path to libc.so
# - patch sets custom ld.so for each arch in its triplet directory
# - sets custom MUSL_DYNAMIC_LINKER/MUSL_DYNAMIC_LINKER32/MUSL_DYNAMIC_LINKER64 with full path
# - applied only during final static compilation step
# XXX - libstdc++.a objects being compiled without -fPIC, why?
# makes .so link hard/impossible
# maybe "-no-pie" is needed?
# bah
# - https://stackoverflow.com/questions/46827433/g-compile-error-rodata-can-not-be-used-when-making-a-shared-object-recomp
# - https://github.com/rust-lang/rust/issues/47037
# - https://stackoverflow.com/questions/23257965/how-do-i-compile-gnu-libstdc-a-with-pic
# - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=28811
# - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58638
# may need to replace $lt_prog_compiler_pic_CXX with explicit -fPIC
# XXX - in $(TRIPLET)/lib/*.la files
# - need to set proper libdir='/$(TRIPLET)/lib'
# basic stuff: shell, timestamp and architecture
SHELL := /bin/bash
ifeq ($(TS),)
TS := $(shell date '+%Y%m%d%H%M')
endif
ARCH := $(shell uname -m | sed 's/^\(arm\).*/\1/g')
# versions
#GCCVER := 10.3.0
GCCVER := 9.4.0
BINUTILSVER := 2.33.1
# XXX - gettexttiny breaks between 1.2.4 and 1.2.5; need to bisect to figure out where
#MUSLVER := 1.2.5
MUSLVER := 1.2.4
ISLVER := 0.21
#LINUXVER := 4.4.10
## musl
#MUSLSHA1 := null
#MUSLFILE := musl-$(MUSLVER).tar.gz
# form our triplet, arm needs to be hardware float
TRIPLET_BASE := $(ARCH)-linux-musl
TRIPLET =
ifeq ($(ARCH),arm)
TRIPLET=$(TRIPLET_BASE)eabihf
else
TRIPLET=$(TRIPLET_BASE)
endif
# and cpu-specific stuff
ARCH_GCC_CONFIG =
ifeq ($(ARCH),arm)
# hard-float only, default arch to and tune for rpi on arm 32-bit
ARCH_GCC_CONFIG='--with-arch=armv6kz --with-tune=arm1176jzf-s --with-float=hard --with-fpu=vfp'
endif
# build out paths to expand into
# XXX - make pkg and add a postfix i.e., name pacakge "statictoolchin9"
# CWPKG := statictoolchain
# CWPOST :=
# MUSLTOP := $(CWTOP)/software/$(CWPKG)$(CWPOST)
TMPSHAREDDIR := $(PWD)/shared-build
CWTOP := /usr/local/crosware
MUSLTOP := $(CWTOP)/software/statictoolchain
CWTCPREFIX := $(TS)-
CWSTATICINST := $(MUSLTOP)/$(CWTCPREFIX)$(TRIPLET)
CWARCHDIR := $(CWSTATICINST)/$(TRIPLET)
CWARCHLIBDIR := $(CWARCHDIR)/lib
CWCURRENT := $(MUSLTOP)/current
CWCURRENTLD := $(CWCURRENT)/$(TRIPLET)/lib/ld.so
# our final archive will be dumped to /tmp
CWSTATICDIR := $(shell basename $(CWSTATICINST))
CWSTATICTAR := /tmp/$(CWSTATICDIR).tar
CWSTATICGZ := $(CWSTATICTAR).gz
# and add our temp shared build to our path
EXPATH := $(TMPSHAREDDIR)/bin
PATH := $(EXPATH):$(PATH)
export PATH
# musl-cross-make stuff
CONFMAK := config.mak
CONFMAKDIST := $(CONFMAK).dist
.PHONY: all prereqs build_shared build_static cleanconfs create_symlinks archive_compiler get_patches remove_patches # muslsha
all: prereqs cleanconfs config.mak.common config.mak.shared config.mak.static build_shared build_static create_symlinks archive_compiler # muslsha
## generate a musl sha file if there isn't one
#muslsha:
# test -e hashes/$(MUSLFILE).sha1 || echo '$(MUSLSHA1) $(MUSLFILE)' > hashes/$(MUSLFILE).sha1
# clean up configs everytime we run
cleanconfs:
echo 'cleaning configs'
rm -f $(CONFMAK).common $(CONFMAK).shared $(CONFMAK).static
# building on alpine in a miniroot, we may not have everything
# we need to check we're not using busybox wget too - no ssl by default
prereqs:
echo 'checking prerequisites'
which gzip
bash --version | egrep -qi '^GNU bash'
curl --version | egrep -qi '(tls|ssl)'
gcc --version | egrep -qi '^gcc'
g++ --version | egrep -qi '^g\+\+'
git --version | egrep -qi '^git version'
make --version | egrep -qi '^GNU Make'
# XXX - busybox wget from crosware appears to work fine
# XXX - just verify it's available for now
# XXX - DL_CMD defaults to "wget -c -O"
# XXX - append --no-check-certificate to force cert acceptance?
#wget --version | egrep -qi '(tls|ssl)'
# change to "curl -k -C - -L -o" in config.mak.common to avoid wget completely
# check left for posterity...
which wget
echo
echo 'attempting to make directory $(CWSTATICINST)'
echo 'if this fails, make sure you can write to $(CWSTATICINST) or its parent directory'
mkdir -p $(CWSTATICINST)
echo
echo 'if this fails, make sure you are running in a musl-cross-make git checkout'
test -e $(CONFMAKDIST)
echo
# $(CONFMAK) common toolchain options
$(CONFMAK).common: cleanconfs
echo 'creating $@'
echo -n > $@
echo 'GCC_VER = $(GCCVER)' >> $@
echo 'BINUTILS_VER = $(BINUTILSVER)' >> $@
echo 'MUSL_VER = $(MUSLVER)' >> $@
echo 'ISL_VER = $(ISLVER)' >> $@
#echo 'LINUX_VER = $(LINUXVER)' >> $@
echo 'DL_CMD = curl -k -C - -L -o' >> $@
echo 'TARGET = $(TRIPLET)' >> $@
echo 'COMMON_CONFIG += CFLAGS="-fPIC -g0 -Os" CXXFLAGS="-fPIC -g0 -Os"' >> $@
echo 'COMMON_CONFIG += --disable-nls' >> $@
echo 'GCC_CONFIG += --enable-languages=c,c++' >> $@
echo 'GCC_CONFIG += --disable-multilib' >> $@
echo 'GCC_CONFIG += --disable-lto' >> $@
echo 'GCC_CONFIG += --with-pic' >> $@
echo 'GCC_CONFIG += --enable-cxx-flags=-fPIC' >> $@
#echo 'GCC_CONFIG += --disable-default-pie' >> $@
echo 'GCC_CONFIG += $(ARCH_GCC_CONFIG)' >> $@
echo 'BINUTILS_CONFIG += --enable-install-libiberty' >> $@
echo 'BINUTILS_CONFIG += --disable-lto' >> $@
#echo 'MUSL_CONFIG += --with-malloc=oldmalloc' >> $@
sort -o $@ $@
# temporary shared toolchain build config
$(CONFMAK).shared: $(CONFMAK).common
echo 'creating $@'
cat $< > $@
echo 'COMMON_CONFIG += LDFLAGS="$(LDFLAGS) -s"' >> $@
echo 'OUTPUT = $(TMPSHAREDDIR)' >> $@
sort -o $@ $@
# final static toolchain build config
$(CONFMAK).static: $(CONFMAK).common
echo 'creating $@'
cat $< > $@
echo 'COMMON_CONFIG += --enable-shared=no --disable-shared --enable-static=yes --enable-static' >> $@
echo 'COMMON_CONFIG += LDFLAGS="$(LDFLAGS) -s --static"' >> $@
# XXX - need -fPIC in CC/CXX def?
echo 'COMMON_CONFIG += CC="$(TRIPLET)-gcc -static --static -fPIC" CXX="$(TRIPLET)-g++ -static --static -fPIC"' >> $@
#echo 'COMMON_CONFIG += --build=$(TRIPLET)' >> $@
#echo 'COMMON_CONFIG += --host=$(TRIPLET)' >> $@
#echo 'COMMON_CONFIG += --target=$(TRIPLET)' >> $@
echo 'OUTPUT = $(CWSTATICINST)' >> $@
# XXX - need this? need libstdc++ compiled w/-fPIC, not sure how...
echo 'GCC_CONFIG += --disable-host-shared' >> $@
# XXX - risc-v 64-bit (riscv64) only???
echo $(TRIPLET) | grep -q riscv64 && echo 'LINUX_VER = 4.19.90' >> $@ || true
sort -o $@ $@
# custom ld.so path stuff
get_patches: remove_patches
# XXX - change when default versions are updated
# XXX - wget needs to be changed to whatever $(DL_CMD) is set to
mkdir -p ./patches/gcc-$(GCCVER) || true
cd ./patches/gcc-$(GCCVER)/ ; curl -kLO https://raw.githubusercontent.com/ryanwoodsmall/musl-misc/master/musl-cross-make-confs/patches/9999-crosware-ldso_gcc-$(GCCVER).diff
cd ./patches/gcc-$(GCCVER)/ ; curl -kLO https://raw.githubusercontent.com/ryanwoodsmall/musl-misc/master/musl-cross-make-confs/patches/9999-crosware-gcc-$(GCCVER)-libstdcpp-v3-configure.diff
mkdir -p ./patches/binutils-$(BINUTILSVER) || true
cd ./patches/binutils-$(BINUTILSVER)/ ; curl -kLO https://raw.githubusercontent.com/ryanwoodsmall/musl-misc/master/musl-cross-make-confs/patches/9999-crosware-ldso_binutils-$(BINUTILSVER).diff
#mkdir -p ./patches/musl-$(MUSLVER) || true
#cd ./patches/musl-$(MUSLVER) ; curl -kLo 9999-crosware-CVE-2020-28928.diff https://raw.githubusercontent.com/ryanwoodsmall/musl-misc/master/patches/CVE-2020-28928.patch
# XXX - hackity hacky hack
remove_patches:
rm -f ./patches/*/*crosware* || true
# build and install temporary shared toolchain
build_shared: $(CONFMAK).shared get_patches
echo 'building temporary shared compiler'
cat $< > $(CONFMAK)
make clean || echo -n
make -j`nproc`
make install
make clean
# build and install final static toolchain
build_static: $(CONFMAK).static build_shared get_patches
echo 'building final static compiler'
hash -r
cat $< > $(CONFMAK)
make clean || echo -n
make -j`nproc`
make install
git rev-parse HEAD > $(CWSTATICINST)/musl-cross-make_commit.txt
# XXX - need binutils/gmp/mpc/mpfr libs for self-hosting?
# XXX - is this safe???
# XXX - crosware has gmp, mpfr, mpc, binutils recipes now...
#install -m 0644 ./build/local/$(TRIPLET)/obj_binutils/bfd/libbfd.a $(CWARCHLIBDIR)/
#install -m 0644 ./build/local/$(TRIPLET)/obj_binutils/libiberty/libiberty.a $(CWARCHLIBDIR)/
#install -m 0644 ./build/local/$(TRIPLET)/obj_binutils/opcodes/libopcodes.a $(CWARCHLIBDIR)/
#install -m 0644 ./build/local/$(TRIPLET)/obj_gcc/gmp/.libs/libgmp.a $(CWARCHLIBDIR)/
#install -m 0644 ./build/local/$(TRIPLET)/obj_gcc/mpc/src/.libs/libmpc.a $(CWARCHLIBDIR)/
#install -m 0644 ./build/local/$(TRIPLET)/obj_gcc/mpfr/src/.libs/libmpfr.a $(CWARCHLIBDIR)/
# XXX - reenable when things shake out
#make clean
# create triplet-program symlinks to bare program
create_symlinks: build_static
# XXX need $(TRIPLET)-${program} to ${program} symlinks for man pages
echo 'creating symbolic links'
cd $(CWSTATICINST)/bin ; for p in $(TRIPLET)-* ; do ln -sf $$p `echo $$p | sed s/$(TRIPLET)-//g` ; done
cd $(CWSTATICINST)/bin ; for p in $(TRIPLET)-* ; do ln -sf $$p `echo $$p | sed s/$(TRIPLET)-/musl-/g` ; done
cd $(CWARCHLIBDIR) ; for l in ld*.so* ; do ln -sf `pwd`/libc.so $$l ; done
cd $(CWARCHLIBDIR) ; ln -sf `pwd`/libc.so ld.so
cd $(CWARCHLIBDIR) ; ln -sf `pwd`/libc.so ldd
cd $(CWARCHLIBDIR) ; ln -sf `pwd`/ldd $(CWSTATICINST)/bin/musl-ldd
# final tar and compress
archive_compiler: create_symlinks
echo 'archiving static compiler'
cd $(MUSLTOP) ; tar -cf $(CWSTATICTAR) $(CWSTATICDIR)
gzip -9 $(CWSTATICTAR)
echo
echo 'static compiler archived to $(CWSTATICGZ)'
echo