all-makefiles-under vs all-subdir-makefiles vs first-makefiles-under

参考:all-makefiles-under v.s. first-makefiles-under

definitions.mk - OpenGrok cross reference for /build/make/core/definitions.mk (aospxref.com)

先来看一下这三者的定义:

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
###########################################################
## Retrieve a list of all makefiles immediately below some directory
###########################################################

#找出指定目录的二级目录的所有Android.mk
define all-makefiles-under
$(wildcard $(1)/*/Android.mk)
endef

###########################################################
## Look under a directory for makefiles that don't have parent
## makefiles.
###########################################################
# $(1): directory to search under
# Ignores $(1)/Android.mk
define first-makefiles-under
$(shell build/make/tools/findleaves.py $(FIND_LEAVES_EXCLUDES) \
--mindepth=2 $(addprefix --dir=,$(1)) Android.mk)
endef

###########################################################
## Retrieve a list of all makefiles immediately below your directory
## Must be called before including any other makefile!!
###########################################################
#找出当前目录的二级目录的所有Android.mk
define all-subdir-makefiles
$(call all-makefiles-under,$(call my-dir))
endef

The builtin wildcard is quite efficient. So we should use $(call all-makefiles-under, $(LOCAL_PATH)) as you can.

first-makefiles-under calls the script build/tools/findleaves.py to find Android.mks in the given directory.

If it finds an Android.mk in a directory, it just stops searching the subdirs.

It’s not efficient, especially in case you have deep complex directory structure that does not have Android.mk in the top dirs.

We should use it as little as possible.

If your Android.mk’s only purpose is to include all first-level sub-directory Android.mks, use:

include $(call all-subdir-makefiles)

If you want to include Android.mks in a specific set of first-level sub-directories:

include $(call all-named-subdir-makefiles, )

注意:

1、上面的函数都不包括指定目录的Android.mk。

2、Both all-subdir-makefiles and all-named-subdir-makefiles use $(my-dir), which uses makefile’s builtin MAKEFILE_LIST to get the current include chain of makefiles.

So they must be the called before any “include” statement in your makefile.

位置错误会报错:error: my-dir must be called before including any other makefile..

原因分析:

Android AOSP Android.mk - $(call all-subdir-makefiles) Path problems | Sebastian Weisgerber

Problems using NDK build system and subdirectories

即:

This is due to a limitation of the GNU Make command language: All variables are global, and the implementation of $(call my-dir) can only give you the name of the last included Makefile (even if you already exited the Makefile). There is no way to implement something that works properly with GNU Make.

测试结果:

假设app目录结构如下

1
2
3
4
5
6
7
8
app->app1->Android.mk
->app2->Android.mk
->app3->app4->Android.mk
->app5->Android.mk
->app6->Android.mk
->app7->Android.mk
->app8->Android.mk
->Android.mk

在app目录分别调用以上函数,结果如下:

1
2
3
4
#all-makefiles-under
app->app1->Android.mk
->app2->Android.mk
->app6->Android.mk
1
2
3
4
#all-subdir-makefiles
app->app1->Android.mk
->app2->Android.mk
->app6->Android.mk
1
2
3
4
5
6
#first-makefiles-under
app->app1->Android.mk
->app2->Android.mk
->app3->app4->Android.mk
->app5->Android.mk
->app6->Android.mk

Android.mk中call all-subdir-makefiles和call all-makefiles-under,$(LOCAL_PATH)的区别_jackyu613的专栏 Embedded Linux Android-CSDN博客

call all-subdir-makefiles和call all-makefiles-under - 简书


all-makefiles-under vs all-subdir-makefiles vs first-makefiles-under
https://iwesley.top/article/ea68fa1f/
作者
Wesley
发布于
2022年1月15日
许可协议