Postgresql源码(140)理解PG的编译流程(make、Makefile、Makefile.global.in)

news/2025/1/22 7:21:35/

PG16

PG中使用的makefile>makefile看起来代码比较多,但是实际逻辑比较简单,这里做一些抽象总结。

总结

  1. Makefile.global.in的$(recurse)宏自动生成了target,可以方便的进入内存目录进行编译。
all: all-common-recurse
all-common-recurse:  submake-generated-headersMAKE -C common allall: all-backend-recurse
all-backend-recurse:  submake-generated-headersMAKE -C backend allinstall: install-common-recurse
install-common-recurse:  submake-generated-headersMAKE -C common installinstall: install-backend-recurse
install-backend-recurse:  submake-generated-headersMAKE -C backend installcheck: check-common-recurse
check-common-recurse:  submake-generated-headers  temp-installMAKE -C common checkcheck: check-backend-recurse
check-backend-recurse:  submake-generated-headers  temp-installMAKE -C backend check
  1. Makefile.global.in同时定义了对.o合.bc文件的生成规则:
%.o : %.c@if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi$(COMPILE.c) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po%.o : %.cpp@if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi$(COMPILE.cc) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po%.bc : %.c$(COMPILE.c.bc) -o $@ $<%.bc : %.cpp$(COMPILE.cxx.bc) -o $@ $<
  1. 在最内层目录的Makefile定义了OBJS例如:OBJS=aset.o mcxt.o ...,然后调用common.mk文件,对OBJS中的每一个元素进行隐式编译,简化版内层Makefile:
OBJS = aset.o mcxt.o# 没有OBJS生成规则,make隐式生成.o文件,调用上面定义的%.o : %.c规则
objfiles.txt: $(OBJS)# echo or touch objfiles
  1. 如果打开了llvm,简化版内层Makefile:
OBJS = aset.o mcxt.o# 没有aset.bc mcxt.bc生成规则,make隐式生成.bc文件,调用上面定义的%.bc : %.c规则。
objfiles.txt: $(patsubst %.o,%.bc, $(OBJS))     ## objfiles.txt: aset.bc mcxt.bc
# 没有OBJS生成规则,make隐式生成.o文件,调用上面定义的%.o : %.c规则
$(patsubst %.o,%.bc, $(OBJS)): $(OBJS)          ## aset.bc mcxt.bc: aset.o mcxt.o

make_69">1 执行make后发生了什么?

简单工程中的makefile>makefile中可以看到很多target,依赖关系一般也比较直接,例如下面例子中:

OrcV2CBindingsAddObjectFile 依赖 OrcV2CBindingsAddObjectFile.o 依赖 OrcV2CBindingsAddObjectFile.c

all: __BUILD_DIR \$(BUILD_DIR)/OrcV2CBindingsAddObjectFile \$(BUILD_DIR)/OrcV2CBindingsBasicUsage.PHONY: __BUILD_DIR$(BUILD_DIR)/OrcV2CBindingsAddObjectFile: $(BUILD_DIR)/OrcV2CBindingsAddObjectFile.o${CXX} $< ${CXXFLAGS} ${LLVM_LD_FLAGS} -o $@ 
$(BUILD_DIR)/OrcV2CBindingsAddObjectFile.o: OrcV2CBindingsAddObjectFile.c${CC} -c $< ${CFLAGS} ${LLVM_CC_FLAGS} -o $@

但打开PG根目录下的Makefile:

subdir = src
top_builddir = ..
include Makefile.globalSUBDIRS = \common \port \timezone \backend \backend/utils/mb/conversion_procs \backend/snowball \include \interfaces \backend/replication/libpqwalreceiver \backend/replication/pgoutput \fe_utils \bin \pl \makefile>makefiles \test/regress \test/isolation \test/perlifeq ($(with_llvm), yes)
SUBDIRS += backend/jit/llvm
endif# There are too many interdependencies between the subdirectories, so
# don't attempt parallel make here.
.NOTPARALLEL:$(recurse)...
...
  • 看不到target信息,代码的可读性比较差,因为所有target都是生成的。优点是target动态生成,代码量少,比较灵活。
  • 所有的细节都封装在$(recurse)中,具体在Makefile.global.in中定义。

2 Makefile.global.in

截取关键代码:


