0 | module Core.Options
  1 |
  2 | import Core.Core
  3 | import public Core.Options.Log
  4 | import Core.TT
  5 |
  6 | import Libraries.Utils.Path
  7 | import Idris.Syntax.Pragmas
  8 |
  9 | import Data.List
 10 | import Data.Maybe
 11 | import Data.String
 12 | import Data.SortedSet
 13 |
 14 | %default total
 15 |
 16 | public export
 17 | record Dirs where
 18 |   constructor MkDirs
 19 |   working_dir : String
 20 |   source_dir : Maybe String -- source directory, relative to working directory
 21 |   build_dir : String -- build directory, relative to working directory
 22 |   depends_dir : String -- local dependencies directory, relative to working directory
 23 |   output_dir : Maybe String -- output directory, relative to working directory
 24 |   prefix_dir : String -- installation prefix, for finding data files (e.g. run time support)
 25 |   extra_dirs : List String -- places to look for import files
 26 |   package_search_paths : List Path -- paths at which to look for packages
 27 |   package_dirs : List String -- places where specific needed packages at required versions are located
 28 |   lib_dirs : List String -- places to look for libraries (for code generation)
 29 |   data_dirs : List String -- places to look for data file
 30 |
 31 | export
 32 | execBuildDir : Dirs -> String
 33 | execBuildDir d = build_dir d </> "exec"
 34 |
 35 | export
 36 | outputDirWithDefault : Dirs -> String
 37 | outputDirWithDefault d = fromMaybe (build_dir d </> "exec") (output_dir d)
 38 |
 39 | public export
 40 | toString : Dirs -> String
 41 | toString d@(MkDirs wdir sdir bdir ldir odir dfix edirs ppaths pdirs ldirs ddirs) = """
 42 |   + Working Directory      :: \{ show wdir }
 43 |   + Source Directory       :: \{ show sdir }
 44 |   + Build Directory        :: \{ show bdir }
 45 |   + Local Depend Directory :: \{ show ldir }
 46 |   + Output Directory       :: \{ show $ outputDirWithDefault d }
 47 |   + Installation Prefix    :: \{ show dfix }
 48 |   + Extra Directories      :: \{ show edirs }
 49 |   + Package Search Paths   :: \{ show ppaths }
 50 |   + Package Directories    :: \{ show pdirs }
 51 |   + CG Library Directories :: \{ show ldirs }
 52 |   + Data Directories       :: \{ show ddirs }
 53 |   """
 54 |
 55 | public export
 56 | data CG = Chez
 57 |         | ChezSep
 58 |         | Racket
 59 |         | Gambit
 60 |         | Node
 61 |         | Javascript
 62 |         | RefC
 63 |         | VMCodeInterp
 64 |         | Other String
 65 |
 66 | export
 67 | Eq CG where
 68 |   Chez == Chez = True
 69 |   ChezSep == ChezSep = True
 70 |   Racket == Racket = True
 71 |   Gambit == Gambit = True
 72 |   Node == Node = True
 73 |   Javascript == Javascript = True
 74 |   RefC == RefC = True
 75 |   VMCodeInterp == VMCodeInterp = True
 76 |   Other s == Other t = s == t
 77 |   _ == _ = False
 78 |
 79 | export
 80 | Ord CG where
 81 |   compare (Other x) (Other y) = compare x y
 82 |   compare x y = compare (constrToInt x) (constrToInt y)
 83 |
 84 |     where
 85 |       constrToInt : CG -> Int
 86 |       constrToInt Chez = 1
 87 |       constrToInt ChezSep = 2
 88 |       constrToInt Racket = 3
 89 |       constrToInt Gambit = 4
 90 |       constrToInt Node = 5
 91 |       constrToInt Javascript = 6
 92 |       constrToInt RefC = 7
 93 |       constrToInt VMCodeInterp = 8
 94 |       constrToInt (Other _) = 9
 95 |
 96 |
 97 | export
 98 | Show CG where
 99 |   show Chez = "chez"
