BASE.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 convertext {
  47. if ($_[0]->isdef($_[1])) { return $_[0]->def($_[1]); }
  48. if ($_[0]->isobj($_[1])) { return $_[0]->obj($_[1]); }
  49. if ($_[0]->isres($_[1])) { return $_[0]->res($_[1]); }
  50. if ($_[0]->isasm($_[1])) { return $_[0]->asm($_[1]); }
  51. return $_[1];
  52. }
  53. # Helpers ############################################################
  54. # __base EXPR, LIST
  55. # This returns the given path (EXPR) with the matching suffix from LIST stripped
  56. sub __base {
  57. my $path = shift;
  58. foreach (@_) {
  59. if ($path =~ m|\Q${_}\E$|) {
  60. return $`;
  61. }
  62. }
  63. return $path;
  64. }
  65. # __isshared EXPR
  66. # EXPR is supposed to be a library name. This will return true if that library
  67. # can be assumed to be a shared library, otherwise false
  68. sub __isshared {
  69. return !($disabled{shared} || $_[0] =~ /\.a$/);
  70. }
  71. # __concat LIST
  72. # Returns the concatenation of all elements of LIST if none of them is
  73. # undefined. If one of them is undefined, returns undef instead.
  74. sub __concat {
  75. my $result = '';
  76. foreach (@_) {
  77. return undef unless defined $_;
  78. $result .= $_;
  79. }
  80. return $result;
  81. }
  82. 1;