Pre and Post Processing for BMK

BlitzMax Forums/BlitzMax Module Tweaks/Pre and Post Processing for BMK

Otus(Posted 2008) [#1]
The pre part is a dirty hack - if something goes wrong, the original source file may be corrupted, though a copy is made. If someone can figure out a better way to do this, please let me know. Potential uses include something like upx or an icon hacker.

Use --pre <path> and --post <path> as command line options for BMK to enable it. Paths may be full paths or file names in BlitzMax/bin. Extension is needed, because the file is checked to exist.

The pre processor should support -o targetfile sourcefile and the post processor should simply process the executable passed as an argument.

For example, I have upx.exe in my BlitzMax\bin folder, so I can compile apps with bmk makeapp --post upx.exe file.bmx for automatic compression of the executable.

Here's the diff against revision 92 of the SVN source (current):

Index: bmk_config.bmx
===================================================================
--- bmk_config.bmx	(revision 92)
+++ bmk_config.bmx	(working copy)
@@ -27,6 +27,8 @@
 Global opt_proxyport
 Global opt_traceheaders
 Global opt_appstub$="brl.appstub" ' BaH 28/9/2007
+Global opt_preprocess$
+Global opt_postprocess$
 
 Global opt_dumpbuild
 
@@ -134,6 +136,14 @@
 			n:+1
 			If n=args.length CmdError
 			opt_appstub=args[n]
+		Case "-pre"
+			n:+1
+			If n=args.length CmdError
+			opt_preprocess=FindPP(args[n])
+		Case "-post"
+			n:+1
+			If n=args.length CmdError
+			opt_postprocess=FindPP(args[n])
 		Default
 			CmdError
 		End Select
@@ -143,4 +153,10 @@
 
 End Function
 
+Function FindPP:String(path$) 
+	If FileType(path) = FILETYPE_FILE Return path
+	path = BlitzMaxPath() + "/bin/" + path
+	If FileType(path) = FILETYPE_FILE Return path
+	Throw "Unable to find preprocessor."
+End Function
 
Index: bmk_make.bmx
===================================================================
--- bmk_make.bmx	(revision 92)
+++ bmk_make.bmx	(working copy)
@@ -304,8 +304,26 @@
 				Local opts$=bcc_opts
 				If main_file opts=" -t "+opt_apptype+opts
 			
-				CompileBMX src_path,obj_path,opts
-						
+				If opt_preprocess
+					Local src_ppcopy$
+					src_ppcopy = ExtractDir(src_path) + "/.bmx/" + StripDir(src_path)
+					CopyFile src_path , src_ppcopy
+					Print "Pre processing..."
+					Sys( CQuote(opt_preprocess) + " -o " + CQuote(src_path) + " " + CQuote(src_path) ) 
+					Try
+						CompileBMX src_path,obj_path,opts
+					Catch o:Object
+						DeleteFile src_path
+						CopyFile src_ppcopy , src_path
+						Throw o
+					End Try
+					DeleteFile src_path
+					CopyFile src_ppcopy , src_path
+					DeleteFile src_ppcopy
+				Else
+					CompileBMX src_path,obj_path,opts
+				End If
+					
 				If EXPERIMENTAL_SPEEDUP
 					Local i_path$=StripExt( obj_path )+".i"
 
@@ -377,6 +395,10 @@
 		lnk_files=FilePaths( lnk_files )
 		AddList ext_files,lnk_files
 		LinkApp opt_outfile,lnk_files,makelib
+		If opt_postprocess
+			Print "Post processing..."
+			Sys( CQuote(opt_postprocess) + " " + CQuote(opt_outfile) ) 
+		End If
 	EndIf
 	
 	app_main=""