文章

qt5.15.2 交叉编译 loongarch64

qt5.15.2 交叉编译 loongarch64

该文主要介绍使用 CentOS7 x86_64 交叉编译为 loongarch64

qt5.15.2 交叉编译 loongarch64

该篇文章都是基于上一篇文章 qt5.15.2交叉编译arm64 修改。

1. 环境

  • 虚拟机系统:CentOS-7-x86_64-DVD-2207-02.iso
  • 交叉编译工具链:loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3.tar.xz
  • Qt:qt-everywhere-src-5.15.2.tar.xz

项目中用到证书以及网页,所以我需要 OpenSSL X11 WebEngine 模块

编译qt core需要4G内存,编译X11需要8G,编译WebEngine至少需要24G,否则会内存耗尽导致退出

2. 准备依赖

2.1. 软件包说明

所有的原始文件都是放在 /cross 目录下

1
2
mkdir /cross
cd /cross
软件包下载地址路径安装路径
loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3.tar.xzhttp://www.loongnix.cn/zh/toolchain/GNU//cross内部包含 sysroot
gcc-5.4.0.tar.gzhttps://mirrors.nju.edu.cn/gnu/gcc/gcc-5.4.0//cross/usr/local/gcc
各种依赖 /cross/dependence/loongarch64

2.2. 配置交叉编译环境

loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3.tar.xz 是交叉编译工具链。

需要做的就是解压以及配置环境变量

1
2
3
4
5
6
7
8
# 解压
tar -xvf loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3.tar.xz
# 修改环境变量文件
vim /etc/profile
# 添加
export PATH=$PATH:/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin
# 使环境变量文件生效
source /etc/profile

使用 loongarch64-linux-gnu-gcc -v 查看到版本即为配置成功。

2.3. 升级系统 GCC

CentOS7 默认的版本的是 gcc4.8,为了和交叉编译统一,需要升级版本为 gcc5.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 可能缺失基础 gcc
yum install gcc gcc-c++

# 升级 gcc
tar -xvf gcc-5.4.0.tar.gz
cd gcc-5.4.0
# 自动检查预编译环境并安装,需要联网
./contrib/download_prerequisites
# 安装路径是 /usr/local/gcc
./configure --prefix=/usr/local/gcc --enable-checking=release --enable-languages=c,c++ --disable-multilib
make -j4 && make install

# 修改环境变量文件
vim /etc/profile
# 添加
export PATH=/usr/local/gcc/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/gcc/lib64:$LD_LIBRARY_PATH
# 使环境变量文件生效
source /etc/profile

使用 gcc -v 查看到版本即为配置成功。

image-20240512202240264

2.4. 下载依赖

X11 相关下载网址:https://www.x.org/releases/individual/

文件都放在 /cross/dependence

1
2
mkdir /cross/dependence
cd /cross/dependence

以下是下载 OpenSSL X11 WebEngine 所需要依赖库的脚本 download_dependence.sh,需要联网使用且部分还需要挂代理。

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
#!/bin/sh
# openssl
wget -c https://www.openssl.org/source/old/1.1.1/openssl-1.1.1q.tar.gz

# x11 dependence
wget -c https://www.x.org/releases/individual/proto/xorgproto-2020.1.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXrender-0.9.10.tar.gz
wget -c https://www.x.org/releases/individual/lib/xtrans-1.4.0.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXau-1.0.9.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXrender-0.9.10.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXext-1.3.4.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXrandr-1.5.2.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXfixes-5.0.3.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXcomposite-0.4.5.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXcursor-1.2.0.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXi-1.7.10.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXScrnSaver-1.2.3.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXtst-1.2.3.tar.gz
wget -c https://www.x.org/releases/individual/lib/libxshmfence-1.3.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXxf86vm-1.1.4.tar.gz
wget -c https://www.x.org/releases/individual/util/util-macros-1.19.3.tar.gz
wget -c https://www.x.org/releases/individual/data/xkeyboard-config/xkeyboard-config-2.30.tar.gz
wget -c https://www.x.org/releases/individual/lib/libSM-1.2.3.tar.gz
wget -c https://www.x.org/releases/individual/lib/libICE-1.0.10.tar.gz
wget -c https://xkbcommon.org/download/libxkbcommon-1.4.0.tar.xz
wget -c https://dri.freedesktop.org/libdrm/libdrm-2.4.100.tar.gz

# xcb
wget -c https://www.x.org/releases/individual/xcb/libpthread-stubs-0.4.tar.gz
wget -c https://www.x.org/releases/individual/xcb/xcb-util-0.3.9.tar.gz
wget -c https://www.x.org/releases/individual/xcb/xcb-util-image-0.3.9.tar.gz
wget -c https://www.x.org/releases/individual/xcb/xcb-util-keysyms-0.3.9.tar.gz
wget -c https://www.x.org/releases/individual/xcb/xcb-util-wm-0.3.9.tar.gz
wget -c https://www.x.org/releases/individual/xcb/xcb-util-renderutil-0.3.9.tar.gz
wget -c https://www.x.org/releases/individual/xcb/xcb-proto-1.13.tar.gz
wget -c https://www.x.org/releases/individual/xcb/libxcb-1.13.tar.gz

# glib2
wget -c https://www.zlib.net/fossils/zlib-1.2.11.tar.gz
wget -c https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
wget -c https://github.com/libffi/libffi/releases/download/v3.4.3/libffi-3.4.3.tar.gz
wget -c https://github.com/SELinuxProject/selinux/releases/download/20191204/libsepol-3.0.tar.gz
wget -c https://github.com/SELinuxProject/selinux/releases/download/20191204/libselinux-3.0.tar.gz
wget -c https://ftp.acc.umu.se/pub/GNOME/sources/glib/2.61/glib-2.61.3.tar.xz

# x11
wget -c https://www.x.org/releases/individual/lib/libX11-1.7.5.tar.gz

