|
| 1 | +############################################################################# |
| 2 | +# Primesense template makefile. |
| 3 | +# This file should not be made, but only included from other makefiles. |
| 4 | +# By default, this makefile compiles in release. To compile a debug version: |
| 5 | +# make CFG=Debug |
| 6 | +# |
| 7 | +# Project makefile should define the following BEFORE including this file: |
| 8 | +# SRC_FILES - a list of all source files |
| 9 | +# Output name under one of the following: |
| 10 | +# EXE_NAME (executable), |
| 11 | +# LIB_NAME (dynamic library) or |
| 12 | +# SLIB_NAME (static library) or |
| 13 | +# BIN_DIR - Bin directory (output dir) |
| 14 | +# INC_DIRS - a list of additional include directories |
| 15 | +# LIB_DIRS - a list of additional library directories |
| 16 | +# USED_LIBS - a list of libraries to link with |
| 17 | +# DEFINES - [Optional] additional preprocessor defines |
| 18 | +# CFLAGS - [Optional] additional flags for the compiler |
| 19 | +# LDFLAGS - [Optional] additional flags for the linker |
| 20 | +# SSE_GENERATION - [Optional] The SSE generation to use (default is 3) |
| 21 | +# TARGET_SYS_ROOT - [Optional] The path to the root of the target |
| 22 | +############################################################################# |
| 23 | + |
| 24 | +ifndef TARGET_SYS_ROOT |
| 25 | + TARGET_SYS_ROOT = / |
| 26 | +endif |
| 27 | + |
| 28 | +# take this file's dir |
| 29 | +COMMON_CPP_MAKE_FILE_DIR = $(dir $(lastword $(MAKEFILE_LIST))) |
| 30 | + |
| 31 | +include $(COMMON_CPP_MAKE_FILE_DIR)CommonDefs.mak |
| 32 | + |
| 33 | +# define a function to figure .o file for each source file (placed under intermediate directory) |
| 34 | +SRC_TO_OBJ = $(addprefix ./$(INT_DIR)/,$(addsuffix .o,$(notdir $(basename $1)))) |
| 35 | + |
| 36 | +# create a list of all object files |
| 37 | +OBJ_FILES = $(call SRC_TO_OBJ,$(SRC_FILES_LIST)) |
| 38 | + |
| 39 | +# define a function to translate any source file to its dependency file (note that the way we create |
| 40 | +# dep files, as a side affect of compilation, always puts the files in the INT_DIR with suffix .d) |
| 41 | +SRC_TO_DEP = $(addprefix ./$(INT_DIR)/,$(addsuffix .d,$(notdir $(basename $1)))) |
| 42 | + |
| 43 | +# create a list of all dependency files |
| 44 | +DEP_FILES = $(call SRC_TO_DEP,$(SRC_FILES_LIST)) |
| 45 | + |
| 46 | +# append the -I switch to each include directory |
| 47 | +INC_DIRS_OPTION = $(foreach dir,$(INC_DIRS),-I$(dir)) |
| 48 | + |
| 49 | +# append the -L switch to each library directory |
| 50 | +LIB_DIRS_OPTION = $(foreach dir,$(LIB_DIRS),-L$(dir)) -L$(OUT_DIR) |
| 51 | + |
| 52 | +# append the -l switch to each library used |
| 53 | +USED_LIBS_OPTION = $(foreach lib,$(USED_LIBS),-l$(lib)) |
| 54 | + |
| 55 | +# append the -D switch to each define |
| 56 | +DEFINES_OPTION = $(foreach def,$(DEFINES),-D$(def)) |
| 57 | + |
| 58 | +# tell compiler to use the target system root |
| 59 | +ifneq ("$(TARGET_SYS_ROOT)","/") |
| 60 | + CFLAGS += --sysroot=$(TARGET_SYS_ROOT) |
| 61 | + LDFLAGS += --sysroot=$(TARGET_SYS_ROOT) |
| 62 | +endif |
| 63 | + |
| 64 | +# set Debug / Release flags |
| 65 | +ifeq "$(CFG)" "Debug" |
| 66 | + CFLAGS += -g |
| 67 | +endif |
| 68 | +ifeq "$(CFG)" "Release" |
| 69 | + CFLAGS += -O2 -DNDEBUG |
| 70 | + CSFLAGS += -o+ |
| 71 | +endif |
| 72 | + |
| 73 | +CFLAGS += $(INC_DIRS_OPTION) $(DEFINES_OPTION) |
| 74 | +LDFLAGS += $(LIB_DIRS_OPTION) $(USED_LIBS_OPTION) |
| 75 | + |
| 76 | +# some lib / exe specifics |
| 77 | +ifneq "$(LIB_NAME)" "" |
| 78 | + OUTPUT_NAME = lib$(LIB_NAME).so |
| 79 | + CFLAGS += -fPIC -fvisibility=hidden |
| 80 | + ifneq ("$(OSTYPE)","Darwin") |
| 81 | + LDFLAGS += -Wl,--no-undefined |
| 82 | + OUTPUT_NAME = lib$(LIB_NAME).so |
| 83 | + OUTPUT_COMMAND = $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS) -shared |
| 84 | + else |
| 85 | + LDFLAGS += -undefined error |
| 86 | + OUTPUT_NAME = lib$(LIB_NAME).dylib |
| 87 | + OUTPUT_COMMAND = $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS) -dynamiclib -headerpad_max_install_names |
| 88 | + endif |
| 89 | +endif |
| 90 | +ifneq "$(EXE_NAME)" "" |
| 91 | + OUTPUT_NAME = $(EXE_NAME) |
| 92 | + OUTPUT_COMMAND = $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS) |
| 93 | +endif |
| 94 | +ifneq "$(SLIB_NAME)" "" |
| 95 | + OUTPUT_NAME = lib$(SLIB_NAME).a |
| 96 | + OUTPUT_COMMAND = $(AR) $(OUTPUT_FILE) $(OBJ_FILES) |
| 97 | +endif |
| 98 | + |
| 99 | +define CREATE_SRC_TARGETS |
| 100 | +# create a target for the object file (the CXX command creates both an .o file |
| 101 | +# and a .d file) |
| 102 | +ifneq ("$(OSTYPE)","Darwin") |
| 103 | +$(call SRC_TO_OBJ,$1) : $1 | $(INT_DIR) |
| 104 | + $(CXX) -MD -MP -MT "$(call SRC_TO_DEP,$1) $$@" -c $(CFLAGS) -o $$@ $$< |
| 105 | +else |
| 106 | +$(call SRC_TO_OBJ,$1) : $1 | $(INT_DIR) |
| 107 | + $(CXX) -c $(CFLAGS) -o $$@ $$< |
| 108 | +endif |
| 109 | +endef |
| 110 | + |
| 111 | +############################################################################# |
| 112 | +# Targets |
| 113 | +############################################################################# |
| 114 | +.PHONY: clean-objs clean-defs |
| 115 | + |
| 116 | +include $(COMMON_CPP_MAKE_FILE_DIR)CommonTargets.mak |
| 117 | + |
| 118 | +# create targets for each source file |
| 119 | +$(foreach src,$(SRC_FILES_LIST),$(eval $(call CREATE_SRC_TARGETS,$(src)))) |
| 120 | + |
| 121 | +# include all dependency files (we don't need them the first time, so we can use -include) |
| 122 | +-include $(DEP_FILES) |
| 123 | + |
| 124 | +$(OUTPUT_FILE): $(OBJ_FILES) |
| 125 | + $(OUTPUT_COMMAND) |
| 126 | + |
| 127 | +clean-objs: |
| 128 | + rm -rf $(OBJ_FILES) |
| 129 | + |
| 130 | +clean-defs: |
| 131 | + rm -rf $(DEP_FILES) |
| 132 | + |
| 133 | +clean: clean-objs clean-defs |
0 commit comments