zoukankan      html  css  js  c++  java
  • 命令模式——示例——程序组织

    #在内核中将生成的目标文件与源文件放在一起,此处将生成的目标文件放在一个目录下,类似于 android的lk目录下的组织。
    #当头文件更新时,更新cpp文件的时间戳,进而重新生成依赖文件,并执行源文件到目标文件的编译。
    #tools目录下的depend.sh,可以直接写在Makefile中,但是太长,所以单独列出。
    #本文件在运行时,显示“has modification time 3.2e+05s in the future",系统的时间混乱了(强制关机时,ubuntu好像都这样),设置正确的系统时间即可
    #如您无意看到此肆文(当然,所有内容均备自己查阅),有不当之处,请指出。
     1 # Makefile
     2 
     3 #关于下文中{}与()的不良用法说明:
    4 #1、承如徐海兵先生译文所述,应保文件中()与{}用法的一致性。 5 #2、本实例中之所以没有在环境中统一()与{}的用法,但秉承: 6 # ()用于函数调用 7 # {}用于变量 8 # 原因在于:bash中区分()与{},so…….如果先学习Makefile,请遵循良好的习惯 9 10 include rule.mk 11 target = mytest 12 GCC = g++ 13 14 .PHONY : all 15 all : ${target} 16 17 ${target} : ${objects} 18 ${Q}${GCC} -o $@ $^ 19 20 #注意: 21 # include 放在最终目标关系之下,否则会造成将include文件的目标作为最终目标 22 ifneq ($(filter-out distclean clean tags, ${MAKECMDGOALS}), ) 23 -include ${depends} 24 endif 25 ifeq (${MAKECMDGOALS}, ) 26 sinclude ${depends} 27 endif 28 29 #1、此处主要生成cpp与h之间的依赖关系 30 # 一般情况下,cpp与h之间没有什么依赖关系, 31 # 在depened.sh中主要是在命令行写明:如相关的h文件变化,则更新cpp的时间戳, 32 # 从而使得目标文件也变化. 33 #2、当然我们也可以轻松地写出:%.d, %.o: %.cpp的规则,承如徐海兵先生译文所示; 34 # 此处则采用如上1中解释所述的规则(自造). 35 #3、使用续航符,以使得在同一shell进程中执行. 36 #4、@放在行首. 37 ${DEP}/%.d : ${CUR_DIR}/%.cpp 38 ${Q}${GCC} -MM ${CFLAGS} $< -MF $@; \ 39 mv $@ $@.$$$$; \ 40 /bin/bash tools/depend.sh $@.$$$$ > $@; \ 41 rm $@.$$$$ 42 43 ${OBJ}/%.o : ${CUR_DIR}/%.cpp 44 ${Q} ${GCC} -c ${CFLAGS} $< -o $@ 45 46 47 .PHONY : distclean clean tags 48 distclean : clean 49 -$(RM) -r tags 50 51 clean : 52 -$(RM) -r ${target} ${OBJ} ${DEP} 53 54 tags : 55 ctags -R 56 57 .PHONY: FROCE 58 FORCE : ;
     1 # rule.mk
     2 
     3 # V=1时不显示命令执行过程
     4 ifeq ("$(origin V)", "command line")
     5   BUILD_VERBOSE = ${V}
     6 endif
     7 ifndef BUILD_VERBOSE
     8   BUILD_VERBOSE = 0
     9 endif
    10 
    11 ifeq (${BUILD_VERBOSE}, 1)
    12   Q = @
    13 else
    14   Q = 
    15 endif
    16 
    17 SHELL := /bin/bash
    18 
    19 CUR_DIR := $(shell pwd)
    20 
    21 INCS := ${CUR_DIR}/include
    22 CFLAGS := -Wall -I${INCS}
    23 
    24 DEP := ${CUR_DIR}/depend
    25 
    26 OBJ := ${CUR_DIR}/object
    27 
    28 ifeq ($(wildcard ${DEP}), )
    29   $(shell mkdir -p ${DEP})
    30 endif
    31 
    32 ifeq ($(wildcard ${OBJ}), )
    33   $(shell mkdir -p ${OBJ})
    34 endif
    35 
    36 src_cpp = $(wildcard *.cpp)
    37 objects = $(patsubst %.cpp, ${OBJ}/%.o, $(src_cpp))
    38 depends = $(patsubst %.cpp, ${DEP}/%.d, $(src_cpp))
    
    
     1 #! /bin/sh
     2 
     3 depend_temp=/tmp/depend.$$$$
     4 rm -rf ${depend_temp}
     5                                                                                                                     
     6 #注意对首行的解析,尤其是续航符后不能有其它字符(立即换行)
     7 #sed -n -e '1p' $1 | cut -d ' ' -f 2- | awk 'BEGIN{x = 1} {printf "%s : ", $x; x++} END {while (x < NF) {printf "%s", $x; x++} if(NF > 1){printf "%s" ,$NF} printf "\n"}' > ${depend_temp}
     8 
     9 sed -n -e '1p' $1 | cut -d ' ' -f 2- > ${depend_temp}
    10 
    11 sed -n -e '1p' ${depend_temp} | grep '^\\' 1>&/dev/null && rm ${depend_temp} && touch ${depend_temp}
    12 
    13 awk 'NR>1 {print $0}' $1 >> ${depend_temp}
    14 #tail -n +2 $1 >> ${depend_temp}
    15 
    16 cp ${depend_temp} ${depend_temp}.bak
    17 
    18 rm ${depend_temp} && touch ${depend_temp}
    19 
    20 sed -n -e '1p' ${depend_temp}.bak | awk 'BEGIN{x =1 }  {printf "%s : ", $x; x++} END{while (x < NF) {printf "%s", $x; x++} if(NF >1) {printf "%s", $NF} printf "\n"}' > ${depend_temp}
    21 awk 'NR>1 {print $0}' ${depend_temp}.bak >> ${depend_temp}
    22 rm ${depend_temp}.bak
    23 
    24 add_line=`head -n 1 ${depend_temp} | cut -d ' ' -f 1`
    25 #在最后一行加入touch命令
    26 #${parameter%word},可以看下bash中的此用法
    27 echo -e "\ttouch ${add_line}" >> ${depend_temp}
    28 cat ${depend_temp}
    29 
    30 rm -rf ${depend_temp}
    
    
    
    
    
  • 相关阅读:
    Day 20 初识面向对象
    Day 16 常用模块
    Day 15 正则表达式 re模块
    D14 模块 导入模块 开发目录规范
    Day 13 迭代器,生成器,内置函数
    Day 12 递归,二分算法,推导式,匿名函数
    Day 11 闭包函数.装饰器
    D10 函数(二) 嵌套,命名空间作用域
    D09 函数(一) 返回值,参数
    Day 07 Day08 字符编码与文件处理
  • 原文地址:https://www.cnblogs.com/openix/p/3132441.html
Copyright © 2011-2022 走看看