Skip to content

Implicit command dependency may fail if command is generated by the build #2877

Open

Description

@bdbaddog

This issue was originally created at: 2012-10-10 07:42:56.
This issue was reported by: marcnarc.

marcnarc said at 2012-10-10 07:42:56

SCons doesn't always pick up a dependency on $CC. Here's a simple SConstruct:

e = DefaultEnvironment()
mycc = e.Command(
    target="mycc",
    source="/usr/bin/gcc",
    action="cp $SOURCE $TARGET",
)
e.Replace(CC=mycc[0].abspath)
e.Program(target="testapp", source="test.c")

The above works fine:

$ scons --tree=prune
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
cp /usr/bin/gcc mycc
/usr/home/marcnarc/Code/foo/sconsdep/mycc -o test.o -c test.c
/usr/home/marcnarc/Code/foo/sconsdep/mycc -o testapp test.o
+-.
  +-SConstruct
  +-mycc
  | +-/usr/bin/gcc
  | +-/bin/cp
  +-test.c
  +-test.o
  | +-test.c
  | +-[mycc]
  +-testapp
    +-[test.o]
    +-[mycc]
scons: done building targets.

However, it breaks with seemingly innocent changes:

  • Put mycc inside a tools/ directory (target="tools/mycc").
  • Change the program's name from "testapp". Some names work but others don't. Here are some names that don't work: aragon frodo gimli legolas boromir. And here are some names that do work: waragon, phrodo, qgimli plegolas sauron.
  • Run scons with the -q option.

What breaks is the dependency on mycc. In each failing case the tree shows no dependency from test.o on mycc:

...
+-.
  +-SConstruct
  +-test.c
  +-test.o
  | +-test.c
  +-testapp
  | +-[test.o]
  +-tools
    +-tools/mycc
      +-/usr/bin/gcc
      +-/bin/cp

(I also see this problem with SCons 1.2.0.)

marcnarc said at 2012-10-10 12:28:32

Created an attachment (id=905) taskmaster trace from a successful run

marcnarc said at 2012-10-10 12:28:55

Created an attachment (id=906) taskmaster trace from a failed run

marcnarc said at 2012-10-10 12:37:50

Created an attachment (id=907) taskmaster trace when the program's name is "gimli"

marcnarc said at 2012-10-10 12:38:48

Attachment taskmaster.fail.trace is from a run where the mycc target is "tools/mycc".

Attachment taskmaster.fail-gimli.trace is from a run where the mycc target is just "mycc" but the program's target is "gimli".

marcnarc said at 2012-10-22 07:24:28

Johan Holmberg noted on scons-users that all the working target names are lexically > than "mycc".

I've verified that the lexicographic order of the target names is significant: Renaming the mycc target to "Amycc" fixes the broken program names ("aragon" et al.).

marcnarc said at 2012-10-22 07:28:17

A bit more info:

Specifying the target name (e.g. "testapp") on the scons command line always produces a failure, regardless of the name of the mycc target.

Votes for this issue: 1.

marcnarc attached taskmaster.good.trace at 2012-10-10 12:28:32.

taskmaster trace from a successful run

marcnarc attached taskmaster.fail.trace at 2012-10-10 12:28:55.

taskmaster trace from a failed run

marcnarc attached taskmaster.fail-gimli.trace at 2012-10-10 12:37:50.

taskmaster trace when the program's name is "gimli"

