BASE.pm 3.7 KB

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