前言
最近有个想法,想把ineedle整体架构从头自己编写代码来实现一下,来加深对iNeedle系统的理解,同时加强Linux + C相关知识。由于iNeedle系统的庞大,只能是先把框架搭起来,根据某些功能再往里边添加东西。首先遇到的问题就是每写一部分代码都要进行调试,既不想使用gcc独立的命令来进行调试,这样代码多了或路径复杂就难以控制;又不想使用iNeedle原版的编译文件,于是自己按照旧版本抽取出需要编译iNeedle系统的脚本代码来。这个脚本用来编译iNeedle项目,主要是利用了bakefile工具。bakefile是一个跨平台的自动生成makefile的开源工具,需要在项目中的每个子目录中指定***.bkl配置文件(该配置文件指定需要编译哪些文件,指定头文件等),bakefile利用每个子目录中bkl文件来生成相应的makefile文件;然后在主目录(编译文件所在目录)中生成主makefile文件,由主makefile直接进行系统编译,生成可执行程序。
bakefile安装:
如果CentOS系统:
yum install bakefile -y
如果debian或ubuntu系统:
需要源码安装,可以到官网下载bakefile-0.2.9.tar.gz版本的即可:
tar -zxvf bakefile-0.2.9.tar.gz; cd bakefile-0.2.9; ./configure; make; make install
bakefile使用
bakefile -f gnu "$folder.bkl" -o "$folder.mk" >/dev/null 2>&1
单独使用使用make命令进行编译:
make -f ineedle.mk install
bkl文件参考
1 <?xml version="1.0"?> 2 <makefile> 3 <template id="posT"> 4 <include>../include/pcap</include> 5 <include>../include/</include> 6 <include>./</include> 7 <sources>pos/pos_lib.c</sources> 8 <sources>pos/pos_var.c</sources> 9 <sources>pos/db_var.c</sources> 10 <sources>pos/pos_db.c</sources> 11 <sources>pos/hk.c</sources> 12 <sources>pos/hashfunctions.c</sources> 13 <sources>pos/md5.c</sources> 14 <sources>pos/ios_time.c</sources> 15 <sources>pos/pos_ut.c</sources> 16 <sources>pos/ios_sparse.c</sources> 17 <sources>pos/pos_lock.c</sources> 18 <sources>pos/iweb_api.c</sources> 19 <sources>pos/db_clean.c</sources> 20 <sources>pos/pos_mem.c</sources> 21 </template> 22 <exe id="pos_obj" template="posT"></exe> 23 </makefile>
iNeedle系统编译脚本
1 #!/bin/bash 2 # 3 # 这个编译文件是从老版本compile.dat中提取出来单独编译ineedle的,目的是用来调试ineedle使用的。 4 # 5 project_name="ineedle" 6 project_modl="root shell pos cfg ilog session sha timer traf filter istat lex monitor cron http report diskdb dll alarm snmp persist system" 7 project_shuc="nd" 8 project_targ="ineedle" 9 project_cmpa="-L../lib/linux -lpcap -lmysqlclient -lhasp_linux_96828 -rdynamic -ldl -lnetsnmp" 10 project_macr="DBG DBG2 INEEDLE_ROOT NCURSES _INEEDLE_AMON _INEEDLE_USTAT _INEEDLE_URL _INEEDLE_CIP _INEEDLE_WEBDELAY _INEEDLE_AMON_ABN _INEEDLE_ALARM _INEEDLE_ELOG _INEEDLE_POST _FILE_OFFSET_BITS=64 _LARGEFILE_SOURCE _LARGEFILE64_SOURCE " 11 12 compileFlag="debug" 13 splitter="------------------------------------------------------" 14 SECFLAG=`expr 0` 15 16 function show_copyright() 17 { 18 tput clear 19 tput smso 20 echo $splitter 21 echo " iNeedle Compiler ©right dztec.cn " 22 echo $splitter 23 echo 24 tput rmso 25 } 26 27 function loopmodules() 28 { 29 modules=$project_modl 30 echo $spiltter 31 echo "generating module files......" 32 echo $spiltter 33 34 for folder in $modules;do 35 #echo $folder 36 folder=`expr $folder | tr -d ‘ ‘` 37 filepath="$folder/$folder.bkl" 38 #echo $filepath 39 if [ ! -r "$filepath" ];then 40 echo -n -e "file: ‘$filepath‘ doesnot exit!" 41 tput blink 42 echo -e "\tPlz check the file" 43 tput sgr0 44 SECFLAG=`expr 1` 45 continue 46 fi 47 48 cd "$folder" 49 folderlen=`expr length $folder` 50 if [ $folderlen -lt 3 ];then 51 echo -n -e "generating $folder.mk ...... \t\t\t\t" 52 else 53 echo -n -e "generating $folder.mk ...... \t\t\t" 54 fi 55 56 if ! bakefile -f gnu "$folder.bkl" -o "$folder.mk" >/dev/null 2>&1 ;then 57 tput smso 58 tput smul 59 echo "[FAILED]" 60 cd "../" 61 tput rmso 62 tput sgr0 63 SECFLAG=`expr 1` 64 continue 65 fi 66 tput blink 67 tput smul 68 echo "[OK]" 69 tput sgr0 70 cd "../" 71 done 72 } 73 74 function genemake() 75 { 76 target=ineedle 77 compileMacro=$project_macr 78 targetMakeFile="ineedle.mk" 79 modules=$project_modl 80 compile_arg=$project_cmpa 81 if [ $compileFlag == "debug" ];then 82 CFLAGS="CFLAGS=-g" 83 release="-g" 84 for flg in $compileMacro 85 do 86 CFLAGS="$CFLAGS -D$flg" 87 done 88 fi 89 rm $targetMakeFile >/dev/null 2>&1 90 echo "TARGET = $target" >> $targetMakeFile 91 echo "$CFLAGS" >> $targetMakeFile 92 HEAD="" 93 94 for folder in $modules 95 do 96 FOLD=`expr $folder | tr a-z A-Z` 97 HEAD="$HEAD \$("$FOLD"_OBJ_OBJECTS)" 98 echo "include $folder/$folder.mk" >> $targetMakeFile 99 done 100 101 echo >> $targetMakeFile 102 echo "install:$target">>$targetMakeFile 103 echo >> $targetMakeFile 104 echo "$target:$HEAD">>$targetMakeFile 105 echo -e -n "\t\$(CC)$HEAD $release -o $target $compile_arg" >>$targetMakeFile 106 107 } 108 109 function parseerror() 110 { 111 echo $splitter 112 warnings=`cat .temp | grep -E "warning" | wc -l` 113 tput smso 114 echo -n "WARNINGS: " 115 tput rmso 116 echo $warnings 117 tput smso 118 echo -n "ERRORS: " 119 echo "--" 120 tput rmso 121 echo 122 cat .temp | grep -E -i "cannot|multiple|undefined|Error|Stop" | sed ‘s/.*\/\(.*\/.*\)/\1/‘ 123 echo $splitter 124 } 125 126 function move_compile_files() 127 { 128 target="ineedle" 129 mv *.o ../obj >/dev/null 2>&1 130 mv *.d ../obj >/dev/null 2>&1 131 mv ./$target ../release/ >/dev/null 2>&1 132 chmod u+s ../release/$target 133 chmod g+s ../release/$target 134 } 135 136 function compile() 137 { 138 target=ineedle 139 targetMakeFile=ineedle.mk 140 if make -f $targetMakeFile install >.temp 2>&1 ;then 141 echo $splitter 142 tput smso 143 echo "Compiled successfully!" 144 tput rmso 145 echo $splitter 146 echo 147 else 148 parseerror 149 tput smso 150 echo "Compiling failed!" 151 tput rmso 152 echo $spiltter 153 fi 154 cat .temp | grep -V ".mk\|gcc -c -o" >.warning 155 } 156 157 158 function run() 159 { 160 pnn="ineedle" 161 162 tput smso 163 echo $splitter 164 tput rmso 165 tput smso 166 echo "compiling $pnn......" 167 tput rmso 168 tput smso 169 echo $splitter 170 tput rmso 171 172 loopmodules 173 174 if [ $SECFLAG == 1 ]; then 175 echo $splitter 176 tput smso 177 echo "error happened" 178 tput rmso 179 echo $splitter 180 exit 181 fi 182 183 genemake 184 compile 185 move_compile_files 186 } 187 188 function load_header_file() 189 { 190 if [ -r "./ineedle-linux.h" ];then 191 cp ineedle-linux.h ineedle.h 192 fi 193 } 194 195 load_header_file 196 show_copyright 197 run
原文:http://www.cnblogs.com/liwei0526vip/p/4977396.html