Commit 831accbd authored by lahiker42's avatar lahiker42

Add cmake support.

Minor cleanups to dispatch, set "has_idle" correctly.


git-svn-id: https://protobuf-c.googlecode.com/svn/trunk@276 00440858-1255-0410-a3e6-75ea37f81c3a
parent 8c7d9497
cmake_minimum_required(VERSION 2.8)
project(protobuf-c)
set(PROTOBUF_C_VERSION_MAJOR 0)
set(PROTOBUF_C_VERSION_MINOR 14)
set(PROTOBUF_C_VERSION_PATCH 1)
set(PROTOBUF_C_VERSION ${PROTOBUF_C_VERSION_MAJOR}.${PROTOBUF_C_VERSION_MINOR})
set(CMAKE_ROOT_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${CMAKE_SYSTEM}
CACHE PATH "root output directory in which to locate target files")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_ROOT_OUTPUT_DIRECTORY}/bin
CACHE PATH "output directory in which to build runtime target files")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_ROOT_OUTPUT_DIRECTORY}/lib
CACHE PATH "output directory in which to build library target files")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ROOT_OUTPUT_DIRECTORY}/lib
CACHE PATH "output directory in which to build archive target files")
function(process_libprotobuf_c_pc)
set(prefix ${PROTOBUF_C_INSTALL_PREFIX})
set(exec_prefix ${PROTOBUF_C_INSTALL_EXEC_PREFIX})
set(libdir ${PROTOBUF_C_INSTALL_LIBDIR})
set(includedir ${PROTOBUF_C_INSTALL_INCLUDEDIR})
set(VERSION ${PROTOBUF_C_VERSION})
configure_file(libprotobuf-c.pc.in
${CMAKE_ROOT_OUTPUT_DIRECTORY}/pkgconfig/libprotobuf-c.pc @ONLY)
install(DIRECTORY ${CMAKE_ROOT_OUTPUT_DIRECTORY}/pkgconfig DESTINATION
${PROTOBUF_C_INSTALL_LIBDIR})
endfunction()
set(PROTOBUF_C_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}
CACHE PATH "install location for architecture-independent files")
set(PROTOBUF_C_INSTALL_EXEC_PREFIX ${CMAKE_INSTALL_PREFIX}
CACHE PATH "install location for architecture-dependent files")
if ( UNIX )
set(PROTOBUF_C_INSTALL_BINDIR ${PROTOBUF_C_INSTALL_EXEC_PREFIX}/bin
CACHE PATH "install location for executable files")
set(PROTOBUF_C_INSTALL_LIBDIR ${PROTOBUF_C_INSTALL_EXEC_PREFIX}/lib
CACHE PATH "install location for object code libraries")
set(PROTOBUF_C_INSTALL_INCLUDEDIR ${PROTOBUF_C_INSTALL_PREFIX}/include
CACHE PATH "install location for public header files")
process_libprotobuf_c_pc()
else()
set(PROTOBUF_C_INSTALL_BINDIR bin
CACHE PATH "install location for executable files")
set(PROTOBUF_C_INSTALL_LIBDIR lib
CACHE PATH "install location for object code libraries")
set(PROTOBUF_C_INSTALL_INCLUDEDIR include
CACHE PATH "install location for public header files")
endif()
enable_testing()
add_subdirectory(src)
# Use "make package" or something like that to create a protobuf-c package.
set(CPACK_PACKAGE_CONTACT "David Benson <daveb@ffem.org>")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "C bindings for protocol-buffers")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_VERSION_MAJOR "${PROTOBUF_C_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${PROTOBUF_C_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${PROTOBUF_C_VERSION_PATCH}")
if ( WIN32 )
set(CPACK_GENERATOR ZIP)
else()
# Look at http://public.kitware.com/Bug/view.php?id=7000
set(CPACK_SET_DESTDIR "ON")
set(CPACK_GENERATOR DEB)
set(CPACK_DEBIAN_PACKAGE_SECTION "libs")
set(CPACK_DEBIAN_PACKAGE_PRIORITY "low")
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "http://code.google.com/p/protobuf-c")
endif()
include(CPack)
0.15 (NOT YET RELEASED)
0.15:
- make protobuf_c_message_init() into a function (Issue #49, daveb)
- Fix for freeing memory after unpacking bytes w/o a default-value.
(Andrei Nigmatulin)
- minor windows portability issues (use ProtobufC_FD) (Pop Stelian)
- --with-endianness={little,big} (Pop Stelian)
- bug setting up values of has_idle in public dispatch,
make protobuf_c_dispatch_run() use only public members (daveb)
- provide cmake support and some Windows compatibility (Nikita Manovich)
0.14:
- build fix (missing dependency in test directory)
......
SUBDIRS = src
EXTRA_DIST = scripts pkgwriteinfo.in
EXTRA_DIST = scripts pkgwriteinfo.in CMakeLists.txt
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libprotobuf-c.pc
......
......@@ -11,6 +11,9 @@ AC_CHECK_HEADERS(sys/select.h)
AC_CHECK_HEADERS(alloca.h)
AC_CHECK_HEADERS(malloc.h)
AC_CHECK_HEADERS(winsock.h)
AC_CHECK_HEADERS(io.h)
AC_CHECK_HEADERS(sys/uio.h)
AC_CHECK_HEADERS(unistd.h)
BUILD_PROTOC_C=1
AC_ARG_ENABLE(protoc, [ --disable-protoc Suppress build of protoc_c],
......
set(BUILD_PROTOBUF_C_COMPILER ON
CACHE BOOL "build protobuf-c compiler (aka protoc-c)")
if ( BUILD_PROTOBUF_C_COMPILER )
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})
set(PROTOC_C_SOURCES
google/protobuf/compiler/c/c_service.cc
google/protobuf/compiler/c/c_helpers.cc
google/protobuf/compiler/c/c_enum.cc
google/protobuf/compiler/c/c_enum_field.cc
google/protobuf/compiler/c/c_string_field.cc
google/protobuf/compiler/c/c_primitive_field.cc
google/protobuf/compiler/c/c_extension.cc
google/protobuf/compiler/c/c_file.cc
google/protobuf/compiler/c/c_field.cc
google/protobuf/compiler/c/c_message.cc
google/protobuf/compiler/c/c_generator.cc
google/protobuf/compiler/c/c_message_field.cc
google/protobuf/compiler/c/main.cc
google/protobuf/compiler/c/c_bytes_field.cc)
add_executable(protoc-c ${PROTOC_C_SOURCES})
target_link_libraries(protoc-c ${PROTOBUF_LIBRARY}
${PROTOBUF_PROTOC_LIBRARY})
if ( UNIX )
find_package(Threads REQUIRED)
target_link_libraries(protoc-c ${CMAKE_THREAD_LIBS_INIT})
endif()
install(TARGETS protoc-c DESTINATION ${PROTOBUF_C_INSTALL_BINDIR})
endif()
include(CheckIncludeFiles)
check_include_files("alloca.h" HAVE_ALLOCA_H)
check_include_files("malloc.h" HAVE_MALLOC_H)
check_include_files("sys/poll.h" HAVE_SYS_POLL_H)
check_include_files("sys/select.h" HAVE_SYS_SELECT_H)
check_include_files("inttypes.h" HAVE_INTTYPES_H)
check_include_files("sys/uio.h" HAVE_SYS_UIO_H)
check_include_files("unistd.h" HAVE_UNISTD_H)
check_include_files("io.h" HAVE_IO_H)
include(TestBigEndian)
test_big_endian(IS_BIG_ENDIAN)
if ( NOT IS_BIG_ENDIAN )
set(IS_LITTLE_ENDIAN 1)
endif()
add_definitions(-DHAVE_PROTOBUF_C_CONFIG_H=1)
configure_file(google/protobuf-c/protobuf-c-config.h.in
google/protobuf-c/protobuf-c-config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/google/protobuf-c)
set(BUILD_PROTOBUF_C_STATIC_LIB ON
CACHE BOOL "build static protobuf-c library")
set(BUILD_PROTOBUF_C_SHARED_LIB ON
CACHE BOOL "build shared protobuf-c library")
if ( WIN32 )
set(PROTOBUF_C_SOURCES
google/protobuf-c/protobuf-c-data-buffer.c
google/protobuf-c/protobuf-c.c)
else()
set(PROTOBUF_C_SOURCES
google/protobuf-c/protobuf-c-dispatch.c
google/protobuf-c/protobuf-c-data-buffer.c
google/protobuf-c/protobuf-c-rpc.c
google/protobuf-c/protobuf-c.c)
endif()
if ( BUILD_PROTOBUF_C_STATIC_LIB )
add_library(protobuf-c-static STATIC ${PROTOBUF_C_SOURCES})
install(TARGETS protobuf-c-static
ARCHIVE DESTINATION ${PROTOBUF_C_INSTALL_LIBDIR})
set_target_properties(protobuf-c-static PROPERTIES OUTPUT_NAME protobuf-c)
get_target_property(DIRECTORY protobuf-c-static ARCHIVE_OUTPUT_DIRECTORY)
set_target_properties(protobuf-c-static PROPERTIES ARCHIVE_OUTPUT_DIRECTORY
${DIRECTORY}/static)
endif()
if ( BUILD_PROTOBUF_C_SHARED_LIB )
add_library(protobuf-c-shared SHARED ${PROTOBUF_C_SOURCES})
install(TARGETS protobuf-c-shared
RUNTIME DESTINATION ${PROTOBUF_C_INSTALL_BINDIR}
LIBRARY DESTINATION ${PROTOBUF_C_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${PROTOBUF_C_INSTALL_LIBDIR}/dllimport)
set_target_properties(protobuf-c-shared PROPERTIES OUTPUT_NAME protobuf-c)
set_target_properties(protobuf-c-shared PROPERTIES VERSION
${PROTOBUF_C_VERSION})
get_target_property(DIRECTORY protobuf-c-shared ARCHIVE_OUTPUT_DIRECTORY)
set_target_properties(protobuf-c-shared PROPERTIES ARCHIVE_OUTPUT_DIRECTORY
${DIRECTORY}/shared)
get_target_property(DIRECTORY protobuf-c-shared LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(protobuf-c-shared PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${DIRECTORY}/shared)
if ( WIN32 )
set_target_properties(protobuf-c-shared PROPERTIES COMPILE_DEFINITIONS
"PROTOBUF_C_USE_SHARED_LIB;PROTOBUF_C_EXPORT")
endif()
endif()
if ( BUILD_PROTOBUF_C_STATIC_LIB OR BUILD_PROTOBUF_C_SHARED_LIB )
if ( WIN32 )
set(PROTOBUF_C_PUBLIC_HEADERS
google/protobuf-c/protobuf-c.h
google/protobuf-c/protobuf-c-private.h)
else()
set(PROTOBUF_C_PUBLIC_HEADERS
google/protobuf-c/protobuf-c.h
google/protobuf-c/protobuf-c-private.h
google/protobuf-c/protobuf-c-dispatch.h
google/protobuf-c/protobuf-c-rpc.h)
endif()
install(FILES ${PROTOBUF_C_PUBLIC_HEADERS} DESTINATION
${PROTOBUF_C_INSTALL_INCLUDEDIR})
endif()
add_subdirectory(test)
......@@ -23,6 +23,7 @@ endif
lib_LTLIBRARIES = libprotobuf-c.la
protobufcincludedir = $(includedir)/google/protobuf-c
EXTRA_DIST = CMakeLists.txt test/CMakeLists.txt
libprotobuf_c_la_SOURCES = \
google/protobuf-c/protobuf-c-dispatch.c \
......
......@@ -18,9 +18,19 @@
#define BUFFER_RECYCLING 0
#if HAVE_PROTOBUF_C_CONFIG_H
#include "protobuf-c-config.h"
#endif
#include <sys/types.h>
#if HAVE_SYS_UIO_H /* writev function isn't available on Windows */
#include <sys/uio.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#elif HAVE_IO_H
#include <io.h>
#define read _read
#endif
#include <string.h>
#include <errno.h>
#if HAVE_ALLOCA_H
......@@ -546,6 +556,7 @@ errno_is_ignorable (int e)
return e == EINTR || e == EAGAIN;
}
#if HAVE_SYS_UIO_H
/**
* protobuf_c_data_buffer_writev:
* @read_from: buffer to take data from.
......@@ -590,7 +601,9 @@ protobuf_c_data_buffer_writev (ProtobufCDataBuffer *read_from,
protobuf_c_data_buffer_discard (read_from, rv);
return rv;
}
#endif
#if HAVE_SYS_UIO_H
/**
* protobuf_c_data_buffer_writev_len:
* @read_from: buffer to take data from.
......@@ -645,6 +658,7 @@ protobuf_c_data_buffer_writev_len (ProtobufCDataBuffer *read_from,
protobuf_c_data_buffer_discard (read_from, rv);
return rv;
}
#endif
/**
* protobuf_c_data_buffer_read_in_fd:
......
......@@ -6,6 +6,9 @@
* * kqueue() implementation
* * windows port (yeah, right, volunteers are DEFINITELY needed for this one...)
*/
#if HAVE_PROTOBUF_C_CONFIG_H
#include "protobuf-c-config.h"
#endif
#include <assert.h>
#if HAVE_ALLOCA_H
# include <alloca.h>
......
......@@ -31,8 +31,8 @@
/* enabled for efficiency, can be useful to disable for debugging */
#define RECYCLE_REQUESTS 1
#define UINT_TO_POINTER(ui) ((void*)(ui))
#define POINTER_TO_UINT(ptr) ((unsigned)(ptr))
#define UINT_TO_POINTER(ui) ((void*)(uintptr_t)(ui))
#define POINTER_TO_UINT(ptr) ((unsigned)(uintptr_t)(ptr))
#define MAX_FAILED_MSG_LENGTH 512
......
......@@ -32,9 +32,17 @@
* use size_t consistently
*/
#if HAVE_PROTOBUF_C_CONFIG_H
#include "protobuf-c-config.h"
#endif
#include <stdio.h> /* for occasional printf()s */
#include <stdlib.h> /* for abort(), malloc() etc */
#include <string.h> /* for strlen(), memcpy(), memmove() */
#if HAVE_ALLOCA_H
#include <alloca.h>
#elif HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifndef PRINT_UNPACK_ERRORS
#define PRINT_UNPACK_ERRORS 1
......@@ -252,7 +260,7 @@ sint32_size (int32_t v)
static inline size_t
uint64_size (uint64_t v)
{
uint32_t upper_v = (v>>32);
uint32_t upper_v = (uint32_t )(v>>32);
if (upper_v == 0)
return uint32_size ((uint32_t)v);
else if (upper_v < (1<<3))
......@@ -551,8 +559,8 @@ sint32_pack (int32_t value, uint8_t *out)
static size_t
uint64_pack (uint64_t value, uint8_t *out)
{
uint32_t hi = value>>32;
uint32_t lo = value;
uint32_t hi = (uint32_t )(value>>32);
uint32_t lo = (uint32_t )value;
unsigned rv;
if (hi == 0)
return uint32_pack ((uint32_t)lo, out);
......@@ -592,15 +600,16 @@ sint64_pack (int64_t value, uint8_t *out)
/* Pack a 32-bit value, little-endian.
Used for fixed32, sfixed32, float) */
static inline size_t
fixed32_pack (uint32_t value, uint8_t *out)
fixed32_pack (uint32_t value, void *out)
{
#if IS_LITTLE_ENDIAN
memcpy (out, &value, 4);
#else
out[0] = value;
out[1] = value>>8;
out[2] = value>>16;
out[3] = value>>24;
uint8_t *buf = out;
buf[0] = value;
buf[1] = value>>8;
buf[2] = value>>16;
buf[3] = value>>24;
#endif
return 4;
}
......@@ -611,7 +620,7 @@ fixed32_pack (uint32_t value, uint8_t *out)
a 64-bit version would be appreciated, plus a way
to decide to use 64-bit math where convenient. */
static inline size_t
fixed64_pack (uint64_t value, uint8_t *out)
fixed64_pack (uint64_t value, void *out)
{
#if IS_LITTLE_ENDIAN
memcpy (out, &value, 8);
......@@ -1620,9 +1629,10 @@ static uint64_t
parse_uint64 (unsigned len, const uint8_t *data)
{
unsigned shift, i;
uint64_t rv;
if (len < 5)
return parse_uint32 (len, data);
uint64_t rv = ((data[0] & 0x7f))
rv = ((data[0] & 0x7f))
| ((data[1] & 0x7f)<<7)
| ((data[2] & 0x7f)<<14)
| ((data[3] & 0x7f)<<21);
......@@ -2146,7 +2156,7 @@ protobuf_c_message_unpack (const ProtobufCMessageDescriptor *desc,
goto error_cleanup_during_scan;
}
/* XXX: consider optimizing for field[1].id == tag, if field[1] exists! */
if (last_field->id != tag)
if (last_field == NULL || last_field->id != tag)
{
/* lookup field */
int field_index = int_range_lookup (desc->n_field_ranges,
......
......@@ -44,9 +44,17 @@
# if defined(_MSC_VER)
/* On windows, in ms visual studio, define the types ourselves */
# define int32_t signed __int32
# define INT32_MIN _I32_MIN
# define INT32_MAX _I32_MAX
# define uint32_t unsigned __int32
# define UINT32_MIN _UI32_MIN
# define UINT32_MAX _UI32_MAX
# define int64_t signed __int64
# define INT64_MIN _I64_MIN
# define INT64_MAX _I64_MAX
# define uint64_t unsigned __int64
# define UINT64_MIN _UI64_MIN
# define UINT64_MAX _UI64_MAX
# define uint8_t unsigned char
# else
/* Use the system inttypes.h */
......@@ -54,6 +62,18 @@
# endif
#endif
#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
# ifdef PROTOBUF_C_EXPORT
# define PROTOBUF_C_API __declspec(dllexport)
# else
# define PROTOBUF_C_API __declspec(dllimport)
#endif
#else
# define PROTOBUF_C_API
#endif
PROTOBUF_C_BEGIN_DECLS
typedef enum
......@@ -85,6 +105,7 @@ typedef enum
PROTOBUF_C_TYPE_MESSAGE,
} ProtobufCType;
typedef int protobuf_c_boolean;
#define PROTOBUF_C_OFFSETOF(struct, member) offsetof(struct, member)
......@@ -118,18 +139,18 @@ struct _ProtobufCAllocator
*
* NOTE: you may modify this allocator.
*/
extern ProtobufCAllocator protobuf_c_default_allocator; /* settable */
extern PROTOBUF_C_API ProtobufCAllocator protobuf_c_default_allocator; /* settable */
/* This is the system allocator, meaning it uses malloc() and free().
*
* NOTE: please do NOT modify this allocator.
*/
extern ProtobufCAllocator protobuf_c_system_allocator; /* use malloc, free etc */
extern PROTOBUF_C_API ProtobufCAllocator protobuf_c_system_allocator; /* use malloc, free etc */
/* This is the function that our default allocators call when they
run out-of-memory. The default behavior of this function is to
terminate your program. */
extern void (*protobuf_c_out_of_memory) (void);
extern PROTOBUF_C_API void (*protobuf_c_out_of_memory) (void);
/* --- append-only data buffer --- */
typedef struct _ProtobufCBuffer ProtobufCBuffer;
......@@ -317,23 +338,23 @@ struct _ProtobufCMessage
(2) Provide a virtual buffer (a ProtobufCBuffer) to
accept data as we scan through it.
*/
size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message);
size_t protobuf_c_message_pack (const ProtobufCMessage *message,
PROTOBUF_C_API size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message);
PROTOBUF_C_API size_t protobuf_c_message_pack (const ProtobufCMessage *message,
uint8_t *out);
size_t protobuf_c_message_pack_to_buffer (const ProtobufCMessage *message,
PROTOBUF_C_API size_t protobuf_c_message_pack_to_buffer (const ProtobufCMessage *message,
ProtobufCBuffer *buffer);
ProtobufCMessage *
PROTOBUF_C_API ProtobufCMessage *
protobuf_c_message_unpack (const ProtobufCMessageDescriptor *,
ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void protobuf_c_message_free_unpacked (ProtobufCMessage *message,
PROTOBUF_C_API void protobuf_c_message_free_unpacked (ProtobufCMessage *message,
ProtobufCAllocator *allocator);
/* WARNING: 'message' must be a block of memory
of size descriptor->sizeof_message. */
void protobuf_c_message_init (const ProtobufCMessageDescriptor *,
PROTOBUF_C_API void protobuf_c_message_init (const ProtobufCMessageDescriptor *,
void *message);
/* --- services --- */
......@@ -378,23 +399,23 @@ void protobuf_c_service_destroy (ProtobufCService *);
/* --- querying the descriptors --- */
const ProtobufCEnumValue *
PROTOBUF_C_API const ProtobufCEnumValue *
protobuf_c_enum_descriptor_get_value_by_name
(const ProtobufCEnumDescriptor *desc,
const char *name);
const ProtobufCEnumValue *
PROTOBUF_C_API const ProtobufCEnumValue *
protobuf_c_enum_descriptor_get_value
(const ProtobufCEnumDescriptor *desc,
int value);
const ProtobufCFieldDescriptor *
PROTOBUF_C_API const ProtobufCFieldDescriptor *
protobuf_c_message_descriptor_get_field_by_name
(const ProtobufCMessageDescriptor *desc,
const char *name);
const ProtobufCFieldDescriptor *
PROTOBUF_C_API const ProtobufCFieldDescriptor *
protobuf_c_message_descriptor_get_field
(const ProtobufCMessageDescriptor *desc,
unsigned value);
const ProtobufCMethodDescriptor *
PROTOBUF_C_API const ProtobufCMethodDescriptor *
protobuf_c_service_descriptor_get_method_by_name
(const ProtobufCServiceDescriptor *desc,
const char *name);
......@@ -436,6 +457,26 @@ struct _ProtobufCBufferSimple
do { if ((simp_buf)->must_free_data) \
protobuf_c_default_allocator.free (&protobuf_c_default_allocator.allocator_data, (simp_buf)->data); } while (0)
typedef enum
{
PROTOBUF_C_CTYPE_INT32,
PROTOBUF_C_CTYPE_UINT32,
PROTOBUF_C_CTYPE_INT64,
PROTOBUF_C_CTYPE_UINT64,
PROTOBUF_C_CTYPE_FLOAT,
PROTOBUF_C_CTYPE_DOUBLE,
PROTOBUF_C_CTYPE_BOOL,
PROTOBUF_C_CTYPE_ENUM,
PROTOBUF_C_CTYPE_STRING,
PROTOBUF_C_CTYPE_BYTES,
PROTOBUF_C_CTYPE_MESSAGE,
} ProtobufCCType;
extern ProtobufCCType protobuf_c_type_to_ctype (ProtobufCType type);
#define protobuf_c_type_to_ctype(type) \
((ProtobufCCType)(protobuf_c_type_to_ctype_array[(type)]))
/* ====== private ====== */
#include "protobuf-c-private.h"
......
......@@ -44,7 +44,7 @@ void ParseOptions(const string& text, vector<pair<string, string> >* output) {
vector<string> parts;
SplitStringUsing(text, ",", &parts);
for (int i = 0; i < parts.size(); i++) {
for (unsigned i = 0; i < parts.size(); i++) {
string::size_type equals_pos = parts[i].find_first_of('=');
pair<string, string> value;
if (equals_pos == string::npos) {
......@@ -90,7 +90,7 @@ bool CGenerator::Generate(const FileDescriptor* file,
// __declspec(dllimport) depending on what is being compiled.
string dllexport_decl;
for (int i = 0; i < options.size(); i++) {
for (unsigned i = 0; i < options.size(); i++) {
if (options[i].first == "dllexport_decl") {
dllexport_decl = options[i].second;
} else {
......
......@@ -33,6 +33,19 @@ namespace protobuf {
namespace compiler {
namespace c {
#if defined(_MSC_VER)
// FIXME: In the case where the generated string is longer than the buffer,
// _snprint() returns a negative value, where snprintf() returns the number
// of characters that *would* have been stored, had there been room.
// That is fundamental, as it allows snprintf() to be used to find the size
// necessary for the buffer, simply by calling it with the size of the buffer
// passed in as zero.
// Note: at the present moment return value isn't used in the code.
#define snprintf _snprintf
#pragma warning(disable:4800)
#pragma warning(disable:4996)
#endif
string DotsToUnderscores(const string& name) {
return StringReplace(name, ".", "_", true);
}
......@@ -41,11 +54,6 @@ string DotsToColons(const string& name) {
return StringReplace(name, ".", "::", true);
}
string SimpleItoa(int i) {
char buf[100];
snprintf(buf,sizeof(buf),"%d",i);
return buf;
}
string SimpleFtoa(float f) {
char buf[100];
snprintf(buf,sizeof(buf),"%.*g", FLT_DIG, f);
......@@ -132,7 +140,7 @@ string FullNameToLower(const string &full_name) {
vector<string> pieces;
SplitStringUsing(full_name, ".", &pieces);
string rv = "";
for (int i = 0; i < pieces.size(); i++) {
for (unsigned i = 0; i < pieces.size(); i++) {
if (pieces[i] == "") continue;
if (rv != "") rv += "__";
rv += CamelToLower(pieces[i]);
......@@ -143,7 +151,7 @@ string FullNameToUpper(const string &full_name) {
vector<string> pieces;
SplitStringUsing(full_name, ".", &pieces);
string rv = "";
for (int i = 0; i < pieces.size(); i++) {
for (unsigned i = 0; i < pieces.size(); i++) {
if (pieces[i] == "") continue;
if (rv != "") rv += "__";
rv += CamelToUpper(pieces[i]);
......@@ -154,7 +162,7 @@ string FullNameToC(const string &full_name) {
vector<string> pieces;
SplitStringUsing(full_name, ".", &pieces);
string rv = "";
for (int i = 0; i < pieces.size(); i++) {
for (unsigned i = 0; i < pieces.size(); i++) {
if (pieces[i] == "") continue;
if (rv != "") rv += "__";
rv += ToCamel(pieces[i]);
......@@ -256,7 +264,7 @@ string StripProto(const string& filename) {
// Convert a file name into a valid identifier.
string FilenameIdentifier(const string& filename) {
string result;
for (int i = 0; i < filename.size(); i++) {
for (unsigned i = 0; i < filename.size(); i++) {
if (isalnum(filename[i])) {
result.push_back(filename[i]);
} else {
......
......@@ -25,6 +25,7 @@
#include <string>
#include <vector>
#include <sstream>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
......@@ -46,7 +47,12 @@ string ClassName(const Descriptor* descriptor, bool qualified);
string ClassName(const EnumDescriptor* enum_descriptor, bool qualified);
// --- Borrowed from stubs. ---
string SimpleItoa(int i);
template <typename T> string SimpleItoa(T n) {
std::stringstream stream;
stream << n;
return stream.str();
}
string SimpleFtoa(float f);
string SimpleDtoa(double f);
void SplitStringUsing(const string &str, const char *delim, std::vector<string> *out);
......
......@@ -366,6 +366,7 @@ GenerateMessageDescriptor(io::Printer* printer) {
}
}
if ( descriptor_->field_count() ) {
printer->Print(vars,
"static const ProtobufCFieldDescriptor $lcclassname$__field_descriptors[$n_fields$] =\n"
"{\n");
......@@ -411,6 +412,17 @@ GenerateMessageDescriptor(io::Printer* printer) {
delete [] sorted_fields;
vars["n_ranges"] = SimpleItoa(n_ranges);
} else {
/* MS compiler can't handle arrays with zero size and empty
* initialization list. Furthermore it is an extension of GCC only but
* not a standard. */
vars["n_ranges"] = "0";
printer->Print(vars,
"#define $lcclassname$__field_descriptors NULL\n"
"#define $lcclassname$__field_indices_by_name NULL\n"
"#define $lcclassname$__number_ranges NULL\n");
}
printer->Print(vars,
"const ProtobufCMessageDescriptor $lcclassname$__descriptor =\n"
"{\n"
......
if ( BUILD_PROTOBUF_C_COMPILER AND BUILD_PROTOBUF_C_STATIC_LIB )
function(BUILD_C_TEST target_name proto_name)
set(TMP_OUTPUT_DIR ${CMAKE_BINARY_DIR}/${target_name}/generated-code)
file(MAKE_DIRECTORY ${TMP_OUTPUT_DIR})
set(GENERATED_SOURCES ${TMP_OUTPUT_DIR}/${proto_name}.pb-c.h
${TMP_OUTPUT_DIR}/${proto_name}.pb-c.c)
add_executable(${target_name} ${target_name}.c ${GENERATED_SOURCES}
${ARGN})
set_target_properties(${target_name} PROPERTIES COMPILE_FLAGS
"-I${CMAKE_BINARY_DIR}/${target_name}")
target_link_libraries(${target_name} protobuf-c-static)
add_custom_command(OUTPUT ${GENERATED_SOURCES}
COMMAND protoc-c -I${CMAKE_CURRENT_SOURCE_DIR}
--c_out=${TMP_OUTPUT_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/${proto_name}.proto
DEPENDS protoc-c ${CMAKE_CURRENT_SOURCE_DIR}/${proto_name}.proto
COMMENT "Running C protocol buffer compiler on ${proto_name}"
VERBATIM)
endfunction()
function(BUILD_CPP_TEST target_name proto_name)
set(TMP_OUTPUT_DIR ${CMAKE_BINARY_DIR}/${target_name}/generated-code)
file(MAKE_DIRECTORY ${TMP_OUTPUT_DIR})
set(GENERATED_SOURCES ${TMP_OUTPUT_DIR}/${proto_name}.pb.h
${TMP_OUTPUT_DIR}/${proto_name}.pb.cc)
add_executable(${target_name} ${target_name}.cc ${GENERATED_SOURCES}
${ARGN})
set_target_properties(${target_name} PROPERTIES COMPILE_FLAGS
"-I${CMAKE_BINARY_DIR}/${target_name} -I${PROTOBUF_INCLUDE_DIRS}")
target_link_libraries(${target_name} ${PROTOBUF_LIBRARIES})
if ( UNIX )
find_package(Threads REQUIRED)
target_link_libraries(${target_name} ${CMAKE_THREAD_LIBS_INIT})
endif()
add_custom_command(OUTPUT ${GENERATED_SOURCES}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} -I${CMAKE_CURRENT_SOURCE_DIR}
--cpp_out=${TMP_OUTPUT_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/${proto_name}.proto
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${proto_name}.proto
COMMENT "Running C++ protocol buffer compiler on ${proto_name}"
VERBATIM)
endfunction()
build_cpp_test(cxx-generate-packed-data test-full)
set(TMP_OUTPUT_DIR ${CMAKE_BINARY_DIR}/cxx-generate-packed-data)
file(MAKE_DIRECTORY ${TMP_OUTPUT_DIR}/generated-code)
set(GENERATED_INC_FILE ${TMP_OUTPUT_DIR}/generated-code/test-full-cxx-output.inc)
add_custom_command(OUTPUT ${GENERATED_INC_FILE}
COMMAND cxx-generate-packed-data > ${GENERATED_INC_FILE}
DEPENDS cxx-generate-packed-data)
build_c_test(test-generated-code2 test-full ${GENERATED_INC_FILE})
get_target_property(EXTRA_COMPILE_FLAGS test-generated-code2
COMPILE_FLAGS)
set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -I${TMP_OUTPUT_DIR}")
set_target_properties(test-generated-code2 PROPERTIES COMPILE_FLAGS
${EXTRA_COMPILE_FLAGS})
add_test(NAME test-generated-code2 COMMAND test-generated-code2)
build_c_test(test-generated-code test)
add_test(NAME test-generated-code COMMAND test-generated-code)
if ( UNIX )
build_c_test(test-rpc test)
build_c_test(example-client test)
build_c_test(example-server test)
add_test(NAME test-rpc COMMAND test-rpc)
endif()
endif()
......@@ -3,7 +3,26 @@
#define __STDC_LIMIT_MACROS
#include "generated-code/test-full.pb.h"
#include <inttypes.h>
#include <limits.h>
# if defined(_MSC_VER)
/* On windows, in ms visual studio, define the types ourselves */
# define int32_t signed __int32
# define INT32_MIN _I32_MIN
# define INT32_MAX _I32_MAX
# define uint32_t unsigned __int32
# define UINT32_MIN _UI32_MIN
# define UINT32_MAX _UI32_MAX
# define int64_t signed __int64
# define INT64_MIN _I64_MIN
# define INT64_MAX _I64_MAX
# define uint64_t unsigned __int64
# define UINT64_MIN _UI64_MIN
# define UINT64_MAX _UI64_MAX
# define uint8_t unsigned char
# else
/* Use the system inttypes.h */
# include <inttypes.h>
# endif
#include <stdio.h>
using namespace foo;
......
......@@ -11,7 +11,7 @@ enum TestEnumSmall {
// these number are specifically chosen to test the
// boundaries of when an enum requires a certain number of bytes.
// e.g. 16383 requires 3 bytes; 16383 requires 4.
// e.g. 16383 requires 3 bytes; 16384 requires 4.
enum TestEnum {
VALUE0 = 0;
VALUE1 = 1;
......
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
......@@ -1434,33 +1433,32 @@ static void test_free (void *allocator_data, void *data)
}
static ProtobufCAllocator test_allocator = {
.alloc = test_alloc,
.free = test_free,
.allocator_data = &test_allocator_data,
test_alloc, test_free, 0, 0, &test_allocator_data
};
#define SETUP_TEST_ALLOC_BUFFER(pbuf, len) \
uint8_t bytes[] = "some bytes", *pbuf; \
size_t len, _len2; \
Foo__DefaultRequiredValues _req = FOO__DEFAULT_REQUIRED_VALUES__INIT; \
Foo__AllocValues _mess = FOO__ALLOC_VALUES__INIT; \
_mess.a_string = "some string"; \
_mess.r_string = repeated_strings_2; \
_mess.n_r_string = sizeof(repeated_strings_2) / sizeof(*repeated_strings_2); \
uint8_t bytes[] = "some bytes"; \
_mess.a_bytes.len = sizeof(bytes); \
_mess.a_bytes.data = bytes; \
_mess.a_mess = &_req; \
size_t len = foo__alloc_values__get_packed_size (&_mess); \
uint8_t *pbuf = malloc (len); \
len = foo__alloc_values__get_packed_size (&_mess); \
pbuf = malloc (len); \
assert (pbuf); \
size_t _len2 = foo__alloc_values__pack (&_mess, pbuf); \
_len2 = foo__alloc_values__pack (&_mess, pbuf); \
assert (len == _len2);
static void
test_alloc_graceful_cleanup (uint8_t *packed, size_t len, int good_allocs)
{
Foo__AllocValues *mess;
test_allocator_data.alloc_count = 0;
test_allocator_data.allocs_left = good_allocs;
Foo__AllocValues *mess;
mess = foo__alloc_values__unpack (&test_allocator, len, packed);
assert (test_allocator_data.allocs_left < 0 ? !mess : !!mess);
if (mess)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment