LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2010, 01:01 PM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
exporting makefile variables to $(shell) environment


I'm aware that one can export make variables to other makefiles; however, how does one export them to the environment of $(shell)? Take the example below:
Code:
export TEST
VARIABLE=$(shell echo $$TEST)
.PHONY: all
all:
	#$(VARIABLE)
In this example, I might call make TEST=test. The goal is for $TEST to be available to the environment of the shell escape. This is because I need its value in a script which is called. For example:
Code:
VARIABLE=$(shell i-need-TEST.sh)
My current solution is the following:
Code:
VARIABLE=$(shell export TEST="$(TEST)"; i-need-TEST.sh)
but this only works if I know all if the variables needed at that point (as opposed to being able to export variables in included makefiles.) Is there an easy solution? Thanks.
Kevin Barry
 
Old 05-12-2010, 11:05 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,793

Rep: Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087
Environment:
Quote:
When make runs a command script, variables defined in the makefile are placed into the environment of that command. This allows you to pass values to sub-make invocations (see Recursive Use of make). By default, only variables that came from the environment or the command line are passed to recursive invocations. You can use the export directive to pass other variables. See Communicating Variables to a Sub-make, for full details.
Do you need to use the shell function? It seems that the variables aren't exported to its environment.

Code:
~/tmp$ cat v.mk
export TEST
VARIABLE=echo $$TEST
.PHONY: all
all:
        $(VARIABLE)
~/tmp$ make -f v.mk TEST=test
echo $TEST
test
 
Old 05-13-2010, 08:20 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
You're right that the target environment is taken from the makefile; however, I need shell because the targets are taken from a script, and that script determines the targets conditionally, based on the environment. For example:
Code:
#!/bin/bash
#(targets.sh)
eval echo target$SUFFIX.txt
Code:
#makefile

export SUFFIX
TARGETS1=$(shell ./targets.sh)
TARGETS2=$(shell export SUFFIX="$(SUFFIX)"; ./targets.sh)

.PHONY: all TARGETS1 TARGETS2
all: $(TARGETS1) $(TARGETS2)

$(TARGETS1):
	echo TARGET1: $@

$(TARGETS2):
	echo TARGET2: $@
Code:
> make SUFFIX='{-default,-debug}'
Output:
Code:
echo TARGET1: target.txt
TARGET1: target.txt
echo TARGET2: target-default.txt
TARGET2: target-default.txt
echo TARGET2: target-debug.txt
TARGET2: target-debug.txt
Part of the point is that the script being called is also used outside of make; therefore, the script can't be integrated into the makefile itself.
Kevin Barry

Last edited by ta0kira; 05-13-2010 at 08:34 AM.
 
1 members found this post helpful.
Old 05-13-2010, 11:21 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,793

Rep: Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087
It's possible to hack something with .VARIABLES:

Code:
#MAKFILE

# get only the variables with plain names
MAKE_ENV := $(shell echo '$(.VARIABLES)' | awk -v RS=' ' '/^[a-zA-Z0-9]+$$/')
SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))')

export SUFFIX
TARGETS1:=$(shell ./targets.sh)
TARGETS2:=$(shell $(SHELL_EXPORT) ./targets.sh)

.PHONY: all $(TARGETS1) $(TARGETS2)
all: $(TARGETS1) $(TARGETS2)

$(TARGETS1):
	echo TARGET1: $@

$(TARGETS2):
	echo TARGET2: $@
 
Old 05-14-2010, 12:45 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Thanks, that looks like what I need.
Kevin Barry
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing shell script variables into makefile pampo Programming 1 04-19-2010 09:14 AM
[SOLVED] Makefile creation - how to add environment variables? macdudeosx Programming 5 04-16-2010 10:03 PM
Problem with exporting environment variables in Fedora sugantha Linux - Newbie 11 05-26-2008 05:13 AM
exporting environment variables from a file? concoran Linux - Newbie 4 04-19-2008 05:49 PM
Basic question about exporting shell variables balteo Linux - General 2 09-13-2005 11:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration