Maybe not the best example, but if you have a project with several weird things like that, make is often easier.
And can do what a shell script can.
Could you elaborate on its advantages vs just?
* Snakefile -> requires installation
* bash -> macOS is now ZSH. (Is bash a better choice than Make?)
* python -> what if I'm not using python?
What else is as "accessible" as Makefile?
EDIT: formatting
Makefiles don't run code themselves, the recipe is written to a file and executed using "sh" (at least by default, the shell to use is changeable).
At least, that's how I prefer to do it. A project that's heavy JS? Use a JS task runner. Heavy python? Python task runner. Heavy shell? And so on.
Trying to keep up with JS task runners for web projects is why I started using Make and haven’t looked back.
I hate JS as much as the next guy, but if I'm developing in JS, I use a JS task runner. Even a simple one. There's not much to "keep up with" IMO - they are quite easy to create and use, if the common/popular ones are missing features or move too fast for you.
make is not part of the default macOS install - You need to install developer tools - which I suppose if you are a developer is OK.
As for bash as it is an old verion on macOS you need to stick to a simpler subset - better write as sh.
So sh seems to be the most general.
Then consider Windows - ah ... You are going to have to install something. Shells don't run natively and how do you get snakefile or just.
So python or make are the cross platform tools you have
> and docker doesn’t give definable artifacts so you can’t make other things depend on them either
Similar to how old projects have "make configure" to do some initial setup before "make" actually builds the project, I've done stuff on occasion where something like "make check" would pull information out of a system and create timestamped files in a scratch/ directory. Then the normal "make" would compare those files to the codebase to see if the system needed to be updated.
It is different from the first use I just mentioned, since you need two commands and it's not entirely automatic, but it's still simpler than checking each of the dependencies yourself.
It is very convenient never having to ask oneself "Is my node_modules up to date?" after pulling or switching branches. I'll just let Make figure that out.
I do, however, like having some of the amenities of a task runner without bringing in the "cruft" that's suited toward incremental builds. I like using Just (https://just.systems/) for those cases.
It's more task-oriented, the way people tend to write Makefiles with .PHONY rules, but it's all in go. It can be bootstrapped just with go too, and comes with some utilities to do make-like incremental builds if you need to.
Btw, from the linked page:
> Makefiles are hard to read and hard to write. Mostly because makefiles are essentially fancy bash scripts with significant white space and additional make-related syntax.
Wait what? What does bash have to do with anything? Mage may well be amazing, but it doesn't sound like this person knows make that well at all. Which makes me think they're simply trying to reinvent the wheel -- in 'go'.
Make is still really about file to file transformations, and `go` already wraps up all of the behavior one would normally use make for. Plus you need make + a shell + go, vs. mage where all that's needed is go.
I can't speak for the author, but I assume they're reacting to how Makefiles tend to be used in go projects and not how make works generally.
GO_SRCS=$(shell find src -name '*.go' -not -name '*_test.go)
GO_TESTS=$(shell find src -name '*_test.go')
.PHONY: run
docker-run: target/image.target
docker run example
.PHONY: integration-test
integration-test: target/integration-test.target
.PHONY: test
test: target/test.target
target/bin: $(GO_SRCS)
mkdir -p $(@D)
GOOS=linux-amd64 go build -ldflags='-s -w' -o $@
target/context.target: Dockerfile target/bin
rm -fr $(@:.target=)
mkdir -p $(@:.target=)
cp Dockerfile target/bin $(@:.target=)
target/image.target: target/context.target
docker build -t example $(<:.target=)
target/integration-test.target: target/image.target
# TODO: test the docker image
target/test.target: $(GO_SRCS) $(GO_TESTS)
go test
What is your better thing?(The answer is Bazel, but if you think Make is a "supercar".......)
You're using make to run a bunch of tasks, including where tasks may depend on other tasks having been run before.
A task-runner tool like `just` is better suited to this task. https://github.com/casey/just
`just` has some nice UX improvements over `make`. (e.g. doesn't require soft tabs, can list recipes out of the box, recipes can take command line arguments, supports .env files, can be run from any subdirectory).
> You're using make to run a bunch of tasks, including where tasks may depend on other tasks having been run before.
And the cool thing is....it works for both!
It can represent an actual file (e.g. a compiled binary) or abstract result (e.g., a successful test).
Make is a tool to make files based on dependencies
It was not created as a scripting language. It understands trees.
It is not to “make” your project. It “makes” “files”
Seems like useful information to me....
It's best practice, though the only time you need it is when a virtual target happens to have the same name as an actual file.
Unless the question was, why do I have to mark targets as PHONY more generally.
In which case the answer is, you don't. You only do this when you want the recipe to always run when invoked, as opposed to the standard mode where invocation is conditional on the file being out-of-date.
A shell script?
Yes, abusing Make the way I and many others do is not the ideal use case, but it works just fine. It's good enough at being a task manager.
I do still use it for simple automation, because it's nice to have a language-agnostic way to do simple things. But once it grows beyond about a page of text it tends to become a real nightmare, and is nigh impossible for most people to help maintain... which is not at all helped by its absolute lack of clear best-practices or warnings when misconfigured, and poor meshing with many common systems (like source-modifying tools, e.g. gofmt).
It doesn't help that every guide starts out with
# so easy!
thing:
./build thing
When in reality you pretty much always need at least how this page ends, to be even slightly stable and maintainable: # The final build step.
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# Build step for C source
$(BUILD_DIR)/%.c.o: %.c
mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# Build step for C++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
mkdir -p $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
It's a horrifying bait-and-switch that a lot of people never fully learn their way through to the end.Which is an immediate consequence of make being built to make things, not run tasks. So it goes.
- clean out build artefacts - rebuild test builds - run tests
sounds very much ok to me.
Order isn't guaranteed, and most clean targets won't have any dependency relationship with test targets, so it could test and then clean. Or interleave them (clean between test dependencies). Or run them both simultaneously if someone has set the -j flag, and then who knows what happens.
Other than that, make's behavior when a target updates dependencies of another target which do not share dependencies can get extremely complicated, and often depends on execution order. Clean generally affects many/most, so it's sometimes very problematic to run with any other.
And what tool is as powerful that doesn’t have the ‘problem’ that a lot of people never learn it fully? (Why would I learn make or any other tool to the full? I browse its manual so that I know what it can do, and (hopefully) remember features exist when I need them, so that I can (often temporarily) learn them then)
It's the uncountable number of edge cases, lack of versioning/features (you can't declare what you need, you just succeed/fail/misbehave, often silently), massive massive problems dealing with entirely normal things in file paths like spaces, and more useful output than `make -d` for very common cases like "why am I rebuilding every time" / accidentally cyclic dependencies.
For starters.
Make does a lot right, but the amount of inconsistencies and friction to be reliable with it is truly absurd and unnecessary, and you need to go to extreme lengths to correctly handle them (e.g. cmake).
It always starts with `print 'Hello world'`, but it isn't that simple.
The only complexity there is getting it installed on everyone's machine, but that's true of most tooling, even the common stuff given versions won't match. I solve that with Nix.
Make is pretty darn far down the "deeply inconsistent and error-prone" side of things when you try to do anything correctly and reliably with it, particularly on multiple systems and across various implementations/versions. It survives because it's ubiquitous and just barely good enough.
* http://jdebp.uk/Softwares/nosh/guide/commands/reboot.xml
* https://www.freedesktop.org/software/systemd/man/reboot.html
I suspect that you may have just started a list. (-:
That being said I really do like this article. I want to learn how to use make properly.
I really don’t like Make for the common use-cases it is being used for in the modern day, such as the Golang ecosystem or in many cases: creating docker files.
Make, at its core, is a tool that is meant to help you structure your dependencies for incrementally building things, but people use it as a glorified task runner simply due to the fact that it’s such a common build tool that it is nearly always installed if you have a development toolchain on your machine.
Go and docker already have incremental compilation built in, and docker doesn’t give definable artifacts so you can’t make other things depend on them either
It is a powerful tool, but the syntax is hideous and has more jagged edges than bash does, and we aren’t even using it in a way that justifies this. Makes me so frustrated for some reason.
Like everyone deciding to use a supercar to do farmwork.