Activity

  1. mwichmann commented on Apr 3, 2022

    @mwichmann
    Collaborator

    This is just weird, why should there be a lexicograhical relationship to the pathname of a tool? Anyway, I try to reproduce it, it doesn't work like this, now if doesn't "make" the tool at all.

  2. bdbaddog commented on Apr 4, 2022

    @bdbaddog
    ContributorAuthor

    It seems you can't just cp gcc to another name in another location anymore..

    $ python ~/devel/scons//git/as_scons/scripts/scons.py
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    cp /usr/bin/gcc mycc
    /home/bdbaddog/devel/scons/git/scons-bugswork/2877/mycc -o test.o -c test.c
    mycc: fatal error: cannot execute 'cc1': execvp: No such file or directory
    compilation terminated.
    scons: *** [test.o] Error 1
    scons: building terminated because of errors.
    
  3. bdbaddog commented on Apr 4, 2022

    @bdbaddog
    ContributorAuthor

    ugh. I can repro.
    You can see the files here: https://github.com/bdbaddog/scons-bugswork/tree/main/2877

    This is via the copy to tools dir referred to above.

  4. mwichmann commented on Apr 4, 2022

    @mwichmann
    Collaborator

    Yeah, I was seeing the gcc problem as well, probably for demo purposes should pick a different tool. Also if the action uses Copy it should figure out whether mkdir needed or not without having to wire it in? So maybe this isn't actually a problem? Using Copy it now looks like this - I see the tool in the deps where it's expected.

    $ scons --tree=prune
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    Copy("bin/mycc", "/usr/bin/gcc")
    /home/mats/github/scons/exp/issue2877/bin/mycc -o test.o -c test.c
    mycc: fatal error: cannot execute 'cc1': execvp: No such file or directory
    compilation terminated.
    scons: *** [test.o] Error 1
    +-.
      +-SConstruct
      +-bin
      | +-bin/mycc
      |   +-/usr/bin/gcc
      +-test.c
      +-test.o
      | +-test.c
      | +-[bin/mycc]
      +-testapp
        +-[test.o]
        +-[bin/mycc]
    scons: building terminated because of errors.
  5. bdbaddog commented on Apr 4, 2022

    @bdbaddog
    ContributorAuthor

    @mwichmann try it with my sample code linked above. I still see the issue.
    I think it's an issue with the implicit command dependencies...

  6. mwichmann commented on Apr 4, 2022

    @mwichmann
    Collaborator

    You're right, the lex-order-matters thing is still there, I didn't even notice in my test (pasted above) that I dodged it by using bin/toolname instead of tools/toolname. Just like the original report, I can toggle success/fail by swapping the subdirectory names between bin and tools... weird.

  7. mwichmann commented on Apr 4, 2022

    @mwichmann
    Collaborator

    SCons must be sorting something in order to get reproducibility, and thus somehow introducing this issue?

  8. mwichmann commented on Nov 18, 2026

    @mwichmann
    Collaborator

    This is convoluted enough that I asked an AI for help analyzing it. There's a lot going on here. Abbreviated, and possibly leaving out important details, the "compiler" in a builder is an implicit dependency, part of the DAG once discovered. But since in this case it's generated, and the check is filesystem-based (WhereIs) - if it hasn't been generated yet, it's not discovered, and no node is created (basically "SCons discovers it too late"). As a result the sorted in-memory tree is walked instead of having a dep in place, and so the lex order matters. Underneath that is a layer where filesystem probes are sorted for consistency, and that sorted order is used in places where maybe it doesn't need to be sorted. Redesigning to get rid of that leakage (if you want to call it that) seems like a big/risky lift.

    For specific cases where people are using generated "compilers", using the much-maligned Depends might be the best answer?

  9. added this to the Research milestone on Nov 19, 2026
  10. mwichmann commented on Nov 20, 2026

    @mwichmann
    Collaborator

    My current suggested band-aid is to add some doc helpers about this. Whether we close the issue after those additions or not, I don't have an opinion on yet. Open to comments

  11. self-assigned this
    on Nov 20, 2026
  12. mwichmann commented on Nov 20, 2026

    @mwichmann
    Collaborator

    I'm changing the issue summary in the hopes it will be easier to find if anyone runs into this.

    Basically, you can make the original example work with tweak, but you have to do two things, neither of which is necessarily intuitive.

    1. If the tool is generated by the build, and is not yet in the filesystem, SCons won't find it, and so won't make a dependency on the tool. That's just how implicit command dependencies are implemented. Add the dependency yourself.
    2. In the case of intermediate builds, like C code in the example, you have to use the Object builder, and attach the dependency to that, even if you had no other reason to use Object. The Program builder will first make an object file, then make the executable. If you attach the depend to the executable node/name, SCons will make sure the tool is built before linking - but not before compiling the object.

    So here's the reworked example:

    e = Environment()
    mycc = e.Command(
        target="tools/mycc",
        source="fakecc.in",
        action="cp $SOURCE $TARGET",
    )
    e.Replace(CC=mycc[0].abspath)
    o = e.Object(source="test.c")
    e.Depends(o, mycc)
    p = e.Program(target="testapp", source=o)

    and here's the tree from that:

    +-.
      +-SConstruct
      +-fakecc.in
      +-test.c
      +-test.o
      | +-test.c
      | +-tools/mycc
      | | +-fakecc.in
      | | +-/bin/cp
      | +-[tools/mycc]
      +-testapp
      | +-[test.o]
      | +-[tools/mycc]
      +-tools
        +-[tools/mycc]
    

    I'm going to propose a PR that documents some of this - the only place I can think where it might be seen is in $IMPLICIT_COMMAND_DEPENDENCIES

  13. changed the title [-]Dependencies on $CC are flaky[/-] [+]Implicit command dependency may fail if command is generated by the build[/+] on Nov 20, 2026
  14. removed this from the Research milestone on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions