dubugger
dubugger

Reputation: 109

How to use source in my makefile while keep my previous makefile content

I am writing an makefile and I need to set up env.

I use source /opt/intel/oneapi/setvars.sh to set up my env but I want to make it in my Makefile.

I search for how to use source in Makefile and I learn to write this:

SHELL=/bin/bash
s:
    source /opt/intel/oneapi/setvars.sh
CC = mpicc
OPT = -Ofast
CFLAGS = -Wall -std=c99 $(OPT) -fopenmp -march=native
LDFLAGS = -Wall -fopenmp
LDLIBS = $(LDFLAGS)

targets = benchmark-naive benchmark-omp benchmark-mpi
objects = check.o benchmark.o stencil-naive.o stencil-omp.o stencil-mpi.o

.PHONY : default
default : all

.PHONY : all
all : clean $(targets)

benchmark-naive : check.o benchmark.o stencil-naive.o
    $(CC) -o $@ $^ $(LDLIBS)

benchmark-omp : check.o benchmark.o stencil-omp.o
    $(CC) -o $@ $^ $(LDLIBS)

benchmark-mpi : check.o benchmark.o stencil-mpi.o
    $(CC) -o $@ $^ $(LDLIBS)

%.o : %.c common.h
    $(CC) -c $(CFLAGS) $< -o $@

.PHONY: clean
clean:
    rm -rf $(targets) $(objects)

But after run make it just load my env and do nothing. Can anyone help me to put my source in my makefile while keep my previous makefile content?

Upvotes: 1

Views: 151

Answers (3)

Philippe
Philippe

Reputation: 26592

Can you try this :

SHELL=/bin/bash --rcfile /opt/intel/oneapi/setvars.sh

Upvotes: 0

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12332

You have to call make with the env set recursively if it wasn't set:

ifndef <some test to see if env is missing>
%:
     source /opt/intel/oneapi/setvars.sh $(MAKE) $(MAKECMDGOALS)
else
<normal makefile>
endif

Upvotes: 1

Marcell Juh&#225;sz
Marcell Juh&#225;sz

Reputation: 548

You can look at the following makefile:

# makefile
all: prog

prog:
    export ASD=asd
    echo $$ASD
    export ASD=asd; echo $$ASD

The output is:

export ASD=asd
echo $ASD

export ASD=asd; echo $ASD
asd

A makefile is not a bash script. 'make' is processing a makefile and it creates a child process for each line to run a shell to execute the statements. The variable that you set in a line is only valid for that child process, it will not be inherited back to the parent or to subsequent child processes.

I know this does not solve your issue, but at least it is some kind of an explanation why it does not work.

Upvotes: 0

Related Questions