summaryrefslogtreecommitdiffhomepage
path: root/src/internal/slibtool_mkvars_impl.c
blob: a18dbfee6ce0292d3a237b30aed9ec0eaeb065cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************/
/*  slibtool: a strong libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include "slibtool_mkvars_impl.h"
#include "slibtool_driver_impl.h"
#include "slibtool_errinfo_impl.h"
#include "slibtool_symlink_impl.h"
#include "slibtool_readlink_impl.h"
#include "slibtool_realpath_impl.h"
#include "slibtool_visibility_impl.h"

static int slbt_get_mkvars_var(
	const struct slbt_driver_ctx *  dctx,
	const struct slbt_txtfile_ctx * tctx,
	const char *                    var,
	const char                      space,
	char                            (*val)[PATH_MAX])
{
	const char **   pline;
	const char *    mark;
	const char *    match;
	ssize_t         len;
	int             cint;

	/* init */
	match = 0;
	len   = strlen(var);

	/* search for ^var= */
	for (pline=tctx->txtlinev; !match && *pline; pline++) {
		if (!strncmp(*pline,var,len)) {
			if (isspace((*pline)[len]) || ((*pline)[len] == '=')) {
				mark = &(*pline)[len];

				for (; isspace(cint = *mark); )
					mark++;

				if (mark[0] != '=')
					return SLBT_CUSTOM_ERROR(
						dctx,
						SLBT_ERR_MKVARS_PARSE);

				mark++;

				for (; isspace(cint = *mark); )
					mark++;

				match = mark;
			}
		}
	}

	/* not found? */
	if (!match) {
		(*val)[0] = '\0';
		return 0;
	}

	/* validate */
	for (mark=match; *mark; mark++) {
		if ((*mark >= 'a') && (*mark <= 'z'))
			(void)0;

		else if ((*mark >= 'A') && (*mark <= 'Z'))
			(void)0;

		else if ((*mark >= '0') && (*mark <= '9'))
			(void)0;

		else if ((*mark == '+') || (*mark == '-'))
			(void)0;

		else if ((*mark == '/') || (*mark == '@'))
			(void)0;

		else if ((*mark == '.') || (*mark == '_'))
			(void)0;

		else if ((*mark == ':') || (*mark == space))
			(void)0;

		else
			return SLBT_CUSTOM_ERROR(
				dctx,
				SLBT_ERR_MKVARS_PARSE);
	}

	/* all done */
	strcpy(*val,match);

	return 0;
}

slbt_hidden int slbt_get_mkvars_flags(
	struct slbt_driver_ctx *	dctx,
	const char *			mkvars,
	uint64_t *			flags)
{
	struct slbt_driver_ctx_impl *   ctx;
	struct slbt_txtfile_ctx *       confctx;
	char *                          dash;
	uint64_t			optshared;
	uint64_t			optstatic;
	char                            val[PATH_MAX];

	/* driver context (ar, ranlib, cc) */
	ctx = slbt_get_driver_ictx(dctx);

	/* cache the makefile in library friendly form) */
	if (slbt_lib_get_txtfile_ctx(dctx,mkvars,&ctx->mkvarsctx) < 0)
		return SLBT_NESTED_ERROR(dctx);

	confctx = ctx->mkvarsctx;

	/* scan */
	optshared = 0;
	optstatic = 0;

	/* slibtool */
	if (slbt_get_mkvars_var(dctx,confctx,"SLIBTOOL",0,&val) < 0)
		return SLBT_NESTED_ERROR(dctx);

	if ((dash = strrchr(val,'-'))) {
		if (!strcmp(dash,"-shared")) {
			optshared = SLBT_DRIVER_SHARED;
			optstatic = SLBT_DRIVER_DISABLE_STATIC;

		} else if (!strcmp(dash,"-static")) {
			optshared = SLBT_DRIVER_DISABLE_SHARED;
			optstatic = SLBT_DRIVER_STATIC;
		} else {
			return SLBT_CUSTOM_ERROR(
				dctx,
				SLBT_ERR_MKVARS_PARSE);
		}
	} else {
		optshared = SLBT_DRIVER_SHARED;
		optstatic = SLBT_DRIVER_STATIC;
	}

	*flags = optshared | optstatic;

	/* host */
	if (!ctx->cctx.host.host) {
		if (slbt_get_mkvars_var(dctx,confctx,"host",0,&val) < 0)
			return SLBT_NESTED_ERROR(dctx);

		if (val[0] && !(ctx->host.host = strdup(val)))
			return SLBT_SYSTEM_ERROR(dctx,0);

		ctx->cctx.host.host = ctx->host.host;
	}


	/* ar tool */
	if (!ctx->cctx.host.ar) {
		if (slbt_get_mkvars_var(dctx,confctx,"AR",0x20,&val) < 0)
			return SLBT_NESTED_ERROR(dctx);

		if (val[0] && !(ctx->host.ar = strdup(val)))
			return SLBT_SYSTEM_ERROR(dctx,0);

		ctx->cctx.host.ar = ctx->host.ar;
	}


	/* nm tool */
	if (!ctx->cctx.host.nm) {
		if (slbt_get_mkvars_var(dctx,confctx,"NM",0x20,&val) < 0)
			return SLBT_NESTED_ERROR(dctx);

		if (val[0] && !(ctx->host.nm = strdup(val)))
			return SLBT_SYSTEM_ERROR(dctx,0);

		ctx->cctx.host.nm = ctx->host.nm;
	}


	/* ranlib tool */
	if (!ctx->cctx.host.ranlib) {
		if (slbt_get_mkvars_var(dctx,confctx,"RANLIB",0x20,&val) < 0)
			return SLBT_NESTED_ERROR(dctx);

		if (val[0] && !(ctx->host.ranlib = strdup(val)))
			return SLBT_SYSTEM_ERROR(dctx,0);

		ctx->cctx.host.ranlib = ctx->host.ranlib;
	}


	/* as tool (optional) */
	if (!ctx->cctx.host.as) {
		if (slbt_get_mkvars_var(dctx,confctx,"AS",0x20,&val) < 0)
			return SLBT_NESTED_ERROR(dctx);

		if (val[0] && !(ctx->host.as = strdup(val)))
			return SLBT_SYSTEM_ERROR(dctx,0);


		ctx->cctx.host.as = ctx->host.as;
	}


	/* dlltool tool (optional) */
	if (!ctx->cctx.host.dlltool) {
		if (slbt_get_mkvars_var(dctx,confctx,"DLLTOOL",0x20,&val) < 0)
			return SLBT_NESTED_ERROR(dctx);

		if (val[0] && !(ctx->host.dlltool = strdup(val)))
			return SLBT_SYSTEM_ERROR(dctx,0);

		ctx->cctx.host.dlltool = ctx->host.dlltool;
	}


	/* all done */
	return 0;
}