Browse Source

improve amalgamation

Julius Pfrommer 9 years ago
parent
commit
df4753b00d

+ 3 - 3
CMakeLists.txt

@@ -211,16 +211,16 @@ add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.c
                PRE_BUILD
 
                COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${GIT_COMMIT_ID} ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
-                              ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
+                                            ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
 
-               DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources})
+               DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources})
 
 if(ENABLE_AMALGAMATION)
+    add_definitions(-DUA_AMALGAMATE)
     add_custom_target(amalgamation ALL DEPENDS ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c)
     add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c ${PROJECT_BINARY_DIR}/open62541.h)
     include_directories(${PROJECT_BINARY_DIR})
 else()
-    add_definitions(-DNOT_AMALGATED)
     add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
     include_directories(${PROJECT_SOURCE_DIR}/include)
     include_directories(${PROJECT_SOURCE_DIR}/deps)

+ 2 - 6
examples/logger_stdout.h

@@ -6,12 +6,8 @@
 #ifndef LOGGER_STDOUT_H_
 #define LOGGER_STDOUT_H_
 
-#ifdef NOT_AMALGATED
-    #include "ua_types.h"
-    #include "ua_log.h"
-#else
-    #include "open62541.h"
-#endif
+#include "ua_types.h"
+#include "ua_log.h"
 
 /** Initialises the logger for the current thread. */
 UA_EXPORT UA_Logger Logger_Stdout_new(void);

+ 8 - 12
examples/networklayer_tcp.c

@@ -3,11 +3,11 @@
  * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  */
 
-#ifdef NOT_AMALGATED
-# define _XOPEN_SOURCE 500 //some users need this for some reason
-# include <stdlib.h> // malloc, free
-# include <string.h> // memset
-#endif
+#include "networklayer_tcp.h"
+
+#include <stdlib.h> // malloc, free
+#include <string.h> // memset
+#include <errno.h>
 
 #ifdef _WIN32
 # include <malloc.h>
@@ -26,14 +26,10 @@
 # define CLOSESOCKET(S) close(S)
 #endif
 
-#include "networklayer_tcp.h" // UA_MULTITHREADING is defined in here
 #ifdef UA_MULTITHREADING
 # include <urcu/uatomic.h>
 #endif
 
-/* with a space, so the include is not removed during amalgamation */
-# include <errno.h>
-
 /****************************/
 /* Generic Socket Functions */
 /****************************/
