1# ################################################################
2# LZ4 library - Makefile
3# Copyright (C) Yann Collet 2011-2016
4# All rights reserved.
5#
6# This Makefile is validated for Linux, macOS, *BSD, Hurd, Solaris, MSYS2 targets
7#
8# BSD license
9# Redistribution and use in source and binary forms, with or without modification,
10# are permitted provided that the following conditions are met:
11#
12# * Redistributions of source code must retain the above copyright notice, this
13#   list of conditions and the following disclaimer.
14#
15# * Redistributions in binary form must reproduce the above copyright notice, this
16#   list of conditions and the following disclaimer in the documentation and/or
17#   other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30# You can contact the author at :
31#  - LZ4 source repository : https://github.com/Cyan4973/lz4
32#  - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c
33# ################################################################
34
35# Version numbers
36LIBVER_MAJOR_SCRIPT:=`sed -n '/define LZ4_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h`
37LIBVER_MINOR_SCRIPT:=`sed -n '/define LZ4_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h`
38LIBVER_PATCH_SCRIPT:=`sed -n '/define LZ4_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h`
39LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
40LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
41LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
42LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
43LIBVER  := $(shell echo $(LIBVER_SCRIPT))
44
45BUILD_SHARED:=yes
46BUILD_STATIC:=yes
47
48CPPFLAGS+= -DXXH_NAMESPACE=LZ4_
49CFLAGS  ?= -O3
50DEBUGFLAGS:= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
51             -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes \
52             -Wundef -Wpointer-arith -Wstrict-aliasing=1
53CFLAGS  += $(DEBUGFLAGS) $(MOREFLAGS)
54FLAGS    = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
55
56SRCFILES := $(sort $(wildcard *.c))
57
58include ../Makefile.inc
59
60# OS X linker doesn't support -soname, and use different extension
61# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
62ifeq ($(TARGET_OS), Darwin)
63	SHARED_EXT = dylib
64	SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
65	SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
66	SONAME_FLAGS = -install_name $(libdir)/liblz4.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER)
67else
68	SONAME_FLAGS = -Wl,-soname=liblz4.$(SHARED_EXT).$(LIBVER_MAJOR)
69	SHARED_EXT = so
70	SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
71	SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
72endif
73
74.PHONY: default
75default: lib-release
76
77lib-release: DEBUGFLAGS :=
78lib-release: lib
79
80lib: liblz4.a liblz4
81
82all: lib
83
84all32: CFLAGS+=-m32
85all32: all
86
87liblz4.a: $(SRCFILES)
88ifeq ($(BUILD_STATIC),yes)  # can be disabled on command line
89	@echo compiling static library
90	$(Q)$(CC) $(CPPFLAGS) $(CFLAGS) -c $^
91	$(Q)$(AR) rcs $@ *.o
92endif
93
94ifeq ($(WINBASED),yes)
95liblz4-dll.rc: liblz4-dll.rc.in
96	@echo creating library resource
97	$(Q)sed -e 's|@LIBLZ4@|$(LIBLZ4)|' \
98         -e 's|@LIBVER_MAJOR@|$(LIBVER_MAJOR)|g' \
99         -e 's|@LIBVER_MINOR@|$(LIBVER_MINOR)|g' \
100         -e 's|@LIBVER_PATCH@|$(LIBVER_PATCH)|g' \
101          $< >$@
102
103liblz4-dll.o: liblz4-dll.rc
104	$(WINDRES) -i liblz4-dll.rc -o liblz4-dll.o
105
106$(LIBLZ4): $(SRCFILES) liblz4-dll.o
107else
108$(LIBLZ4): $(SRCFILES)
109endif
110ifeq ($(BUILD_SHARED),yes)  # can be disabled on command line
111	@echo compiling dynamic library $(LIBVER)
112  ifeq ($(WINBASED),yes)
113	$(Q)$(CC) $(FLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o dll/$@.dll -Wl,--out-implib,dll/$(LIBLZ4_EXP)
114  else
115	$(Q)$(CC) $(FLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@
116	@echo creating versioned links
117	$(Q)$(LN_SF) $@ liblz4.$(SHARED_EXT_MAJOR)
118	$(Q)$(LN_SF) $@ liblz4.$(SHARED_EXT)
119  endif
120endif
121
122ifeq (,$(filter MINGW%,$(TARGET_OS)))
123liblz4: $(LIBLZ4)
124endif
125
126clean:
127ifeq ($(WINBASED),yes)
128	$(Q)$(RM) *.rc
129endif
130	$(Q)$(RM) core *.o liblz4.pc dll/$(LIBLZ4).dll dll/$(LIBLZ4_EXP)
131	$(Q)$(RM) *.a *.$(SHARED_EXT) *.$(SHARED_EXT_MAJOR) *.$(SHARED_EXT_VER)
132	@echo Cleaning library completed
133
134#-----------------------------------------------------------------------------
135# make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets
136#-----------------------------------------------------------------------------
137ifeq ($(POSIX_ENV),Yes)
138
139.PHONY: listL120
140listL120:  # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility)
141	find . -type f -name '*.c' -o -name '*.h' | while read -r filename; do awk 'length > 120 {print FILENAME "(" FNR "): " $$0}' $$filename; done
142
143DESTDIR     ?=
144# directory variables : GNU conventions prefer lowercase
145# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
146# support both lower and uppercase (BSD), use lower in script
147PREFIX      ?= /usr/local
148prefix      ?= $(PREFIX)
149EXEC_PREFIX ?= $(prefix)
150exec_prefix ?= $(EXEC_PREFIX)
151BINDIR      ?= $(exec_prefix)/bin
152bindir      ?= $(BINDIR)
153LIBDIR      ?= $(exec_prefix)/lib
154libdir      ?= $(LIBDIR)
155INCLUDEDIR  ?= $(prefix)/include
156includedir  ?= $(INCLUDEDIR)
157
158  ifneq (,$(filter $(TARGET_OS),OpenBSD FreeBSD NetBSD DragonFly MidnightBSD))
159PKGCONFIGDIR ?= $(prefix)/libdata/pkgconfig
160  else
161PKGCONFIGDIR ?= $(libdir)/pkgconfig
162  endif
163pkgconfigdir ?= $(PKGCONFIGDIR)
164
165liblz4.pc: liblz4.pc.in Makefile
166	@echo creating pkgconfig
167	$(Q)sed -e 's|@PREFIX@|$(prefix)|' \
168         -e 's|@LIBDIR@|$(libdir)|' \
169         -e 's|@INCLUDEDIR@|$(includedir)|' \
170         -e 's|@VERSION@|$(LIBVER)|' \
171          $< >$@
172
173install: lib liblz4.pc
174	$(Q)$(INSTALL_DIR) $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ $(DESTDIR)$(bindir)/
175	$(Q)$(INSTALL_DATA) liblz4.pc $(DESTDIR)$(pkgconfigdir)/
176	@echo Installing libraries
177  ifeq ($(BUILD_STATIC),yes)
178	$(Q)$(INSTALL_DATA) liblz4.a $(DESTDIR)$(libdir)/liblz4.a
179	$(Q)$(INSTALL_DATA) lz4frame_static.h $(DESTDIR)$(includedir)/lz4frame_static.h
180  endif
181  ifeq ($(BUILD_SHARED),yes)
182# Traditionnally, one installs the DLLs in the bin directory as programs
183# search them first in their directory. This allows to not pollute system
184# directories (like c:/windows/system32), nor modify the PATH variable.
185    ifeq ($(WINBASED),yes)
186	$(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4).dll $(DESTDIR)$(bindir)
187	$(Q)$(INSTALL_PROGRAM) dll/$(LIBLZ4_EXP) $(DESTDIR)$(libdir)
188    else
189	$(Q)$(INSTALL_PROGRAM) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)
190	$(Q)$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR)
191	$(Q)$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT)
192    endif
193  endif
194	@echo Installing headers in $(includedir)
195	$(Q)$(INSTALL_DATA) lz4.h $(DESTDIR)$(includedir)/lz4.h
196	$(Q)$(INSTALL_DATA) lz4hc.h $(DESTDIR)$(includedir)/lz4hc.h
197	$(Q)$(INSTALL_DATA) lz4frame.h $(DESTDIR)$(includedir)/lz4frame.h
198	@echo lz4 libraries installed
199
200uninstall:
201	$(Q)$(RM) $(DESTDIR)$(pkgconfigdir)/liblz4.pc
202  ifeq (WINBASED,1)
203	$(Q)$(RM) $(DESTDIR)$(bindir)/$(LIBLZ4).dll
204	$(Q)$(RM) $(DESTDIR)$(libdir)/$(LIBLZ4_EXP)
205  else
206	$(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT)
207	$(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR)
208	$(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_VER)
209  endif
210	$(Q)$(RM) $(DESTDIR)$(libdir)/liblz4.a
211	$(Q)$(RM) $(DESTDIR)$(includedir)/lz4.h
212	$(Q)$(RM) $(DESTDIR)$(includedir)/lz4hc.h
213	$(Q)$(RM) $(DESTDIR)$(includedir)/lz4frame.h
214	$(Q)$(RM) $(DESTDIR)$(includedir)/lz4frame_static.h
215	@echo lz4 libraries successfully uninstalled
216
217endif
218