applet: rework APK_DEFINE_APPLET to use constructor attribute

this allows the applet registration to work in a portable way, without having to
weird things with the linker.

ref #10794
cute-signatures
Ariadne Conill 2021-12-10 14:33:52 -06:00
parent 980e58efc6
commit 3fd120db5c
4 changed files with 3 additions and 17 deletions

View File

@ -409,7 +409,6 @@ int main(int argc, char **argv)
#ifdef TEST_MODE
apk_string_array_init(&test_repos);
#endif
apk_applet_register_builtin();
apk_argv = malloc(sizeof(char*[argc+2]));
memcpy(apk_argv, argv, sizeof(char*[argc]));

View File

@ -56,14 +56,10 @@ struct apk_applet {
extern const struct apk_option_group optgroup_global, optgroup_commit, optgroup_signing;
void apk_applet_register(struct apk_applet *);
void apk_applet_register_builtin(void);
struct apk_applet *apk_applet_find(const char *name);
void apk_applet_help(struct apk_applet *applet, struct apk_out *out);
typedef void (*apk_init_func_t)(void);
#define APK_DEFINE_APPLET(x) \
static void __register_##x(void) { apk_applet_register(&x); } \
static apk_init_func_t __regfunc_##x __attribute__((__section__("initapplets"))) __attribute((used)) = __register_##x;
__attribute__((constructor)) static void __register_##x(void) { apk_applet_register(&x); }
#endif

View File

@ -223,6 +223,7 @@ APK_ARRAY(apk_string_array, char *);
#define foreach_array_item(iter, array) \
for (iter = &(array)->item[0]; iter < &(array)->item[(array)->num]; iter++)
#define LIST_HEAD(name) struct list_head name = { &name, &name }
#define LIST_END (void *) 0xe01
#define LIST_POISON1 (void *) 0xdeadbeef
#define LIST_POISON2 (void *) 0xabbaabba

View File

@ -10,7 +10,7 @@
#include "apk_applet.h"
#include "apk_print.h"
static struct list_head apk_applet_list;
static LIST_HEAD(apk_applet_list);
#define apk_applet_foreach(iter) list_for_each_entry(iter, &apk_applet_list, node)
@ -20,16 +20,6 @@ void apk_applet_register(struct apk_applet *applet)
list_add_tail(&applet->node, &apk_applet_list);
}
void apk_applet_register_builtin(void)
{
extern apk_init_func_t __start_initapplets[], __stop_initapplets[];
apk_init_func_t *p;
list_init(&apk_applet_list);
for (p = __start_initapplets; p < __stop_initapplets; p++)
(*p)();
}
struct apk_applet *apk_applet_find(const char *name)
{
struct apk_applet *a;