此脚本自动生成 makefile 依赖规则

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

此脚本自动生成 makefile 依赖规则

#1

帖子 cjxgm » 2011-08-09 20:31

暂定名为 Automatic Dependencies Solving Makefile Generating Configure Script :em06
[bash]#!/bin/bash

# Automatic Dependencies Solving Makefile Generating Configure Script
# Written by eXerigumo Clanjor

# CUSTOMIZE
MAIN="main"
LIBS="-lglut -lGL -lGLU"

# GLOBAL VARS
CXXFLAGS="-s"
CXXOPTIMIZE="-O2"
DEPS=()
OBJS=()

# -- void main(argv)
main()
{
# parse arguments
while ! [ -z "$1" ]; do
case $1 in
"--help") show_help
;;
"-h") show_help
;;
"--enable-debug") CXXFLAGS="-g -DDEBUG"
CXXOPTIMIZE="" # No optimization
shift ;;
"-Ofast") CXXOPTIMIZE="-Ofast"
shift ;;
"--clean") make cleanall
rm -f makefile
exit 0
;;
*) error "unknown argument: $1"
;;
esac
done

generate_makefile
}

# -- void generate_makefile(void)
generate_makefile()
{
echo -e "# Generated by ADSMGCS." > makefile
echo >> makefile
echo -e "CXXFLAGS = -Wall $CXXFLAGS $CXXOPTIMIZE $LIBS" >> makefile
echo >> makefile

echo -e "all: $MAIN" >> makefile
echo -e "clean:" >> makefile
echo -e "\trm -f *.o" >> makefile
echo -e "cleanall: clean" >> makefile
echo -e "\trm -f $MAIN" >> makefile
echo -e "rebuild: clean all" >> makefile
echo -e "debug: all" >> makefile
echo -e "\t./$MAIN" >> makefile
echo >> makefile

generate_dep "${MAIN}.cc"

echo -e "$MAIN:" ${OBJS[*]} >> makefile
echo -e '\t$(CXX) $(CXXFLAGS) -o $@ $^' >> makefile
cat .makefile >> makefile
echo >> makefile
rm -f .makefile
}

# -- void show_help(void)
show_help()
{
echo
echo -e "OPTIONS:"
echo -e "\t--help, -h"
echo -e "\t\tShow this help"
echo -e "\t--enable-debug"
echo -e "\t\tEnable debug mode"
echo -e "\t-Ofast"
echo -e "\t\tUse -Ofast instead of -O2"
echo

exit 0
}

# -- void error(string err)
error()
{
echo -e "\e[1;31merror:\e[m $1"
exit 1
}

# -- void generate_dep(string file)
generate_dep()
{
echo -e "Solving dependencies of $1..."

if ! g++ -MM "$1" > .dep; then
error "unexpected failure of g++."
fi
cat .dep >> .makefile
echo -e '\t$(CXX) $(CXXFLAGS) -c -o $@ $<' >> .makefile

local dep="`cat .dep|awk '{sub(/\\\\/, ""); printf $0}' \
|sed 's/[^ ]\+: [^ ]\+ \?//g'`"
local obj="`cat .dep|head -n 1|awk -F: '{printf $1}'`"
rm -f .dep

OBJS[${#OBJS[*]}]="$obj"

local d
for d in $dep; do
if need_recursive_dep "$d"; then
d="`echo $d|sed 's/\\.h$/.cc/g'`"
generate_dep "$d"
fi
done
}

# -- bool need_recursive_dep(string file)
need_recursive_dep()
{
local d
for d in ${DEPS[*]}; do
[[ "$d" == "$1" ]] && return 1
done

DEPS[${#DEPS[*]}]="$1"
return 0
}

main $*
[/bash]
此脚本利用 g++ 自动生成 makefile 依赖规则,且能顺利处理好循环依赖问题
目前只适用于 C++,后缀名要求是 .cc/.h,主文件名在全局变量 MAIN 中指定

将其保存为 configure(这个名字我喜欢 :em06 ),加上 x 权限,然后
[bash]./configure
make[/bash]
即可
上次由 cjxgm 在 2011-08-09 21:34,总共编辑 1 次。
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 此脚本自动生成 makefile 依赖规则

#2

帖子 eexpress » 2011-08-09 21:23

c++的阿。我说怎么会自动罗。
● 鸣学
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

Re: 此脚本自动生成 makefile 依赖规则

#3

帖子 cjxgm » 2011-08-09 21:35

C 也可以的嘛,改一下脚本里的后缀替换就好了(gcc 的都可以的吧)
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
MaskRay
帖子: 61
注册时间: 2010-04-29 22:00
系统: Gentoo Linux ~amd64
联系:

Re: 此脚本自动生成 makefile 依赖规则

#4

帖子 MaskRay » 2011-08-11 16:32

有些问题,比如 main.cc 引用的 xx.h 不在当前目录,那么就会在那个目录找 xx.cc,而生成的 xx.o xx.d 却在当前目录下。注意用 -MF -MT 指定目标名和生成的依赖文件名。

这件事实际上用 make 本身就能做,Makefile 如下:

代码: 全选

PROG := main
SRCS := $(PROG).c

$(PROG): $(SRCS:.c=.o)
	$(LINK.c) $^ -o $@

sinclude $(SRCS:.c=.d)

%.o: %.c
	gcc -MM -MP -MT $@ -MF $(@:.o=.d) $<
	$(COMPILE.c) $< -o $@
回复