Re: Dependency between Command calls

Bill Deegan <[email protected]> Sat, 12 Apr 2025 13:17:35 -0700
Newsgroups gmane.comp.programming.tools.scons.user
Message-ID <CAEyG4CG4KkhDQJAMENOQ9c7uwmyVyjnBCZeYBTgtN86ErpDT4g@mail.gmail.com>
--===============4608512623648563752==
Content-Type: multipart/alternative; boundary="000000000000283b1106329a83bd"

--000000000000283b1106329a83bd
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

This should be pretty close to what you want:

https://gist.github.com/bdbaddog/698e0c4fe9922eb24da6a66cd760f8a1

Give it a try and let us know what you think.
Also it may be easier to get your solution working if you join our discord
server.
https://discord.gg/bXVpWAy

-Bill
SCons Project Manager

On Sat, Apr 12, 2025 at 12:26=E2=80=AFPM Mats Wichmann <[email protected]> w=
rote:

> On 4/12/25 12:13, Oliver Koch via Scons-users wrote:
> > Dear SCons experts,
> >
> > Thank your for your last supporting comments!
> >
> > I have developed a small SConstruct file (attached) that implements to
> > tool calls via Command().
> > However, I have a few questions regarding generalization as given in th=
e
> > comments of the SConstruct file.
> >
> > The required input file (MyDeps.puml) is also attached.
> >
> > The output of the two subsequent runs of "scons" are given in Run1.out
> > and Run2.out.
> >
> > So, the build is executed as intended, but the toy example is not reall=
y
> > scalable to a real world tool chain.
> >
> > Could you answer my questions resp. give me some hints, how to make thi=
s
> > a "real" build chain?
> >
> > Thank you!
> >
> > Oliver
>
> A few comments, out of order.
>
> # This is not executed. Why?
> act =3D env.Action(PlantUML, cmdstr=3D"Building ${TARGET}")
>
> The Action method produces an action object, but you never do anything
> with that action object. You have to tell something that act is its
> action before anything happens with it. It's fine to pass it to Command,
> that works in the same way as passing it an action string (which will
> anyway be converted to an action object). So like:
>
> tgt =3D env.Command(target, source, action=3Dact)
>
> ----
>
> from SCons.Script import *
>
> you don't need to do that in an SConstruct/SConscript file, the
> execution context causes it to be done for you.  Where you need that
> import is in files that are run directly and not executed by the
> SConscript() function.
>
> ----
>
> The classic way to scale when there are many files is to write a
> builder.  I think we've previously sent you links on that, please check
> the earlier messages.  But you don't have to. You could also take this:
>
> # I am lloking for something like:
> # for svg in SVGs:
> #   env.Command(svg.basename + '.svgz', svg, SVGZ)
>
> and actually enable it, there's no real reason it wouldn't work
>
> from pathlib import Path
>
> for svg in SVGs:
>      tgtpath =3D str(Path(svg).name) + '.svgz'
>      env.Command(tgtpath, svg, action=3DSVGZ)
>
>
> That's a bit "hacky", you should properly let SCons compute the path and
> there are facilities for doing so. If you write a real Builder, part of
> the plumbing it the knowlede of how to transform source to target names
> for the type of files that builder handles.-
>
> There's also a function which can let you find files - read up on Glob
> in the manual. The nice thing about Glob() is it "finds" files that
> haven't been built yet, as it can compute targets based on the
> information in the graph. So you can potentially:
>
> for svg in env.Glob('*.svg'):   # these will be Nodes, unless you add a
> strings=3DTrue arg
>
> ----
>
> def PlantUML(target, source, env):
>    import os
>    for src in source:
>      Cmd =3D 'java.exe' + \
>            ' -jar C:\\Progra~1\\PlantUML\\plantuml.jar' + \
>            ' -tsvg ' + str(src)
>      print('Calling: ', Cmd)
>      os.system(Cmd)
>    # target should not be an input, but is given from Cmd output.
>    # How to make a list of targets from the just created files?
>    for tgt in target:
>      print('  Creating: ', str(tgt))
>
> If you do things this way, you bypass the work SCons does for you. If
> you build a command string like Cmd, and include substitutable elements
> (like $SOURCE), and pass that to Command (instead of going directly out
> via os.system() - and fwiw, using subprocess.run() is preferred if you
> *MUST*), then bingo: the return from Command is the list of targets it
> computed, and you have your answer to
>
>    # How to make a list of targets from the just created files?
>
>
>
> _______________________________________________
> Scons-users mailing list
> [email protected]
> https://pairlist4.pair.net/mailman/listinfo/scons-users
>

--000000000000283b1106329a83bd
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>This should be pretty close to what you want:</div><d=
iv><br></div><div><a href=3D"https://gist.github.com/bdbaddog/698e0c4fe9922=
eb24da6a66cd760f8a1">https://gist.github.com/bdbaddog/698e0c4fe9922eb24da6a=
66cd760f8a1</a></div><div><br></div><div>Give it a try and let us know what=
 you think.</div><div>Also it may be easier to get your solution working if=
 you join our discord server.</div><div><a href=3D"https://discord.gg/bXVpW=
