I am trying to get a simple example of using build.rs to run Makefile commands but I have not had much success yet. Does anyone have an example of this working?
So far I have a small project with the following structure:
-makefile_integration
-----c_folder
---------c_test_lib.c (source file, library to build)
---------Makefile
-----src/main.rs
-----build.rs
Makefile is shown below. In c_folder, make and make clean create and destroy libc_test.a (along with other object files) in that folder as expected.
SRCS = c_test_lib.c
OBJS = $(SRCS:.c=.o)
TARGET_LIB = libc_test.a
CC = cc
RM = rm -f
$(TARGET_LIB): $(OBJS)
ar rcs $@ $^
all: ${TARGET_LIB}
$(SRCS:.c=.d):%.d:%.c
$(CC) -MM $< >$@
include $(SRCS:.c=.d)
clean:
-${RM} ${TARGET_LIB} ${OBJS} ${SRCS:.c=.d}
.PHONY: all clean
In build.rs, I have tried a couple of ways to get the same functionality from Cargo, but none are working.
let current = env::current_dir().unwrap();
Command::new("make").env("PATH", current).args(&["-f", "c_folder/Makefile"]).status().unwrap();
This panics and cannot find the directory I want.
let c_folder = env::current_dir().unwrap().join("c_folder");
Command::new("make").current_dir(&Path::new(&c_folder)).status().unwrap();
This builds libc_test.a in c_folder (not somewhere in target) so main.rs does not have access to any C functions. And for whatever reason cargo clean does not clean this folder
Does anyone have an example of getting a build.rs to call a Makefile properly? Or know what I am doing wrong? I am trying to avoid using the Makefile to call cargo (though I know this is possible) and using cargo-make, and I think this should be possible with the proper methods on Command.