《cmake调用shell》

版权声明:copyright@xxdk2017 https://blog.csdn.net/u011583798/article/details/78444914

1. CMakeLists.txt

add_custom_target(config ALL
    COMMAND bash x.sh  
)

2. shell

#########################################################################
# File Name: x.sh
# Author: XXDK
# Created Time: Wed 01 Nov 2017 05:08:50 PM CST
#########################################################################

#!/bin/bash

# masking all out put info!
set +x 

#-----------------------------------------------
# function fprint()
#-----------------------------------------------
fprint() 
{
	#if [ 1 ] 
	if [ ! 1 ] 
	then
		echo -n "Func: $FUNCNAME Line: $LINENO " && echo $1;
	fi
}

#-----------------------------------------------
# global variable value
#-----------------------------------------------
exec_path=`pwd`;
fprint "exec_path: $exec_path";

config_file_path=`cd ../config_file; pwd`;
fprint "config_file_path: $config_file_path";

src_root_path=`cd ../; pwd`;
fprint "src_root_path: $src_root_path";

out_directory="$src_root_path/out";
fprint "out_directory: $out_directory";

config_file_list=`cd ../config_file; ls`;
config_file_common='config_file_common.conf';
out_dir_com_prefix='cleanna';
config_file_count=0;

#-----------------------------------------------
# function total_config_file() 
#-----------------------------------------------
clean_out_directory()
{
	if [ -d ../out ]
	then
		rm ../out -rf;
	fi
}
#-----------------------------------------------
# function total_config_file() 
#-----------------------------------------------
total_config_file() 
{
	for file in $config_file_list
	do
		if [ -f $1/$file ]
		then 
			config_file_count=`expr $config_file_count + 1`;
			fprint $file;
		fi
	done 

	return $config_file_count;
}

#-----------------------------------------------
# function check_file_exist() 
#-----------------------------------------------
check_file_exist()  
{
	if [ -f $1 ]
	then
		fprint "get $1";
		return 1;
	else 
		fprint "no $1";
		return 0;
	fi
}

#-----------------------------------------------
# function create_out_directory() 
#-----------------------------------------------
create_out_directory() 
{
	if [ ! -d $1 ]
	then
		mkdir $1;
		fprint "create $1 success!";
		return 1;
	else 
		fprint "$1 already exist!";
		return 0;
	fi
}

#-----------------------------------------------
# function process_config_file()
#-----------------------------------------------
process_config_file() 
{
	local target_dir=$1;
	if [ ! -d "$target_dir/app" ]  # out/cleannaXXXX/app
	then 
		mkdir "$target_dir/app";
		touch "$target_dir/app/config_file.conf"; #out/cleannaXXXX/app/config_file.conf
		cat "$config_file_path/$config_file_common" > "$target_dir/app/config_file.conf";
		cat "$config_file_path/$file" >> "$target_dir/app/config_file.conf";
		return 1;
	else 
		fprint "$target_dir/app already exist!";
		return 0;
	fi
}

#-----------------------------------------------
# function process_bootauto_file()
#-----------------------------------------------
process_bootauto_file() 
{
	local target_dir=$1;
	if [ ! -d "$target_dir/boot" ]  # out/cleannaXXXX/boot
	then 
		mkdir "$target_dir/boot";
		check_file_exist "$config_file_path/boot/BootAuto.sh"
		if [ $? = 1 ] 
		then 
			#out/cleannaXXXX/boot/BootAuto.sh
			cp "$config_file_path/boot/BootAuto.sh" "$target_dir/boot"; 
			return 1;
		else 
			fprint "check $config_file_path/boot/BootAuto.sh error";
			return 0;
		fi
	else 
		fprint "$target_dir/boot/BootAuto.sh already exist!";
		return 0;
	fi
}

#-----------------------------------------------
# function process_small_vex_file()
#-----------------------------------------------
process_small_vex_file() 
{
	local target_dir=$1;
	check_file_exist "$config_file_path/small_voc.txt.compressed"
	if [ $? = 1 ] 
	then 
		#out/cleannaXXXX/app/small_voc.txt.compressed
		cp "$config_file_path/small_voc.txt.compressed" "$target_dir/app"; 
		return 1;
	else 
		fprint "check $config_file_path/small_voc.txt.compressed error";
		return 0;
	fi
}

