Skip to content

Commit 1c35158

Browse files
committed
cmake: reorganize source into directories and add cmake build
- add CMakeLists.txt with build equivalent to the Makefile - emulate getversion to set SOVERSION on the shared library - update the Makefile to build with the new directory layout
1 parent 1aa11b0 commit 1c35158

32 files changed

+153
-41
lines changed

.gitignore

+16-16
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
*.la
55
*.so
66
*.so.*
7-
gif2raw
8-
gif2rgb
9-
gifasm
10-
gifbg
11-
gifbuild
12-
gifclrmp
13-
gifcolor
14-
gifecho
15-
giffilter
16-
giffix
17-
gifhisto
18-
gifinto
19-
gifsponge
20-
giftext
21-
giftool
22-
gifwedge
7+
examples/gif2raw
8+
examples/gif2rgb
9+
examples/gifasm
10+
examples/gifbg
11+
examples/gifbuild
12+
examples/gifclrmp
13+
examples/gifcolor
14+
examples/gifecho
15+
examples/giffilter
16+
examples/giffix
17+
examples/gifhisto
18+
examples/gifinto
19+
examples/gifsponge
20+
examples/giftext
21+
examples/giftool
22+
examples/gifwedge
2323
doc/gif2raw.1
2424
doc/gif2raw.html
2525
doc/gif2rgb.1

