12

I may fall into a X-Y problem with this question and I encourage you guys to correct me if I am wrong.

I would like to configure a toolchain environment that can work on different platforms and compiler versions. I initially wrote a long Perl script that generates a configuration Makefile that contain only variables. I wanted to be simple so I did not write anything complex using automake or autoconf. Moreover I wanted the reconfiguration process to be very fast. In my case my own written ./configure does everything in less than a second. I am very happy with that.

However I feel I can use a better approach using environment variables. Instead of writing a Makefile with the specific variables I can set the current shell environment directly. For example:

export cc=gcc

Unfortunately, some variables are already declared in the $PATH. The solution is to add the new $PATH in the front of the other:

export PATH=/new/toolchain/path:$PATH

echo $PATH
/new/toolchain/path:/old/toolchain/path:/usr/bin:/bin...

I feel this is ugly I would like to remove the old path before adding the new one.

To conclude:

  1. Is it better to use the environment instead of custom makefiles to set a build configuration?
  2. How to properly adjust existing environment variables?
Omid N
  • 947
  • 2
  • 11
  • 24
nowox
  • 25,978
  • 39
  • 143
  • 293
  • 1
    Have you tried to use high-level build systems such as SCons, CMake, autoconf? – myaut May 18 '15 at 07:53
  • Well, CMake uses make and I want to keep the things as simple as it can. So make would be better in my case. Autoconf seems quite complex and I really should find a good tutorial. I am still lost with it. I really should take a look at SCons. I did not know it. – nowox May 18 '15 at 08:03
  • Just a remark: The export command you posted does _not_ lead to the path you posted (missing `$` sign). – Michael Jaros May 18 '15 at 09:44
  • 1
    @MichaelJaros Right, I fixed it. – nowox May 18 '15 at 09:56
  • `Rake` or `tup` are pretty good too. Make sucks for anything but the most basic stuff. – Petr Skocik May 18 '15 at 10:24

5 Answers5

5

When I have several variables to set, I write a wrapper script which I then use as a prefix to the command that I want to modify. That lets me use the prefix either

  • applying to a single command, such as make, or
  • initializing a shell, so that subsequent commands use the altered settings.

I use wrappers for

  • setting compiler options (such as clang, to set the CC variable, making configure scripts "see" it as the chosen compiler),
  • setting locale variables, to test with POSIX C versus en_US versus en_US.UTF-8, etc.
  • testing with reduced environments, such as in cron.

Each of the wrappers does what is needed to identify the proper PATH, LD_LIBRARY_PATH, and similar variables.

For example, I wrote this ad hoc script about ten years ago to test with a local build of python:

#!/bin/bash
ver=2.4.2
export TOP=/usr/local/python-$ver
export PATH=$TOP/bin:$PATH
export LD_LIBRARY_PATH=`newpath -n LD_LIBRARY_PATH -bd $TOP/lib $TOP/lib/gcc/i686-pc-linux-gnu/$ver`
if test -d $TOP
then
    exec $*
else
    echo no $TOP
    exit 1
fi

and used it as with-python-2.4.2 myscript.

Some wrappers simply call another script. For example, I use this wrapper around the configure script to setup variables for cross-compiling:

#!/bin/sh
# $Id: cfg-mingw,v 1.7 2014/09/20 20:49:31 tom Exp $
# configure to cross-compile using mingw32

BUILD_CC=${CC:-gcc}
unset CC
unset CXX

TARGET=`choose-mingw32`

if test -n "$TARGET"
then
    PREFIX=
    test -d /usr/$TARGET && PREFIX="--prefix=/usr/$TARGET"
    cfg-normal \
            --with-build-cc=$BUILD_CC \
            --host=$TARGET \
            --target=$TARGET \
            $PREFIX "$@"
else
    echo "? cannot find MinGW compiler in path"
    exit 1
fi

where choose-mingw32 and cfg-normal are scripts that (a) find the available target name for the cross-compiler and (b) provide additional options to the configure script.

Others may suggest shell aliases or functions. I do not use those for this purpose because my command-line shell is usually tcsh, while I run these commands from (a) other shell scripts, (b) directory editor, or (c) text-editor. Those use the POSIX shell (except of course, for scripts requiring specific features), making aliases or functions of little use.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
2

You can create an individualized environment for a particular command invocation:

VAR1=val1 VAR2=val2 VAR3=val3 make

I find this cleaner than doing:

   export VAR1=val1
   export VAR2=val2
   export VAR3=val3
   make

unless you're in a wrapper script and maybe even then as with VAR1=val1 VAR2=val2 VAR3=val3 make the VAR variables will be whatever they were before the make invocation (including but not limited to unexported and nonexistent).

Long lines is a non-issue, you can always split it across several lines:

VAR1=val1\
VAR2=val2\
VAR3=val3\
make

You can set up environment variables like this for any Unix command. The shell will all set it up. Some applications (such as make or rake) will modify their environment based on arguments that look like variable definitions (see prodev_paris's answer), but that depends on the application.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

Is it better to use the environment instead of custom makefiles to set a build configuration?

The best practice for build systems is to not depend on any environment variables at all. So that nothing more is necessary to build your project than:

git clone ... my_project
make -C my_project

Having to set environment variables is error prone and may lead to inconsistent builds.

How to properly adjust existing environment variables?

You may not need to adjust those at all. By using complete paths to tools like compilers you disentangle your build system from the environment.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • I agree with you, however this might lead to a considerable overhead where the project has to be reconfigured several time a day. It might take time to always recheck for dependencies. I am always impressed how much time I can waste with `./configure` when the basecode is only few C files. – nowox May 18 '15 at 12:31
  • 1
    Well, you can run `./configure` a few times, save results and make a shell script that invokes `make` with a specific config, e.g. `make-arm-v7.sh`, `make-arv-v8.sh`, etc.. Build out-of-tree, so that you do not have to clean your build directory when switching between different target architectures. – Maxim Egorushkin May 18 '15 at 12:36
1

As we all know, it is preferrable to integrate standard tools for a task like building your products instead of creating your own approach. The effort usually pays off in the long term.

That being said, a simple approach would be to define different environment files (e.g. build-phone.env) setting working directory, PATH, CC etc. for your different products and source your environment files interactively on demand:

. /path/to/build-phone.env
[your build commands]
. /path/to/build-watch.env
[your build commands]
Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
0

I think you may benefit from using direct variable definition when you call your makefile, like in the following:

make FOO=bar target

Where FOO is the variable you want to set with value bar.

Note that in this case it take precedence over environment definition! So you can easily override your PATH variable...

Please have a look at this detail topic for more info: https://stackoverflow.com/a/2826178/4716013

Community
  • 1
  • 1
prodev_paris
  • 495
  • 1
  • 4
  • 17
  • Good solution but in my case I have about 10 variables to set (product type, architecture, compiler, version, etc.). I would be a bit counter-productive to always write make ARCH=... CC=... ... all – nowox May 18 '15 at 08:08
  • As I understand your situation you may be able to write a 'simple' script that do it for you :) – prodev_paris May 18 '15 at 08:11
  • it's what I did. I have a custom Perl script that generate a `config.mak` used by the main Makefile. – nowox May 18 '15 at 12:32