Skip to content

Commit 41219ca

Browse files
committed
* macros implementation
1 parent 46c0c49 commit 41219ca

File tree

11 files changed

+356
-3
lines changed

11 files changed

+356
-3
lines changed

Autark

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ set {
6060
log.c
6161
map.c
6262
node_basename.c
63+
node_call.c
6364
node_cc.c
6465
node_check.c
6566
node_configure.c
@@ -68,6 +69,7 @@ set {
6869
node_in_sources.c
6970
node_include.c
7071
node_join.c
72+
node_macro.c
7173
node_meta.c
7274
node_run.c
7375
node_script.c

amalgamator.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ cat ./node_error.c >> ${F}
9595
cat ./node_echo.c >> ${F}
9696
cat ./node_install.c >> ${F}
9797
cat ./node_find.c >> ${F}
98+
cat ./node_macro.c >> ${F}
99+
cat ./node_call.c >> ${F}
98100
cat ./autark_core.c >> ${F}
99101
cat ./main.c >> ${F}
100102

dist/autark.c

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define CONFIG_H
33

44
#define META_VERSION "0.9.0"
5-
#define META_REVISION "de3b58a"
5+
#define META_REVISION "46c0c49"
66

77
#endif
88
#define _AMALGAMATE_
@@ -945,6 +945,8 @@ int node_error_setup(struct node*);
945945
int node_echo_setup(struct node*);
946946
int node_install_setup(struct node*);
947947
int node_find_setup(struct node*);
948+
int node_macro_setup(struct node*);
949+
int node_call_setup(struct node*);
948950

949951
#endif
950952
#ifndef AUTARK_H
@@ -999,6 +1001,8 @@ void autark_build_prepare(const char *script_path);
9991001
#define NODE_TYPE_ERROR 0x100000U
10001002
#define NODE_TYPE_ECHO 0x200000U
10011003
#define NODE_TYPE_INSTALL 0x400000U
1004+
#define NODE_TYPE_MACRO 0x800000U
1005+
#define NODE_TYPE_CALL 0x1000000U
10021006

10031007
#define NODE_FLG_BOUND 0x01U
10041008
#define NODE_FLG_INIT 0x02U
@@ -1010,6 +1014,7 @@ void autark_build_prepare(const char *script_path);
10101014
#define NODE_FLG_IN_SRC 0x80U
10111015
#define NODE_FLG_NO_CWD 0x100U
10121016
#define NODE_FLG_NEGATE 0x200U
1017+
#define NODE_FLG_CALLED 0x400U
10131018

10141019
#define NODE_FLG_IN_ANY (NODE_FLG_IN_SRC | NODE_FLG_IN_CACHE | NODE_FLG_NO_CWD)
10151020

@@ -1099,6 +1104,8 @@ void node_reset(struct node *n);
10991104

11001105
const char* node_value(struct node *n);
11011106

1107+
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*));
1108+
11021109
void node_module_setup(struct node *n, unsigned flags);
11031110

11041111
void node_init(struct node *n);
@@ -6456,6 +6463,70 @@ int node_find_setup(struct node *n) {
64566463
return 0;
64576464
}
64586465
#ifndef _AMALGAMATE_
6466+
#include "script.h"
6467+
#include "alloc.h"
6468+
#include "utils.h"
6469+
#endif
6470+
6471+
#define MACRO_MAX_ARGS_NUM 64
6472+
6473+
struct _macro {
6474+
struct node* args[MACRO_MAX_ARGS_NUM];
6475+
};
6476+
6477+
static int _macro_args_visitor(struct node *n, int lvl, void *ctx) {
6478+
if (lvl < 0) {
6479+
return 0;
6480+
}
6481+
struct _macro *m = n->impl;
6482+
if (!(n->value[0] == '&' && n->value[1] == '\0' && n->child)) {
6483+
return 0;
6484+
}
6485+
int rc = 0;
6486+
int idx = utils_strtol(n->child->value, 10, &rc);
6487+
if (rc || idx < 1 || idx > MACRO_MAX_ARGS_NUM) {
6488+
node_fatal(0, n, "Invalid macro argument index");
6489+
return 0;
6490+
}
6491+
idx--;
6492+
m->args[idx] = n;
6493+
return 0;
6494+
}
6495+
6496+
static void _macro_args_init(struct node *n) {
6497+
akcheck(node_visit(n, 1, 0, _macro_args_visitor));
6498+
}
6499+
6500+
static void _macro_init(struct node *n) {
6501+
struct unit *unit = unit_peek();
6502+
const char *key = node_value(n->child);
6503+
if (!key) {
6504+
node_warn(n, "No name specified for 'macro' directive");
6505+
return;
6506+
}
6507+
struct _macro *m = xcalloc(1, sizeof(*m));
6508+
_macro_args_init(n);
6509+
unit_env_set_node(unit, key, n);
6510+
}
6511+
6512+
static void _macro_dispose(struct node *n) {
6513+
struct _macro *m = n->impl;
6514+
free(m);
6515+
}
6516+
6517+
int node_macro_setup(struct node *n) {
6518+
n->flags |= NODE_FLG_NO_CWD;
6519+
n->init = _macro_init;
6520+
return 0;
6521+
}
6522+
#ifndef _AMALGAMATE_
6523+
#include "script.h"
6524+
#endif
6525+
6526+
int node_call_setup(struct node *n) {
6527+
return 0;
6528+
}
6529+
#ifndef _AMALGAMATE_
64596530

