1###############################################################################
2#
3#  Copyright 2010 Jonathan Wallace. All rights reserved.
4#
5#  Redistribution and use in source and binary forms, with or without
6#  modification, are permitted provided that the following conditions are met:
7#
8#      1. Redistributions of source code must retain the above copyright
9#         notice, this list of conditions and the following disclaimer.
10#
11#      2. Redistributions in binary form must reproduce the above copyright
12#         notice, this list of conditions and the following disclaimer in the
13#         documentation and/or other materials provided with the distribution.
14#
15#  THIS SOFTWARE IS PROVIDED BY JONATHAN WALLACE ``AS IS'' AND ANY
16#  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JONATHAN WALLACE OR
19#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21#  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23#  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
24#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
25#  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26#  DAMAGE.
27#
28#  The views and conclusions contained in the software and documentation are
29#  those of the authors and should not be interpreted as representing official
30#  policies, either expressed or implied, of Jonathan Wallace....
31#
32#
33#  Author Bernhard Fluehmann
34#
35#  bernhard.fluehmann@patton-inalp.com
36#
37#
38#  TARGETS
39#    all
40#      Builds either a shared or a static library, depending of the value
41#      of the SHARED variable.
42#      Default: static
43#
44#    banner
45#      Displays the library version and the target
46#
47#    install
48#      Installs either the shared of the static library, depending of the value
49#      of the SHARED variable. Installs the header files as well.
50#      Builds the library before installing if it does not exist already.
51#      Ececutes ldconfig in case of the shared library.
52#      Default locations:
53#        library: $(exec_prefix)/$(libdir) => /usr/local/lib
54#        headers:   $(prefix)/$(includedir) => /usr/local/include
55#
56#    install_headers
57#      Installs the header files.
58#      Default location: $(prefix)/$(includedir) => /usr/local/include
59#
60#    clean
61#      Removes the shared or the static library, depending of the value
62#      of the SHARED variable, from this folder and all the folders and objects
63#      which were created to build it.
64#
65#    uninstall
66#      Removes the shared or the static library, depending of the value
67#      of the SHARED variable from the system. In case of the shared library,
68#      removes the symbolic links as well and executes ldconfig.
69#
70#    uninstall_headers
71#      Remove the headers from the system.
72#
73#  Variables
74#    prefix
75#      The general installation root directory. Default: /usr/local
76#
77#    exec_prefix
78#      The installation root directory to place executables and libraries.
79#      Default: prefix
80#
81#    libdir
82#      The directory where the libraries are installed, as a subdirectory of
83#      exec_prefix. Default: lib
84#
85#    includedir
86#      The directory where the header files are installed, as a subdirectory of
87#      prefix. Default: include
88#
89#    srcdir
90#      The directory where the object source files are located.
91#      Default: Sources
92#
93#    CXX
94#      The compiler to be used. Default: c++
95#
96#    CXXFLAGS
97#      The compiler flags used to compile the object files.
98#      Default:
99#        cxxflags_default for default
100#        cxxflags_small   for BUILD_TYPE=small
101#        cxxflags_debug   for BUILD_TYPE=debug
102#
103#    AR
104#      The archiver to be used. Default: ar
105#
106#    PIC
107#      The PIC to be used. Default: PIC
108#
109#    BUILD_TYPE
110#      Used to select a specific set of predefined configuaration parameters.
111#      Default:  undefined: The default settings are used
112#      Options:
113#        small
114#        debug
115#
116#    SHARED
117#      Used to select if the library is a shared or a dynamic one
118#      Default: undefined: static
119#      Options:
120#        0:         static
121#        1:         shared
122#
123#
124#
125
126# JSON source files to build
127objects        = internalJSONNode.o JSONAllocator.o JSONChildren.o \
128                 JSONDebug.o JSONIterators.o JSONMemory.o JSONNode.o \
129                 JSONNode_Mutex.o JSONPreparse.o JSONStream.o JSONValidator.o \
130                 JSONWorker.o JSONWriter.o libjson.o
131OS=$(shell uname)
132
133# Defaults
134ifeq ($(OS), Darwin)
135	cxxflags_default = -c -fast -ffast-math -fexpensive-optimizations -DNDEBUG
136else
137	cxxflags_default = -c -O3 -ffast-math -fexpensive-optimizations -DNDEBUG
138endif
139cxxflags_small   = -c -Os -ffast-math -DJSON_LESS_MEMORY -DNDEBUG
140cxxflags_debug   = -c -g -DJSON_SAFE -DJSON_DEBUG
141cxxflags_shared  = -f$(PIC)
142libname          = libjson
143libname_hdr      := $(libname)
144libname_debug    = $(libname)_dbg
145suffix_shared    = so
146suffix_static    = a
147major_version    = 7
148minor_version    = 6.0
149objdir           = Objects
150
151
152# Variables
153prefix          ?= /usr/local
154exec_prefix     ?= $(prefix)
155libdir          ?= lib
156includedir      ?= include
157srcdir          ?= Source
158CXX             ?= c++
159AR              ?= ar
160PIC             ?= PIC
161BUILD_TYPE      ?= "default"
162SHARED          ?= "1"
163
164
165# Internal Variables
166inst_path        = $(exec_prefix)/$(libdir)
167include_path     = $(prefix)/$(includedir)
168
169
170# Usage check
171ifdef CXXFLAGS
172ifdef BUILD_TYPE
173	$(error CXXFLAGS and BUILD_TYPE are mutually exclusive)
174endif
175endif
176
177# BUILD_TYPE specific settings
178ifeq ($(BUILD_TYPE), small)
179	CXXFLAGS     = $(cxxflags_small)
180else ifeq ($(BUILD_TYPE), debug)
181	CXXFLAGS     = $(cxxflags_debug)
182	libname     := $(libname_debug)
183else
184	CXXFLAGS    ?= $(cxxflags_default)
185endif
186
187# SHARED specific settings
188ifeq ($(SHARED), 1)
189	libname_shared               = $(libname).$(suffix_shared)
190	libname_shared_major_version = $(libname_shared).$(major_version)
191	lib_target                   = $(libname_shared_major_version).$(minor_version)
192	objdir                      := $(objdir)_shared
193	CXXFLAGS                    := $(CXXFLAGS) $(cxxflags_shared)
194else
195	lib_target                   = $(libname).$(suffix_static)
196	objdir                      := $(objdir)_static
197endif
198
199# Phony targets
200.PHONY: all banner installdirs install install_headers clean uninstall \
201        uninstall_headers
202
203# Targets
204all: $(lib_target)
205	@echo "============================================================"
206	@echo "Done"
207	@echo "============================================================"
208
209banner:
210	@echo "============================================================"
211	@echo "libjson version: "$(major_version).$(minor_version) "target: "$(target) "OS: "$(OS)
212	@echo "============================================================"
213
214installdirs: banner
215	mkdir -p $(objdir)
216
217# Libraries
218ifeq ($(SHARED),1)
219$(lib_target): banner installdirs $(addprefix $(objdir)/, $(objects))
220	@echo "Link "
221	cd $(objdir) ; \
222	if test "$(OS)" = "Darwin" ; then \
223		$(CXX) -shared -Wl,-dylib_install_name -Wl,$(libname_shared_major_version) -o $@ $(objects) ; \
224	else \
225		$(CXX) -shared -Wl,-soname,$(libname_shared_major_version) -o $@ $(objects) ; \
226	fi ; \
227	mv -f $@ ../
228	@echo "Link: Done"
229else
230$(lib_target): banner installdirs $(addprefix $(objdir)/, $(objects))
231	@echo "Archive"
232	cd $(objdir) ; \
233	$(AR) -cvq $@ $(objects) ; \
234	mv -f $@ ../
235	@echo "Archive: Done"
236endif
237
238# Compile object files
239$(objdir)/%.o: $(srcdir)/%.cpp
240	$(CXX) $< -o $@ $(CXXFLAGS)
241
242ifeq ($(SHARED),1)
243install: banner install_headers $(lib_target)
244	@echo "Install shared library"
245	cp -f ./$(lib_target) $(inst_path)
246	cd $(inst_path) ; \
247	ln -sf $(lib_target) $(libname_shared_major_version) ; \
248	ln -sf $(libname_shared_major_version) $(libname_shared)
249	ifneq ($(OS),Darwin)
250		ldconfig
251	endif
252	@echo "Install shared library: Done."
253else
254install: banner install_headers $(lib_target)
255	@echo "Install static library"
256	cp -f ./$(lib_target) $(inst_path)
257	@echo "Install static library: Done."
258endif
259
260install_headers: banner
261	@echo "Install header files"
262	mkdir -p $(include_path)/$(libname_hdr)
263	cp -f ./*.h $(include_path)/$(libname_hdr)
264	mkdir -p $(include_path)/$(libname_hdr)/$(srcdir)
265	cp -f ./$(srcdir)/*.h $(include_path)/$(libname_hdr)/$(srcdir)
266	cp -r ./$(srcdir)/JSONDefs $(include_path)/$(libname_hdr)/$(srcdir)
267	chmod -R a+r $(include_path)/$(libname_hdr)
268	find  $(include_path)/$(libname_hdr) -type d -exec chmod a+x {} \;
269	@echo "Install header files: Done."
270
271clean: banner
272	@echo "Clean library and object folder"
273	rm -rf $(objdir)
274	rm -f $(lib_target)
275	@echo "Clean library and object folder: Done"
276
277ifeq ($(SHARED),1)
278uninstall: banner uninstall_headers
279	@echo "Uninstall shared library"
280	rm -f $(inst_path)/$(libname_shared)
281	rm -f $(inst_path)/$(libname_shared_major_version)
282	rm -f $(inst_path)/$(lib_target)
283	ldconfig
284	@echo "Uninstall shared library: Done"
285else
286uninstall: banner uninstall_headers
287	@echo "Uninstall static library"
288	rm -f $(inst_path)/$(lib_target)
289	@echo "Uninstall static library: Done"
290endif
291
292uninstall_headers: banner
293	@echo "Uninstall header files"
294	rm -rf $(include_path)/$(libname)
295	@echo "Uninstall header files: Done"
296