-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
87 lines (64 loc) · 2.1 KB
/
Makefile
File metadata and controls
87 lines (64 loc) · 2.1 KB
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
PROJECT := torch_scope
CONFIGS := Makefile.config
include $(CONFIGS)
OBJ_DIR := obj
SRC_DIR := src
INC_DIR := include
LIB_DIR := lib
PREFIX := $(INSTALL_DIR)
LIB := $(LIB_DIR)/lib$(PROJECT).so
CUR_DIR := $(shell pwd)
CXX ?= g++
CXX_FLAGS ?=
INCLUDES ?=
LDFLAGS ?=
LINK_LIBS ?=
INCLUDES += -I$(INC_DIR)
TORCH_DIR = $(shell python3 -c "import torch; import os; print(os.path.dirname(torch.__file__))")
INCLUDES += -I$(TORCH_DIR)/include -I$(TORCH_DIR)/include/torch/csrc/api/include
LDFLAGS += -L$(TORCH_DIR)/lib -Wl,-rpath=$(TORCH_DIR)/lib
LINK_LIBS += -lc10 -ltorch -ltorch_cpu
# --- Add CUDA (or HIP) component if present
# Try torch_cuda first; if not found, try torch_hip (ROCm)
ifneq ("$(wildcard $(TORCH_DIR)/lib/libtorch_cuda.so)","")
LINK_LIBS += -ltorch_cuda -lc10_cuda
endif
ifneq ("$(wildcard $(TORCH_DIR)/lib/libtorch_hip.so)","")
LINK_LIBS += -ltorch_hip
endif
PYTHON_INCLUDE_DIR = $(shell python3 -c "import sysconfig; print(sysconfig.get_path('include'))")
PYTHON_LIB_DIR = $(shell python3 -c "import sysconfig; print(sysconfig.get_path('stdlib'))")
PYTHON_VERSION = $(shell python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
INCLUDES += -I$(PYTHON_INCLUDE_DIR)
LDFLAGS += -L$(PYTHON_LIB_DIR)/../ -Wl,-rpath=$(PYTHON_LIB_DIR)/../
LINK_LIBS += -lpython$(PYTHON_VERSION)
CXX_FLAGS += -std=c++17
ifeq ($(DEBUG), 1)
CXX_FLAGS += -g -O0
else
CXX_FLAGS += -O3
endif
SRCS := $(notdir $(wildcard $(SRC_DIR)/*.cpp $(SRC_DIR)/*/*.cpp))
OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(SRCS)))
all: dirs libs
dirs: $(OBJ_DIR) $(LIB_DIR)
libs: $(LIB)
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
$(LIB_DIR):
mkdir -p $(LIB_DIR)
$(LIB): $(OBJS)
$(CXX) $(LDFLAGS) -fPIC -shared -o $@ $^ $(LINK_LIBS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXX_FLAGS) $(INCLUDES) -fPIC -c $< -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/*/%.cpp
$(CXX) $(CXX_FLAGS) $(INCLUDES) -fPIC -c $< -o $@
.PHONY: clean
clean:
-rm -rf $(OBJ_DIR) $(LIB_DIR) $(PREFIX)
.PHONY: install
install: all
mkdir -p $(PREFIX)/lib
mkdir -p $(PREFIX)/include
cp -r $(LIB) $(PREFIX)/lib
cp -r $(INC_DIR)/$(PROJECT).h $(PREFIX)/include