Question

I'm trying to setup a Makefile to compile my Rust project. To speed things up I don't want to recompile the whole project all at once. Rust allows you to create libraries that can then be linked into the main executable. Given two files src/iomrascalai.rs (the main program) and src/board/mod.rs (the library) to compile it by hand it would work like this:

$ rustc --out-dir lib src/board/mod.rs
$ ls lib/
libboard-d085aa56-0.0.rlib
$ rustc --out-dir bin -L lib src/iomrascalai.rs
$ ls bin/
iomrascalai

What I'm trying to do in the Makefile is to define dynamic rules to compile the libraries. I'm trying to do it like that as the mapping from source file (src/board/mod.rs) to library name (lib/libboard-d085aa56-0.0.rlib) is non-trivial. However, I can't seem to make it work:

MAIN   = src/iomrascalai.rs
CRATES = src/board/mod.rs

LIBS   = $(foreach crate, $(CRATES), $(call TO_LIB, $(crate)))

all: exe

exe: $(MAIN) $(LIBS)
    rustc --out-dir bin -L lib $(MAIN)

TO_LIB = $(addprefix lib/, $(shell rustc --crate-file-name $(1)))

define COMPILE_CRATE
$(call TO_LIB, $(1)): $(1)
    rustc --out-dir lib $(1)
endef

$(foreach crate, $(CRATES), $(eval $(call COMPILE_CRATE, $(crate))))

Running make results in the following error:

$ make
rustc --out-dir bin -L lib src/iomrascalai.rs
src/iomrascalai.rs:22:1: 22:20 error: can't find crate for `board`
src/iomrascalai.rs:22 extern crate board;
                  ^~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
make: *** [exe] Error 101

So the libraries aren't compiled which means that the main program can't be compiled. When I try to run make to compile the lib target then it works:

$ rustc --crate-file-name src/board/mod.rs
libboard-d085aa56-0.0.rlib
$ make lib/libboard-d085aa56-0.0.rlib
rustc --out-dir lib  src/board/mod.rs

So for some reason the exe doesn't honor the prerequisites even though the rules are defined ...

Was it helpful?

Solution

Recursively expanded make variables (those which are assigned with =) are expanded whenever the variable is substituted. That means, other variables you use need to be defined before. In your case, you need to move up the definition of TO_LIB a few lines (at least above the exe rule since the expansion of LIBS need to have TO_LIB available there)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top