1 (* M2Preprocess.mod provides a mechanism to invoke the C preprocessor.
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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/>. *)
22 IMPLEMENTATION MODULE M2Preprocess ;
25 FROM SYSTEM IMPORT WORD ;
27 FROM DynamicStrings IMPORT string, InitString, Mark, KillString, EqualArray, InitStringCharStar,
28 Dup, ConCat, ConCatChar, RIndex, Slice ;
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 ;
48 PROCEDURE OnExitDelete (filename: String) : String ;
50 IncludeItemIntoList (ListOfFiles, makekey (filename)) ;
56 RemoveFile - removes a single file, s.
59 PROCEDURE RemoveFile (w: WORD) ;
64 IF unlink (KeyToCharStar (n)) # 0
74 PROCEDURE RemoveFiles () : INTEGER ;
76 ForeachItemInListDo (ListOfFiles, RemoveFile) ;
82 MakeSaveTempsFileName - return a temporary file "filename.i".
85 PROCEDURE MakeSaveTempsFileName (filename: String) : String ;
87 RETURN ConCat (Dup (filename), InitString ('.i'))
88 END MakeSaveTempsFileName ;
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.
101 PROCEDURE PreprocessModule (filename: String) : String ;
105 commandLine: String ;
107 command := CppCommandLine () ;
108 IF (command = NIL) OR EqualArray (command, '')
114 tempfile := InitStringCharStar (MakeSaveTempsFileName (filename))
116 tempfile := InitStringCharStar (make_temp_file (KeyToCharStar (MakeKey('i'))))
118 commandLine := Dup (command) ;
119 commandLine := ConCat (ConCat (ConCat (ConCatChar (Dup (commandLine), ' '), filename),
120 Mark (InitString(' -o '))),
122 (* use pexecute in the future
123 res := pexecute(string(Slice(commandLine, 0, Index(commandLine, ' ', 0))), etc etc );
125 (* for now we'll use system *)
128 fprintf1 (StdOut, "preprocess: %s\n", commandLine)
130 IF system (string (commandLine)) # 0
132 fprintf1 (StdErr, 'C preprocessor failed when preprocessing %s\n', filename) ;
135 commandLine := KillString (commandLine) ;
140 RETURN OnExitDelete (tempfile)
143 END PreprocessModule ;
147 InitList (ListOfFiles) ;
148 IF atexit (RemoveFiles) # 0