# webengine
wget -c https://www.x.org/releases/individual/lib/libxkbfile-1.1.0.tar.gz
wget -c https://www.x.org/releases/individual/lib/libXdamage-1.1.5.tar.gz
wget -c https://github.com/libexpat/libexpat/releases/download/R_2_2_9/expat-2.2.9.tar.gz
wget -c https://dbus.freedesktop.org/releases/dbus/dbus-1.13.6.tar.gz
wget -c https://download.savannah.gnu.org/releases/freetype/freetype-2.10.1.tar.gz
wget -c ftp://xmlsoft.org/libxml2/libxml2-2.9.9.tar.gz
wget -c https://nchc.dl.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz
wget -c https://www.freedesktop.org/software/fontconfig/release/fontconfig-2.13.92.tar.gz
wget -c https://github.com/sqlite/sqlite/archive/refs/tags/version-3.30.1.tar.gz
wget -c https://www.linuxfromscratch.org/patches/blfs/12.1/nss-3.98-standalone-1.patch
wget -c https://ftp.mozilla.org/pub/security/nss/releases/NSS_3_98_RTM/src/nss-3.98-with-nspr-4.35.tar.gz
wget -c https://gitlab.freedesktop.org/mesa/mesa/-/archive/mesa-19.0.0/mesa-mesa-19.0.0.tar.gz
wget -c https://github.com/unicode-org/icu/releases/download/release-70-rc/icu4c-70rc-src.tgz
wget -c https://github.com/mm2/Little-CMS/archive/refs/tags/lcms2.10.tar.gz

2.5. 编译依赖

2.5.1. 前置条件

上述依赖库编译还需要本机编译工具,以下代码是下载需要的编译工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# xkbcommon 编译环境
yum install bison
yum install python3
python3 -m pip install meson
python3 -m pip install ninja
python3 -m pip install mako

# dbus 编译环境
yum install flex
yum install libtool

# nss 编译环境
yum install tcl
yum install patch
yum install zlib-devel
# qt 实际编译需要本地的 nss
yum install nss

# fontconfig 编译环境
yum install gperf

2.5.2. 架构检测

相比于 arm64 架构,loongarch64 架构是 2022 年出的新架构,所以很多底层依赖库交叉编译时不支持,其中 libffi3.4.3 icu70 nspr4.35/nss3.98 支持 loongarch64。剩下的可以通过修改 config.subconfig.guess 进行自定义架构1

文件都放在 /cross/dependence

1
2
3
4
# config.sub
sudo wget -O config.sub "git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD"
# config.guess
sudo wget -O config.guess "git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD"

2.5.3. 编译依赖

文件都放在 /cross/dependence

xkbcommon glib mesa 是使用 mesonninja 构建,所以需要编辑 cross-file。可能需要自行更改自己对应文件中编译相关目录。

编辑 loongarch64_xkbcommon.txt,内容如下:

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
[binaries]
c = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-gcc'
cpp = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-c++'
ar = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-ar'
strip = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-strip'
ld = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-ld'
pkgconfig = '/usr/bin/pkg-config'
 
[properties]
pkg_config_libdir = ['/loongarch64/lib/pkgconfig', '/loongarch64/share/pkgconfig']
skip_sanity_check = true
 
[paths]
libdir = '/loongarch64/lib'
includedir = '/loongarch64/include'
prefix = '/loongarch64'
 
[host_machine]
system = 'linux'
cpu_family = 'loongarch64'
cpu = 'loongarch64'
endian = 'little'
 
[build_machine]
system = 'linux'
cpu_family = 'x86_64'
cpu = 'i686'
endian = 'little'

编辑 loongarch64_glib.txt,内容如下:

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
[binaries]
c = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-gcc'
cpp = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-c++'
ar = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-ar'
strip = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-strip'
ld = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-ld'
pkgconfig = '/usr/bin/pkg-config'
 
[properties]
pkg_config_libdir = ['/loongarch64/lib/pkgconfig', '/loongarch64/share/pkgconfig']
skip_sanity_check = true

[paths]
libdir = '/loongarch64/lib'
includedir = '/loongarch64/include'
prefix = '/loongarch64'
 
[host_machine]
system = 'linux'
cpu_family = 'loongarch64'
cpu = 'loongarch64'
endian = 'little'
 
[build_machine]
system = 'linux'
cpu_family = 'x86_64'
cpu = 'i686'
endian = 'little'

编辑 loongarch64_mesa.txt,内容如下:

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
[binaries]
c = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-gcc'
cpp = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-c++'
ar = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-ar'
strip = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-strip'
ld = '/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-ld'
pkgconfig = '/usr/bin/pkg-config'
 
[properties]
pkg_config_libdir = ['/loongarch64/lib/pkgconfig', '/loongarch64/share/pkgconfig']
skip_sanity_check = true
 
[paths]
libdir = '/loongarch64/lib'
includedir = '/loongarch64/include'
prefix = '/loongarch64'
 
[host_machine]
system = 'linux'
cpu_family = 'loongarch64'
cpu = 'loongarch64'
endian = 'little'
 
[build_machine]
system = 'linux'
cpu_family = 'x86_64'
cpu = 'i686'
endian = 'little'

以下是编译依赖的脚本 loongarch64_dependence.sh,直接运行即可

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
#!/bin/sh
set -e

MY_PREFIX=/loongarch64
MY_HOST=loongarch64-linux-gnu

export PKG_CONFIG_PATH=${MY_PREFIX}/lib/pkgconfig:${MY_PREFIX}/share/pkgconfig
export C_INCLUDE_PATH=${MY_PREFIX}/include:$C_INCLUDE_PATH
export LD_LIBRARY_PATH=${MY_PREFIX}/lib:$LD_LIBRARY_PATH

export CFLAGS="$CFLAGS -I${MY_PREFIX}/include"
export LDFLAGS="$LDFLAGS -L${MY_PREFIX}/lib"
export LDFLAGS="$LDFLAGS -Wl,-rpath-link=${MY_PREFIX}/lib -Wl,-rpath-link=${MY_PREFIX}/lib64"
export LDFLAGS="$LDFLAGS -Wl,-rpath=${MY_PREFIX}/lib -Wl,-rpath=${MY_PREFIX}/lib64"

export CC=${MY_HOST}-gcc
export CXX=${MY_HOST}-g++

build_script_openssl() {
    ./Configure \
        linux-generic64 \
        --prefix=${MY_PREFIX}

    make -j4 && make install
}

build_script_xcb_common() {
    \cp ../config.sub ./

    ./configure \
        --prefix=${MY_PREFIX} \
        --host=${MY_HOST} \
        --enable-option-checking \
        --enable-malloc0returnsnull

    make -j4 && make install
}

build_script_x11() {
    ./configure \
        --prefix=${MY_PREFIX} \
        --host=${MY_HOST} \
        --enable-option-checking \
        --enable-unix-transport \
        --enable-tcp-transport \
        --enable-ipv6 \
        --enable-local-transport \
        --enable-malloc0returnsnull

    make -j4 && make install
}

