Is dependency on a symlink possible in a Makefile?
Asked Answered
A

2

9

I need a couple of symlinks in my project.

From src/openlayers, folders img and theme have to be symlinked in contrib/openlayers. The contrib/openlayers folder should also be created automatically.

.PHONY: run
run: contrib/openlayers/theme contrib/openlayers/img
   ../bin/pserve development.ini --reload

contrib/openlayers/theme:
    ln -s src/openlayers/theme $@

contrib/openlayers/img:
    ln -s src/openlayers/img $@

But this rule tries to create symlinks every time. (I put -f flag to ln, so it re-creates the symlinks every time.)

Aggappe answered 16/4, 2012 at 20:7 Comment(0)
B
4

Sure, this can work. Make treats everything like a file, including a symlink. It will check if the file exists (since you don't list any prerequisites, there is no timestamp comparison). In the case of a symlink it's really checking whatever the link points to, of course, not the link itself.

You don't show what happens when you do this but based on your description one of two things is happening: either (a) the contrib/openlayers directory doesn't exist so the ln command is generating an error and not creating the symlink so of course make will try to recreate it the next time it runs, or (b) your symlink is being created incorrectly and pointing to nothing, which means when make tries to see if it exists it fails and make will try to recreate it.

If, for example, your src directory is a sibling of your contrib directory, then your symlinks are just wrong; you'll get:

contrib/openlayers/theme -> src/openlayers/theme

Or, when the kernel tries to resolve it:

contrib/openlayers/src/openlayers/theme

It's highly unlikely that's what you want. I suggest you use something like this:

contrib/openlayers/theme:
        mkdir -p contrib/openlayers
        ln -s ../../src/openlayers/theme contrib/openlayers/theme

Then verify that the symlink, once created, actually points where you want it to go.

Beaut answered 16/4, 2012 at 21:21 Comment(0)
L
21

In case you ever you run into this problem in spite of your symlink pointing correctly to an existing file: also keep in mind that "make" looks at the mtime of the destination file of the symlink, and not at the mtime of the symbolic link itself.

Therefore, if the rule that calls "ln -s" has any dependency that is newer than the file your symbolic links points at, then "make" has to rerun the commands in that rule each time. It will do so again and again because creating a symlink that points to a file does not update the mtime of that file.

You may be able to use the "touch" command to ensure that the destination of your link has a more recent mtime than your dependency.

Liberalize answered 2/10, 2013 at 13:32 Comment(1)
Good point! Helped me figure out why my rule kept wanting to create the link (https://mcmap.net/q/768928/-opencv-and-python-virtualenv)Budwig
B
4

Sure, this can work. Make treats everything like a file, including a symlink. It will check if the file exists (since you don't list any prerequisites, there is no timestamp comparison). In the case of a symlink it's really checking whatever the link points to, of course, not the link itself.

You don't show what happens when you do this but based on your description one of two things is happening: either (a) the contrib/openlayers directory doesn't exist so the ln command is generating an error and not creating the symlink so of course make will try to recreate it the next time it runs, or (b) your symlink is being created incorrectly and pointing to nothing, which means when make tries to see if it exists it fails and make will try to recreate it.

If, for example, your src directory is a sibling of your contrib directory, then your symlinks are just wrong; you'll get:

contrib/openlayers/theme -> src/openlayers/theme

Or, when the kernel tries to resolve it:

contrib/openlayers/src/openlayers/theme

It's highly unlikely that's what you want. I suggest you use something like this:

contrib/openlayers/theme:
        mkdir -p contrib/openlayers
        ln -s ../../src/openlayers/theme contrib/openlayers/theme

Then verify that the symlink, once created, actually points where you want it to go.

Beaut answered 16/4, 2012 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.