如何捕获CMake的命令行参数?(How to capture CMake command line

2019-06-23 17:55发布

我想记录传递给CMake的我生成的脚本的参数。 例如,“my-config.in”将被cmake的处理,它的定义是这样的:

配置= “@ CMAKE_ARGS @”

之后cmakemy-config将包含行是这样的:

配置= “ - DLINUX -DUSE_FOO = Y -DCMAKE_INSTALL_PREFIX = / USR”

我试过CMAKE_ARGSCMAKE_OPTIONS ,但未能成功。 没有文件提到这一点。 :-(

Answer 1:

我不知道它提供该信息的任何变量,但你可以自己生成它(有一些附加条件)。

任何-D传递给CMake的参数被添加到高速缓存文件CMakeCache.txt中生成目录,并且不必再次在命令行上指定期间后续调用被重新应用。

因此,在你的例子,如果你先执行CMake的作为

cmake ../.. -DCMAKE_INSTALL_PREFIX:PATH=/usr

那么你会发现,随后只需运行

cmake .

仍会有CMAKE_INSTALL_PREFIX设置为/usr


如果你从寻找什么CMAKE_ARGS从CMake的每一个调用的命令行上定义,那么变量的完整列表下面应该做的伎俩:

get_cmake_property(CACHE_VARS CACHE_VARIABLES)
foreach(CACHE_VAR ${CACHE_VARS})
  get_property(CACHE_VAR_HELPSTRING CACHE ${CACHE_VAR} PROPERTY HELPSTRING)
  if(CACHE_VAR_HELPSTRING STREQUAL "No help, variable specified on the command line.")
    get_property(CACHE_VAR_TYPE CACHE ${CACHE_VAR} PROPERTY TYPE)
    if(CACHE_VAR_TYPE STREQUAL "UNINITIALIZED")
      set(CACHE_VAR_TYPE)
    else()
      set(CACHE_VAR_TYPE :${CACHE_VAR_TYPE})
    endif()
    set(CMAKE_ARGS "${CMAKE_ARGS} -D${CACHE_VAR}${CACHE_VAR_TYPE}=\"${${CACHE_VAR}}\"")
  endif()
endforeach()
message("CMAKE_ARGS: ${CMAKE_ARGS}")

这是一个有点脆弱,因为它依赖于一个事实,即每对已经通过命令行来设置变量的短语“在命令行上指定的任何帮助,变量”。 指定为其HELPSTRING财产。 如果CMake的更改此默认HELPSTRING ,你必须更新if相应的声明。


如果这不是你想要的CMAKE_ARGS显示,而是只从当前执行的论据,那么我不认为有办法做到这一点短黑客的CMake的源代码! 不过,我希望这不是你想要的,因为以前所有的命令行参数有效地重新应用每一次。



Answer 2:

存储CMake的命令行参数的方法之一,就是有一个叫做包装脚本~/bin/cmake (*** 1),它不两两件事:

  • 创建./cmake_call.sh存储的命令行参数
  • 调用真正cmake用命令行参数的可执行

~/bin/cmake #代码如下所示

#!/usr/bin/env bash

#
# Place this file into this location: ~/bin/cmake
# (with executable rights)
#
# This is a wrapper for cmake!
# * It calls cmake -- see last line of the script
# It also:
# * Creates a file cmake_call.sh in the current directory (build-directory)
#   which stores the cmake-call with all it's cmake-flags etc.
#   (It also stores successive calls to cmake, so that you have a trace of all your cmake calls)
#
# You can simply reinvoke the last cmake commandline with: ./cmake_call.sh  !!!!!!!!!!
#
# cmake_call.sh is not created
#   when cmake is called without any flags,
#   or when it is called with flags such as --help, -E, -P, etc. (refer to NON_STORE_ARGUMENTS -- you might need to modify it to suit your needs)

SCRIPT_PATH=$(readlink -f "$BASH_SOURCE")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")

#http://stackoverflow.com/a/13864829
if [ -z ${SUDO_USER+x} ]; then
    # var SUDO_USER is unset
    user=$USER
else
    user=$SUDO_USER
fi


#http://stackoverflow.com/a/34621068
path_append ()  { path_remove $1 $2; export $1="${!1}:$2"; }
path_prepend()  { path_remove $1 $2; export $1="$2:${!1}"; }
path_remove ()  { export $1="`echo -n ${!1} | awk -v RS=: -v ORS=: '$1 != "'$2'"' | sed 's/:$//'`"; }

path_remove PATH ~/bin # when calling cmake (at the bottom of this script), do not invoke this script again!




# when called with no arguments, don't create cmake_call.sh
if [[ -z "$@" ]]; then
   cmake "$@"
   exit
fi


# variable NON_STORE_ARGUMENTS stores flags which, if any are present, cause cmake_call.sh to NOT be created
read -r -d '' NON_STORE_ARGUMENTS <<'EOF'
-E
--build
#-N
-P
--graphviz
--system-information
--debug-trycompile
#--debug-output
--help
-help
-usage
-h
-H
--version
-version
/V
--help-full
--help-manual
--help-manual-list
--help-command
--help-command-list
--help-commands
--help-module
--help-module-list
--help-modules
--help-policy
--help-policy-list
--help-policies
--help-property
--help-property-list
--help-properties
--help-variable
--help-variable-list
--help-variables
EOF

NON_STORE_ARGUMENTS=$(echo "$NON_STORE_ARGUMENTS" | head -c -1 `# remove last newline` | sed "s/^/^/g" `#begin every line with ^` | tr '\n' '|')

#echo "$NON_STORE_ARGUMENTS" ## for debug purposes

## store all the args
ARGS_STR=
for arg in "$@"; do
    if cat <<< "$arg" | grep -E -- "$NON_STORE_ARGUMENTS" &> /dev/null; then  # don't use   echo "$arg"  ....
                                                                              # since echo "-E"    does not do what you want here,
                                                                              # but   cat <<< "-E" does what you want (print minus E)
        # do not create cmake_call.sh
        cmake "$@"
        exit
    fi

    # concatenate to ARGS_STR
    ARGS_STR="${ARGS_STR}$(echo -n " \"$arg\"" | sed "s,\($(pwd)\)\(\([/ \t,:;'\"].*\)\?\)$,\$(pwd)\2,g")"
    #                                             replace $(pwd) followed by
    #                                                                  /             or
    #                                                                   whitespace   or
    #                                                                      ,         or
    #                                                                       :        or
    #                                                                        ;       or
    #                                                                         '      or
    #                                                                           "
    #                                                                    or nothing
    #                                             with \$(pwd)
done









if [[ ! -e $(pwd)/cmake_call.sh ]]; then
echo    "#!/usr/bin/env bash" >  $(pwd)/cmake_call.sh

# escaping:
# note in the HEREDOC below, \\ means \ in the output!!
#                            \$ means $ in the output!!
#                            \` means ` in the output!!
cat <<EOF                     >>  $(pwd)/cmake_call.sh


#http://stackoverflow.com/a/34621068
path_remove ()  { export \$1="\`echo -n \${!1} | awk -v RS=: -v ORS=: '\$1 != "'\$2'"' | sed 's/:\$//'\`"; }

path_remove PATH ~/bin # when calling cmake (at the bottom of this script), do not invoke ~/bin/cmake but real cmake!


EOF
else
    # remove bottom 2 lines from cmake_call.sh
    sed -i '$ d' $(pwd)/cmake_call.sh
    sed -i '$ d' $(pwd)/cmake_call.sh
fi


echo "ARGS='${ARGS_STR}'" >> $(pwd)/cmake_call.sh
echo "echo cmake \"\$ARGS\""  >> $(pwd)/cmake_call.sh
echo "eval cmake \"\$ARGS\""  >> $(pwd)/cmake_call.sh
#echo "eval which cmake"       >> $(pwd)/cmake_call.sh

chmod +x     $(pwd)/cmake_call.sh
chown $user: $(pwd)/cmake_call.sh

cmake "$@"

用法:

mkdir build
cd    build
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$(pwd)/install ..

这将创建cmake_call.sh包含以下内容:

#!/usr/bin/env bash


#http://stackoverflow.com/a/34621068
path_remove ()  { export $1="`echo -n ${!1} | awk -v RS=: -v ORS=: '$1 != "'$2'"' | sed 's/:$//'`"; }

path_remove PATH ~/bin # when calling cmake (at the bottom of this script), do not invoke ~/bin/cmake but real cmake!


ARGS=' "-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_INSTALL_PREFIX=$(pwd)/install" ".."'
echo cmake "$ARGS"
eval cmake "$ARGS"

第三届最后一行存储cmake的论点。 现在,您可以重新调用,您使用简单地调用确切的命令行:

./cmake_call.sh

脚注:

(*** 1) ~/bin/cmake通常是由于PATH ~/.profile 。 当创建~/bin/cmake非常一日一次,它可能需要注销并重新登录方式,因此.profile中看到的~/bin



文章来源: How to capture CMake command line arguments?
标签: cmake