acf9619195372ae79e6b78374795cf60e75dcd5e
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
, nocode1
, nocode2
: 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("expectd + or - after selector 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("selector '^0' have invalid 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(" -b Do not compile modules"); Log
.Ln
;
160 Log
.String(" -x Do not link objects"); Log
.Ln
;
161 Log
.String(" -c Remove all generated files"); Log
.Ln
;
162 Log
.String(" -s Generate symbol files only"); Log
.Ln
;
163 Log
.String(" -d selector Add selector"); Log
.Ln
;
164 Log
.String(" -t target Specify target rules"); Log
.Ln
;
165 Log
.String(" -o file Generate object file"); Log
.Ln
;
166 Log
.String(" -j num Specifies the number of jobs to run simultaneously"); Log
.Ln
;
167 Log
.String(" -f path Specifies path to directory with targets"); Log
.Ln
;
168 Log
.String(" -h Print help"); Log
.Ln
;
169 Log
.String(" -v Print version"); Log
.Ln
;
175 exe
:= NIL; auto
:= FALSE
; jobs
:= 1; def
.next
:= NIL; mno
:= 0; rno
:= 0;
176 target
:= NewStr("default"); base
:= NewStr("cprules");
178 CASE DswOpts
.GetOpt("acbxd:sgGo:t:j:f:") OF
180 |
"b": nocode1
:= TRUE
181 |
"x": nocode2
:= TRUE
184 |
"G": Kernel
.intTrap
:= TRUE
185 |
"s": symonly
:= TRUE
186 |
"f": base
:= DswOpts
.str
187 |
"d": Define(DswOpts
.str
, TRUE
)
189 |
"j": jobs
:= MIN(MAX(StrToInt(DswOpts
.str
, 1), 1), maxJobs
)
190 |
"o": exe
:= DswOpts
.str
191 |
"t": target
:= DswOpts
.str
192 |
"v": Log
.String(version
); Log
.Ln
; Kernel
.Quit(0)
193 |
":": Error("missing argument for option -^0", DswOpts
.str
, "", 0)
194 |
"?": Error("unknown option -^0", DswOpts
.str
, "", 0)
195 |
"$": AddModule(DswOpts
.str
, def
, dirList
)
201 PROCEDURE ReadDefines
;
202 VAR loc
: Files
.Locator
; name
: Files
.Name
; m
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
204 loc
:= Files
.dir
.This(base
).This(target
);
205 DswDocuments
.Open(loc
, "defines", m
, res
);
207 r
:= m
.NewReader(NIL);
212 WHILE ~r
.eot
& (r
.char
<= 20X
) DO r
.Read
END;
213 WHILE ~r
.eot
& (r
.char
> 20X
) DO name
[i
] := r
.char
; r
.Read
; INC(i
) END;
214 IF i
# 0 THEN name
[i
] := 0X
; Define(name
, FALSE
) END
219 PROCEDURE CheckParams
;
221 IF (exe
# NIL) & (exe^
= "") THEN
222 Error("specified empty file name for exe", "", "", 0)
225 Error("specified empty rule", "", "", 0)
228 Error("specified empty path to cpmake rules", "", "", 0)
233 (* --------- loader --------- *)
235 PROCEDURE Import (m
: Module
; IN name
: DevCPT
.Name
);
236 VAR i
, j
: INTEGER; imp
: Module
;
239 ASSERT(name
# "", 21);
240 IF debugImport
THEN Log
.String(" import "); Log
.String(name$
) END;
241 IF name
= "SYSTEM" THEN INCL(DevCPM
.options
, DevCPM
.sysImp
)
242 ELSIF name
= "COM" THEN INCL(DevCPM
.options
, DevCPM
.com
)
243 ELSIF name
= "JAVA" THEN INCL(DevCPM
.options
, DevCPM
.java
)
245 IF debugImport
THEN Log
.Char(" ") END;
246 i
:= 0; (* find module in local list *)
247 WHILE (i
< m
.mno
) & (m
.imp
[i
].name$
# name$
) DO INC(i
) END;
249 j
:= 0; (* find module in global list *)
250 WHILE (j
< mno
) & (modList
[j
].name$
# name$
) DO INC(j
) END;
253 Log
.String("module " + name
+ " required before " + m
.name
); Log
.Ln
; INC(werr
)
255 NEW(imp
); imp
.name
:= name$
;
256 modList
[mno
] := imp
; INC(mno
)
260 m
.imp
[m
.mno
] := imp
; INC(m
.mno
)
264 IF debugImport
THEN Log
.Ln
END;
267 PROCEDURE ParseModule (m
: Module
);
268 VAR sym
: BYTE; SelfName
, impName
, aliasName
: DevCPT
.Name
;
270 PROCEDURE err (n
: SHORTINT);
274 PROCEDURE CheckSym(s
: SHORTINT);
276 IF sym
= s
THEN DevCPS
.Get(sym
) ELSE DevCPM
.err(s
) END
280 IF debugImport
THEN Log
.String("module " + m
.name
); Log
.Ln
END;
281 DevCPS
.Init
; DevCPS
.Get(sym
);
282 IF sym
= module
THEN DevCPS
.Get(sym
) ELSE err(16) END;
284 SelfName
:= DevCPS
.name$
; DevCPS
.Get(sym
);
286 INCL(DevCPM
.options
, DevCPM
.interface
); DevCPS
.Get(sym
);
287 IF sym
= eql
THEN DevCPS
.Get(sym
)
288 ELSE INCL(DevCPM
.options
, DevCPM
.noCode
)
290 IF sym
= string
THEN INCL(m
.flags
, library
); DevCPS
.Get(sym
)
296 IF sym
= import
THEN DevCPS
.Get(sym
);
299 aliasName
:= DevCPS
.name$
; impName
:= aliasName$
; DevCPS
.Get(sym
);
300 IF sym
= becomes
THEN DevCPS
.Get(sym
);
301 IF sym
= ident
THEN impName
:= DevCPS
.name$
; DevCPS
.Get(sym
) ELSE err(ident
) END
306 IF sym
= comma
THEN DevCPS
.Get(sym
)
307 ELSIF sym
= ident
THEN err(comma
)
313 LOOP (* preprocessor must read module fully *)
319 IF DevCPS
.name
# SelfName
THEN err(4) END;
341 PROCEDURE CheckModule (m
: Module
; source
: String
; OUT ok
: BOOLEAN);
345 DevCPM
.symList
:= m
.dir
;
346 (*DevCPM.codePath := m.outcode;*)
347 (*DevCPM.symPath := m.outsym;*)
348 DevCPM
.name
:= m
.name$
;
349 INCL(DevCPM
.options
, DevCPM
.comAware
);
351 INCL(DevCPM
.options
, DevCPM
.trap
)
353 (*IF oberon IN m.opts THEN INCL(DevCPM.options, DevCPM.oberon) END;*)
357 DevCPR
.Set(s
.name
, s
.value
);
363 IF DevCPR
.used
THEN INCL(m
.flags
, force
) END;
370 PROCEDURE MakePath (IN dir
, name
: Files
.Name
; IN type
: Files
.Type
; OUT path
: Files
.Name
);
372 ASSERT(name
# "", 21);
373 IF dir
= "" THEN path
:= modDir
+ "/" + name
374 ELSE path
:= dir
+ "/" + modDir
+ "/" + name
376 Kernel
.MakeFileName(path
, type
)
379 PROCEDURE Open (loc
: Files
.Locator
; IN sub
, name
: Files
.Name
; OUT path
: Files
.Name
; OUT text
: DswDocuments
.Model
);
382 ASSERT(loc
# NIL, 20);
383 ASSERT(name
# "", 21);
384 (* !!! use Kernel.MakeFileName instead ".ext" concat !!! *)
385 MakePath(sub
, name
, "cp", path
);
386 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
388 MakePath(sub
, name
, "odc", path
);
389 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
390 IF (text
= NIL) & (sub
= "") THEN
391 MakePath(sysDir
, name
, "cp", path
);
392 loc
:= Files
.dir
.This(sysDir
).This(modDir
);
393 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
395 MakePath(sysDir
, name
, "odc", path
);
396 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
405 PROCEDURE GetSource (IN modName
: ARRAY OF CHAR; list
: DevCPM
.Directory
; OUT path
: Files
.Name
; OUT s
: String
);
407 sub
, name
: Files
.Name
; loc
: Files
.Locator
; base
: DevCPM
.Directory
;
408 text
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
410 s
:= NIL; path
:= ""; base
:= list
;
411 Kernel
.SplitName(modName
, sub
, name
);
412 loc
:= Files
.dir
.This(sub
).This(modDir
);
413 Open(loc
, sub
, name
, path
, text
);
414 WHILE (text
= NIL) & (base
# NIL) DO
415 ASSERT(base
.legacy
, 100);
416 loc
:= Files
.dir
.This(base
.path
).This(sub
).This(modDir
);
417 Open(loc
, sub
, name
, path
, text
);
421 NEW(s
, text
.Length() + 1);
423 r
:= text
.NewReader(NIL);
424 FOR i
:= 0 TO text
.Length() - 1 DO
425 r
.Read
; s
[i
] := r
.char
431 PROCEDURE Trace (m
, parent
: Module
; VAR lno
: INTEGER);
434 IF ~
(trace
IN m
.flags
) THEN
435 INCL(m
.flags
, trace
);
436 FOR i
:= 0 TO m
.mno
- 1 DO
437 Trace(m
.imp
[i
], m
, lno
);
438 m
.depth
:= MAX(m
.depth
, m
.imp
[i
].depth
+ 1)
440 IF ~
(imported
IN m
.flags
) THEN
441 INCL(m
.flags
, imported
);
447 Error("recursive import of ^0 in ^1", m
.name$
, parent
.name$
, 0)
452 VAR i
, j
: INTEGER; m
: Module
;
454 ASSERT((mno
= 0) OR (lnkList
[0] # NIL), 20);
460 WHILE (j
>= 0) & (cmpList
[j
].depth
> m
.depth
) DO
461 cmpList
[j
+ 1] := cmpList
[j
];
470 VAR i
, j
, num
: INTEGER; m
: Module
; src
: String
; ok
: BOOLEAN;
473 WHILE (err
= 0) & (i
< mno
) DO
475 GetSource(m
.name$
, m
.dir
, m
.odc
, src
);
477 CheckModule(m
, src
, ok
);
478 IF ~ok
THEN INC(err
) END
480 Error("unable to open module ^1", m
.name$
, "", 0)
486 FOR i
:= 0 TO rno
- 1 DO
487 Trace(modList
[i
], modList
[i
], num
)
489 ASSERT((err
# 0) OR (num
= mno
), 100);
492 Log
.String("Parallel depth:"); Log
.Ln
;
493 FOR i
:= 0 TO mno
- 1 DO
494 Log
.String(" " + cmpList
[i
].name
); Log
.Int(cmpList
[i
].depth
); Log
.Ln
;
499 PROCEDURE IsCompiled (m
: Module
): BOOLEAN;
500 CONST target
= {hasSym
, hasObj
};
501 VAR i
: INTEGER; ready
: BOOLEAN;
505 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
= target
);
506 WHILE ready
& (i
< m
.mno
) DO
507 ready
:= IsCompiled(m
.imp
[i
]);
513 PROCEDURE Ready (m
: Module
): BOOLEAN;
514 CONST target
= {hasSym
, hasObj
};
515 VAR i
: INTEGER; ready
: BOOLEAN;
518 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
# target
) & (m
.worker
= NIL);
519 WHILE ready
& (i
< m
.mno
) DO
520 ready
:= IsCompiled(m
.imp
[i
]);
526 PROCEDURE PrepareCompiler (m
: Module
): DswProcs
.Process
;
527 VAR p
: DswProcs
.Process
; s
: Selector
;
530 ASSERT(m
.odc
# "", 21);
531 ASSERT(m
.worker
= NIL, 22);
532 p
:= DswProcs
.dir
.New();
533 p
.Program(base
+ "/" + target
+ "/" + "build");
535 IF nocode1
THEN p
.PutParam("-b") END;
536 IF nocode2
THEN p
.PutParam("-x") END;
537 IF force
IN m
.flags
THEN p
.PutParam("-f") END;
538 IF symonly
OR (library
IN m
.flags
) THEN p
.PutParam("-s") END;
541 IF s
.value
THEN p
.PutParam("-D") ELSE p
.PutParam("-d") END;
549 VAR i
, j
, num
, res
: INTEGER; ok
: BOOLEAN; m
: Module
; w
: DswProcs
.Process
;
551 IF mno
= 0 THEN RETURN END;
553 WHILE (err
= 0) & (num
< mno
) OR (j
> 0) DO
555 WHILE (err
= 0) & (i
< mno
) & (j
< jobs
) DO
558 w
:= PrepareCompiler(m
);
559 IF debugJobs
THEN Log
.String("Start job " + m
.name
) END;
562 IF debugJobs
THEN Log
.String(" ok") END;
566 IF debugJobs
THEN Log
.String(" fail") END;
567 INCL(m
.flags
, hasErrors
);
570 IF debugJobs
THEN Log
.Ln
END
574 WHILE (err
= 0) & (j
>= jobs
) OR (j
> 0) DO
576 WHILE (j
> 0) & (i
< mno
) DO
579 IF (w
# NIL) & w
.IsTerminated() THEN
581 IF debugJobs
THEN Log
.String("Stop job " + m
.name
); Log
.Int(res
); Log
.Ln
END;
583 INCL(m
.flags
, hasObj
);
584 INCL(m
.flags
, hasSym
);
587 INCL(m
.flags
, hasErrors
);
600 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; i
, res
: INTEGER;
602 IF ~symonly
& (exe
# NIL) THEN
603 p
:= DswProcs
.dir
.New();
604 p
.Program(base
+ "/" + target
+ "/" + "link");
606 FOR i
:= 0 TO mno
- 1 DO
607 IF ~
(library
IN lnkList
[i
].flags
) THEN
608 p
.PutParam(lnkList
[i
].name$
)
616 Error("link failed with code ^2", "", "", res
)
618 ELSE Error("link failed: unable to execute script", "", "", 0)
624 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; res
: INTEGER;
627 p
:= DswProcs
.dir
.New();
628 p
.Program(base
+ "/" + target
+ "/" + "clean");
634 Error("cleanup failed with code ^2", "", "", res
)
636 ELSE Error("cleanup failed: unable to execute script", "", "", 0)
643 IF Kernel
.trapCount
= 0 THEN
660 ELSE Error("trap occured", "", "", 0)
662 IF err
= 0 THEN Kernel
.Quit(0)
669 ASSERT(def
# NIL, 100);
670 Kernel
.intTrap
:= FALSE
;