standard_targets = all install installdirs uninstall distprep clean distclean maintainer-clean coverage check checkprep installcheck init-po update-po
# these targets should recurse even into subdirectories not being built:
standard_always_targets = distprep clean distclean maintainer-clean.PHONY: $(standard_targets) install-strip html man installcheck-parallel update-unicode# make `all' the default target
all:##########################################################################
#
# Programs and flags# CompilersCPP = @CPP@
CPPFLAGS = @CPPFLAGS@
PG_SYSROOT = @PG_SYSROOT@override CPPFLAGS := $(ICU_CFLAGS) $(CPPFLAGS)ifdef PGXS
override CPPFLAGS := -I$(includedir_server) -I$(includedir_internal) $(CPPFLAGS)
else # not PGXS
override CPPFLAGS := -I$(top_srcdir)/src/include $(CPPFLAGS)
ifdef VPATH
override CPPFLAGS := -I$(top_builddir)/src/include $(CPPFLAGS)
endif
endif # not PGXSCC = @CC@
GCC = @GCC@
SUN_STUDIO_CC = @SUN_STUDIO_CC@
CXX = @CXX@
CFLAGS = @CFLAGS@
CFLAGS_SL = @CFLAGS_SL@
# *_MODULE are for flags applied to extension libraries
CFLAGS_SL_MODULE = @CFLAGS_SL_MODULE@
CXXFLAGS_SL_MODULE = @CXXFLAGS_SL_MODULE@
CFLAGS_UNROLL_LOOPS = @CFLAGS_UNROLL_LOOPS@
CFLAGS_VECTORIZE = @CFLAGS_VECTORIZE@
CFLAGS_CRC = @CFLAGS_CRC@
PERMIT_DECLARATION_AFTER_STATEMENT = @PERMIT_DECLARATION_AFTER_STATEMENT@
CXXFLAGS = @CXXFLAGS@LLVM_CPPFLAGS = @LLVM_CPPFLAGS@
LLVM_CFLAGS = @LLVM_CFLAGS@
LLVM_CXXFLAGS = @LLVM_CXXFLAGS@# Kind-of compilersBISON = @BISON@
BISONFLAGS = @BISONFLAGS@ $(YFLAGS)
FLEX = @FLEX@
FLEXFLAGS = @FLEXFLAGS@ $(LFLAGS)
DTRACE = @DTRACE@
DTRACEFLAGS = @DTRACEFLAGS@
ZIC = @ZIC@# LinkingAR = @AR@
AROPT = crs
LIBS = @LIBS@
LDAP_LIBS_FE = @LDAP_LIBS_FE@
LDAP_LIBS_BE = @LDAP_LIBS_BE@
UUID_LIBS = @UUID_LIBS@
LLVM_LIBS=@LLVM_LIBS@# Tree-wide build supportall install check installcheck: submake-generated-headers.PHONY: submake-generated-headerssubmake-generated-headers:
ifndef NO_GENERATED_HEADERS
ifeq ($(MAKELEVEL),0)$(MAKE) -C $(top_builddir)/src/backend generated-headers
endif
endif##########################################################################
#
# Recursive make supportdefine _create_recursive_target
.PHONY: $(1)-$(2)-recurse
$(1): $(1)-$(2)-recurse
$(1)-$(2)-recurse: $(if $(filter all install check installcheck, $(3)), submake-generated-headers) $(if $(filter check, $(3)), temp-install)$$(MAKE) -C $(2) $(3)
endefrecurse = $(foreach target,$(if $1,$1,$(standard_targets)),$(foreach subdir,$(if $2,$2,$(SUBDIRS)),$(eval $(call 

在最后面我们看到了recurse的定义:
recurse = $(foreach target,$(if $1,$1,$(standard_targets)),$(foreach subdir,$(if $2,$2,$(SUBDIRS)),$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))))
这里调用了_create_recursive_target函数来生成所有目录的target。

PG的例子太复杂,这里抽象出来一个最小用例来继续分析。

3 $(recurse)最小DEMO

Makefile


subdir = src
top_builddir = ..
include Makefile.globalSUBDIRS = \common \port \timezone \backend$(recurse)

Makefile.global


standard_targets = all install check
.PHONY: $(standard_targets)all:###############################
all install check installcheck: submake-generated-headers.PHONY: submake-generated-headerssubmake-generated-headers:
ifndef NO_GENERATED_HEADERS
ifeq ($(MAKELEVEL),0)$(info ------------GENERATEHEADERS------------)
endif
endif###############################
define _create_recursive_target
.PHONY: $(1)-$(2)-recurse
$(1): $(1)-$(2)-recurse
$(1)-$(2)-recurse: $(if $(filter all install check installcheck, $(3)), submake-generated-headers) $(if $(filter check, $(3)), temp-install)$(info ------------GENERATE TATGETS------------)$(info $$1: $1, $$2: $2, $$3: $3)$(info $(1): $(1)-$(2)-recurse)$(info $(1)-$(2)-recurse: $(if $(filter all install check installcheck, $(3)), submake-generated-headers) $(if $(filter check, $(3)), temp-install))$(info MAKE -C $(2) $(3))$$(MAKE) -C $(2) $(3)
endefrecurse = $(foreach target,$(if $1,$1,$(standard_targets)),$(foreach subdir,$(if $2,$2,$(SUBDIRS)),$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))))# recurse = \
#     $(foreach target,$(if $1,$1,$(standard_targets)),\
#         $(foreach subdir,$(if $2,$2,$(SUBDIRS)),\
#             $(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))\
#         )\
#     )

