1CJSON_OBJ = cJSON.o 2UTILS_OBJ = cJSON_Utils.o 3CJSON_LIBNAME = libcjson 4UTILS_LIBNAME = libcjson_utils 5CJSON_TEST = cJSON_test 6 7CJSON_TEST_SRC = cJSON.c test.c 8 9LDLIBS = -lm 10 11LIBVERSION = 1.7.14 12CJSON_SOVERSION = 1 13UTILS_SOVERSION = 1 14 15CJSON_SO_LDFLAG=-Wl,-soname=$(CJSON_LIBNAME).so.$(CJSON_SOVERSION) 16UTILS_SO_LDFLAG=-Wl,-soname=$(UTILS_LIBNAME).so.$(UTILS_SOVERSION) 17 18PREFIX ?= /usr/local 19INCLUDE_PATH ?= include/cjson 20LIBRARY_PATH ?= lib 21 22INSTALL_INCLUDE_PATH = $(DESTDIR)$(PREFIX)/$(INCLUDE_PATH) 23INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) 24 25INSTALL ?= cp -a 26 27CC = gcc -std=c89 28 29# validate gcc version for use fstack-protector-strong 30MIN_GCC_VERSION = "4.9" 31GCC_VERSION := "`$(CC) -dumpversion`" 32IS_GCC_ABOVE_MIN_VERSION := $(shell expr "$(GCC_VERSION)" ">=" "$(MIN_GCC_VERSION)") 33ifeq "$(IS_GCC_ABOVE_MIN_VERSION)" "1" 34 CFLAGS += -fstack-protector-strong 35else 36 CFLAGS += -fstack-protector 37endif 38 39PIC_FLAGS = -fPIC 40R_CFLAGS = $(PIC_FLAGS) -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat -Wundef -Wswitch-default -Wconversion $(CFLAGS) 41 42uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') 43 44#library file extensions 45SHARED = so 46STATIC = a 47 48## create dynamic (shared) library on Darwin (base OS for MacOSX and IOS) 49ifeq (Darwin, $(uname)) 50 SHARED = dylib 51 CJSON_SO_LDFLAG = "" 52 UTILS_SO_LDFLAG = "" 53endif 54 55#cJSON library names 56CJSON_SHARED = $(CJSON_LIBNAME).$(SHARED) 57CJSON_SHARED_VERSION = $(CJSON_LIBNAME).$(SHARED).$(LIBVERSION) 58CJSON_SHARED_SO = $(CJSON_LIBNAME).$(SHARED).$(CJSON_SOVERSION) 59CJSON_STATIC = $(CJSON_LIBNAME).$(STATIC) 60 61#cJSON_Utils library names 62UTILS_SHARED = $(UTILS_LIBNAME).$(SHARED) 63UTILS_SHARED_VERSION = $(UTILS_LIBNAME).$(SHARED).$(LIBVERSION) 64UTILS_SHARED_SO = $(UTILS_LIBNAME).$(SHARED).$(UTILS_SOVERSION) 65UTILS_STATIC = $(UTILS_LIBNAME).$(STATIC) 66 67SHARED_CMD = $(CC) -shared -o 68 69.PHONY: all shared static tests clean install 70 71all: shared static tests 72 73shared: $(CJSON_SHARED) $(UTILS_SHARED) 74 75static: $(CJSON_STATIC) $(UTILS_STATIC) 76 77tests: $(CJSON_TEST) 78 79test: tests 80 ./$(CJSON_TEST) 81 82.c.o: 83 $(CC) -c $(R_CFLAGS) $< 84 85#tests 86#cJSON 87$(CJSON_TEST): $(CJSON_TEST_SRC) cJSON.h 88 $(CC) $(R_CFLAGS) $(CJSON_TEST_SRC) -o $@ $(LDLIBS) -I. 89 90#static libraries 91#cJSON 92$(CJSON_STATIC): $(CJSON_OBJ) 93 $(AR) rcs $@ $< 94#cJSON_Utils 95$(UTILS_STATIC): $(UTILS_OBJ) 96 $(AR) rcs $@ $< 97 98#shared libraries .so.1.0.0 99#cJSON 100$(CJSON_SHARED_VERSION): $(CJSON_OBJ) 101 $(CC) -shared -o $@ $< $(CJSON_SO_LDFLAG) $(LDFLAGS) 102#cJSON_Utils 103$(UTILS_SHARED_VERSION): $(UTILS_OBJ) 104 $(CC) -shared -o $@ $< $(CJSON_OBJ) $(UTILS_SO_LDFLAG) $(LDFLAGS) 105 106#objects 107#cJSON 108$(CJSON_OBJ): cJSON.c cJSON.h 109#cJSON_Utils 110$(UTILS_OBJ): cJSON_Utils.c cJSON_Utils.h cJSON.h 111 112 113#links .so -> .so.1 -> .so.1.0.0 114#cJSON 115$(CJSON_SHARED_SO): $(CJSON_SHARED_VERSION) 116 ln -s $(CJSON_SHARED_VERSION) $(CJSON_SHARED_SO) 117$(CJSON_SHARED): $(CJSON_SHARED_SO) 118 ln -s $(CJSON_SHARED_SO) $(CJSON_SHARED) 119#cJSON_Utils 120$(UTILS_SHARED_SO): $(UTILS_SHARED_VERSION) 121 ln -s $(UTILS_SHARED_VERSION) $(UTILS_SHARED_SO) 122$(UTILS_SHARED): $(UTILS_SHARED_SO) 123 ln -s $(UTILS_SHARED_SO) $(UTILS_SHARED) 124 125#install 126#cJSON 127install-cjson: 128 mkdir -p $(INSTALL_LIBRARY_PATH) $(INSTALL_INCLUDE_PATH) 129 $(INSTALL) cJSON.h $(INSTALL_INCLUDE_PATH) 130 $(INSTALL) $(CJSON_SHARED) $(CJSON_SHARED_SO) $(CJSON_SHARED_VERSION) $(INSTALL_LIBRARY_PATH) 131#cJSON_Utils 132install-utils: install-cjson 133 $(INSTALL) cJSON_Utils.h $(INSTALL_INCLUDE_PATH) 134 $(INSTALL) $(UTILS_SHARED) $(UTILS_SHARED_SO) $(UTILS_SHARED_VERSION) $(INSTALL_LIBRARY_PATH) 135 136install: install-cjson install-utils 137 138#uninstall 139#cJSON 140uninstall-cjson: uninstall-utils 141 $(RM) $(INSTALL_LIBRARY_PATH)/$(CJSON_SHARED) 142 $(RM) $(INSTALL_LIBRARY_PATH)/$(CJSON_SHARED_VERSION) 143 $(RM) $(INSTALL_LIBRARY_PATH)/$(CJSON_SHARED_SO) 144 $(RM) $(INSTALL_INCLUDE_PATH)/cJSON.h 145 146#cJSON_Utils 147uninstall-utils: 148 $(RM) $(INSTALL_LIBRARY_PATH)/$(UTILS_SHARED) 149 $(RM) $(INSTALL_LIBRARY_PATH)/$(UTILS_SHARED_VERSION) 150 $(RM) $(INSTALL_LIBRARY_PATH)/$(UTILS_SHARED_SO) 151 $(RM) $(INSTALL_INCLUDE_PATH)/cJSON_Utils.h 152 153remove-dir: 154 $(if $(wildcard $(INSTALL_LIBRARY_PATH)/*.*),,rmdir $(INSTALL_LIBRARY_PATH)) 155 $(if $(wildcard $(INSTALL_INCLUDE_PATH)/*.*),,rmdir $(INSTALL_INCLUDE_PATH)) 156 157uninstall: uninstall-utils uninstall-cjson remove-dir 158 159clean: 160 $(RM) $(CJSON_OBJ) $(UTILS_OBJ) #delete object files 161 $(RM) $(CJSON_SHARED) $(CJSON_SHARED_VERSION) $(CJSON_SHARED_SO) $(CJSON_STATIC) #delete cJSON 162 $(RM) $(UTILS_SHARED) $(UTILS_SHARED_VERSION) $(UTILS_SHARED_SO) $(UTILS_STATIC) #delete cJSON_Utils 163 $(RM) $(CJSON_TEST) #delete test 164