source: roaraudio/roarclients/roar-config.c @ 5811:28f6eed531d7

Last change on this file since 5811:28f6eed531d7 was 5811:28f6eed531d7, checked in by phi, 11 years ago

improved roar_env_render_path_r() and added support for it to roar-config

File size: 8.5 KB
Line 
1//roar-config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27
28static const struct {
29 const char * name;
30 const char * cflags;
31 const char * libs;
32} flags[] = {
33 // native/own libs:
34 {"roar",      ROAR_CFLAGS, ROAR_LIBS        }, // NOTE: libroar *MUST* be the first entry
35 {"roardsp",   ROAR_CFLAGS, ROAR_LIBS_DSP    },
36 {"roarmidi",  ROAR_CFLAGS, ROAR_LIBS_MIDI   },
37 {"roarlight", ROAR_CFLAGS, ROAR_LIBS_LIGHT  },
38 {"roareio",   ROAR_CFLAGS, ROAR_LIBS_EIO    },
39 // comp libs:
40 {"roaresd",   ROAR_CFLAGS, ROAR_LIBS_C_ESD  },
41 {"esd",       ROAR_CFLAGS, ROAR_LIBS_C_ESD  },
42 {"roarartsc", ROAR_CFLAGS, ROAR_LIBS_C_ARTSC},
43 {"artsc",     ROAR_CFLAGS, ROAR_LIBS_C_ARTSC},
44 {"roarpulse", ROAR_CFLAGS, ROAR_LIBS_C_PULSE},
45 {"pulse",     ROAR_CFLAGS, ROAR_LIBS_C_PULSE},
46 {"roarpulse-simple", ROAR_CFLAGS, ROAR_LIBS_C_PULSE_SIMPLE},
47 {"pulse-simple",     ROAR_CFLAGS, ROAR_LIBS_C_PULSE_SIMPLE},
48 {"roarsndio", ROAR_CFLAGS, ROAR_LIBS_C_SNDIO},
49 {"sndio",     ROAR_CFLAGS, ROAR_LIBS_C_SNDIO},
50 {"roaryiff",  ROAR_CFLAGS, ROAR_LIBS_C_YIFF },
51 {"Y2",        ROAR_CFLAGS, ROAR_LIBS_C_YIFF },
52 {NULL, NULL, NULL}
53}, * flags_ptr = NULL;
54
55struct version {
56 int major, minor, revision;
57};
58
59static struct version parse_version(const char * version) {
60 struct version ret = {-1, -1, -1};
61 char * next;
62
63 if ( !strcasecmp(version, "current") )
64  version = COMMON_VERSION;
65
66 ret.major    = strtoll(version, &next, 0);
67 ret.minor    = strtoll(next+1, &next, 0);
68 ret.revision = strtoll(next+1, &next, 0);
69
70 return ret;
71}
72
73static int compare_versions_eq(struct version a, struct version b) {
74 return a.major == b.major && a.minor == b.minor && a.revision == b.revision;
75}
76
77static int compare_versions_gt(struct version a, struct version b) {
78 if ( a.major > b.major )
79  return 1;
80 if ( a.minor > b.minor )
81  return 1;
82 if ( a.revision > b.revision )
83  return 1;
84 return 0;
85}
86
87static int compare_versions_ge(struct version a, struct version b) {
88 return compare_versions_gt(a, b) || compare_versions_eq(a, b);
89}
90
91static int compare_versions_lt(struct version a, struct version b) {
92 return compare_versions_gt(b, a);
93}
94
95static int compare_versions_le(struct version a, struct version b) {
96 return compare_versions_lt(a, b) || compare_versions_eq(a, b);
97}
98
99static const struct {
100 const char * op;
101 int neg;
102 int (*func)(struct version a, struct version b);
103} compare_versions_funcs[] = {
104 {"eq", 0, compare_versions_eq},
105 {"ne", 1, compare_versions_eq},
106 {"gt", 0, compare_versions_gt},
107 {"ge", 0, compare_versions_ge},
108 {"lt", 0, compare_versions_lt},
109 {"le", 0, compare_versions_le}
110};
111
112static int compare_versions(const char * a, const char * op, const char * b) {
113 struct version va = parse_version(a);
114 struct version vb = parse_version(b);
115 size_t i;
116 int ret;
117
118 ROAR_DBG("compare_versions(a='%s', op='%s', b='%s'): {%i, %i, %i} <%s> {%i, %i, %i}\n", a, op, b, va.major, va.minor, va.revision, op, vb.major, vb.minor, vb.revision);
119
120 for (i = 0; i < (sizeof(compare_versions_funcs)/sizeof(*compare_versions_funcs)); i++) {
121  if ( !strcasecmp(compare_versions_funcs[i].op, op) ) {
122   ret = compare_versions_funcs[i].func(va, vb);
123   if ( compare_versions_funcs[i].neg )
124    ret = ret ? 0 : 1;
125
126   return ret ? 0 : 32;
127  }
128 }
129
130 ROAR_ERR("compare_versions(*): Operator \"%s\" not found.", op);
131 return 1;
132}
133
134void print_path(const char * name, int null_as_universal, const char * product, const char * provider) {
135 char * path = roar_libroar_get_path(name, null_as_universal, product, provider);
136 if ( path == NULL ) {
137  fprintf(stderr, "Error: Can not get path: %s: %s\n", name, roar_errorstring);
138  return;
139 }
140 printf("%s\n", path);
141 roar_mm_free(path);
142}
143
144void print_renderpath(const char * path) {
145 char buf[1024];
146
147 if ( roar_env_render_path_r(buf, sizeof(buf), path) != 0 ) {
148  fprintf(stderr, "Error: Can not render path: %s: %s\n", path, roar_errorstring);
149  return;
150 }
151 printf("%s\n", buf);
152}
153
154void usage (void) {
155 printf("Usage: roar-config --version\n"
156        "       roar-config --compare-versions VERSIONA OPERATOR VERSIONB\n"
157        "       roar-config [{--output-pc|--output-normal}] [--libs] [--cflags] [LIB]\n"
158        "       roar-config [--product PRODUCT] [--provider PROVIDER] [--universal] --path PATH\n"
159        "       roar-config --render-path PATH\n");
160
161 printf("\nOptions:\n\n");
162
163 printf("  --help              - Show this help\n"
164        "  --version           - Show version of library\n"
165        "  --compare-versions A OP B\n"
166        "                      - Compare version A against B with operator OP.\n"
167        "  --path NAME         - Print path NAME\n"
168        "  --product PRODUCT   - Product string for --path\n"
169        "  --provider PROVIDER - Provider string for --path\n"
170        "  --universal         - Use universal path for --path\n"
171        "  --render-path PATH  - Print a rendered path\n"
172        "  --libs              - Show linker flags (-lxxx) needed to link library\n"
173        "  --cflags            - Show compiler flags needed to link library\n"
174        "  --output-pc         - Output PC format\n"
175        "  --output-normal     - Output in \"classical\" format\n"
176       );
177
178}
179
180#define _strcat(buf, n) strncat(buf, n, sizeof(buf)-1-strlen(buf))
181
182int main (int argc, char * argv[]) {
183 enum { NORMAL, PC } mode = NORMAL;
184 int null_as_universal = 0;
185 const char * product = NULL;
186 const char * provider = NULL;
187 int i, h;
188 int cflags = 0;
189 int libs   = 0;
190 char buf[1024] = {0};
191
192 if ( argc == 1 ) {
193  usage();
194  return 0;
195 }
196
197 for (i = 1; i < argc; i++) {
198  if ( !strcmp(argv[i], "--version") ) {
199   printf("%s\n", COMMON_VERSION);
200  } else if ( !strcmp(argv[i], "--help") || !strcmp(argv[i], "-h") ) {
201   usage();
202   return 0;
203  } else if ( !strcmp(argv[i], "--compare-versions") ) {
204   return compare_versions(argv[i+1], argv[i+2], argv[i+3]);
205  } else if ( !strcmp(argv[i], "--product") ) {
206   product = argv[++i];
207  } else if ( !strcmp(argv[i], "--provider") ) {
208   provider = argv[++i];
209  } else if ( !strcmp(argv[i], "--universal") ) {
210   null_as_universal = 1;
211  } else if ( !strcmp(argv[i], "--path") ) {
212   print_path(argv[++i], null_as_universal, product, provider);
213  } else if ( !strcmp(argv[i], "--render-path") ) {
214   print_renderpath(argv[++i]);
215  } else if ( !strcmp(argv[i], "--libs") ) {
216   libs   = 1;
217  } else if ( !strcmp(argv[i], "--cflags") ) {
218   cflags = 1;
219  } else if ( !strcmp(argv[i], "--output-normal") ) {
220   mode = NORMAL;
221  } else if ( !strcmp(argv[i], "--output-pc") ) {
222   mode = PC;
223  } else if ( flags_ptr == NULL ) {
224   if ( !strncmp(argv[i], "lib", 3) )
225    argv[i] += 3;
226
227   for (h = 0; flags[h].name != NULL; h++) {
228    if ( !strcasecmp(argv[i], flags[h].name) )
229     flags_ptr = &(flags[h]);
230   }
231
232   if ( flags_ptr == NULL ) {
233    fprintf(stderr, "Unknown lib: %s\n", argv[i]);
234    return 2;
235   }
236  } else {
237   fprintf(stderr, "Unknown option: %s\n", argv[i]);
238   usage();
239   return 1;
240  }
241 }
242
243 if ( flags_ptr == NULL )
244  flags_ptr = &(flags[0]);
245
246 switch (mode) {
247  case NORMAL:
248    if ( cflags || libs ) {
249     if ( cflags )
250      _strcat(buf, flags_ptr->cflags);
251
252     if ( libs )
253      _strcat(buf, flags_ptr->libs);
254
255     puts(buf);
256    }
257   break;
258  case PC:
259    printf(
260           "prefix=%s\n"
261           "exec_prefix=${prefix}\n"
262           "libdir=%s\n"
263           "includedir=%s\n",
264           PREFIX, PREFIX_LIB, PREFIX_INC
265          );
266    printf("\n");
267    printf(
268           "Name: lib%s\n"
269           "Description: lib%s is part of RoarAudio Sound System\n"
270           "Version: %s\n"
271//           "Requires: libroar\n"
272           "Conflicts:\n"
273           "Libs: -L${libdir} %s\n"
274           "Cflags: -I${includedir} %s\n",
275           flags_ptr->name,
276           flags_ptr->name,
277           COMMON_VERSION,
278           flags_ptr->libs,
279           flags_ptr->cflags
280          );
281   break;
282 }
283
284 return 0;
285}
286
287//ll
Note: See TracBrowser for help on using the repository browser.