Fix build on OS X

OS X has no open_memstream and its linker has no --gc-sections.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Thiago Macieira
2015-09-29 14:15:48 -07:00
parent 8c00769296
commit 2a5fb79dae
6 changed files with 150 additions and 3 deletions
+1
View File
@@ -74,3 +74,4 @@ bin
lib
src/cjson
!Makefile
.config
+11 -3
View File
@@ -7,7 +7,8 @@ includedir = $(prefix)/include
pkgconfigdir = $(libdir)/pkgconfig
CFLAGS = -Wall -Wextra
LDFLAGS = -Wl,--gc-sections
LDFLAGS_GCSECTIONS = -Wl,--gc-sections
LDFLAGS = $(if $(gc_sections-pass),$(LDFLAGS_GCSECTIONS))
GIT_ARCHIVE = git archive --prefix="$(PACKAGE)/" -9
INSTALL = install
@@ -27,6 +28,7 @@ TINYCBOR_SOURCES = \
src/cborparser.c \
src/cborpretty.c \
src/cbortojson.c \
$(if $(open_memstream-pass),,src/open_memstream.c) \
#
CBORDUMP_SOURCES = tools/cbordump/cbordump.c
@@ -67,12 +69,17 @@ ifeq ($(origin QMAKE),file)
endif
endif
-include .config
# Rules
all: lib/libtinycbor.a bin/cbordump tinycbor.pc
all: .config lib/libtinycbor.a bin/cbordump tinycbor.pc
check: tests/Makefile | lib/libtinycbor.a
$(MAKE) -C tests check
silentcheck: | lib/libtinycbor.a
TESTARGS=-silent $(MAKE) -f $(MAKEFILE) -s check
configure: .config
.config: Makefile.configure
$(MAKE) -f $(SRCDIR)Makefile.configure OUT='>&10' configure 10> $@
lib bin:
$(MKDIR) $@
@@ -145,7 +152,7 @@ release: .git
v=v`cat $(SRCDIR)VERSION` && git -C $(SRCDIR) tag -a -m "TinyCBOR release $$v" $(GITTAGFLAGS) $$v
$(MAKE) -f $(MAKEFILE) dist
.PHONY: all check silentcheck install uninstall
.PHONY: all check silentcheck configure install uninstall
.PHONY: mostlyclean clean distclean
.PHONY: dist distcheck release
.SECONDARY:
@@ -156,3 +163,4 @@ cflags += -std=c99 $(CFLAGS)
%.o: %.c
@test -d $(@D) || $(MKDIR) $(@D)
$(CC) $(cflags) -c -o $@ $<
+21
View File
@@ -0,0 +1,21 @@
ALLTESTS = open_memstream funopen gc_sections
MAKEFILE := $(lastword $(MAKEFILE_LIST))
OUT :=
PROGRAM-open_memstream = extern int open_memstream(); int main() { return open_memstream(); }
PROGRAM-funopen = extern int funopen(); int main() { return funopen(); }
PROGRAM-gc_sections = int main() {}
CCFLAGS-gc_sections = -Wl,--gc-sections
sink:
@echo >&2 Please run from the top-level Makefile.
configure: $(foreach it,$(ALLTESTS),check-$(it))
check-%:
@echo $(subst check-,,$@)-tested := 1 $(OUT)
$(if $(V),,@)if echo "$($(subst check-,PROGRAM-,$@))" | \
$(CC) -xc $($(subst check-,CCFLAGS-,$@)) -o /dev/null - $(if $(V),,>/dev/null 2>&1); \
then \
echo $(subst check-,,$@)-pass := 1 $(OUT); \
fi
+2
View File
@@ -36,6 +36,8 @@
#include <stdlib.h>
#include <string.h>
extern FILE *open_memstream(char **bufptr, size_t *sizeptr);
enum ConversionStatusFlags {
TypeWasNotNative = 0x100, // anything but strings, boolean, null, arrays and maps
TypeWasTagged = 0x200,
+4
View File
@@ -33,6 +33,10 @@
#include <stddef.h>
#include <stdint.h>
#ifndef __cplusplus
# include <stdbool.h>
#endif
#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L || __cpp_static_assert >= 200410
# define cbor_static_assert(x) static_assert(x, #x)
#elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)
+111
View File
@@ -0,0 +1,111 @@
/****************************************************************************
**
** Copyright (C) 2015 Intel Corporation
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/
#define _BSD_SOURCE 1
#define _GNU_SOURCE 1
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__unix__) || defined(__APPLE__)
# include <unistd.h>
#endif
#ifdef __APPLE__
typedef int RetType;
typedef int LenType;
#elif __GLIBC__
typedef ssize_t RetType;
typedef size_t LenType;
#else
# error "Cannot implement open_memstream!"
#endif
#include "compilersupport_p.h"
struct Buffer
{
char **ptr;
size_t *len;
size_t alloc;
};
static RetType write_to_buffer(void *cookie, const char *data, LenType len)
{
struct Buffer *b = (struct Buffer *)cookie;
char *ptr = *b->ptr;
size_t newsize;
errno = EFBIG;
if (unlikely(add_check_overflow(*b->len, len, &newsize)))
return -1;
if (newsize > b->alloc) {
// make room
size_t newalloc = newsize + newsize / 2 + 1; // give 50% more room
ptr = realloc(ptr, newalloc);
if (ptr == NULL)
return -1;
b->alloc = newalloc;
*b->ptr = ptr;
}
memcpy(ptr + *b->len, data, len);
*b->len = newsize;
return len;
}
static int close_buffer(void *cookie)
{
struct Buffer *b = (struct Buffer *)cookie;
(*b->ptr)[*b->len] = '\0';
free(b);
return 0;
}
FILE *open_memstream(char **bufptr, size_t *lenptr)
{
struct Buffer *b = (struct Buffer *)malloc(sizeof(struct Buffer));
if (b == NULL)
return NULL;
b->alloc = 0;
b->len = lenptr;
b->ptr = bufptr;
*bufptr = NULL;
*lenptr = 0;
#ifdef __APPLE__
return funopen(b, NULL, write_to_buffer, NULL, close_buffer);
#elif __GLIBC__
static const cookie_io_functions_t vtable = {
NULL,
write_to_buffer,
NULL,
close_buffer
};
#endif
}