pkgconfig.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* ----- pkgconfig.c ----- */
  2. char* pkg_config(char** packages, char* opts) {
  3. char* tmp;
  4. int num_pkgs = list_len(packages);
  5. if(num_pkgs == 0) return strdup("");
  6. char* pkgs = join_str_list(packages, " ");
  7. for(char* c = opts; *c; c++) {
  8. switch(*c) {
  9. case 'c':
  10. case 'C':
  11. case 'i':
  12. case 'I':
  13. tmp = strjoin(" ", "--cflags", pkgs);
  14. free(pkgs);
  15. pkgs = tmp;
  16. break;
  17. case 'l':
  18. case 'L':
  19. tmp = strjoin(" ", "--libs", pkgs);
  20. free(pkgs);
  21. pkgs = tmp;
  22. break;
  23. }
  24. }
  25. tmp = strjoin(" ", "pkg-config", pkgs);
  26. free(pkgs);
  27. FILE* f = popen(tmp, "r");
  28. free(tmp);
  29. if(!f) {
  30. fprintf(stderr, "Could not run command '%s'\n", tmp);
  31. exit(1);
  32. return NULL;
  33. }
  34. int len = 2048;
  35. int fill = 0;
  36. char* buffer = malloc(len * sizeof(*buffer));
  37. while(!feof(f)) {
  38. if(fill + 1 >= len) {
  39. len *= 2;
  40. buffer = realloc(buffer, len * sizeof(*buffer));
  41. }
  42. fill += fread(buffer + fill, 1, len - fill - 1, f);
  43. }
  44. buffer[fill] = 0;
  45. pclose(f);
  46. // strip out newlines and other garbage
  47. for(char* c = buffer; *c; c++) if(isspace(*c)) *c = ' ';
  48. return buffer;
  49. }
  50. /* -END- pkgconfig.c ----- */