File indexing completed on 2022-09-27 16:26:54

0001 #!/bin/sh
0002 # Copyright (C) 2013~2014 by Yichao Yu
0003 # yyc1992@gmail.com
0004 #
0005 # Redistribution and use in source and binary forms, with or without
0006 # modification, are permitted provided that the following conditions
0007 # are met:
0008 #
0009 # 1. Redistributions of source code must retain the above copyright
0010 #    notice, this list of conditions and the following disclaimer.
0011 # 2. Redistributions in binary form must reproduce the above copyright
0012 #    notice, this list of conditions and the following disclaimer in the
0013 #    documentation and/or other materials provided with the distribution.
0014 #
0015 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 
0026 helper_cache=${__CMAKE_HELPER_UTILS_CACHE}
0027 cache_base=${CMAKE_GENERAL_BASE}
0028 . "${cache_base}/cmake_helper_general_export.sh"
0029 action=$1
0030 shift
0031 
0032 __load_command_setting() {
0033     for __varname in "$@"; do
0034         eval "${__varname}="'${__command_'"${name}_${__varname}}"
0035     done
0036 }
0037 
0038 __load_all_command_settings() {
0039     . "${helper_cache}/command_${name}_setting.sh"
0040     __load_command_setting working_dir input_file output_file error_file \
0041         env_file
0042 }
0043 
0044 rewrite_cmd_settings() {
0045     name=$1
0046     shift
0047     __load_all_command_settings
0048     for var in env_file input_file output_file error_file working_dir; do
0049         quoted=${!var//\'/\'\\\'\'}
0050         printf "__command_${name}_${var}='%s'\n" "${quoted}"
0051     done > "${helper_cache}/command_${name}_setting.sh"
0052 }
0053 
0054 run_command() {
0055     name=$1
0056     shift
0057     __load_all_command_settings
0058     [ "x${env_file}x" != xx ] && {
0059         . "${env_file}" || return 1
0060     }
0061     [ "x${input_file}x" != xx ] && {
0062         exec < "${input_file}" || return 1
0063     }
0064     [ "x${output_file}x" != xx ] && {
0065         exec > "${output_file}" || return 1
0066     }
0067     [ "x${error_file}x" != xx ] && {
0068         exec 2> "${error_file}" || return 1
0069     }
0070     [ "x${working_dir}x" != xx ] && {
0071         cd "${working_dir}" || return 1
0072     }
0073     "__command_${name}_run" "$@"
0074 }
0075 
0076 clear_cache() {
0077     for path in "$@"; do
0078         [ -d "${path}" ] || continue
0079         "${CMAKE_HELPER_CMAKE_COMMAND}" -E remove_directory "${path}"
0080         "${CMAKE_HELPER_CMAKE_COMMAND}" -E make_directory "${path}"
0081     done > /dev/null 2>&1
0082 }
0083 
0084 case "$action" in
0085     --clear-cache)
0086         clear_cache "$@"
0087         exit $?
0088         ;;
0089     --run-command)
0090         run_command "$@"
0091         exit $?
0092         ;;
0093     --rewrite-cmd-settings)
0094         rewrite_cmd_settings "$@"
0095         exit $?
0096         ;;
0097 esac
0098 exit 1