Automatically distill headers from other project

catch_inc.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
 
dir_cur=`pwd`
dir_inc=original_inc
dir_src=/path/to/other/project
 
withly()
{
	[ -e $2 ] && $*
}
 
withoutly()
{
	[ -e $2 ] || $*
}
 
withly rm inc.makefile1
withly rm Makefile
 
if [ x$1 == xclean ] ; then
	withly rm inc.makefile
	withly rm ${dir_inc} -Rf
	exit 0
fi
 
withoutly mkdir ${dir_inc}
withoutly touch inc.makefile
 
while [ 1 ] ; do
	cat inc.makefile | sort | uniq > inc.makefile1
	cp inc.makefile1 inc.makefile
	cat inc.makefile | sed ':t;N;s/\n//;b t' | sed 's/\//\\\//g' | sed 's/\ /\\\ /g' > inc.makefile1
	sed "s/@original_includes@/`cat inc.makefile1`/g" Makefile.pat > Makefile
	lost_header=`make 2>&1 | grep "#include" | awk '{if ($2 == "|" && $3 == "#include") print $4}' | awk -F\" '{print $2}'`
	if [ x${lost_header} == x ] ; then
		break
	fi
	found_header=`cd ${dir_src}; find . -name ${lost_header} | head -n 1`
	echo ${found_header}
	found_header=${found_header##./}
	( cd ${dir_src}; cp --path ${found_header} ${dir_cur}/${dir_inc}/ )
	found_header=`dirname ${found_header}`
	echo "-I${dir_inc}/${found_header} " | cat >> inc.makefile
done

For an example, pattern file Makefile.pat is as

1
2
3
4
...
CFLAGS=-g -Iinclude @original_includes@
LDFLAGS=-pthread -lm
...

from which the catch_inc.sh script can generate Makefile like

1
2
3
4
...
CFLAGS=-g -Iinclude -Ioriginal_inc/subdir/in/other/project/inc1 -Ioriginal_inc/subdir/in/other/project/common/inc2
LDFLAGS=-pthread -lm
...

refer to:
https://www.cnblogs.com/liqiu/p/4506508.html

references to including header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <memory>
unique_ptr
shared_ptr
enable_shared_from_this
 
#include <algorithm>
remove
find
remove_if
find_if
 
#include <sstream>
istringstream
ostringstream
stringstream
 
#include <stdint.h>
int64_t
 
#include <unistd.h>
sleep