build_script_xkbcommon() {
    meson setup build \
        --prefix ${MY_PREFIX} \
        --buildtype release \
        --cross-file ../loongarch64_xkbcommon.txt \
        -D enable-x11=true \
        -D enable-wayland=false \
        -D enable-docs=false \
        -D enable-xkbregistry=false \
        -D xkb-config-root=${MY_PREFIX}/lib/pkgconfig

    ninja -C build
    ninja -C build install
}

build_script_selinux() {
    export CC=${MY_HOST}-gcc
    export CXX=${MY_HOST}-g++
    export PREFIX=${MY_PREFIX}
    export SHLIBDIR=${MY_PREFIX}/lib
    make -j4 && make install
}

build_script_freetype() {
    ./configure \
        --prefix=${MY_PREFIX} \
        --host=${MY_HOST} \
        --with-zlib=no \
        --with-png=no \
        --enable-freetype-config

    make -j4 && make install
}

build_script_xml2() {
    ./configure \
        --prefix=${MY_PREFIX} \
        --host=${MY_HOST} \
        --with-zlib=no \
        --without-python

    make -j4 && make install
}

build_script_zlib() {
    ./configure \
        --prefix=${MY_PREFIX} \
        --shared \
        --static

    make -j4 && make install
}

build_script_glib() {
    meson setup build \
        --prefix ${MY_PREFIX} \
        --buildtype release \
        --cross-file ../loongarch64_glib.txt \
        -D libmount=false

    ninja -C build
    ninja -C build install
}

build_script_mesa() {
    export CFLAGS="-I${MY_PREFIX}/include/libdrm -I${MY_PREFIX}/include/libdrm/nouveau $CFLAGS"
    meson setup build \
        --prefix ${MY_PREFIX} \
        --buildtype release \
        --cross-file ../loongarch64_mesa.txt \
        -D gallium-drivers=nouveau,freedreno \
        -D dri-drivers="" \
        -D vulkan-drivers="" \
        -D platforms=x11

    ninja -C build
    ninja -C build install
}

build_script_icu() {
    mkdir $(pwd)/build
    cd $(pwd)/build
    ../runConfigureICU Linux/gcc
    make -j4
    cd ..

    ./configure \
        --prefix=${MY_PREFIX} \
        --host=${MY_HOST} \
        --with-cross-build=$(pwd)/build

    make -j4 && make install
}

build_openssl() {
    rm -rf openssl-1.1.1q
    tar -xvf openssl-1.1.1q.tar.gz
    cd openssl-1.1.1q
    build_script_openssl
    cd ..
}

build_xtrans() {
    rm -rf xtrans-1.4.0
    tar -xvf xtrans-1.4.0.tar.gz
    cd xtrans-1.4.0
    build_script_xcb_common
    cd ..
}

build_xcb_proto() {
    rm -rf xcb-proto-1.13
    tar -xvf xcb-proto-1.13.tar.gz
    cd xcb-proto-1.13
    build_script_xcb_common
    cd ..
}

build_xorgproto() {
    rm -rf xorgproto-2020.1
    tar -xvf xorgproto-2020.1.tar.gz
    cd xorgproto-2020.1
    build_script_xcb_common
    cd ..
}

build_Xau() {
    rm -rf libXau-1.0.9
    tar -xvf libXau-1.0.9.tar.gz
    cd libXau-1.0.9
    build_script_xcb_common
    cd ..
}

build_xcb() {
    rm -rf libxcb-1.13
    tar -xvf libxcb-1.13.tar.gz
    cd libxcb-1.13
    \cp ../config.sub ./build-aux/
    build_script_xcb_common
    cd ..
}

build_pthread() {
    rm -rf libpthread-stubs-0.4
    tar -xvf libpthread-stubs-0.4.tar.gz
    cd libpthread-stubs-0.4
    build_script_xcb_common
    cd ..
}

build_xcb_util() {
    rm -rf xcb-util-0.3.9
    tar -xvf xcb-util-0.3.9.tar.gz
    cd xcb-util-0.3.9
    build_script_xcb_common
    cd ..
}

build_xcb_util_image() {
    rm -rf xcb-util-image-0.3.9
    tar -xvf xcb-util-image-0.3.9.tar.gz
    cd xcb-util-image-0.3.9
    build_script_xcb_common
    cd ..
}

build_xcb_util_keysyms() {
    rm -rf xcb-util-keysyms-0.3.9
    tar -xvf xcb-util-keysyms-0.3.9.tar.gz
    cd xcb-util-keysyms-0.3.9
    build_script_xcb_common
    cd ..
}

build_xcb_util_renderutil() {
    rm -rf xcb-util-renderutil-0.3.9
    tar -xvf xcb-util-renderutil-0.3.9.tar.gz
    cd xcb-util-renderutil-0.3.9
    build_script_xcb_common
    cd ..
}

build_xcb_util_wm() {
    rm -rf xcb-util-wm-0.3.9
    tar -xvf xcb-util-wm-0.3.9.tar.gz
    cd xcb-util-wm-0.3.9
    build_script_xcb_common
    cd ..
}

build_util_macros() {
    rm -rf util-macros-1.19.3
    tar -xvf util-macros-1.19.3.tar.gz
    cd util-macros-1.19.3
    build_script_xcb_common
    cd ..
}

build_xrender() {
    rm -rf libXrender-0.9.10
    tar -xvf libXrender-0.9.10.tar.gz
    cd libXrender-0.9.10
    build_script_xcb_common
    cd ..
}

build_xext() {
    rm -rf libXext-1.3.4
    tar -xvf libXext-1.3.4.tar.gz
    cd libXext-1.3.4
    build_script_xcb_common
    cd ..
}

build_drm() {
    rm -rf libdrm-2.4.100
    tar -xvf libdrm-2.4.100.tar.gz
    cd libdrm-2.4.100
    \cp ../config.sub ./build-aux/
    build_script_xcb_common
    cd ..
}

build_xrandr() {
    rm -rf libXrandr-1.5.2
    tar -xvf libXrandr-1.5.2.tar.gz
    cd libXrandr-1.5.2
    build_script_xcb_common
    cd ..
}

build_xfixes() {
    rm -rf libXfixes-5.0.3
    tar -xvf libXfixes-5.0.3.tar.gz
    cd libXfixes-5.0.3
    build_script_xcb_common
    cd ..
}

build_xcomposite() {
    rm -rf libXcomposite-0.4.5
    tar -xvf libXcomposite-0.4.5.tar.gz
    cd libXcomposite-0.4.5
    build_script_xcb_common
    cd ..
}

