hotplug2: compile in the worker module statically, saves >20k uncompressed
[openwrt.git] / package / hotplug2 / patches / 110-static_worker.patch
1 --- a/Makefile
2 +++ b/Makefile
3 @@ -4,7 +4,24 @@ SOFTWARE=hotplug2
4  VERSION=1.0-alpha
5  
6  BINS=hotplug2 hotplug2-modwrap
7 -SUBDIRS=parser rules workers
8 +SUBDIRS=parser rules
9 +
10 +hotplug2-objs := \
11 +       hotplug2.o netlink.o seqnum.o settings.o uevent.o xmemutils.o \
12 +       workers/loader.o parser/parser.o parser/buffer.o parser/token.o \
13 +       parser/token_queue.o parser/lexer.o rules/ruleset.o rules/rule.o \
14 +       rules/condition.o rules/expression.o rules/execution.o \
15 +       rules/command.o
16 +
17 +ifdef STATIC_WORKER
18 +  ifeq ($(wildcard workers/worker_$(STATIC_WORKER).c),)
19 +    $(error Worker source worker/worker_$(STATIC_WORKER).c not found)
20 +  endif
21 +  hotplug2-objs += action.o workers/worker_$(STATIC_WORKER).o
22 +else
23 +  SUBDIRS += workers
24 +endif
25 +
26  DESTDIR=
27  
28  
29 @@ -13,13 +30,15 @@ all: $(BINS)
30  install:
31         $(INSTALL_BIN) $(BINS) $(DESTDIR)/sbin/
32  
33 -
34 -hotplug2: hotplug2.o netlink.o seqnum.o settings.o uevent.o xmemutils.o \
35 -          workers/loader.o parser/parser.o parser/buffer.o parser/token.o \
36 -          parser/token_queue.o parser/lexer.o rules/ruleset.o rules/rule.o \
37 -          rules/condition.o rules/expression.o rules/execution.o \
38 -          rules/command.o 
39 +hotplug2: $(hotplug2-objs)
40  
41  coldplug2: coldplug2.o
42  
43  include common.mak
44 +
45 +ifdef STATIC_WORKER
46 +  CFLAGS += -DSTATIC_WORKER=1
47 +else
48 +  CFLAGS += $(FPIC)
49 +endif
50 +
51 --- a/common.mak
52 +++ b/common.mak
53 @@ -1,8 +1,11 @@
54  # vim:set sw=8 nosta:
55  
56 -CFLAGS=-Os -Wall -g -fPIC
57 +COPTS=-Os -Wall -g
58  LDFLAGS=-g -ldl
59  
60 +CFLAGS=$(COPTS)
61 +FPIC=-fPIC
62 +
63  INSTALL=install -c -m 644
64  INSTALL_BIN=install -c -m 755
65  
66 --- a/workers/loader.c
67 +++ b/workers/loader.c
68 @@ -1,5 +1,23 @@
69  #include "loader.h"
70  
71 +#ifdef STATIC_WORKER
72 +
73 +extern struct worker_module_t worker_module;
74 +static struct loader_ctx_t static_ctx = {
75 +       .module = &worker_module
76 +};
77 +
78 +struct loader_ctx_t *worker_load(const char *name)
79 +{
80 +       return &static_ctx;
81 +}
82 +
83 +void worker_free(struct loader_ctx_t *ctx)
84 +{
85 +}
86 +
87 +#else
88 +
89  struct loader_ctx_t *worker_load(const char *name) {
90         struct loader_ctx_t *ctx;
91  
92 @@ -12,7 +30,7 @@ struct loader_ctx_t *worker_load(const c
93                 return NULL;
94         }
95         
96 -       ctx->module = dlsym(ctx->dl_handle, "module");
97 +       ctx->module = dlsym(ctx->dl_handle, "worker_module");
98         if (ctx->module == NULL) {
99                 fprintf(stderr, "Loader error: %s\n", dlerror());
100                 worker_free(ctx);
101 @@ -31,3 +49,5 @@ void worker_free(struct loader_ctx_t *ct
102         
103         free(ctx);
104  }
105 +
106 +#endif
107 --- a/hotplug2.c
108 +++ b/hotplug2.c
109 @@ -261,17 +261,21 @@ int main(int argc, char *argv[]) {
110         }
111  
112         /* Load the worker. */
113 +#ifndef STATIC_WORKER
114         if (settings->worker_name == NULL) {
115                 fprintf(stderr, "Missing worker name.\n");
116                 settings_clear(settings);
117                 exit(1);
118         }
119 +#endif
120         settings->worker = worker_load(settings->worker_name);
121 +#ifndef STATIC_WORKER
122         if (settings->worker == NULL) {
123                 fprintf(stderr, "Unable to load worker: %s\n", settings->worker_name);
124                 settings_clear(settings);
125                 exit(1);
126         }
127 +#endif
128         
129         /* Prepare a netlink connection to the kernel. */
130         settings->netlink_socket = netlink_init();
131 --- a/workers/worker_example.c
132 +++ b/workers/worker_example.c
133 @@ -62,7 +62,7 @@ static int worker_example_process(void *
134         return 0;
135  }
136  
137 -struct worker_module_t module = {
138 +struct worker_module_t worker_module = {
139         "Hotplug2 example module",
140         worker_example_init,
141         worker_example_deinit,
142 --- a/workers/worker_fork.c
143 +++ b/workers/worker_fork.c
144 @@ -443,7 +443,7 @@ static int worker_fork_process(void *in_
145         return 0;
146  }
147  
148 -struct worker_module_t module = {
149 +struct worker_module_t worker_module = {
150         "Hotplug2 forking module",
151         worker_fork_init,
152         worker_fork_deinit,
153 --- a/workers/worker_single.c
154 +++ b/workers/worker_single.c
155 @@ -18,7 +18,7 @@ static int worker_single_process(void *s
156         return 0;
157  }
158  
159 -struct worker_module_t module = {
160 +struct worker_module_t worker_module = {
161         "Hotplug2 single process module",
162         worker_single_init,
163         worker_single_deinit,