简体   繁体   中英

Using GNU Make for Script Building

I have a script (the language is VBScript, but for the sake of the question, it's unimportant) which is used as the basis for a number of other scripts -- call it a "base script" or "wrapper script" for others. I would like to modularize my repository so that this base script can be combined with the functions unique to a specific script instance and then rebuilt later, should either one of the two change.

Example:

  • baseScript.vbs -- Logging, reporting, and other generic functions.

  • queryServerFunctions.vbs -- A script with specific, unique tasks (functions) that depend on functions in baseScript.vbs .

I would like to use make to combine the two (or any arbitrary number of files with script fragments) into a single script -- say, queryServer.vbs -- that is entirely self-contained. This self-contained script could then be rebuilt by make anytime either of its source scripts changes.

The question, then, is: Can I use make to manage script builds and, if so, what is the best or preferred way of doing so?

If it matters, my scripting environment is Cygwin running on Windows 7 x64.

The combination of VBScript and GNU make is unusual, so I doubt you'll find a "preferred way" of doing this. It is certainly possible. Taking your example, and adding another script called fooServer.vbs to show how the solution works for multiple scripts, here's a simple makefile:

# list all possible outputs here
OUTPUT := queryServer.vbs fooServer.vbs

# tell make that the default action should be to produce all outputs
.PHONY: all
all: $(OUTPUT)

# generic recipe for combining scripts together
$(OUTPUT):
        cat $+ > $@
# note: The first character on the line above should be a tab, not a space

# now we describe the inputs for each output file
queryServer.vbs: baseScript.vbs queryServerFunctions.vbs
fooServer.vbs: baseScript.vbs fooFunctions.vbs

That will create the two scripts for you from their inputs, and if you touch, for example queryServerFunctions.vbs then only queryServer.vbs will be remade.

Why go to all that trouble, though?

The purpose of make is to "rebuild" things efficiently, by comparing file timestamps to judge when a build step can be safely skipped because the inputs have not changed since the last time the output file was updated. The assumption is that build steps are expensive, so it's worth skipping them where possible, and the performance gain is worth the risk of skipping too much due to bugs in the makefile or misleading file timestamps.

For copying a few small files around, I would say a simple batch file like

copy /y baseScript.vbs+queryServerFunctions.vbs queryServer.vbs
copy /y baseScript.vbs+fooServerFunctions.vbs fooServer.vbs

would be a better fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM