如何获取cmake进程的工作目录?
How to get the working directory of the cmake process?
如何获取调用 cmake
的工作目录?
我知道我可以 运行 execute_process()
在我的 CMake 代码的开头,但也许有一些更好的方法,比如 CMake 进程的工作目录的内置变量。
我想要实现的是将CMake变量中给定的相对路径(用-D...
给出)转换为CMake中的绝对路径。
What I want to achieve is to convert a relative path given in a CMake variable (given with -D...) to an absolute path in CMake.
然后只需在 CMakeLists.txt 中创建 PATH
类型(用于目录)或 FILEPATH
类型的缓存变量。 CMake 会自动将未类型化的命令行参数转换为绝对路径。
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(test NONE)
set(input_file "" CACHE FILEPATH "some input file")
message(STATUS "${input_file}")
$ cmake -S . -B build -Dinput_file=foo.txt
-- /path/to/foo.txt
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
查看文档:
It is possible for the cache entry to exist prior to the call but have no type set if it was created on the cmake(1) command line by a user through the -D<var>=<value>
option without specifying a type. In this case the set command will add the type. Furthermore, if the <type>
is PATH
or FILEPATH
and the <value>
provided on the command line is a relative path, then the set command will treat the path as relative to the current working directory and convert it to an absolute path.
此处:https://cmake.org/cmake/help/latest/command/set.html?highlight=current%20working%20directory
相关地,当运行处于脚本模式(而不是项目模式)时,设置了几个常用变量到当前工作目录:
When run in -P
script mode, CMake sets the variables CMAKE_BINARY_DIR
, CMAKE_SOURCE_DIR
, CMAKE_CURRENT_BINARY_DIR
and CMAKE_CURRENT_SOURCE_DIR
to the current working directory.
https://cmake.org/cmake/help/latest/variable/CMAKE_SOURCE_DIR.html
如何获取调用 cmake
的工作目录?
我知道我可以 运行 execute_process()
在我的 CMake 代码的开头,但也许有一些更好的方法,比如 CMake 进程的工作目录的内置变量。
我想要实现的是将CMake变量中给定的相对路径(用-D...
给出)转换为CMake中的绝对路径。
What I want to achieve is to convert a relative path given in a CMake variable (given with -D...) to an absolute path in CMake.
然后只需在 CMakeLists.txt 中创建 PATH
类型(用于目录)或 FILEPATH
类型的缓存变量。 CMake 会自动将未类型化的命令行参数转换为绝对路径。
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(test NONE)
set(input_file "" CACHE FILEPATH "some input file")
message(STATUS "${input_file}")
$ cmake -S . -B build -Dinput_file=foo.txt
-- /path/to/foo.txt
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
查看文档:
It is possible for the cache entry to exist prior to the call but have no type set if it was created on the cmake(1) command line by a user through the
-D<var>=<value>
option without specifying a type. In this case the set command will add the type. Furthermore, if the<type>
isPATH
orFILEPATH
and the<value>
provided on the command line is a relative path, then the set command will treat the path as relative to the current working directory and convert it to an absolute path.
此处:https://cmake.org/cmake/help/latest/command/set.html?highlight=current%20working%20directory
相关地,当运行处于脚本模式(而不是项目模式)时,设置了几个常用变量到当前工作目录:
When run in
-P
script mode, CMake sets the variablesCMAKE_BINARY_DIR
,CMAKE_SOURCE_DIR
,CMAKE_CURRENT_BINARY_DIR
andCMAKE_CURRENT_SOURCE_DIR
to the current working directory.
https://cmake.org/cmake/help/latest/variable/CMAKE_SOURCE_DIR.html