DEADSOFTWARE

cpmake: fix preprocessor selector handling
[cpc.git] / src / generic / Dsw / Mod / MakeMain.cp
index 9df3961d6c067c22ccc57a3672d525ed4631c20b..e024a3c913d0944e511175121e9b210f793d901a 100644 (file)
@@ -5,6 +5,9 @@ MODULE DswMakeMain;
   CONST
     version = "0.3.0";
 
   CONST
     version = "0.3.0";
 
+    modDir = "Mod";
+    sysDir = "System";
+
     maxImps = 127;
     maxJobs = maxImps;
 
     maxImps = 127;
     maxJobs = maxImps;
 
@@ -57,9 +60,9 @@ MODULE DswMakeMain;
 
     Module = POINTER TO RECORD
       name: DevCPT.Name;
 
     Module = POINTER TO RECORD
       name: DevCPT.Name;
-      path: Files.Name;
-      selectors: Selector; (* with head, list of selectors for this module *)
+      odc: Files.Name;
       depth: INTEGER; (* 0: leaf, MAX: root *)
       depth: INTEGER; (* 0: leaf, MAX: root *)
+      dir: DevCPM.Directory;
       mno: INTEGER;
       imp: ARRAY maxImps OF Module;
       flags: SET;
       mno: INTEGER;
       imp: ARRAY maxImps OF Module;
       flags: SET;
@@ -67,16 +70,60 @@ MODULE DswMakeMain;
     END;
 
   VAR
     END;
 
   VAR
-    err: INTEGER;
+    werr, err: INTEGER;
     mno, rno: INTEGER; (* num modules *)
     modList, lnkList, cmpList: ARRAY maxImps OF Module;
     def: Selector; (* with head, global list of selectors *)
     processor, compiler, os, linker: INTEGER;
     cpcExe, cplExe: String;
     cpcArgs, cplArgs: StringList;
     mno, rno: INTEGER; (* num modules *)
     modList, lnkList, cmpList: ARRAY maxImps OF Module;
     def: Selector; (* with head, global list of selectors *)
     processor, compiler, os, linker: INTEGER;
     cpcExe, cplExe: String;
     cpcArgs, cplArgs: StringList;
+    dirList: DevCPM.Directory;
     auto: BOOLEAN;
     jobs: INTEGER;
     exe: String;
     auto: BOOLEAN;
     jobs: INTEGER;
     exe: String;
+    printCompileModList: BOOLEAN;
+    printLinkModList: BOOLEAN;
+    printCompileFileList: BOOLEAN;
+
+  PROCEDURE Error (e: INTEGER; IN p0, p1: ARRAY OF CHAR; i2: INTEGER);
+    VAR msg, p2: ARRAY 128 OF CHAR;
+  BEGIN
+    CASE e OF
+    | 0: msg := "option -D expect + or - after identifier"
+    | 1: msg := "option -D expect identifier"
+    | 2: msg := "option -U expect identifier"
+    | 3: msg := "module name must be identifier"
+    | 4: msg := "expected integer" (* p0 = in str *)
+    | 5: msg := "unterminated string"
+    | 6: msg := "unknown processor ^0"
+    | 7: msg := "unknown os ^0"
+    | 8: msg := "unknown compiler ^0"
+    | 9: msg := "unknown linker ^0"
+    | 10: msg := "missing argument for option ^0"
+    | 11: msg := "unknown option ^0"
+    | 12: msg := "compiler not selected"
+    | 13: msg := "processor not selected"
+    | 14: msg := "processor not supported by native compiler"
+    | 15: msg := "internal linker not required for cpfront"
+    | 16: msg := "cpfront can't out executable file"
+    | 17: msg := "linker not selected"
+    | 18: msg := "os not selected"
+    | 19: msg := "output file name can't be empty"
+    | 20: msg := "recursive import of ^0 in ^1"
+    | 21: msg := "unable to open module ^1"
+    | 22: msg := "linker terminated with code ^2"
+    | 23: msg := "unable to execute linker"
+    | 24: msg := "trap occured"
+    | 25: msg := "compiler not installed"
+    | 26: msg := "linker not installed"
+    ELSE
+      Strings.IntToString(err, msg)
+    END;
+    Strings.IntToString(i2, p2);
+    Log.String(Kernel.argv[0]$); Log.String(": ");
+    Log.ParamMsg(msg, p0, p1, p2); Log.Ln;
+    INC(err)
+  END Error;
 
   (* --------- options --------- *)
 
 
   (* --------- options --------- *)
 
@@ -90,54 +137,30 @@ MODULE DswMakeMain;
     RETURN i
   END IdentLen;
 
     RETURN i
   END IdentLen;
 
-  PROCEDURE Define (n: ARRAY OF CHAR);
+  PROCEDURE Define (n: ARRAY OF CHAR; overwrite: BOOLEAN);
     VAR i: INTEGER; v: BOOLEAN; s: Selector;
   BEGIN
     i := IdentLen(n);
     IF i # 0 THEN
       IF ~((n[i] = 0X) OR (((n[i] = "+") OR (n[i] = "-")) & (n[i + 1] = 0X))) THEN
     VAR i: INTEGER; v: BOOLEAN; s: Selector;
   BEGIN
     i := IdentLen(n);
     IF i # 0 THEN
       IF ~((n[i] = 0X) OR (((n[i] = "+") OR (n[i] = "-")) & (n[i + 1] = 0X))) THEN