@@ -67,16 +63,16 @@ static UA_StatusCode socket_recv(UA_Connection *connection, UA_ByteString *respo
         return UA_STATUSCODE_BADOUTOFMEMORY;
     struct timeval tmptv = {0, timeout * 1000};
     if(0 != setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tmptv, sizeof(struct timeval))){
-		UA_free(response->data);
+		free(response->data);
         return UA_STATUSCODE_BADINTERNALERROR;
     }
     int ret = recv(connection->sockfd, (char*)response->data, connection->localConf.recvBufferSize, 0);
 	if(ret == 0) {
-		UA_free(response->data);
+		free(response->data);
         UA_ByteString_init(response);
 		return UA_STATUSCODE_GOOD; /* no response -> retry */
 	} else if(ret < 0) {
-        UA_free(response->data);
+        free(response->data);
         UA_ByteString_init(response);
 #ifdef _WIN32
 		if(WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) {

+ 1 - 6
examples/networklayer_tcp.h

@@ -10,17 +10,12 @@
 extern "C" {
 #endif
 
-#ifdef NOT_AMALGATED
 #include "ua_server.h"
 #include "ua_client.h"
-#else
-#include "open62541.h"
-#endif
 
 /** @brief Create the TCP networklayer and listen to the specified port */
 UA_EXPORT UA_ServerNetworkLayer ServerNetworkLayerTCP_new(UA_ConnectionConfig conf, UA_UInt32 port);
-UA_EXPORT UA_Connection ClientNetworkLayerTCP_connect(UA_ConnectionConfig conf, char *endpointUrl,
-                                                      UA_Logger *logger);
+UA_EXPORT UA_Connection ClientNetworkLayerTCP_connect(UA_ConnectionConfig conf, char *endpointUrl, UA_Logger *logger);
 
 #ifdef __cplusplus
 } // extern "C"

+ 12 - 13
examples/server.c

@@ -3,32 +3,31 @@
  * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  */
 
+#ifdef UA_AMALGAMATE
+# include "open62541.h"
+#else
+# include <time.h>
+# include "ua_types.h"
+# include "ua_server.h"
+# include "logger_stdout.h"
+# include "networklayer_tcp.h"
+#endif
+
 #include <signal.h>
 #include <errno.h> // errno, EINTR
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #ifdef _MSC_VER
-    #include <io.h> //access
+# include <io.h> //access
 #else
-    #include <unistd.h> //access
+# include <unistd.h> //access
 #endif
 
-#define __USE_XOPEN2K
 #ifdef UA_MULTITHREADING
 # include <pthread.h>
 #endif
 
-#ifdef NOT_AMALGATED
-# include <time.h>
-# include "ua_types.h"
-# include "ua_server.h"
-# include "logger_stdout.h"
-# include "networklayer_tcp.h"
-#else
-# include "open62541.h"
-#endif
-
 /****************************/
 /* Server-related variables */
 /****************************/

+ 6 - 8
examples/server_simple.c

@@ -9,17 +9,15 @@
 #include <errno.h> // errno, EINTR
 #include <string.h>
 
-#ifdef NOT_AMALGATED
-    #include "ua_types.h"
-    #include "ua_server.h"
+#ifdef UA_AMALGAMATE
+# include "open62541.h"
 #else
-    #include "open62541.h"
+# include "ua_types.h"
+# include "ua_server.h"
+# include "logger_stdout.h"
+# include "networklayer_tcp.h"
 #endif
 
-// provided by the user, implementations available in the /examples folder
-#include "logger_stdout.h"
-#include "networklayer_tcp.h"
-
 UA_Boolean running = 1;
 UA_Logger logger;
 

+ 1 - 1
include/ua_client.h

@@ -5,7 +5,7 @@
 extern "C" {
 #endif
 
-#include "ua_util.h"
+#include "ua_config.h"
 #include "ua_types.h"
 #include "ua_connection.h"
 #include "ua_log.h"

+ 1 - 0
include/ua_server.h

@@ -20,6 +20,7 @@
 extern "C" {
 #endif
 
+#include "ua_config.h"
 #include "ua_types.h"
 #include "ua_types_generated.h"
 #include "ua_nodeids.h"

+ 4 - 8
src/ua_util.h

@@ -1,9 +1,7 @@
 #ifndef UA_UTIL_H_
 #define UA_UTIL_H_
 
-#ifndef UA_AMALGAMATE
-# include "ua_config.h"
-#endif
+#include "ua_config.h"
 
 /*********************/
 /* Memory Management */
@@ -19,9 +17,9 @@
 
 /* Visual Studio needs __restrict */
 #ifdef _MSC_VER
-    #define UA_RESTRICT __restrict
+# define UA_RESTRICT __restrict
 #else
-    #define UA_RESTRICT restrict
+# define UA_RESTRICT restrict
 #endif
 
 #define UA_NULL ((void *)0)
@@ -83,9 +81,7 @@
 /* External Dependencies */
 /*************************/
 
-#ifndef UA_AMALGAMATE
-# include "queue.h"
-#endif
+#include "queue.h"
 
 #ifdef UA_MULTITHREADING
 # define _LGPL_SOURCE

+ 18 - 37
tools/amalgamate.py

@@ -5,37 +5,24 @@ import os.path
 import io
 
 parser = argparse.ArgumentParser()
-parser.add_argument('version', help='version to include')
-parser.add_argument('outfile', help='outfile w/o extension')
+parser.add_argument('version', help='file version')
+parser.add_argument('outfile', help='outfile with extension .c/.h')
 parser.add_argument('inputs', nargs='*', action='store', help='input filenames')
 args = parser.parse_args()
 
 outname = args.outfile.split("/")[-1]
+is_c = False
+if outname[-2:] == ".c":
+    is_c = True
 pos = outname.find(".")
 if pos > 0:
     outname = outname[:pos]
-include_re = re.compile("^#include ([\"<].*[\">]).*$")
+include_re = re.compile("^#include (\".*\").*$")
 guard_re = re.compile("^#(?:(?:ifndef|define) [A-Z_]+_H_|endif /\* [A-Z_]+_H_ \*/)")
 includes = []
 
-is_c = False
-
 print ("Starting amalgamating file "+ args.outfile)
 
-for fname in args.inputs:
-    if("util.h" in fname):
-        is_c = True
-        continue
-    with io.open(fname, encoding="utf8") as infile:
-        print ("Integrating file '" + fname + "'...", end=""),
-        for line in infile:
-            res = include_re.match(line)
-            if res:
-                inc = res.group(1)
-                if not inc in includes and not inc[0] == '"':
-                    includes.append(inc)
-        print ("done."),
-
 file = io.open(args.outfile, 'w')
 file.write(u'''/* THIS IS A SINGLE-FILE DISTRIBUTION CONCATENATED FROM THE OPEN62541 SOURCES 
  * visit http://open62541.org/ for information about this software
@@ -68,31 +55,25 @@ extern "C" {
 if not is_c:
     for inc in includes:
         file.write(u"#include " + inc + "\n")
+    file.write(u'''#ifndef UA_AMALGAMATE
+# define UA_AMALGAMATE
+#endif\n\n''')
 else:
-    file.write(u"#define UA_AMALGAMATE\n")
     file.write(u'''#ifndef UA_DYNAMIC_LINKING
 # define UA_DYNAMIC_LINKING
 #endif\n\n''')
-    for fname in args.inputs:
-        if "ua_config.h" in fname or "ua_util.h" in fname:
-            with io.open(fname, encoding="utf8") as infile:
-                print ("Integrating file '" + fname + "'...", end=""),
-                for line in infile:
-                    file.write(line)
-                print ("done."),
     file.write(u"#include \"" + outname + ".h\"\n")
 
 for fname in args.inputs:
-    if not "util.h" in fname:
-        with io.open(fname, encoding="utf8") as infile:
-            file.write(u"/*********************************** amalgamated original file \"" + fname + u"\" ***********************************/\n")
-            print ("Integrating file '" + fname + "'...", end=""),
-            for line in infile:
-                inc_res = include_re.match(line)
-                guard_res = guard_re.match(line)
-                if not inc_res and not guard_res:
-                    file.write(line)
-            print ("done."),
+    with io.open(fname, encoding="utf8") as infile:
+        file.write(u"\n/*********************************** amalgamated original file \"" + fname + u"\" ***********************************/\n\n")
+        print ("Integrating file '" + fname + "'...", end=""),
+        for line in infile:
+            inc_res = include_re.match(line)
+            guard_res = guard_re.match(line)
+            if not inc_res and not guard_res:
+                file.write(line)
+        print ("done."),
 
 if not is_c:
     file.write(u'''