100 |   show ChezSep = "chez-sep"
101 |   show Racket = "racket"
102 |   show Gambit = "gambit"
103 |   show Node = "node"
104 |   show Javascript = "javascript"
105 |   show RefC = "refc"
106 |   show VMCodeInterp = "vmcode-interp"
107 |   show (Other s) = s
108 |
109 | public export
110 | record PairNames where
111 |   constructor MkPairNs
112 |   pairType : Name
113 |   fstName : Name
114 |   sndName : Name
115 |
116 | public export
117 | record RewriteNames where
118 |   constructor MkRewriteNs
119 |   equalType : Name
120 |   rewriteName : Name
121 |
122 | public export
123 | record PrimNames where
124 |   constructor MkPrimNs
125 |   fromIntegerName : Maybe Name
126 |   fromStringName : Maybe Name
127 |   fromCharName : Maybe Name
128 |   fromDoubleName : Maybe Name
129 |   fromTTImpName : Maybe Name
130 |   fromNameName : Maybe Name
131 |   fromDeclsName : Maybe Name
132 |
133 | export
134 | primNamesToList : PrimNames -> List Name
135 | primNamesToList (MkPrimNs i s c d t n dl) = catMaybes [i,s,c,d,t,n,dl]
136 |
137 | -- Other options relevant to the current session (so not to be saved in a TTC)
138 | public export
139 | record ElabDirectives where
140 |   constructor MkElabDirectives
141 |   lazyActive : Bool
142 |   unboundImplicits : Bool
143 |   totality : TotalReq
144 |   ambigLimit : Nat
145 |   autoImplicitLimit : Nat
146 |   nfThreshold : Nat
147 |   totalLimit : Nat
148 |   --
149 |   -- produce traditional (prefix) record projections,
150 |   -- in addition to postfix (dot) projections
151 |   -- default: yes
152 |   prefixRecordProjections : Bool
153 |
154 | ||| Options relevant after running a typechecking session
155 | public export
156 | record PostSession where
157 |   constructor MkPostSession
158 |   checkOnly : Bool
159 |   outputFile : Maybe String
160 |   execExpr : List String
161 |   runRepl : Maybe String
162 |
163 | export
164 | defaultPost : PostSession
165 | defaultPost = MkPostSession
166 |   { checkOnly = False
167 |   , outputFile = Nothing
168 |   , execExpr = []
169 |   , runRepl = Nothing
170 |   }
171 |
172 | -- tag for PostSession
173 | export
174 | data PostS : Type where
175 |
176 | public export
177 | record Session where
178 |   constructor MkSessionOpts
179 |   noprelude : Bool
180 |   totalReq : TotalReq
181 |   nobanner : Bool
182 |   findipkg : Bool
183 |   codegen : CG
184 |   directives : List String
185 |   searchTimeout : Integer -- maximum number of milliseconds to run
186 |                           -- expression/program search
187 |   ignoreMissingPkg : Bool -- fail silently on missing packages. This is because
188 |           -- while we're bootstrapping, we find modules by a different route
189 |           -- but we still want to have the dependencies listed properly
190 |
191 |   -- Troubleshooting
192 |   logEnabled : Bool -- do we check logging flags at all? This is 'False' until
193 |                     -- any logging is enabled.
194 |   logLevel : LogLevels
195 |   logTimings : Maybe Nat -- log level, higher means more details
196 |   debugElabCheck : Bool -- do conversion check to verify results of elaborator
197 |   dumpcases : Maybe String -- file to output compiled case trees
198 |   dumplifted : Maybe String -- file to output lambda lifted definitions
199 |   dumpanf : Maybe String -- file to output ANF definitions
200 |   dumpvmcode : Maybe String -- file to output VM code definitions
201 |   profile : Bool -- generate profiling information, if supported
202 |   logErrorCount : Nat -- when parsing alternatives fails, how many errors
203 |                       -- should be shown.
204 |   noCSE : Bool -- disable common subexpression elimination
205 |
206 |   -- Warnings
207 |   warningsAsErrors : Bool
208 |   showShadowingWarning : Bool
209 |
210 |   -- Experimental
211 |   checkHashesInsteadOfModTime : Bool
212 |   incrementalCGs : List CG
213 |   wholeProgram : Bool
214 |      -- Use whole program compilation for executables, no matter what
215 |      -- incremental CGs are set (intended for overriding any environment
216 |      -- variables that set incremental compilation)
217 |   caseTreeHeuristics : Bool -- apply heuristics to pick matches for case tree building
218 |
219 | public export
220 | record PPrinter where
221 |   constructor MkPPOpts
222 |   showImplicits : Bool
223 |   showMachineNames : Bool
224 |   showFullEnv : Bool
225 |   fullNamespace : Bool
226 |
227 | public export
228 | record Options where
229 |   constructor MkOptions
230 |   dirs : Dirs
231 |   printing : PPrinter
232 |   session : Session
233 |   elabDirectives : ElabDirectives
234 |   pairnames : Maybe PairNames
235 |   rewritenames : Maybe RewriteNames
236 |   primnames : PrimNames
237 |   extensions : List LangExt
238 |   additionalCGs : List (String, CG)
239 |   hashFn : Maybe String
240 |   -- fullName and definition for %foreign_impl
241 |   foreignImpl : SortedSet (Name, String)
242 |
243 | export
244 | availableCGs : Options -> List (String, CG)
245 | availableCGs o
246 |     = [("chez", Chez),
247 |        ("chez-sep", ChezSep),
248 |        ("racket", Racket),
249 |        ("node", Node),
250 |        ("javascript", Javascript),
251 |        ("refc", RefC),
252 |        ("gambit", Gambit),
253 |        ("vmcode-interp", VMCodeInterp)] ++ additionalCGs o
254 |
255 | export
256 | getCG : Options -> String -> Maybe CG
257 | getCG o cg = lookup (toLower cg) (availableCGs o)
258 |
259 | defaultDirs : Dirs
260 | defaultDirs = MkDirs "." Nothing "build" "depends" Nothing
261 |                      "/usr/local" ["."] [] [] [] []
262 |
263 | defaultPPrint : PPrinter
264 | defaultPPrint = MkPPOpts
265 |     { showImplicits = False
266 |     , showMachineNames = False
267 |     , showFullEnv = True
268 |     , fullNamespace = False
269 |     }
270 |
271 | export
272 | docsPPrint : PPrinter
273 | docsPPrint = MkPPOpts
274 |     { showImplicits = False
275 |     , showMachineNames = True
276 |     , showFullEnv = False
277 |     , fullNamespace = False
278 |     }
279 |
280 | export
281 | defaultSession : Session
282 | defaultSession = MkSessionOpts False CoveringOnly False False Chez [] 1000 False False
283 |                                defaultLogLevel Nothing False Nothing Nothing
284 |                                Nothing Nothing False 1 False False True
285 |                                False [] False False
286 |
287 | export
288 | defaultElab : ElabDirectives
289 | defaultElab = MkElabDirectives True True CoveringOnly 3 50 25 5 True
290 |
291 | -- FIXME: This turns out not to be reliably portable, since different systems
292 | -- may have tools with the same name but different required arugments. We
293 | -- probably need another way (perhaps our own internal hash function, although
294 | -- that's not going to be as good as sha256).
295 | export
296 | defaultHashFn : Core (Maybe String)
297 | defaultHashFn
298 |     = do Nothing <- coreLift $ pathLookup ["sha256sum", "gsha256sum"]
299 |            | Just p => pure $ Just $ p ++ " --tag"
300 |          Nothing <- coreLift $ pathLookup ["sha256"]
301 |            | Just p => pure $ Just p
302 |          Nothing <- coreLift $ pathLookup ["openssl"]
303 |            | Just p => pure $ Just $ p ++ " sha256"
304 |          pure Nothing
305 |
306 | export
307 | defaults : Core Options
308 | defaults
309 |     = do -- hashFn <- defaultHashFn
310 |          -- Temporarily disabling the hash function until we have a more
311 |          -- portable way of working out what to call, and allowing a way for
312 |          -- it to fail gracefully.
313 |          pure $ MkOptions
314 |            defaultDirs defaultPPrint defaultSession defaultElab Nothing Nothing
315 |            (MkPrimNs Nothing Nothing Nothing Nothing Nothing Nothing Nothing) [] [] Nothing empty
316 |
317 | -- Reset the options which are set by source files
318 | export
319 | clearNames : Options -> Options
320 | clearNames = { pairnames := Nothing,
321 |                rewritenames := Nothing,
322 |                primnames := MkPrimNs Nothing Nothing Nothing Nothing Nothing Nothing Nothing,
323 |                foreignImpl := empty,
324 |                extensions := []
325 |              }
326 |
327 | export
328 | addForeignImpl : (fullName : Name) -> (def : String) -> Options -> Options
329 | addForeignImpl fullName def = { foreignImpl $= insert (fullName, def) }
330 |
331 | export
332 | setPair : (pairType : Name) -> (fstn : Name) -> (sndn : Name) ->
333 |           Options -> Options
334 | setPair ty f s = { pairnames := Just (MkPairNs ty f s) }
335 |
336 | export
337 | setRewrite : (eq : Name) -> (rwlemma : Name) -> Options -> Options
338 | setRewrite eq rw = { rewritenames := Just (MkRewriteNs eq rw) }
339 |
340 | export
341 | setFromInteger : Name -> Options -> Options
342 | setFromInteger n = { primnames->fromIntegerName := Just n }
343 |
344 | export
345 | setFromString : Name -> Options -> Options
346 | setFromString n = { primnames->fromStringName := Just n }
347 |
348 | export
349 | setFromChar : Name -> Options -> Options
350 | setFromChar n = { primnames->fromCharName := Just n }
351 |
352 | export
353 | setFromDouble : Name -> Options -> Options
354 | setFromDouble n = { primnames->fromDoubleName := Just n }
355 |
356 | export
357 | setFromTTImp : Name -> Options -> Options
358 | setFromTTImp n = { primnames->fromTTImpName := Just n }
359 |
360 | export
361 | setFromName : Name -> Options -> Options
362 | setFromName n = { primnames->fromNameName := Just n }
363 |
364 | export
365 | setFromDecls : Name -> Options -> Options
366 | setFromDecls n = { primnames->fromDeclsName := Just n }
367 |
368 | export
369 | setExtension : LangExt -> Options -> Options
370 | setExtension e = { extensions $= (e ::) }
371 |
372 | export
373 | isExtension : LangExt -> Options -> Bool
374 | isExtension e opts = e `elem` extensions opts
375 |
376 | export
377 | addCG : (String, CG) -> Options -> Options
378 | addCG cg = { additionalCGs $= (cg::) }
379 |