4 ms·
I use the gcc preprocessor in Javascript (to avoid modules and tools like browserify): out="out/make1.js" main="make1/main.js" gcc -C -E -H -P -nos
by mabynogy 9y ago
I use the gcc preprocessor in Javascript (to avoid modules and tools like browserify):
out="out/make1.js"
main="make1/main.js"
gcc -C -E -H -P -nostdinc -undef -x c $main -o $out
node --harmony $out $@
- kevin_thibedeau 9y agoYou would be better served with m4.
- flukus 9y agoI looked into m4 recently but it seems to be about the worst template engine imaginable. I'd take the c pre-processor over that any day. In the end I opted for a sed/awk/cat combo instead.
- daxelrod 9y agoCould you please point to some good m4 learning and reference resources? The only time I had to write m4 was admittedly in the context of autotools, but I found it really difficult to get answers to my questions about how everything worked.
- s_kilk 9y agoI wrote up a little tutorial on using m4 as a CSS pre-processor: http://blog.00null.net/post/144763147991/use-the-unix-m4-as-a-css-pre-processor http://blog.00null.net/post/144763147991/use-the-unix-m4-as-...
- flukus 9y agoWow thanks, that's a much better intro to the ones I cam across like this (http://mbreen.com/m4.html http://mbreen.com/m4.html). I may have to reevaluate it.
- daxelrod 9y agoCould you please point to some good m4 learning and reference resources? The only time I had to write m4 was admittedly in the context of autotools, but I found it really difficult to get answers to my questions about how everything worked.
- ndesaulniers 9y agolooked up these flags, since I didn't know all of them: -C: compile but do not link (I think this is covered by -E) -E: stop after running the preprocessor -H: print the name of each header file included, indenting child #includes (what do you use this for? is it printed to stderr?) -P: inhibit the generation of line markers from the preprocessor (I've used this before, but can't recall the problem it solves? Not expanding __LINE__?) -nostdinc: do not search standard system directories for header files (/usr/include, /usr/local/include, ..., this is standard in the Linux kernel, since there is no libc) -undef: do not predefine any system-specific or GCC macros (probably to not expand occurrences of sequences like "__GNUC__" and others from unexpectedly being expanded). -x <language>: specify the language, rather then letting the compiler guess based on the file extension (is this required?)
- Goopplesoft 9y agoThanks. Also, this site also seems to have args from man pages: https://explainshell.com/explain?cmd=gcc+-C+-E+-H+-P+-nostdinc+-undef+-x+c+%24main+-o+%24out https://explainshell.com/explain?cmd=gcc+-C+-E+-H+-P+-nostdi...
- ndesaulniers 9y agoThat's a neat utility! Thanks for sharing.
- dima55 9y agoyou mixed up -c and -C. Here he's preserving comments
- ksherlock 9y ago> -P: inhibit the generation of line markers from the preprocessor (I've used this before, but can't recall the problem it solves? Not expanding __LINE__?) It prevents #line directives which are useful for a C compiler but erroneous for javascript.
- slackingoff2017 9y agoWhat the fuck! I don't know if you're a genius or just really crazy
- AceJohnny2 9y agoMeh, it's just using the C PreProcessor, cpp, but driven through GCC. The C preprocessor is pretty simple (well, until you start wanting to evaluate C expressions as part of your #ifdefs) and very well-known. It's not inherently tied to C (QED), so it makes sense. There are other cases where people use it on something other than C, but as kevin_thibedeau mentions, he'd be better served with a more powerful (and generic) macro processor such as M4.
- pierrec 9y agoAs another abuser of the C preprocessor, I'll point out that you can greatly simplify your call to gcc... by not using gcc, just call cpp directly: cpp -P -H $main $out should be equivalent, unless I'm missing something. And since somebody was asking why use -H, I'm guessing it's just for the useful verbosity of it. I've found it also causes helpful notices to be printed like "header guards may be useful for the following file..."
- mkup 9y agoYes, preprocessor is available as a standalone tool for preprocessing source files in assembly language (.S = input file before preprocessing, .s = output file after preprocessing or source file which does not require preprocessing). Of course standalone preprocessor can be (ab)used to preprocess Javascript source files and any other files, as grandparent commenter suggested.