build_xcursor() {
    rm -rf libXcursor-1.2.0
    tar -xvf libXcursor-1.2.0.tar.gz
    cd libXcursor-1.2.0
    build_script_xcb_common
    cd ..
}

build_xi() {
    rm -rf libXi-1.7.10
    tar -xvf libXi-1.7.10.tar.gz
    cd libXi-1.7.10
    build_script_xcb_common
    cd ..
}

build_xscrnsaver() {
    rm -rf libXScrnSaver-1.2.3
    tar -xvf libXScrnSaver-1.2.3.tar.gz
    cd libXScrnSaver-1.2.3
    build_script_xcb_common
    cd ..
}

build_xtst() {
    rm -rf libXtst-1.2.3
    tar -xvf libXtst-1.2.3.tar.gz
    cd libXtst-1.2.3
    build_script_xcb_common
    cd ..
}

build_xshmfence() {
    rm -rf libxshmfence-1.3
    tar -xvf libxshmfence-1.3.tar.gz
    cd libxshmfence-1.3
    build_script_xcb_common
    cd ..
}

build_xxf86vm() {
    rm -rf libXxf86vm-1.1.4
    tar -xvf libXxf86vm-1.1.4.tar.gz
    cd libXxf86vm-1.1.4
    build_script_xcb_common
    cd ..
}

build_xkeyboard() {
    rm -rf xkeyboard-config-2.30
    tar -xvf xkeyboard-config-2.30.tar.gz
    cd xkeyboard-config-2.30
    build_script_xcb_common
    cd ..
}

build_xkbcommon() {
    rm -rf libxkbcommon-1.4.0
    tar -xvf libxkbcommon-1.4.0.tar.xz
    cd libxkbcommon-1.4.0
    build_script_xkbcommon
    cd ..
}

build_ice() {
    rm -rf libICE-1.0.10
    tar -xvf libICE-1.0.10.tar.gz
    cd libICE-1.0.10
    build_script_xcb_common
    cd ..
}

build_sm() {
    rm -rf libSM-1.2.3
    tar -xvf libSM-1.2.3.tar.gz
    cd libSM-1.2.3
    build_script_xcb_common
    cd ..
}

build_x11() {
    rm -rf libX11-1.7.5
    tar -xvf libX11-1.7.5.tar.gz
    cd libX11-1.7.5
    build_script_x11
    cd ..
}

# glib
build_pcre() {
    rm -rf pcre-8.45
    tar -xvf pcre-8.45.tar.gz
    cd pcre-8.45
    build_script_xcb_common
    cd ..
}

build_zlib() {
    rm -rf zlib-1.2.11
    tar -xvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    build_script_zlib
    cd ..
}

build_ffi() {
    rm -rf libffi-3.4.3
    tar -xvf libffi-3.4.3.tar.gz
    cd libffi-3.4.3
    build_script_xcb_common
    cd ..
}

build_glib() {
    rm -rf glib-2.61.3
    tar -xvf glib-2.61.3.tar.xz
    cd glib-2.61.3
    build_script_glib
    cd ..
}

# webengine
build_xkbfile() {
    rm -rf libxkbfile-1.1.0
    tar -xvf libxkbfile-1.1.0.tar.gz
    cd libxkbfile-1.1.0
    build_script_xcb_common
    cd ..
}

build_xdamage() {
    rm -rf libXdamage-1.1.5
    tar -xvf libXdamage-1.1.5.tar.gz
    cd libXdamage-1.1.5
    build_script_xcb_common
    cd ..
}

build_expat() {
    rm -rf expat-2.2.9
    tar -xvf expat-2.2.9.tar.gz
    cd expat-2.2.9
    \cp ../config.sub ./conftools/
    build_script_xcb_common
    cd ..
}

build_sepol() {
    rm -rf libsepol-3.0
    tar -xvf libsepol-3.0.tar.gz
    cd libsepol-3.0
    build_script_selinux
    cd ..
}

build_selinux() {
    rm -rf libselinux-3.0
    tar -xvf libselinux-3.0.tar.gz
    cd libselinux-3.0
    build_script_selinux
    cd ..
}

build_dbus() {
    rm -rf dbus-1.13.6
    tar -xvf dbus-1.13.6.tar.gz
    cd dbus-1.13.6
    \cp ../config.sub ./build-aux/
    build_script_xcb_common
    cd ..
}

build_freetype() {
    rm -rf freetype-2.10.1
    tar -xvf freetype-2.10.1.tar.gz
    cd freetype-2.10.1
    \cp ../config.sub ./builds/unix/
    build_script_freetype
    cd ..
}

build_xml2() {
    rm -rf libxml2-v2.9.9
    tar -xvf libxml2-v2.9.9.tar.gz
    cd libxml2-v2.9.9
    ./autogen.sh
    \cp ../config.sub ./
    build_script_xml2
    cd ..
}

build_uuid() {
    rm -rf libuuid-1.0.3
    tar -xvf libuuid-1.0.3.tar.gz
    cd libuuid-1.0.3
    build_script_xcb_common
    cd ..
}

build_fontconfig() {
    rm -rf fontconfig-2.13.92
    tar -xvf fontconfig-2.13.92.tar.gz
    cd fontconfig-2.13.92
    build_script_xcb_common
    cd ..
}

build_sqlite3() {
    rm -rf sqlite-version-3.30.1
    tar -xvf version-3.30.1.tar.gz
    cd sqlite-version-3.30.1
    build_script_xcb_common
    cd ..
}

build_mesa() {
    rm -rf mesa-mesa-19.0.0
    tar -xvf mesa-mesa-19.0.0.tar.gz
    cd mesa-mesa-19.0.0
    build_script_mesa
    cd ..
}

build_icu() {
    rm -rf icu
    tar -xvf icu4c-70rc-src.tgz
    cd icu/source
    \cp ../../config.sub ./
    build_script_icu
    cd ../..
}

build_lcms2() {
    rm -rf Little-CMS-lcms2.10
    tar -xvf lcms2.10.tar.gz
    cd Little-CMS-lcms2.10
    build_script_xcb_common
    cd ..
}

# openssl
build_openssl

# glib
build_pcre
build_zlib
build_ffi
build_sepol
build_selinux
build_glib

# x11
build_xtrans
build_xcb_proto
build_xorgproto
build_Xau
build_xcb
build_pthread
build_xcb_util
build_xcb_util_image
build_xcb_util_keysyms
build_xcb_util_renderutil
build_xcb_util_wm
build_util_macros
build_xkeyboard
build_drm
build_xshmfence
build_xkbcommon
build_ice
build_sm
build_x11