-        Log.String("option -D expect + or - after identifier"); Log.Ln; INC(err)
+        Error(0, "", "", 0)
       END;
       v := n[i] # "-"; n[i] := 0X; s := def;
       WHILE (s.next # NIL) & (s.next.name$ # n$) DO s := s.next END;
       IF s.next = NIL THEN
         NEW(s.next);
         Strings.StringToUtf8(n, s.next.name, i);
       END;
       v := n[i] # "-"; n[i] := 0X; s := def;
       WHILE (s.next # NIL) & (s.next.name$ # n$) DO s := s.next END;
       IF s.next = NIL THEN
         NEW(s.next);
         Strings.StringToUtf8(n, s.next.name, i);
-        ASSERT(i = 0)
-      END;
-      s.next.value := v
+        ASSERT(i = 0);
+        s.next.value := v
+      ELSIF overwrite THEN
+        s.next.value := v
+      END
     ELSE
     ELSE
-      Log.String("option -D expect identifier"); Log.Ln; INC(err)
+      Error(1, "", "", 0)
     END
   END Define;
 
     END
   END Define;
 
-  PROCEDURE Undefine (IN n: ARRAY OF CHAR);
-    VAR i: INTEGER; s: Selector;
-  BEGIN
-    i := IdentLen(n);
-    IF (i # 0) & (n[i] = 0X) THEN
-      s := def;
-      WHILE (s.next # NIL) & (s.next.name$ # n$) DO s := s.next END;
-      IF s.next # NIL THEN s.next := s.next.next END
-    ELSE
-      Log.String("option -U expect identifier"); Log.Ln; INC(err)
-    END
-  END Undefine;
-
-  PROCEDURE CopySelectorList (base: Selector): Selector;
-    VAR s, t, x: Selector;
-  BEGIN
-    ASSERT(base # NIL, 20);
-    s := base; NEW(t); x := t;
-    WHILE s # NIL DO
-      x^ := s^;
-      IF s.next # NIL THEN NEW(x.next); x := x.next END;
-      s := s.next
-    END;
-    RETURN t
-  END CopySelectorList;
-
-  PROCEDURE AddModule (IN n: ARRAY OF CHAR; selectors: Selector);
+  PROCEDURE AddModule (IN n: ARRAY OF CHAR; selectors: Selector; dir: DevCPM.Directory);
     VAR i, res: INTEGER; m: Module;
   BEGIN
     i := IdentLen(n);
     VAR i, res: INTEGER; m: Module;
   BEGIN
     i := IdentLen(n);
@@ -148,12 +171,12 @@ MODULE DswMakeMain;
         NEW(m);
         Strings.StringToUtf8(n, m.name, res);
         ASSERT(res = 0);
         NEW(m);
         Strings.StringToUtf8(n, m.name, res);
         ASSERT(res = 0);
-        m.selectors := CopySelectorList(selectors);
+        m.dir := dir;
         modList[i] := m;
         INC(mno)
       END
     ELSE
         modList[i] := m;
         INC(mno)
       END
     ELSE
-      Log.String("module name must be identifier"); Log.Ln; INC(err)
+      Error(3, "", "", 0)
     END
   END AddModule;
 
     END
   END AddModule;
 
@@ -162,7 +185,7 @@ MODULE DswMakeMain;
   BEGIN
     Strings.StringToInt(s, x, res);
     IF res # 0 THEN
   BEGIN
     Strings.StringToInt(s, x, res);
     IF res # 0 THEN
-      Log.String("expected integer"); Log.Ln; INC(err);
+      Error(4, s, "", 0);
       x := def
     END;
     RETURN x
       x := def
     END;
     RETURN x
@@ -222,7 +245,7 @@ MODULE DswMakeMain;
           AddChar(ch); ch := s[i]; INC(i)
         END;
         IF ch # 0X THEN ch := s[i]; INC(i)
           AddChar(ch); ch := s[i]; INC(i)
         END;
         IF ch # 0X THEN ch := s[i]; INC(i)
-        ELSE Log.String("unterminated string"); Log.Ln; INC(err)
+        ELSE Error(5, "", "", 0)
         END
       | " ":
         REPEAT ch := s[i]; INC(i) UNTIL ch # " ";
         END
       | " ":
         REPEAT ch := s[i]; INC(i) UNTIL ch # " ";
@@ -251,19 +274,22 @@ MODULE DswMakeMain;
     Log.String("      dev2     Dev2 based ELF/PE linker"); Log.Ln;
 (*
     Log.String("  -Fp path     Add path with project"); Log.Ln;
     Log.String("      dev2     Dev2 based ELF/PE linker"); Log.Ln;
 (*
     Log.String("  -Fp path     Add path with project"); Log.Ln;
-    Log.String("  -Fx path     Add postfix for project directories"); Log.Ln;
 *)
     Log.String("  -Xp path     Use executable file for Component Pascal compiler"); Log.Ln;
     Log.String("  -Xi path     Use executable file for internal linker (native only)"); Log.Ln;
     Log.String("  -Cg params   Pass parameters to Component Pasacal compiler directly"); Log.Ln;
     Log.String("  -Ci params   Pass parameters to internal linker directly"); Log.Ln;
 *)
     Log.String("  -Xp path     Use executable file for Component Pascal compiler"); Log.Ln;
     Log.String("  -Xi path     Use executable file for internal linker (native only)"); Log.Ln;
     Log.String("  -Cg params   Pass parameters to Component Pasacal compiler directly"); Log.Ln;
     Log.String("  -Ci params   Pass parameters to internal linker directly"); Log.Ln;
+    Log.String("  -II          Trap on user interrupt"); Log.Ln;
+    Log.String("  -IC          Print module list for compilation and quit"); Log.Ln;
+    Log.String("  -IL          Print module list for linking and quit"); Log.Ln;
+    Log.String("  -Ic          Print file list for compilation and quit"); Log.Ln;
 (*
 (*
-    Log.String("  -a           Enable automatic dependency resolution"); Log.Ln;
+    Log.String("  -Il          Print file list for linking and quit"); Log.Ln;
 *)
 *)
+    Log.String("  -a           Enable automatic dependency resolution"); Log.Ln;
     Log.String("  -o name      Generate executable file"); Log.Ln;
     Log.String("  -j num       Specifies the number of jobs to run simultaneously"); Log.Ln;
     Log.String('  -D ident["+"|"-"] Add preprocessor selector'); Log.Ln;
     Log.String("  -o name      Generate executable file"); Log.Ln;
     Log.String("  -j num       Specifies the number of jobs to run simultaneously"); Log.Ln;
     Log.String('  -D ident["+"|"-"] Add preprocessor selector'); Log.Ln;
-    Log.String('  -U ident     Remove preprocessor selector'); Log.Ln;
     Log.String("  -h           Print help and quit"); Log.Ln;
     Log.String("  -V           Print version and quit"); Log.Ln;
     Kernel.Quit(1)
     Log.String("  -h           Print help and quit"); Log.Ln;
     Log.String("  -V           Print version and quit"); Log.Ln;
     Kernel.Quit(1)
@@ -285,7 +311,7 @@ MODULE DswMakeMain;
       IF s$ = "none" THEN processor := anymach
       ELSIF s$ = "486" THEN processor := mach386
       ELSIF s$ = "68k" THEN processor := mach68k
       IF s$ = "none" THEN processor := anymach
       ELSIF s$ = "486" THEN processor := mach386
       ELSIF s$ = "68k" THEN processor := mach68k
-      ELSE Log.String("unknwon processor "); Log.String(s); Log.Ln; INC(err)
+      ELSE Error(6, s, "", 0)
       END
     | "s":
       s := DswOpts.str;
       END
     | "s":
       s := DswOpts.str;
@@ -297,24 +323,24 @@ MODULE DswMakeMain;
       ELSIF s$ = "win32" THEN os := win32
       ELSIF s$ = "cygwin" THEN os := cygwin
       ELSIF s$ = "darwin" THEN os := darwin
       ELSIF s$ = "win32" THEN os := win32
       ELSIF s$ = "cygwin" THEN os := cygwin
       ELSIF s$ = "darwin" THEN os := darwin
-      ELSE Log.String("unknwon os "); Log.String(s); Log.Ln; INC(err)
+      ELSE Error(7, s, "", 0)
       END
     | "g":
       s := DswOpts.str;
       Strings.ToLower(s, s);
       IF s$ = "native" THEN compiler := cpnative
       ELSIF s$ = "cpfront" THEN compiler := cpfront
       END
     | "g":
       s := DswOpts.str;
       Strings.ToLower(s, s);
       IF s$ = "native" THEN compiler := cpnative
       ELSIF s$ = "cpfront" THEN compiler := cpfront
-      ELSE Log.String("unknwon compiler "); Log.String(s); Log.Ln; INC(err)
+      ELSE Error(8, s, "", 0)
       END
     | "i":
       s := DswOpts.str;
       Strings.ToLower(s, s);
       IF s$ = "dev2" THEN linker := dev2
       END
     | "i":
       s := DswOpts.str;
       Strings.ToLower(s, s);
       IF s$ = "dev2" THEN linker := dev2
-      ELSE Log.String("unknwon linker "); Log.String(s); Log.Ln; INC(err)
+      ELSE Error(9, s, "", 0)
       END
       END
-    | ":": Log.String("missing argument for option -T"); Log.String(DswOpts.str); Log.Ln; INC(err)
-    | "?": Log.String("unknown option -T"); Log.String(DswOpts.str); Log.Ln; INC(err)
-    | 0X: Log.String("unknown option -T"); Log.Ln; INC(err)
+    | ":": Error(10, "-T" + DswOpts.str, "", 0)
+    | "?": Error(11, "-T" + DswOpts.str, "", 0)
+    | 0X: Error(11, "-T", "", 0)
     END
   END ParseTargetOpts;
 
     END
   END ParseTargetOpts;
 
@@ -323,9 +349,9 @@ MODULE DswMakeMain;
     CASE DswOpts.GetOpt("g:i:") OF
     | "g": cpcArgs := ToStringList(DswOpts.str);
     | "i": cplArgs := ToStringList(DswOpts.str);
     CASE DswOpts.GetOpt("g:i:") OF
     | "g": cpcArgs := ToStringList(DswOpts.str);
     | "i": cplArgs := ToStringList(DswOpts.str);
-    | ":": Log.String("missing argument for option -C"); Log.String(DswOpts.str); Log.Ln; INC(err)
-    | "?": Log.String("unknown option -C"); Log.String(DswOpts.str); Log.Ln; INC(err)
-    | 0X: Log.String("unknown option -C"); Log.Ln; INC(err)
+    | ":": Error(10, "-C" + DswOpts.str, "", 0)
+    | "?": Error(11, "-C" + DswOpts.str, "", 0)
+    | 0X: Error(11, "-C", "", 0)
     END
   END ParseCommandOpts;
 
     END
   END ParseCommandOpts;
 
@@ -334,70 +360,128 @@ MODULE DswMakeMain;
     CASE DswOpts.GetOpt("g:i:") OF
     | "g": cpcExe := DswOpts.str;
     | "i": cplExe := DswOpts.str;
     CASE DswOpts.GetOpt("g:i:") OF
     | "g": cpcExe := DswOpts.str;
     | "i": cplExe := DswOpts.str;
-    | ":": Log.String("missing argument for option -X"); Log.String(DswOpts.str); Log.Ln; INC(err)
-    | "?": Log.String("unknown option -X"); Log.String(DswOpts.str); Log.Ln; INC(err)
-    | 0X: Log.String("unknown option -X"); Log.Ln; INC(err)
+    | ":": Error(10, "-X" + DswOpts.str, "", 0)
+    | "?": Error(11, "-X" + DswOpts.str, "", 0)
+    | 0X: Error(11, "-X", "", 0)
     END
   END ParseExternalOpts;
 
     END
   END ParseExternalOpts;
 
+  PROCEDURE ParseInfoOpts;
+  BEGIN
+    CASE DswOpts.GetOpt("CLcI") OF
+    | "I": Kernel.intTrap := TRUE
+    | "C": printCompileModList := TRUE
+    | "L": printLinkModList := TRUE
+    | "c": printCompileFileList := TRUE
+    | ":": Error(10, "-I" + DswOpts.str, "", 0)
+    | "?": Error(11, "-I" + DswOpts.str, "", 0)
+    | 0X: Error(11, "-I", "", 0)
+    END
+  END ParseInfoOpts;
+
+  PROCEDURE ParseFileOpts;
+    VAR d: DevCPM.Directory;
+  BEGIN
+    CASE DswOpts.GetOpt("p:") OF
+    | "p": NEW(d); d.path := DswOpts.str$; d.legacy := TRUE; d.next := dirList; dirList := d
+    | ":": Error(10, "-F" + DswOpts.str, "", 0)
+    | "?": Error(11, "-F" + DswOpts.str, "", 0)
+    | 0X: Error(11, "-F", "", 0)
+    END
+  END ParseFileOpts;
+
   PROCEDURE ParseArgs;
   BEGIN
     exe := NIL; auto := FALSE; jobs := 1; def.next := NIL; mno := 0; rno := 0;
     processor := anymach; os := anyos; compiler := anycp;
     LOOP
   PROCEDURE ParseArgs;
   BEGIN
     exe := NIL; auto := FALSE; jobs := 1; def.next := NIL; mno := 0; rno := 0;
     processor := anymach; os := anyos; compiler := anycp;
     LOOP
-      CASE DswOpts.GetOpt("ao:j:D:U:TCXhV") OF
+      CASE DswOpts.GetOpt("ao:j:D:TCXIFhV") OF
       | "a": auto := TRUE
       | "o": exe := DswOpts.str
       | "j": jobs := MIN(MAX(StrToInt(DswOpts.str, 1), 1), maxJobs)
       | "h": Help
       | "V": Version
       | "a": auto := TRUE
       | "o": exe := DswOpts.str
       | "j": jobs := MIN(MAX(StrToInt(DswOpts.str, 1), 1), maxJobs)
       | "h": Help
       | "V": Version
-      | "D": Define(DswOpts.str)
-      | "U": Undefine(DswOpts.str)
+      | "D": Define(DswOpts.str, TRUE)
       | "T": ParseTargetOpts
       | "C": ParseCommandOpts
       | "X": ParseExternalOpts
       | "T": ParseTargetOpts
       | "C": ParseCommandOpts
       | "X": ParseExternalOpts
-      | ":": Log.String("missing argument for option -"); Log.String(DswOpts.str); Log.Ln; INC(err)
-      | "?": Log.String("unknown option -"); Log.String(DswOpts.str); Log.Ln; INC(err)
-      | "$": AddModule(DswOpts.str, def)
+      | "I": ParseInfoOpts
+      | "F": ParseFileOpts
+      | ":": Error(10, "-" + DswOpts.str, "", 0)
+      | "?": Error(11, "-" + DswOpts.str, "", 0)
+      | "$": AddModule(DswOpts.str, def, dirList)
       | 0X: EXIT
       END
     END;
   END ParseArgs;
 
   PROCEDURE CheckParams;
       | 0X: EXIT
       END
     END;
   END ParseArgs;
 
   PROCEDURE CheckParams;
+
+    PROCEDURE Default (IN name: ARRAY OF CHAR; value: BOOLEAN);
+    BEGIN
+      IF value = TRUE THEN
+        Define(name + "+", FALSE)
+      ELSE
+        Define(name + "-", FALSE)
+      END
+    END Default;
+
   BEGIN
     IF compiler = anycp THEN
   BEGIN
     IF compiler = anycp THEN
-      Log.String("compiler not selected"); Log.Ln; INC(err)
+      Error(12, "", "", 0)
     ELSIF compiler = cpnative THEN
       IF processor = anymach THEN
     ELSIF compiler = cpnative THEN
       IF processor = anymach THEN
-        Log.String("processor not selected"); Log.Ln; INC(err)
+        Error(13, "", "", 0)
       ELSIF processor # mach386 THEN
       ELSIF processor # mach386 THEN
-        Log.String("processor not supported by native compiler"); Log.Ln; INC(err)
+        Error(14, "", "", 0)
       END
     END;
     IF (compiler = cpfront) & (linker # anyint) THEN
       END
     END;
     IF (compiler = cpfront) & (linker # anyint) THEN
-      Log.String("internal linker not required for cpfront"); Log.Ln; INC(err)
+      Error(15, "", "", 0)
     END;
     IF (compiler = cpfront) & (exe # NIL) THEN
     END;
     IF (compiler = cpfront) & (exe # NIL) THEN
-      Log.String("cpfront can't out executable file"); Log.Ln; INC(err)
+      Error(16, "", "", 0)
     END;
     IF (exe # NIL) & (compiler = cpnative) & (linker = anyint) THEN
     END;
     IF (exe # NIL) & (compiler = cpnative) & (linker = anyint) THEN
-      Log.String("linker not selected"); Log.Ln; INC(err)
+      Error(17, "", "", 0)
     END;
     IF (linker = dev2) & (os = anyos) THEN
     END;
     IF (linker = dev2) & (os = anyos) THEN
-      Log.String("os not selected"); Log.Ln; INC(err)
+      Error(18, "", "", 0)
     END;
     IF (exe # NIL) & (exe^ = "") THEN
     END;
     IF (exe # NIL) & (exe^ = "") THEN
-      Log.String("output file name can't be empty"); Log.Ln; INC(err)
+      Error(19, "", "", 0)
     END;
     END;
-    IF cpcExe = NIL THEN
-      IF compiler = cpnative THEN cpcExe := NewStr("cpc486")
-      ELSIF compiler = cpfront THEN cpcExe := NewStr("cpfront")
+    IF (cpcExe = NIL) & (compiler # anycp) THEN
+      IF compiler = cpnative THEN cpcExe := DswProcs.dir.GetPath("cpc486")
+      ELSIF compiler = cpfront THEN cpcExe := DswProcs.dir.GetPath("cpfront")
+      END;
+      IF cpcExe = NIL THEN
+        Error(25, "", "", 0)
       END
     END;
       END
     END;
-    IF cplExe = NIL THEN
-      IF linker = dev2 THEN cplExe := NewStr("cpl486") END
-    END
+    IF (cplExe = NIL) & (linker # anyint) THEN
+      cplExe := DswProcs.dir.GetPath("cpl486");
+      IF cplExe = NIL THEN
+        Error(26, "", "", 0)
+      END
+    END;
+    (* cpompiler *)
+    Default("NATIVE",  compiler = cpnative);
+    Default("CPFRONT", compiler = cpfront);
+    (* processor *)
+    Default("I486", processor = mach386);
+    Default("I386", processor = mach386);
+    Default("M68K", processor = mach68k);
+    (* operating system *)
+    Default("LINUX", os = linux);
+    Default("FREEBSD", os = freebsd);
+    Default("OPENBSD", os = openbsd);
+    Default("DARWIN", os = darwin);
+    Default("WIN32", os = win32);
+    Default("CYGWIN", os = cygwin);
+    Default("POSIX", os IN {linux, freebsd, openbsd, darwin, cygwin});
+    (* linker *)
+    Default("DEV2", linker = dev2);
   END CheckParams;
 
   (* --------- loader --------- *)
   END CheckParams;
 
   (* --------- loader --------- *)
@@ -419,7 +503,10 @@ MODULE DswMakeMain;
         j := 0; (* find module in global list *)
         WHILE (j < mno) & (modList[j].name$ # name$) DO INC(j) END;
         IF j >= mno THEN
         j := 0; (* find module in global list *)
         WHILE (j < mno) & (modList[j].name$ # name$) DO INC(j) END;
         IF j >= mno THEN
-          NEW(imp); imp.name := name$; imp.selectors := CopySelectorList(m.selectors);
+          IF ~auto THEN
+            Log.String("module " + name + " required before " + m.name); Log.Ln; INC(werr)
+          END;
+          NEW(imp); imp.name := name$;
           modList[mno] := imp; INC(mno)
         ELSE
           imp := modList[j]
           modList[mno] := imp; INC(mno)
         ELSE
           imp := modList[j]
@@ -476,6 +563,29 @@ MODULE DswMakeMain;
           END
         END;
         CheckSym(semicolon)
           END
         END;
         CheckSym(semicolon)
+      END;
+      LOOP (* preprocessor must read module fully *)
+        IF sym = end THEN
+          DevCPS.Get(sym);
+          IF sym = ident THEN
+            DevCPS.Get(sym);
+            IF sym = period THEN
+              IF DevCPS.name # SelfName THEN err(4) END;
+              EXIT
+            ELSIF sym = eof THEN
+              err(period);
+              EXIT
+            END
+          ELSIF sym = eof THEN
+            err(ident);
+            EXIT
+          END;
+        ELSIF sym = eof THEN
+          err(end);
+          EXIT
+        ELSE
+          DevCPS.Get(sym);
+        END
       END
     ELSE err(ident)
     END;
       END
     ELSE err(ident)
     END;
@@ -486,8 +596,8 @@ MODULE DswMakeMain;
     VAR s: Selector;
   BEGIN
     DevCPM.Init(source);
     VAR s: Selector;
   BEGIN
     DevCPM.Init(source);
+    DevCPM.symList := m.dir;
 (*
 (*
-    DevCPM.symList := m.insym;
     DevCPM.codePath := m.outcode;
     DevCPM.symPath := m.outsym;
 *)
     DevCPM.codePath := m.outcode;
     DevCPM.symPath := m.outsym;
 *)
@@ -498,7 +608,7 @@ MODULE DswMakeMain;
     IF oberon IN m.opts THEN INCL(DevCPM.options, DevCPM.oberon) END;
 *)
     DevCPR.Init;
     IF oberon IN m.opts THEN INCL(DevCPM.options, DevCPM.oberon) END;
 *)
     DevCPR.Init;
-    s := m.selectors.next;
+    s := def.next;
     WHILE s # NIL DO
       DevCPR.Set(s.name, s.value);
       s := s.next
     WHILE s # NIL DO
       DevCPR.Set(s.name, s.value);
       s := s.next
@@ -512,31 +622,27 @@ MODULE DswMakeMain;
     Kernel.FastCollect
   END CheckModule;
 
     Kernel.FastCollect
   END CheckModule;
 
-  PROCEDURE GetSource (IN modName: ARRAY OF CHAR; OUT path: Files.Name; OUT s: String);
-    CONST modDir = "Mod"; sysDir = "System";
-    VAR dir, name: Files.Name; loc: Files.Locator;
-      text: DswDocuments.Model; r: DswDocuments.Reader; i, res: INTEGER;
-
-    PROCEDURE MakePath (dir, name: Files.Name; type: Files.Type; OUT path: Files.Name);
-    BEGIN
-      ASSERT(name # "", 21);
-      IF dir = "" THEN path := modDir + "/" + name
-      ELSE path := dir + "/" + modDir + "/" + name
-      END;
-      Kernel.MakeFileName(path, type)
-    END MakePath;
+  PROCEDURE MakePath (IN dir, name: Files.Name; IN type: Files.Type; OUT path: Files.Name);
+  BEGIN
+    ASSERT(name # "", 21);
+    IF dir = "" THEN path := modDir + "/" + name
+    ELSE path := dir + "/" + modDir + "/" + name
+    END;
+    Kernel.MakeFileName(path, type)
+  END MakePath;
 
 
+  PROCEDURE Open (loc: Files.Locator; IN sub, name: Files.Name; OUT path: Files.Name; OUT text: DswDocuments.Model);
+    VAR res: INTEGER;
   BEGIN
   BEGIN
-    s := NIL; path := "";
-    Kernel.SplitName(modName, dir, name);
-    loc := Files.dir.This(dir).This(modDir);
-    (* --> Kernel.MakeFileName(name, Kernel.docType); <-- *)
-    MakePath(dir, name, "cp", path);
+    ASSERT(loc # NIL, 20);
+    ASSERT(name # "", 21);
+    (* !!! use Kernel.MakeFileName instead ".ext" concat !!! *)
+    MakePath(sub, name, "cp", path);
     DswDocuments.Open(loc, name + ".cp", text, res);
     IF text = NIL THEN
     DswDocuments.Open(loc, name + ".cp", text, res);
     IF text = NIL THEN
-      MakePath(dir, name, "odc", path);
+      MakePath(sub, name, "odc", path);
       DswDocuments.Open(loc, name + ".odc", text, res);
       DswDocuments.Open(loc, name + ".odc", text, res);
-      IF (text = NIL) & (dir = "") THEN
+      IF (text = NIL) & (sub = "") THEN
         MakePath(sysDir, name, "cp", path);
         loc := Files.dir.This(sysDir).This(modDir);
         DswDocuments.Open(loc, name + ".cp", text, res);
         MakePath(sysDir, name, "cp", path);
         loc := Files.dir.This(sysDir).This(modDir);
         DswDocuments.Open(loc, name + ".cp", text, res);
@@ -548,6 +654,23 @@ MODULE DswMakeMain;
           END
         END
       END
           END
         END
       END
+    END
+  END Open;
+
+  PROCEDURE GetSource (IN modName: ARRAY OF CHAR; list: DevCPM.Directory; OUT path: Files.Name; OUT s: String);
+    VAR
+      sub, name: Files.Name; loc: Files.Locator; base: DevCPM.Directory; 
+      text: DswDocuments.Model; r: DswDocuments.Reader; i, res: INTEGER;
+  BEGIN
+    s := NIL; path := ""; base := list;
+    Kernel.SplitName(modName, sub, name);
+    loc := Files.dir.This(sub).This(modDir);
+    Open(loc, sub, name, path, text);
+    WHILE (text = NIL) & (base # NIL) DO
+      ASSERT(base.legacy, 100);
+      loc := Files.dir.This(base.path).This(sub).This(modDir);
+      Open(loc, sub, name, path, text);
+      base := base.next
     END;
     IF text # NIL THEN
       NEW(s, text.Length() + 1);
     END;
     IF text # NIL THEN
       NEW(s, text.Length() + 1);
@@ -576,7 +699,7 @@ MODULE DswMakeMain;
       END;
       EXCL(m.flags, trace)
     ELSE
       END;
       EXCL(m.flags, trace)
     ELSE
-      Log.String("recursive import of " + m.name + " in " + parent.name); Log.Ln; INC(err)
+      Error(20, m.name$, parent.name$, 0)
     END
   END Trace;
 
     END
   END Trace;
 
@@ -599,20 +722,21 @@ MODULE DswMakeMain;
   END Sort;
 
   PROCEDURE CheckDeps;
   END Sort;
 
   PROCEDURE CheckDeps;
-    VAR i, j, num: INTEGER; m: Module; s: String; ok: BOOLEAN;
+    VAR i, j, num: INTEGER; m: Module; src: String; ok: BOOLEAN;
   BEGIN
     i := 0; rno := mno;
     WHILE (err = 0) & (i < mno) DO
       m := modList[i];
   BEGIN
     i := 0; rno := mno;
     WHILE (err = 0) & (i < mno) DO
       m := modList[i];
-      GetSource(m.name$, m.path, s);
-      IF s # NIL THEN
-        CheckModule(m, s, ok);
+      GetSource(m.name$, m.dir, m.odc, src);
+      IF src # NIL THEN
+        CheckModule(m, src, ok);
         IF ~ok THEN INC(err) END
       ELSE
         IF ~ok THEN INC(err) END
       ELSE
-        Log.String("unable to open module " + m.name); Log.Ln; INC(err)
+        Error(21, m.name$, "", 0)
       END;
       INC(i)
     END;
       END;
       INC(i)
     END;
+    INC(err, werr);
     num := 0;
     FOR i := 0 TO rno - 1 DO
       Trace(modList[i], modList[i], num)
     num := 0;
     FOR i := 0 TO rno - 1 DO
       Trace(modList[i], modList[i], num)
@@ -671,101 +795,38 @@ MODULE DswMakeMain;
     IF debugArgs THEN Log.Ln END
   END PutParams;
 
     IF debugArgs THEN Log.Ln END
   END PutParams;
 
+  PROCEDURE PutPathList (w: DswProcs.Process; IN par: ARRAY OF CHAR; base: DevCPM.Directory);
+  BEGIN
+    IF base # NIL THEN
+      PutPathList(w, par, base.next); (* in revese order *)
+      w.PutParam(par);
+      w.PutParam(base.path);
+    END
+  END PutPathList;
+
   PROCEDURE ExecuteCompiler (m: Module): DswProcs.Process;
   PROCEDURE ExecuteCompiler (m: Module): DswProcs.Process;
-    VAR w: DswProcs.Process; ok: BOOLEAN;
+    VAR w: DswProcs.Process; ok: BOOLEAN; s: Selector;
   BEGIN
     ASSERT(m # NIL, 20);
   BEGIN
     ASSERT(m # NIL, 20);
-    ASSERT(m.path # "", 21);
+    ASSERT(m.odc # "", 21);
     ASSERT(m.worker = NIL, 22);
     w := DswProcs.dir.New();
     w.Program(cpcExe);
     w.PutParam("-legacy");
     ASSERT(m.worker = NIL, 22);
     w := DswProcs.dir.New();
     w.Program(cpcExe);
     w.PutParam("-legacy");
-    PutParams(w, cpcArgs);
-    CASE compiler OF
-    | cpfront:
-      w.PutParam("-define+"); w.PutParam("CPFRONT");
-      w.PutParam("-define-"); w.PutParam("NATIVE");
-    | cpnative:
-      w.PutParam("-define-"); w.PutParam("CPFRONT");
-      w.PutParam("-define+"); w.PutParam("NATIVE");
-    END;
-    CASE processor OF
-    | mach386:
-      w.PutParam("-define+"); w.PutParam("I486");
-      w.PutParam("-define-"); w.PutParam("M68K");
-    | mach68k:
-      w.PutParam("-define+"); w.PutParam("I486");
-      w.PutParam("-define-"); w.PutParam("M68K");
-    ELSE
-      w.PutParam("-define-"); w.PutParam("I486");
-      w.PutParam("-define-"); w.PutParam("M68K");
-    END;
-    CASE os OF
-    | anyos:
-      w.PutParam("-define-"); w.PutParam("POSIX");
-      w.PutParam("-define-"); w.PutParam("LINUX");
-      w.PutParam("-define-"); w.PutParam("FREEBSD");
-      w.PutParam("-define-"); w.PutParam("OPENBSD");
-      w.PutParam("-define-"); w.PutParam("WIN32");
-      w.PutParam("-define-"); w.PutParam("DARWIN");
-    | linux:
-      w.PutParam("-define+"); w.PutParam("POSIX");
-      w.PutParam("-define+"); w.PutParam("LINUX");
-      w.PutParam("-define-"); w.PutParam("FREEBSD");
-      w.PutParam("-define-"); w.PutParam("OPENBSD");
-      w.PutParam("-define-"); w.PutParam("WIN32");
-      w.PutParam("-define-"); w.PutParam("DARWIN");
-    | freebsd:
-      w.PutParam("-define+"); w.PutParam("POSIX");
-      w.PutParam("-define-"); w.PutParam("LINUX");
-      w.PutParam("-define+"); w.PutParam("FREEBSD");
-      w.PutParam("-define-"); w.PutParam("OPENBSD");
-      w.PutParam("-define-"); w.PutParam("WIN32");
-      w.PutParam("-define-"); w.PutParam("CYGWIN");
-      w.PutParam("-define-"); w.PutParam("DARWIN");
-    | openbsd:
-      w.PutParam("-define+"); w.PutParam("POSIX");
-      w.PutParam("-define-"); w.PutParam("LINUX");
-      w.PutParam("-define-"); w.PutParam("FREEBSD");
-      w.PutParam("-define+"); w.PutParam("OPENBSD");
-      w.PutParam("-define-"); w.PutParam("WIN32");
-      w.PutParam("-define-"); w.PutParam("CYGWIN");
-      w.PutParam("-define-"); w.PutParam("DARWIN");
-    | win32:
-      w.PutParam("-define-"); w.PutParam("POSIX");
-      w.PutParam("-define-"); w.PutParam("LINUX");
-      w.PutParam("-define-"); w.PutParam("FREEBSD");
-      w.PutParam("-define-"); w.PutParam("OPENBSD");
-      w.PutParam("-define+"); w.PutParam("WIN32");
-      w.PutParam("-define-"); w.PutParam("CYGWIN");
-      w.PutParam("-define-"); w.PutParam("DARWIN");
-    | cygwin:
-      w.PutParam("-define+"); w.PutParam("POSIX");
-      w.PutParam("-define-"); w.PutParam("LINUX");
-      w.PutParam("-define-"); w.PutParam("FREEBSD");
-      w.PutParam("-define-"); w.PutParam("OPENBSD");
-      w.PutParam("-define+"); w.PutParam("WIN32");
-      w.PutParam("-define+"); w.PutParam("CYGWIN");
-      w.PutParam("-define-"); w.PutParam("DARWIN");
-    | darwin:
-      w.PutParam("-define+"); w.PutParam("POSIX");
-      w.PutParam("-define-"); w.PutParam("LINUX");
-      w.PutParam("-define-"); w.PutParam("FREEBSD");
-      w.PutParam("-define-"); w.PutParam("OPENBSD");
-      w.PutParam("-define-"); w.PutParam("WIN32");
-      w.PutParam("-define-"); w.PutParam("CYGWIN");
-      w.PutParam("-define+"); w.PutParam("DARWIN");
-    END;
-    CASE linker OF
-    | dev2:
-      w.PutParam("-define+"); w.PutParam("DEV2");
-    ELSE
-      w.PutParam("-define-"); w.PutParam("DEV2");
+    PutPathList(w, "-legacysymdir", m.dir);
+    s := def.next;
+    WHILE s # NIL DO
+      IF s.value = TRUE THEN w.PutParam("-define+")
+      ELSE w.PutParam("-define-")
+      END;
+      w.PutParam(s.name$);
+      s := s.next
     END;
     END;
-    w.PutParam(m.path);
+    PutParams(w, cpcArgs);
+    w.PutParam(m.odc);
     w.Execute(ok);
     IF ok THEN
     w.Execute(ok);
     IF ok THEN
-      Log.String("Compile " + m.name + " (" + m.path + ")"); Log.Ln;
+      Log.String("Compile " + m.name + " (" + m.odc + ")"); Log.Ln;
     ELSE
       w := NIL
     END;
     ELSE
       w := NIL
     END;
@@ -829,6 +890,7 @@ MODULE DswMakeMain;
     ASSERT(compiler = cpnative, 22);
     p := DswProcs.dir.New();
     p.Program(cplExe);
     ASSERT(compiler = cpnative, 22);
     p := DswProcs.dir.New();
     p.Program(cplExe);
+    PutPathList(p, "-legacycodedir", dirList);
     IF os # anyos THEN
       p.PutParam("-os");
       CASE os OF
     IF os # anyos THEN
       p.PutParam("-os");
       CASE os OF
@@ -860,12 +922,10 @@ MODULE DswMakeMain;
       Log.String("Link "); Log.String(exe); Log.Ln;
       res := p.Result();
       IF res # 0 THEN
       Log.String("Link "); Log.String(exe); Log.Ln;
       res := p.Result();
       IF res # 0 THEN
-        Log.String("linker terminated with error"); Log.Int(res); Log.Ln;
-        INC(err)
+        Error(22, "", "", res)
       END
     ELSE
       END
     ELSE
-      Log.String("unable to execute linker"); Log.Int(i); Log.Ln;
-      INC(err)
+      Error(23, "", "", 0)
     END
   END LinkDev2;
 
     END
   END LinkDev2;
 
@@ -879,24 +939,60 @@ MODULE DswMakeMain;
     END
   END Link;
 
     END
   END Link;
 
+  PROCEDURE PrintInfo;
+    VAR i: INTEGER;
+  BEGIN
+    IF printCompileModList THEN
+      FOR i := 0 TO mno - 1 DO
+        Log.String(cmpList[i].name$); Log.Char(" ")
+      END;
+      Log.Ln
+    ELSIF printLinkModList THEN
+      FOR i := 0 TO mno - 1 DO
+        IF ~(library IN lnkList[i].flags) THEN
+          Log.String(lnkList[i].name$); Log.Char(" ")
+        END
+      END;
+      Log.Ln
+    ELSIF printCompileFileList THEN
+      FOR i := 0 TO mno - 1 DO
+        Log.String(cmpList[i].odc); Log.Char(" ")
+      END;
+      Log.Ln
+(*  ELSIF printLinkFileList THEN
+      FOR i := 0 TO mno - 1 DO
+        IF ~(library IN lnkList[i].flags) THEN
+          Log.String(lnkList[i].ocf); Log.Char(" ")
+        END
+      END;
+      Log.Ln*)
+    END
+  END PrintInfo;
+
   PROCEDURE Main;
   PROCEDURE Main;
-    VAR m: Module; s: Selector; p: DswProcs.Process; ok: BOOLEAN; i, res: INTEGER;
   BEGIN
     IF Kernel.trapCount = 0 THEN
       ParseArgs;
       IF err = 0 THEN
   BEGIN
     IF Kernel.trapCount = 0 THEN
       ParseArgs;
       IF err = 0 THEN
+        IF printCompileModList OR printLinkModList OR printCompileFileList THEN
+          auto := TRUE
+        END;
         CheckParams;
         IF err = 0 THEN
           CheckDeps;
           IF err = 0 THEN
         CheckParams;
         IF err = 0 THEN
           CheckDeps;
           IF err = 0 THEN
-            Compile;
-            IF err = 0 THEN
-              Link
+            IF printCompileModList OR printLinkModList OR printCompileFileList THEN
+              PrintInfo
+            ELSE
+              Compile;
+              IF err = 0 THEN
+                Link
+              END
             END
           END
         END
             END
           END
         END
-     END
-    ELSE INC(err)
+      END
+    ELSE Error(24, "", "", 0)
     END;
     IF err = 0 THEN Kernel.Quit(0)
     ELSE Kernel.Quit(1)
     END;
     IF err = 0 THEN Kernel.Quit(0)
     ELSE Kernel.Quit(1)
@@ -905,5 +1001,7 @@ MODULE DswMakeMain;
 
 BEGIN
   NEW(def);
 
 BEGIN
   NEW(def);
+  ASSERT(def # NIL, 100);
+  Kernel.intTrap := FALSE;
   Kernel.Start(Main)
 END DswMakeMain.
   Kernel.Start(Main)
 END DswMakeMain.