Make and last modification times of directories on Linux

前端 未结 3 2037
花落未央
花落未央 2021-01-24 16:08

Consider the following makefile:

foo :
    mkdir foo

foo/a.out : foo a.in
    cp a.in foo/a.out

foo/b.out : foo b.in
    cp b.in foo/b.out

an

相关标签:
3条回答
  • 2021-01-24 16:27

    I like @Beta's answer, but it is not portable. For a simple portable workaround, create a sentinel file when you create the directory, and depend on the sentinel file instead.

    foo/.dir:
            mkdir -p foo
            touch $@
    foo/a.out: a.in foo/.dir
            cp $< $@
    foo/b.out: b.in foo/.dir
            cp $< $@
    

    This can be further simplified with a pattern rule:

    foo/%.out: %.in foo/.dir
            cp $< $@
    
    0 讨论(0)
  • 2021-01-24 16:35

    Instead of making the targets dependent on the directory, you can simply create the directory unconditionally in their build rules:

    foo/a.out: a.in
            mkdir -p foo
            cp a.in foo/a.out
    
    foo/b.out: b.in
            mkdir -p foo
            cp b.in foo/b.out
    

    This avoids problems with using a directory as a prerequisite.

    0 讨论(0)
  • 2021-01-24 16:38

    As an authority on Make recently pointed out:

    "The one thing you must NEVER do is use a directory as a simple prerequisite. The rules the filesystem uses to update the modified time on directories do not work well with make."

    But I think this will do what you want:

    foo :
        mkdir foo
    
    foo/a.out : a.in | foo
        cp a.in foo/a.out
    
    foo/b.out : b.in | foo
        cp b.in foo/b.out
    
    0 讨论(0)
提交回复
热议问题