about summary refs log tree commit diff
path: root/buildtools/libopt.c
blob: e0bfde7e74ad3de910605ae19309daaa7a6df2ba (plain) (blame)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*----------------------------------------------------------------------------
                                 libopt
------------------------------------------------------------------------------
  This is a program to convert link library filepaths to linker options
  that select them.  E.g. ../lib/libnetpbm.so becomes -L../lib -lnetpbm   .

  Each argument is a library filepath.  The option string to identify
  all of those library filepaths goes to Standard Output.

  If there is no slash in the library filepath, we assume it is just a
  filename to be searched for in the linker's default search path, and
  generate a -l option, but no -L.

  If an argument doesn't make sense as a library filespec, we copy
  it verbatim, blank delimited, to the output string.

  The "lib" part of the library name, which we call the prefix, may be
  other than "lib".  The list of recognized values is compiled in as
  the macro SHLIBPREFIXLIST (see below).
  
  There is no newline or null character or anything after the output.

  If you specify the option "-runtime", the output includes a -R
  option in addition, for every library after "-runtime" in the
  arguments.  -R tells the buildtime linker to include the specified
  library's directory in the search path that the runtime linker uses
  to find dynamically linked libraries.

  But if you compile this program with the EXPLICIT macro defined, and
  a library filepath contains a slash, then it skips all that -L/-l
  nonsense and just outputs the input library filepath verbatim (plus
  any -R option).
------------------------------------------------------------------------------
  Why would you want to use this?

  On some systems, the -L/-l output of this program has exactly the
  same effect as the filepath input when used in the arguments to a
  link command.  A GNU/Linux system, for example.  On others (Solaris,
  for example), if you include /tmp/lib/libnetpbm.so in the link as a
  link object, the executable gets built in such a way that the system
  accesses the shared library /tmp/lib/libnetpbm.so at run time.  On the
  other hand, if you instead put the options -L/tmp/lib -lnetpbm on the
  link command, the executable gets built so that the system accesses
  libnetpbm.so in its actual installed directory at runtime (that
  location might be determined by a --rpath linker option or a
  LD_LIBRARY_PATH environment variable at run time).

  In a make file, it is nice to use the same variable as the
  dependency of a rule that builds an executable and as the thing that
  the rule's command uses to identify its input.  Here is an example
  of using libopt for that:

     NETPBMLIB=../lib/libnetpbm.so
     ...
     pbmmake: pbmmake.o $(NETPBMLIB)
             ld -o pbmmake pbmmake.o `libopt $(NETPBMLIB)` --rpath=/lib/netpbm

  Caveat: "-L../lib -lnetpbm" is NOT exactly the same as
  "../lib/libnetpbm.so" on any system.  All of the -l libraries are
  searched for in all of the -L directories.  So you just might get a
  different library with the -L/-l version than if you specify the
  library file explicitly.

  That's why you should compile with -DEXPLICIT if your linker can
  handle explicit file names.

-----------------------------------------------------------------------------*/
#define _BSD_SOURCE 1      /* Make sure strdup() is in stdio.h */
#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */

#define MAX_PREFIXES 10

/* Here's how to use SHLIBPREFIXLIST:  Use a -D compile option to pass in
   a value appropriate for the platform on which you are linking libraries.

   It's a blank-delimited list of prefixes that library names might
   have.  "lib" is traditionally the only prefix, as in libc or
   libnetpbm.  However, on Windows there is a convention of using
   different prefixes to distinguish different co-existent versions of
   the same library (kind of like a major number in some unices). 
   E.g. the value "cyg lib" is appropriate for a Cygwin system.
*/
#ifndef SHLIBPREFIXLIST
#  define SHLIBPREFIXLIST "lib"
#endif

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

typedef unsigned char bool;
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif

#ifdef DLLVERSTR
static const char * dllverstr = DLLVERSTR;
#else
static const char * dllverstr = "";
#endif

bool const explicit = 
#ifdef EXPLICIT
TRUE
#else
FALSE
#endif
;

static void
strfree(const char * const arg) {
    free((void*) arg);
}