执行结果:
在这里插入图片描述

4 $(recurse)最小DEMO结果分析

recurse = $(foreach target,$(if $1,$1,$(standard_targets)),$(foreach subdir,$(if $2,$2,$(SUBDIRS)),$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))))

展开后

recurse = \$(foreach target,$(if $1,$1,$(standard_targets)),\$(foreach subdir,$(if $2,$2,$(SUBDIRS)),\$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))\)\)
  • 外层循环遍历standard_targets = all install check,也就是PG定义的所有make指令,需要为每个指令定义target。
  • 内层循环遍历SUBDIRS = common backend,这个是Makefile中定义的,也就是谁include Makefile.global,谁负责定义好SUBDIRS。表示当前目录下,哪些子目录需要进行编译。

调用_create_recursive_target函数,为makefile>makefile动态定义target:

define _create_recursive_target
.PHONY: $(1)-$(2)-recurse
$(1): $(1)-$(2)-recurse
$(1)-$(2)-recurse: $(if $(filter all install check installcheck, $(3)), submake-generated-headers) $(if $(filter check, $(3)), temp-install)$$(MAKE) -C $(2) $(3)
endef

函数执行后,生成一系列target,两个小细节:

  • 如果target是all install check installcheck集中情况,需要增加submake-generated-headers依赖,这个是生成头文件的,PG的一些头文件是编译时生成出来的。
  • 如果target是check,需要增加temp-install依赖,temp-install是负责regress测试的target。
all: all-common-recurse
all-common-recurse:  submake-generated-headersMAKE -C common allall: all-backend-recurse
all-backend-recurse:  submake-generated-headersMAKE -C backend allinstall: install-common-recurse
install-common-recurse:  submake-generated-headersMAKE -C common installinstall: install-backend-recurse
install-backend-recurse:  submake-generated-headersMAKE -C backend installcheck: check-common-recurse
check-common-recurse:  submake-generated-headers  temp-installMAKE -C common checkcheck: check-backend-recurse
check-backend-recurse:  submake-generated-headers  temp-installMAKE -C backend check

5 继续完善demo

  • 前面提到的demo只构造了target,并没有真正编译.c文件。
  • 继续增加了.c文件编译到.o的部分。
$ tree
.
├── common
│   ├── aset.c
│   └── Makefile
├── common.mk
├── Makefile
└── Makefile.global1 directory, 5 files

common/Makefile

subdir = src/backend/utils/mmgr
top_builddir = ../../../..
include ../Makefile.globalOBJS = \aset.oinclude ../common.mk

common.mk

subsysfilename = objfiles.txtifneq ($(subdir), src/backend)
all: $(subsysfilename)
endifobjfiles.txt: $(OBJS)$(if $(filter-out $(OBJS),$?),( $(if $(SUBDIROBJS),cat $(SUBDIROBJS); )echo $(addprefix $(subdir)/,$(OBJS)) ) >$@,touch $@)

Makefile


subdir = src
top_builddir = ..
include Makefile.globalSUBDIRS = \common \backend$(recurse)

Makefile.global


standard_targets = all install check
.PHONY: $(standard_targets)all:###############################
all install check installcheck: submake-generated-headers.PHONY: submake-generated-headerssubmake-generated-headers:
ifndef NO_GENERATED_HEADERS
ifeq ($(MAKELEVEL),0)$(info ------------GENERATEHEADERS------------)
endif
endif###############################
define _create_recursive_target
.PHONY: $(1)-$(2)-recurse
$(1): $(1)-$(2)-recurse
$(1)-$(2)-recurse: $(if $(filter all install check installcheck, $(3)), submake-generated-headers) $(if $(filter check, $(3)), temp-install)$(info ------------GENERATE TATGETS------------)$(info $$1: $1, $$2: $2, $$3: $3)$(info $(1): $(1)-$(2)-recurse)$(info $(1)-$(2)-recurse: $(if $(filter all install check installcheck, $(3)), submake-generated-headers) $(if $(filter check, $(3)), temp-install))$(info MAKE -C $(2) $(3))$$(MAKE) -C $(2) $(3)
endefrecurse = $(foreach target,$(if $1,$1,$(standard_targets)),$(foreach subdir,$(if $2,$2,$(SUBDIRS)),$(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))))# recurse = \
#     $(foreach target,$(if $1,$1,$(standard_targets)),\
#         $(foreach subdir,$(if $2,$2,$(SUBDIRS)),\
#             $(eval $(call _create_recursive_target,$(target),$(subdir),$(if $3,$3,$(target))))\
#         )\
#     )

