2
0

BASE.pm 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package platform::BASE;
  2. use strict;
  3. use warnings;
  4. use Carp;
  5. # Assume someone set @INC right before loading this module
  6. use configdata;
  7. # Globally defined "platform specific" extensions, available for uniformity
  8. sub depext { '.d' }
  9. # Functions to convert internal file representations to platform specific
  10. # ones. Note that these all depend on extension functions that MUST be
  11. # defined per platform.
  12. #
  13. # Currently known internal or semi-internal extensions are:
  14. #
  15. # .a For libraries that are made static only.
  16. # Internal libraries only.
  17. # .o For object files.
  18. # .s, .S Assembler files. This is an actual extension on Unix
  19. # .res Resource file. This is an actual extension on Windows
  20. sub binname { return $_[1] } # Name of executable binary
  21. sub dsoname { return $_[1] } # Name of dynamic shared object (DSO)
  22. sub sharedname { return __isshared($_[1]) ? $_[1] : undef } # Name of shared lib
  23. sub staticname { return __base($_[1], '.a') } # Name of static lib
  24. # Convenience function to convert the shlib version to an acceptable part
  25. # of a file or directory name. By default, we consider it acceptable as is.
  26. sub shlib_version_as_filename { return $config{shlib_version} }
  27. # Convenience functions to convert the possible extension of an input file name
  28. sub bin { return $_[0]->binname($_[1]) . $_[0]->binext() }
  29. sub dso { return $_[0]->dsoname($_[1]) . $_[0]->dsoext() }
  30. sub sharedlib { return __concat($_[0]->sharedname($_[1]), $_[0]->shlibext()) }
  31. sub staticlib { return $_[0]->staticname($_[1]) . $_[0]->libext() }
  32. # More convenience functions for intermediary files
  33. sub def { return __base($_[1], '.ld') . $_[0]->defext() }
  34. sub obj { return __base($_[1], '.o') . $_[0]->objext() }
  35. sub res { return __base($_[1], '.res') . $_[0]->resext() }
  36. sub dep { return __base($_[1], '.o') . $_[0]->depext() } # <- objname
  37. sub asm { return __base($_[1], '.S', '.s') . $_[0]->asmext() }
  38. # Another set of convenience functions for standard checks of certain
  39. # internal extensions and conversion from internal to platform specific
  40. # extension. Note that the latter doesn't deal with libraries because
  41. # of ambivalence
  42. sub isdef { return $_[1] =~ m|\.ld$|; }
  43. sub isobj { return $_[1] =~ m|\.o$|; }
  44. sub isres { return $_[1] =~ m|\.res$|; }
  45. sub isasm { return $_[1] =~ m|\.[Ss]$|; }
  46. sub isstaticlib { return $_[1] =~ m|\.a$|; }
  47. sub convertext {
  48. if ($_[0]->isdef($_[1])) { return $_[0]->def($_[1]); }
  49. if ($_[0]->isobj($_[1])) { return $_[0]->obj($_[1]); }
  50. if ($_[0]->isres($_[1])) { return $_[0]->res($_[1]); }
  51. if ($_[0]->isasm($_[1])) { return $_[0]->asm($_[1]); }
  52. if ($_[0]->isstaticlib($_[1])) { return $_[0]->staticlib($_[1]); }
  53. return $_[1];
  54. }
  55. # Helpers ############################################################
  56. # __base EXPR, LIST
  57. # This returns the given path (EXPR) with the matching suffix from LIST stripped
  58. sub __base {
  59. my $path = shift;
  60. foreach (@_) {
  61. if ($path =~ m|\Q${_}\E$|) {
  62. return $`;
  63. }
  64. }
  65. return $path;
  66. }
  67. # __isshared EXPR
  68. # EXPR is supposed to be a library name. This will return true if that library
  69. # can be assumed to be a shared library, otherwise false
  70. sub __isshared {
  71. return !($disabled{shared} || $_[0] =~ /\.a$/);
  72. }
  73. # __concat LIST
  74. # Returns the concatenation of all elements of LIST if none of them is
  75. # undefined. If one of them is undefined, returns undef instead.
  76. sub __concat {
  77. my $result = '';
  78. foreach (@_) {
  79. return undef unless defined $_;
  80. $result .= $_;
  81. }
  82. return $result;
  83. }
  84. 1;