zl程序教程

您现在的位置是:首页 >  工具

当前栏目

QEMU零知识学习4 —— QEMU编译

学习 编译 知识 QEMU
2023-09-14 09:15:40 时间

接前一篇文章《QEMU零知识学习3 —— QEMU配置》,链接如下:

QEMU零知识学习3 —— QEMU配置_蓝天居士的博客-CSDN博客

编译QEMU

经过配置之后,编译就很简单了,直接执行make即可。本文仍然分为带“--target-list=x86_64-softmmu”选项和不带此选项两种情况进行介绍。

  • 带“--target-list=x86_64-softmmu”选项的编译

编译过程和结果如下所示:

$ make
changing dir to build for make ""...
make[1]: 进入目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”
/usr/bin/ninja  build.ninja && touch build.ninja.stamp
ninja: no work to do.
/usr/bin/meson introspect --targets --tests --benchmarks | /usr/bin/python3 -B scripts/mtest2make.py > Makefile.mtest
[1/2623] Generating config-poison.h with a custom command (wrapped by meson to capture output)
[2/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt.c.o
[3/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_ro.c.o
[4/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_wip.c.o
[5/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_sw.c.o
[6/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_rw.c.o
[7/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_strerror.c.o
[8/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_empty_tree.c.o
[9/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_addresses.c.o
[10/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_overlay.c.o
[11/2623] Compiling C object libfdt.a.p/dtc_libfdt_fdt_check.c.o
[12/2623] Linking static target libfdt.a
[13/2623] Generating qemu-version.h with a custom command (wrapped by meson to capture output)
[14/2623] Generating qemu-options.def with a custom command (wrapped by meson to capture output)
[15/2623] Generating qemu-img-cmds.h with a custom command (wrapped by meson to capture output)
[16/2623] Generating hmp-commands.h with a custom command (wrapped by meson to capture output)
[17/2623] Generating hmp-commands-info.h with a custom command (wrapped by meson to capture output)
[18/2623] Compiling C object subprojects/libvhost-user/libvhost-user.a.p/libvhost-user.c.o
[19/2623] Linking static target subprojects/libvhost-user/libvhost-user.a
[20/2623] Compiling C object subprojects/libvhost-user/link-test.p/link-test.c.o
[21/2623] Linking target subprojects/libvhost-user/link-test
[22/2623] Compiling C object subprojects/libvhost-user/libvhost-user-glib.a.p/libvhost-user-glib.c.o
[23/2623] Linking static target subprojects/libvhost-user/libvhost-user-glib.a
[24/2623] Compiling C object subprojects/libvduse/libvduse.a.p/libvduse.c.o
[25/2623] Linking static target subprojects/libvduse/libvduse.a
[26/2623] Generating qapi/shared QAPI source files with a custom command
[27/2623] Generating trace/trace-root.h with a custom command
[28/2623] Generating trace/trace-root.c with a custom command
[29/2623] Generating trace/trace-crypto.h with a custom command
[30/2623] Generating trace/trace-crypto.c with a custom command
[31/2623] Generating trace/trace-qapi.h with a custom command
[32/2623] Generating trace/trace-qapi.c with a custom command
[33/2623] Generating trace/trace-qom.h with a custom command
[34/2623] Generating trace/trace-qom.c with a custom command
[35/2623] Generating trace/trace-monitor.h with a custom command
[36/2623] Generating trace/trace-monitor.c with a custom command
[37/2623] Generating trace/trace-util.h with a custom command
[38/2623] Generating trace/trace-util.c with a custom command
[39/2623] Generating trace/trace-gdbstub.h with a custom command
[40/2623] Generating trace/trace-gdbstub.c with a custom command
[41/2623] Generating trace/trace-authz.h with a custom command
[42/2623] Generating trace/trace-authz.c with a custom command
[43/2623] Generating trace/trace-block.h with a custom command
[44/2623] Generating trace/trace-block.c with a custom command
[45/2623] Generating trace/trace-io.h with a custom command
[46/2623] Generating trace/trace-io.c with a custom command
[47/2623] Generating trace/trace-nbd.h with a custom command
[48/2623] Generating trace/trace-nbd.c with a custom command
[49/2623] Generating trace/trace-scsi.h with a custom command
[50/2623] Generating trace/trace-scsi.c with a custom command
[51/2623] Generating trace/trace-accel_kvm.h with a custom command
[52/2623] Generating trace/trace-accel_kvm.c with a custom command
[53/2623] Generating trace/trace-audio.h with a custom command
[54/2623] Generating trace/trace-audio.c with a custom command
[55/2623] Generating trace/trace-backends.h with a custom command
[56/2623] Generating trace/trace-backends.c with a custom command
[57/2623] Generating trace/trace-backends_tpm.h with a custom command
[58/2623] Generating trace/trace-backends_tpm.c with a custom command
[59/2623] Generating trace/trace-chardev.h with a custom command
[60/2623] Generating trace/trace-chardev.c with a custom command
[61/2623] Generating trace/trace-ebpf.h with a custom command
[62/2623] Generating trace/trace-ebpf.c with a custom command
[63/2623] Generating trace/trace-hw_9pfs.h with a custom command
[64/2623] Generating trace/trace-hw_9pfs.c with a custom command
[65/2623] Generating trace/trace-hw_acpi.h with a custom command
[66/2623] Generating trace/trace-hw_acpi.c with a custom command
[67/2623] Generating trace/trace-hw_adc.h with a custom command
[68/2623] Generating trace/trace-hw_adc.c with a custom command
[69/2623] Generating trace/trace-hw_alpha.h with a custom command
[70/2623] Generating trace/trace-hw_alpha.c with a custom command
[71/2623] Generating trace/trace-hw_arm.h with a custom command
[72/2623] Generating trace/trace-hw_arm.c with a custom command
[73/2623] Generating trace/trace-hw_audio.h with a custom command
[74/2623] Generating trace/trace-hw_audio.c with a custom command
[75/2623] Generating trace/trace-hw_block.h with a custom command
[76/2623] Generating trace/trace-hw_block.c with a custom command
[77/2623] Generating trace/trace-hw_block_dataplane.h with a custom command
[78/2623] Generating trace/trace-hw_block_dataplane.c with a custom command
[79/2623] Generating trace/trace-hw_char.h with a custom command
[80/2623] Generating trace/trace-hw_char.c with a custom command
[81/2623] Generating trace/trace-hw_display.h with a custom command
[82/2623] Generating trace/trace-hw_display.c with a custom command
[83/2623] Generating trace/trace-hw_dma.h with a custom command
[84/2623] Generating trace/trace-hw_dma.c with a custom command
[85/2623] Generating trace/trace-hw_hyperv.h with a custom command
[86/2623] Generating trace/trace-hw_hyperv.c with a custom command
[87/2623] Generating trace/trace-hw_i2c.h with a custom command
[88/2623] Generating trace/trace-hw_i2c.c with a custom command
[89/2623] Generating trace/trace-hw_i386.h with a custom command
[90/2623] Generating trace/trace-hw_i386.c with a custom command
[91/2623] Generating trace/trace-hw_i386_xen.h with a custom command
[92/2623] Generating trace/trace-hw_i386_xen.c with a custom command
[93/2623] Generating trace/trace-hw_ide.h with a custom command
[94/2623] Generating trace/trace-hw_ide.c with a custom command
[95/2623] Generating trace/trace-hw_input.h with a custom command
[96/2623] Generating trace/trace-hw_input.c with a custom command
[97/2623] Generating trace/trace-hw_intc.h with a custom command
[98/2623] Generating trace/trace-hw_intc.c with a custom command
[99/2623] Generating trace/trace-hw_isa.h with a custom command
[100/2623] Generating trace/trace-hw_isa.c with a custom command
[101/2623] Generating trace/trace-hw_mem.h with a custom command
[102/2623] Generating trace/trace-hw_mem.c with a custom command
[103/2623] Generating trace/trace-hw_mips.h with a custom command
[104/2623] Generating trace/trace-hw_mips.c with a custom command
[105/2623] Generating trace/trace-hw_misc.h with a custom command
[106/2623] Generating trace/trace-hw_misc.c with a custom command
[107/2623] Generating trace/trace-hw_misc_macio.h with a custom command
[108/2623] Generating trace/trace-hw_misc_macio.c with a custom command
[109/2623] Generating trace/trace-hw_net.h with a custom command
[110/2623] Generating trace/trace-hw_net.c with a custom command
[111/2623] Generating trace/trace-hw_net_can.h with a custom command
[112/2623] Generating trace/trace-hw_net_can.c with a custom command
[113/2623] Generating trace/trace-hw_nubus.h with a custom command
[114/2623] Generating trace/trace-hw_nubus.c with a custom command
[115/2623] Generating trace/trace-hw_nvme.h with a custom command
[116/2623] Generating trace/trace-hw_nvme.c with a custom command
[117/2623] Generating trace/trace-hw_nvram.h with a custom command
[118/2623] Generating trace/trace-hw_nvram.c with a custom command
[119/2623] Generating trace/trace-hw_pci.h with a custom command
[120/2623] Generating trace/trace-hw_pci.c with a custom command
[121/2623] Generating trace/trace-hw_pci_host.h with a custom command
[122/2623] Generating trace/trace-hw_pci_host.c with a custom command
[123/2623] Generating trace/trace-hw_ppc.h with a custom command
[124/2623] Generating trace/trace-hw_ppc.c with a custom command
[125/2623] Generating trace/trace-hw_rdma.h with a custom command
[126/2623] Generating trace/trace-hw_rdma.c with a custom command
[127/2623] Generating trace/trace-hw_rdma_vmw.h with a custom command
[128/2623] Generating trace/trace-hw_rdma_vmw.c with a custom command
[129/2623] Generating trace/trace-hw_rtc.h with a custom command
[130/2623] Generating trace/trace-hw_rtc.c with a custom command
[131/2623] Generating trace/trace-hw_s390x.h with a custom command
[132/2623] Generating trace/trace-hw_s390x.c with a custom command
[133/2623] Generating trace/trace-hw_scsi.h with a custom command
[134/2623] Generating trace/trace-hw_scsi.c with a custom command
[135/2623] Generating trace/trace-hw_sd.h with a custom command
[136/2623] Generating trace/trace-hw_sd.c with a custom command
[137/2623] Generating trace/trace-hw_sh4.h with a custom command
[138/2623] Generating trace/trace-hw_sh4.c with a custom command
[139/2623] Generating trace/trace-hw_sparc.h with a custom command
[140/2623] Generating trace/trace-hw_sparc.c with a custom command
[141/2623] Generating trace/trace-hw_sparc64.h with a custom command
[142/2623] Generating trace/trace-hw_sparc64.c with a custom command
[143/2623] Generating trace/trace-hw_ssi.h with a custom command
[144/2623] Generating trace/trace-hw_ssi.c with a custom command
[145/2623] Generating trace/trace-hw_timer.h with a custom command
[146/2623] Generating trace/trace-hw_timer.c with a custom command
[147/2623] Generating trace/trace-hw_tpm.h with a custom command
[148/2623] Generating trace/trace-hw_tpm.c with a custom command
[149/2623] Generating trace/trace-hw_usb.h with a custom command
[150/2623] Generating trace/trace-hw_usb.c with a custom command

……

[2450/2474] Compiling C object tests/qtest/qos-test.p/emc141x-test.c.o
[2451/2474] Compiling C object tests/qtest/qos-test.p/usb-hcd-ohci-test.c.o
[2452/2474] Compiling C object tests/qtest/qos-test.p/virtio-test.c.o
[2453/2474] Compiling C object tests/qtest/qos-test.p/virtio-blk-test.c.o
[2454/2474] Compiling C object tests/qtest/qos-test.p/virtio-net-test.c.o
[2455/2474] Compiling C object tests/qtest/qos-test.p/virtio-rng-test.c.o
[2456/2474] Compiling C object tests/qtest/qos-test.p/virtio-scsi-test.c.o
[2457/2474] Compiling C object tests/qtest/qos-test.p/virtio-serial-test.c.o
[2458/2474] Compiling C object tests/qtest/qos-test.p/virtio-iommu-test.c.o
[2459/2474] Compiling C object tests/qtest/qos-test.p/vmxnet3-test.c.o
[2460/2474] Compiling C object tests/qtest/qos-test.p/e1000e-test.c.o
[2461/2474] Compiling C object tests/qtest/qos-test.p/vhost-user-test.c.o
[2462/2474] Compiling C object tests/qtest/qos-test.p/vhost-user-blk-test.c.o
[2463/2474] Linking target tests/qtest/qos-test
[2464/2474] Compiling C object tests/qtest/readconfig-test.p/readconfig-test.c.o
[2465/2474] Linking target tests/qtest/readconfig-test
[2466/2474] Generating po/bg/LC_MESSAGES/qemu-bg.mo with a custom command
[2467/2474] Generating po/de_DE/LC_MESSAGES/qemu-de_DE.mo with a custom command
[2468/2474] Generating po/fr_FR/LC_MESSAGES/qemu-fr_FR.mo with a custom command
[2469/2474] Generating po/hu/LC_MESSAGES/qemu-hu.mo with a custom command
[2470/2474] Generating po/it/LC_MESSAGES/qemu-it.mo with a custom command
[2471/2474] Generating po/sv/LC_MESSAGES/qemu-sv.mo with a custom command
[2472/2474] Generating po/tr/LC_MESSAGES/qemu-tr.mo with a custom command
[2473/2474] Generating po/uk/LC_MESSAGES/qemu-uk.mo with a custom command
[2474/2474] Generating po/zh_CN/LC_MESSAGES/qemu-zh_CN.mo with a custom command
make[1]: 离开目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”
changing dir to build for make ""...
make[1]: 进入目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”
[1/50] Generating qemu-version.h with a custom command (wrapped by meson to capture output)
[2/34] Generating tests/include/QAPI test (include) with a custom command
make[1]: 离开目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”

在笔者的电脑上,整个过程花费了大约10~15分钟。

  • 不带“--target-list=x86_64-softmmu”选项的编译

编译过程和结果如下所示:

$ make
changing dir to build for make ""...
make[1]: 进入目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”
/usr/bin/ninja  build.ninja && touch build.ninja.stamp
ninja: no work to do.
/usr/bin/meson introspect --targets --tests --benchmarks | /usr/bin/python3 -B scripts/mtest2make.py > Makefile.mtest
[1/9862] Generating config-poison.h with a custom command (wrapped by meson to capture output)
[2/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt.c.o
[3/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_ro.c.o
[4/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_wip.c.o
[5/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_sw.c.o
[6/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_rw.c.o
[7/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_strerror.c.o
[8/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_empty_tree.c.o
[9/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_addresses.c.o
[10/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_overlay.c.o
[11/9862] Compiling C object libfdt.a.p/dtc_libfdt_fdt_check.c.o
[12/9862] Linking static target libfdt.a
[13/9862] Generating qemu-version.h with a custom command (wrapped by meson to capture output)
[14/9862] Generating qemu-options.def with a custom command (wrapped by meson to capture output)
[15/9862] Generating qemu-img-cmds.h with a custom command (wrapped by meson to capture output)
[16/9862] Generating hmp-commands.h with a custom command (wrapped by meson to capture output)
[17/9862] Generating hmp-commands-info.h with a custom command (wrapped by meson to capture output)
[18/9862] Compiling C object subprojects/libvhost-user/libvhost-user.a.p/libvhost-user.c.o
[19/9862] Linking static target subprojects/libvhost-user/libvhost-user.a
[20/9862] Compiling C object subprojects/libvhost-user/link-test.p/link-test.c.o
[21/9862] Linking target subprojects/libvhost-user/link-test
[22/9862] Compiling C object subprojects/libvhost-user/libvhost-user-glib.a.p/libvhost-user-glib.c.o
[23/9862] Linking static target subprojects/libvhost-user/libvhost-user-glib.a
[24/9862] Compiling C object subprojects/libvduse/libvduse.a.p/libvduse.c.o
[25/9862] Linking static target subprojects/libvduse/libvduse.a
[26/9862] Generating qapi/shared QAPI source files with a custom command
[27/9862] Generating trace/trace-root.h with a custom command

……

[9830/9862] Linking target tests/qtest/tco-test
[9831/9862] Compiling C object tests/qtest/cpu-plug-test.p/cpu-plug-test.c.o
[9832/9862] Linking target tests/qtest/cpu-plug-test
[9833/9862] Compiling C object tests/qtest/q35-test.p/q35-test.c.o
[9834/9862] Linking target tests/qtest/q35-test
[9835/9862] Compiling C object tests/qtest/vmgenid-test.p/vmgenid-test.c.o
[9836/9862] Compiling C object tests/qtest/vmgenid-test.p/boot-sector.c.o
[9837/9862] Compiling C object tests/qtest/vmgenid-test.p/acpi-utils.c.o
[9838/9862] Linking target tests/qtest/vmgenid-test
[9839/9862] Compiling C object tests/qtest/test-x86-cpuid-compat.p/test-x86-cpuid-compat.c.o
[9840/9862] Linking target tests/qtest/test-x86-cpuid-compat
[9841/9862] Compiling C object tests/qtest/dbus-display-test.p/meson-generated_.._.._.._ui_dbus-display1.c.o
[9842/9862] Compiling C object tests/qtest/dbus-display-test.p/dbus-display-test.c.o
[9843/9862] Linking target tests/qtest/dbus-display-test
[9844/9862] Compiling C object tests/qtest/m48t59-test.p/m48t59-test.c.o
[9845/9862] Linking target tests/qtest/m48t59-test
[9846/9862] Compiling C object tests/qtest/pnv-xscom-test.p/pnv-xscom-test.c.o
[9847/9862] Linking target tests/qtest/pnv-xscom-test
[9848/9862] Compiling C object tests/qtest/rtas-test.p/rtas-test.c.o
[9849/9862] Linking target tests/qtest/rtas-test
[9850/9862] Compiling C object tests/qtest/virtio-ccw-test.p/virtio-ccw-test.c.o
[9851/9862] Linking target tests/qtest/virtio-ccw-test
[9852/9862] Compiling C object tests/qtest/prom-env-test.p/prom-env-test.c.o
[9853/9862] Linking target tests/qtest/prom-env-test
[9854/9862] Generating po/bg/LC_MESSAGES/qemu-bg.mo with a custom command
[9855/9862] Generating po/de_DE/LC_MESSAGES/qemu-de_DE.mo with a custom command
[9856/9862] Generating po/fr_FR/LC_MESSAGES/qemu-fr_FR.mo with a custom command
[9857/9862] Generating po/hu/LC_MESSAGES/qemu-hu.mo with a custom command
[9858/9862] Generating po/it/LC_MESSAGES/qemu-it.mo with a custom command
[9859/9862] Generating po/sv/LC_MESSAGES/qemu-sv.mo with a custom command
[9860/9862] Generating po/tr/LC_MESSAGES/qemu-tr.mo with a custom command
[9861/9862] Generating po/uk/LC_MESSAGES/qemu-uk.mo with a custom command
[9862/9862] Generating po/zh_CN/LC_MESSAGES/qemu-zh_CN.mo with a custom command
make[1]: 离开目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”
changing dir to build for make ""...
make[1]: 进入目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”
[1/150] Generating qemu-version.h with a custom command (wrapped by meson to capture output)
[2/34] Generating tests/include/QAPI test (include) with a custom command
make[1]: 离开目录“/home/penghao/QEMU_kvm/QEMU/source_codes/qemu-7.2.0/build”

可以看到,不带“--target-list=x86_64-softmmu”选项编译时,项目数为9862,远超带选项时的2474。因此在编译时间上也长了很多,大约需要65分钟。

编译完成后,在build/x86_64-softmmu/下会生成qemu-system-x86_64文件,如下所示:

$ ls build/x86_64-softmmu/
qemu-system-x86_64

$ ls -l build/x86_64-softmmu/
总计 0
lrwxrwxrwx 1 penghao penghao 21  1月16日 15:56 qemu-system-x86_64 -> ../qemu-system-x86_64

可以看到,它是一个链接文件,指向了build/下的qemu-system-x86_64文件,如下所示:

$ ls -l build/qemu-system-x86_64
-rwxrwxr-x 1 penghao penghao 64111688  1月16日 17:02 build/qemu-system-x86_64

这就是我们需要的用户空间用于客户机的工具了。至此,编译工作完成。

注:

笔者是在自己的类Ubuntu操作系统下进行编译的。如果在Ubuntu操作系统下编译,需要安装Ninja和pixman-1这2个包,之后才能编译通过。