Ay">https://discord.gg/bXVpWAy</a></div><div><br></div><div>-Bill</div><div=
>SCons Project Manager</div></div><br><div class=3D"gmail_quote gmail_quote=
_container"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, Apr 12, 2025 at 1=
2:26=E2=80=AFPM Mats Wichmann &lt;<a href=3D"mailto:[email protected]">mats@=
wichmann.us</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding=
-left:1ex">On 4/12/25 12:13, Oliver Koch via Scons-users wrote:<br>
&gt; Dear SCons experts,<br>
&gt; <br>
&gt; Thank your for your last supporting comments!<br>
&gt; <br>
&gt; I have developed a small SConstruct file (attached) that implements to=
<br>
&gt; tool calls via Command().<br>
&gt; However, I have a few questions regarding generalization as given in t=
he<br>
&gt; comments of the SConstruct file.<br>
&gt; <br>
&gt; The required input file (MyDeps.puml) is also attached.<br>
&gt; <br>
&gt; The output of the two subsequent runs of &quot;scons&quot; are given i=
n Run1.out<br>
&gt; and Run2.out.<br>
&gt; <br>
&gt; So, the build is executed as intended, but the toy example is not real=
ly<br>
&gt; scalable to a real world tool chain.<br>
&gt; <br>
&gt; Could you answer my questions resp. give me some hints, how to make th=
is<br>
&gt; a &quot;real&quot; build chain?<br>
&gt; <br>
&gt; Thank you!<br>
&gt; <br>
&gt; Oliver<br>
<br>
A few comments, out of order.<br>
<br>
# This is not executed. Why?<br>
act =3D env.Action(PlantUML, cmdstr=3D&quot;Building ${TARGET}&quot;)<br>
<br>
The Action method produces an action object, but you never do anything <br>
with that action object. You have to tell something that act is its <br>
action before anything happens with it. It&#39;s fine to pass it to Command=
, <br>
that works in the same way as passing it an action string (which will <br>
anyway be converted to an action object). So like:<br>
<br>
tgt =3D env.Command(target, source, action=3Dact)<br>
<br>
----<br>
<br>
from SCons.Script import *<br>
<br>
you don&#39;t need to do that in an SConstruct/SConscript file, the <br>
execution context causes it to be done for you.=C2=A0 Where you need that <=
br>
import is in files that are run directly and not executed by the <br>
SConscript() function.<br>
<br>
----<br>
<br>
The classic way to scale when there are many files is to write a <br>
builder.=C2=A0 I think we&#39;ve previously sent you links on that, please =
check <br>
the earlier messages.=C2=A0 But you don&#39;t have to. You could also take =
this:<br>
<br>
# I am lloking for something like:<br>
# for svg in SVGs:<br>
#=C2=A0 =C2=A0env.Command(svg.basename + &#39;.svgz&#39;, svg, SVGZ)<br>
<br>
and actually enable it, there&#39;s no real reason it wouldn&#39;t work<br>
<br>
from pathlib import Path<br>
<br>
for svg in SVGs:<br>
=C2=A0 =C2=A0 =C2=A0tgtpath =3D str(Path(svg).name) + &#39;.svgz&#39;<br>
=C2=A0 =C2=A0 =C2=A0env.Command(tgtpath, svg, action=3DSVGZ)<br>
<br>
<br>
That&#39;s a bit &quot;hacky&quot;, you should properly let SCons compute t=
he path and <br>
there are facilities for doing so. If you write a real Builder, part of <br=
>
the plumbing it the knowlede of how to transform source to target names <br=
>
for the type of files that builder handles.-<br>
<br>
There&#39;s also a function which can let you find files - read up on Glob =
<br>
in the manual. The nice thing about Glob() is it &quot;finds&quot; files th=
at <br>
haven&#39;t been built yet, as it can compute targets based on the <br>
information in the graph. So you can potentially:<br>
<br>
for svg in env.Glob(&#39;*.svg&#39;):=C2=A0 =C2=A0# these will be Nodes, un=
less you add a <br>
strings=3DTrue arg<br>
<br>
----<br>
<br>
def PlantUML(target, source, env):<br>
=C2=A0 =C2=A0import os<br>
=C2=A0 =C2=A0for src in source:<br>
=C2=A0 =C2=A0 =C2=A0Cmd =3D &#39;java.exe&#39; + \<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&#39; -jar C:\\Progra~1\\PlantUML\=
\plantuml.jar&#39; + \<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&#39; -tsvg &#39; + str(src)<br>
=C2=A0 =C2=A0 =C2=A0print(&#39;Calling: &#39;, Cmd)<br>
=C2=A0 =C2=A0 =C2=A0os.system(Cmd)<br>
=C2=A0 =C2=A0# target should not be an input, but is given from Cmd output.=
<br>
=C2=A0 =C2=A0# How to make a list of targets from the just created files?<b=
r>
=C2=A0 =C2=A0for tgt in target:<br>
=C2=A0 =C2=A0 =C2=A0print(&#39;=C2=A0 Creating: &#39;, str(tgt))<br>
<br>
If you do things this way, you bypass the work SCons does for you. If <br>
you build a command string like Cmd, and include substitutable elements <br=
>
(like $SOURCE), and pass that to Command (instead of going directly out <br=
>
via os.system() - and fwiw, using subprocess.run() is preferred if you <br>
*MUST*), then bingo: the return from Command is the list of targets it <br>
computed, and you have your answer to<br>
<br>
=C2=A0 =C2=A0# How to make a list of targets from the just created files?<b=
r>
<br>
<br>
<br>
_______________________________________________<br>
Scons-users mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Scons-users@scon=
s.org</a><br>
<a href=3D"https://pairlist4.pair.net/mailman/listinfo/scons-users" rel=3D"=
noreferrer" target=3D"_blank">https://pairlist4.pair.net/mailman/listinfo/s=
cons-users</a><br>
</blockquote></div>

--000000000000283b1106329a83bd--

--===============4608512623648563752==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Scons-users mailing list
[email protected]
https://pairlist4.pair.net/mailman/listinfo/scons-users

--===============4608512623648563752==--