common.mk 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. ################################################################################
  2. #
  3. # Copyright 1993-2006 NVIDIA Corporation. All rights reserved.
  4. #
  5. # NOTICE TO USER:
  6. #
  7. # This source code is subject to NVIDIA ownership rights under U.S. and
  8. # international Copyright laws.
  9. #
  10. # NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
  11. # CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
  12. # IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH
  13. # REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
  14. # MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
  15. # IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
  16. # OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  17. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  18. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  19. # OR PERFORMANCE OF THIS SOURCE CODE.
  20. #
  21. # U.S. Government End Users. This source code is a "commercial item" as
  22. # that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting of
  23. # "commercial computer software" and "commercial computer software
  24. # documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995)
  25. # and is provided to the U.S. Government only as a commercial end item.
  26. # Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
  27. # 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
  28. # source code with only those rights set forth herein.
  29. #
  30. ################################################################################
  31. #
  32. # Common build script
  33. #
  34. ################################################################################
  35. .SUFFIXES : .cu .cu_dbg_o .c_dbg_o .cpp_dbg_o .cu_rel_o .c_rel_o .cpp_rel_o .cubin
  36. # Add new SM Versions here as devices with new Compute Capability are released
  37. SM_VERSIONS := sm_10 sm_11 sm_12 sm_13
  38. CUDA_INSTALL_PATH ?= /usr/local/cuda
  39. ifdef cuda-install
  40. CUDA_INSTALL_PATH := $(cuda-install)
  41. endif
  42. # detect OS
  43. OSUPPER = $(shell uname -s 2>/dev/null | tr [:lower:] [:upper:])
  44. OSLOWER = $(shell uname -s 2>/dev/null | tr [:upper:] [:lower:])
  45. # 'linux' is output for Linux system, 'darwin' for OS X
  46. DARWIN = $(strip $(findstring DARWIN, $(OSUPPER)))
  47. # Basic directory setup for SDK
  48. # (override directories only if they are not already defined)
  49. SRCDIR ?=
  50. ROOTDIR ?= ..
  51. ROOTBINDIR ?= $(ROOTDIR)/../bin
  52. BINDIR ?= $(ROOTBINDIR)/$(OSLOWER)
  53. ROOTOBJDIR ?= obj
  54. LIBDIR := $(ROOTDIR)/../lib
  55. COMMONDIR := $(ROOTDIR)/../common
  56. # Compilers
  57. NVCC := $(CUDA_INSTALL_PATH)/bin/nvcc
  58. CXX := g++
  59. CC := gcc
  60. LINK := g++ -fPIC
  61. # Includes
  62. INCLUDES += -I. -I$(CUDA_INSTALL_PATH)/include -I$(COMMONDIR)/inc
  63. # architecture flag for cubin build
  64. CUBIN_ARCH_FLAG := -m32
  65. # Warning flags
  66. CXXWARN_FLAGS := \
  67. -W -Wall \
  68. -Wimplicit \
  69. -Wswitch \
  70. -Wformat \
  71. -Wchar-subscripts \
  72. -Wparentheses \
  73. -Wmultichar \
  74. -Wtrigraphs \
  75. -Wpointer-arith \
  76. -Wcast-align \
  77. -Wreturn-type \
  78. -Wno-unused-function \
  79. $(SPACE)
  80. CWARN_FLAGS := $(CXXWARN_FLAGS) \
  81. -Wstrict-prototypes \
  82. -Wmissing-prototypes \
  83. -Wmissing-declarations \
  84. -Wnested-externs \
  85. -Wmain \
  86. # Compiler-specific flags
  87. NVCCFLAGS :=
  88. CXXFLAGS := $(CXXWARN_FLAGS)
  89. CFLAGS := $(CWARN_FLAGS)
  90. # Common flags
  91. COMMONFLAGS += $(INCLUDES) -DUNIX
  92. # Debug/release configuration
  93. ifeq ($(dbg),1)
  94. COMMONFLAGS += -g
  95. NVCCFLAGS += -D_DEBUG
  96. BINSUBDIR := debug
  97. LIBSUFFIX := D
  98. else
  99. COMMONFLAGS += -O3
  100. BINSUBDIR := release
  101. LIBSUFFIX :=
  102. NVCCFLAGS += --compiler-options -fno-strict-aliasing
  103. CXXFLAGS += -fno-strict-aliasing
  104. CFLAGS += -fno-strict-aliasing
  105. endif
  106. # append optional arch/SM version flags (such as -arch sm_11)
  107. #NVCCFLAGS += $(SMVERSIONFLAGS)
  108. # architecture flag for cubin build
  109. CUBIN_ARCH_FLAG := -m32
  110. # detect if 32 bit or 64 bit system
  111. HP_64 = $(shell uname -m | grep 64)
  112. # OpenGL is used or not (if it is used, then it is necessary to include GLEW)
  113. ifeq ($(USEGLLIB),1)
  114. ifneq ($(DARWIN),)
  115. OPENGLLIB := -L/System/Library/Frameworks/OpenGL.framework/Libraries -lGL -lGLU $(COMMONDIR)/lib/$(OSLOWER)/libGLEW.a
  116. else
  117. OPENGLLIB := -lGL -lGLU
  118. ifeq "$(strip $(HP_64))" ""
  119. OPENGLLIB += -lGLEW
  120. else
  121. OPENGLLIB += -lGLEW_x86_64
  122. endif
  123. endif
  124. CUBIN_ARCH_FLAG := -m64
  125. endif
  126. ifeq ($(USEGLUT),1)
  127. ifneq ($(DARWIN),)
  128. OPENGLLIB += -framework GLUT
  129. else
  130. OPENGLLIB += -lglut
  131. endif
  132. endif
  133. ifeq ($(USEPARAMGL),1)
  134. PARAMGLLIB := -lparamgl$(LIBSUFFIX)
  135. endif
  136. ifeq ($(USERENDERCHECKGL),1)
  137. RENDERCHECKGLLIB := -lrendercheckgl$(LIBSUFFIX)
  138. endif
  139. ifeq ($(USECUDPP), 1)
  140. ifeq "$(strip $(HP_64))" ""
  141. CUDPPLIB := -lcudpp
  142. else
  143. CUDPPLIB := -lcudpp64
  144. endif
  145. CUDPPLIB := $(CUDPPLIB)$(LIBSUFFIX)
  146. ifeq ($(emu), 1)
  147. CUDPPLIB := $(CUDPPLIB)_emu
  148. endif
  149. endif
  150. # Libs
  151. LIB := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER)
  152. ifeq ($(USEDRVAPI),1)
  153. LIB += -lcuda ${OPENGLLIB} $(PARAMGLLIB) $(RENDERCHECKGLLIB) $(CUDPPLIB) ${LIB}
  154. else
  155. LIB += -lcudart ${OPENGLLIB} $(PARAMGLLIB) $(RENDERCHECKGLLIB) $(CUDPPLIB) ${LIB}
  156. endif
  157. ifeq ($(USECUFFT),1)
  158. ifeq ($(emu),1)
  159. LIB += -lcufftemu
  160. else
  161. LIB += -lcufft
  162. endif
  163. endif
  164. ifeq ($(USECUBLAS),1)
  165. ifeq ($(emu),1)
  166. LIB += -lcublasemu
  167. else
  168. LIB += -lcublas
  169. endif
  170. endif
  171. # Lib/exe configuration
  172. ifneq ($(STATIC_LIB),)
  173. TARGETDIR := $(LIBDIR)
  174. TARGET := $(subst .a,$(LIBSUFFIX).a,$(LIBDIR)/$(STATIC_LIB))
  175. LINKLINE = ar qv $(TARGET) $(OBJS)
  176. else
  177. # LIB += -lcutil$(LIBSUFFIX)
  178. # Device emulation configuration
  179. ifeq ($(emu), 1)
  180. NVCCFLAGS += -deviceemu
  181. CUDACCFLAGS +=
  182. BINSUBDIR := emu$(BINSUBDIR)
  183. # consistency, makes developing easier
  184. CXXFLAGS += -D__DEVICE_EMULATION__
  185. CFLAGS += -D__DEVICE_EMULATION__
  186. endif
  187. TARGETDIR := $(BINDIR)/$(BINSUBDIR)
  188. TARGET := $(TARGETDIR)/$(EXECUTABLE)
  189. LINKLINE = $(LINK) -o $(TARGET) $(OBJS) $(LIB)
  190. endif
  191. # check if verbose
  192. ifeq ($(verbose), 1)
  193. VERBOSE :=
  194. else
  195. VERBOSE := @
  196. endif
  197. ################################################################################
  198. # Check for input flags and set compiler flags appropriately
  199. ################################################################################
  200. ifeq ($(fastmath), 1)
  201. NVCCFLAGS += -use_fast_math
  202. endif
  203. ifeq ($(keep), 1)
  204. NVCCFLAGS += -keep
  205. NVCC_KEEP_CLEAN := *.i* *.cubin *.cu.c *.cudafe* *.fatbin.c *.ptx
  206. endif
  207. ifdef maxregisters
  208. NVCCFLAGS += -maxrregcount $(maxregisters)
  209. endif
  210. # Add cudacc flags
  211. NVCCFLAGS += $(CUDACCFLAGS)
  212. # workaround for mac os x cuda 1.1 compiler issues
  213. ifneq ($(DARWIN),)
  214. NVCCFLAGS += --host-compilation=C
  215. endif
  216. # Add common flags
  217. NVCCFLAGS += $(COMMONFLAGS)
  218. CXXFLAGS += $(COMMONFLAGS)
  219. CFLAGS += $(COMMONFLAGS)
  220. ifeq ($(nvcc_warn_verbose),1)
  221. NVCCFLAGS += $(addprefix --compiler-options ,$(CXXWARN_FLAGS))
  222. NVCCFLAGS += --compiler-options -fno-strict-aliasing
  223. endif
  224. ################################################################################
  225. # Set up object files
  226. ################################################################################
  227. OBJDIR := $(ROOTOBJDIR)/$(BINSUBDIR)
  228. OBJS += $(patsubst %.cpp,$(OBJDIR)/%.cpp_o,$(notdir $(CCFILES)))
  229. OBJS += $(patsubst %.c,$(OBJDIR)/%.c_o,$(notdir $(CFILES)))
  230. OBJS += $(patsubst %.cu,$(OBJDIR)/%.cu_o,$(notdir $(CUFILES)))
  231. ################################################################################
  232. # Set up cubin files
  233. ################################################################################
  234. CUBINDIR := $(SRCDIR)data
  235. CUBINS += $(patsubst %.cu,$(CUBINDIR)/%.cubin,$(notdir $(CUBINFILES)))
  236. ################################################################################
  237. # Rules
  238. ################################################################################
  239. $(OBJDIR)/%.c_o : $(SRCDIR)%.c $(C_DEPS)
  240. $(VERBOSE)$(CC) $(CFLAGS) -o $@ -c $<
  241. $(OBJDIR)/%.cpp_o : $(SRCDIR)%.cpp $(C_DEPS)
  242. $(VERBOSE)$(CXX) $(CXXFLAGS) -o $@ -c $<
  243. $(OBJDIR)/%.cu_o : $(SRCDIR)%.cu $(CU_DEPS)
  244. $(VERBOSE)$(NVCC) $(NVCCFLAGS) $(SMVERSIONFLAGS) -o $@ -c $<
  245. $(CUBINDIR)/%.cubin : $(SRCDIR)%.cu cubindirectory
  246. $(VERBOSE)$(NVCC) $(CUBIN_ARCH_FLAG) $(NVCCFLAGS) $(SMVERSIONFLAGS) -o $@ -cubin $<
  247. #
  248. # The following definition is a template that gets instantiated for each SM
  249. # version (sm_10, sm_13, etc.) stored in SMVERSIONS. It does 2 things:
  250. # 1. It adds to OBJS a .cu_sm_XX_o for each .cu file it finds in CUFILES_sm_XX.
  251. # 2. It generates a rule for building .cu_sm_XX_o files from the corresponding
  252. # .cu file.
  253. #
  254. # The intended use for this is to allow Makefiles that use common.mk to compile
  255. # files to different Compute Capability targets (aka SM arch version). To do
  256. # so, in the Makefile, list files for each SM arch separately, like so:
  257. #
  258. # CUFILES_sm_10 := mycudakernel_sm10.cu app.cu
  259. # CUFILES_sm_12 := anothercudakernel_sm12.cu
  260. #
  261. define SMVERSION_template
  262. OBJS += $(patsubst %.cu,$(OBJDIR)/%.cu_$(1)_o,$(notdir $(CUFILES_$(1))))
  263. $(OBJDIR)/%.cu_$(1)_o : $(SRCDIR)%.cu $(CU_DEPS)
  264. $(VERBOSE)$(NVCC) -o $$@ -c $$< $(NVCCFLAGS) -arch $(1)
  265. endef
  266. # This line invokes the above template for each arch version stored in
  267. # SM_VERSIONS. The call funtion invokes the template, and the eval
  268. # function interprets it as make commands.
  269. $(foreach smver,$(SM_VERSIONS),$(eval $(call SMVERSION_template,$(smver))))
  270. $(TARGET): makedirectories $(OBJS) $(CUBINS) Makefile
  271. $(VERBOSE)$(LINKLINE)
  272. cubindirectory:
  273. $(VERBOSE)mkdir -p $(CUBINDIR)
  274. makedirectories:
  275. $(VERBOSE)mkdir -p $(LIBDIR)
  276. $(VERBOSE)mkdir -p $(OBJDIR)
  277. $(VERBOSE)mkdir -p $(TARGETDIR)
  278. tidy :
  279. $(VERBOSE)find . | egrep "#" | xargs rm -f
  280. $(VERBOSE)find . | egrep "\~" | xargs rm -f
  281. clean : tidy
  282. $(VERBOSE)rm -f $(OBJS)
  283. $(VERBOSE)rm -f $(CUBINS)
  284. $(VERBOSE)rm -f $(TARGET)
  285. $(VERBOSE)rm -f $(NVCC_KEEP_CLEAN)
  286. clobber : clean
  287. $(VERBOSE)rm -rf $(ROOTOBJDIR)