CMakeLists.txt
我的raspberrypi/pico-sdk项目中有此代码片段,我有条件地调用该pico_sdk_init()
函数(即宏):
# ...
if(myCONDITION STREQUAL "Something")
message("*** BEFORE pico_sdk_init() ***")
pico_sdk_init()
message("*** BEFORE return() ***")
return() # exit for debug
# other CMake conditional code commands continue here ...
endif() # myCONDITION STREQUAL "Something"
# ...
因此,我mkdir build && cd build
在项目目录中执行CMakeLists.txt
,然后运行cmake
- 在这种情况下,我得到如下输出:
$ cmake ../ -DCMAKE_BUILD_TYPE=Debug -G "MSYS Makefiles"
...
*** BEFORE pico_sdk_init() ***
Build type is Debug
...
Using PICO_EXAMPLES_PATH from environment ...
*** BEFORE return() ***
-- Configuring done
-- Generating done
-- Build files have been written to: C:/path/to/myproject/build
在此之后,如果我从子文件夹检查./build
,我会在Makefile
那里(以及一大堆其他位置)看到:
$ find . -name Makefile
./Makefile
./pico-sdk/docs/Makefile
./pico-sdk/Makefile
./pico-sdk/src/common/boot_picobin_headers/Makefile
...
./pico-sdk/src/rp2_common/tinyusb/Makefile
./pico-sdk/tools/Makefile
...对于子文件Makefile
夹./build
,我有以下目标:
$ make #press [TAB] here
all default_target
bs2_default/fast depend
bs2_default_bin/fast edit_cache/fast
bs2_default_library/fast help
clean/fast pioasmBuild/fast
cmake_check_build_system preinstall/fast
cmake_force rebuild_cache/fast
cyw43_driver_picow_cyw43_bus_pio_spi_pio_h/fast
make pioasmBuild
因此,我现在可以运行,并且它将构建良好:
$ make pioasmBuild
Scanning dependencies of target pioasmBuild
[ 12%] Creating directories for 'pioasmBuild'
[ 25%] No download step for 'pioasmBuild'
...
Scanning dependencies of target pioasm
[100%] Built target pioasm
[ 87%] Performing install step for 'pioasmBuild'
[100%] Built target pioasm
Install the project...
-- Install configuration: "Release"
[100%] Completed 'pioasmBuild'
[100%] Built target pioasmBuild
$ find . -name pioasm.exe
./pioasm/pioasm.exe
./pioasm-install/pioasm/pioasm.exe
伟大的。
现在,假设我不想使用return()
- 退出调试,但是相反,在那时,我想运行等效的make pioasmBuild
:因此从那时起,pioasm.exe
可用于“其他 CMake 条件代码命令”;为此,我将代码片段重写为:
# ...
if(myCONDITION STREQUAL "Something")
message("*** BEFORE pico_sdk_init() ***")
pico_sdk_init()
execute_process(
# this command, the equivalent of `make pioasmBuild`, fails with "Error: could not load cache"
COMMAND ${CMAKE_COMMAND} --build . --target pioasmBuild
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
message("*** NOT anymore BEFORE return() ***")
#return() # exit for debug
# other CMake conditional code commands continue here ...
endif() # myCONDITION STREQUAL "Something"
# ...
正如评论所暗示的那样,make pioasmBuild
此时的等效操作失败 -在子目录rm -rf *
中清理之后./build
,这是我在cmake
输出中得到的内容:
$ cmake ../ -DCMAKE_BUILD_TYPE=Debug -G "MSYS Makefiles"
...
*** BEFORE pico_sdk_init() ***
Build type is Debug
...
Using PICO_EXAMPLES_PATH from environment ...
Error: could not load cache
*** NOT anymore BEFORE return() ***
... other CMake conditional code commands continue here ...
所以,据我所知,Error: could not load cache
发生这种情况是因为此时子文件夹Makefile
尚未创建!./build
事实上,据我目前对这个过程的理解:在前面的案例中,Makefile
中的那个的创建是由提前退出本身触发的!./build
return()
所以我的问题是 - 是否有一种规范的方法可以强制 CMake 在CMakeLists.txt
文件中的这个(或任意)点完成配置、生成和写入构建文件(使用此时可用的任何数据;并且至少在这种情况下,如前所述,该点有足够的数据来生成一个正常运行的Makefile
) - 而不强制进程cmake
在此时退出CMakeLists.txt
,而是继续执行剩余的命令(包括可能Makefile
直接从 CMake 在先前/早期生成的中构建目标)?
(当然,在这种“早期生成”的情况下,当CMakeLists.txt
达到其原本“自然”的结尾时,所有Makefile
s 和相关的构建文件都将被覆盖,可能会出现例如更多的目标,这是由于执行文件剩余部分而产生的更多数据)