How to make two different source directories in a Makefile into one bin directory?

I have the following Makefile to create my erlang project:

.SUFFIXES: .erl .beam .yrl

ERL_SRC := $(wildcard src/*.erl)
ERL_OBJ := $(patsubst src/%.erl,ebin/%.beam,${ERL_SRC})

all: main

main: ${ERL_OBJ}

ebin/%.beam: src/%.erl
        erlc +debug_info -W -o ebin $<

clean:
        rm -fr ebin/*.beam

I am trying to update this to also create my eunit tests in the test / eunit folder and get output to the same ebin folder as src like this:

.SUFFIXES: .erl .beam .yrl

ERL_SRC := $(wildcard src/*.erl)
ERL_OBJ := $(patsubst src/%.erl,ebin/%.beam,${ERL_SRC})
EUNIT_SRC := $(wildcard test/eunit/*.erl)
EUNIT_OBJ := $(patsubst test/eunit/%.erl,ebin/%.beam,${EUNIT_SRC})

all: main

main: ${ERL_OBJ}

ebin/%.beam: src/%.erl test/eunit/%.erl
        erlc +debug_info -W -o ebin $<

clean:
        rm -fr ebin/*.beam

eunit: ${EUNIT_OBJ}

test: main eunit

Make the main work great, but if I try to run the test, it will not work with:

make: *** No rule to make target `ebin/data_eunit.beam', needed by `eunit'.  Stop.

The test_eunit.erl module is in the / eunit test. The problem seems to be related to the ebin /% target value. If I change src /%. Erl on test / eunit /%. Erl, then I can build tests, but not src. How can I build from two source folders and output the output to one output folder?

+2
source share
6 answers

vpath/VPATH Makefile

.SUFFIXES: .erl .beam .yrl

# use vpath to tell make where to search for %.erl files
vpath %.erl src eunit
# or use VPATH to tell make where to search for any prerequisite
# VPATH=src:eunit    

ERL_OBJ = $(patsubst src/%.erl,ebin/%.beam, $(wildcard src/*erl))
ERL_OBJ += $(patsubst eunit/%.erl,ebin/%.beam, $(wildcard eunit/*erl))

all: main

main: ${ERL_OBJ}

ebin/%.beam: %.erl
    erlc +debug_info -W -o ebin $<

clean:
    rm -fr ebin/*.beam
+5

, . erlang erl -make Emakefile, :

{"src/*", [debug_info, {outdir, "ebin"}, {i, "include"}]}.

, , src loc, - ebin. , .

+4

, ebin/ . Makefile Makefile:

all:
    (cd src && erl -make)

test:
    (cd test && erl -make && \
       erl -noinput -eval 'eunit:test({dir, "."}, [verbose]), init:stop()')

src/Emakefile:

{['*'],
 [{outdir,"../ebin"}]}.

test/Emakefile

{['../src/*'], 
 [debug_info, {d, 'TEST'}]}.
{['*'],
 [debug_info, {d, 'TEST'}]}.

, make all, src/*. erl ebin/, make test, src/*. erl test/*. erl test/ .

TEST , , ifdefs, eunit:

-ifdef(TEST).
test_code_() -> ...
-endif.

, .

+2

, ebin, .

0

, :

.SUFFIXES: .erl .beam .yrl

ERL_SRC := $(wildcard src/*.erl)
ERL_OBJ := $(patsubst src/%.erl,ebin/%.beam,${ERL_SRC})
EUNIT_SRC := $(wildcard test/eunit/*.erl)
EUNIT_OBJ := $(patsubst test/eunit/%.erl,ebin/%.beam,${EUNIT_SRC})

all: main

main: ${ERL_OBJ}

${ERL_OBJ}: ${ERL_SRC}
        erlc +debug_info -W -o ebin $<

clean:
        rm -fr ebin/*.beam

${EUNIT_OBJ}: ${EUNIT_SRC}
        erlc +debug_info -W -o ebin $<

eunit: ${EUNIT_OBJ}

test: main eunit

? , ${ERL_OBJ} ${EUNIT_OBJ} .

0

Instead of "ebin /%. Beam: src /%. Erl test / eunit /%. Erl" you tried simply:

% beam:%. erl

0
source

All Articles