From 85bb26a08c02d27caa9d33abc80a3b6449208ad1 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 8 Apr 2025 17:28:03 +0200 Subject: [PATCH] use cargo instead of rustc --- Cargo.toml | 8 +++----- makefile | 39 ++++++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b08e922..9dee3fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "dummy" -version = "0.1.0" -edition = "2021" +name = "mcaselector-lite" +version = "0.0.1" +edition = "2024" [lib] crate-type = ["staticlib"] @@ -11,5 +11,3 @@ panic = "abort" [profile.release] panic = "abort" - -[dependencies] diff --git a/makefile b/makefile index 7a28beb..27cac15 100644 --- a/makefile +++ b/makefile @@ -4,28 +4,29 @@ # - rustc # - bear (debug) # - git bash (windows) -NAME := mcaselector-lite - -# TODO: use cargo instead of rustc, as then the Cargo.toml file is actually useful (and will help with linting) -RUSTC := rustc +NAME := mcaselector-lite +DEBUG ?= 0 +ARCH ?= 0 # C compiler options CC := clang CLANG := c CSTD := c17 CFLAGS := -Wall -Wextra -Wpedantic -Wno-pointer-arith -static -RSFLAGS := --crate-type=staticlib -C panic=abort LDFLAGS := -DEBUG ?= 0 -ARCH ?= 0 + +# Rust compiler options +RUSTC := cargo rustc +RSFLAGS := ifeq ($(DEBUG),1) CFLAGS += -DDEBUG -g -Og -RSFLAGS += -C debuginfo=2 +RSOUT := debug PROF := dbg else CFLAGS += -O2 -Werror -RSFLAGS += -D warnings +RSOUT := release +RSFLAGS := --release PROF := rel endif @@ -34,10 +35,12 @@ ifeq ($(ARCH),linux-x86_64) CFLAGS += -target x86_64-pc-linux-gnu LDFLAGS += -target x86_64-pc-linux-gnu RSFLAGS += --target=x86_64-unknown-linux-gnu +RSOUT := target/x86_64-unknown-linux-gnu/$(RSOUT) else ifeq ($(ARCH),win-x86_64) CFLAGS += -target x86_64-pc-windows-gnu LDFLAGS += -target x86_64-pc-windows-gnu RSFLAGS += --target=x86_64-pc-windows-gnu +RSOUT := target/x86_64-pc-windows-gnu/$(RSOUT) EXT := .exe else $(error you must set the ARCH environment variable to one of these: 'linux-x86_64' 'win-x86_64') @@ -49,7 +52,9 @@ C_SRC := $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/**/**/*.c) $ C_OBJ := $(patsubst src/%,obj/$(ARCH)/$(PROF)/%,$(C_SRC:.c=.o)) C_DEP := $(C_OBJ:.o=.d) RS_SRC := $(wildcard src/*.rs) $(wildcard src/**/*.rs) $(wildcard src/**/**/*.rs) $(wildcard src/**/**/**/*.rs) $(wildcard src/**/**/**/**/*.rs) -RS_OBJ := $(patsubst src/%,obj/$(ARCH)/$(PROF)/%,$(RS_SRC:.rs=.o)) +RS_LIB := $(RSOUT)/libmcaselector_lite.a +RS_DEP := $(RSOUT)/libmcaselector_lite.d +RSOUT := DIR := bin/$(ARCH)/$(PROF) $(sort obj/$(ARCH)/$(PROF) $(dir $(C_SRC)) $(dir $(RS_SRC))) DIR_BUILD := bin/$(ARCH)/$(PROF) @@ -65,11 +70,11 @@ run: compile ./$(TARGET) compile: compile_commands.json $(DIR) $(TARGET) clean: - rm -rf bin/ obj/ compile_commands.json + rm -rf bin/ obj/ target/ compile_commands.json # TODO: write a structure for the unit tests in this # create the binary (linking step) -$(TARGET): $(C_OBJ) $(RS_OBJ) +$(TARGET): $(C_OBJ) $(RS_LIB) @$(call wr_colour,"RUSTC: '$(RUSTC)'",94) @$(call wr_colour,"CC: '$(CC)'",94) @$(call wr_colour,"CFLAGS: '$(CFLAGS)'",94) @@ -77,7 +82,7 @@ $(TARGET): $(C_OBJ) $(RS_OBJ) @$(call wr_colour,"LDFLAGS: '$(LDFLAGS)'",94) @$(call wr_colour,"linking to: '$@'",92) - @$(CC) $(LDFLAGS) -o $@ $(C_OBJ) $(RS_OBJ) + @$(CC) $(LDFLAGS) -o $@ $(C_OBJ) $(RS_LIB) @$(call wr_colour,"current profile: '$(PROF)'",93) # create .o and .d files for C sources @@ -85,10 +90,9 @@ $(C_OBJ): $(C_SRC) @$(call wr_colour,"compiling $(notdir $@) from $(notdir $<)",92) @$(CC) $(CFLAGS) -c -MD -MP -std=$(CSTD) -x $(CLANG) -o $@ $< -# create .o files for RUST sources -$(RS_OBJ): $(RS_SRC) - @$(call wr_colour,"compiling $(notdir $@) from $(notdir $<)",92) - @$(RUSTC) $(RSFLAGS) --emit=obj -o $@ $< +# create .o and .d files for RUST sources +$(RS_LIB): $(RS_SRC) + $(RUSTC) $(RSFLAGS) # create directories $(DIR): @@ -106,3 +110,4 @@ endif # include the dependencies -include $(C_DEP) +-include $(RS_DEP)