target.scm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ;;; Compilation targets
  2. ;; Copyright (C) 2011 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system base target)
  19. #:use-module (rnrs bytevectors)
  20. #:export (target-type with-target
  21. target-cpu target-vendor target-os
  22. target-endianness target-word-size))
  23. ;;;
  24. ;;; Target types
  25. ;;;
  26. (define %target-type (make-fluid))
  27. (define (target-type)
  28. (or (fluid-ref %target-type)
  29. %host-type))
  30. (define (validate-target target)
  31. (if (or (not (string? target))
  32. (let ((parts (string-split target #\-)))
  33. (or (< 3 (length parts))
  34. (or-map string-null? parts))))
  35. (error "invalid target" target)))
  36. (define (with-target target thunk)
  37. (validate-target target)
  38. (with-fluids ((%target-type target))
  39. (thunk)))
  40. (define (target-cpu)
  41. (let ((t (target-type)))
  42. (substring t 0 (string-index t #\-))))
  43. (define (target-vendor)
  44. (let* ((t (target-type))
  45. (start (1+ (string-index t #\-))))
  46. (substring t start (string-index t #\- start))))
  47. (define (target-os)
  48. (let* ((t (target-type))
  49. (start (1+ (string-index t #\- (1+ (string-index t #\-))))))
  50. (substring t start)))
  51. (define (target-endianness)
  52. (if (equal? (target-type) %host-type)
  53. (native-endianness)
  54. (error "cross-compilation not yet handled" %host-type (target-type))))
  55. (define (target-word-size)
  56. (if (equal? (target-type) %host-type)
  57. ((@ (system foreign) sizeof) '*)
  58. (error "cross-compilation not yet handled" %host-type (target-type))))