DEADSOFTWARE

cpmake: read module fully
[cpc.git] / src / generic / Dsw / Mod / MakeMain.cp
index 48de2a89eddc90b6cb3f46dcdc82a973ccca0f4e..989b8bcb2ce6727184062b5f0eb9489993a5acbf 100644 (file)
@@ -3,6 +3,8 @@ MODULE DswMakeMain;
   IMPORT Kernel, Files, Log, Strings, DswOpts, DswProcs, DswDocuments, DevCPM, DevCPT, DevCPR, DevCPS;
 
   CONST
+    version = "0.3.0";
+
     maxImps = 127;
     maxJobs = maxImps;
 
@@ -30,7 +32,7 @@ MODULE DswMakeMain;
     anymach = 0; cmach = 1; mach386 = 10; mach68k = 20;
 
     (* operation system types *)
-    anyos = 0; linux = 1; freebsd = 2; openbsd = 3; win32 = 4;
+    anyos = 0; linux = 1; freebsd = 2; openbsd = 3; win32 = 4; cygwin = 5; darwin = 6;
 
     (* compiler types *)
     anycp = 0; cpnative = 1; cpfront = 2;
@@ -41,9 +43,11 @@ MODULE DswMakeMain;
     debugImport = FALSE;
     debugOrder = FALSE;
     debugJobs = FALSE;
+    debugArgs = FALSE;
 
   TYPE
     String = POINTER TO ARRAY OF CHAR;
+    StringList = POINTER TO ARRAY OF String;
 
     Selector = POINTER TO RECORD
       name: DevCPT.Name;
@@ -63,11 +67,13 @@ MODULE DswMakeMain;
     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;
     auto: BOOLEAN;
     jobs: INTEGER;
     exe: String;
@@ -162,6 +168,72 @@ MODULE DswMakeMain;
     RETURN x
   END StrToInt;
 
+  PROCEDURE NewStr (IN s: ARRAY OF CHAR): String;
+    VAR p: String;
+  BEGIN
+    NEW(p, LEN(s$) + 1); p^ := s$;
+    RETURN p
+  END NewStr;
+
+  PROCEDURE ToStringList (IN s: ARRAY OF CHAR): StringList;
+    VAR i: INTEGER; ch, term: CHAR; pars: StringList;
+
+    PROCEDURE AddChar (c: CHAR);
+      VAR i, n, len: INTEGER; str: String;
+    BEGIN
+      IF pars = NIL THEN
+        NEW(pars, 1); NEW(pars[0], 2); pars[0, 0] := c
+      ELSE
+        n := LEN(pars) - 1;
+        len := LEN(pars[n]$);
+        NEW(str, len + 2);
+        FOR i := 0 TO len - 1 DO
+          str[i] := pars[n, i]
+        END;
+        str[i] := c;
+        pars[n] := str
+      END
+    END AddChar;
+
+    PROCEDURE AddLine;
+      VAR i, len: INTEGER; p: StringList;
+    BEGIN
+      IF pars = NIL THEN
+        NEW(pars, 1); i := 0;
+      ELSE
+        len := LEN(pars);
+        NEW(p, len + 1);
+        FOR i := 0 TO len - 1 DO
+          p[i] := pars[i]
+        END;
+        pars := p
+      END;
+      NEW(pars[i], 1)
+    END AddLine;
+
+  BEGIN
+    i := 0;
+    REPEAT ch := s[i]; INC(i) UNTIL ch # " ";
+    WHILE ch # 0X DO
+      CASE ch OF
+      | '"', "'":
+        term := ch; ch := s[i]; INC(i);
+        WHILE (ch # term) & (ch # 0X) DO
+          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)
+        END
+      | " ":
+        REPEAT ch := s[i]; INC(i) UNTIL ch # " ";
+        IF ch # 0X THEN AddLine END
+      ELSE
+        AddChar(ch); ch := s[i]; INC(i)
+      END
+    END;
+    RETURN pars
+  END ToStringList;
+
   PROCEDURE Help;
   BEGIN
     Log.String("Usage: cpmake [options] module..."); Log.Ln;
@@ -180,21 +252,27 @@ MODULE DswMakeMain;
 (*
     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("  -Cp params   Pass parameters to Component Pasacal compiler directly"); 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("  -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('  -U ident     Remove preprocessor selector'); Log.Ln;
-    Log.String("  -h           Print help"); Log.Ln;
-    Log.String("  -v           Print version"); Log.Ln;
+    Log.String("  -h           Print help and quit"); Log.Ln;
+    Log.String("  -V           Print version and quit"); Log.Ln;
     Kernel.Quit(1)
   END Help;
 
+  PROCEDURE Version;
+  BEGIN
+    Log.String(version); Log.Ln;
+    Kernel.Quit(0)
+  END Version;
+
   PROCEDURE ParseTargetOpts;
     VAR s: DswOpts.String;
   BEGIN
@@ -212,6 +290,11 @@ MODULE DswMakeMain;
       Strings.ToLower(s, s);
       IF s$ = "none" THEN os := anyos
       ELSIF s$ = "linux" THEN os := linux
+      ELSIF s$ = "freebsd" THEN os := freebsd
+      ELSIF s$ = "openbsd" THEN os := openbsd
+      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)
       END
     | "g":
@@ -233,25 +316,50 @@ MODULE DswMakeMain;
     END
   END ParseTargetOpts;
 
+  PROCEDURE ParseCommandOpts;
+  BEGIN
+    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)
+    END
+  END ParseCommandOpts;
+
+  PROCEDURE ParseExternalOpts;
+  BEGIN
+    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)
+    END
+  END ParseExternalOpts;
+
   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:Th") OF
