/red-system/rsc.r

http://github.com/dockimbel/Red · R · 156 lines · 135 code · 20 blank · 1 comment · 8 complexity · 0f1d5d71225412940ce0ece0831af781 MD5 · raw file

  1. REBOL [
  2. Title: "Red/System compiler wrapper"
  3. Author: "Nenad Rakocevic, Andreas Bolka"
  4. File: %rsc.r
  5. Rights: "Copyright (C) 2011 Nenad Rakocevic, Andreas Bolka. All rights reserved."
  6. License: "BSD-3 - https://github.com/dockimbel/Red/blob/master/BSD-3-License.txt"
  7. Usage: {
  8. do/args %rsc.r "[-v <integer!>] path/source.reds"
  9. }
  10. ]
  11. unless value? 'system-dialect [
  12. do %compiler.r
  13. ]
  14. rsc: context [
  15. fail: func [value] [
  16. print value
  17. if system/options/args [quit/return 1]
  18. halt
  19. ]
  20. fail-try: func [component body /local err] [
  21. if error? set/any 'err try body [
  22. err: disarm err
  23. foreach w [arg1 arg2 arg3][
  24. set w either unset? get/any in err w [none][
  25. get/any in err w
  26. ]
  27. ]
  28. fail [
  29. "***" component "Internal Error:"
  30. system/error/(err/type)/type #":"
  31. reduce system/error/(err/type)/(err/id) newline
  32. "*** Where:" mold/flat err/where newline
  33. "*** Near: " mold/flat err/near newline
  34. ]
  35. ]
  36. ]
  37. load-filename: func [filename /local result] [
  38. unless any [
  39. all [
  40. #"%" = first filename
  41. attempt [result: load filename]
  42. file? result
  43. ]
  44. attempt [result: to-rebol-file filename]
  45. ] [
  46. fail ["Invalid filename:" filename]
  47. ]
  48. result
  49. ]
  50. load-targets: func [/local targets] [
  51. targets: load %config.r
  52. if exists? %custom-targets.r [
  53. insert targets load %custom-targets.r
  54. ]
  55. targets
  56. ]
  57. parse-options: has [
  58. args srcs opts output target verbose filename config config-name
  59. ] [
  60. args: any [system/options/args parse any [system/script/args ""] none]
  61. ;; Select a default target based on the REBOL version.
  62. target: any [
  63. select [
  64. 2 "Darwin"
  65. 3 "MSDOS"
  66. 4 "Linux"
  67. ] system/version/4
  68. "MSDOS"
  69. ]
  70. srcs: copy []
  71. opts: make system-dialect/options-class [link?: yes]
  72. parse args [
  73. any [
  74. ["-r" | "--no-runtime"] (opts/runtime?: no)
  75. | ["-g" | "--debug-stabs"] (opts/debug?: yes)
  76. | ["-l" | "--literal-pool"] (opts/literal-pool?: yes)
  77. | ["-o" | "--output"] set output skip
  78. | ["-t" | "--target"] set target skip
  79. | ["-v" | "--verbose"] set verbose skip
  80. | set filename skip (append srcs load-filename filename)
  81. ]
  82. ]
  83. ;; Process -t/--target first, so that all other command-line options
  84. ;; can potentially override the target config settings.
  85. unless config: select load-targets config-name: to word! trim target [
  86. fail ["Unknown target:" target]
  87. ]
  88. opts: make opts config
  89. opts/config-name: config-name
  90. ;; Process -o/--output (if any).
  91. if output [
  92. opts/build-prefix: %""
  93. opts/build-basename: load-filename output
  94. ]
  95. ;; Process -v/--verbose (if any).
  96. if verbose [
  97. unless attempt [opts/verbosity: to integer! trim verbose] [
  98. fail ["Invalid verbosity:" verbose]
  99. ]
  100. ]
  101. ;; Process input sources.
  102. if empty? srcs [fail "No source files specified."]
  103. foreach src srcs [
  104. unless exists? src [
  105. fail ["Cannot access source file:" src]
  106. ]
  107. ]
  108. reduce [srcs opts]
  109. ]
  110. main: has [srcs opts build-dir result] [
  111. set [srcs opts] parse-options
  112. ;; If we use a build directory, ensure it exists.
  113. if all [opts/build-prefix find opts/build-prefix %/] [
  114. build-dir: copy/part opts/build-prefix find/last opts/build-prefix %/
  115. unless attempt [make-dir/deep build-dir] [
  116. fail ["Cannot access build dir:" build-dir]
  117. ]
  118. ]
  119. print [
  120. newline
  121. "-= Red/System Compiler =-" newline
  122. "Compiling" srcs "..."
  123. ]
  124. fail-try "Compiler" [
  125. result: system-dialect/compile/options srcs opts
  126. ]
  127. print ["^/...compilation time:" tab round result/1/second * 1000 "ms"]
  128. if result/2 [
  129. print [
  130. "...linking time:" tab tab round result/2/second * 1000 "ms^/"
  131. "...output file size:" tab result/3 "bytes"
  132. ]
  133. ]
  134. ]
  135. fail-try "Driver" [main]
  136. ]