summaryrefslogtreecommitdiff
path: root/src/internal/slibtool_libmeta_impl.c
blob: 0db06863dd0492e4ced1b71713973aaafa9c3050 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************/
/*  slibtool: a skinny libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <slibtool/slibtool.h>
#include "slibtool_driver_impl.h"
#include "slibtool_dprintf_impl.h"
#include "slibtool_errinfo_impl.h"
#include "slibtool_metafile_impl.h"
#include "slibtool_visibility_impl.h"

static int  slbt_create_default_library_wrapper(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	const char *			arname,
	const char *			soname,
	const char *			soxyz,
	const char *			solnk)
{
	int					ret;
	int					fdout;
	const char *				header;
	const char *				base;
	bool					fnover;
	bool					fvernum;
	int					current;
	int					revision;
	int					age;
	const struct slbt_source_version *	verinfo;

	(void)ectx;

	/* create */
	if ((fdout = openat(
			slbt_driver_fdcwd(dctx),
			dctx->cctx->output,
			O_RDWR|O_CREAT|O_TRUNC,
			0644)) < 0)
		return SLBT_SYSTEM_ERROR(dctx,dctx->cctx->output);

	/* version info */
	current  = 0;
	age      = 0;
	revision = 0;

	if (dctx->cctx->verinfo.verinfo)
		sscanf(dctx->cctx->verinfo.verinfo,"%d:%d:%d",
			&current,&revision,&age);

	fnover  = !!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION);
	fvernum = !!(dctx->cctx->verinfo.vernumber);
	verinfo = slbt_api_source_version();

	/* wrapper header */
	header = "libtool compatible library wrapper\n";
	base   = "";

	/* wrapper content */
	ret = slbt_dprintf(fdout,
		"# %s%s"
		"# Generated by %s (slibtool %d.%d.%d)\n"
		"# [commit reference: %s]\n\n"

		"dlname='%s'\n"
		"library_names='%s %s %s'\n"
		"old_library='%s'\n\n"

		"inherited_linker_flags='%s'\n"
		"dependency_libs='%s'\n"
		"weak_library_names='%s'\n\n"

		"current=%d\n"
		"age=%d\n"
		"revision=%d\n\n"

		"installed=%s\n"
		"shouldnotlink=%s\n\n"

		"dlopen='%s'\n"
		"dlpreopen='%s'\n\n"

		"libdir='%s'\n",

		/* wrapper header */
		base,header,

		/* nickname, verinfo */
		dctx->program,
		verinfo->major,verinfo->minor,verinfo->revision,
		verinfo->commit,

		/* dlname */
		fnover ? solnk : soxyz,

		/* library_names */
		fnover ? solnk : soxyz,
		fnover ? solnk : soname,
		solnk,

		/* old_library */
		arname,

		/* inherited_linker_flags, dependency_libs, weak_library_names */
		"","","",

		/* current, age, revision */
		fvernum ? dctx->cctx->verinfo.major : current,
		fvernum ? dctx->cctx->verinfo.minor : age,
		fvernum ? dctx->cctx->verinfo.major : revision,

		/* installed, shouldnotlink */
		"no","no",

		/* dlopen, dlpreopen */
		"","",

		/* libdir */
		dctx->cctx->rpath ? dctx->cctx->rpath : "");

	close(fdout);

	return (ret < 0) ? SLBT_SYSTEM_ERROR(dctx,0) : 0;
}

static int  slbt_create_compatible_library_wrapper(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	const char *			arname,
	const char *			soname,
	const char *			soxyz,
	const char *			solnk)
{
	int					ret;
	int					fdout;
	const char *				base;
	bool					fnover;
	bool					fvernum;
	int					current;
	int					revision;
	int					age;
	const struct slbt_source_version *	verinfo;

	(void)ectx;

	/* create */
	if ((fdout = openat(
			slbt_driver_fdcwd(dctx),
			dctx->cctx->output,
			O_RDWR|O_CREAT|O_TRUNC,
			0644)) < 0)
		return SLBT_SYSTEM_ERROR(dctx,dctx->cctx->output);

	/* version info */
	current  = 0;
	age      = 0;
	revision = 0;

	/* wrapper header */
	if ((base = strrchr(dctx->cctx->output,'/'))) {
		base++;
	} else {
		base = dctx->cctx->output;
	}

	if (dctx->cctx->verinfo.verinfo)
		sscanf(dctx->cctx->verinfo.verinfo,"%d:%d:%d",
			&current,&revision,&age);

	fnover  = !!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION);
	fvernum = !!(dctx->cctx->verinfo.vernumber);
	verinfo = slbt_api_source_version();

	/* wrapper content */
	ret = slbt_dprintf(fdout,
		"# %s - a libtool library file\n"
		"# Generated by %s (slibtool %d.%d.%d)\n"
		"# [commit reference: %s]\n"
		"#\n"
		"# Please DO NOT delete this file!\n"
		"# It is necessary for linking the library.\n\n"

		"# The name that we can dlopen(3).\n"
		"dlname='%s'\n\n"

		"# Names of this library.\n"
		"library_names='%s %s %s'\n\n"

		"# The name of the static archive.\n"
		"old_library='%s'\n\n"

		"# Linker flags that can not go in dependency_libs.\n"
		"inherited_linker_flags='%s'\n\n"

		"# Libraries that this one depends upon.\n"
		"dependency_libs='%s'\n\n"

		"# Names of additional weak libraries provided by this library\n"
		"weak_library_names='%s'\n\n"

		"# Version information for %s%s.\n"
		"current=%d\n"
		"age=%d\n"
		"revision=%d\n\n"

		"# Is this an already installed library?\n"
		"installed=%s\n\n"

		"# Should we warn about portability when linking against -modules?\n"
		"shouldnotlink=%s\n\n"

		"# Files to dlopen/dlpreopen\n"
		"dlopen='%s'\n"
		"dlpreopen='%s'\n\n"

		"# Directory that this library needs to be installed in:\n"
		"libdir='%s'\n",

		/* wrapper header */
		base,

		/* nickname, verinfo */
		dctx->program,
		verinfo->major,verinfo->minor,verinfo->revision,
		verinfo->commit,

		/* dlname */
		fnover ? solnk : soxyz,

		/* library_names */
		fnover ? solnk : soxyz,
		fnover ? solnk : soname,
		solnk,

		/* old_library */
		arname,

		/* inherited_linker_flags, dependency_libs, weak_library_names */
		"","","",

		/* version information for */
		(dctx->cctx->drvflags & SLBT_DRIVER_MODULE)
		    ? "" : dctx->cctx->settings.dsoprefix,
		dctx->cctx->libname,

		/* current, age, revision */
		fvernum ? dctx->cctx->verinfo.major : current,
		fvernum ? dctx->cctx->verinfo.minor : age,
		fvernum ? dctx->cctx->verinfo.major : revision,

		/* installed, shouldnotlink */
		"no","no",

		/* dlopen, dlpreopen */
		"","",

		/* libdir */
		dctx->cctx->rpath ? dctx->cctx->rpath : "");

	close(fdout);

	return (ret < 0) ? SLBT_SYSTEM_ERROR(dctx,0) : 0;
}

slbt_hidden int slbt_create_library_wrapper(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	const char *			arname,
	const char *			soname,
	const char *			soxyz,
	const char *			solnk)
{
	if (dctx->cctx->drvflags & SLBT_DRIVER_LEGABITS)
		return slbt_create_compatible_library_wrapper(
			dctx,ectx,arname,soxyz,soname,solnk);
	else
		return slbt_create_default_library_wrapper(
			dctx,ectx,arname,soxyz,soname,solnk);
}