forked from OpenGenus/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_dependencies.make
94 lines (78 loc) · 4.8 KB
/
generate_dependencies.make
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#for Cpp
.PHONY: all generate_dependency append_command
all: generate_dependency append_command;
G++FLAGS = -Wall -std=c++11
# warning: the '^^^^^^^^^^' cannot be used in file-name
RECOVER-NAME = $(subst ^^^^^^^^^^,\ ,$(strip $1))
RECOVER-NAME2 = $(subst ^^^^^^^^^^,\\\ ,$(strip $1))
CONVERT-CPP-TO-DEPENDENCY-NAME = $(subst .cpp,.d,$(1))
CONVERT-DEPENDENCY-TO-CPP-NAME = $(subst .d,.cpp,$(1))
FIND-CPP-TESTS = $(shell find -name 'test_*.cpp' | sed 's: :^^^^^^^^^^:g')
FIND-CPP-SOURCES = $(filter-out $(cpp_tests),$(cpp_all_files))
FIND-CPP-TEST-DEPENDENCIES = $(shell find -name 'test_*.d' | sed 's: :^^^^^^^^^^:g')
FIND-CPP-SOURCE-DEPENDENCIES = $(filter-out $(cpp_test_dependencies),$(cpp_all_dependencies))
cpp_all_files = $(shell find -name '*.cpp' | sed 's: :^^^^^^^^^^:g')
cpp_tests = $(call FIND-CPP-TESTS)
cpp_sources = $(call FIND-CPP-SOURCES)
# the var i is used for prevent if has same target name
ins = $(eval i=$(shell echo $$(($(i)+1))))
# get dependencies of all files,
# only contains one target:prerequisites in each file.
# e.g. %.o: %.cpp other.cpp\n
GENERATE-SOURCE-DEPENDENCIES = @$(foreach file,$(cpp_sources),echo printf $(i) '>' $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
printf '${i}' > $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
echo $(CXX) -MM $(call RECOVER-NAME,$(file)) '>>' $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
$(CXX) -MM $(call RECOVER-NAME,$(file)) >> $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
echo "cat $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)))";\
cat $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
printf $(i)$(notdir $(call RECOVER-NAME2,$(subst .cpp,.o,$(file))))" " >> dependencies_list;\
echo $(call ins);\
echo "";)
GENERATE-TEST-DEPENDENCIES = @$(foreach file,$(cpp_tests),echo printf $(i) '>' $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
printf '${i}' > $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
echo $(CXX) -MM $(call RECOVER-NAME,$(file)) '>>' $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
$(CXX) -MM $(call RECOVER-NAME,$(file)) >> $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
echo "cat $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)))";\
cat $(call RECOVER-NAME,$(call CONVERT-CPP-TO-DEPENDENCY-NAME,$(file)));\
printf $(i)$(notdir $(call RECOVER-NAME2,$(subst .cpp,.o,$(file))))" " >> dependencies_list;\
echo $(call ins);\
echo "";)
##############################################
# add target and prerequisites to dependencies
##############################################
generate_dependency:
@echo "##############################\n\
\r# generate_dependency start. #\n\
\r##############################"
# clear list
@printf "" > dependencies_list
$(call GENERATE-SOURCE-DEPENDENCIES)
$(call GENERATE-TEST-DEPENDENCIES)
@echo "############################\n\
\r# generate_dependency end. #\n\
\r############################"
cpp_all_dependencies = $(shell find -name '*.d' | sed 's: :^^^^^^^^^^:g')
cpp_test_dependencies = $(call FIND-CPP-TEST-DEPENDENCIES)
cpp_source_dependencies = $(call FIND-CPP-SOURCE-DEPENDENCIES)
APPEND-SOURCE-COMMAND = @$(foreach file,$(cpp_source_dependencies),echo '$(CXX) -c $(G++FLAGS) $(call RECOVER-NAME,$(call CONVERT-DEPENDENCY-TO-CPP-NAME,$(file)))' '>>' $(call RECOVER-NAME,$(file));\
echo '\t$(CXX) -c $(G++FLAGS) $(call RECOVER-NAME,$(call CONVERT-DEPENDENCY-TO-CPP-NAME,$(file)))' >> $(call RECOVER-NAME,$(file));\
echo "cat $(call RECOVER-NAME,$(file))";\
cat $(call RECOVER-NAME,$(file));\
echo "";)
APPEND-TEST-COMMAND = @$(foreach file,$(cpp_test_dependencies),echo '$(CXX) -o $(G++FLAGS) $(call RECOVER-NAME,$(call CONVERT-DEPENDENCY-TO-CPP-NAME,$(file)))' '>>' $(call RECOVER-NAME,$(file));\
echo '\t$(CXX) -o $(G++FLAGS) $(call RECOVER-NAME,$(call CONVERT-DEPENDENCY-TO-CPP-NAME,$(file)))' >> $(call RECOVER-NAME,$(file));\
echo "cat $(call RECOVER-NAME,$(file))";\
cat $(call RECOVER-NAME,$(file));\
echo "";)
############################
# add command to dependencies
############################
append_command:
@echo "#########################\n\
\r# append_command start. #\n\
\r#########################"
$(call APPEND-SOURCE-COMMAND)
$(call APPEND-TEST-COMMAND)
@echo "########################\n\
\r# append_command done. #\n\
\r########################"