aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-10 12:39:39 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-10 12:39:39 +0300
commite681dd0d75a4b6a7974cc898477f3a138f7872c2 (patch)
treed6ef45c88d9e69bae390b2d88738a7d55b161c40
parent4dccf28ba9c212b232147fd3823554d04b30c392 (diff)
Add an example pb_syshdr.h file for platforms without C99.
This allows building the tests easily on Visual C++ in C mode. Also add checks to pb.h that the defined integer types are of the proper sizes. This may prevent some difficult to debug problems later..
-rw-r--r--compat/pb_syshdr.h71
-rw-r--r--pb.h11
-rw-r--r--tests/SConstruct7
3 files changed, 89 insertions, 0 deletions
diff --git a/compat/pb_syshdr.h b/compat/pb_syshdr.h
new file mode 100644
index 00000000..c170c8de
--- /dev/null
+++ b/compat/pb_syshdr.h
@@ -0,0 +1,71 @@
+/* This is an example of a header file for platforms/compilers that do
+ * not come with stdint.h/stddef.h/stdbool.h/string.h. To use it, define
+ * PB_SYSTEM_HEADER as "pb_syshdr.h", including the quotes, and add the
+ * compat folder to your include path.
+ *
+ * It is very likely that you will need to customize this file to suit
+ * your platform. For any compiler that supports C99, this file should
+ * not be necessary.
+ */
+
+#ifndef _PB_SYSHDR_H_
+#define _PB_SYSHDR_H_
+
+/* stdint.h subset */
+/* You will need to modify these to match the word size of your platform. */
+typedef signed char int8_t;
+typedef unsigned char uint8_t;
+typedef signed short int16_t;
+typedef unsigned short uint16_t;
+typedef signed int int32_t;
+typedef unsigned int uint32_t;
+typedef signed long long int64_t;
+typedef unsigned long long uint64_t;
+
+/* stddef.h subset */
+typedef uint32_t size_t;
+#define offsetof(st, m) ((size_t)(&((st *)0)->m))
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+/* stdbool.h subset */
+typedef int bool;
+#define false 0
+#define true 1
+
+/* string.h subset */
+/* Implementations are from the Public Domain C Library (PDCLib). */
+static size_t strlen( const char * s )
+{
+ size_t rc = 0;
+ while ( s[rc] )
+ {
+ ++rc;
+ }
+ return rc;
+}
+
+static void * memcpy( void *s1, const void *s2, size_t n )
+{
+ char * dest = (char *) s1;
+ const char * src = (const char *) s2;
+ while ( n-- )
+ {
+ *dest++ = *src++;
+ }
+ return s1;
+}
+
+static void * memset( void * s, int c, size_t n )
+{
+ unsigned char * p = (unsigned char *) s;
+ while ( n-- )
+ {
+ *p++ = (unsigned char) c;
+ }
+ return s;
+}
+
+#endif
diff --git a/pb.h b/pb.h
index e3e68ce5..c63b3514 100644
--- a/pb.h
+++ b/pb.h
@@ -215,6 +215,17 @@ struct _pb_field_t {
} pb_packed;
PB_PACKED_STRUCT_END
+/* Make sure that the standard integer types are of the expected sizes.
+ * All kinds of things may break otherwise.. atleast all fixed* types. */
+STATIC_ASSERT(sizeof(int8_t) == 1, INT8_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(uint8_t) == 1, UINT8_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(int16_t) == 2, INT16_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(uint16_t) == 2, UINT16_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(int32_t) == 4, INT32_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(uint32_t) == 4, UINT32_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(int64_t) == 8, INT64_T_WRONG_SIZE)
+STATIC_ASSERT(sizeof(uint64_t) == 8, UINT64_T_WRONG_SIZE)
+
/* This structure is used for 'bytes' arrays.
* It has the number of bytes in the beginning, and after that an array.
* Note that actual structs used will have a different length of bytes array.
diff --git a/tests/SConstruct b/tests/SConstruct
index b2d524cc..a690aff0 100644
--- a/tests/SConstruct
+++ b/tests/SConstruct
@@ -12,6 +12,13 @@ env.Append(PROTOCPATH = ['#../generator', '/usr/include', '.'])
# Define the include path to find nanopb.proto
env.Append(PROTOCPATH = ['#../generator', '/usr/include', '.'])
+# If the platform doesn't support C99, use our own header file instead.
+conf = Configure(env)
+if not conf.CheckCHeader('stdbool.h'):
+ conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
+ conf.env.Append(CPPPATH = "#../compat")
+env = conf.Finish()
+
# Now include the SConscript files from all subdirectories
SConscript(Glob('*/SConscript'), exports = 'env')