| 1 |
(* M2Preprocess.mod provides a mechanism to invoke the C preprocessor. |
| 2 |
|
| 3 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 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, Length ; |
| 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 |
|
| 37 |
FROM M2Options IMPORT Verbose, PPonly, GetObj, GetMD, GetMMD, GetCpp, GetMQ, |
| 38 |
CppCommandLine, SaveTemps, GetSaveTempsDir, GetDumpDir, |
| 39 |
GetM, GetMM ; |
| 40 |
|
| 41 |
FROM NameKey IMPORT Name, MakeKey, KeyToCharStar, makekey ; |
| 42 |
|
| 43 |
|
| 44 |
CONST |
| 45 |
Debugging = FALSE ; |
| 46 |
|
| 47 |
VAR |
| 48 |
ListOfFiles: List ; |
| 49 |
|
| 50 |
|
| 51 |
(* |
| 52 |
OnExitDelete - when the application finishes delete filename. |
| 53 |
*) |
| 54 |
|
| 55 |
PROCEDURE OnExitDelete (filename: String) : String ; |
| 56 |
BEGIN |
| 57 |
IF filename # NIL |
| 58 |
THEN |
| 59 |
IF Debugging |
| 60 |
THEN |
| 61 |
printf ("scheduling removal: %s\n", string (filename)) |
| 62 |
END ; |
| 63 |
IncludeItemIntoList (ListOfFiles, makekey (string (filename))) |
| 64 |
END ; |
| 65 |
RETURN filename |
| 66 |
END OnExitDelete ; |
| 67 |
|
| 68 |
|
| 69 |
(* |
| 70 |
RemoveFile - removes a single file, s. |
| 71 |
*) |
| 72 |
|
| 73 |
PROCEDURE RemoveFile (w: WORD) ; |
| 74 |
VAR |
| 75 |
n: Name ; |
| 76 |
BEGIN |
| 77 |
n := w ; |
| 78 |
IF Debugging |
| 79 |
THEN |
| 80 |
printf ("removing: %s\n", KeyToCharStar (n)) |
| 81 |
END ; |
| 82 |
IF unlink (KeyToCharStar (n)) # 0 |
| 83 |
THEN |
| 84 |
END |
| 85 |
END RemoveFile ; |
| 86 |
|
| 87 |
|
| 88 |
(* |
| 89 |
RemoveFiles - |
| 90 |
*) |
| 91 |
|
| 92 |
PROCEDURE RemoveFiles () : INTEGER ; |
| 93 |
BEGIN |
| 94 |
ForeachItemInListDo (ListOfFiles, RemoveFile) ; |
| 95 |
RETURN 0 |
| 96 |
END RemoveFiles ; |
| 97 |
|
| 98 |
(* |
| 99 |
Return the filename with no path. |
| 100 |
*) |
| 101 |
|
| 102 |
PROCEDURE GetFileName (Path: String) : String ; |
| 103 |
VAR |
| 104 |
fstart: INTEGER ; |
| 105 |
BEGIN |
| 106 |
fstart := RIndex(Path, '/', 0) ; |
| 107 |
IF fstart=-1 |
| 108 |
THEN |
| 109 |
fstart := 0 |
| 110 |
ELSE |
| 111 |
fstart := fstart + 1 |
| 112 |
END ; |
| 113 |
RETURN Dup (Slice(Path, fstart, Length (Path))) |
| 114 |
END GetFileName ; |
| 115 |
|
| 116 |
|
| 117 |
(* |
| 118 |
MakeSaveTempsFileName - return a temporary file like |
| 119 |
"./filename.{def,mod}.m2i" in the current working directory unless |
| 120 |
SaveTempsDir = obj, when we put it in the dumpdir if that is specified |
| 121 |
(or fallback to '.' if not). |
| 122 |
We have to keep the original extension because that disambiguates .def |
| 123 |
and .mod files (otherwise, we'd need two 'preprocessed' extensions). |
| 124 |
*) |
| 125 |
|
| 126 |
PROCEDURE MakeSaveTempsFileName (filename: String) : String ; |
| 127 |
BEGIN |
| 128 |
RETURN MakeSaveTempsFileNameExt (filename, InitString ('.m2i')) |
| 129 |
END MakeSaveTempsFileName ; |
| 130 |
|
| 131 |
|
| 132 |
(* |
| 133 |
MakeSaveTempsFileNameExt - creates and return the temporary filename.ext. |
| 134 |
in the current working directory unless |
| 135 |
SaveTempsDir = obj, when we put it in the dumpdir |
| 136 |
if that is specified (or fallback to '.' if not). |
| 137 |
*) |
| 138 |
|
| 139 |
PROCEDURE MakeSaveTempsFileNameExt (filename, ext: String) : String ; |
| 140 |
VAR |
| 141 |
NewName, |
| 142 |
DumpDir, |
| 143 |
NewDir : String ; |
| 144 |
BEGIN |
| 145 |
NewName := ConCat (Dup (GetFileName (filename)), ext) ; |
| 146 |
NewDir := Dup (GetSaveTempsDir ()) ; |
| 147 |
DumpDir := Dup (GetDumpDir ()) ; |
| 148 |
IF Debugging |
| 149 |
THEN |
| 150 |
fprintf1 (StdOut, "newname: %s", NewName) ; |
| 151 |
fprintf1 (StdOut, " NewDir: %s", NewDir) ; |
| 152 |
fprintf1 (StdOut, " DumpDir: %s\n", DumpDir) |
| 153 |
END ; |
| 154 |
IF (NewDir#NIL) AND EqualArray (NewDir, 'obj') AND (DumpDir#NIL) |
| 155 |
THEN |
| 156 |
RETURN ConCat (DumpDir, NewName) |
| 157 |
ELSE |
| 158 |
RETURN ConCat (InitString ('./'), NewName) |
| 159 |
END ; |
| 160 |
END MakeSaveTempsFileNameExt ; |
| 161 |
|
| 162 |
|
| 163 |
(* |
| 164 |
BuildCommandLineExecute - build the cpp command line and execute the command and return |
| 165 |
the tempfile containing the preprocessed source. |
| 166 |
*) |
| 167 |
|
| 168 |
PROCEDURE BuildCommandLineExecute (filename: String; |
| 169 |
topSource, deleteDep: BOOLEAN; |
| 170 |
command, outputdep: String) : String ; |
| 171 |
VAR |
| 172 |
tempfile, |
| 173 |
commandLine: String ; |
| 174 |
BEGIN |
| 175 |
commandLine := Dup (command) ; |
| 176 |
tempfile := NIL ; |
| 177 |
(* We support MD and MMD for the main file only, at present. *) |
| 178 |
IF topSource OR PPonly |
| 179 |
THEN |
| 180 |
IF GetMD () |
| 181 |
THEN |
| 182 |
tempfile := ConCat (InitString(' -MD '), outputdep) |
| 183 |
ELSIF GetMMD () |
| 184 |
THEN |
| 185 |
tempfile := ConCat (InitString(' -MMD '), outputdep) |
| 186 |
END ; |
| 187 |
IF tempfile#NIL |
| 188 |
THEN |
| 189 |
commandLine := ConCat (Dup (commandLine), Dup (tempfile)) ; |
| 190 |
(* We can only add MQ if we already have an MD/MMD. *) |
| 191 |
IF GetMQ () # NIL |
| 192 |
THEN |
| 193 |
tempfile := InitStringCharStar (GetMQ ()) ; |
| 194 |
commandLine := ConCat (Dup (commandLine), Dup (tempfile)) |
| 195 |
END |
| 196 |
END |
| 197 |
END ; |
| 198 |
(* The output file depends on whether we are in stand-alone PP mode, and |
| 199 |
if an output file is specified. *) |
| 200 |
tempfile := NIL ; |
| 201 |
IF PPonly |
| 202 |
THEN |
| 203 |
IF GetObj () # NIL |
| 204 |
THEN |
| 205 |
tempfile := InitStringCharStar (GetObj ()) |
| 206 |
END |
| 207 |
ELSIF SaveTemps |
| 208 |
THEN |
| 209 |
tempfile := MakeSaveTempsFileName (filename) |
| 210 |
ELSE |
| 211 |
tempfile := InitStringCharStar (make_temp_file (KeyToCharStar (MakeKey('.m2i')))) |
| 212 |
END ; |
| 213 |
commandLine := ConCat (ConCatChar (Dup (commandLine), ' '), filename) ; |
| 214 |
IF tempfile # NIL |
| 215 |
THEN |
| 216 |
commandLine := ConCat (ConCat (Dup (commandLine), |
| 217 |
Mark (InitString(' -o '))), tempfile) ; |
| 218 |
END ; |
| 219 |
IF (outputdep # NIL) AND (Length (outputdep) > 0) AND (GetM () OR GetMM ()) |
| 220 |
THEN |
| 221 |
commandLine := ConCat (commandLine, ConCat (Mark (InitString (' -MF ')), |
| 222 |
outputdep)) ; |
| 223 |
IF deleteDep AND (NOT SaveTemps) |
| 224 |
THEN |
| 225 |
outputdep := OnExitDelete (outputdep) |
| 226 |
END |
| 227 |
END ; |
| 228 |
(* use pexecute in the future |
| 229 |
res := pexecute(string(Slice(commandLine, 0, Index(commandLine, ' ', 0))), etc etc ); *) |
| 230 |
(* for now we'll use system *) |
| 231 |
IF Verbose |
| 232 |
THEN |
| 233 |
fprintf1 (StdOut, "preprocess: %s\n", commandLine) |
| 234 |
END ; |
| 235 |
IF system (string (commandLine)) # 0 |
| 236 |
THEN |
| 237 |
fprintf1 (StdErr, 'C preprocessor failed when preprocessing %s\n', filename) ; |
| 238 |
exit (1) |
| 239 |
END ; |
| 240 |
commandLine := KillString (commandLine) ; |
| 241 |
IF SaveTemps |
| 242 |
THEN |
| 243 |
RETURN tempfile |
| 244 |
ELSE |
| 245 |
RETURN OnExitDelete (tempfile) |
| 246 |
END |
| 247 |
END BuildCommandLineExecute ; |
| 248 |
|
| 249 |
|
| 250 |
(* |
| 251 |
PreprocessModule - preprocess a file, filename, returning the new filename |
| 252 |
of the preprocessed file. |
| 253 |
Preprocessing will only occur if requested by the user. |
| 254 |
If no preprocessing was requested then filename is returned. |
| 255 |
If preprocessing occurs then a temporary file is created |
| 256 |
and its name is returned. |
| 257 |
All temporary files will be deleted when the compiler exits. |
| 258 |
outputdep is the filename which will contain the dependency |
| 259 |
info if -M, -MM is provided. outputdep can be NIL in which case |
| 260 |
it is ignored. |
| 261 |
*) |
| 262 |
|
| 263 |
PROCEDURE PreprocessModule (filename: String; |
| 264 |
topSource, deleteDep: BOOLEAN; |
| 265 |
outputDep: String) : String ; |
| 266 |
VAR |
| 267 |
command: String ; |
| 268 |
BEGIN |
| 269 |
IF GetCpp () |
| 270 |
THEN |
| 271 |
command := CppCommandLine () ; |
| 272 |
IF (command = NIL) OR EqualArray (command, '') |
| 273 |
THEN |
| 274 |
RETURN Dup (filename) |
| 275 |
END ; |
| 276 |
command := BuildCommandLineExecute (filename, topSource, deleteDep, |
| 277 |
command, outputDep) ; |
| 278 |
IF command = NIL |
| 279 |
THEN |
| 280 |
RETURN filename |
| 281 |
ELSE |
| 282 |
RETURN command |
| 283 |
END |
| 284 |
ELSE |
| 285 |
RETURN Dup (filename) |
| 286 |
END |
| 287 |
END PreprocessModule ; |
| 288 |
|
| 289 |
|
| 290 |
BEGIN |
| 291 |
InitList (ListOfFiles) ; |
| 292 |
IF atexit (RemoveFiles) # 0 |
| 293 |
THEN |
| 294 |
HALT |
| 295 |
END |
| 296 |
END M2Preprocess. |