SCons / doc / user / ENV.xml

<!--

  __COPYRIGHT__

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-->

  <para>

    When &SCons; builds a target file,
    it does not execute the commands with
    the same external environment
    that you used to execute &SCons;.
    Instead, it uses the dictionary
    stored in the &cv-link-ENV; construction variable
    as the external environment
    for executing commands.

  </para>

  <para>

    The most important ramification of this behavior
    is that the &PATH; environment variable,
    which controls where the operating system
    will look for commands and utilities,
    is not the same as in the external environment
    from which you called &SCons;.
    This means that &SCons; will not, by default,
    necessarily find all of the tools
    that you can execute from the command line.

  </para>

  <para>

    The default value of the &PATH; environment variable
    on a POSIX system
    is <literal>/usr/local/bin:/bin:/usr/bin</literal>.
    The default value of the &PATH; environment variable
    on a Windows system comes from the Windows registry
    value for the command interpreter.
    If you want to execute any commands--compilers, linkers, etc.--that
    are not in these default locations,
    you need to set the &PATH; value
    in the &cv-ENV; dictionary
    in your construction environment.

  </para>

  <para>

    The simplest way to do this is to initialize explicitly
    the value when you create the construction environment;
    this is one way to do that:

  </para>

  <programlisting>
    path = ['/usr/local/bin', '/bin', '/usr/bin']
    env = Environment(ENV = {'PATH' : path})
  </programlisting>

  <para>

  Assign a dictionary to the &cv-ENV;
  construction variable in this way
  completely resets the external environment
  so that the only variable that will be
  set when external commands are executed
  will be the &PATH; value.
  If you want to use the rest of
  the values in &cv-ENV; and only
  set the value of &PATH;,
  the most straightforward way is probably:

  </para>

  <programlisting>
    env['ENV']['PATH'] = ['/usr/local/bin', '/bin', '/usr/bin']
  </programlisting>

  <para>

  Note that &SCons; does allow you to define
  the directories in the &PATH; in a string,
  separated by the pathname-separator character
  for your system (':' on POSIX systems, ';' on Windows):

  </para>

  <programlisting>
    env['ENV']['PATH'] = '/usr/local/bin:/bin:/usr/bin'
  </programlisting>

  <para>

  But doing so makes your &SConscript; file less portable,
  (although in this case that may not be a huge concern
  since the directories you list are likley system-specific, anyway).

  </para>

  <!--

  <scons_example name="ex1">
    <file name="SConstruct" printme="1">
    env = Environment()
    env.Command('foo', [], '__ROOT__/usr/bin/printenv.py')
    </file>
    <file name="__ROOT__/usr/bin/printenv.py" chmod="0755">
    #!/usr/bin/env python
    import os
    import sys
    if len(sys.argv) > 1:
        keys = sys.argv[1:]
    else:
        keys = os.environ.keys()
        keys.sort()
    for key in keys:
        print "    " + key + "=" + os.environ[key]
    </file>
  </scons_example>

  <para>

  </para>

  <scons_output example="ex1">
    <scons_output_command>scons -Q</scons_output_command>
  </scons_output>

  -->

  <section>
  <title>Propagating &PATH; From the External Environment</title>

    <para>

    You may want to propagate the external &PATH;
    to the execution environment for commands.
    You do this by initializing the &PATH;
    variable with the &PATH; value from
    the <literal>os.environ</literal>
    dictionary,
    which is Python's way of letting you
    get at the external environment:

    </para>

    <programlisting>
      import os
      env = Environment(ENV = {'PATH' : os.environ['PATH']})
    </programlisting>

    <para>

    Alternatively, you may find it easier
    to just propagate the entire external
    environment to the execution environment
    for commands.
    This is simpler to code than explicity
    selecting the &PATH; value:

    </para>

    <programlisting>
      import os
      env = Environment(ENV = os.environ)
    </programlisting>

    <para>

    Either of these will guarantee that
    &SCons; will be able to execute
    any command that you can execute from the command line.
    The drawback is that the build can behave
    differently if it's run by people with
    different &PATH; values in their environment--for example,
    if both the <literal>/bin</literal> and
    <literal>/usr/local/bin</literal> directories
    have different &cc; commands,
    then which one will be used to compile programs
    will depend on which directory is listed
    first in the user's &PATH; variable.

    </para>

  </section>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.