这里主要需要关注的就是common.mk。每个源码目录中都有一些文件需要编译,所以都需要配一个Makefile,这些Makefile中相同的逻辑抽取到了common.mk中。

objfiles.txt: $(OBJS)......

注意这里OBJS = aset.o,这里makefile>makefile构造objfiles.txt依赖aset.o,而aset.o触发了Makefile的隐式规则,会自动从aset.c编译为aset.o。

除了规则外,具体的编译方法也可以在Makefile中定义,例如PG的makefile>makefile.global.in中定义了对.o文件的处理方法。

%.o : %.c@if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi$(COMPILE.c) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po%.o : %.cpp@if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi$(COMPILE.cc) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po

llvm支持:common.mk中还有一段,如果打开了llvm后,objfiles.txt会同时依赖.o和.bc文件。

ifeq ($(with_llvm), yes)
objfiles.txt: $(patsubst %.o,%.bc, $(OBJS))
$(patsubst %.o,%.bc, $(OBJS)): $(OBJS)
endif

makefile>makefile.global.in中也定义了对.bc文件的处理方法:

%.bc : %.c$(COMPILE.c.bc) -o $@ $<%.bc : %.cpp$(COMPILE.cxx.bc) -o $@ $<

http://www.ppmy.cn/news/1565163.html

相关文章

ANSYS HFSS 中的相控天线阵列仿真方法

概述 相控天线阵列系统广泛使用&#xff0c;从国防雷达应用到商业 5G 应用。设计这些天线阵列涉及复杂的数学运算&#xff0c;需要全波仿真。Ansys HFSS 全场 3D 电磁仿真软件可以在合理的时间内以较低的计算成本仿真复杂的相控阵天线系统&#xff0c;同时考虑复杂激励、环境&…

Python与Excel:开启自动化办公新时代

引言 在当今数字化办公的大环境下&#xff0c;日常工作中处理Excel表格的任务愈发频繁且繁杂。传统的手动操作不仅耗时费力&#xff0c;还容易出错。而Python作为一门功能强大且应用广泛的编程语言&#xff0c;为我们实现Excel办公自动化提供了高效的解决方案。借助Python的丰…

编译chromium笔记

编译环境&#xff1a; windows10 powershell7.2.24 git 2.47.1 https://storage.googleapis.com/chrome-infra/depot_tools.zip 配置git git config --global user.name "John Doe" git config --global user.email "jdoegmail.com" git config --global …

如何使用 Pytest -k 选项轻松筛选测试用例

关注开源优测不迷路 大数据测试过程、策略及挑战 测试框架原理&#xff0c;构建成功的基石 在自动化测试工作之前&#xff0c;你应该知道的10条建议 在自动化测试中&#xff0c;重要的不是工具 你是否曾不得不从成百上千个测试中费力筛选&#xff0c;只为运行几个特定的测试&am…

PHP语言的数据库编程

PHP语言的数据库编程 引言 随着互联网的发展&#xff0c;动态网站已成为主流&#xff0c;而动态网站的核心就是与数据库进行交互。PHP&#xff08;超文本预处理器&#xff09;是一种流行的开源服务器端脚本语言&#xff0c;被广泛用于Web开发。它以其简单易学和功能强大而受到…

前端——JS

目录 什么是JS&#xff1f; JS引入方式 JS基础语法 输出语句 变量 常量 数据类型 函数 自定义对象 什么是JS&#xff1f; JavaScript&#xff08;简称&#xff1a;JS&#xff09; 是一门跨平台、面向对象的脚本语言&#xff0c;是用来控制网页行为的&#xff0c;实现人…

2、ansible的playbook

ansible的脚本&#xff1a;playbook剧本 脚本的作用&#xff1a;复用 playbook的组成部分 1、开头 ---&#xff1a;表示是一个yaml文件&#xff0c;但是可以忽略。 2、Tasks&#xff08;任务&#xff09;&#xff1a;包含了目标主机上执行的操作&#xff0c;操作还是由模板来…

MySQL和SQL server的区别

在当今数据驱动的世界里&#xff0c;数据库技术的选择对于企业和个人开发者来说至关重要。MySQL 和 SQL Server 是两个广泛使用的数据库管理系统&#xff08;DBMS&#xff09;&#xff0c;它们各自拥有独特的优势和适用场景。本文将深入探讨这两个数据库系统之间的区别&#xf…