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
: 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(" -t target Specify target rules"); Log
.Ln
;
161 Log
.String(" -o file Generate object file"); Log
.Ln
;
162 Log
.String(" -j num Specifies the number of jobs to run simultaneously"); Log
.Ln
;
163 Log
.String(" -f path Specifies path to directory with targets"); Log
.Ln
;
164 Log
.String(" -h Print help"); Log
.Ln
;
165 Log
.String(" -v Print version"); Log
.Ln
;
171 exe
:= NIL; auto
:= FALSE
; jobs
:= 1; def
.next
:= NIL; mno
:= 0; rno
:= 0;
172 target
:= NewStr("default"); base
:= NewStr("cprules");
174 CASE DswOpts
.GetOpt("acdgo:t:j:f:") OF
178 |
"f": base
:= DswOpts
.str
180 |
"j": jobs
:= MIN(MAX(StrToInt(DswOpts
.str
, 1), 1), maxJobs
)
181 |
"o": exe
:= DswOpts
.str
182 |
"t": target
:= DswOpts
.str
183 |
"v": Log
.String(version
); Log
.Ln
; Kernel
.Quit(0)
184 |
":": Error("missing argument for option -^0", DswOpts
.str
, "", 0)
185 |
"?": Error("unknown option -^0", DswOpts
.str
, "", 0)
186 |
"$": AddModule(DswOpts
.str
, def
, dirList
)
192 PROCEDURE ReadDefines
;
193 VAR loc
: Files
.Locator
; name
: Files
.Name
; m
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
195 loc
:= Files
.dir
.This(base
).This(target
);
196 DswDocuments
.Open(loc
, "defines", m
, res
);
198 r
:= m
.NewReader(NIL);
203 WHILE ~r
.eot
& (r
.char
<= 20X
) DO r
.Read
END;
204 WHILE ~r
.eot
& (r
.char
> 20X
) DO name
[i
] := r
.char
; r
.Read
; INC(i
) END;
205 IF i
# 0 THEN name
[i
] := 0X
; Define(name
, FALSE
) END
210 PROCEDURE CheckParams
;
212 IF (exe
# NIL) & (exe^
= "") THEN
213 Error("specified empty file name for exe", "", "", 0)
216 Error("specified empty rule", "", "", 0)
219 Error("specified empty path to cpmake rules", "", "", 0)
224 (* --------- loader --------- *)
226 PROCEDURE Import (m
: Module
; IN name
: DevCPT
.Name
);
227 VAR i
, j
: INTEGER; imp
: Module
;
230 ASSERT(name
# "", 21);
231 IF debugImport
THEN Log
.String(" import "); Log
.String(name$
) END;
232 IF name
= "SYSTEM" THEN INCL(DevCPM
.options
, DevCPM
.sysImp
)
233 ELSIF name
= "COM" THEN INCL(DevCPM
.options
, DevCPM
.com
)
234 ELSIF name
= "JAVA" THEN INCL(DevCPM
.options
, DevCPM
.java
)
236 IF debugImport
THEN Log
.Char(" ") END;
237 i
:= 0; (* find module in local list *)
238 WHILE (i
< m
.mno
) & (m
.imp
[i
].name$
# name$
) DO INC(i
) END;
240 j
:= 0; (* find module in global list *)
241 WHILE (j
< mno
) & (modList
[j
].name$
# name$
) DO INC(j
) END;
244 Log
.String("module " + name
+ " required before " + m
.name
); Log
.Ln
; INC(werr
)
246 NEW(imp
); imp
.name
:= name$
;
247 modList
[mno
] := imp
; INC(mno
)
251 m
.imp
[m
.mno
] := imp
; INC(m
.mno
)
255 IF debugImport
THEN Log
.Ln
END;
258 PROCEDURE ParseModule (m
: Module
);
259 VAR sym
: BYTE; SelfName
, impName
, aliasName
: DevCPT
.Name
;
261 PROCEDURE err (n
: SHORTINT);
265 PROCEDURE CheckSym(s
: SHORTINT);
267 IF sym
= s
THEN DevCPS
.Get(sym
) ELSE DevCPM
.err(s
) END
271 IF debugImport
THEN Log
.String("module " + m
.name
); Log
.Ln
END;
272 DevCPS
.Init
; DevCPS
.Get(sym
);
273 IF sym
= module
THEN DevCPS
.Get(sym
) ELSE err(16) END;
275 SelfName
:= DevCPS
.name$
; DevCPS
.Get(sym
);
277 INCL(DevCPM
.options
, DevCPM
.interface
); DevCPS
.Get(sym
);
278 IF sym
= eql
THEN DevCPS
.Get(sym
)
279 ELSE INCL(DevCPM
.options
, DevCPM
.noCode
)
281 IF sym
= string
THEN INCL(m
.flags
, library
); DevCPS
.Get(sym
)
287 IF sym
= import
THEN DevCPS
.Get(sym
);
290 aliasName
:= DevCPS
.name$
; impName
:= aliasName$
; DevCPS
.Get(sym
);
291 IF sym
= becomes
THEN DevCPS
.Get(sym
);
292 IF sym
= ident
THEN impName
:= DevCPS
.name$
; DevCPS
.Get(sym
) ELSE err(ident
) END
297 IF sym
= comma
THEN DevCPS
.Get(sym
)
298 ELSIF sym
= ident
THEN err(comma
)
304 LOOP (* preprocessor must read module fully *)
310 IF DevCPS
.name
# SelfName
THEN err(4) END;
332 PROCEDURE CheckModule (m
: Module
; source
: String
; OUT ok
: BOOLEAN);
336 DevCPM
.symList
:= m
.dir
;
337 (*DevCPM.codePath := m.outcode;*)
338 (*DevCPM.symPath := m.outsym;*)
339 DevCPM
.name
:= m
.name$
;
340 INCL(DevCPM
.options
, DevCPM
.comAware
);
342 INCL(DevCPM
.options
, DevCPM
.trap
)
344 (*IF oberon IN m.opts THEN INCL(DevCPM.options, DevCPM.oberon) END;*)
348 DevCPR
.Set(s
.name
, s
.value
);
354 IF DevCPR
.used
THEN INCL(m
.flags
, force
) END;
361 PROCEDURE MakePath (IN dir
, name
: Files
.Name
; IN type
: Files
.Type
; OUT path
: Files
.Name
);
363 ASSERT(name
# "", 21);
364 IF dir
= "" THEN path
:= modDir
+ "/" + name
365 ELSE path
:= dir
+ "/" + modDir
+ "/" + name
367 Kernel
.MakeFileName(path
, type
)
370 PROCEDURE Open (loc
: Files
.Locator
; IN sub
, name
: Files
.Name
; OUT path
: Files
.Name
; OUT text
: DswDocuments
.Model
);
373 ASSERT(loc
# NIL, 20);
374 ASSERT(name
# "", 21);
375 (* !!! use Kernel.MakeFileName instead ".ext" concat !!! *)
376 MakePath(sub
, name
, "cp", path
);
377 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
379 MakePath(sub
, name
, "odc", path
);
380 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
381 IF (text
= NIL) & (sub
= "") THEN
382 MakePath(sysDir
, name
, "cp", path
);
383 loc
:= Files
.dir
.This(sysDir
).This(modDir
);
384 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
386 MakePath(sysDir
, name
, "odc", path
);
387 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
396 PROCEDURE GetSource (IN modName
: ARRAY OF CHAR; list
: DevCPM
.Directory
; OUT path
: Files
.Name
; OUT s
: String
);
398 sub
, name
: Files
.Name
; loc
: Files
.Locator
; base
: DevCPM
.Directory
;
399 text
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
401 s
:= NIL; path
:= ""; base
:= list
;
402 Kernel
.SplitName(modName
, sub
, name
);
403 loc
:= Files
.dir
.This(sub
).This(modDir
);
404 Open(loc
, sub
, name
, path
, text
);
405 WHILE (text
= NIL) & (base
# NIL) DO
406 ASSERT(base
.legacy
, 100);
407 loc
:= Files
.dir
.This(base
.path
).This(sub
).This(modDir
);
408 Open(loc
, sub
, name
, path
, text
);
412 NEW(s
, text
.Length() + 1);
414 r
:= text
.NewReader(NIL);
415 FOR i
:= 0 TO text
.Length() - 1 DO
416 r
.Read
; s
[i
] := r
.char
422 PROCEDURE Trace (m
, parent
: Module
; VAR lno
: INTEGER);
425 IF ~
(trace
IN m
.flags
) THEN
426 INCL(m
.flags
, trace
);
427 FOR i
:= 0 TO m
.mno
- 1 DO
428 Trace(m
.imp
[i
], m
, lno
);
429 m
.depth
:= MAX(m
.depth
, m
.imp
[i
].depth
+ 1)
431 IF ~
(imported
IN m
.flags
) THEN
432 INCL(m
.flags
, imported
);
438 Error("recursive import of ^0 in ^1", m
.name$
, parent
.name$
, 0)
443 VAR i
, j
: INTEGER; m
: Module
;
445 ASSERT((mno
= 0) OR (lnkList
[0] # NIL), 20);
451 WHILE (j
>= 0) & (cmpList
[j
].depth
> m
.depth
) DO
452 cmpList
[j
+ 1] := cmpList
[j
];
461 VAR i
, j
, num
: INTEGER; m
: Module
; src
: String
; ok
: BOOLEAN;
464 WHILE (err
= 0) & (i
< mno
) DO
466 GetSource(m
.name$
, m
.dir
, m
.odc
, src
);
468 CheckModule(m
, src
, ok
);
469 IF ~ok
THEN INC(err
) END
471 Error("unable to open module ^1", m
.name$
, "", 0)
477 FOR i
:= 0 TO rno
- 1 DO
478 Trace(modList
[i
], modList
[i
], num
)
480 ASSERT((err
# 0) OR (num
= mno
), 100);
483 Log
.String("Parallel depth:"); Log
.Ln
;
484 FOR i
:= 0 TO mno
- 1 DO
485 Log
.String(" " + cmpList
[i
].name
); Log
.Int(cmpList
[i
].depth
); Log
.Ln
;
490 PROCEDURE IsCompiled (m
: Module
): BOOLEAN;
491 CONST target
= {hasSym
, hasObj
};
492 VAR i
: INTEGER; ready
: BOOLEAN;
496 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
= target
);
497 WHILE ready
& (i
< m
.mno
) DO
498 ready
:= IsCompiled(m
.imp
[i
]);
504 PROCEDURE Ready (m
: Module
): BOOLEAN;
505 CONST target
= {hasSym
, hasObj
};
506 VAR i
: INTEGER; ready
: BOOLEAN;
509 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
# target
) & (m
.worker
= NIL);
510 WHILE ready
& (i
< m
.mno
) DO
511 ready
:= IsCompiled(m
.imp
[i
]);
517 PROCEDURE PrepareCompiler (m
: Module
): DswProcs
.Process
;
518 VAR p
: DswProcs
.Process
; s
: Selector
;
521 ASSERT(m
.odc
# "", 21);
522 ASSERT(m
.worker
= NIL, 22);
523 p
:= DswProcs
.dir
.New();
524 p
.Program(base
+ "/" + target
+ "/" + "build");
526 IF force
IN m
.flags
THEN p
.PutParam("-f") END;
527 IF library
IN m
.flags
THEN p
.PutParam("-s") END;
530 IF s
.value
THEN p
.PutParam("-D") ELSE p
.PutParam("-d") END;
538 VAR i
, j
, num
, res
: INTEGER; ok
: BOOLEAN; m
: Module
; w
: DswProcs
.Process
;
540 IF mno
= 0 THEN RETURN END;
542 WHILE (err
= 0) & (num
< mno
) OR (j
> 0) DO
544 WHILE (err
= 0) & (i
< mno
) & (j
< jobs
) DO
547 w
:= PrepareCompiler(m
);
548 IF debugJobs
THEN Log
.String("Start job " + m
.name
) END;
551 IF debugJobs
THEN Log
.String(" ok") END;
555 IF debugJobs
THEN Log
.String(" fail") END;
556 INCL(m
.flags
, hasErrors
);
559 IF debugJobs
THEN Log
.Ln
END
563 WHILE (err
= 0) & (j
>= jobs
) OR (j
> 0) DO
565 WHILE (j
> 0) & (i
< mno
) DO
568 IF (w
# NIL) & w
.IsTerminated() THEN
570 IF debugJobs
THEN Log
.String("Stop job " + m
.name
); Log
.Int(res
); Log
.Ln
END;
572 INCL(m
.flags
, hasObj
);
573 INCL(m
.flags
, hasSym
);
576 INCL(m
.flags
, hasErrors
);
589 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; i
, res
: INTEGER;
592 p
:= DswProcs
.dir
.New();
593 p
.Program(base
+ "/" + target
+ "/" + "link");
595 FOR i
:= 0 TO mno
- 1 DO
596 IF ~
(library
IN lnkList
[i
].flags
) THEN
597 p
.PutParam(lnkList
[i
].name$
)
605 Error("link failed with code ^2", "", "", res
)
607 ELSE Error("link failed: unable to execute script", "", "", 0)
613 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; res
: INTEGER;
616 p
:= DswProcs
.dir
.New();
617 p
.Program(base
+ "/" + target
+ "/" + "clean");
623 Error("cleanup failed with code ^2", "", "", res
)
625 ELSE Error("cleanup failed: unable to execute script", "", "", 0)
632 IF Kernel
.trapCount
= 0 THEN
649 ELSE Error("trap occured", "", "", 0)
651 IF err
= 0 THEN Kernel
.Quit(0)
658 ASSERT(def
# NIL, 100);
659 Kernel
.intTrap
:= FALSE
;