Krolyxon
Commit 25a3114

Branches

Refs →

nsxiv25a3114

Rework the build system (#19)

* Added simple configure script

Added simple script to autodetect if optional dependencies are installed
and enable/disable them as needed. Note this solution uses the compiler
directly instead of an external program like pkgconfig so it doesn't
require any extra dependencies. It is intended to work with any
arbitrary compiler; it has been tested with tcc and gcc.

There are some "breaking" changes hidden here
- HAVE_GIFLIB was renamed to HAVE_LIBGIF to match HAVE_LIBEXIF
- Simply typing `make` will no longer try to build with optional
  dependencies

* use implicit RM variable

* General clean-up in configure

- Use printf instead of echo
- Format style

* honor env PREFIX, use LDLIBS

* Revert "General clean-up in configure"

This reverts commit 8683c179dbf273a330f9a224a4d481a7bea42c5f.

* honor env LDFLAGS if set

* Don't set OPTIONAL_LIBS in configure

* make OBJ all caps

* follow suckless style build system

- remove configure script.
- HAVE_LIBGIF and HAVE_LIBEXIF defaults back to 1
- unload several varibales onto config.mk
- make version all-caps
- add -O2 optimization
- use CPPFLAGS for includes and defines

* Revert "follow suckless style build system"

This reverts commit 8bf75b1f68d72df349edba8d998d4659dd956dd8.

* Generate config.mk from make

* Inlined configure in Makefile

* update docs

* cleanups

- changes to config.mk should trigger a rebuild
- remove potentially confusing variables form Makefile

* Use install instead of mkdir/cp/chmod when sensible

* fixup! Inlined configure in Makefile

* Don't generate config.mk on rm -f *.o sxiv

* update docs and cleanups

- make config.mk silent
- mention editing config.mk in README

* fallback to 0 if user edits config.mk in unexpected way

* add comment on config.mk

* remove invalid comment

configure script is removed

* slight restructure

- make version all caps
- restructure variables that users may want to edit to top
- use CPPFLAGS for defines
- add some comments
- remove needless echos since we have verbose output now

* add echos back
Author
TAAPArthur
Date
2021-09-14 07:42 -0700
Committer
Berke Kocaoğlu · 2021-09-16 22:55 +0300
Parent
cca7834
Commit
25a31147093e249be997da9b42da6901496fdf52

+66 −48

4 files changed

  1. M .gitignore +1 −0
  2. M Makefile +50 −42
  3. M README.md +11 −2
  4. M image.c +4 −4
M .gitignore View
@@ -3,3 +3,4 @@ version.h
33 *.d
44 *.o
55 sxiv
6+config.mk
M Makefile View
@@ -1,82 +1,90 @@
1−version = 26
1+# Include configure options
2+ifneq (clean,$(filter clean,$(MAKECMDGOALS)))
3+-include config.mk
4+endif
25
3−srcdir = .
4−VPATH = $(srcdir)
6+# sxiv version
7+VERSION = 26
58
6−PREFIX = /usr/local
9+# PREFIX for install
10+PREFIX ?= /usr/local
711 MANPREFIX = $(PREFIX)/share/man
812
913 # autoreload backend: inotify/nop
1014 AUTORELOAD = inotify
1115
12−# enable features requiring giflib (-lgif)
13−HAVE_GIFLIB = 1
14−
15−# enable features requiring libexif (-lexif)
16−HAVE_LIBEXIF = 1
17−
18−cflags = -std=c99 -Wall -pedantic $(CFLAGS)
19−cppflags = -I. $(CPPFLAGS) -D_XOPEN_SOURCE=700 \
20− -DHAVE_GIFLIB=$(HAVE_GIFLIB) -DHAVE_LIBEXIF=$(HAVE_LIBEXIF) \
16+# CFLAGS, any optimization flags goes here
17+CFLAGS ?= -std=c99 -Wall -pedantic
18+
19+ifeq ($(HAVE_LIBEXIF), 1)
20+ OPTIONAL_LIBS += -lexif
21+else
22+ HAVE_LIBEXIF = 0
23+endif
24+ifeq ($(HAVE_LIBGIF), 1)
25+ OPTIONAL_LIBS += -lgif
26+else
27+ HAVE_LIBGIF = 0
28+endif
29+
30+CPPFLAGS = -D_XOPEN_SOURCE=700 \
31+ -DHAVE_LIBGIF=$(HAVE_LIBGIF) -DHAVE_LIBEXIF=$(HAVE_LIBEXIF) \
2132 -I/usr/include/freetype2 -I$(PREFIX)/include/freetype2
2233
23−lib_exif_0 =
24−lib_exif_1 = -lexif
25−lib_gif_0 =
26−lib_gif_1 = -lgif
27−ldlibs = $(LDLIBS) -lImlib2 -lX11 -lXft -lfontconfig \
28− $(lib_exif_$(HAVE_LIBEXIF)) $(lib_gif_$(HAVE_GIFLIB))
34+LDLIBS = -lImlib2 -lX11 -lXft -lfontconfig $(OPTIONAL_LIBS)
2935
30−objs = autoreload_$(AUTORELOAD).o commands.o image.o main.o options.o \
36+OBJS = autoreload_$(AUTORELOAD).o commands.o image.o main.o options.o \
3137 thumbs.o util.o window.o
3238
33−all: sxiv
34−
3539 .PHONY: all clean install uninstall
3640 .SUFFIXES:
3741 .SUFFIXES: .c .o
38−$(V).SILENT:
3942
40−sxiv: $(objs)
43+all: sxiv
44+
45+sxiv: $(OBJS)
4146 @echo "LINK $@"
42− $(CC) $(LDFLAGS) -o $@ $(objs) $(ldlibs)
47+ $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
4348
44−$(objs): Makefile sxiv.h commands.lst config.h
49+.c.o:
50+ @echo "CC $@"
51+ $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
52+
53+$(OBJS): Makefile sxiv.h commands.lst config.h config.mk
4554 options.o: version.h
4655 window.o: icon/data.h
4756
48−.c.o:
49− @echo "CC $@"
50− $(CC) $(cflags) $(cppflags) -c -o $@ $<
57+config.mk:
58+ @echo "GEN $@"
59+ @echo "# 0 = disable, 1 = enable" > config.mk
60+ @for lib in exif gif; do \
61+ if echo "int main(){}" | $(CC) "-l$$lib" -o /dev/null -x c - 2>/dev/null ; then \
62+ echo "HAVE_LIB$$lib=1" | tr '[:lower:]' '[:upper:]' >> config.mk ; \
63+ fi \
64+ done
5165
5266 config.h:
5367 @echo "GEN $@"
54− cp $(srcdir)/config.def.h $@
68+ cp config.def.h $@
5569
5670 version.h: Makefile .git/index
5771 @echo "GEN $@"
58− v="$$(cd $(srcdir); git describe 2>/dev/null)"; \
59− echo "#define VERSION \"$${v:-$(version)}\"" >$@
60−
61−.git/index:
72+ v="$$(git describe 2>/dev/null)"; \
73+ echo "#define VERSION \"$${v:-$(VERSION)}\"" >$@
6274
6375 clean:
64− rm -f *.o sxiv
76+ $(RM) *.o sxiv
6577
6678 install: all
6779 @echo "INSTALL bin/sxiv"
68− mkdir -p $(DESTDIR)$(PREFIX)/bin
69− cp sxiv $(DESTDIR)$(PREFIX)/bin/
70− chmod 755 $(DESTDIR)$(PREFIX)/bin/sxiv
80+ install -Dt $(DESTDIR)$(PREFIX)/bin sxiv
7181 @echo "INSTALL sxiv.1"
7282 mkdir -p $(DESTDIR)$(MANPREFIX)/man1
73− sed "s!PREFIX!$(PREFIX)!g; s!VERSION!$(version)!g" sxiv.1 \
83+ sed "s!PREFIX!$(PREFIX)!g; s!VERSION!$(VERSION)!g" sxiv.1 \
7484 >$(DESTDIR)$(MANPREFIX)/man1/sxiv.1
7585 chmod 644 $(DESTDIR)$(MANPREFIX)/man1/sxiv.1
7686 @echo "INSTALL share/sxiv/"
77− mkdir -p $(DESTDIR)$(PREFIX)/share/sxiv/exec
78− cp exec/* $(DESTDIR)$(PREFIX)/share/sxiv/exec/
79− chmod 755 $(DESTDIR)$(PREFIX)/share/sxiv/exec/*
87+ install -Dt $(DESTDIR)$(PREFIX)/share/sxiv/exec exec/*
8088
8189 uninstall:
8290 @echo "REMOVE bin/sxiv"
M README.md View
@@ -42,8 +42,8 @@ sxiv requires the following software to be installed:
4242 * Xft
4343 * freetype2
4444 * fontconfig
45− * giflib (optional, disabled with `HAVE_GIFLIB=0`)
46− * libexif (optional, disabled with `HAVE_LIBEXIF=0`)
45+ * giflib (optional, automatically enabled if installed)
46+ * libexif (optional, automatically enabled if installed)
4747
4848 Please make sure to install the corresponding development packages in case that
4949 you want to build sxiv on a distribution with separate runtime and development
@@ -62,6 +62,15 @@ Please note, that the latter one requires root privileges.
6262 By default, sxiv is installed using the prefix "/usr/local", so the full path
6363 of the executable will be "/usr/local/bin/sxiv".
6464
65+Running make will automatically detect if libexif and libgif are available and
66+enable them if so. CLI arguments will override any automatic detection.
67+For example:
68+
69+ $ make HAVE_LIBGIF=0
70+
71+will always disable libgif.
72+Alternatively, they can be disabled via editing `config.mk`.
73+
6574 You can install sxiv into a directory of your choice by changing the second
6675 command to:
6776
M image.c View
@@ -31,7 +31,7 @@
3131 #include <libexif/exif-data.h>
3232 #endif
3333
34−#if HAVE_GIFLIB
34+#if HAVE_LIBGIF
3535 #include <gif_lib.h>
3636 enum { DEF_GIF_DELAY = 75 };
3737 #endif
@@ -115,7 +115,7 @@ void exif_auto_orientate(const fileinfo_t *file)
115115 }
116116 #endif
117117
118−#if HAVE_GIFLIB
118+#if HAVE_LIBGIF
119119 bool img_load_gif(img_t *img, const fileinfo_t *file)
120120 {
121121 GifFileType *gif;
@@ -295,7 +295,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
295295
296296 return !err;
297297 }
298−#endif /* HAVE_GIFLIB */
298+#endif /* HAVE_LIBGIF */
299299
300300 Imlib_Image img_open(const fileinfo_t *file)
301301 {
@@ -333,7 +333,7 @@ bool img_load(img_t *img, const fileinfo_t *file)
333333 #endif
334334
335335 if ((fmt = imlib_image_format()) != NULL) {
336−#if HAVE_GIFLIB
336+#if HAVE_LIBGIF
337337 if (STREQ(fmt, "gif"))
338338 img_load_gif(img, file);
339339 #endif