Last active
December 15, 2015 15:09
-
-
Save xsddz/5279719 to your computer and use it in GitHub Desktop.
根据源码生成 makefile 文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# filename: create_makefile.sh | |
# | |
# function definiton {{{ | |
#=============================================================================== | |
usage() | |
{ | |
echo "Usage: $script_file_name [options] {main_source_program_name [...]}" | |
echo | |
echo "description:" | |
echo "the first specified source_program_name must be the the program" | |
echo "which has the main funtion, you can also specify the" | |
echo "auxiliary program name after it if needed." | |
echo | |
echo "options:" | |
echo " -h, --help: print this help page." | |
echo " -r : used for a release version." | |
echo " -e {name} : specify the the final name for execute the project." | |
exit 0 | |
} | |
error_option() | |
{ | |
echo "You had misused the $script_file_name shell script" | |
echo "Enter $script_file_name -h for help." | |
exit 1 | |
} | |
set_main_target_name() | |
{ | |
main_target_name="$1" | |
#echo $main_target_name | |
} | |
create_main_target() | |
{ | |
curr_filename=$(basename -- $1) | |
if [ "X$main_target_name" = "X" ]; then | |
set_main_target_name ${curr_filename%.c} | |
fi | |
if [ "X$src_prgm_name_set" != "X" ]; then # same as [ "X$object_set" != "X" ]; then | |
src_prgm_name_set="$src_prgm_name_set $curr_filename" | |
object_set="$object_set $(echo $curr_filename | sed 's/\(.*\)\.c/\1.o/')" | |
else | |
src_prgm_name_set=$curr_filename | |
object_set=$(echo $curr_filename | sed 's/\(.*\)\.c/\1.o/') | |
fi | |
#echo $curr_filename | |
#echo $src_prgm_name_set | |
#echo $object_set | |
} | |
create_obj_target() | |
{ | |
curr_filename=$(basename -- $1) | |
if [ "X$object_target_set" != "X" ]; then | |
object_target_set="${object_target_set}\n$($compiler -MM ${curr_path}$curr_filename)" | |
else | |
object_target_set=$($compiler -MM ${curr_path}$curr_filename) | |
fi | |
#echo $curr_filename | |
#echo -e $object_target_set | |
} | |
change_cflags() | |
{ | |
cflags=$(echo $cflags | sed 's/\(.*\) *-g \(.*\)/\1 \2/') | |
#echo $cflags | |
} | |
create_makefile() | |
{ | |
echo "# start makefile" | |
echo "#" | |
echo | |
echo "CC = gcc" | |
echo "CFLAGS = $cflags" | |
echo "RM = rm -f" | |
echo | |
echo "all = $main_target_name" | |
echo "objects = $object_set" | |
echo | |
echo | |
echo -e "\$(all): \$(objects)" | |
echo -e "\t\$(CC) \$(CFLAGS) -o \$@ \$(objects)" | |
echo | |
echo -e "$object_target_set" | |
echo | |
echo | |
echo ".PHINY: clean clean_exe" | |
echo "clean:" | |
echo -e "\t-\$(RM) *.o" | |
echo "clean_exe:" | |
echo -e "\t-\$(RM) *.exe" | |
echo "#" | |
echo "# end makefile" | |
} | |
#=============================================================================== | |
# }}} | |
# here we start the script | |
[ $# -eq 0 ] && exit 0 | |
curr_path='./' | |
script_file_name=$(basename $0) | |
compiler=gcc | |
cflags="-g -Wall -O3" | |
src_prgm_name_set="" | |
curr_filename="" | |
main_target_name="" | |
object_set="" | |
object_target_set="" | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-e) | |
shift | |
[ $# -eq 0 ]\ | |
|| [ "X$1" = "X-h" ]|| [ "X$1" = "X--help" ]\ | |
|| [ "X$1" = "X-e" ] || [ "X$1" = "X-r" ]\ | |
&& error_option | |
set_main_target_name $1 | |
;; | |
-r) | |
change_cflags | |
;; | |
-h | --help) | |
usage $script_file_name | |
;; | |
-[[:alpha:]]) | |
error_option | |
;; | |
*) | |
create_main_target $1 | |
create_obj_target $1 | |
;; | |
esac | |
shift | |
done | |
[ "X$src_prgm_name_set" = "X" ] && error_option | |
printf 'creating "Makefile.auto"..........' | |
# backup original makefile which havn't used anymore {{{ | |
#[ -e Makefile ] && mv Makefile Makefile.old | |
#[ -e makefile ] && mv makefile makefile.old | |
# }}} | |
create_makefile > Makefile.auto | |
printf "\t[ok]\n" | |
# print variable's value which haven't used anymore {{{ | |
#echo "curr_path: $curr_path" | |
#echo "script_filename: $script_file_name" | |
#echo "compiler: $compiler" | |
#echo "cflags: $cflags" | |
#echo "src_prgm_name_set: $src_prgm_name_set" | |
#echo "curr_filename: $curr_filename" | |
#echo "main_target_name: $main_target_name" | |
#echo "object_set: $object_set" | |
#echo "object_target_set: $object_target_set" | |
# }}} | |
unset curr_path curr_filename script_file_name compiler cflags src_prgm_name_set main_target object_set object_target_set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment