1 /*******************************************************************/
2 /* slibtool: a skinny libtool implementation, written in C */
3 /* Copyright (C) 2016--2018 Z. Gilboa */
4 /* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
5 /*******************************************************************/
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdbool.h>
13 #include <slibtool/slibtool.h>
14 #include "slibtool_driver_impl.h"
15 #include "slibtool_dprintf_impl.h"
16
17 #ifndef SLBT_DRIVER_FLAGS
18 #define SLBT_DRIVER_FLAGS SLBT_DRIVER_VERBOSITY_ERRORS \
19 | SLBT_DRIVER_VERBOSITY_USAGE
20 #endif
21
22 static const char vermsg[] = "%s%s%s (https://git.foss21.org/slibtool): "
23 "version %s%d.%d.%d%s.\n"
24 "%s%s%s%s%s\n";
25
26 static const char * const slbt_ver_color[6] = {
27 "\x1b[1m\x1b[35m","\x1b[0m",
28 "\x1b[1m\x1b[32m","\x1b[0m",
29 "\x1b[1m\x1b[34m","\x1b[0m"
30 };
31
32 static const char * const slbt_ver_plain[6] = {
33 "","",
34 "","",
35 "",""
36 };
37
slbt_version(struct slbt_driver_ctx * dctx,int fdout)38 static ssize_t slbt_version(struct slbt_driver_ctx * dctx, int fdout)
39 {
40 const struct slbt_source_version * verinfo;
41 const char * const * verclr;
42 bool gitver;
43
44 verinfo = slbt_source_version();
45 verclr = isatty(fdout) ? slbt_ver_color : slbt_ver_plain;
46 gitver = strcmp(verinfo->commit,"unknown");
47
48 if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER)
49 verclr = slbt_ver_plain;
50
51 return slbt_dprintf(fdout,vermsg,
52 verclr[0],dctx->program,verclr[1],
53 verclr[2],verinfo->major,verinfo->minor,
54 verinfo->revision,verclr[3],
55 gitver ? "[commit reference: " : "",
56 verclr[4],gitver ? verinfo->commit : "",
57 verclr[5],gitver ? "]" : "");
58 }
59
slbt_perform_driver_actions(struct slbt_driver_ctx * dctx)60 static void slbt_perform_driver_actions(struct slbt_driver_ctx * dctx)
61 {
62 if (dctx->cctx->drvflags & SLBT_DRIVER_CONFIG)
63 slbt_output_config(dctx);
64
65 if (dctx->cctx->drvflags & SLBT_DRIVER_FEATURES)
66 slbt_output_features(dctx);
67
68 if (dctx->cctx->mode == SLBT_MODE_COMPILE)
69 slbt_exec_compile(dctx,0);
70
71 if (dctx->cctx->mode == SLBT_MODE_EXECUTE)
72 slbt_exec_execute(dctx,0);
73
74 if (dctx->cctx->mode == SLBT_MODE_INSTALL)
75 slbt_exec_install(dctx,0);
76
77 if (dctx->cctx->mode == SLBT_MODE_LINK)
78 slbt_exec_link(dctx,0);
79
80 if (dctx->cctx->mode == SLBT_MODE_UNINSTALL)
81 slbt_exec_uninstall(dctx,0);
82 }
83
slbt_exit(struct slbt_driver_ctx * dctx,int ret)84 static int slbt_exit(struct slbt_driver_ctx * dctx, int ret)
85 {
86 slbt_output_error_vector(dctx);
87 slbt_free_driver_ctx(dctx);
88 return ret;
89 }
90
slbt_main(char ** argv,char ** envp,const struct slbt_fd_ctx * fdctx)91 int slbt_main(char ** argv, char ** envp, const struct slbt_fd_ctx * fdctx)
92 {
93 int ret;
94 const char * harg;
95 int fdout;
96 uint64_t flags;
97 uint64_t noclr;
98 struct slbt_driver_ctx * dctx;
99 char * program;
100 char * dash;
101 char * sargv[5];
102
103 flags = SLBT_DRIVER_FLAGS;
104 fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
105 noclr = getenv("NO_COLOR") ? SLBT_DRIVER_ANNOTATE_NEVER : 0;
106
107 /* harg */
108 harg = (!argv || !argv[0] || !argv[1] || argv[2])
109 ? 0 : argv[1];
110
111 /* --version only? */
112 if (harg && (!strcmp(harg,"--version")
113 || !strcmp(harg,"--help-all")
114 || !strcmp(harg,"--help")
115 || !strcmp(harg,"-h"))) {
116 sargv[0] = argv[0];
117 sargv[1] = argv[1];
118 sargv[2] = "--mode=compile";
119 sargv[3] = "<compiler>";
120 sargv[4] = 0;
121
122 return (slbt_get_driver_ctx(sargv,envp,flags|noclr,fdctx,&dctx))
123 ? SLBT_ERROR : (slbt_version(dctx,fdout) < 0)
124 ? slbt_exit(dctx,SLBT_ERROR)
125 : slbt_exit(dctx,SLBT_OK);
126 }
127
128 /* program */
129 if ((program = strrchr(argv[0],'/')))
130 program++;
131 else
132 program = argv[0];
133
134 /* dash */
135 if ((dash = strrchr(program,'-')))
136 dash++;
137
138 /* flags */
139 if (dash == 0)
140 flags = SLBT_DRIVER_FLAGS;
141
142 else if (!(strcmp(dash,"shared")))
143 flags = SLBT_DRIVER_FLAGS | SLBT_DRIVER_DISABLE_STATIC;
144
145 else if (!(strcmp(dash,"static")))
146 flags = SLBT_DRIVER_FLAGS | SLBT_DRIVER_DISABLE_SHARED;
147
148 /* debug */
149 if (!(strcmp(program,"dlibtool")))
150 flags |= SLBT_DRIVER_DEBUG;
151
152 else if (!(strncmp(program,"dlibtool",8)))
153 if ((program[8] == '-') || (program[8] == '.'))
154 flags |= SLBT_DRIVER_DEBUG;
155
156 /* legabits */
157 if (!(strcmp(program,"clibtool")))
158 flags |= SLBT_DRIVER_LEGABITS;
159
160 else if (!(strncmp(program,"clibtool",8)))
161 if ((program[8] == '-') || (program[8] == '.'))
162 flags |= SLBT_DRIVER_LEGABITS;
163
164 /* heuristics */
165 if (!(strcmp(program,"rlibtool")))
166 flags |= SLBT_DRIVER_HEURISTICS;
167
168 /* heuristics + legabits */
169 if (!(strcmp(program,"rclibtool")))
170 flags |= (SLBT_DRIVER_HEURISTICS
171 | SLBT_DRIVER_LEGABITS);
172
173 /* heuristics + debug */
174 if (!(strcmp(program,"rdlibtool")))
175 flags |= (SLBT_DRIVER_HEURISTICS
176 | SLBT_DRIVER_DEBUG);
177
178 /* heuristics + debug + legabits */
179 if (!(strcmp(program,"rdclibtool")))
180 flags |= (SLBT_DRIVER_HEURISTICS
181 | SLBT_DRIVER_DEBUG
182 | SLBT_DRIVER_LEGABITS);
183
184 /* driver context */
185 if ((ret = slbt_get_driver_ctx(argv,envp,flags|noclr,fdctx,&dctx)))
186 return (ret == SLBT_USAGE)
187 ? !argv || !argv[0] || !argv[1] || !argv[2]
188 : SLBT_ERROR;
189
190 if (dctx->cctx->drvflags & SLBT_DRIVER_VERSION)
191 if ((slbt_version(dctx,fdout)) < 0)
192 return slbt_exit(dctx,SLBT_ERROR);
193
194 slbt_perform_driver_actions(dctx);
195
196 return slbt_exit(dctx,dctx->errv[0] ? SLBT_ERROR : SLBT_OK);
197 }
198