64606531
#ifndef META_VERSION
64616532
#define META_VERSION "dev"
@@ -8143,6 +8214,10 @@ static unsigned _rule_type(const char *key, unsigned *flags) {
81438214
return NODE_TYPE_INSTALL;
81448215
} else if (strcmp(key, "library") == 0) {
81458216
return NODE_TYPE_FIND;
8217+
} else if (strcmp(key, "macro") == 0) {
8218+
return NODE_TYPE_MACRO;
8219+
} else if (strcmp(key, "call") == 0) {
8220+
return NODE_TYPE_CALL;
81468221
} else {
81478222
return NODE_TYPE_BAG;
81488223
}
@@ -8328,6 +8403,10 @@ static int _node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct
83288403
return visitor(n, -lvl, ctx);
83298404
}
83308405

8406+
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*)) {
8407+
return _node_visit(n, lvl, ctx, visitor);
8408+
}
8409+
83318410
static void _preprocess_script(struct value *v) {
83328411
const char *p = v->buf;
83338412
char *nv = xmalloc(v->len + 1), *w = nv;
@@ -8542,6 +8621,12 @@ static int _node_bind(struct node *n) {
85428621
case NODE_TYPE_FIND:
85438622
rc = node_find_setup(n);
85448623
break;
8624+
case NODE_TYPE_MACRO:
8625+
rc = node_macro_setup(n);
8626+
break;
8627+
case NODE_TYPE_CALL:
8628+
rc = node_call_setup(n);
8629+
break;
85458630
}
85468631

85478632
switch (n->type) {
@@ -8629,6 +8714,30 @@ static void _post_build_subnodes(struct node *n) {
86298714
}
86308715
}
86318716

8717+
static void _macro_call_init(struct node *n) {
8718+
}
8719+
8720+
static void _macros_init(struct sctx *s) {
8721+
for (int i = 0; i < s->nodes.num; ++i) {
8722+
struct node *n = NODE_AT(&s->nodes, i);
8723+
if (n->type == NODE_TYPE_MACRO) {
8724+
n->flags |= NODE_FLG_SETUP;
8725+
_node_context_push(n);
8726+
n->init(n);
8727+
_node_context_pop(n);
8728+
}
8729+
}
8730+
for (int i = 0; i < s->nodes.num; ++i) {
8731+
struct node *n = NODE_AT(&s->nodes, i);
8732+
if (n->type == NODE_TYPE_CALL && !(n->flags & NODE_FLG_CALLED)) {
8733+
n->flags |= NODE_FLG_CALLED;
8734+
_node_context_push(n);
8735+
_macro_call_init(n);
8736+
_node_context_pop(n);
8737+
}
8738+
}
8739+
}
8740+
86328741
void node_init(struct node *n) {
86338742
if (!node_is_init(n)) {
86348743
n->flags |= NODE_FLG_INIT;
@@ -8786,6 +8895,7 @@ int script_include(struct node *parent, const char *file, struct node **out) {
87868895

87878896
void script_build(struct sctx *s) {
87888897
akassert(s->root);
8898+
_macros_init(s);
87898899
node_init(s->root);
87908900
node_setup(s->root);
87918901
node_build(s->root);

dist/build.sh

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# https://github.com/Softmotions/autark
77

88
META_VERSION=0.9.0
9-
META_REVISION=de3b58a
9+
META_REVISION=46c0c49
1010
cd "$(cd "$(dirname "$0")"; pwd -P)"
1111

1212
prev_arg=""
@@ -62,7 +62,7 @@ cat <<'a292effa503b' > ${AUTARK_HOME}/autark.c
6262
#ifndef CONFIG_H
6363
#define CONFIG_H
6464
#define META_VERSION "0.9.0"
65-
#define META_REVISION "de3b58a"
65+
#define META_REVISION "46c0c49"
6666
#endif
6767
#define _AMALGAMATE_
6868
#define _XOPEN_SOURCE 700
@@ -752,6 +752,8 @@ int node_error_setup(struct node*);
752752
int node_echo_setup(struct node*);
753753
int node_install_setup(struct node*);
754754
int node_find_setup(struct node*);
755+
int node_macro_setup(struct node*);
756+
int node_call_setup(struct node*);
755757
#endif
756758
#ifndef AUTARK_H
757759
#define AUTARK_H
@@ -796,6 +798,8 @@ void autark_build_prepare(const char *script_path);
796798
#define NODE_TYPE_ERROR 0x100000U
797799
#define NODE_TYPE_ECHO 0x200000U
798800
#define NODE_TYPE_INSTALL 0x400000U
801+
#define NODE_TYPE_MACRO 0x800000U
802+
#define NODE_TYPE_CALL 0x1000000U
799803
#define NODE_FLG_BOUND 0x01U
800804
#define NODE_FLG_INIT 0x02U
801805
#define NODE_FLG_SETUP 0x04U
@@ -806,6 +810,7 @@ void autark_build_prepare(const char *script_path);
806810
#define NODE_FLG_IN_SRC 0x80U
807811
#define NODE_FLG_NO_CWD 0x100U
808812
#define NODE_FLG_NEGATE 0x200U
813+
#define NODE_FLG_CALLED 0x400U
809814
#define NODE_FLG_IN_ANY (NODE_FLG_IN_SRC | NODE_FLG_IN_CACHE | NODE_FLG_NO_CWD)
810815
#define node_is_init(n__) (((n__)->flags & NODE_FLG_INIT) != 0)
811816
#define node_is_setup(n__) (((n__)->flags & NODE_FLG_SETUP) != 0)
@@ -867,6 +872,7 @@ void node_product_add(struct node*, const char *prod, char pathbuf[PATH_MAX]);
867872
void node_product_add_raw(struct node*, const char *prod);
868873
void node_reset(struct node *n);
869874
const char* node_value(struct node *n);
875+
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*));
870876
void node_module_setup(struct node *n, unsigned flags);
871877
void node_init(struct node *n);
872878
void node_setup(struct node *n);
@@ -5628,6 +5634,62 @@ int node_find_setup(struct node *n) {
56285634
return 0;
56295635
}
56305636
#ifndef _AMALGAMATE_
5637+
#include "script.h"
5638+
#include "alloc.h"
5639+
#include "utils.h"
5640+
#endif
5641+
#define MACRO_MAX_ARGS_NUM 64
5642+
struct _macro {
5643+
struct node* args[MACRO_MAX_ARGS_NUM];
5644+
};
5645+
static int _macro_args_visitor(struct node *n, int lvl, void *ctx) {
5646+
if (lvl < 0) {
5647+
return 0;
5648+
}
5649+
struct _macro *m = n->impl;
5650+
if (!(n->value[0] == '&' && n->value[1] == '\0' && n->child)) {
5651+
return 0;
5652+
}
5653+
int rc = 0;
5654+
int idx = utils_strtol(n->child->value, 10, &rc);
5655+
if (rc || idx < 1 || idx > MACRO_MAX_ARGS_NUM) {
5656+
node_fatal(0, n, "Invalid macro argument index");
5657+
return 0;
5658+
}
5659+
idx--;
5660+
m->args[idx] = n;
5661+
return 0;
5662+
}
5663+
static void _macro_args_init(struct node *n) {
5664+
akcheck(node_visit(n, 1, 0, _macro_args_visitor));
5665+
}
5666+
static void _macro_init(struct node *n) {
5667+
struct unit *unit = unit_peek();
5668+
const char *key = node_value(n->child);
5669+
if (!key) {
5670+
node_warn(n, "No name specified for 'macro' directive");
5671+
return;
5672+
}
5673+
struct _macro *m = xcalloc(1, sizeof(*m));
5674+
_macro_args_init(n);
5675+
unit_env_set_node(unit, key, n);
5676+
}
5677+
static void _macro_dispose(struct node *n) {
5678+
struct _macro *m = n->impl;
5679+
free(m);
5680+
}
5681+
int node_macro_setup(struct node *n) {
5682+
n->flags |= NODE_FLG_NO_CWD;
5683+
n->init = _macro_init;
5684+
return 0;
5685+
}
5686+
#ifndef _AMALGAMATE_
5687+
#include "script.h"
5688+
#endif
5689+
int node_call_setup(struct node *n) {
5690+
return 0;
5691+
}
5692+
#ifndef _AMALGAMATE_
56315693
#ifndef META_VERSION
56325694
#define META_VERSION "dev"
56335695
#endif
@@ -7183,6 +7245,10 @@ static unsigned _rule_type(const char *key, unsigned *flags) {
71837245
return NODE_TYPE_INSTALL;
71847246
} else if (strcmp(key, "library") == 0) {
71857247
return NODE_TYPE_FIND;
7248+
} else if (strcmp(key, "macro") == 0) {
7249+
return NODE_TYPE_MACRO;
7250+
} else if (strcmp(key, "call") == 0) {
7251+
return NODE_TYPE_CALL;
71867252
} else {
71877253
return NODE_TYPE_BAG;
71887254
}
@@ -7353,6 +7419,9 @@ static int _node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct
73537419
}
73547420
return visitor(n, -lvl, ctx);
73557421
}
7422+
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*)) {
7423+
return _node_visit(n, lvl, ctx, visitor);
7424+
}
73567425
static void _preprocess_script(struct value *v) {
73577426
const char *p = v->buf;
73587427
char *nv = xmalloc(v->len + 1), *w = nv;
@@ -7551,6 +7620,12 @@ static int _node_bind(struct node *n) {
75517620
case NODE_TYPE_FIND:
75527621
rc = node_find_setup(n);
75537622
break;
7623+
case NODE_TYPE_MACRO:
7624+
rc = node_macro_setup(n);
7625+
break;
7626+
case NODE_TYPE_CALL:
7627+
rc = node_call_setup(n);
7628+
break;
75547629
}
75557630
switch (n->type) {
75567631
case NODE_TYPE_RUN:
@@ -7627,6 +7702,28 @@ static void _post_build_subnodes(struct node *n) {
76277702
node_post_build(nn);
76287703
}
76297704
}
7705+
static void _macro_call_init(struct node *n) {
7706+
}
7707+
static void _macros_init(struct sctx *s) {
7708+
for (int i = 0; i < s->nodes.num; ++i) {
7709+
struct node *n = NODE_AT(&s->nodes, i);
7710+
if (n->type == NODE_TYPE_MACRO) {
7711+
n->flags |= NODE_FLG_SETUP;
7712+
_node_context_push(n);
7713+
n->init(n);
7714+
_node_context_pop(n);
7715+
}
7716+
}
7717+
for (int i = 0; i < s->nodes.num; ++i) {
7718+
struct node *n = NODE_AT(&s->nodes, i);
7719+
if (n->type == NODE_TYPE_CALL && !(n->flags & NODE_FLG_CALLED)) {
7720+
n->flags |= NODE_FLG_CALLED;
7721+
_node_context_push(n);
7722+
_macro_call_init(n);
7723+
_node_context_pop(n);
7724+
}
7725+
}
7726+
}
76307727
void node_init(struct node *n) {
76317728
if (!node_is_init(n)) {
76327729
n->flags |= NODE_FLG_INIT;
@@ -7776,6 +7873,7 @@ int script_include(struct node *parent, const char *file, struct node **out) {
77767873
}
77777874
void script_build(struct sctx *s) {
77787875
akassert(s->root);
7876+
_macros_init(s);
77797877
node_init(s->root);
77807878
node_setup(s->root);
77817879
node_build(s->root);

0 commit comments

Comments
 (0)