5c84f5296973d52dd8df3146e4eee924c5bade84
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; library
= 5;
39 String
= POINTER TO ARRAY OF CHAR;
41 Selector
= POINTER TO RECORD
47 Module
= POINTER TO RECORD
50 depth
: INTEGER; (* 0: leaf, MAX: root *)
51 dir
: DevCPM
.Directory
;
53 imp
: ARRAY maxImps
OF Module
;
55 worker
: DswProcs
.Process
;
59 auto
, trap
, clean
: BOOLEAN;
60 exe
, target
, base
: String
;
65 mno
, rno
: INTEGER; (* num modules *)
66 modList
, lnkList
, cmpList
: ARRAY maxImps
OF Module
;
67 def
: Selector
; (* with head, global list of selectors *)
68 dirList
: DevCPM
.Directory
;
70 PROCEDURE Error (IN str
, p0
, p1
: ARRAY OF CHAR; i2
: INTEGER);
71 VAR p2
: ARRAY 32 OF CHAR;
73 Strings
.IntToString(i2
, p2
);
74 Log
.String("cpmake: "); Log
.ParamMsg(str
, p0
, p1
, p2
); Log
.Ln
;
81 (* --------- options --------- *)
83 PROCEDURE IdentLen (IN s
: ARRAY OF CHAR): INTEGER;
87 IF Strings
.IsIdentStart(s
[0]) THEN
88 REPEAT INC(i
) UNTIL ~Strings
.IsIdent(s
[i
])
93 PROCEDURE Define (n
: ARRAY OF CHAR; overwrite
: BOOLEAN);
94 VAR i
: INTEGER; v
: BOOLEAN; s
: Selector
;
98 IF ~
((n
[i
] = 0X
) OR (((n
[i
] = "+") OR (n
[i
] = "-")) & (n
[i
+ 1] = 0X
))) THEN
99 Error("defines: expectd + or - after identifier (^0)", n
, "", 0)
101 v
:= n
[i
] # "-"; n
[i
] := 0X
; s
:= def
;
102 WHILE (s
.next
# NIL) & (s
.next
.name$
# n$
) DO s
:= s
.next
END;
105 Strings
.StringToUtf8(n
, s
.next
.name
, i
);
111 ELSE Error("defines: '^0' not valid identifier", n
, "", 0)
115 PROCEDURE AddModule (IN n
: ARRAY OF CHAR; selectors
: Selector
; dir
: DevCPM
.Directory
);
116 VAR i
, res
: INTEGER; m
: Module
;
119 IF (i
# 0) & (n
[i
] = 0X
) THEN
121 WHILE (i
< mno
) & (modList
[i
].name$
# n$
) DO INC(i
) END;
124 Strings
.StringToUtf8(n
, m
.name
, res
);
131 Error("invalid module name", "", "", 0)
135 PROCEDURE StrToInt (IN s
: ARRAY OF CHAR; def
: INTEGER): INTEGER;
138 Strings
.StringToInt(s
, x
, res
);
140 Error("invalid integer value", s
, "", 0);
146 PROCEDURE NewStr (IN s
: ARRAY OF CHAR): String
;
149 NEW(p
, LEN(s$
) + 1); p^
:= s$
;
155 Log
.String("Usage: cpmake [options] module..."); Log
.Ln
;
156 Log
.String("Options:"); Log
.Ln
;
157 Log
.String(" -a Enable automatic dependency resolution"); Log
.Ln
;
158 Log
.String(" -c Remove all generated files"); Log
.Ln
;
159 Log
.String(" -t target Specify target rules"); Log
.Ln
;
160 Log
.String(" -o file Generate object file"); Log
.Ln
;
161 Log
.String(" -j num Specifies the number of jobs to run simultaneously"); Log
.Ln
;
162 Log
.String(" -f path Specifies path to directory with targets"); Log
.Ln
;
163 Log
.String(" -h Print help"); Log
.Ln
;
164 Log
.String(" -v Print version"); Log
.Ln
;
170 exe
:= NIL; auto
:= FALSE
; jobs
:= 1; def
.next
:= NIL; mno
:= 0; rno
:= 0;
171 target
:= NewStr("default"); base
:= NewStr("cprules");
173 CASE DswOpts
.GetOpt("acdgo:t:j:f:") OF
177 |
"f": base
:= DswOpts
.str
179 |
"j": jobs
:= MIN(MAX(StrToInt(DswOpts
.str
, 1), 1), maxJobs
)
180 |
"o": exe
:= DswOpts
.str
181 |
"t": target
:= DswOpts
.str
182 |
"v": Log
.String(version
); Log
.Ln
; Kernel
.Quit(0)
183 |
":": Error("missing argument for option -^0", DswOpts
.str
, "", 0)
184 |
"?": Error("unknown option -^0", DswOpts
.str
, "", 0)
185 |
"$": AddModule(DswOpts
.str
, def
, dirList
)
191 PROCEDURE ReadDefines
;
192 VAR loc
: Files
.Locator
; name
: Files
.Name
; m
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
194 loc
:= Files
.dir
.This(base
).This(target
);
195 DswDocuments
.Open(loc
, "defines", m
, res
);
197 r
:= m
.NewReader(NIL);
202 WHILE ~r
.eot
& (r
.char
<= 20X
) DO r
.Read
END;
203 WHILE ~r
.eot
& (r
.char
> 20X
) DO name
[i
] := r
.char
; r
.Read
; INC(i
) END;
204 IF i
# 0 THEN name
[i
] := 0X
; Define(name
, FALSE
) END
209 PROCEDURE CheckParams
;
211 IF (exe
# NIL) & (exe^
= "") THEN
212 Error("specified empty file name for exe", "", "", 0)
215 Error("specified empty rule", "", "", 0)
218 Error("specified empty path to cpmake rules", "", "", 0)
223 (* --------- loader --------- *)
225 PROCEDURE Import (m
: Module
; IN name
: DevCPT
.Name
);
226 VAR i
, j
: INTEGER; imp
: Module
;
229 ASSERT(name
# "", 21);
230 IF debugImport
THEN Log
.String(" import "); Log
.String(name$
) END;
231 IF name
= "SYSTEM" THEN INCL(DevCPM
.options
, DevCPM
.sysImp
)
232 ELSIF name
= "COM" THEN INCL(DevCPM
.options
, DevCPM
.com
)
233 ELSIF name
= "JAVA" THEN INCL(DevCPM
.options
, DevCPM
.java
)
235 IF debugImport
THEN Log
.Char(" ") END;
236 i
:= 0; (* find module in local list *)
237 WHILE (i
< m
.mno
) & (m
.imp
[i
].name$
# name$
) DO INC(i
) END;
239 j
:= 0; (* find module in global list *)
240 WHILE (j
< mno
) & (modList
[j
].name$
# name$
) DO INC(j
) END;
243 Log
.String("module " + name
+ " required before " + m
.name
); Log
.Ln
; INC(werr
)
245 NEW(imp
); imp
.name
:= name$
;
246 modList
[mno
] := imp
; INC(mno
)
250 m
.imp
[m
.mno
] := imp
; INC(m
.mno
)
254 IF debugImport
THEN Log
.Ln
END;
257 PROCEDURE ParseModule (m
: Module
);
258 VAR sym
: BYTE; SelfName
, impName
, aliasName
: DevCPT
.Name
;
260 PROCEDURE err (n
: SHORTINT);
264 PROCEDURE CheckSym(s
: SHORTINT);
266 IF sym
= s
THEN DevCPS
.Get(sym
) ELSE DevCPM
.err(s
) END
270 IF debugImport
THEN Log
.String("module " + m
.name
); Log
.Ln
END;
271 DevCPS
.Init
; DevCPS
.Get(sym
);
272 IF sym
= module
THEN DevCPS
.Get(sym
) ELSE err(16) END;
274 SelfName
:= DevCPS
.name$
; DevCPS
.Get(sym
);
276 INCL(DevCPM
.options
, DevCPM
.interface
); DevCPS
.Get(sym
);
277 IF sym
= eql
THEN DevCPS
.Get(sym
)
278 ELSE INCL(DevCPM
.options
, DevCPM
.noCode
)
280 IF sym
= string
THEN INCL(m
.flags
, library
); DevCPS
.Get(sym
)
286 IF sym
= import
THEN DevCPS
.Get(sym
);
289 aliasName
:= DevCPS
.name$
; impName
:= aliasName$
; DevCPS
.Get(sym
);
290 IF sym
= becomes
THEN DevCPS
.Get(sym
);
291 IF sym
= ident
THEN impName
:= DevCPS
.name$
; DevCPS
.Get(sym
) ELSE err(ident
) END
296 IF sym
= comma
THEN DevCPS
.Get(sym
)
297 ELSIF sym
= ident
THEN err(comma
)
303 LOOP (* preprocessor must read module fully *)
309 IF DevCPS
.name
# SelfName
THEN err(4) END;
331 PROCEDURE CheckModule (m
: Module
; source
: String
; OUT ok
: BOOLEAN);
335 DevCPM
.symList
:= m
.dir
;
336 (*DevCPM.codePath := m.outcode;*)
337 (*DevCPM.symPath := m.outsym;*)
338 DevCPM
.name
:= m
.name$
;
339 INCL(DevCPM
.options
, DevCPM
.comAware
);
341 INCL(DevCPM
.options
, DevCPM
.trap
)
343 (*IF oberon IN m.opts THEN INCL(DevCPM.options, DevCPM.oberon) END;*)
347 DevCPR
.Set(s
.name
, s
.value
);
359 PROCEDURE MakePath (IN dir
, name
: Files
.Name
; IN type
: Files
.Type
; OUT path
: Files
.Name
);
361 ASSERT(name
# "", 21);
362 IF dir
= "" THEN path
:= modDir
+ "/" + name
363 ELSE path
:= dir
+ "/" + modDir
+ "/" + name
365 Kernel
.MakeFileName(path
, type
)
368 PROCEDURE Open (loc
: Files
.Locator
; IN sub
, name
: Files
.Name
; OUT path
: Files
.Name
; OUT text
: DswDocuments
.Model
);
371 ASSERT(loc
# NIL, 20);
372 ASSERT(name
# "", 21);
373 (* !!! use Kernel.MakeFileName instead ".ext" concat !!! *)
374 MakePath(sub
, name
, "cp", path
);
375 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
377 MakePath(sub
, name
, "odc", path
);
378 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
379 IF (text
= NIL) & (sub
= "") THEN
380 MakePath(sysDir
, name
, "cp", path
);
381 loc
:= Files
.dir
.This(sysDir
).This(modDir
);
382 DswDocuments
.Open(loc
, name
+ ".cp", text
, res
);
384 MakePath(sysDir
, name
, "odc", path
);
385 DswDocuments
.Open(loc
, name
+ ".odc", text
, res
);
394 PROCEDURE GetSource (IN modName
: ARRAY OF CHAR; list
: DevCPM
.Directory
; OUT path
: Files
.Name
; OUT s
: String
);
396 sub
, name
: Files
.Name
; loc
: Files
.Locator
; base
: DevCPM
.Directory
;
397 text
: DswDocuments
.Model
; r
: DswDocuments
.Reader
; i
, res
: INTEGER;
399 s
:= NIL; path
:= ""; base
:= list
;
400 Kernel
.SplitName(modName
, sub
, name
);
401 loc
:= Files
.dir
.This(sub
).This(modDir
);
402 Open(loc
, sub
, name
, path
, text
);
403 WHILE (text
= NIL) & (base
# NIL) DO
404 ASSERT(base
.legacy
, 100);
405 loc
:= Files
.dir
.This(base
.path
).This(sub
).This(modDir
);
406 Open(loc
, sub
, name
, path
, text
);
410 NEW(s
, text
.Length() + 1);
412 r
:= text
.NewReader(NIL);
413 FOR i
:= 0 TO text
.Length() - 1 DO
414 r
.Read
; s
[i
] := r
.char
420 PROCEDURE Trace (m
, parent
: Module
; VAR lno
: INTEGER);
423 IF ~
(trace
IN m
.flags
) THEN
424 INCL(m
.flags
, trace
);
425 FOR i
:= 0 TO m
.mno
- 1 DO
426 Trace(m
.imp
[i
], m
, lno
);
427 m
.depth
:= MAX(m
.depth
, m
.imp
[i
].depth
+ 1)
429 IF ~
(imported
IN m
.flags
) THEN
430 INCL(m
.flags
, imported
);
436 Error("recursive import of ^0 in ^1", m
.name$
, parent
.name$
, 0)
441 VAR i
, j
: INTEGER; m
: Module
;
443 ASSERT((mno
= 0) OR (lnkList
[0] # NIL), 20);
449 WHILE (j
>= 0) & (cmpList
[j
].depth
> m
.depth
) DO
450 cmpList
[j
+ 1] := cmpList
[j
];
459 VAR i
, j
, num
: INTEGER; m
: Module
; src
: String
; ok
: BOOLEAN;
462 WHILE (err
= 0) & (i
< mno
) DO
464 GetSource(m
.name$
, m
.dir
, m
.odc
, src
);
466 CheckModule(m
, src
, ok
);
467 IF ~ok
THEN INC(err
) END
469 Error("unable to open module ^1", m
.name$
, "", 0)
475 FOR i
:= 0 TO rno
- 1 DO
476 Trace(modList
[i
], modList
[i
], num
)
478 ASSERT((err
# 0) OR (num
= mno
), 100);
481 Log
.String("Parallel depth:"); Log
.Ln
;
482 FOR i
:= 0 TO mno
- 1 DO
483 Log
.String(" " + cmpList
[i
].name
); Log
.Int(cmpList
[i
].depth
); Log
.Ln
;
488 PROCEDURE IsCompiled (m
: Module
): BOOLEAN;
489 CONST target
= {hasSym
, hasObj
};
490 VAR i
: INTEGER; ready
: BOOLEAN;
494 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
= target
);
495 WHILE ready
& (i
< m
.mno
) DO
496 ready
:= IsCompiled(m
.imp
[i
]);
502 PROCEDURE Ready (m
: Module
): BOOLEAN;
503 CONST target
= {hasSym
, hasObj
};
504 VAR i
: INTEGER; ready
: BOOLEAN;
507 ready
:= ~
(hasErrors
IN m
.flags
) & (m
.flags
* target
# target
) & (m
.worker
= NIL);
508 WHILE ready
& (i
< m
.mno
) DO
509 ready
:= IsCompiled(m
.imp
[i
]);
515 PROCEDURE PrepareCompiler (m
: Module
): DswProcs
.Process
;
516 VAR p
: DswProcs
.Process
; s
: Selector
;
519 ASSERT(m
.odc
# "", 21);
520 ASSERT(m
.worker
= NIL, 22);
521 p
:= DswProcs
.dir
.New();
522 p
.Program(base
+ "/" + target
+ "/" + "build");
528 VAR i
, j
, num
, res
: INTEGER; ok
: BOOLEAN; m
: Module
; w
: DswProcs
.Process
;
530 IF mno
= 0 THEN RETURN END;
532 WHILE (err
= 0) & (num
< mno
) OR (j
> 0) DO
534 WHILE (err
= 0) & (i
< mno
) & (j
< jobs
) DO
537 w
:= PrepareCompiler(m
);
538 IF debugJobs
THEN Log
.String("Start job " + m
.name
) END;
541 IF debugJobs
THEN Log
.String(" ok") END;
545 IF debugJobs
THEN Log
.String(" fail") END;
546 INCL(m
.flags
, hasErrors
);
549 IF debugJobs
THEN Log
.Ln
END
553 WHILE (err
= 0) & (j
>= jobs
) OR (j
> 0) DO
555 WHILE (j
> 0) & (i
< mno
) DO
558 IF (w
# NIL) & w
.IsTerminated() THEN
560 IF debugJobs
THEN Log
.String("Stop job " + m
.name
); Log
.Int(res
); Log
.Ln
END;
562 INCL(m
.flags
, hasObj
);
563 INCL(m
.flags
, hasSym
);
566 INCL(m
.flags
, hasErrors
);
579 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; i
, res
: INTEGER;
582 p
:= DswProcs
.dir
.New();
583 p
.Program(base
+ "/" + target
+ "/" + "link");
585 FOR i
:= 0 TO mno
- 1 DO
586 IF ~
(library
IN lnkList
[i
].flags
) THEN
587 p
.PutParam(lnkList
[i
].name$
)
595 Error("link failed with code ^2", "", "", res
)
597 ELSE Error("link failed: unable to execute script", "", "", 0)
603 VAR p
: DswProcs
.Process
; ok
: BOOLEAN; res
: INTEGER;
606 p
:= DswProcs
.dir
.New();
607 p
.Program(base
+ "/" + target
+ "/" + "clean");
613 Error("cleanup failed with code ^2", "", "", res
)
615 ELSE Error("cleanup failed: unable to execute script", "", "", 0)
622 IF Kernel
.trapCount
= 0 THEN
639 ELSE Error("trap occured", "", "", 0)
641 IF err
= 0 THEN Kernel
.Quit(0)
648 ASSERT(def
# NIL, 100);
649 Kernel
.intTrap
:= FALSE
;