Makefile 4.58 KB
Newer Older
wangkaixiong's avatar
init  
wangkaixiong committed
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

cc           := hipcc
name         := pro
workdir      := workspace
srcdir       := src
# export CUDA_VISIBLE_DEVICES=0
objdir       := objs
stdcpp       := c++17
dst_so_path  := libs/alg/libEXEC.so
project_name := EXEC
demodir		 := demo
defined      := 
pwd          := $(abspath .)
# 静态推理:0  动态推理:1
run_args     := 0
nvcc         := 
cuda_arch    := 


export pwd workdir srcdir objdir demodir

demo_srcs   := $(shell find $(demodir) -name "*.cpp")
demo_objs   := $(demo_srcs:.cpp=.o)
demo_objs   := $(demo_objs:$(demodir)/%=$(objdir)/%)

cpp_srcs := $(shell find $(srcdir) -name "*.cpp")
cpp_objs := $(cpp_srcs:.cpp=.o)
cpp_objs := $(cpp_objs:$(srcdir)/%=$(objdir)/%)

cc_srcs := $(shell find $(srcdir) -name "*.cc")
cc_objs := $(cc_srcs:.cc=.o)
cc_objs := $(cc_objs:$(srcdir)/%=$(objdir)/%)

cu_srcs  := $(shell find $(srcdir) -name "*.cu")
cu_objs  := $(cu_srcs:.cu=.cu.o)
cu_objs  := $(cu_objs:$(srcdir)/%=objs/%)


dtk_path  := $(shell find /opt -name "dtk-*")

include_paths := $(dtk_path)/cuda/include \
			$(pwd)/3rdparty/opencv_4.5_lean/include/opencv4   \
			$(srcdir)	\
			. \
			$(pwd)/3rdparty/ppl/include \
			$(dtk_path)/include \
			$(srcdir)/Utility


library_paths :=  \
			$(pwd)/3rdparty/opencv_4.5_lean/lib  \
			$(pwd)/3rdparty/opencv_4.5_lean/lib64  \
			$(pwd)/3rdparty/opencv_4.5_lean/lib/opencv4/3rdparty \
			$(pwd)/3rdparty/opencv_4.5_lean/lib64/opencv4/3rdparty \
			$(pwd)/3rdparty/ppl/lib \
			$(dtk_path)/cuda/lib64 \
			$(dtk_path)/hip/lib \
			$(dtk_path)/lib


empty := 
library_path_export := $(subst $(empty) $(empty),:,$(library_paths))

link_opencv := opencv_core opencv_imgproc opencv_imgcodecs opencv_dnn  
# link_3rd    := libprotobuf libtiff libjpeg-turbo IlmImf libpng libwebp quirc zlib ade ittnotify libopenjp2      
link_cuda   := cudart cuda cublas
link_trt    := migraphx migraphx_gpu migraphx_onnx
link_sys    := m stdc++ dl rt
link_cudnn  := 
link_librarys := $(link_opencv) $(link_trt) $(link_cuda) $(link_sys) 

run_paths     := $(foreach item,$(library_paths),-Wl,-rpath=$(item))
include_paths := $(foreach item,$(include_paths),-I$(item))
library_paths := $(foreach item,$(library_paths),-L$(item))
link_librarys := $(foreach item,$(link_librarys),-l$(item))
defined       := $(foreach item,$(defined),-D$(item))

link_librarys     += -pthread -fopenmp

# cpp_compile_flags := -std=$(stdcpp) -fPIC -m64 -O3 -w  $(defined) -fuse-ld=gold -fuse-ld=gold --offload-arch=gfx926 --offload-arch=gfx906
# cpp_compile_flags += $(include_paths)
# cu_compile_flags  := -std=c++17 -w -O1 -Xcompiler "$(cpp_compile_flags)" $(cuda_arch) $(support_define) --gpu-max-threads-per-block=1024 --offload-arch=gfx926 --offload-arch=gfx906 
# cu_compile_flags  += $(include_paths)

cpp_compile_flags := -std=$(stdcpp) -fPIC -m64 -O3 -w  $(defined) --gpu-max-threads-per-block=1024
cpp_compile_flags += $(include_paths)
cu_compile_flags  := -std=c++17 -w -O3 -Xcompiler "$(cpp_compile_flags)" $(cuda_arch) $(support_define) --gpu-max-threads-per-block=1024
cu_compile_flags  += $(include_paths)

link_flags 		  += $(library_paths) $(link_librarys) $(run_paths)

#pro         workspace/pro
$(name)   : $(workdir)/$(name)
all       : $(name)

run       : $(name)
	@echo Run_start
	@cd $(workdir) && ./$(name) $(run_args)

build_so : $(dst_so_path)
$(dst_so_path) : $(cpp_objs) $(cc_objs) $(cu_objs) 
	@echo Link $@
	@mkdir -p $(dir $@)
	@$(cc) $^ -shared -o $@ $(link_flags)

run_by_so : $(dst_so_path)
	@echo Compile demo.cpp
	@$(cc) -c $(demo_srcs)  -o objs/$(project_name).o $(cpp_compile_flags)
	@echo Link dst_so
	@$(cc) objs/$(project_name).o -o $(workdir)/$(name) -Llibs/alg -l$(project_name) $(link_flags)
	@./$(workdir)/$(name) $(run_args)

$(workdir)/$(name) : $(cpp_objs) $(cc_objs) $(cu_objs)
	@echo Link $@
	@mkdir -p $(dir $@)
	@$(cc) $^ -o $@ $(link_flags)

$(objdir)/%.o : $(srcdir)/%.cpp
	@echo Compile CXX $<
	@mkdir -p $(dir $@)
	@$(cc) -c $< -o $@ $(cpp_compile_flags)

$(objdir)/%.o : $(srcdir)/%.cc
	@echo Compile CUDA $<
	@mkdir -p $(dir $@)
	@$(cc) -c $< -o $@ $(cpp_compile_flags)

$(objdir)/%.cu.o : $(srcdir)/%.cu
	@echo Compile CUDA $<
	@mkdir -p $(dir $@)
	@$(nvcc) -c $< -o $@ $(cu_compile_flags)

$(objdir)/%.o : $(demodir)/%.cpp
	@echo Compile DEMO $<
	@mkdir -p $(dir $@)
	@$(cc) -c $< -o $@ $(cpp_compile_flags)

# 定义清理指令
clean :
	@rm -rf $(objdir) $(workdir)/$(name)
	@rm -f libs/alg/* ./*.o pro src/*.o demo/*.o workspace/core.* core.*

debug :
	@echo $(LD_LIBRARY_PATH):$(library_path_export)

.PHONY : clean run $(name) all build_so run_by_so