7d98bdf84b7e7105d35dfc0253a9df0e355e5f42
3 IMPORT Kernel
, Files
, Log
, Strings
, DswOpts
, DswProcs
, DswDocuments
, DevCPM
, DevCPT
, DevCPR
, DevCPS
;
15 null
= 0; times
= 1; slash
= 2; div
= 3; mod
= 4;
16 and
= 5; plus
= 6; minus
= 7; or
= 8; eql
= 9;
17 neq
= 10; lss
= 11; leq
= 12; gtr
= 13; geq
= 14;
18 in
= 15; is
= 16; arrow
= 17; dollar
= 18; period
= 19;
19 comma
= 20; colon
= 21; upto
= 22; rparen
= 23; rbrak
= 24;
20 rbrace
= 25; of
= 26; then
= 27; do
= 28; to
= 29;
22 lparen
= 40; lbrak
= 41; lbrace
= 42; becomes
= 44;
23 number
= 45; nil
= 46; string
= 47; ident
= 48; semicolon
= 49;
24 bar
= 50; end
= 51; else
= 52; elsif
= 53; until
= 54;
25 if
= 55; case
= 56; while
= 57; repeat
= 58; for
= 59;
26 loop
= 60; with
= 61; exit
= 62; return
= 63; array
= 64;
27 record
= 65; pointer
= 66; begin
= 67; const
= 68; type
= 69;
28 var
= 70; out
= 71; procedure
= 72; close
= 73; import
= 74;
29 module
= 75; eof
= 76;
31 (* module state flags *)
32 imported
= 0; trace
= 1; hasObj
= 2; hasSym
= 3; hasErrors
= 4;
33 library
= 5; force
= 6;
40 String
= POINTER TO ARRAY OF CHAR;
42 Selector
= POINTER TO RECORD
48 Module
= POINTER TO RECORD
51 depth
: INTEGER; (* 0: leaf, MAX: root *)
52 dir
: DevCPM
.Directory
;
54 imp
: ARRAY maxImps
OF Module
;
56 worker
: DswProcs
.Process
;
60 auto
, trap
, clean
, symonly
: BOOLEAN;
61 exe
, target
, base
: String
;
66 mno
, rno
: INTEGER; (* num modules *)
67 modList
, lnkList
, cmpList
: ARRAY maxImps
OF Module
;
68 def
: Selector
; (* with head, global list of selectors *)
69 dirList
: DevCPM
.Directory
;
71 PROCEDURE Error (IN str
, p0
, p1
: ARRAY OF CHAR; i2
: INTEGER);
72 VAR p2
: ARRAY 32 OF CHAR;
74 Strings
.IntToString(i2
, p2
);
75 Log
.String("cpmake: "); Log
.ParamMsg(str
, p0
, p1
, p2
); Log
.Ln
;
82 (* --------- options --------- *)
84 PROCEDURE IdentLen (IN s
: ARRAY OF CHAR): INTEGER;
88 IF Strings
.IsIdentStart(s
[0]) THEN
89 REPEAT INC(i
) UNTIL ~Strings
.IsIdent(s
[i
])
94 PROCEDURE Define (n
: ARRAY OF CHAR; overwrite
: BOOLEAN);
95 VAR i
: INTEGER; v
: BOOLEAN; s
: Selector
;
99 IF ~
((n
[i
] = 0X
) OR (((n
[i
] = "+") OR (n
[i
] = "-")) & (n
[i
+ 1] = 0X
))) THEN
100 Error("defines: expectd + or - after identifier (^0)", n
, "", 0)
102 v
:= n
[i
] # "-"; n
[i
] := 0X
; s
:= def
;
103 WHILE (s
.next
# NIL) & (s
.next
.name$
# n$
) DO s
:= s
.next
END;
106 Strings
.StringToUtf8(n
, s
.next
.name
, i
);
112 ELSE Error("defines: '^0' not valid identifier", n
, "", 0)
116 PROCEDURE AddModule (IN n
: ARRAY OF CHAR; selectors
: Selector
; dir
: DevCPM
.Directory
);
117 VAR i
, res
: INTEGER; m
: Module
;
120 IF (i
# 0) & (n
[i
] = 0X
) THEN
122 WHILE (i
< mno
) & (modList
[i
].name$
# n$
) DO INC(i
) END;
125 Strings
.StringToUtf8(n
, m
.name
, res
);
132 Error("invalid module name", "", "", 0)
136 PROCEDURE StrToInt (IN s
: ARRAY OF CHAR; def
: INTEGER): INTEGER;
139 Strings
.StringToInt(s
, x
, res
);
141 Error("invalid integer value", s
, "", 0);
147 PROCEDURE NewStr (IN s
: ARRAY OF CHAR): String
;
150 NEW(p
, LEN(s$
) + 1); p^
:= s$
;
156 Log
.String("Usage: cpmake [options] module..."); Log
.Ln
;
157 Log
.String("Options:"); Log
.Ln
;
158 Log
.String(" -a Enable automatic dependency resolution"); Log
.Ln
;
159 Log
.String(" -c Remove all generated files"); Log
.Ln
;
160 Log
.String(" -s Generate symbol files only"); Log
.Ln
;
161 Log
.String(" -t target Specify target rules"); Log
.Ln
;
162 Log
.String(" -o file Generate object file"); Log
.Ln
;
163 Log
.String(" -j num Specifies the number of jobs to run simultaneously"); Log
.Ln
;
164 Log
.String(" -f path Specifies path to directory with targets"); Log
.Ln
;
165 Log
.String(" -h Print help"); Log
.Ln
;
166 Log
.String(" -v Print version"); Log
.Ln
;
172 exe
:= NIL; auto
:= FALSE
; jobs
:= 1; def
.next
:= NIL; mno
:= 0; rno
:= 0;
173 target
:= NewStr("default"); base
:= NewStr("cprules");
175 CASE DswOpts
.GetOpt("acdsgo:t:j:f:") OF
179 |
"s": symonly
:= TRUE
180 |
"f": base
:= DswOpts
.str
182 |
"j": jobs
:= MIN(MAX(StrToInt(DswOpts
.str
, 1), 1), maxJobs
)
183 |
"o": exe
:= DswOpts
.str
184 |
"t": target
:= DswOpts
.str
185 |
"v": Log
.String(version
); Log
.Ln
; Kernel
.Quit(0)
186 |
":": Error("missing argument for option -^0", DswOpts
.str
, "", 0)
187 |
"?": Error("unknown option -^0", DswOpts
.str
, "", 0)
188 |
"$": AddModule(DswOpts
.str
, def
, dirList
)
194 PROCEDURE ReadDefines
;
195 VAR loc
: Files
.Locator
; name
: Files
.Name
; m
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
197 loc
:= Files
.dir
.This(base
).This(target
);
198 DswDocuments
.Open(loc
, "defines", m
, res
);
200 r
:= m
.NewReader(NIL);
205 WHILE ~r
.eot
& (r
.char
<= 20X
) DO r
.Read
END;
206 WHILE ~r
.eot
& (r
.char
> 20X
) DO name
[i
] := r
.char
; r
.Read
; INC(i
) END;
207 IF i
# 0 THEN name
[i
] := 0X
; Define(name
, FALSE
) END
212 PROCEDURE CheckParams
;
214 IF (exe
# NIL) & (exe^
= "") THEN
215 Error("specified empty file name for exe", "", "", 0)
218 Error("specified empty rule", "", "", 0)
221 Error("specified empty path to cpmake rules", "", "", 0)
226 (* --------- loader --------- *)
228 PROCEDURE Import (m
: Module
; IN name
: DevCPT
.Name
);
229 VAR i
, j
: INTEGER; imp
: Module
;
232 ASSERT(name
# "", 21);
233 IF debugImport
THEN Log
.String(" import "); Log
.String(name$
) END;
234 IF name
= "SYSTEM" THEN INCL(DevCPM
.options
, DevCPM
.sysImp
)
235 ELSIF name
= "COM" THEN INCL(DevCPM
.options
, DevCPM
.com
)
236 ELSIF name
= "JAVA" THEN INCL(DevCPM
.options
, DevCPM
.java
)
238 IF debugImport
THEN Log
.Char(" ") END;
239 i
:= 0; (* find module in local list *)
240 WHILE (i
< m
.mno
) & (m
.imp
[i
].name$
# name$
) DO INC(i
) END;
242 j
:= 0; (* find module in global list *)
243 WHILE (j
< mno
) & (modList
[j
].name$
# name$
) DO INC(j
) END;
246 Log
.String("module " + name
+ " required before " + m
.name
); Log
.Ln
; INC(werr
)
248 NEW(imp
); imp
.name
:= name$
;
249 modList
[mno
] := imp
; INC(mno
)
253 m
.imp
[m
.mno
] := imp
; INC(m
.mno
)
257 IF debugImport
THEN Log
.Ln
END;
260 PROCEDURE ParseModule (m
: Module
);
261 VAR sym
: BYTE; SelfName
, impName
, aliasName
: DevCPT
.Name
;
263 PROCEDURE err (n
: SHORTINT);
267 PROCEDURE CheckSym(s
: SHORTINT);
269 IF sym
= s
THEN DevCPS
.Get(sym
) ELSE DevCPM
.err(s
) END
273 IF debugImport
THEN Log
.String("module " + m
.name
); Log
.Ln
END;
274 DevCPS
.Init
; DevCPS
.Get(sym
);
275 IF sym
= module
THEN DevCPS
.Get(sym
) ELSE err(16) END;
277 SelfName
:= DevCPS
.name$
; DevCPS
.Get(sym
);
279 INCL(DevCPM
.options
, DevCPM
.interface
); DevCPS
.Get(sym
);
280 IF sym
= eql
THEN DevCPS
.Get(sym
)
281 ELSE INCL(DevCPM
.options
, DevCPM
.noCode
)
283 IF sym
= string
THEN INCL(m
.flags
, library
); DevCPS
.Get(sym
)
289 IF sym
= import
THEN DevCPS
.Get(sym
);
292 aliasName
:= DevCPS
.name$
; impName
:= aliasName$
; DevCPS
.Get(sym
);
293 IF sym
= becomes
THEN DevCPS
.Get(sym
);
294 IF sym
= ident
THEN impName
:= DevCPS
.name$
; DevCPS
.Get(sym
) ELSE err(ident
) END
299 IF sym
= comma
THEN DevCPS
.Get(sym
)
300 ELSIF sym
= ident
THEN err(comma
)
306 LOOP (* preprocessor must read module fully *)
312 IF DevCPS
.name
# SelfName
THEN err(4) END;
334 PROCEDURE CheckModule (m
: Module
; source
: String
; OUT ok
: BOOLEAN);
338 DevCPM
.symList
:= m
.dir
;
339 (*DevCPM.codePath := m.outcode;*)
340 (*DevCPM.symPath := m.outsym;*)
341 DevCPM
.name
:= m
.name$
;
342 INCL(DevCPM
.options
, DevCPM
.comAware
);
344 INCL(DevCPM
.options
, DevCPM
.trap
)
346 (*IF oberon IN m.opts THEN INCL(DevCPM.options, DevCPM.oberon) END;*)
350 DevCPR
.Set(s
.name
, s
.value
);
356 IF DevCPR
.used
THEN INCL(m
.flags
, force
) END;
363 PROCEDURE MakePath (IN dir
, name
: Files
.Name
; IN type
: Files
.Type
; OUT path
: Files
.Name
);
365 ASSERT(name
# "", 21);
366 IF dir
= "" THEN path
:= modDir
+ "/" + name
367 ELSE path
:= dir
+ "/" + modDir
+ "/" + name
369 Kernel
.MakeFileName(path
, type
)
372 PROCEDURE Open (loc
: Files
.Locator
; IN sub
, name
: Files
.Name
; OUT path
: Files
.Name
; OUT text
: DswDocuments
.Model
);
375 ASSERT(loc
# NIL, 20);
376 ASSERT(name
# "", 21);
377 (* !!! use Kernel.MakeFileName instead ".ext" concat !!! *)
378 MakePath(sub
, name
, "cp", path
);
379 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
381 MakePath(sub
, name
, "odc", path
);
382 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
383 IF (text
= NIL) & (sub
= "") THEN
384 MakePath(sysDir
, name
, "cp", path
);
385 loc
:= Files
.dir
.This(sysDir
).This(modDir
);
386 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
388 MakePath(sysDir
, name
, "odc", path
);
389 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
398 PROCEDURE GetSource (IN modName
: ARRAY OF CHAR; list
: DevCPM
.Directory
; OUT path
: Files
.Name
; OUT s
: String
);
400 sub
, name
: Files
.Name
; loc
: Files
.Locator
; base
: DevCPM
.Directory
;
401 text
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
403 s
:= NIL; path
:= ""; base
:= list
;
404 Kernel
.SplitName(modName
, sub
, name
);
405 loc
:= Files
.dir
.This(sub
).This(modDir
);
406 Open(loc
, sub
, name
, path
, text
);
407 WHILE (text
= NIL) & (base
# NIL) DO
408 ASSERT(base
.legacy
, 100);
409 loc
:= Files
.dir
.This(base
.path
).This(sub
).This(modDir
);
410 Open(loc
, sub
, name
, path
, text
);
414 NEW(s
, text
.Length() + 1);
416 r
:= text
.NewReader(NIL);
417 FOR i
:= 0 TO text
.Length() - 1 DO
418 r
.Read
; s
[i
] := r
.char
424 PROCEDURE Trace (m
, parent
: Module
; VAR lno
: INTEGER);
427 IF ~
(trace
IN m
.flags
) THEN
428 INCL(m
.flags
, trace
);
429 FOR i
:= 0 TO m
.mno
- 1 DO
430 Trace(m
.imp
[i
], m
, lno
);
431 m
.depth
:= MAX(m
.depth
, m
.imp
[i
].depth
+ 1)
433 IF ~
(imported
IN m
.flags
) THEN
434 INCL(m
.flags
, imported
);
440 Error("recursive import of ^0 in ^1", m
.name$
, parent
.name$
, 0)
445 VAR i
, j
: INTEGER; m
: Module
;
447 ASSERT((mno
= 0) OR (lnkList
[0] # NIL), 20);
453 WHILE (j
>= 0) & (cmpList
[j
].depth
> m
.depth
) DO
454 cmpList
[j
+ 1] := cmpList
[j
];
463 VAR i
, j
, num
: INTEGER; m
: Module
; src
: String
; ok
: BOOLEAN;
466 WHILE (err
= 0) & (i
< mno
) DO
468 GetSource(m
.name$
, m
.dir
, m
.odc
, src
);
470 CheckModule(m
, src
, ok
);
471 IF ~ok
THEN INC(err
) END
473 Error("unable to open module ^1", m
.name$
, "", 0)
479 FOR i
:= 0 TO rno
- 1 DO
480 Trace(modList
[i
], modList
[i
], num
)
482 ASSERT((err
# 0) OR (num
= mno
), 100);
485 Log
.String("Parallel depth:"); Log
.Ln
;
486 FOR i
:= 0 TO mno
- 1 DO
487 Log
.String(" " + cmpList
[i
].name
); Log
.Int(cmpList
[i
].depth
); Log
.Ln
;
492 PROCEDURE IsCompiled (m
: Module
): BOOLEAN;
493 CONST target
= {hasSym
, hasObj
};
494 VAR i
: INTEGER; ready
: BOOLEAN;
498 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
= target
);
499 WHILE ready
& (i
< m
.mno
) DO
500 ready
:= IsCompiled(m
.imp
[i
]);
506 PROCEDURE Ready (m
: Module
): BOOLEAN;
507 CONST target
= {hasSym
, hasObj
};
508 VAR i
: INTEGER; ready
: BOOLEAN;
511 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
# target
) & (m
.worker
= NIL);
512 WHILE ready
& (i
< m
.mno
) DO
513 ready
:= IsCompiled(m
.imp
[i
]);
519 PROCEDURE PrepareCompiler (m
: Module
): DswProcs
.Process
;
520 VAR p
: DswProcs
.Process
; s
: Selector
;
523 ASSERT(m
.odc
# "", 21);
524 ASSERT(m
.worker
= NIL, 22);
525 p
:= DswProcs
.dir
.New();
526 p
.Program(base
+ "/" + target
+ "/" + "build");
528 IF force
IN m
.flags
THEN p
.PutParam("-f") END;
529 IF symonly
OR (library
IN m
.flags
) THEN p
.PutParam("-s") END;
532 IF s
.value
THEN p
.PutParam("-D") ELSE p
.PutParam("-d") END;
540 VAR i
, j
, num
, res
: INTEGER; ok
: BOOLEAN; m
: Module
; w
: DswProcs
.Process
;
542 IF mno
= 0 THEN RETURN END;
544 WHILE (err
= 0) & (num
< mno
) OR (j
> 0) DO
546 WHILE (err
= 0) & (i
< mno
) & (j
< jobs
) DO
549 w
:= PrepareCompiler(m
);
550 IF debugJobs
THEN Log
.String("Start job " + m
.name
) END;
553 IF debugJobs
THEN Log
.String(" ok") END;
557 IF debugJobs
THEN Log
.String(" fail") END;
558 INCL(m
.flags
, hasErrors
);
561 IF debugJobs
THEN Log
.Ln
END
565 WHILE (err
= 0) & (j
>= jobs
) OR (j
> 0) DO
567 WHILE (j
> 0) & (i
< mno
) DO
570 IF (w
# NIL) & w
.IsTerminated() THEN
572 IF debugJobs
THEN Log
.String("Stop job " + m
.name
); Log
.Int(res
); Log
.Ln
END;
574 INCL(m
.flags
, hasObj
);
575 INCL(m
.flags
, hasSym
);
578 INCL(m
.flags
, hasErrors
);
591 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; i
, res
: INTEGER;
593 IF ~symonly
& (exe
# NIL) THEN
594 p
:= DswProcs
.dir
.New();
595 p
.Program(base
+ "/" + target
+ "/" + "link");
597 FOR i
:= 0 TO mno
- 1 DO
598 IF ~
(library
IN lnkList
[i
].flags
) THEN
599 p
.PutParam(lnkList
[i
].name$
)
607 Error("link failed with code ^2", "", "", res
)
609 ELSE Error("link failed: unable to execute script", "", "", 0)
615 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; res
: INTEGER;
618 p
:= DswProcs
.dir
.New();
619 p
.Program(base
+ "/" + target
+ "/" + "clean");
625 Error("cleanup failed with code ^2", "", "", res
)
627 ELSE Error("cleanup failed: unable to execute script", "", "", 0)
634 IF Kernel
.trapCount
= 0 THEN
651 ELSE Error("trap occured", "", "", 0)
653 IF err
= 0 THEN Kernel
.Quit(0)
660 ASSERT(def
# NIL, 100);
661 Kernel
.intTrap
:= FALSE
;