#-----------------------------------------------
# function process_small_vex_file()
#-----------------------------------------------
process_sta_wifi_file() 
{
	local target_dir=$1
	check_file_exist "$config_file_path/boot/sta_wifi_common.sh"
	if [ $? = 1 ] 
	then 
		#out/cleannaXXXX/boot/sta_wifi.sh
		cp "$config_file_path/boot/sta_wifi_common.sh" "$target_dir/boot/sta_wifi.sh"; 
		return 1;
	else 
		fprint "check $config_file_path/boot/sta_wifi_common.sh error";
		return 0;
	fi
}

#-----------------------------------------------
# function print_out_directory() 
#-----------------------------------------------
print_out_directory() 
{
	out_config_file_dir=`cd ../out; ls`;
	for dir in "$out_config_file_dir"
	do 
		fprint $dir;
	done 
}

#-----------------------------------------------
# function main_task() 
#-----------------------------------------------
main_task() 
{
	for file in $config_file_list
	do
		if [ -f "$config_file_path/$file" ]
		then 
			if [ ${file#*.} = "conf" ] #extract file which has ".conf" suffix
			then
				file_no_conf_suffix=${file%.*}; #discard suffix ".conf"
				fprint ${file_no_conf_suffix};
				file_no_cfc_prefix=${file_no_conf_suffix##*_}; #cfc confile_file_cleanna_
				fprint ${file_no_cfc_prefix};

				if [ ! -d "$out_directory""/""$out_dir_com_prefix""$file_no_cfc_prefix" ]
				then 
					local target_dir="$out_directory""/""$out_dir_com_prefix""$file_no_cfc_prefix";    # out/cleannaXXXX/
					mkdir $target_dir;    # out/cleannaXXXX/
					# 1. config_file 
					process_config_file $target_dir;
					if [ $? = 1 ]; then 
						fprint "process config file success!";
					else 
						fprint "process config file failed!";
					fi
					# 2. boot 
					process_bootauto_file $target_dir;
					if [ $? = 1 ]; then 
						fprint "process bootauto file success!";
					else 
						fprint "process bootauto file failed!";
					fi
					# 3. small_voc
					process_small_vex_file $target_dir;
					if [ $? = 1 ]; then 
						fprint "process small_voc file success!";
					else 
						fprint "process small_voc file failed!";
					fi
					# 4. sta_wifi
					process_sta_wifi_file $target_dir;
					if [ $? = 1 ]; then 
						fprint "process sta_wifi file success!";
					else 
						fprint "process sta_wifi file failed!";
					fi
				else 
					fprint ""$out_directory""/""$out_dir_com_prefix""$file_no_cfc_prefix" already exist!";
					return 0;
				fi
			fi
			config_file_count=`expr $config_file_count + 1`;
			fprint $file;
		fi
	done 
	return 1;
}

#-----------------------------------------------
# function main() 
#-----------------------------------------------
main() 
{
	# 1.---------------------------------------
	clean_out_directory;

	# 2.---------------------------------------
	total_config_file $config_file_path;
	if [ $? = 0 ]; then 
		fprint "no config file to process!";
	else 
		fprint "total config file: $? ";
	fi
	
	# 3.---------------------------------------
	check_file_exist "$config_file_path/$config_file_common" 
	if [ $? = 1 ]; then 
		fprint "config_file_common.sh check pass!";
	else 
		fprint "can't find config_file_common.sh";
	fi

	# 4.---------------------------------------
	create_out_directory "$out_directory";
	if [ $? = 1 ]; then 
		fprint "Create roboserver/out directory ok";
	else 
		fprint "Create roboserver/out directory error";
	fi

	# 5.---------------------------------------
	main_task;
}

#-----------------------------------------------
# running extry main
#-----------------------------------------------
#set +x
while true
do 
	main;
	break;
done 
set -x








猜你喜欢

转载自blog.csdn.net/u011583798/article/details/78444914