Makefile 4.5 KB

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