1# Makefile 2# 3# Copyright (C) 2010-2023 Olaf Bergmann <bergmann@tzi.org> and others 4# 5# SPDX-License-Identifier: BSD-2-Clause 6# 7# This file is part of the CoAP C library libcoap. Please see README and 8# COPYING for terms of use. 9 10libcoap_dir = ../.. 11 12LIBCOAP_API_VERSION = $(shell if [ -f $(libcoap_dir)/configure.ac ]; then \ 13 sed -ne 's/^LIBCOAP_API_VERSION=\([0-9]\+\).*$$/\1/p' $(libcoap_dir)/configure.ac; \ 14 else echo -n "3"; fi) 15 16coap_include_dir = $(libcoap_dir)/include/coap$(LIBCOAP_API_VERSION) 17 18WITH_LWIP_BRANCH=STABLE-2_1_3_RELEASE 19WITH_LWIP_CONTRIB_BRANCH=STABLE-2_1_0_RELEASE 20# Update to latest TinyDTLS submodule as defined for libcoap 21WITH_TINYDTLS_BRANCH=f1d47d533b70ac10e4ff0ae3a088dfe4f265d242 22 23# Need to determine which library clock_gettime() resides in (as found by ./autogen.sh) 24LDLIBS := $(shell if [ -f $(libcoap_dir)/config.log ] ; then \ 25 grep ac_cv_search_clock_gettime=- $(libcoap_dir)/config.log|cut -d= -f2 ; fi) 26 27# 28# Remove the 4 -dtls checks from "all" if you do not want DTLS included in 29# some of the built objects. 30# 31all: lwip \ 32 lwip-contrib \ 33 check-version \ 34 check-tinydtls \ 35 $(coap_include_dir)/coap.h \ 36 lib-server \ 37 server \ 38 lib-client \ 39 client \ 40 lib-server-dtls \ 41 server-dtls \ 42 lib-client-dtls \ 43 client-dtls 44 45lwip: 46 git clone --depth 1 https://git.savannah.nongnu.org/git/lwip.git -b $(WITH_LWIP_BRANCH) 47 (cd lwip ; git checkout $(WITH_LWIP_BRANCH)) 48 $(MAKE) 49 50lwip-contrib: 51 git clone --depth 1 https://git.savannah.nongnu.org/git/lwip/lwip-contrib.git -b $(WITH_LWIP_CONTRIB_BRANCH) 52 (cd lwip-contrib ; git checkout $(WITH_LWIP_CONTRIB_BRANCH)) 53 $(MAKE) 54 55check-version: 56 @(if [ -d lwip ] ; then \ 57 cd lwip ; \ 58 TAG=`git describe --tags --all`; \ 59 if [ "$$TAG" != ${WITH_LWIP_BRANCH} ] ; then \ 60 if [ "$$TAG" != "tags/${WITH_LWIP_BRANCH}" ] ; then \ 61 echo "Updating lwip to ${WITH_LWIP_BRANCH}" ; \ 62 cd .. ; \ 63 rm -rf lwip ; \ 64 ${MAKE}; \ 65 fi ; \ 66 fi ; \ 67 fi) 68 @(if [ -d lwip-contrib ] ; then \ 69 cd lwip-contrib ; \ 70 TAG=`git describe --tags`; \ 71 if [ "$$TAG" != ${WITH_LWIP_CONTRIB_BRANCH} ] ; then \ 72 if [ "$$TAG" != "tags/${WITH_LWIP_CONTRIB_BRANCH}" ] ; then \ 73 echo "Updating lwip-contib to ${WITH_LWIP_CONTRIB_BRANCH}" ; \ 74 cd .. ; \ 75 rm -rf lwip-contrib ; \ 76 ${MAKE}; \ 77 fi ; \ 78 fi ; \ 79 fi) 80 81# base libcoap git has empty ext/tinydtls 82check-tinydtls: 83 @(if [ ! -d $(libcoap_dir)/ext/tinydtls ] ; then \ 84 mkdir -p $(libcoap_dir)/ext ; \ 85 (cd $(libcoap_dir)/ext ; git clone https://github.com/eclipse/tinydtls.git) ; \ 86 (cd $(libcoap_dir)/ext/tinydtls ; git checkout ${WITH_TINYDTLS_BRANCH}) ; \ 87 fi ; \ 88 if [ ! -f $(libcoap_dir)/ext/tinydtls/dtls.c ] ; then \ 89 IN_GIT=`git rev-parse --is-inside-work-tree` ; \ 90 if [ "$$IN_GIT" = "true" ] ; then \ 91 (cd $(libcoap_dir) ; git submodule init ; git submodule update) ; \ 92 else \ 93 (cd $(libcoap_dir)/ext ; git clone https://github.com/eclipse/tinydtls.git) ; \ 94 (cd $(libcoap_dir)/ext/tinydtls ; git checkout ${WITH_TINYDTLS_BRANCH}) ; \ 95 fi ; \ 96 if [ ! -f $(libcoap_dir)/ext/tinydtls/dtls.c ] ; then \ 97 exit 1 ; \ 98 fi ; \ 99 fi ; \ 100 IN_GIT=`git rev-parse --is-inside-work-tree` ; \ 101 if [ "$$IN_GIT" = "true" ] ; then \ 102 PWDDIR=`pwd` ; \ 103 cd $(libcoap_dir) ; TAG=`git ls-tree HEAD ext/tinydtls | cut -d\ -f3 | cut -f1` ;\ 104 if [ ! -z "$$TAG" ] ; then \ 105 if [ "$$TAG" != ${WITH_TINYDTLS_BRANCH} ] ; then \ 106 echo "Update WITH_TINYDTLS_BRANCH in Makefile to $$TAG" >&2 ; \ 107 exit 1 ; \ 108 fi ;\ 109 fi ;\ 110 cd $$PWDDIR ; \ 111 fi ;\ 112 if [ ! -f $(libcoap_dir)/ext/tinydtls/dtls_config.h ] ; then \ 113 (cd $(libcoap_dir)/ext/tinydtls ; ./autogen.sh ; ./configure) ; \ 114 ${MAKE} ; \ 115 fi) 116 117# lwip and coap opts (include early to shadow the lwip-contrib/ports/unix/proj/minimal/ file and any ../../config.h) 118CFLAGS += -DWITH_LWIP -iquote./config $(EXTRA_CFLAGS) 119 120# lwip library 121 122CFLAGS += -Ilwip/src/include/ -Ilwip/src/include/ipv4/ \ 123 -Ilwip-contrib/ports/unix/port/include/ \ 124 -Ilwip-contrib/ports/unix/proj/minimal/ 125 126LWIP_SRC = def.c init.c tapif.c etharp.c netif.c timeouts.c stats.c udp.c \ 127 tcp.c pbuf.c ip4_addr.c ip4.c inet_chksum.c tcp_in.c tcp_out.c \ 128 icmp.c raw.c ip4_frag.c sys_arch.c ethernet.c ip.c mem.c memp.c 129vpath %.c lwip/src/core/ lwip-contrib/ports/unix/proj/minimal/ \ 130 lwip/src/netif/ lwip/src/core/ipv4/ lwip-contrib/ports/unix/port/ \ 131 lwip-contrib/ports/unix/port/netif/ 132# CFLAGS += -DLWIP_UNIX_LINUX 133 134# if ipv6 is used 135vpath %.c lwip/src/core/ipv6/ 136LWIP_SRC += mld6.c ip6.c icmp6.c ethip6.c nd6.c ip6_addr.c ip6_frag.c 137 138CN_LWIP_OBJ =$(patsubst %.c,lib-client/%.o,$(LWIP_SRC)) 139SN_LWIP_OBJ =$(patsubst %.c,lib-server/%.o,$(LWIP_SRC)) 140CD_LWIP_OBJ =$(patsubst %.c,lib-client-dtls/%.o,$(LWIP_SRC)) 141SD_LWIP_OBJ =$(patsubst %.c,lib-server-dtls/%.o,$(LWIP_SRC)) 142 143# coap library 144 145CFLAGS += -std=gnu99 146 147CFLAGS += -I$(libcoap_dir)/include 148 149vpath %.c $(libcoap_dir)/src 150 151COAP_SRC = coap_address.c \ 152 coap_asn1.c \ 153 coap_async.c \ 154 coap_block.c \ 155 coap_cache.c \ 156 coap_debug.c \ 157 coap_dtls.c \ 158 coap_encode.c \ 159 coap_hashkey.c \ 160 coap_io.c \ 161 coap_io_lwip.c \ 162 coap_layers.c \ 163 coap_net.c \ 164 coap_netif.c \ 165 coap_notls.c \ 166 coap_option.c \ 167 coap_oscore.c \ 168 coap_pdu.c \ 169 coap_resource.c \ 170 coap_session.c \ 171 coap_subscribe.c \ 172 coap_tinydtls.c \ 173 coap_str.c \ 174 coap_tcp.c \ 175 coap_uri.c \ 176 coap_ws.c 177 178CN_COAP_OBJ =$(patsubst %.c,lib-client/%.o,$(COAP_SRC)) 179SN_COAP_OBJ =$(patsubst %.c,lib-server/%.o,$(COAP_SRC)) 180CD_COAP_OBJ =$(patsubst %.c,lib-client-dtls/%.o,$(COAP_SRC)) 181SD_COAP_OBJ =$(patsubst %.c,lib-server-dtls/%.o,$(COAP_SRC)) 182 183# tinydtls library 184 185vpath %.c $(libcoap_dir)/ext/tinydtls $(libcoap_dir)/ext/tinydtls/sha2 $(libcoap_dir)/ext/tinydtls/aes $(libcoap_dir)/ext/tinydtls/ecc 186 187TINYDTLS_CFLAGS = -I. -I$(libcoap_dir)/ext -I$(libcoap_dir)/ext/tinydtls -DDTLSv12 -DWITH_SHA256 -DSHA2_USE_INTTYPES_H -DDTLS_CHECK_CONTENTTYPE -DCOAP_WITH_LIBTINYDTLS -DHAVE_DTLS_SET_LOG_HANDLER=1 188 189DTLS_SRC = dtls.c \ 190 dtls_debug.c \ 191 crypto.c \ 192 dtls_time.c \ 193 hmac.c \ 194 sha2.c \ 195 session.c \ 196 peer.c \ 197 netq.c \ 198 rijndael_wrap.c \ 199 rijndael.c \ 200 ecc.c \ 201 ccm.c \ 202 dtls_prng.c 203 204CN_DTLS_OBJ =$(patsubst %.c,lib-client/%.o,$(DTLS_SRC)) 205SN_DTLS_OBJ =$(patsubst %.c,lib-server/%.o,$(DTLS_SRC)) 206CD_DTLS_OBJ =$(patsubst %.c,lib-client-dtls/%.o,$(DTLS_SRC)) 207SD_DTLS_OBJ =$(patsubst %.c,lib-server-dtls/%.o,$(DTLS_SRC)) 208 209CFLAGS += -g3 -Wall -Wextra -pedantic -O0 210 211CN_APP_OBJ =$(patsubst %.c,lib-client/%.o,client.c client-coap.c) 212SN_APP_OBJ =$(patsubst %.c,lib-server/%.o,server.c server-coap.c) 213CD_APP_OBJ =$(patsubst %.c,lib-client-dtls/%.o,client.c client-coap.c) 214SD_APP_OBJ =$(patsubst %.c,lib-server-dtls/%.o,server.c server-coap.c) 215 216 217CN_OBJS = ${CN_APP_OBJ} ${CN_LWIP_OBJ} ${CN_COAP_OBJ} 218SN_OBJS = ${SN_APP_OBJ} ${SN_LWIP_OBJ} ${SN_COAP_OBJ} 219CD_OBJS = ${CD_APP_OBJ} ${CD_LWIP_OBJ} ${CD_COAP_OBJ} ${CD_DTLS_OBJ} 220SD_OBJS = ${SD_APP_OBJ} ${SD_LWIP_OBJ} ${SD_COAP_OBJ} ${SD_DTLS_OBJ} 221 222$(coap_include_dir)/coap.h: 223 @(if [ ! -f $(coap_include_dir)/coap.h ] ; then \ 224 (cd $(libcoap_dir) ; ./autogen.sh ; ./configure --disable-dtls --disable-man) ; \ 225 ${MAKE}; \ 226 fi) 227 228${SN_OBJS} ${SD_OBJS}: $(coap_include_dir)/coap.h server-coap.h 229 230server: ${SN_OBJS} 231 $(CC) $(CFLAGS) ${SN_OBJS} -o server ${LDLIBS} 232 233server-dtls: ${SD_OBJS} 234 $(CC) $(CFLAGS) ${SD_OBJS} -o server-dtls ${LDLIBS} 235 236${CN_OBJS} ${CD_OBJS}: $(coap_include_dir)/coap.h client-coap.h 237 238client: ${CN_OBJS} 239 $(CC) $(CFLAGS) ${CN_OBJS} -o client ${LDLIBS} 240 241client-dtls: ${CD_OBJS} 242 $(CC) $(CFLAGS) ${CD_OBJS} -o client-dtls ${LDLIBS} 243 244lib-server: 245 @mkdir -p $@ 246 247lib-server/%.o: %.c config/lwipopts.h config/lwippools.h config/coap_config.h 248 $(CC) ${CFLAGS} -DCOAP_SERVER_SUPPORT -c $< -o $@ 249 250lib-server-dtls: 251 @mkdir -p $@ 252 253lib-server-dtls/%.o: %.c config/lwipopts.h config/lwippools.h config/coap_config.h 254 $(CC) ${CFLAGS} ${TINYDTLS_CFLAGS} -DCOAP_SERVER_SUPPORT -c $< -o $@ 255 256lib-client: 257 @mkdir -p $@ 258 259lib-client/%.o: %.c config/lwipopts.h config/lwippools.h config/coap_config.h 260 $(CC) ${CFLAGS} -DCOAP_CLIENT_SUPPORT -c $< -o $@ 261 262lib-client-dtls: 263 @mkdir -p $@ 264 265lib-client-dtls/%.o: %.c config/lwipopts.h config/lwippools.h config/coap_config.h 266 $(CC) ${CFLAGS} ${TINYDTLS_CFLAGS} -DCOAP_CLIENT_SUPPORT -c $< -o $@ 267 268clean: 269 rm -rf server server-dtls client client-dtls \ 270 ${CN_OBJS} ${SN_OBJS} ${CD_OBJS} ${SD_OBJS} \ 271 lib-server lib-server-dtls lib-client lib-client-dtls 272 273.PHONY: all clean 274