static void
parse_prefixlist(const char * const prefixlist, 
                 char * parsed_prefixes[MAX_PREFIXES+1],
                 bool * const errorP) {
/*----------------------------------------------------------------------------
   Given a space-separated list of tokens (suffixes), return an
   argv-style array of pointers, each to a newly malloc'ed storage
   area the prefix as a null-terminated string.  Return a null string
   for the unused entries at the end of the array

   We never return more than MAX_PREFIXES prefixes from the input, so
   there is guaranteed always to be one null string at the end of the
   array.

   In case of error, return *errorP == TRUE and don't allocate any
   storage.  Otherwise, return *errorP = FALSE.
-----------------------------------------------------------------------------*/
    char * prlist;

    prlist = strdup(prefixlist);
    if (prlist == NULL)
        *errorP = TRUE;
    else {
        if (strlen(prlist) <= 0) 
            *errorP = TRUE;
        else {
            /* NOTE: Mac OS X, at least, does not have strtok_r().
               2001.09.24
            */
            char * token;
            int num_tokens;
            int i;
            
            for (i=0; i < MAX_PREFIXES + 1; i++) {
                parsed_prefixes[i] = NULL;
            }
            num_tokens = 0;
            token = strtok(prlist, " ");
            *errorP = FALSE;  /* initial value */
            while (token != NULL && num_tokens < MAX_PREFIXES && !*errorP) {
                parsed_prefixes[num_tokens] = strdup (token);
                if (parsed_prefixes[num_tokens] == NULL) 
                    *errorP = TRUE;
                num_tokens++;
                token = strtok(NULL, " ");
            }
            for (i = num_tokens; i < MAX_PREFIXES + 1 && !*errorP;  i++) {
                parsed_prefixes[i] = strdup("");
                if (parsed_prefixes[i] == NULL) 
                    *errorP = TRUE;
            }
        }
        if (*errorP) {
            /* Deallocate any array entries we successfully did */
            int i;
            for (i = 0; i < MAX_PREFIXES + 1; i++)
                if (parsed_prefixes[i])
                    free(parsed_prefixes[i]);
        }
        free(prlist);
    }
}



static void
parse_prefix(const char * const filename, 
             bool * const prefix_good_p, unsigned int * const prefix_length_p,
             bool * const error_p) {
/*----------------------------------------------------------------------------
   Find the library name prefix (e.g. "lib") in the library filename
   'filename'.
   
   Return the length of the prefix, in characters, as *prefix_length_p.
   (The prefix always starts at the beginning of the filename).

   Iff we don't find a valid library name prefix, return *prefix_good_p
   == FALSE.  

   The list of valid prefixes is compiled in as the blank-delimited
   string which is the value of the SHLIBPREFIXLIST macro.
-----------------------------------------------------------------------------*/
    char * shlibprefixlist[MAX_PREFIXES+1];
        /* An argv-style array of prefix strings in the first entries, 
           null strings in the later entries.  At most MAX_PREFIXES prefixes,
           so at least one null string.
        */
    char * prefix;
        /* The prefix that the filename actually
           uses.  e.g. if shlibprefixlist = { "lib", "cyg", "", ... } and the
           filename is "path/to/cyg<something>.<extension>", then 
           prefix = "cyg".  String is in the same storage as pointed to by
           shlibprefixlist (shlibprefixlist[1] in this example).
        */
    bool prefix_good;
        /* The first part of the filename matched one of the prefixes
           in shlibprefixlist[].
        */
    int prefix_length;
    int i;

    parse_prefixlist(SHLIBPREFIXLIST , shlibprefixlist, error_p);
    if (!*error_p) {
        if (strcmp(shlibprefixlist[0], "") == 0) {
            fprintf(stderr, "libopt was compiled with an invalid value "
                    "of the SHLIBPREFIX macro.  It seems to have no "
                    "tokens.  SHLIBPREFIX = '%s'", 
                    SHLIBPREFIXLIST);
            exit(100);
        }

        i = 0;  /* start with the first entry in shlibprefixlist[] */
        prefix_length = 0;  /* initial value */
        prefix = shlibprefixlist[i];
        prefix_good = FALSE;  /* initial value */
        while ( (*prefix != '\0' ) && !prefix_good ) {
            /* stop condition: shlibprefixlist has MAX_PREFIXES+1 entries.
             * we only ever put tokens in the 0..MAX_PREFIXES-1 positions.
             * Then, we fill DOWN from the MAX_PREFIXES position with '\0'
             * so we insure that the shlibprefixlist array contains at 
             * least one final '\0' string, but probably many '\0' 
             * strings (depending on how many tokens there were).               
             */
            prefix_length = strlen(prefix);
            if (strncmp(filename, prefix, prefix_length) == 0) {
                prefix_good = TRUE;
                /* at this point, prefix is pointing to the correct
                 * entry, and prefix_length has the correct value.
                 * When we bail out of the while loop because of the
                 * !prefix_good clause, we can then use these 
                 * vars (prefix, prefix_length) 
                 */
            } else {
                prefix = shlibprefixlist[++i];
            }
        }
        *prefix_length_p = prefix_length;
        *prefix_good_p = prefix_good;
        { 
            int i;
            for (i=0; i < MAX_PREFIXES + 1; i++) 
                free (shlibprefixlist[i]);
        }
    }
}