# webengine
build_xrender
build_xext
build_xrandr
build_xfixes
build_xcomposite
build_xcursor
build_xi
build_xscrnsaver
build_xtst
build_xxf86vm
build_xkbfile
build_xdamage
build_expat
build_dbus
build_freetype
build_xml2
build_uuid
build_fontconfig
build_sqlite3
build_mesa
build_icu
build_lcms2

2.5.4. nss交叉编译

webengine 模块需要用到 nssnss 交叉编译比较麻烦,具体步骤参考linux交叉编译nss3,nspr-爱代码爱编程2

  • nss: https://ftp.mozilla.org/pub/security/nss/releases/

  • patch: https://www.linuxfromscratch.org/patches/blfs/12.1/

以下列出需要用到的命令,下面脚本不能直接执行,以参考链接为准。

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
#!/bin/sh
MY_PREFIX=/usr/local
MY_PREFIX_CROSS=/loongarch64
MY_HOST=loongarch64-linux-gnu

# build_init() {c
#     rm -rf nss-3.98
#     tar -xvf nss-3.98-with-nspr-4.25.tar.gz
#     cd nss-3.98
#     patch -Np1 -i ../nss-3.98-standalone-1.patch
#     cp -rf nspr nspr_local
#     cp -rf nss nss_local
# }

build_script_nspr_local() {
    cd nspr_local

    sed -i '/^RELEASE/s|^|#|' pr/src/misc/Makefile.in
    sed -i 's|$(LIBRARY) ||' config/rules.mk

    ./configure \
        --with-pthreads \
        --enable-64bit \
        --with-mozilla

    make -j1 && make install
}

build_script_nspr() {
    cd nspr
    \cp ../../config.sub ./build/autoconf/

    sed -i '/^RELEASE/s|^|#|' pr/src/misc/Makefile.in
    sed -i 's|$(LIBRARY) ||' config/rules.mk

    ./configure \
        --prefix=${MY_PREFIX_CROSS} \
        --host=${MY_HOST} \
        --with-pthreads \
        --with-mozilla \
        --enable-64bit

    make -j1 && make install
    # cp ./nspr_local/config/nsinstall ./nspr/config/
}

build_script_nss_local() {
    cd nss_local

    make -j1 \
        BUILD_OPT=1 \
        NSPR_INCLUDE_DIR=${MY_PREFIX}/include/nspr \
        NSPR_LIB_DIR=${MY_PREFIX}/lib \
        USE_STATIC_RTL=1 \
        USE_SYSTEM_ZLIB=1 \
        ZLIB_LIBS=-lz \
        NSS_ENABLE_WERROR=0 \
        $([ -f ${MY_PREFIX}/include/sqlite3.h ] && echo NSS_USE_SYSTEM_SQLITE=1) \
        $([ $(uname -m) = x86_64 ] && echo USE_64=1)
}

build_script_nss() {
    cd nss

    export CC=${MY_HOST}-gcc
    export CCC=${MY_HOST}-g++
    export C_INCLUDE_PATH=${MY_PREFIX_CROSS}/include:$C_INCLUDE_PATH
    export LD_LIBRARY_PATH=${MY_PREFIX_CROSS}/lib:$LD_LIBRARY_PATH

    make -j1 \
        BUILD_OPT=1 \
        NSPR_INCLUDE_DIR=${MY_PREFIX_CROSS}/include/nspr \
        NSPR_LIB_DIR=${MY_PREFIX_CROSS}/lib \
        USE_STATIC_RTL=1 \
        USE_SYSTEM_ZLIB=1 \
        ZLIB_LIBS=-lz \
        NSS_ENABLE_WERROR=0 \
        $([ -f ${MY_PREFIX_CROSS}/include/sqlite3.h ] && echo NSS_USE_SYSTEM_SQLITE=1) \
        USE_64=1 \
        CROSS_COMPILE=1 \
        PREFIX=${MY_PREFIX_CROSS} \
        OS_TEST=loongarch64

    cd ..
    # cp ./nss_local/coreconf/nsinstall/Linux3.10_x86_64_cc_glibc_PTH_64_OPT.OBJ/nsinstall ./nss/coreconf/nsinstall/Linux3.10_loongarch64_glibc_PTH_64_OPT.OBJ/
}

