1 /* do not edit automatically generated by mc from mcPreprocess. */
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
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
21 # if !defined (PROC_D)
23 typedef void (*PROC_t
) (void);
24 typedef struct { PROC_t proc
; } PROC
;
27 #define _mcPreprocess_C
29 #include "GmcPreprocess.h"
31 # include "GDynamicStrings.h"
36 # include "GmcPrintf.h"
37 # include "GmcOptions.h"
39 static alists_alist listOfFiles
;
42 preprocessModule - preprocess a file, filename, returning the new filename
43 of the preprocessed file.
44 Preprocessing will only occur if requested by the user.
45 If no preprocessing was requested then filename is returned.
46 If preprocessing occurs then a temporary file is created
47 and its name is returned.
48 All temporary files will be deleted when the compiler exits.
51 extern "C" DynamicStrings_String
mcPreprocess_preprocessModule (DynamicStrings_String filename
);
57 static DynamicStrings_String
makeTempFile (DynamicStrings_String ext
);
63 static DynamicStrings_String
onExitDelete (DynamicStrings_String filename
);
66 removeFile - removes a single file, s.
69 static void removeFile (void * a
);
75 static void removeFiles (void);
82 static DynamicStrings_String
makeTempFile (DynamicStrings_String ext
)
84 return DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "/tmp/mctemp.", 12), ext
);
85 /* static analysis guarentees a RETURN statement will be used before here. */
86 __builtin_unreachable ();
94 static DynamicStrings_String
onExitDelete (DynamicStrings_String filename
)
96 alists_includeItemIntoList (listOfFiles
, reinterpret_cast <void *> (DynamicStrings_Dup (filename
)));
98 /* static analysis guarentees a RETURN statement will be used before here. */
99 __builtin_unreachable ();
104 removeFile - removes a single file, s.
107 static void removeFile (void * a
)
109 DynamicStrings_String s
;
111 s
= static_cast<DynamicStrings_String
> (a
);
112 if ((libc_unlink (DynamicStrings_string (s
))) != 0)
121 static void removeFiles (void)
123 alists_foreachItemInListDo (listOfFiles
, (alists_performOperation
) {(alists_performOperation_t
) removeFile
});
128 preprocessModule - preprocess a file, filename, returning the new filename
129 of the preprocessed file.
130 Preprocessing will only occur if requested by the user.
131 If no preprocessing was requested then filename is returned.
132 If preprocessing occurs then a temporary file is created
133 and its name is returned.
134 All temporary files will be deleted when the compiler exits.
137 extern "C" DynamicStrings_String
mcPreprocess_preprocessModule (DynamicStrings_String filename
)
139 DynamicStrings_String tempfile
;
140 DynamicStrings_String command
;
141 DynamicStrings_String commandLine
;
144 command
= mcOptions_getCppCommandLine ();
145 if (DynamicStrings_EqualArray (command
, (const char *) "", 0))
151 tempfile
= DynamicStrings_InitStringCharStar (reinterpret_cast <void *> (makeTempFile (DynamicStrings_InitString ((const char *) "cpp", 3))));
152 commandLine
= DynamicStrings_Dup (command
);
153 commandLine
= DynamicStrings_ConCat (DynamicStrings_ConCat (DynamicStrings_ConCat (DynamicStrings_ConCatChar (DynamicStrings_Dup (commandLine
), ' '), filename
), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) " -o ", 4))), tempfile
);
154 if (mcOptions_getVerbose ())
156 mcPrintf_fprintf1 (FIO_StdOut
, (const char *) "%s\\n", 4, (const unsigned char *) &commandLine
, (sizeof (commandLine
)-1));
158 if ((libc_system (DynamicStrings_string (commandLine
))) != 0)
160 mcPrintf_fprintf1 (FIO_StdErr
, (const char *) "C preprocessor failed when preprocessing %s\\n", 45, (const unsigned char *) &filename
, (sizeof (filename
)-1));
163 commandLine
= DynamicStrings_KillString (commandLine
);
164 return onExitDelete (tempfile
);
166 /* static analysis guarentees a RETURN statement will be used before here. */
167 __builtin_unreachable ();
170 extern "C" void _M2_mcPreprocess_init (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])
172 listOfFiles
= alists_initList ();
173 if (! (M2RTS_InstallTerminationProcedure ((PROC
) {(PROC_t
) removeFiles
})))
176 __builtin_unreachable ();
180 extern "C" void _M2_mcPreprocess_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])