add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d) ,))
时间: 2023-11-21 11:06:43 浏览: 14
这是一个 Makefile 中的函数,它的作用是判断指定目录是否存在,如果存在则将目录加入列表中。具体来说,它接收一个目录列表作为参数,对于列表中的每个目录,使用 `wildcard` 函数查找这个目录是否存在,如果存在则将这个目录加入返回的列表中。这里的 `$(srctree)` 是一个 Makefile 的内置变量,表示源代码根目录的路径。
相关问题
add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d) ,)) the usage of wildcard and foreach
`wildcard` is a function in GNU Make that allows you to perform wildcard pattern matching on file names. It takes a pattern argument and returns a space-separated list of file names that match the pattern. For example, `$(wildcard *.c)` would return a list of all the `.c` files in the current directory.
`foreach` is another function in GNU Make that allows you to iterate over a list of values and perform some action for each value. It takes two arguments: a variable name and a list of values. For each value in the list, the variable is set to that value and the specified action is performed.
In the `add_if_exists` example you provided, `wildcard` is used to check if certain directories exist in the source tree. `foreach` is then used to iterate over a list of directories and call `wildcard` on each directory. If the directory exists, it is added to the return list. If it does not exist, it is skipped.
Here is a breakdown of the `add_if_exists` code:
```
add_if_exists = $(foreach d,$(1),$(if $(wildcard $(srctree)/$(d)),$(d),))
```
- `add_if_exists` is a variable that contains the function definition.
- `$(1)` is a reference to the first argument passed to the function.
- `foreach d,$(1),` sets up a loop that iterates over the values in the first argument and sets the variable `d` to each value in turn.
- `$(wildcard $(srctree)/$(d))` performs wildcard pattern matching on the directory `$(srctree)/$(d)` to see if it exists.
- `$(if $(wildcard $(srctree)/$(d)),$(d),)` is a conditional statement that returns `$(d)` if the directory exists, or an empty string if it doesn't.
- The overall effect is to return a space-separated list of directories that exist in the source tree.
if(NOT EXISTS ${CTK_BINARY_DIR}/CTK-build/bin) file(MAKE_DIRECTORY ${CTK_BINARY_DIR}/CTK-build/bin) endif() #----------------------------------------------------------------------------- set(proj CTK) set(ep_cxx_standard_arg) if(CMAKE_CXX_STANDARD) set(ep_cxx_standard_arg "-DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}") endif() # ADD INSTALL_COMMAND if(WIN32) set(_INSTALL_CMD nmake install) else() set(_INSTALL_CMD make install) endif() # Set install location set(_INSTALL_DIR ${CTK_BINARY_DIR}/../CTKInstall) ExternalProject_Add(${proj} ${${proj}_EP_ARGS} DOWNLOAD_COMMAND "" CMAKE_CACHE_ARGS -DCTK_SUPERBUILD:BOOL=OFF -DCTK_SUPERBUILD_BINARY_DIR:PATH=${CTK_BINARY_DIR} -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} -DCMAKE_CXX_FLAGS_INIT:STRING=${CMAKE_CXX_FLAGS_INIT} -DCMAKE_C_FLAGS_INIT:STRING=${CMAKE_C_FLAGS_INIT} -DCMAKE_INSTALL_PREFIX:PATH=${_INSTALL_DIR} -DCMAKE_MACOSX_RPATH:BOOL=${CMAKE_MACOSX_RPATH} ${ep_cxx_standard_arg} SOURCE_DIR ${CTK_SOURCE_DIR} BINARY_DIR ${CTK_BINARY_DIR}/CTK-build INSTALL_COMMAND ${_INSTALL_CMD} DEPENDS ${CTK_DEPENDENCIES}
这段代码看起来是一个CMake脚本,用于构建和安装CTK(Common Toolkit)项目。CTK是一个开源的C++工具包,用于开发医学图像处理和可视化应用。
这段代码的主要作用是使用ExternalProject_Add函数来添加CTK项目,并设置构建和安装选项。以下是对代码中各部分的解释:
1. 创建目录:首先,通过`if(NOT EXISTS ${CTK_BINARY_DIR}/CTK-build/bin)`判断`${CTK_BINARY_DIR}/CTK-build/bin`目录是否存在,如果不存在,则使用`file(MAKE_DIRECTORY ${CTK_BINARY_DIR}/CTK-build/bin)`创建该目录。这个目录可能用于存放编译后的可执行文件。
2. 设置项目名称:通过`set(proj CTK)`设置项目名称为CTK。
3. 设置C++标准:根据`${CMAKE_CXX_STANDARD}`的值,通过`set(ep_cxx_standard_arg "-DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}")`设置CMake的C++标准选项。
4. 设置安装命令:根据当前操作系统的类型,通过`if(WIN32)`判断,如果是Windows系统,则设置`_INSTALL_CMD`为`nmake install`,否则设置为`make install`。这个命令将在构建完成后执行安装操作。
5. 设置安装目录:通过`set(_INSTALL_DIR ${CTK_BINARY_DIR}/../CTKInstall)`设置安装目录为`${CTK_BINARY_DIR}/../CTKInstall`。这个目录将用于安装CTK项目。
6. 添加CTK项目:通过`ExternalProject_Add`函数添加CTK项目,并设置相关的CMake选项和参数。其中,`SOURCE_DIR`指定CTK源码目录,`BINARY_DIR`指定CTK构建目录,`INSTALL_COMMAND`指定安装命令,`DEPENDS`指定CTK项目的依赖项。
以上就是这段代码的主要内容和作用。通过执行这段脚本,可以构建和安装CTK项目,并将结果安装到指定的目录中。
阅读全文