install_nss() {
    cd ./dist/Linux3.10_loongarch64_glibc_PTH_64_OPT.OBJ

    install -v -m755 lib/*.so ${MY_PREFIX_CROSS}/lib
    install -v -m644 lib/{*.chk,libcrmf.a} ${MY_PREFIX_CROSS}/lib
    install -v -m755 -d ${MY_PREFIX_CROSS}/include/nss
    cp -v -RL ../{public,private}/nss/* ${MY_PREFIX_CROSS}/include/nss
    install -v -m755 bin/{certutil,nss-config,pk12util} ${MY_PREFIX_CROSS}/bin
    install -v -m644 lib/pkgconfig/nss.pc ${MY_PREFIX_CROSS}/lib/pkgconfig
    
    cd ..
}

# 编译 nss
# build_init
build_script_nspr_local
build_script_nspr

build_script_nss_local
build_script_nss

install_nss

2.5.5. nodejs下载

nodejs 不用交叉编译,下载创建链接即可。

1
2
3
4
5
6
7
# 下载 nodejs
wget -c https://registry.npmmirror.com/-/binary/node/v10.24.1/node-v10.24.1-linux-x64.tar.gz
tar -xvf node-v10.24.1-linux-x64.tar.gz
mv node-v10.24.1-linux-x64 /usr/local/nodejs
# 建立node npm软链接
ln -s /usr/local/nodejs/bin/node /usr/local/bin
ln -s /usr/local/nodejs/bin/npm /usr/local/bin

2.5.6. 检查编译3

文件都放在 /cross/dependence

以下脚本 check_dependence.sh 是检查是否能被检索到。

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
#!/bin/bash
PRE_PATH=/loongarch64

export PKG_CONFIG_PATH=${PRE_PATH}/lib/pkgconfig:${PRE_PATH}/share/pkgconfig

x11string="xrender,xcb-render,xcb-renderutil,xcb-shape,xcb-randr,xcb-xfixes,xcb-sync,xcb-shm,xcb-icccm,xcb-keysyms,xcb-image,xkbcommon,xkbcommon-x11,fontconfig,freetype2,xext,x11,xcb,x11-xcb,sm,ice,glib-2.0,xcb-xinerama"
x11array=(${x11string//,/ })

webstring="nss,libdrm,xcomposite,xcursor,xi,xrandr,xscrnsaver,xtst,xkbfile,expat,xdamage"
webarray=(${webstring//,/ })

allCount=0
successCount=0
failedStr=""

echo "X11依赖:"
for var in ${x11array[@]}; do
    let allCount++
    modversion="$(pkg-config --modversion --silence-errors $var)"

    if [[ -n $modversion ]]; then
        let successCount++
        echo $var "库版本号" $modversion
    else
        failedStr=$failedStr$var,
        echo $var "未找到库!!!"
    fi
done

echo
echo "WebEngine依赖:"
for var in ${webarray[@]}; do
    let allCount++
    modversion="$(pkg-config --modversion --silence-errors $var)"

    if [[ -n $modversion ]]; then
        let successCount++
        echo $var "库版本号" $modversion
    else
        failedStr=$failedStr$var,
        echo $var "未找到库!!!"
    fi
done

echo
echo "成功 / 总数:"$successCount/$allCount
if [ $allCount -ne $successCount ]; then
    echo "未找到的库 :"$failedStr
fi

运行上述脚本全部找到应该就没问题。

3. 编译Qt

下载地址:https://download.qt.io/archive/qt/5.15/5.15.2/single/

首先解压,以后所有操作都在 /cross/loongarch64-qt-everywhere-src-5.15.2 中操作

1
2
3
tar -xvf qt-everywhere-src-5.15.2.tar.xz
mv qt-everywhere-src-5.15.2 loongarch64-qt-everywhere-src-5.15.2
cd loongarch64-qt-everywhere-src-5.15.2

3.1. 环境配置

loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3 中包含 sysroot,为了路径简短,直接复制到根目录。

1
cp -rf loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/loongarch64-linux-gnu/sysroot/ /sysroot-loongarch64

因为要配置 sysroot,所有查找路径都会加上 ${sysroot} 前缀,所以需要把上述所有依赖安装路径 loongarch64 文件夹拷贝到 sysroot-loongarch64 目录下。将原来 loongarch64 改名,不然 qt 后续编译 webengine 本地链接 nss 会找到这里面。

1
2
3
4
5
6
# 移动和改名
cp -rf /loongarch64 /sysroot-loongarch64/
mv /loongarch64 /loongarch641

# 或者直接移动
mv /loongarch64/ /sysroot-loongarch64/

不知道为什么 webengine 模块不能识别编译脚本中的 export,所以需要在 /etc/profile 设置

1
2
3
4
5
6
7
8
9
10
11
12
vim /etc/profile

# webengine
MY_SYSROOT=/sysroot-loongarch64
MY_PREFIX=${MY_SYSROOT}/loongarch64
export PKG_CONFIG_PATH=${MY_PREFIX}/lib/pkgconfig:${MY_PREFIX}/share/pkgconfig
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${MY_PREFIX}/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${MY_PREFIX}/include/nss
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${MY_PREFIX}/include/nspr
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${MY_PREFIX}/lib

source /etc/profile

3.2. 添加交叉编译平台

所有默认的交叉编译平台配置文件在 /qt-everywhere-src-5.15.2/qtbase/mkspecs,为了不影响文件,可以复制 linux-aarch64-gnu-g++loongarch64-linux-gnu-g++ 作为我们的配置文件。并修改其中的 qmake.conf 文件如下:

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
#
# qmake configuration for building with aarch64-linux-gnu-g++
#

MAKEFILE_GENERATOR      = UNIX
CONFIG                 += incremental
QMAKE_INCREMENTAL_STYLE = sublib

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

QMAKE_INCDIR_POST += \ 
    $$[QT_SYSROOT]/usr/include \
    $$[QT_SYSROOT]/loongarch64/include

QMAKE_RPATHLINKDIR_POST += \ 
    $$[QT_SYSROOT]/usr/lib \
    $$[QT_SYSROOT]/loongarch64/lib \
    $$[QT_SYSROOT]/loongarch64/lib64

# modifications to g++.conf
QMAKE_CC                = loongarch64-linux-gnu-gcc
QMAKE_CXX               = loongarch64-linux-gnu-g++
QMAKE_LINK              = loongarch64-linux-gnu-g++
QMAKE_LINK_SHLIB        = loongarch64-linux-gnu-g++

# modifications to linux.conf
QMAKE_AR                = loongarch64-linux-gnu-ar cqs
QMAKE_OBJCOPY           = loongarch64-linux-gnu-objcopy
QMAKE_NM                = loongarch64-linux-gnu-nm -P
QMAKE_STRIP             = loongarch64-linux-gnu-strip
load(qt_config)

3.3. qtwebengine

1
cd loongarch64-qt-everywhere-src-5.15.2/qtwebengine

qt5.15.2 该模块不支持 loongarch64 架构,需要下载补丁包。

1
2
3
4
5
6
7
8
9
10
11
# 源地址为:https://pkg.loongnix.cn/loongnix/pool/main/q/qtwebengine-opensource-src/

# 下载使用方法:
wget https://pkg.loongnix.cn/loongnix/pool/main/q/qtwebengine-opensource-src/qtwebengine-opensource-src_5.15.2+dfsg-lnd.3.dsc
wget https://pkg.loongnix.cn/loongnix/pool/main/q/qtwebengine-opensource-src/qtwebengine-opensource-src_5.15.2+dfsg.orig.tar.xz
wget https://pkg.loongnix.cn/loongnix/pool/main/q/qtwebengine-opensource-src/qtwebengine-opensource-src_5.15.2+dfsg-lnd.3.debian.tar.xz

# 解压源码方法: 这个命令 CentOS 没有
dpkg-source -x qtwebengine-opensource-src_5.15.2+dfsg-lnd.3.dsc

# 关于loongarch的patch位于:debian/patches目录下的0001-》0016前缀相关的都为loongarch相关patch。

直接解压 qtwebengine-opensource-src_5.15.2+dfsg-lnd.3.debian.tar.xz 即可,里面 debian/patches 包含全部补丁,全部打上即可。脚本 patches.sh 如下

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
#!/bin/sh
patch -Np1 -i debian/patches/0001-port-chromium_qt-to-loongarch64.patch
patch -Np1 -i debian/patches/0002-fix-third_party-for-loongarch64.patch
patch -Np1 -i debian/patches/0003-port-breakpad-for-loongarch64.patch
patch -Np1 -i debian/patches/0004-port-ffmpeg-to-loongarch64-for-chromium.patch
patch -Np1 -i debian/patches/0005-port-ffmpeg-to-loongarch64-for-chromium-add-la64-rel.patch
patch -Np1 -i debian/patches/0006-fix-third_party-for-loongarch64-add-files-for-la64.patch
patch -Np1 -i debian/patches/0007-port-icu-for-loongarch64.patch
patch -Np1 -i debian/patches/0008-port-lss-for-loongarch64.patch
patch -Np1 -i debian/patches/0009-port-pdfium-for-loongarch64.patch
patch -Np1 -i debian/patches/0010-port-swiftshader-for-loongarch64.patch
patch -Np1 -i debian/patches/0011-port-webrtc-for-loongarch64.patch
patch -Np1 -i debian/patches/0012-port-v8-for-loongarch64.patch
patch -Np1 -i debian/patches/0013-make-qtwebengine-can-be-compiled-for-loongarch64.patch
patch -Np1 -i debian/patches/0014-fix-compile-errors-for-mips64el.patch
patch -Np1 -i debian/patches/0015-fix-compiler-internal-error-for-loongarch64.patch
patch -Np1 -i debian/patches/0016-fix-compile-error-for-loongarch64.patch
patch -Np1 -i debian/patches/mipsel-code-range-size.patch
patch -Np1 -i debian/patches/mipsel-link-atomic.patch
patch -Np1 -i debian/patches/mipsel-linux-5.patch
patch -Np1 -i debian/patches/mipsel-no-dav1d.patch
patch -Np1 -i debian/patches/mipsel-ptrace-include.patch
patch -Np1 -i debian/patches/run-unbundling-script.patch
patch -Np1 -i debian/patches/sandbox-time64-syscalls.patch
patch -Np1 -i debian/patches/series
patch -Np1 -i debian/patches/system-icu-utf.patch
patch -Np1 -i debian/patches/system-lcms2.patch
patch -Np1 -i debian/patches/system-nspr-prtime.patch
patch -Np1 -i debian/patches/verbose-gn-bootstrap.patch

因为这是源码,所以有些补丁失败了,后面进行手动修改,使用配套 qt 应该没问题。

3.4. 编译脚本

为了不污染源代码,所以脚本 loongarch64_qt.sh 创建 build 文件夹进行编译。

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
#!/bin/sh
mkdir build_loongarch64
cd build_loongarch64

MY_SYSROOT=/sysroot-loongarch64
MY_PREFIX=${MY_SYSROOT}/loongarch64
MY_XPLATFORM=loongarch64-linux-gnu-g++

export PKG_CONFIG_LIBDIR=${MY_PREFIX}/lib/pkgconfig:${MY_PREFIX}/share/pkgconfig
export PKG_CONFIG_PATH=${MY_PREFIX}/lib/pkgconfig:${MY_PREFIX}/share/pkgconfig

../configure \
    -prefix /opt/qt5.15.2 \
    -confirm-license \
    -opensource \
    -release \
    -sysroot ${MY_SYSROOT} \
    -pkg-config \
    -xplatform ${MY_XPLATFORM} \
    -I${MY_PREFIX}/include \
    -L${MY_PREFIX}/lib \
    -make libs \
    -nomake examples \
    -nomake tools \
    -nomake tests \
    -xcb \
    -dbus \
    -fontconfig

cd ..

执行命令 ./loongarch64_qt.sh,看到以下情况即是包含 OpenSSL X11 QWebEngine,就可以开始编译。

1
2
3
4
5
cd build
# 编译
gmake -j4
# 安装,路径是 /sysroot-loongarch64/opt/qt5.15.2
gmake install

3.5. 问题

无关模块问题最好将 build 文件夹删除重新构建,不然基础上构建会导致部分头文件重复定义。

3.5.1. 问题一

QtWebEngine 架构不支持,qtwebengine/configure.pri 文件已经打上补丁

1
2
3
4
5
6
7
8
9
# 142
defineTest(qtConfTest_detectArch) {
    contains(QT_ARCH, "i386")|contains(QT_ARCH, "x86_64"): return(true)
    contains(QT_ARCH, "arm")|contains(QT_ARCH, "arm64"): return(true)
    contains(QT_ARCH, "mips")|contains(QT_ARCH, "mips64"): return(true)
    contains(QT_ARCH, "loongarch")|contains(QT_ARCH, "loongarch64"): return(true)
    qtLog("Architecture not supported.")
    return(false)
}

但是 loongarch64-linux-gnu-gccqt 进行代码测试时获取不到架构名称,需要直接指定,直接修改 /qtbase/configure.pri

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
# 983
defineTest(qtConfOutput_architecture) {
    #arch = $$qtConfEvaluate("tests.architecture.arch")
    arch = loongarch64
    subarch = $$qtConfEvaluate('tests.architecture.subarch')
    #buildabi = $$qtConfEvaluate("tests.architecture.buildabi")
    buildabi = loongarch64-linux-gnu

    $$qtConfEvaluate("features.cross_compile") {
        host_arch = $$qtConfEvaluate("tests.host_architecture.arch")
        host_buildabi = $$qtConfEvaluate("tests.host_architecture.buildabi")

        privatePro = \
            "host_build {" \
            "    QT_CPU_FEATURES.$$host_arch = $$qtConfEvaluate('tests.host_architecture.subarch')" \
            "} else {" \
            "    QT_CPU_FEATURES.$$arch = $$subarch" \
            "}"
        publicPro = \
            "host_build {" \
            "    QT_ARCH = $$host_arch" \
            "    QT_BUILDABI = $$host_buildabi" \
            "    QT_TARGET_ARCH = $$arch" \
            "    QT_TARGET_BUILDABI = $$buildabi" \
            "} else {" \
            "    QT_ARCH = $$arch" \
            "    QT_BUILDABI = $$buildabi" \
            "}"

    } else {
        privatePro = \
            "QT_CPU_FEATURES.$$arch = $$subarch"
        publicPro = \
            "QT_ARCH = $$arch" \
            "QT_BUILDABI = $$buildabi"
    }

    $${currentConfig}.output.publicPro += $$publicPro
    export($${currentConfig}.output.publicPro)
    $${currentConfig}.output.privatePro += $$privatePro
    export($${currentConfig}.output.privatePro)

    # setup QT_ARCH and QT_CPU_FEATURES variables used by qtConfEvaluate
    QT_ARCH = $$arch
    export(QT_ARCH)
    QT_CPU_FEATURES.$$arch = $$subarch
    export(QT_CPU_FEATURES.$$arch)
}

3.5.2. 问题二

1
#error Target architecture was not detected as supported by Double-Conversion.

未打上补丁地方之一,在 ./qtbase/src/3rdparty/double-conversion/include/double-conversion/utils.h 修改

1
2
3
4
// 96
defined(__mips__) || \
// 改为
defined(__mips__) || defined(__loongarch__) || \

3.5.3. 问题三

1
2
/cross/loongarch64-qt-everywhere-src-5.15.2/qtscript/src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h:493:57: error: cast from 'QTJSC::JSCell*' to 'int32_t' {aka 'int'} loses precision [-fpermissive]
         u.asBits.payload = reinterpret_cast<int32_t>(ptr);

根据提示修改 /qtscript/src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.hint32_t 改为 int64_t

1
2
3
4
// 493
u.asBits.payload = reinterpret_cast<int64_t>(ptr);
// 505
u.asBits.payload = reinterpret_cast<int64_t>(const_cast<JSCell*>(ptr));

3.5.4. 问题四

1
2
ERROR at //build/config/posix/BUILD.gn:37:11: Script returned non-zero exit code.
          exec_script("//build/linux/sysroot_scripts/install-sysroot.py",

这个是没有对应的架构适配,直接修改 /qtwebengine/src/3rdparty/chromium/build/linux/sysroot_scripts/install-sysroot.py添加 la64 相关信息

1
2
3
4
5
6
7
8
9
10
# 44
VALID_ARCHS = ('arm', 'arm64', 'i386', 'amd64', 'mips', 'mips64el', 'la64')

ARCH_TRANSLATIONS = {
    'x64': 'amd64',
    'x86': 'i386',
    'mipsel': 'mips',
    'mips64': 'mips64el',
    'la64': 'mips64el',
}

3.5.5. 问题五

1
2
3
4
5
6
FAILED: obj/third_party/dav1d/dav1d/cdf.o 
/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin/loongarch64-linux-gnu-gcc -MMD -MF obj/third_party/dav1d/dav1d/cdf.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_NSS_CERTS=1 -DUSE_OZONE=1 -DOFFICIAL_BUILD -DTOOLKIT_QT -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -DNO_UNWIND_TABLES -DCR_SYSROOT_HASH=4c1f3e3f4a11f820e152faffc49ccdfa1c435cc0 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DCONFIG_LOG=0 -DDAV1D_API= -DCONFIG_LOG=0 -DDAV1D_API= -Igen -I../../../../../qtwebengine/src/3rdparty/chromium -I../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/libdav1d -I../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/libdav1d/include -I../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/libdav1d/include/dav1d -I../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/config/linux/la64 -I../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/version -I../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/version -D_FILE_OFFSET_BITS=64 -D_POSIX_C_SOURCE=200112L -std=c99 -D_GNU_SOURCE -mcmodel=large -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fPIC -pipe -pthread -O2 -fno-ident -fdata-sections -ffunction-sections -fno-omit-frame-pointer -g0 -fvisibility=hidden -Wno-unused-local-typedefs -Wno-maybe-uninitialized -Wno-deprecated-declarations -fno-delete-null-pointer-checks -Wno-comments -Wno-packed-not-aligned -Wno-dangling-else -Wno-missing-field-initializers -Wno-unused-parameter -std=gnu11 --sysroot=../../../../../../../sysroot-loongarch64 -c ../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/libdav1d/src/cdf.c -o obj/third_party/dav1d/dav1d/cdf.o
../../../../../qtwebengine/src/3rdparty/chromium/third_party/dav1d/libdav1d/src/cdf.c:28:10: fatal error: config.h: No such file or directory
 #include "config.h"
          ^~~~~~~~~~
compilation terminated.

未打上补丁地方之一,loongarch64 不支持 dav1d,修改 /qtwebengine/src/3rdparty/chromium/media/media_options.gni

1
2
// 96
enable_dav1d_decoder = !is_android && !is_ios && target_cpu != "mips64el" && target_cpu != "mipsel" && target_cpu != "la64"

3.5.6. 问题六

1
2
../../../../../qtwebengine/src/3rdparty/chromium/sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.cc:47:10: fatal error: asm/ptrace-abi.h: No such file or directory
 #include <asm/ptrace-abi.h>

未打上补丁地方之一,修改 /qtwebengine/src/3rdparty/chromium/sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.cc

1
2
// 44
#if defined(__mips__) || defined(__loongarch64)

4. 验证

经过上述步骤就能获得 loongarch64 版本的 qt5.15.2,接下来是验证是否可以将程序编译成 loongarch64 版本,主要验证 webengine 模块功能。

下载安装 qt-opensource-linux-x64-5.14.2.run 最后一个离线安装版本:https://download.qt.io/archive/qt/5.14/5.14.2/。

添加交叉编译版本 qmake 路径,不然不能加载

1
2
3
4
5
6
vim /etc/profile

# qmake-loongarch64
export PATH=$PATH:/sysroot-loongarch64/opt/qt5.15.2/bin

source /etc/profile

4.1. 添加构建环境

在工具——选项——Kits——Qt Versions中点击添加,选择交叉编译生成的 qmake,配置了环境变量交叉编译器一般会自动加入。

然后在构建套件中添加一个交叉编译 loongarch64 版本,参数如下:

使用 simplebrowser.pro 示例进行测试。可以看到最终编译结果没有版本显示,但是实际就是 loongarch64

  • 在对应平台查看可能显示 unknown arch 0x102,这是因为 LoongArch 的二进制编号为 258(0x102)
  • 较新的 file 命令才添加了对 LoongArch 的支持。

5. 总结

5.1. /etc/profile

最后 /etc/profile 文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# gcc,放前面覆盖原始 gcc 路径
export PATH=/usr/local/gcc/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/gcc/lib64:$LD_LIBRARY_PATH
# gcc-loongarch64
export PATH=$PATH:/cross/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3/bin

# webengine
MY_SYSROOT=/sysroot-loongarch64
MY_PREFIX=${MY_SYSROOT}/loongarch64
export PKG_CONFIG_PATH=${MY_PREFIX}/lib/pkgconfig:${MY_PREFIX}/share/pkgconfig
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${MY_PREFIX}/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${MY_PREFIX}/include/nss
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:${MY_PREFIX}/include/nspr
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${MY_PREFIX}/lib

# qmake-loongarch64
export PATH=$PATH:${MY_SYSROOT}/opt/qt5.15.2/bin

参考

本文由作者按照 CC BY 4.0 进行授权