How can default env variable in gnu makefile target - makefile

I want to do something like this
test.install:
export BUILD_NUMBER=${BUILD_NUMBER:-$${ANOTHER_VAR}}
but its not working. it always come as blank
EDIT:
This works fine. provided i export BUILD_NUMBER in shell before invoking make command
test.install:
export BUILD_NUMBER=${BUILD_NUMBER}
THese are not working . They both give blank BUILD_NUMBER
test.install:
export BUILD_NUMBER=${BUILD_NUMBER:-$${ANOTHER_VAR}}
and
test.install:
export BUILD_NUMBER=${BUILD_NUMBER:-55}}

If you want to expand variables in bash rather than in the makefile, you'll need to escape the expansion otherwise make is going to look for a variable literally called BUILD_NUMBER:-${ANOTHER_VAR} or BUILD_NUMBER:-55
test.install:
export BUILD_NUMBER=$${BUILD_NUMBER:-$${ANOTHER_VAR}}
test.install:
export BUILD_NUMBER=$${BUILD_NUMBER:-55}}

Related

Export variable declared in script.sh and Import the value in mupltiple Makefiles

I am trying to create something like a global variable that I will use in order to make my project easy to deploy for other developers.
I would like to have an .sh file where there is a variable defining the location of the project.
Later on I want to export this variable and make it accessable in every makefile that I am creating so that I can use this design to keep everything constant and in one place.
This is an example of what I am trying to build:
Creating and exporting the variables in script.sh:
#!/bin/bash
DIRECTORY='some path value here'
Importing the values in multiple Makefiles:
# start script and fetch the value
VAR := $(shell ./script.sh | sed -n '/^result: /s/^.*: //p')
all:
#echo VAR=$(VAR)
I would like to see how other people are dealing with the same problem.
Being a better developer is my goal here. :)
Feedback always welcomed.
Environment variables exported in the shell are visible from make, so in a shell script like this:
#!/bin/sh
VAR=value
export VAR
make $*
The Makefile will start with VAR defined to value. That's one way to get variables from a shell script into make.
If you don't want the shell script to run make, you can have a user source it:
$ source script.sh
$ make
The variables set in the script will be visible to make this way too.
Or course there doesn't seem to be any reason you need a shell script here. Stick your configuration into a fragment of a Makefile (which would look almost exactly like your shell script, but not use quotes for multiple word values) and then include Makefile.inc in your main makefile.
Also note that syntax like this:
#!/bin/sh or another commment
VAR=value
export VAR
It equally valid included in a Makefile or sourced into a shell script. So sometimes it's possible to use the same include file in both places!

Print Makefile variable as text

I didn't find anything about that, and i am trying to wrote this rule in my Makefile.
setenv:
#echo "export DYLD_LIBRARY_PATH=."
#echo "export DYLD_INSERT_LIBRARIES=$(NAME).so"
#echo "export DYLD_FORCE_FLAT_NAMESPACE=1"
#echo "# Run eval $$(make setenv)"
So by running eval $(make setenv) in my terminal, the environment variable will be set.
But it's starting an infinite loop.
I've also try with:
\$(make setenv)
but nothing work ... What is the correct syntax for this ?
EDIT:
\$$(make setenv) Did the trick !
If you're setting environment variables for other recipes, note that:
Using $(shell export ...) won't work: $(shell ...) always spawns a new shell, so anything that is exported into it won't be available outside of that particular invocation;
Using export shell commands in a recipe will only work if .ONESHELL is used (not recommended), because each recipe line runs in a different shell.
The typical way to export environment variables to sub-makes and sub-shells is to use export as a Makefile directive, like this:
export DYLD_LIBRARY_PATH=.
export DYLD_INSERT_LIBRARIES=$(NAME).so
export DYLD_FORCE_FLAT_NAMESPACE=1
Outside any recipe.

Exporting builtin variables in bash script

That might be a trivial question, but what is the right way to use builtin variables in a shell script? For example, if I want to write a script that opens default text editor, which is specified in $EDITOR. Just using
export EDITOR
won't help. I found out that defining EDITOR variable helps to solve that problem:
#!/bin/bash
export EDITOR=vim
$EDITOR
The above will work, but is there a way to export the variable without defining it? Thanks in advance.
EDITOR is not a "builtin" variable, and you can export it (i.e. make it an environment variable) whenever you like. Nothing magical here. While it is true that bash interprets this variable in certain situations, it is quite common that applications access this variable, when they want to launch an editor, so in practice, EDITOR is usually exported. For example, I have in my .zshrc and .bashrc the line
export EDITOR=nano
To your question: You can export a varible with an empty falue, like this
export EDITOR=
but I don't see what you will gain from it.
If you just want to ensure, that your shell script and all descendent processes have EDITOR set, a common idiom is
: ${EDITOR:=vim}
export EDITOR
If the user of your script doesn't define this variable, it is set here - you just need two lines, because bash syntax does not permit to combine this into a single one.

Set and export multiple environment variables to the same value in bash

I would like to set the same value to the locale environment variables: LC_CTYPE, LC_ALL, LANG.
I want to be able to set it directly on the interactive bash shell, something like this:
$ export LC_CTYPE=$LANG=$LC_ALL=C
This answer Assign same value to multiple variables shows how to do it in a script, but not in the interactive shell.
export {LC_CTYPE,LANG,LC_ALL}=C
The reference provided by you isn't a shell reference, it's a PHP example.
In shell, one way would be to use a loop:
for i in FOO BAR BAZ; do
export $i=value
done

Using makefile variables in shell script

I've a makefile where all the environment variables are defined (top directory) and I want to use some of these variables in a shell script present at innermost level.
How should I access those variables in the script? Do I need to IPC for passing the variables or is there any other method to do the same?
If make exports the variables, you can use them right away (but note that changes to shell variables will not change the make variables). If you use GNU make, you can use the export directive.
If make does not export the variable, you can use the shell's one-shot assignment, like in
UNEXPORTED_VAR = foo
all:
UNEXPORTED_VAR='$(UNEXPORTED_VAR)' OTHERVAR='$(OTHERVAR)' script.sh
Like Jens said is correct, export make variables can be used. But his solution is not working for me.
So what you can also do is just export them in the makefile:
export UNEXPORTED_VAR

Resources