| 1 |
/* do not edit automatically generated by mc from mcPreprocess. */ |
| 2 |
/* This file is part of GNU Modula-2. |
| 3 |
|
| 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 |
| 7 |
version. |
| 8 |
|
| 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 |
| 12 |
for more details. |
| 13 |
|
| 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/>. */ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
#include "system.h" |
| 20 |
#include <stdbool.h> |
| 21 |
# if !defined (PROC_D) |
| 22 |
# define PROC_D |
| 23 |
typedef void (*PROC_t) (void); |
| 24 |
typedef struct { PROC_t proc; } PROC; |
| 25 |
# endif |
| 26 |
|
| 27 |
#define _mcPreprocess_C |
| 28 |
|
| 29 |
#include "GmcPreprocess.h" |
| 30 |
# include "GSYSTEM.h" |
| 31 |
# include "GDynamicStrings.h" |
| 32 |
# include "Glibc.h" |
| 33 |
# include "Galists.h" |
| 34 |
# include "GM2RTS.h" |
| 35 |
# include "GFIO.h" |
| 36 |
# include "GmcPrintf.h" |
| 37 |
# include "GmcOptions.h" |
| 38 |
|
| 39 |
static alists_alist listOfFiles; |
| 40 |
|
| 41 |
/* |
| 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. |
| 49 |
*/ |
| 50 |
|
| 51 |
extern "C" DynamicStrings_String mcPreprocess_preprocessModule (DynamicStrings_String filename); |
| 52 |
|
| 53 |
/* |
| 54 |
makeTempFile - |
| 55 |
*/ |
| 56 |
|
| 57 |
static DynamicStrings_String makeTempFile (DynamicStrings_String ext); |
| 58 |
|
| 59 |
/* |
| 60 |
onExitDelete - |
| 61 |
*/ |
| 62 |
|
| 63 |
static DynamicStrings_String onExitDelete (DynamicStrings_String filename); |
| 64 |
|
| 65 |
/* |
| 66 |
removeFile - removes a single file, s. |
| 67 |
*/ |
| 68 |
|
| 69 |
static void removeFile (void * a); |
| 70 |
|
| 71 |
/* |
| 72 |
removeFiles - |
| 73 |
*/ |
| 74 |
|
| 75 |
static void removeFiles (void); |
| 76 |
|
| 77 |
|
| 78 |
/* |
| 79 |
makeTempFile - |
| 80 |
*/ |
| 81 |
|
| 82 |
static DynamicStrings_String makeTempFile (DynamicStrings_String ext) |
| 83 |
{ |
| 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 (); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
/* |
| 91 |
onExitDelete - |
| 92 |
*/ |
| 93 |
|
| 94 |
static DynamicStrings_String onExitDelete (DynamicStrings_String filename) |
| 95 |
{ |
| 96 |
alists_includeItemIntoList (listOfFiles, reinterpret_cast <void *> (DynamicStrings_Dup (filename))); |
| 97 |
return filename; |
| 98 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 99 |
__builtin_unreachable (); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/* |
| 104 |
removeFile - removes a single file, s. |
| 105 |
*/ |
| 106 |
|
| 107 |
static void removeFile (void * a) |
| 108 |
{ |
| 109 |
DynamicStrings_String s; |
| 110 |
|
| 111 |
s = static_cast<DynamicStrings_String> (a); |
| 112 |
if ((libc_unlink (DynamicStrings_string (s))) != 0) |
| 113 |
{} /* empty. */ |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/* |
| 118 |
removeFiles - |
| 119 |
*/ |
| 120 |
|
| 121 |
static void removeFiles (void) |
| 122 |
{ |
| 123 |
alists_foreachItemInListDo (listOfFiles, (alists_performOperation) {(alists_performOperation_t) removeFile}); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/* |
| 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. |
| 135 |
*/ |
| 136 |
|
| 137 |
extern "C" DynamicStrings_String mcPreprocess_preprocessModule (DynamicStrings_String filename) |
| 138 |
{ |
| 139 |
DynamicStrings_String tempfile; |
| 140 |
DynamicStrings_String command; |
| 141 |
DynamicStrings_String commandLine; |
| 142 |
unsigned int pos; |
| 143 |
|
| 144 |
command = mcOptions_getCppCommandLine (); |
| 145 |
if (DynamicStrings_EqualArray (command, (const char *) "", 0)) |
| 146 |
{ |
| 147 |
return filename; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 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 ()) |
| 155 |
{ |
| 156 |
mcPrintf_fprintf1 (FIO_StdOut, (const char *) "%s\\n", 4, (const unsigned char *) &commandLine, (sizeof (commandLine)-1)); |
| 157 |
} |
| 158 |
if ((libc_system (DynamicStrings_string (commandLine))) != 0) |
| 159 |
{ |
| 160 |
mcPrintf_fprintf1 (FIO_StdErr, (const char *) "C preprocessor failed when preprocessing %s\\n", 45, (const unsigned char *) &filename, (sizeof (filename)-1)); |
| 161 |
libc_exit (1); |
| 162 |
} |
| 163 |
commandLine = DynamicStrings_KillString (commandLine); |
| 164 |
return onExitDelete (tempfile); |
| 165 |
} |
| 166 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 167 |
__builtin_unreachable (); |
| 168 |
} |
| 169 |
|
| 170 |
extern "C" void _M2_mcPreprocess_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 171 |
{ |
| 172 |
listOfFiles = alists_initList (); |
| 173 |
if (! (M2RTS_InstallTerminationProcedure ((PROC ) {(PROC_t) removeFiles}))) |
| 174 |
{ |
| 175 |
M2RTS_HALT (-1); |
| 176 |
__builtin_unreachable (); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
extern "C" void _M2_mcPreprocess_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 181 |
{ |
| 182 |
} |