static void
parse_filename(const char *  const filename,
               const char ** const libname_p,
               bool *        const valid_library_p,
               bool *        const static_p,
               bool *        const error_p) {
/*----------------------------------------------------------------------------
   Extract the library name root component of the filename 'filename'.  This
   is just a filename, not a whole pathname.

   Return it in newly malloc'ed storage pointed to by '*libname_p'.
   
   E.g. for "libxyz.so", return "xyz".

   return *valid_library_p == TRUE iff 'filename' validly names a library
   that can be expressed in a -l linker option.

   return *static_p == TRUE iff 'filename' indicates a static library.
   (but undefined if *valid_library_p != TRUE).

   return *error_p == TRUE iff some error such as out of memory prevents
   parsing.

   Do not allocate any memory if *error_p == TRUE or *valid_library_p == FALSE.
-----------------------------------------------------------------------------*/
    char *lastdot;  
    /* Pointer to last period in 'filename'.  Null if none */
    
    /* We accept any period-delimited suffix as a library type suffix.
       It's probably .so or .a, but is could be .kalamazoo for all we
       care. (HOWEVER, the double-suffixed import lib used on 
       cygwin (.dll.a) is NOT understood). 
    */
    char *p;

    lastdot = strrchr(filename, '.');
    if (lastdot == NULL) {
        /* This filename doesn't have any suffix, so we don't understand
           it as a library filename.
        */
        *valid_library_p = FALSE;
        *error_p = FALSE;
    } else {
        unsigned int prefix_length;
        bool prefix_good;

        if (strcmp(lastdot, "a") == 0)
            *static_p = TRUE;
        else
            *static_p = FALSE;

        parse_prefix(filename, &prefix_good, &prefix_length, error_p);
        if (!*error_p) {
            if (!prefix_good) {
                *valid_library_p = FALSE;
            } else {
                /* Extract everything between <prefix> and "." as 
                   the library name root. 
                */
                char * libname;

                libname = strdup(filename + prefix_length);
                if (libname == NULL)
                    *error_p = TRUE;
                else {
                    libname[lastdot - filename - prefix_length] = '\0';
                    if (strlen(dllverstr) > 0) {
                        p = strstr(libname, dllverstr);
                        if (p) {
                            if (libname + strlen(libname) 
                                - strlen(dllverstr) == p) {
                                *p = '\0';
                            }
                        }
                    }
                    if (strlen(libname) == 0) {
                        *valid_library_p = FALSE;
                        strfree(libname);
                    } else
                        *valid_library_p = TRUE;
                }
                *libname_p = libname;
            }
        }
    }
}   



static void
parse_filepath(const char *  const filepath,
               const char ** const directory_p, 
               const char ** const filename_p,
               bool *        const error_p) {
/*----------------------------------------------------------------------------
   Extract the directory and filename components of the filepath 
   'filepath' and return them in newly malloc'ed storage pointed to by
   '*directory_p' and '*filename_p'.

   If there is no directory component, return a null string for it.
-----------------------------------------------------------------------------*/
    char *directory;
    char *lastslash; /* Pointer to last slash in 'filepath', or null if none */

    lastslash = strrchr(filepath, '/');

    if (lastslash == NULL) {
        /* There's no directory component; the filename starts at the
           beginning of the filepath 
        */
        *filename_p = strdup(filepath);
        if (*filename_p == NULL)
            *error_p = TRUE;
        else {
            directory = strdup("");
            if (directory == NULL) {
                *error_p = TRUE;
                strfree(*filename_p);
            } else
                *error_p = FALSE;
        }
    } else {
        /* Split the string at the slash we just found, into filename and 
           directory 
           */
        *filename_p = strdup(lastslash+1);
        if (*filename_p == NULL)
            *error_p = TRUE;
        else {
            directory = strdup(filepath);
            if (directory == NULL) {
                *error_p = TRUE;
                strfree(*filename_p);
            } else {
                *error_p = FALSE;
                directory[lastslash - filepath] = '\0';
            }
        }
    }
    *directory_p = directory;
}



