Home | History | Annotate | Download | only in make
      1 #----------------------------------------------------------------------
      2 # Clients fill in the source files to build
      3 #----------------------------------------------------------------------
      4 # C_SOURCES := main.c
      5 # CXX_SOURCES :=
      6 # OBJC_SOURCES :=
      7 # OBJCXX_SOURCES :=
      8 # DYLIB_C_SOURCES :=
      9 # DYLIB_CXX_SOURCES :=
     10 #
     11 # Specifying DYLIB_ONLY has the effect of building dylib only, skipping
     12 # the building of the a.out executable program.  For example,
     13 # DYLIB_ONLY := YES
     14 #
     15 # Also might be of interest:
     16 # FRAMEWORK_INCLUDES (Darwin only) :=
     17 # CFLAGS_EXTRAS :=
     18 # LD_EXTRAS :=
     19 # SPLIT_DEBUG_SYMBOLS := YES
     20 #
     21 # And test/functionalities/archives/Makefile:
     22 # MAKE_DSYM := NO
     23 # ARCHIVE_NAME := libfoo.a
     24 # ARCHIVE_C_SOURCES := a.c b.c
     25 
     26 # Uncomment line below for debugging shell commands
     27 # SHELL = /bin/sh -x
     28 
     29 #----------------------------------------------------------------------
     30 # If ARCH is not defined, default to x86_64.
     31 # If OS is not defined, use 'uname -s' to determine the OS name.
     32 #----------------------------------------------------------------------
     33 ifeq "$(ARCH)" ""
     34 	ARCH = x86_64
     35 endif
     36 
     37 ifeq "$(OS)" ""
     38 	OS = $(shell uname -s)
     39 endif
     40 
     41 #----------------------------------------------------------------------
     42 # CC defaults to clang.
     43 #
     44 # If you change the defaults of CC, be sure to also change it in the file
     45 # test/plugins/builder_base.py, which provides a Python way to return the
     46 # value of the make variable CC -- getCompiler().
     47 #
     48 # See also these functions:
     49 #   o cxx_compiler
     50 #   o cxx_linker
     51 #----------------------------------------------------------------------
     52 CC ?= clang
     53 ifeq "$(CC)" "cc"
     54 	CC = clang
     55 endif
     56 
     57 #----------------------------------------------------------------------
     58 # ARCHFLAG is the flag used to tell the compiler which architecture
     59 # to compile for. The default is the flag that clang accepts.
     60 #----------------------------------------------------------------------
     61 ARCHFLAG ?= -arch 
     62 
     63 #----------------------------------------------------------------------
     64 # Change any build/tool options needed
     65 #----------------------------------------------------------------------
     66 ifeq "$(OS)" "Darwin"
     67 	DS := dsymutil
     68 	DSFLAGS =
     69 	DSYM = $(EXE).dSYM
     70 	AR := libtool
     71 	ARFLAGS := -static -o
     72 else
     73 	# On non-Apple platforms, -arch becomes -m
     74 	ARCHFLAG := -m
     75 
     76 	# i386 becomes 32, and x86_64 becomes 64
     77 	ifeq "$(ARCH)" "x86_64"
     78 	    override ARCH := $(subst x86_64,64,$(ARCH))
     79 	endif
     80 	ifeq "$(ARCH)" "i386"
     81 	    override ARCH := $(subst i386,32,$(ARCH))
     82 	endif
     83 
     84 	ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
     85 		DSYM = $(EXE).debug
     86 	endif
     87 endif
     88 
     89 CFLAGS ?= -g -O0
     90 CFLAGS += $(ARCHFLAG)$(ARCH) $(FRAMEWORK_INCLUDES) $(CFLAGS_EXTRAS)
     91 
     92 CXXFLAGS +=$(CFLAGS)
     93 LD = $(CC)
     94 LDFLAGS ?= $(CFLAGS)
     95 LDFLAGS += $(LD_EXTRAS)
     96 OBJECTS =
     97 EXE ?= a.out
     98 
     99 ifneq "$(DYLIB_NAME)" ""
    100 	ifeq "$(OS)" "Darwin"
    101 		DYLIB_FILENAME = lib$(DYLIB_NAME).dylib
    102 	else
    103 		DYLIB_FILENAME = lib$(DYLIB_NAME).so
    104 	endif
    105 endif
    106 
    107 # Function that returns the counterpart C++ compiler, given $(CC) as arg.
    108 cxx_compiler = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring icc,$(1)), $(subst icc,icpc,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1)))))
    109 
    110 # Function that returns the C++ linker, given $(CC) as arg.
    111 cxx_linker = $(if $(findstring clang,$(1)), $(subst clang,clang++,$(1)), $(if $(findstring icc,$(1)), $(subst icc,icpc,$(1)), $(if $(findstring llvm-gcc,$(1)), $(subst llvm-gcc,llvm-g++,$(1)), $(subst gcc,g++,$(1)))))
    112 
    113 #----------------------------------------------------------------------
    114 # C++ standard library options
    115 #----------------------------------------------------------------------
    116 ifeq (1,$(USE_LIBSTDCPP))
    117 	# Clang requires an extra flag: -stdlib=libstdc++
    118 	ifneq (,$(findstring clang,$(CC)))
    119 		CXXFLAGS += -stdlib=libstdc++
    120 		LDFLAGS += -stdlib=libstdc++
    121 	endif
    122 endif
    123 
    124 ifeq (1,$(USE_LIBCPP))
    125 	# Clang requires an extra flag: -stdlib=libstdc++
    126 	ifneq (,$(findstring clang,$(CC)))
    127 		CXXFLAGS += -stdlib=libc++
    128 		LDFLAGS += -stdlib=libc++
    129 	endif
    130 endif
    131 
    132 #----------------------------------------------------------------------
    133 # dylib settings
    134 #----------------------------------------------------------------------
    135 ifneq "$(strip $(DYLIB_C_SOURCES))" ""
    136 	DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
    137 endif
    138 
    139 ifneq "$(strip $(DYLIB_OBJC_SOURCES))" ""
    140 	DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
    141 endif
    142 
    143 ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
    144     DYLIB_OBJECTS +=$(strip $(DYLIB_CXX_SOURCES:.cpp=.o))
    145     CXX = $(call cxx_compiler,$(CC))
    146     LD = $(call cxx_linker,$(CC))
    147 endif
    148 
    149 #----------------------------------------------------------------------
    150 # Check if we have any C source files
    151 #----------------------------------------------------------------------
    152 ifneq "$(strip $(C_SOURCES))" ""
    153 	OBJECTS +=$(strip $(C_SOURCES:.c=.o))
    154 endif
    155 
    156 #----------------------------------------------------------------------
    157 # Check if we have any C++ source files
    158 #----------------------------------------------------------------------
    159 ifneq "$(strip $(CXX_SOURCES))" ""
    160 	OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
    161 	CXX = $(call cxx_compiler,$(CC))
    162 	LD = $(call cxx_linker,$(CC))
    163 endif
    164 
    165 #----------------------------------------------------------------------
    166 # Check if we have any ObjC source files
    167 #----------------------------------------------------------------------
    168 ifneq "$(strip $(OBJC_SOURCES))" ""
    169 	OBJECTS +=$(strip $(OBJC_SOURCES:.m=.o))
    170 	LDFLAGS +=-lobjc
    171 endif
    172 
    173 #----------------------------------------------------------------------
    174 # Check if we have any ObjC++ source files
    175 #----------------------------------------------------------------------
    176 ifneq "$(strip $(OBJCXX_SOURCES))" ""
    177 	OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
    178 	CXX = $(call cxx_compiler,$(CC))
    179 	LD = $(call cxx_linker,$(CC))
    180 	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
    181 		LDFLAGS +=-lobjc
    182 	endif
    183 endif
    184 
    185 #----------------------------------------------------------------------
    186 # Check if we have any C source files for archive
    187 #----------------------------------------------------------------------
    188 ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
    189 	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
    190 endif
    191 
    192 #----------------------------------------------------------------------
    193 # Check if we have any C++ source files for archive
    194 #----------------------------------------------------------------------
    195 ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
    196 	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
    197 	CXX = $(call cxx_compiler,$(CC))
    198 	LD = $(call cxx_linker,$(CC))
    199 endif
    200 
    201 #----------------------------------------------------------------------
    202 # Check if we have any ObjC source files for archive
    203 #----------------------------------------------------------------------
    204 ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
    205 	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
    206 	LDFLAGS +=-lobjc
    207 endif
    208 
    209 #----------------------------------------------------------------------
    210 # Check if we have any ObjC++ source files for archive
    211 #----------------------------------------------------------------------
    212 ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
    213 	ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
    214 	CXX = $(call cxx_compiler,$(CC))
    215 	LD = $(call cxx_linker,$(CC))
    216 	ifeq "$(findstring lobjc,$(LDFLAGS))" ""
    217 		LDFLAGS +=-lobjc
    218 	endif
    219 endif
    220 
    221 #----------------------------------------------------------------------
    222 # Check if we are compiling with gcc 4.6
    223 #----------------------------------------------------------------------
    224 ifneq (,$(filter g++,$(CXX)))
    225 	CXXVERSION = $(shell g++ -dumpversion | cut -b 1-3)
    226 	ifeq "$(CXXVERSION)" "4.6"
    227                 # GCC 4.6 cannot handle -std=c++11, so replace it with -std=c++0x
    228                 # instead. FIXME: remove once GCC version is upgraded.
    229 		override CXXFLAGS := $(subst -std=c++11,-std=c++0x,$(CXXFLAGS))
    230 	endif
    231 endif
    232 
    233 #----------------------------------------------------------------------
    234 # DYLIB_ONLY variable can be used to skip the building of a.out.
    235 # See the sections below regarding dSYM file as well as the building of
    236 # EXE from all the objects.
    237 #----------------------------------------------------------------------
    238 
    239 #----------------------------------------------------------------------
    240 # Make the dSYM file from the executable if $(MAKE_DSYM) != "NO"
    241 #----------------------------------------------------------------------
    242 $(DSYM) : $(EXE)
    243 ifeq "$(OS)" "Darwin"
    244 ifneq "$(MAKE_DSYM)" "NO"
    245 ifeq "$(DYLIB_ONLY)" ""
    246 	$(DS) $(DSFLAGS) -o "$(DSYM)" "$(EXE)"
    247 endif
    248 endif
    249 else
    250 ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
    251 ifeq "$(DYLIB_ONLY)" ""
    252 	objcopy --only-keep-debug "$(EXE)" "$(DSYM)"
    253 	objcopy --strip-debug --add-gnu-debuglink="$(DSYM)" "$(EXE)" "$(EXE)"
    254 endif
    255 endif
    256 endif
    257 
    258 #----------------------------------------------------------------------
    259 # Compile the executable from all the objects.
    260 #----------------------------------------------------------------------
    261 ifneq "$(DYLIB_NAME)" ""
    262 ifeq "$(DYLIB_ONLY)" ""
    263 $(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
    264 	$(LD) $(LDFLAGS) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) -o "$(EXE)"
    265 else
    266 EXE = $(DYLIB_FILENAME)
    267 endif
    268 else
    269 $(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
    270 	$(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
    271 endif
    272 
    273 #----------------------------------------------------------------------
    274 # Make the archive
    275 #----------------------------------------------------------------------
    276 ifneq "$(ARCHIVE_NAME)" ""
    277 ifeq "$(OS)" "Darwin"
    278 $(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
    279 	$(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
    280 	$(RM) $(ARCHIVE_OBJECTS)
    281 else
    282 $(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
    283 endif
    284 endif
    285 
    286 #----------------------------------------------------------------------
    287 # Make the dylib
    288 #----------------------------------------------------------------------
    289 $(DYLIB_FILENAME) : $(DYLIB_OBJECTS)
    290 ifeq "$(OS)" "Darwin"
    291 	$(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -install_name "@executable_path/$(DYLIB_FILENAME)" -dynamiclib -o "$(DYLIB_FILENAME)"
    292 ifneq "$(MAKE_DSYM)" "NO"
    293 ifneq "$(DS)" ""
    294 	$(DS) $(DSFLAGS) "$(DYLIB_FILENAME)"
    295 endif
    296 endif
    297 else
    298 	$(LD) $(LDFLAGS) $(DYLIB_OBJECTS) -shared -o "$(DYLIB_FILENAME)"
    299 ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
    300 	objcopy --only-keep-debug "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME).debug"
    301 	objcopy --strip-debug --add-gnu-debuglink="$(DYLIB_FILENAME).debug" "$(DYLIB_FILENAME)" "$(DYLIB_FILENAME)"
    302 endif
    303 endif
    304 
    305 #----------------------------------------------------------------------
    306 # Automatic variables based on items already entered. Below we create
    307 # an objects lists from the list of sources by replacing all entries
    308 # that end with .c with .o, and we also create a list of prerequisite
    309 # files by replacing all .c files with .d.
    310 #----------------------------------------------------------------------
    311 PREREQS := $(OBJECTS:.o=.d)
    312 ifneq "$(DYLIB_NAME)" ""
    313 	DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
    314 endif
    315 
    316 #----------------------------------------------------------------------
    317 # Rule for Generating Prerequisites Automatically using .d files and
    318 # the compiler -MM option. The -M option will list all system headers,
    319 # and the -MM option will list all non-system dependencies.
    320 #----------------------------------------------------------------------
    321 %.d: %.c
    322 	@set -e; rm -f $@; \
    323 	$(CC) -M $(CFLAGS) $< > $@.$$$$; \
    324 	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    325 	rm -f $@.$$$$
    326 
    327 %.d: %.cpp
    328 	@set -e; rm -f $@; \
    329 	$(CXX) -M $(CXXFLAGS) $< > $@.$$$$; \
    330 	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    331 	rm -f $@.$$$$
    332 
    333 %.d: %.m
    334 	@set -e; rm -f $@; \
    335 	$(CC) -M $(CFLAGS) $< > $@.$$$$; \
    336 	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    337 	rm -f $@.$$$$
    338 
    339 %.d: %.mm
    340 	@set -e; rm -f $@; \
    341 	$(CXX) -M $(CXXFLAGS) $< > $@.$$$$; \
    342 	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    343 	rm -f $@.$$$$
    344 
    345 #----------------------------------------------------------------------
    346 # Include all of the makefiles for each source file so we don't have
    347 # to manually track all of the prerequisites for each source file.
    348 #----------------------------------------------------------------------
    349 sinclude $(PREREQS)
    350 ifneq "$(DYLIB_NAME)" ""
    351 	sinclude $(DYLIB_PREREQS)
    352 endif
    353 
    354 # Define a suffix rule for .mm -> .o
    355 .SUFFIXES: .mm .o
    356 .mm.o:
    357 	$(CXX) $(CXXFLAGS) -c $<
    358 
    359 .PHONY: clean
    360 dsym:	$(DSYM)
    361 all:	$(EXE) $(DSYM)
    362 clean::
    363 ifeq "$(DYLIB_NAME)" ""
    364 	rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS) *.d.[0-9] *.d.[0-9][0-9] *.d.[0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9][0-9]
    365 else
    366 	rm -rf "$(EXE)" "$(DSYM)" $(OBJECTS) $(PREREQS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS) $(DYLIB_OBJECTS) $(DYLIB_PREREQS) $(DYLIB_FILENAME) $(DYLIB_FILENAME).dSYM $(DYLIB_FILENAME).debug *.d.[0-9] *.d.[0-9][0-9] *.d.[0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9] *.d.[0-9][0-9][0-9][0-9][0-9]
    367 endif
    368 
    369 #----------------------------------------------------------------------
    370 # From http://blog.melski.net/tag/debugging-makefiles/
    371 # 
    372 # Usage: make print-CC print-CXX print-LD
    373 #----------------------------------------------------------------------
    374 print-%:
    375 	@echo '$*=$($*)'
    376 	@echo '  origin = $(origin $*)'
    377 	@echo '  flavor = $(flavor $*)'
    378 	@echo '   value = $(value  $*)'
    379 
    380 
    382 ### Local Variables: ###
    383 ### mode:makefile ###
    384 ### End: ###
    385