GNUmakefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # GNUmakefile
  2. #
  3. # Copyright 2008 Bryan Ischo <[email protected]>
  4. #
  5. # This file is part of libs3.
  6. #
  7. # libs3 is free software: you can redistribute it and/or modify it under the
  8. # terms of the GNU Lesser General Public License as published by the Free
  9. # Software Foundation, version 3 of the License.
  10. #
  11. # In addition, as a special exception, the copyright holders give
  12. # permission to link the code of this library and its programs with the
  13. # OpenSSL library, and distribute linked combinations including the two.
  14. #
  15. # libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
  16. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. # details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License
  21. # version 3 along with libs3, in a file named COPYING. If not, see
  22. # <https://www.gnu.org/licenses/>.
  23. # I tried to use the autoconf/automake/autolocal/etc (i.e. autohell) tools
  24. # but I just couldn't stomach them. Since this is a Makefile for POSIX
  25. # systems, I will simply do away with autohell completely and use a GNU
  26. # Makefile. GNU make ought to be available pretty much everywhere, so I
  27. # don't see this being a significant issue for portability.
  28. # All commands assume a GNU compiler. For systems which do not use a GNU
  29. # compiler, write scripts with the same names as these commands, and taking
  30. # the same arguments, and translate the arguments and commands into the
  31. # appropriate non-POSIX ones as needed. libs3 assumes a GNU toolchain as
  32. # the most portable way to build software possible. Non-POSIX, non-GNU
  33. # systems can do the work of supporting this build infrastructure.
  34. # --------------------------------------------------------------------------
  35. # Set libs3 version number, unless it is already set.
  36. LIBS3_VER_MAJOR ?= 4
  37. LIBS3_VER_MINOR ?= 1
  38. LIBS3_VER := $(LIBS3_VER_MAJOR).$(LIBS3_VER_MINOR)
  39. # -----------------------------------------------------------------------------
  40. # Determine verbosity. VERBOSE_SHOW should be prepended to every command which
  41. # should only be displayed if VERBOSE is set. QUIET_ECHO may be used to
  42. # echo text only if VERBOSE is not set. Typically, a VERBOSE_SHOW command will
  43. # be paired with a QUIET_ECHO command, to provide a command which is displayed
  44. # in VERBOSE mode, along with text which is displayed in non-VERBOSE mode to
  45. # describe the command.
  46. #
  47. # No matter what VERBOSE is defined to, it ends up as true if it's defined.
  48. # This will be weird if you defined VERBOSE=false in the environment, and we
  49. # switch it to true here; but the meaning of VERBOSE is, "if it's defined to
  50. # any value, then verbosity is turned on". So don't define VERBOSE if you
  51. # don't want verbosity in the build process.
  52. # -----------------------------------------------------------------------------
  53. ifdef VERBOSE
  54. VERBOSE = true
  55. VERBOSE_ECHO = @ echo
  56. VERBOSE_SHOW =
  57. QUIET_ECHO = @ echo > /dev/null
  58. else
  59. VERBOSE = false
  60. VERBOSE_ECHO = @ echo > /dev/null
  61. VERBOSE_SHOW = @
  62. QUIET_ECHO = @ echo
  63. endif
  64. # --------------------------------------------------------------------------
  65. # BUILD directory
  66. ifndef BUILD
  67. ifdef DEBUG
  68. BUILD := build-debug
  69. else
  70. BUILD := build
  71. endif
  72. endif
  73. # --------------------------------------------------------------------------
  74. # DESTDIR directory
  75. ifndef DESTDIR
  76. DESTDIR := /usr
  77. endif
  78. # --------------------------------------------------------------------------
  79. # LIBDIR directory
  80. ifndef LIBDIR
  81. LIBDIR := ${DESTDIR}/lib
  82. endif
  83. # --------------------------------------------------------------------------
  84. # Compiler CC handling
  85. ifndef CC
  86. CC := gcc
  87. endif
  88. # --------------------------------------------------------------------------
  89. # Acquire configuration information for libraries that libs3 depends upon
  90. ifndef CURL_LIBS
  91. CURL_LIBS := $(shell curl-config --libs)
  92. endif
  93. ifndef CURL_CFLAGS
  94. CURL_CFLAGS := $(shell curl-config --cflags)
  95. endif
  96. ifndef LIBXML2_LIBS
  97. LIBXML2_LIBS := $(shell xml2-config --libs)
  98. endif
  99. ifndef LIBXML2_CFLAGS
  100. LIBXML2_CFLAGS := $(shell xml2-config --cflags)
  101. endif
  102. ifndef OPENSSL_LIBS
  103. OPENSSL_LIBS := -lssl -lcrypto
  104. endif
  105. # --------------------------------------------------------------------------
  106. # These CFLAGS assume a GNU compiler. For other compilers, write a script
  107. # which converts these arguments into their equivalent for that particular
  108. # compiler.
  109. ifndef CFLAGS
  110. ifdef DEBUG
  111. CFLAGS := -g
  112. else
  113. CFLAGS := -O3
  114. endif
  115. endif
  116. CFLAGS += -Wall -Werror -Wshadow -Wextra \
  117. -Iinc \
  118. $(CURL_CFLAGS) $(LIBXML2_CFLAGS) \
  119. -DLIBS3_VER_MAJOR=\"$(LIBS3_VER_MAJOR)\" \
  120. -DLIBS3_VER_MINOR=\"$(LIBS3_VER_MINOR)\" \
  121. -DLIBS3_VER=\"$(LIBS3_VER)\" \
  122. -D__STRICT_ANSI__ \
  123. -D_ISOC99_SOURCE \
  124. -D_POSIX_C_SOURCE=200112L
  125. LDFLAGS = $(CURL_LIBS) $(LIBXML2_LIBS) $(OPENSSL_LIBS) -lpthread
  126. STRIP ?= strip
  127. INSTALL := install --strip-program=$(STRIP)
  128. # --------------------------------------------------------------------------
  129. # Default targets are everything
  130. .PHONY: all
  131. all: exported test
  132. # --------------------------------------------------------------------------
  133. # Exported targets are the library and driver program
  134. .PHONY: exported
  135. exported: libs3 s3 headers
  136. # --------------------------------------------------------------------------
  137. # Install target
  138. .PHONY: install
  139. install: exported
  140. $(QUIET_ECHO) $(DESTDIR)/bin/s3: Installing executable
  141. $(VERBOSE_SHOW) $(INSTALL) -Dps -m u+rwx,go+rx $(BUILD)/bin/s3 \
  142. $(DESTDIR)/bin/s3
  143. $(QUIET_ECHO) \
  144. $(LIBDIR)/libs3.so.$(LIBS3_VER): Installing shared library
  145. $(VERBOSE_SHOW) $(INSTALL) -Dps -m u+rw,go+r \
  146. $(BUILD)/lib/libs3.so.$(LIBS3_VER_MAJOR) \
  147. $(LIBDIR)/libs3.so.$(LIBS3_VER)
  148. $(QUIET_ECHO) \
  149. $(LIBDIR)/libs3.so.$(LIBS3_VER_MAJOR): Linking shared library
  150. $(VERBOSE_SHOW) ln -sf libs3.so.$(LIBS3_VER) \
  151. $(LIBDIR)/libs3.so.$(LIBS3_VER_MAJOR)
  152. $(QUIET_ECHO) $(LIBDIR)/libs3.so: Linking shared library
  153. $(VERBOSE_SHOW) ln -sf libs3.so.$(LIBS3_VER_MAJOR) $(LIBDIR)/libs3.so
  154. $(QUIET_ECHO) $(LIBDIR)/libs3.a: Installing static library
  155. $(VERBOSE_SHOW) $(INSTALL) -Dp -m u+rw,go+r $(BUILD)/lib/libs3.a \
  156. $(LIBDIR)/libs3.a
  157. $(QUIET_ECHO) $(DESTDIR)/include/libs3.h: Installing header
  158. $(VERBOSE_SHOW) $(INSTALL) -Dp -m u+rw,go+r $(BUILD)/include/libs3.h \
  159. $(DESTDIR)/include/libs3.h
  160. # --------------------------------------------------------------------------
  161. # Uninstall target
  162. .PHONY: uninstall
  163. uninstall:
  164. $(QUIET_ECHO) Installed files: Uninstalling
  165. $(VERBOSE_SHOW) \
  166. rm -f $(DESTDIR)/bin/s3 \
  167. $(DESTDIR)/include/libs3.h \
  168. $(DESTDIR)/lib/libs3.a \
  169. $(DESTDIR)/lib/libs3.so \
  170. $(DESTDIR)/lib/libs3.so.$(LIBS3_VER_MAJOR) \
  171. $(DESTDIR)/lib/libs3.so.$(LIBS3_VER)
  172. # --------------------------------------------------------------------------
  173. # Compile target patterns
  174. $(BUILD)/obj/%.o: src/%.c
  175. $(QUIET_ECHO) $@: Compiling object
  176. @ mkdir -p $(dir $(BUILD)/dep/$<)
  177. @ $(CC) $(CFLAGS) -M -MG -MQ $@ -DCOMPILINGDEPENDENCIES \
  178. -o $(BUILD)/dep/$(<:%.c=%.d) -c $<
  179. @ mkdir -p $(dir $@)
  180. $(VERBOSE_SHOW) $(CC) $(CFLAGS) -o $@ -c $<
  181. $(BUILD)/obj/%.do: src/%.c
  182. $(QUIET_ECHO) $@: Compiling dynamic object
  183. @ mkdir -p $(dir $(BUILD)/dep/$<)
  184. @ $(CC) $(CFLAGS) -M -MG -MQ $@ -DCOMPILINGDEPENDENCIES \
  185. -o $(BUILD)/dep/$(<:%.c=%.dd) -c $<
  186. @ mkdir -p $(dir $@)
  187. $(VERBOSE_SHOW) $(CC) $(CFLAGS) -fpic -fPIC -o $@ -c $<
  188. # --------------------------------------------------------------------------
  189. # libs3 library targets
  190. LIBS3_SHARED = $(BUILD)/lib/libs3.so.$(LIBS3_VER_MAJOR)
  191. LIBS3_STATIC = $(BUILD)/lib/libs3.a
  192. .PHONY: libs3
  193. libs3: $(LIBS3_SHARED) $(LIBS3_STATIC)
  194. LIBS3_SOURCES := bucket.c bucket_metadata.c error_parser.c general.c \
  195. object.c request.c request_context.c \
  196. response_headers_handler.c service_access_logging.c \
  197. service.c simplexml.c util.c multipart.c
  198. $(LIBS3_SHARED): $(LIBS3_SOURCES:%.c=$(BUILD)/obj/%.do)
  199. $(QUIET_ECHO) $@: Building shared library
  200. @ mkdir -p $(dir $@)
  201. $(VERBOSE_SHOW) $(CC) -shared -Wl,-soname,libs3.so.$(LIBS3_VER_MAJOR) \
  202. -o $@ $^ $(LDFLAGS)
  203. $(LIBS3_STATIC): $(LIBS3_SOURCES:%.c=$(BUILD)/obj/%.o)
  204. $(QUIET_ECHO) $@: Building static library
  205. @ mkdir -p $(dir $@)
  206. $(VERBOSE_SHOW) $(AR) cr $@ $^
  207. # --------------------------------------------------------------------------
  208. # Driver program targets
  209. .PHONY: s3
  210. s3: $(BUILD)/bin/s3
  211. $(BUILD)/bin/s3: $(BUILD)/obj/s3.o $(LIBS3_SHARED)
  212. $(QUIET_ECHO) $@: Building executable
  213. @ mkdir -p $(dir $@)
  214. $(VERBOSE_SHOW) $(CC) -o $@ $^ $(LDFLAGS)
  215. # --------------------------------------------------------------------------
  216. # libs3 header targets
  217. .PHONY: headers
  218. headers: $(BUILD)/include/libs3.h
  219. $(BUILD)/include/libs3.h: inc/libs3.h
  220. $(QUIET_ECHO) $@: Linking header
  221. @ mkdir -p $(dir $@)
  222. $(VERBOSE_SHOW) ln -sf $(abspath $<) $@
  223. # --------------------------------------------------------------------------
  224. # Test targets
  225. .PHONY: test
  226. test: $(BUILD)/bin/testsimplexml
  227. $(BUILD)/bin/testsimplexml: $(BUILD)/obj/testsimplexml.o $(LIBS3_STATIC)
  228. $(QUIET_ECHO) $@: Building executable
  229. @ mkdir -p $(dir $@)
  230. $(VERBOSE_SHOW) $(CC) -o $@ $^ $(LIBXML2_LIBS)
  231. # --------------------------------------------------------------------------
  232. # Clean target
  233. .PHONY: clean
  234. clean:
  235. $(QUIET_ECHO) $(BUILD): Cleaning
  236. $(VERBOSE_SHOW) rm -rf $(BUILD)
  237. .PHONY: distclean
  238. distclean:
  239. $(QUIET_ECHO) $(BUILD): Cleaning
  240. $(VERBOSE_SHOW) rm -rf $(BUILD)
  241. # --------------------------------------------------------------------------
  242. # Clean dependencies target
  243. .PHONY: cleandeps
  244. cleandeps:
  245. $(QUIET_ECHO) $(BUILD)/dep: Cleaning dependencies
  246. $(VERBOSE_SHOW) rm -rf $(BUILD)/dep
  247. # --------------------------------------------------------------------------
  248. # Dependencies
  249. ALL_SOURCES := $(LIBS3_SOURCES) s3.c testsimplexml.c
  250. $(foreach i, $(ALL_SOURCES), $(eval -include $(BUILD)/dep/src/$(i:%.c=%.d)))
  251. $(foreach i, $(ALL_SOURCES), $(eval -include $(BUILD)/dep/src/$(i:%.c=%.dd)))
  252. # --------------------------------------------------------------------------
  253. # Debian package target
  254. DEBPKG = $(BUILD)/pkg/libs3_$(LIBS3_VER).deb
  255. DEBDEVPKG = $(BUILD)/pkg/libs3-dev_$(LIBS3_VER).deb
  256. .PHONY: deb
  257. deb: $(DEBPKG) $(DEBDEVPKG)
  258. $(DEBPKG): DEBARCH = $(shell dpkg-architecture | grep ^DEB_BUILD_ARCH= | \
  259. cut -d '=' -f 2)
  260. $(DEBPKG): exported $(BUILD)/deb/DEBIAN/control $(BUILD)/deb/DEBIAN/shlibs \
  261. $(BUILD)/deb/DEBIAN/postinst \
  262. $(BUILD)/deb/usr/share/doc/libs3/changelog.gz \
  263. $(BUILD)/deb/usr/share/doc/libs3/changelog.Debian.gz \
  264. $(BUILD)/deb/usr/share/doc/libs3/copyright
  265. DESTDIR=$(BUILD)/deb/usr $(MAKE) install
  266. rm -rf $(BUILD)/deb/usr/include
  267. rm -f $(BUILD)/deb/usr/lib/libs3.a
  268. @mkdir -p $(dir $@)
  269. fakeroot dpkg-deb -b $(BUILD)/deb $@
  270. mv $@ $(BUILD)/pkg/libs3_$(LIBS3_VER)_$(DEBARCH).deb
  271. $(DEBDEVPKG): DEBARCH = $(shell dpkg-architecture | grep ^DEB_BUILD_ARCH= | \
  272. cut -d '=' -f 2)
  273. $(DEBDEVPKG): exported $(BUILD)/deb-dev/DEBIAN/control \
  274. $(BUILD)/deb-dev/usr/share/doc/libs3-dev/changelog.gz \
  275. $(BUILD)/deb-dev/usr/share/doc/libs3-dev/changelog.Debian.gz \
  276. $(BUILD)/deb-dev/usr/share/doc/libs3-dev/copyright
  277. DESTDIR=$(BUILD)/deb-dev/usr $(MAKE) install
  278. rm -rf $(BUILD)/deb-dev/usr/bin
  279. rm -f $(BUILD)/deb-dev/usr/lib/libs3.so*
  280. @mkdir -p $(dir $@)
  281. fakeroot dpkg-deb -b $(BUILD)/deb-dev $@
  282. mv $@ $(BUILD)/pkg/libs3-dev_$(LIBS3_VER)_$(DEBARCH).deb
  283. $(BUILD)/deb/DEBIAN/control: debian/control
  284. @mkdir -p $(dir $@)
  285. echo -n "Depends: " > $@
  286. dpkg-shlibdeps -Sbuild -O $(BUILD)/lib/libs3.so.$(LIBS3_VER_MAJOR) | \
  287. cut -d '=' -f 2- >> $@
  288. sed -e 's/LIBS3_VERSION/$(LIBS3_VER)/' \
  289. < $< | sed -e 's/DEBIAN_ARCHITECTURE/$(DEBARCH)/' | \
  290. grep -v ^Source: >> $@
  291. $(BUILD)/deb-dev/DEBIAN/control: debian/control.dev
  292. @mkdir -p $(dir $@)
  293. sed -e 's/LIBS3_VERSION/$(LIBS3_VER)/' \
  294. < $< | sed -e 's/DEBIAN_ARCHITECTURE/$(DEBARCH)/' > $@
  295. $(BUILD)/deb/DEBIAN/shlibs:
  296. echo -n "libs3 $(LIBS3_VER_MAJOR) libs3 " > $@
  297. echo "(>= $(LIBS3_VER))" >> $@
  298. $(BUILD)/deb/DEBIAN/postinst: debian/postinst
  299. @mkdir -p $(dir $@)
  300. cp $< $@
  301. $(BUILD)/deb/usr/share/doc/libs3/copyright: LICENSE
  302. @mkdir -p $(dir $@)
  303. cp $< $@
  304. @echo >> $@
  305. @echo -n "An alternate location for the GNU General Public " >> $@
  306. @echo "License version 3 on Debian" >> $@
  307. @echo "systems is /usr/share/common-licenses/GPL-3." >> $@
  308. $(BUILD)/deb-dev/usr/share/doc/libs3-dev/copyright: LICENSE
  309. @mkdir -p $(dir $@)
  310. cp $< $@
  311. @echo >> $@
  312. @echo -n "An alternate location for the GNU General Public " >> $@
  313. @echo "License version 3 on Debian" >> $@
  314. @echo "systems is /usr/share/common-licenses/GPL-3." >> $@
  315. $(BUILD)/deb/usr/share/doc/libs3/changelog.gz: debian/changelog
  316. @mkdir -p $(dir $@)
  317. gzip --best -c $< > $@
  318. $(BUILD)/deb-dev/usr/share/doc/libs3-dev/changelog.gz: debian/changelog
  319. @mkdir -p $(dir $@)
  320. gzip --best -c $< > $@
  321. $(BUILD)/deb/usr/share/doc/libs3/changelog.Debian.gz: debian/changelog.Debian
  322. @mkdir -p $(dir $@)
  323. gzip --best -c $< > $@
  324. $(BUILD)/deb-dev/usr/share/doc/libs3-dev/changelog.Debian.gz: \
  325. debian/changelog.Debian
  326. @mkdir -p $(dir $@)
  327. gzip --best -c $< > $@