将一个 Makefile 包含到多个其他 Makefile 或如何在项目之间共享 Makefile

include one Makefile to several other Makefile-s OR how to share Makefile between projects

我有以下结构,它由由同一个 Makefile 构建的独立项目组成(差异仅在 BUILD_TYPELINKER_TYPE 变量中):

wakatana@ubuntu:~/make$ ls -1
01-proj
02-proj
03-proj
04-proj

通用 Makefile:

BUILD_TYPE := cargo
.DEFAULT_GOAL := default
FULL_PATH := $(shell readlink -f './build')
DIR_NAME := $(shell basename $(shell pwd))
LINKER_TYPE := none

ifeq ($(BUILD_TYPE), cmake)
symlink: symlink_cmake
default: build_cmake
else ifeq ($(BUILD_TYPE), cargo)
symlink: symlink_cargo
default: build_cargo
endif

ifeq ($(LINKER_TYPE), static)
all: symlink build_static
else ifeq ($(LINKER_TYPE), dynamic)
all: symlink build_dynamic
else ifeq ($(LINKER_TYPE), none)
all: symlink default
endif

ramdisk:
    sudo mount -t tmpfs -o size=1024m tmpfs /mnt/ramdisk

build_cmake:
    rm -rf ./build/*
    cmake -S . -B$(FULL_PATH)
    cmake --build $(FULL_PATH)

build_cargo:
    cargo build --release --verbose

symlink_cmake:
    @ echo "creating symlinks for cmake"
    @ rm -rf ./build
    @ mkdir -p /mnt/ramdisk/$(DIR_NAME)/build
    @ ln -s /mnt/ramdisk/$(DIR_NAME)/build build

symlink_cargo:
    @ echo "creating symlinks for cargo"
    @ rm -rf ./target
    @ mkdir -p /mnt/ramdisk/$(DIR_NAME)/target
    @ ln -s /mnt/ramdisk/$(DIR_NAME)/target target

build_static: default
    gcc main.c -o myprog -I ./include_c/ ./target/release/libmylib.a -lpthread -ldl

build_dynamic: default
    gcc -o myprog -I ./include_c main.c -L ./target/release/ -l:libmylib.so
    @ # export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:"./target/release/"

clean:
    rm -f myprog a.out
    rm -rf ./target ./build

是否有可能在父文件夹中有一个 Makefile 并以某种方式将其包含到所有其他项目中(BUILD_TYPE & LINKER_TYPE 将为特定项目设置)?也欢迎提出更好的 Makefile 的建议。谢谢

我已经弄明白了。可以使用 makeinclude 命令。我的结构现在看起来像这样:

wakatana@ubuntu:~/make$ ls -1
01-proj
02-proj
03-proj
04-proj
makefile.mk

makefile.mk的内容:

ifeq ($(BUILD_TYPE), cmake)
symlink: symlink_cmake
default: build_cmake
else ifeq ($(BUILD_TYPE), cargo)
symlink: symlink_cargo
default: build_cargo
endif

ifeq ($(LINKER_TYPE), static)
all: symlink build_static
else ifeq ($(LINKER_TYPE), dynamic)
all: symlink build_dynamic
else ifeq ($(LINKER_TYPE), none)
all: symlink default
endif

ramdisk:
    sudo mount -t tmpfs -o size=1024m tmpfs /mnt/ramdisk

build_cmake:
    rm -rf ./build/*
    cmake -S . -B$(FULL_PATH)
    cmake --build $(FULL_PATH)

build_cargo:
    cargo build --release --verbose

symlink_cmake:
    @ echo "creating symlinks for cmake"
    @ rm -rf ./build
    @ mkdir -p /mnt/ramdisk/$(DIR_NAME)/build
    @ ln -s /mnt/ramdisk/$(DIR_NAME)/build build

symlink_cargo:
    @ echo "creating symlinks for cargo"
    @ rm -rf ./target
    @ mkdir -p /mnt/ramdisk/$(DIR_NAME)/target
    @ ln -s /mnt/ramdisk/$(DIR_NAME)/target target

build_static: default
    gcc main.c -o myprog -I ./include_c/ ./target/release/libmylib.a -lpthread -ldl
    @ # gcc main.c -I ./include_c/ -L./target/release/ -lmylib -lpthread -ldl
    @ # gcc main.c -I ./include_c/ -L./target/release/ -l:libmylib.a -lpthread -ldl
    @ # vsetko nalinkuje staticky (aj glibc) neda sa ani cez ldd pozriet
    @ # gcc -static main.c -I ./include_c/ -L./target/release/ -l:libmylib.a -lpthread -ldl

build_dynamic: default
    gcc -o myprog -I ./include_c main.c -L ./target/release/ -l:libmylib.so
    @ # gcc -o myprog -I ./include_c main.c -L ./target/release/ -lmylib
    @ # export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:"./target/release/"

clean:
    rm -f myprog a.out
    rm -rf ./target ./build

特定项目中 Makefile 的内容(BUILD_TYPELINKER_TYPE 可能因特定项目而异):

BUILD_TYPE := cmake
.DEFAULT_GOAL := default
FULL_PATH := $(shell readlink -f './build')
DIR_NAME := $(shell basename $(shell pwd))
LINKER_TYPE := none
include ../makefile.mk