static void
doOptions(const char *  const filepath, 
          const char *  const directory,
          const char *  const libname,
          bool          const runtime,
          bool          const explicit,
          bool          const staticlib,
          const char ** const optionsP) {

    char * options;
    char * linkopt;

    if (strlen(directory) == 0) {
        linkopt = malloc(strlen(libname) + 10);
        sprintf(linkopt, "-l%s", libname);
    } else {
        if (explicit)
            linkopt = strdup(filepath);
        else {
            linkopt = malloc(strlen(directory) + strlen(libname) + 10);
            sprintf(linkopt, "-L%s -l%s", directory, libname);
        }
    }
    if (runtime && !staticlib && strlen(directory) > 0) {
        options = malloc(strlen(linkopt) + strlen(directory) + 10);
        sprintf(options, "%s -R%s", linkopt, directory); 
    } else
        options = strdup(linkopt);
    
    strfree(linkopt);
    
    *optionsP = options;
}



static void
processOneLibrary(const char *  const filepath, 
                  bool          const runtime, 
                  bool          const explicit,
                  const char ** const optionsP,
                  bool *        const errorP) {
/*----------------------------------------------------------------------------
   Process the library with filepath 'filepath'.  Return the resulting
   linker option string as a newly malloced null-terminated string at
   *optionsP.
-----------------------------------------------------------------------------*/
    const char * directory;
        /* Directory component of 'filepath' */
    const char * filename;
        /* Filename component of 'filepath' */

    parse_filepath(filepath, &directory, &filename, errorP);
    if (!*errorP) {
        const char *libname;
            /* Library name component of 'filename'.  e.g. xyz in
               libxyz.so 
            */
        bool valid_library;  
            /* Our argument is a valid library filepath that can be
               converted to -l/-L notation.  
            */
        bool staticlib;
            /* Our argument appears to name a static library. */

        parse_filename(filename, 
                       &libname, &valid_library, &staticlib, errorP);
        
        if (!*errorP) {
            if (valid_library) {
                doOptions(filepath, directory, libname, 
                          runtime, explicit, staticlib, optionsP);

                strfree(libname);
            } else
                *optionsP = strdup(filepath);
        }
        strfree(directory); 
        strfree(filename);
    }
}



int
main(int argc, char **argv) {

    bool error;
    bool runtime;  /* -runtime option has been seen */
    bool quiet;    /* -quiet option has been seen */
    int retval;
    unsigned int arg;  /* Index into argv[] of argument we're processing */
    char outputLine[1024];

    strcpy(outputLine, "");  /* initial value */
    runtime = FALSE;  /* initial value */
    quiet = FALSE;   /* initial value */
    error = FALSE;  /* no error yet */
    for (arg = 1; arg < argc && !error; arg++) {
        if (strcmp(argv[arg], "-runtime") == 0)
            runtime = TRUE;
        else if (strcmp(argv[arg], "-quiet") == 0)
            quiet = TRUE;
        else {
            const char * options;
            processOneLibrary(argv[arg], runtime, explicit, 
                              &options, &error);
            if (!error) {
                if (strlen(outputLine) + strlen(options) + 1 + 1 > 
                    sizeof(outputLine))
                    error = TRUE;
                else {
                    strcat(outputLine, " ");
                    strcat(outputLine, options);
                }
                strfree(options);
            }
        }
    }
    if (error) {
        fprintf(stderr, "serious libopt error prevented parsing library "
                "names.  Invalid input to libopt is NOT the problem.\n");
        retval = 10;
    } else {
        fputs(outputLine, stdout);
        retval = 0;
    }
    return retval;
}