Skip to content

Instantly share code, notes, and snippets.

@bismarckjunior
Created December 16, 2018 11:06
Show Gist options
  • Save bismarckjunior/c11872b06d1071f2e0245801c8f72c3b to your computer and use it in GitHub Desktop.
Save bismarckjunior/c11872b06d1071f2e0245801c8f72c3b to your computer and use it in GitHub Desktop.
Makefile for CUDA
#==============================================================================#
# Author: Bismarck Gomes <bismarckgomes@gmail.com> #
# #
# * #
# L bin // Executable folder #
# L build // Objects (*.o) #
# L doc // Documentation #
# L examples // Examples #
# L include // Public header files (*.h) #
# L lib // Library dependencies (*.so, *.dll) #
# L src // Source files (*.cpp, *.cu) and private header files (*.h)#
# L test // Test code files #
# * makefile #
# * readme.md #
# #
# # Compile #
# $ make #
# #
# # Run #
# $ make run #
# #
# # Clean (just files) #
# $ make clean #
# #
# # Clean (files and folders) #
# $ make clobber #
#==============================================================================#
# Main variables
SRCDIR := .
INCDIR :=
BUILDDIR := build
TARGET := bin/main
# Other variables
SOURCES := $(shell find $(SRCDIR) -type f -name "*.cpp")
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.cpp=.o))
SOURCESCU := $(shell find $(SRCDIR) -type f -name "*.cu")
OBJECTSCU := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCESCU:.cu=.cu.o))
TARGETDIR = `dirname $(TARGET)`
# Compilers
HOST_COMPILER := g++
NVCC := nvcc -ccbin $(HOST_COMPILER) #/usr/local/cuda/bin/nvcc
# Flags
NVCCFLAGS := -m64 -dc #-dc used cg::grid_group, cg::this_group
CCFLAGS :=
LDFLAGS :=
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
# Main flags
ALL_CCFLAGS := $(NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_LDFLAGS := $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
# Includes and libraries
INCLUDES := $(addprefix -I ,$(shell find $(SRCDIR) -type d))
LIBRARIES :=
ifneq ($(INCDIR),)
INCLUDES += -I $(INCDIR)
endif
################################################################################
# Gencode arguments
# SMS ?= 30 35 37 50 52 60 61 70
SMS ?= 60 61 70
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
################################################################################
# Target rules
all: build
build: $(TARGET)
run: build
./$(TARGET)
clean:
rm -fr $(OBJECTS) $(OBJECTSCU) $(TARGET)
clobber:
rm -fr $(BUILDDIR) $(TARGETDIR)
$(BUILDDIR)/%.cu.o: $(SRCDIR)/%.cu
@mkdir -p $(BUILDDIR);
$(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp
@mkdir -p $(BUILDDIR);
$(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
$(TARGET): $(OBJECTS) $(OBJECTSCU)
@mkdir -p $(TARGETDIR);
$(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment