PageRenderTime 51ms CodeModel.GetById 32ms app.highlight 2ms RepoModel.GetById 16ms app.codeStats 0ms

/source/core/frontend/BuildParams.ooc

http://github.com/nddrylliog/oc
Unknown | 96 lines | 78 code | 18 blank | 0 comment | 0 complexity | 297352e692e4f9020a5cd2d21f8c89da MD5 | raw file
 1
 2import io/File, os/Env, text/StringTokenizer
 3import structs/[ArrayList, HashMap]
 4
 5import backend/Backend
 6import DynamicLoader
 7
 8BUILD_DATE: extern CString
 9
10BuildParams: class {
11    
12    VERSION := static "0.0"
13    
14    self := ""
15    home := "."
16    verbose := 0
17    dump? := false
18    leftOver: HashMap<String, String>
19    
20    sourcepath := ["."] as ArrayList<String>
21    outpath := "oc_tmp"
22    
23    backend: Backend = null
24    backendString := ""
25    frontendString := ""
26    
27    init: func (map: HashMap<String, String>) {
28        
29        map each(|key, val| match key {
30            case "sourcepath" =>
31                sourcepath = val
32            case "outpath" =>
33                outpath = val
34            case "frontend" =>
35                frontendString = val
36            case "backend" =>
37                backendString = val
38	    case "dump" =>
39		dump? = true
40            case "v" || "verbose" =>
41                verbose += 1
42            case "V" =>
43                "oc v%s - built on %s" printfln(VERSION, BUILD_DATE)
44                exit(0)
45            case "self" =>
46                self = val
47            case =>
48                "Unknown option '%s', DO YOU KNOW THINGS THAT WE DON'T?" printfln(key)
49                leftOver put(key, val)
50        })
51        
52        locateHome()
53        
54        if(backendString == "") {
55            if(verbose > 0) "No backend selected, using C89 backend" println()
56            backendString = "c89"
57        }
58
59        if(frontendString == "") {
60            if(verbose > 0) "No frontend selected, using nagaqueen backend" println()
61            frontendString = "nagaqueen"
62        }
63    }
64    
65    locateHome: func {
66        if(verbose > 0) "Should locate position of oc. Self = %s" printfln(self)
67        selfFile := File new(self)
68        
69        if(selfFile exists?()) {
70            // okay so we have a direct path to the exec - let's back out of bin/
71            guess1 := selfFile getAbsoluteFile() parent() parent()
72            if(verbose > 0) "Guess from direct path is %s" printfln(guess1 path)
73            home = guess1 path
74            return
75        }
76        
77        // hmm let's search the path then
78        path := Env get("PATH")
79        if(path) {
80            path split(File pathDelimiter) each(|folder|
81                if(verbose > 0) "Looking in %s" printfln(folder)
82                // whoever thought of adding '.exe' to executable files wasn't in his right mind -.-
83                f := File new(folder, self)
84                if(!f exists?()) {
85                    f = File new(folder, self + ".exe")
86                    if(!f exists?()) return
87                }
88                
89                guess2 := f getAbsoluteFile() parent() parent()
90                if(verbose > 0) "Guess from binary path is %s" printfln(guess2 path)
91                home = guess2 path
92            )
93        }
94    }
95    
96}