1 (* Copyright (C) 2015-2022 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
4 GNU Modula-2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
9 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License along
15 with gm2; see the file COPYING. If not, write to the Free Software
16 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *)
18 IMPLEMENTATION MODULE mcPreprocess ;
21 FROM SYSTEM IMPORT ADDRESS ;
23 FROM DynamicStrings IMPORT string, InitString, Mark, KillString, EqualArray, InitStringCharStar,
24 Dup, ConCat, ConCatChar, RIndex, Slice ;
26 FROM libc IMPORT system, exit, unlink, printf ;
27 FROM alists IMPORT alist, initList, killList, includeItemIntoList, foreachItemInListDo ;
28 FROM M2RTS IMPORT InstallTerminationProcedure ;
29 FROM FIO IMPORT StdErr, StdOut ;
30 FROM mcPrintf IMPORT fprintf1 ;
31 FROM mcOptions IMPORT getVerbose, getCppCommandLine ;
42 PROCEDURE makeTempFile (ext: String) : String ;
44 RETURN ConCat (InitString ('/tmp/mctemp.'), ext)
52 PROCEDURE onExitDelete (filename: String) : String ;
54 includeItemIntoList (listOfFiles, Dup (filename)) ;
60 removeFile - removes a single file, s.
63 PROCEDURE removeFile (a: ADDRESS) ;
68 IF unlink (string (s))#0
78 PROCEDURE removeFiles ;
80 foreachItemInListDo (listOfFiles, removeFile)
85 preprocessModule - preprocess a file, filename, returning the new filename
86 of the preprocessed file.
87 Preprocessing will only occur if requested by the user.
88 If no preprocessing was requested then filename is returned.
89 If preprocessing occurs then a temporary file is created
90 and its name is returned.
91 All temporary files will be deleted when the compiler exits.
94 PROCEDURE preprocessModule (filename: String) : String ;
101 command := getCppCommandLine () ;
102 IF EqualArray (command, '')
106 tempfile := InitStringCharStar (makeTempFile (InitString ('cpp'))) ;
107 commandLine := Dup (command) ;
108 commandLine := ConCat (ConCat (ConCat (ConCatChar (Dup (commandLine), ' '), filename),
109 Mark (InitString(' -o '))),
113 fprintf1 (StdOut, "%s\n", commandLine)
115 IF system (string (commandLine))#0
117 fprintf1(StdErr, 'C preprocessor failed when preprocessing %s\n', filename) ;
120 commandLine := KillString (commandLine) ;
121 RETURN onExitDelete (tempfile)
123 END preprocessModule ;
127 listOfFiles := initList () ;
128 IF NOT InstallTerminationProcedure (removeFiles)