4 SPDX-FileCopyrightText: Copyright The SCons Foundation (https://scons.org)
5 SPDX-License-Identifier: MIT
6 SPDX-FileType: DOCUMENTATION
8 This file is processed by the bin/SConsDoc.py module.
13 <!ENTITY % version SYSTEM "../version.xml">
16 <!ENTITY % scons SYSTEM "../scons.mod">
19 <!ENTITY % builders-mod SYSTEM "../generated/builders.mod">
21 <!ENTITY % functions-mod SYSTEM "../generated/functions.mod">
23 <!ENTITY % tools-mod SYSTEM "../generated/tools.mod">
25 <!ENTITY % variables-mod SYSTEM "../generated/variables.mod">
30 <chapter id="chap-build-install"
31 xmlns="http://www.scons.org/dbxsd/v1.0"
32 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33 xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">
35 <title>Building and Installing &SCons;</title>
39 This chapter will take you through the basic steps
40 of installing &SCons; so you can use it for your projects.
41 Before that, however, this chapter will also describe the basic steps
42 involved in installing &Python; on your system,
43 in case that is necessary.
44 Fortunately, both &SCons; and &Python;
45 are easy to install on almost any system,
46 and &Python; already comes installed on many systems.
54 Lastly, this chapter also contains a section that
55 provides a brief overview of the &Python; programming language,
56 which is the language used to implement &SCons;,
57 and which forms the basis of the &SCons; configuration files.
58 Becoming familiar with some &Python; concepts will make it easier
59 to understand many of the examples in this User's Guide.
60 Nevertheless, it <emphasis>is</emphasis> possible
61 to configure simple &SCons; builds without knowing &Python;,
62 so you can skip this section if you
63 want to dive in and pick up things
64 by example- -or, of course, if you are
65 already familiar with &Python;.
71 <section id="sect-install-python">
72 <title>Installing Python</title>
75 Because &SCons; is written in the &Python; programming language,
76 you need to have a &Python; interpreter available on your system
78 Before you try to install &Python;,
79 check to see if &Python; is already
80 available on your system by typing
81 <userinput>python -V</userinput>
84 <userinput>python --version</userinput>
85 at your system's command-line prompt.
86 For Linux/Unix/MacOS/BSD type systems this looks like:
90 $ <userinput>python -V</userinput>
95 If you get a version like 2.7.x, you may need to try using the
96 name <command>python3</command> - current &SCons; no longer
97 works with &Python; 2.
101 Note to Windows users: there are a number of different ways &Python;
102 can be installed or invoked on Windows, it is beyond the scope
103 of this guide to unravel all of them. Some have an additional
104 program called the <firstterm>Python launcher</firstterm> (described,
105 somewhat technically, in
106 <ulink url="https://www.python.org/dev/peps/pep-0397/">PEP 397</ulink>):
107 try using the command name <command>py</command> instead of
108 <command>python</command>, if that is not available drop
109 back to trying <command>python</command>
113 C:\><userinput>py -V</userinput>
118 If &Python; is not installed on your system,
119 or is not findable in the current search path,
120 you will see an error message
121 stating something like <computeroutput>"command not found"</computeroutput>
123 or <computeroutput>"'python' is not recognized as an internal
124 or external command, operable program or batch file"</computeroutput>
125 (on Windows <command>cmd</command>).
126 In that case, you need to either install &Python;
127 or fix the search path
128 before you can install &SCons;.
132 The link for downloading &Python; installers (Windows and Mac)
133 from the project's own website is:
134 <ulink url="https://www.python.org/download">https://www.python.org/download</ulink>.
135 There are useful system-specific entries on setup and
136 usage to be found at:
137 <ulink url="https://docs.python.org/3/using">https://docs.python.org/3/using</ulink>
141 For Linux systems, &Python; is
142 almost certainly available as a supported package, probably
143 installed by default; this is often preferred over installing
144 by other means as the system package will be built with
145 carefully chosen optimizations, and will be kept up to date
146 with bug fixes and security patches. In fact, the &Python;
147 project itself does not build installers for Linux for this reason.
148 Many such systems have separate packages for
149 &Python; 2 and &Python; 3 - make sure the &Python; 3 package is
150 installed, as the latest &SCons; requires it.
151 Building from source may still be a
152 useful option if you need a specific version that is not offered by
153 the distribution you are using.
157 Recent versions of the Mac no longer come with &Python;
158 pre-installed; older versions came with a rather out-of-date
159 version (based on &Python; 2.7) which is insufficient to run
161 The python.org installer can be used on the Mac, but there are
162 also other sources such as MacPorts and Homebrew.
163 The Anaconda installation also comes with a bundled &Python;.
167 Windows has even more choices. The Python.org installer is
168 a traditional <filename>.exe</filename> style;
169 the same software is also released as a Windows application through
170 the Microsoft Store. Several alternative builds also exist
171 such as Chocolatey and ActiveState, and, again,
172 a version of Python comes with Anaconda.
176 &SCons; will work with &Python; 3.6 or later.
177 If you need to install &Python; and have a choice,
178 we recommend using the most recent &Python; version available.
179 Newer &Python; versions have significant improvements
180 that help speed up the performance of &SCons;.
185 <section id="sect-install-scons">
186 <title>Installing &SCons;</title>
189 The recommended way to install &SCons; is from the &Python; Package
190 Index (<ulink url="https://pypi.org/project/SCons/">PyPI</ulink>):
194 % <userinput>python -m pip install scons</userinput>
198 If you prefer not to install to the &Python; system location,
199 or do not have privileges to do so, you can add a flag to install
200 to a location specific to your own account and &Python; version:
204 % <userinput>python -m pip install --user scons</userinput>
208 For those users using Anaconda or Miniconda, use the
209 <command>conda</command> installer instead, so the &scons;
210 install location will match the version of &Python; that
211 system will be using. For example:
215 % <userinput>conda install -c conda-forge scons</userinput>
219 If you need a specific
220 version of &SCons; that is different from the current version,
221 <systemitem>pip</systemitem> has a version option
222 (e.g. <userinput>python -m pip install scons==3.1.2</userinput>),
223 or you can follow the instructions in the following sections.
227 &SCons; does comes pre-packaged for installation on many Linux systems.
228 Check your package installation system
229 to see if there is an up-to-date &SCons; package available.
230 Many people prefer to install distribution-native packages if available,
231 as they provide a central point for management and updating;
232 however not all distributions update in a timely fashion.
233 During the still-ongoing &Python; 2 to 3 transition,
234 some distributions may still have two &SCons; packages available,
235 one which uses &Python; 2 and one which uses &Python; 3. Since
236 the latest &scons; only runs on &Python; 3, to get the current version
237 you should choose the &Python; 3 package.
242 <section id="sect-scons-no-install">
243 <title>Using &SCons; Without Installing</title>
246 You don't actually need to "install" &SCons; to use it.
247 Nor do you need to "build" it, unless you are interested in
248 producing the &SCons; documentation, which does use several
249 tools to produce HTML, PDF and other output formats from
250 files in the source tree.
251 All you need to do is
252 call the <filename>scons.py</filename> driver script in a
253 location that contains an &SCons; tree, and it will figure out
254 the rest. You can test that like this:
258 $ <userinput>python <replaceable>/path/to/unpacked</replaceable>/scripts/scons.py --version</userinput>
262 To make use of an uninstalled &SCons;,
263 the first step is to download either the
264 <filename>scons-&buildversion;.tar.gz</filename>
265 or <filename>scons-&buildversion;.zip</filename>,
266 which are available from the SCons download page at
267 <ulink url="https://scons.org/pages/download.html">https://scons.org/pages/download.html</ulink>.
268 There is also a <literal>scons-local</literal> bundle you can make
269 use of. It is arranged a little bit differently, with the idea
270 that you can include it with your own project if you want people
271 to be able to do builds without having to download or install &SCons;.
272 Finally, you can also use a checkout of the git tree from GitHub
273 at a location to point to.
277 Unpack the archive you downloaded,
278 using a utility like <application>tar</application>
280 or <application>WinZip</application> on Windows.
281 This will create a directory called
282 <filename>scons-&buildversion;</filename>,
283 usually in your local directory. The driver script
284 will be in a subdirectory named <filename>scripts</filename>,
285 unless you are using <literal>scons-local</literal>,
286 in which case it will be in the top directory.
287 Now you only need to call <filename>scons.py</filename> by
288 giving a full or relative path to it in order to use that
293 Note that instructions for older versions may have suggested
294 running <userinput>python setup.py install</userinput> to
295 "build and install" &SCons;. This is no longer recommended
296 (in fact, it is not recommended by the wider &Python; packaging
297 community for <emphasis>any</emphasis> end-user installations
298 of &Python; software). There is a <filename>setup.py</filename> file,
299 but it is only tested and used for the automated procedure which
300 prepares an &SCons; bundle for making a release on PyPI,
301 and even that is not guaranteed to work in the future.
306 <section id="sect-install-scons-multi">
307 <title>Running Multiple Versions of &SCons; Side-by-Side</title>
310 In some cases you may need several versions of &SCons;
311 present on a system at the same time - perhaps you have
312 an older project to build that has not yet been "ported"
313 to a newer &SCons; version, or maybe you want to test a
314 new &SCons; release side-by-side with a previous one
315 before switching over.
316 The use of an "uninstalled" package as described in the
317 previous section can be of use for this purpose.
321 Another approach to multiple versions is to create
322 &Python; virtualenvs, and install different &SCons; versions in each.
323 A Python <firstterm>virtual environment</firstterm>
324 is a directory with an isolated set of Python packages,
325 where packages you install/upgrade/remove inside the
326 environment do not affect anything outside it,
327 and those you install/upgrade/remove outside of it
328 do not affect anything inside it.
329 In other words, anything you do with <command>pip</command>
330 in the environment stays in that environment.
331 The &Python; standard library provides a module called
332 <systemitem>venv</systemitem> for creating these
333 (<ulink url="https://docs.python.org/e/library/venv.html"/>),
334 although there are also other tools which provide more precise
335 control of the setup.
339 Using a virtualenv can be useful even for a single version of
340 &SCons;, to gain the advantages of having an isolated environment.
341 It also gets around the problem of not having administrative
342 privileges on a particular system to install a distribution
343 package or use <command>pip</command> to install to a
344 system location, as the virtualenv is completely under your control.
348 The following outline shows how this could be set up
349 on a Linux/POSIX system (the syntax will be a bit different
354 $ <emphasis>create virtualenv named scons3</emphasis>
355 $ <emphasis>create virtualenv named scons4</emphasis>
356 $ <userinput>source scons3/bin/activate</userinput>
357 $ <userinput>pip install scons==3.1.2</userinput>
358 $ <userinput>deactivate</userinput>
359 $ <userinput>source scons4/bin/activate</userinput>
360 $ <userinput>pip install scons</userinput>
361 $ <userinput>deactivate</userinput>
362 $ <emphasis>activate a virtualenv and run 'scons' to use that version</emphasis>
370 <title>Python Basics</title>
374 This section will provide a brief overview of
375 the Python programming language.
376 Skip this section if you are already familiar with Python
377 (or you're really intent on diving into &SCons;
378 and just picking up things as you go).
384 Python has a lot of good
385 documentation freely available on-line
386 to help you get started.
387 The standard tutorial is available at XXX.
394 Python is very easy to pick up.
400 Python variables must be assigned to before they can be referenced.
406 Assignment is like most programming languages:
415 Function calls look like most language function calls:
423 Define functions like so:
425 def func(arg1, arg2):
428 The number of parameters
434 Strings can be enclosed in single quotes or double quotes,
435 backslashes are used to escape characters,
436 triple-quote syntax lets you include quotes and newlines,
437 raw strings begin with 'r'.
443 Lists are enclosed in square brackets,
444 list items are separated by commas.
445 List references use square brackets and integer index values,
446 slice notation lets you select, delete or replace a range.
452 Dictionaries (hashes) are enclosed in curly brackets,
453 : separates keys from values,
455 Dictionary values are referenced using square brackets.
461 Access class attributes (including methods) using a '.'.
467 if: statements look like
469 elif: statements look like
471 else: statements look like
477 for: statements look like
479 while: statements look like
481 break statements look like
483 continue statements look like