CMakeLists.txt

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(giflib)
4+
5+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6+
7+
option(GIF_BUILD_SHARED "Build shared library" ON)
8+
option(GIF_BUILD_STATIC "Build static library" ON)
9+
option(GIF_BUILD_EXAMPLES "Build examples" ON)
10+
option(GIF_INSTALL_LIBS "Install libs" ON)
11+
option(GIF_INSTALL_EXAMPLES "Install exmaples" ON)
12+
13+
include(CheckCSourceCompiles)
14+
include(CheckLibraryExists)
15+
16+
check_library_exists(m sqrtf "" has_libm)
17+
check_c_source_compiles("#include <stdlib.h>
18+
int main(void) { return !!reallocarray(NULL, 1, 1); }" has_reallocarray)
19+
20+
if(has_libm)
21+
set(EXTRA_LIBS ${EXTRA_LIBS} m)
22+
endif()
23+
24+
file(READ include/gif_lib.h gif_lib_h_src)
25+
string(REGEX MATCH "#define GIFLIB_MAJOR [0-9]+" gif_v_major ${gif_lib_h_src})
26+
string(REGEX MATCH "#define GIFLIB_MINOR [0-9]+" gif_v_minor ${gif_lib_h_src})
27+
string(REGEX MATCH "#define GIFLIB_RELEASE [0-9]+" gif_v_release ${gif_lib_h_src})
28+
string(REGEX REPLACE "#define GIFLIB_MAJOR ([0-9]+)" "\\1" GIF_MAJOR ${gif_v_major})
29+
string(REGEX REPLACE "#define GIFLIB_MINOR ([0-9]+)" "\\1" GIF_MINOR ${gif_v_minor})
30+
string(REGEX REPLACE "#define GIFLIB_RELEASE ([0-9]+)" "\\1" GIF_RELEASE ${gif_v_release})
31+
set(GIF_VERSION "${GIF_MAJOR}.${GIF_MINOR}.${GIF_RELEASE}")
32+
33+
#
34+
# giflib library
35+
#
36+
37+
set(gif_source
38+
src/dgif_lib.c
39+
src/egif_lib.c
40+
src/gif_err.c
41+
src/gif_font.c
42+
src/gif_hash.c
43+
src/gifalloc.c)
44+
45+
set(gif_private_headers
46+
include/gif_hash.h
47+
include/gif_lib_private.h)
48+
49+
if(has_reallocarray)
50+
add_definitions(-DHAVE_REALLOCARRAY)
51+
else()
52+
list(APPEND gif_source src/openbsd-reallocarray.c)
53+
endif()
54+
55+
include_directories(include)
56+
57+
if(GIF_BUILD_SHARED)
58+
add_library(gif SHARED)
59+
target_sources(gif PRIVATE ${gif_source} ${gif_private_headers}
60+
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/gif_lib.h)
61+
set_target_properties(gif
62+
PROPERTIES SOVERSION ${GIF_MAJOR} VERSION ${GIF_VERSION})
63+
list(APPEND GIF_LIB_TARGETS gif)
64+
endif()
65+
66+
if(GIF_BUILD_STATIC)
67+
add_library(gif-static STATIC)
68+
target_sources(gif-static PRIVATE ${gif_source})
69+
list(APPEND GIF_LIB_TARGETS gif-static)
70+
endif()
71+
72+
if(GIF_INSTALL_LIBS)
73+
install(TARGETS ${GIF_LIB_TARGETS})
74+
endif()
75+
76+
if(TARGET gif-static)
77+
set(GIF_LIBRARY gif-static PARENT_SCOPE)
78+
elseif(TARGET gif)
79+
set(GIF_LIBRARY gif PARENT_SCOPE)
80+
else()
81+
message(FATAL_ERROR "GIF_BUILD_SHARED or GIF_BUILD_STATIC must be set")
82+
endif()
83+
84+
#
85+
# giflib examples
86+
#
87+
88+
set(gifcommon_source
89+
examples/qprintf.c
90+
examples/quantize.c
91+
examples/getarg.c)
92+
93+
set(gifcommon_headers
94+
examples/getarg.h)
95+
96+
set(gif_examples gif2rgb gifbg gifbuild gifclrmp gifcolor gifecho
97+
giffilter giffix gifhisto gifinto gifsponge giftext giftool gifwedge)
98+
99+
set(gif_installable_examples gif2rgb gifbuild giffix giftext giftool gifclrmp)
100+
101+
if(GIF_BUILD_EXAMPLES)
102+
add_library(gifcommon OBJECT)
103+
target_sources(gifcommon PRIVATE ${gifcommon_source} ${gifcommon_headers})
104+
foreach(example ${gif_examples})
105+
add_executable(${example} examples/${example}.c)
106+
target_link_libraries(${example} ${EXTRA_LIBS} gifcommon ${GIF_LIBRARY})
107+
endforeach()
108+
endif()
109+
110+
if(GIF_INSTALL_EXAMPLES)
111+
install(TARGETS ${gif_installable_examples})
112+
endif()

Makefile

+22-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99
OFLAGS = -O0 -g
1010
OFLAGS = -O2
11-
CFLAGS = -std=gnu99 -fPIC -Wall -Wno-format-truncation $(OFLAGS)
11+
CFLAGS = -std=gnu99 -fPIC -Wall -Wno-format-truncation -Iinclude $(OFLAGS)
1212

1313
SHELL = /bin/sh
1414
TAR = tar
@@ -28,36 +28,36 @@ LIBMINOR=2
2828
LIBPOINT=0
2929
LIBVER=$(LIBMAJOR).$(LIBMINOR).$(LIBPOINT)
3030

31-
SOURCES = dgif_lib.c egif_lib.c gifalloc.c gif_err.c gif_font.c \
32-
gif_hash.c openbsd-reallocarray.c
33-
HEADERS = gif_hash.h gif_lib.h gif_lib_private.h
31+
SOURCES = src/dgif_lib.c src/egif_lib.c src/gifalloc.c src/gif_err.c \
32+
src/gif_font.c src/gif_hash.c src/openbsd-reallocarray.c
33+
HEADERS = include/gif_hash.h include/gif_lib.h include/gif_lib_private.h
3434
OBJECTS = $(SOURCES:.c=.o)
3535

36-
USOURCES = qprintf.c quantize.c getarg.c
37-
UHEADERS = getarg.h
36+
USOURCES = examples/qprintf.c examples/quantize.c examples/getarg.c
37+
UHEADERS = examples/getarg.h
3838
UOBJECTS = $(USOURCES:.c=.o)
3939

4040
# Some utilities are installed
4141
INSTALLABLE = \
42-
gif2rgb \
43-
gifbuild \
44-
giffix \
45-
giftext \
46-
giftool \
47-
gifclrmp
42+
examples/gif2rgb \
43+
examples/gifbuild \
44+
examples/giffix \
45+
examples/giftext \
46+
examples/giftool \
47+
examples/gifclrmp
4848

4949
# Some utilities are only used internally for testing.
5050
# There is a parallel list in doc/Makefile.
5151
# These are all candidates for removal in future releases.
5252
UTILS = $(INSTALLABLE) \
53-
gifbg \
54-
gifcolor \
55-
gifecho \
56-
giffilter \
57-
gifhisto \
58-
gifinto \
59-
gifsponge \
60-
gifwedge
53+
examples/gifbg \
54+
examples/gifcolor \
55+
examples/gifecho \
56+
examples/giffilter \
57+
examples/gifhisto \
58+
examples/gifinto \
59+
examples/gifsponge \
60+
examples/gifwedge
6161

6262
LDLIBS=libgif.a -lm
6363

@@ -73,7 +73,7 @@ libgif.a: $(OBJECTS) $(HEADERS)
7373
$(AR) rcs libgif.a $(OBJECTS)
7474

7575
libutil.so: $(UOBJECTS) $(UHEADERS)
76-
$(CC) $(CFLAGS) -shared $(LDFLAGS) -Wl,-soname -Wl,libutil.so.$(LIBMAJOR) -o libutil.so $(UOBJECTS)
76+
$(CC) $(CFLAGS) -shared $(LDFLAGS) -Wl,-soname -Wl,libutil.so.$(LIBMAJOR) -o libutil.so $(UOBJECTS) libgif.so
7777

7878
libutil.a: $(UOBJECTS) $(UHEADERS)
7979
$(AR) rcs libutil.a $(UOBJECTS)
@@ -95,7 +95,7 @@ install-bin: $(INSTALLABLE)
9595
$(INSTALL) $^ "$(DESTDIR)$(BINDIR)"
9696
install-include:
9797
$(INSTALL) -d "$(DESTDIR)$(INCDIR)"
98-
$(INSTALL) -m 644 gif_lib.h "$(DESTDIR)$(INCDIR)"
98+
$(INSTALL) -m 644 include/gif_lib.h "$(DESTDIR)$(INCDIR)"
9999
install-lib:
100100
$(INSTALL) -d "$(DESTDIR)$(LIBDIR)"
101101
$(INSTALL) -m 644 libgif.a "$(DESTDIR)$(LIBDIR)/libgif.a"

getarg.c examples/getarg.c

File renamed without changes.

getarg.h examples/getarg.h

File renamed without changes.

gif2rgb.c examples/gif2rgb.c

File renamed without changes.

gifbg.c examples/gifbg.c

File renamed without changes.

gifbuild.c examples/gifbuild.c

File renamed without changes.

gifclrmp.c examples/gifclrmp.c

File renamed without changes.

gifcolor.c examples/gifcolor.c

File renamed without changes.

gifecho.c examples/gifecho.c

File renamed without changes.

giffilter.c examples/giffilter.c

File renamed without changes.

giffix.c examples/giffix.c

File renamed without changes.

gifhisto.c examples/gifhisto.c

File renamed without changes.

gifinto.c examples/gifinto.c

File renamed without changes.

gifsponge.c examples/gifsponge.c

File renamed without changes.

giftext.c examples/giftext.c

File renamed without changes.

giftool.c examples/giftool.c

File renamed without changes.

gifwedge.c examples/gifwedge.c

File renamed without changes.

qprintf.c examples/qprintf.c

File renamed without changes.

quantize.c examples/quantize.c

File renamed without changes.

getversion

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Done this way so there's a single point of truth about the version.
66
#
7-
MAJOR=`sed <gif_lib.h -n -e "/MAJOR/s/#define GIFLIB_MAJOR *//p"`
8-
MINOR=`sed <gif_lib.h -n -e "/MINOR/s/#define GIFLIB_MINOR *//p"`
9-
RELEASE=`sed <gif_lib.h -n -e "/RELEASE/s/#define GIFLIB_RELEASE *//p"`
7+
MAJOR=`sed <include/gif_lib.h -n -e "/MAJOR/s/#define GIFLIB_MAJOR *//p"`
8+
MINOR=`sed <include/gif_lib.h -n -e "/MINOR/s/#define GIFLIB_MINOR *//p"`
9+
RELEASE=`sed <include/gif_lib.h -n -e "/RELEASE/s/#define GIFLIB_RELEASE *//p"`
1010
echo ${MAJOR}.${MINOR}.${RELEASE}

gif_hash.h include/gif_hash.h

File renamed without changes.

gif_lib.h include/gif_lib.h

File renamed without changes.
File renamed without changes.

dgif_lib.c src/dgif_lib.c

File renamed without changes.

egif_lib.c src/egif_lib.c

File renamed without changes.

gif_err.c src/gif_err.c

File renamed without changes.

gif_font.c src/gif_font.c

File renamed without changes.

gif_hash.c src/gif_hash.c

File renamed without changes.

gifalloc.c src/gifalloc.c

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)