Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #define TTL_GENERATOR_WINDOWS
  22. #else
  23. #include <dlfcn.h>
  24. #endif
  25. #ifndef nullptr
  26. #define nullptr (0)
  27. #endif
  28. typedef void (*TTL_Generator_Function)(const char* basename);
  29. static int isPathSeparator(char c);
  30. static char* makeNormalPath(const char* path);
  31. // TODO support Unicode paths on the Windows platform
  32. int main(int argc, char* argv[])
  33. {
  34. if (argc != 2)
  35. {
  36. printf("usage: %s /path/to/plugin-DLL\n", argv[0]);
  37. return 1;
  38. }
  39. const char* path = argv[1];
  40. #ifdef TTL_GENERATOR_WINDOWS
  41. const HMODULE handle = LoadLibraryA(path);
  42. #else
  43. void* const handle = dlopen(path, RTLD_LAZY);
  44. #endif
  45. if (! handle)
  46. {
  47. #ifdef TTL_GENERATOR_WINDOWS
  48. printf("Failed to open plugin DLL\n");
  49. #else
  50. printf("Failed to open plugin DLL, error was:\n%s\n", dlerror());
  51. #endif
  52. return 2;
  53. }
  54. #ifdef TTL_GENERATOR_WINDOWS
  55. # if defined(__GNUC__) && (__GNUC__ >= 9)
  56. # pragma GCC diagnostic push
  57. # pragma GCC diagnostic ignored "-Wcast-function-type"
  58. # endif
  59. const TTL_Generator_Function ttlFn = (TTL_Generator_Function)GetProcAddress(handle, "lv2_generate_ttl");
  60. # if defined(__GNUC__) && (__GNUC__ >= 9)
  61. # pragma GCC diagnostic pop
  62. # endif
  63. #else
  64. const TTL_Generator_Function ttlFn = (TTL_Generator_Function)dlsym(handle, "lv2_generate_ttl");
  65. #endif
  66. if (ttlFn != NULL)
  67. {
  68. // convert the paths to a normalized form, such that path separators are
  69. // replaced with '/', and duplicate separators are removed
  70. char* normalPath = makeNormalPath(path);
  71. // get rid of any "./" prefixes
  72. path = normalPath;
  73. while (path[0] == '.' && path[1] == '/')
  74. path += 2;
  75. // extract the file name part
  76. char* basename = strrchr(path, '/');
  77. if (basename != NULL)
  78. basename += 1;
  79. else
  80. basename = (char*)path;
  81. // remove the file extension
  82. char* dotPos = strrchr(basename, '.');
  83. if (dotPos)
  84. *dotPos = '\0';
  85. printf("Generate ttl data for '%s', basename: '%s'\n", path, basename);
  86. ttlFn(basename);
  87. free(normalPath);
  88. }
  89. else
  90. printf("Failed to find 'lv2_generate_ttl' function\n");
  91. #ifdef TTL_GENERATOR_WINDOWS
  92. FreeLibrary(handle);
  93. #else
  94. dlclose(handle);
  95. #endif
  96. return 0;
  97. }
  98. static int isPathSeparator(char c)
  99. {
  100. #ifdef TTL_GENERATOR_WINDOWS
  101. return c == '/' || c == '\\';
  102. #else
  103. return c == '/';
  104. #endif
  105. }
  106. static char* makeNormalPath(const char* path)
  107. {
  108. size_t i, j;
  109. size_t len = strlen(path);
  110. char* result = (char*)malloc(len + 1);
  111. int isSep, wasSep = 0;
  112. for (i = 0, j = 0; i < len; ++i)
  113. {
  114. isSep = isPathSeparator(path[i]);
  115. if (!isSep)
  116. result[j++] = path[i];
  117. else if (!wasSep)
  118. result[j++] = '/';
  119. wasSep = isSep;
  120. }
  121. result[j] = '\0';
  122. return result;
  123. }