+      CASE DswOpts.GetOpt("ao:j:D:U:TCXhV") OF
       | "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)
       | "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)
       | 0X: EXIT
       END
-    END
+    END;
   END ParseArgs;
 
   PROCEDURE CheckParams;
@@ -279,6 +387,14 @@ MODULE DswMakeMain;
     END;
     IF (exe # NIL) & (exe^ = "") THEN
       Log.String("output file name can't be empty"); Log.Ln; INC(err)
+    END;
+    IF cpcExe = NIL THEN
+      IF compiler = cpnative THEN cpcExe := NewStr("cpc486")
+      ELSIF compiler = cpfront THEN cpcExe := NewStr("cpfront")
+      END
+    END;
+    IF cplExe = NIL THEN
+      IF linker = dev2 THEN cplExe := NewStr("cpl486") END
     END
   END CheckParams;
 
@@ -301,6 +417,9 @@ MODULE DswMakeMain;
         j := 0; (* find module in global list *)
         WHILE (j < mno) & (modList[j].name$ # name$) DO INC(j) END;
         IF j >= mno THEN
+          IF ~auto THEN
+            Log.String("module " + name + " required before " + m.name); Log.Ln; INC(werr)
+          END;
           NEW(imp); imp.name := name$; imp.selectors := CopySelectorList(m.selectors);
           modList[mno] := imp; INC(mno)
         ELSE
@@ -358,6 +477,29 @@ MODULE DswMakeMain;
           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;
@@ -495,6 +637,7 @@ MODULE DswMakeMain;
       END;
       INC(i)
     END;
+    INC(err, werr);
     num := 0;
     FOR i := 0 TO rno - 1 DO
       Trace(modList[i], modList[i], num)
@@ -536,6 +679,23 @@ MODULE DswMakeMain;
     RETURN ready
   END Ready;
 
+  PROCEDURE PutParams (w: DswProcs.Process; p: StringList);
+    VAR i: INTEGER;
+  BEGIN
+    ASSERT(w # NIL, 20);
+    IF debugArgs THEN Log.String("PutParams") END;
+    IF p # NIL THEN
+      IF debugArgs THEN Log.String(":[" + p[0]) END;
+      w.PutParam(p[0]);
+      FOR i := 1 TO LEN(p) - 1 DO
+        IF debugArgs THEN Log.String("|" + p[i]) END;
+        w.PutParam(p[i])
+      END;
+      IF debugArgs THEN Log.Char("]") END
+    END;
+    IF debugArgs THEN Log.Ln END
+  END PutParams;
+
   PROCEDURE ExecuteCompiler (m: Module): DswProcs.Process;
     VAR w: DswProcs.Process; ok: BOOLEAN;
   BEGIN
@@ -543,11 +703,90 @@ MODULE DswMakeMain;
     ASSERT(m.path # "", 21);
     ASSERT(m.worker = NIL, 22);
     w := DswProcs.dir.New();
+    w.Program(cpcExe);
+    w.PutParam("-legacy");
+    PutParams(w, cpcArgs);
     CASE compiler OF
-    | cpnative: w.Program("cpc486")
-    | cpfront: w.Program("cpfront")
+    | 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");
     END;
-    w.PutParam("-legacy");
     w.PutParam(m.path);
     w.Execute(ok);
     IF ok THEN
@@ -613,16 +852,16 @@ MODULE DswMakeMain;
     ASSERT((exe # NIL) & (exe^ # ""), 20);
     ASSERT(processor = mach386, 21);
     ASSERT(compiler = cpnative, 22);
-    ASSERT(os IN {linux, freebsd, openbsd, win32}, 23);
     p := DswProcs.dir.New();
-    p.Program("cpl486");
+    p.Program(cplExe);
     IF os # anyos THEN
       p.PutParam("-os");
       CASE os OF
       | linux: p.PutParam("linux")
       | freebsd: p.PutParam("freebsd")
       | openbsd: p.PutParam("openbsd")
-      | win32: p.PutParam("win32")
+      | win32, cygwin: p.PutParam("win32")
+      | darwin: p.PutParam("darwin")
       END
     END;
     p.PutParam("-kernel");
@@ -633,6 +872,7 @@ MODULE DswMakeMain;
     p.PutParam(".");
     p.PutParam("-o");
     p.PutParam(exe);
+    PutParams(p, cplArgs);
     i := 0;
     WHILE i < mno DO
       IF ~(library IN lnkList[i].flags) THEN
@@ -692,15 +932,3 @@ BEGIN
   NEW(def);
   Kernel.Start(Main)
 END DswMakeMain.
-
-==============================
-
-SYNTAX
-  cpmake {options module}
-
-OPTIONS
-  -a                         Enable automatic dependency resolution
-  -o name                    Generate executable file
-  -j num                     Specifies the number of jobs to run simultaneously
-  -D ident["+"|"-"]          Add preprocessor selector
-  -U ident                   Remove proprocessor selector