]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-compiler/M2Preprocess.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-compiler / M2Preprocess.mod
1 (* M2Preprocess.mod provides a mechanism to invoke the C preprocessor.
2
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2Preprocess ;
23
24
25 FROM SYSTEM IMPORT WORD ;
26
27 FROM DynamicStrings IMPORT string, InitString, Mark, KillString, EqualArray, InitStringCharStar,
28 Dup, ConCat, ConCatChar, RIndex, Slice ;
29
30 FROM choosetemp IMPORT make_temp_file ;
31 FROM pexecute IMPORT pexecute ;
32 FROM libc IMPORT system, exit, unlink, printf, atexit ;
33 FROM Lists IMPORT List, InitList, KillList, IncludeItemIntoList, ForeachItemInListDo ;
34 FROM FIO IMPORT StdErr, StdOut ;
35 FROM M2Printf IMPORT fprintf1 ;
36 FROM M2Options IMPORT Verbose, CppCommandLine, SaveTemps ;
37 FROM NameKey IMPORT Name, MakeKey, KeyToCharStar, makekey ;
38
39
40 VAR
41 ListOfFiles: List ;
42
43
44 (*
45 OnExitDelete -
46 *)
47
48 PROCEDURE OnExitDelete (filename: String) : String ;
49 BEGIN
50 IncludeItemIntoList (ListOfFiles, makekey (filename)) ;
51 RETURN filename
52 END OnExitDelete ;
53
54
55 (*
56 RemoveFile - removes a single file, s.
57 *)
58
59 PROCEDURE RemoveFile (w: WORD) ;
60 VAR
61 n: Name ;
62 BEGIN
63 n := w ;
64 IF unlink (KeyToCharStar (n)) # 0
65 THEN
66 END
67 END RemoveFile ;
68
69
70 (*
71 RemoveFiles -
72 *)
73
74 PROCEDURE RemoveFiles () : INTEGER ;
75 BEGIN
76 ForeachItemInListDo (ListOfFiles, RemoveFile) ;
77 RETURN 0
78 END RemoveFiles ;
79
80
81 (*
82 MakeSaveTempsFileName - return a temporary file "filename.i".
83 *)
84
85 PROCEDURE MakeSaveTempsFileName (filename: String) : String ;
86 BEGIN
87 RETURN ConCat (Dup (filename), InitString ('.i'))
88 END MakeSaveTempsFileName ;
89
90
91 (*
92 PreprocessModule - preprocess a file, filename, returning the new filename
93 of the preprocessed file.
94 Preprocessing will only occur if requested by the user.
95 If no preprocessing was requested then filename is returned.
96 If preprocessing occurs then a temporary file is created
97 and its name is returned.
98 All temporary files will be deleted when the compiler exits.
99 *)
100
101 PROCEDURE PreprocessModule (filename: String) : String ;
102 VAR
103 tempfile,
104 command,
105 commandLine: String ;
106 BEGIN
107 command := CppCommandLine () ;
108 IF (command = NIL) OR EqualArray (command, '')
109 THEN
110 RETURN filename
111 ELSE
112 IF SaveTemps
113 THEN
114 tempfile := InitStringCharStar (MakeSaveTempsFileName (filename))
115 ELSE
116 tempfile := InitStringCharStar (make_temp_file (KeyToCharStar (MakeKey('i'))))
117 END ;
118 commandLine := Dup (command) ;
119 commandLine := ConCat (ConCat (ConCat (ConCatChar (Dup (commandLine), ' '), filename),
120 Mark (InitString(' -o '))),
121 tempfile) ;
122 (* use pexecute in the future
123 res := pexecute(string(Slice(commandLine, 0, Index(commandLine, ' ', 0))), etc etc );
124 *)
125 (* for now we'll use system *)
126 IF Verbose
127 THEN
128 fprintf1 (StdOut, "preprocess: %s\n", commandLine)
129 END ;
130 IF system (string (commandLine)) # 0
131 THEN
132 fprintf1 (StdErr, 'C preprocessor failed when preprocessing %s\n', filename) ;
133 exit (1)
134 END ;
135 commandLine := KillString (commandLine) ;
136 IF SaveTemps
137 THEN
138 RETURN tempfile
139 ELSE
140 RETURN OnExitDelete (tempfile)
141 END
142 END
143 END PreprocessModule ;
144
145
146 BEGIN
147 InitList (ListOfFiles) ;
148 IF atexit (RemoveFiles) # 0
149 THEN
150 HALT
151 END
152 END M2Preprocess.