posix-uefi/uefi/Makefile

130 lines
3.4 KiB
Makefile
Raw Normal View History

# detect architecture
2021-02-01 01:09:23 +01:00
MYARCH = $(shell uname -m | sed s,i[3456789]86,ia32,)
ifeq ($(ARCH),)
ARCH = $(MYARCH)
endif
# get source files, generate object names
2021-02-23 14:52:51 +01:00
ifeq ($(SRCS),)
SRCS = $(wildcard *.c) $(wildcard *.S)
endif
2022-07-11 19:58:21 +02:00
ifeq ($(OBJS),)
2021-02-01 01:09:23 +01:00
TMP = $(SRCS:.c=.o)
OBJS = $(TMP:.S=.o)
2022-07-11 19:58:21 +02:00
endif
ifneq ($(OUTDIR),)
# OUTDIR is not used with uefi/*.o deliberately
OUTDIR:=$(addsuffix /,$(OUTDIR))
endif
CFLAGS += -fshort-wchar -fno-strict-aliasing -ffreestanding -fno-stack-protector -fno-stack-check -I. -I./uefi \
-I/usr/include -I/usr/include/efi -I/usr/include/efi/protocol -I/usr/include/efi/$(ARCH) -D__$(ARCH)__
ifeq ($(ARCH),x86_64)
CFLAGS += -DHAVE_USE_MS_ABI -mno-red-zone
endif
2021-02-01 01:09:23 +01:00
# for libuefi.a
LIBSRCS = $(filter-out $(wildcard crt_*.c),$(wildcard *.c)) $(wildcard *.S)
2021-10-15 12:07:02 +02:00
TMP2 = $(LIBSRCS:.c=.o)
LIBOBJS = $(TMP2:.S=.o)
2021-02-01 01:09:23 +01:00
# detect toolchain
ifeq ($(wildcard /usr/bin/clang)$(wildcard /usr/local/bin/clang)$(wildcard /usr/lib/llvm/*/bin/clang),)
2021-02-16 11:12:36 +01:00
USE_GCC = 1
endif
2021-02-16 11:12:36 +01:00
ifneq ($(USE_GCC),)
ifeq ($(ARCH),x86_64)
CFLAGS += -maccumulate-outgoing-args
endif
CFLAGS += -Wno-builtin-declaration-mismatch -fpic -fPIC
2021-02-02 01:26:30 +01:00
LDFLAGS += -nostdlib -shared -Bsymbolic -Luefi uefi/crt_$(ARCH).o
2022-07-11 19:58:21 +02:00
LIBS += -o $(addprefix $(OUTDIR),$(TARGET).so) -luefi -T uefi/elf_$(ARCH)_efi.lds
2021-02-01 01:09:23 +01:00
# see if we're cross-compiling
ifneq ($(ARCH),$(MYARCH))
2021-02-01 01:09:23 +01:00
CC = $(ARCH)-elf-gcc
LD = $(ARCH)-elf-ld
OBJCOPY ?= $(ARCH)-elf-objcopy
2021-02-01 01:09:23 +01:00
else
CC = gcc
LD = ld
2021-02-02 01:26:30 +01:00
OBJCOPY ?= objcopy
endif
ifeq ($(ARCH),aarch64)
EFIARCH = pei-aarch64-little
else
EFIARCH = efi-app-$(ARCH)
endif
2021-02-02 01:26:30 +01:00
AR ?= ar
2021-02-01 01:09:23 +01:00
else
CFLAGS += --target=$(ARCH)-pc-win32-coff -Wno-builtin-requires-header -Wno-incompatible-library-redeclaration -Wno-long-long
2021-02-02 01:26:30 +01:00
LDFLAGS += -subsystem:efi_application -nodefaultlib -dll -entry:uefi_init uefi/*.o
2022-07-11 19:58:21 +02:00
LIBS = -out:$(addprefix $(OUTDIR),$(TARGET))
2021-02-01 01:09:23 +01:00
CC = clang
2021-11-02 19:04:09 +01:00
LD = lld -flavor link
2021-02-02 01:26:30 +01:00
OBJCOPY = true
2021-02-01 01:09:23 +01:00
endif
# recipies
2021-02-01 01:09:23 +01:00
ifeq ($(wildcard uefi/Makefile),)
2022-07-11 19:58:21 +02:00
ALLTARGETS = crt_$(ARCH).o libuefi.a buildlib
2021-02-01 01:09:23 +01:00
else
ALLTARGETS = uefi/crt_$(ARCH).o uefi/libuefi.a $(OBJS) $(TARGET)
endif
2022-08-01 13:36:38 +02:00
all: $(OUTDIR) $(EXTRA) $(ALLTARGETS) $(ALSO)
2022-07-11 19:58:21 +02:00
ifneq ($(OUTDIR),)
$(OUTDIR):
@mkdir -p $(OUTDIR)
endif
2021-02-01 01:09:23 +01:00
uefi/libuefi.a:
2021-02-16 11:12:36 +01:00
@make --no-print-directory -C uefi libuefi.a USE_GCC=$(USE_GCC) ARCH=$(ARCH)
2021-02-02 01:26:30 +01:00
libuefi.lib: $(LIBOBJS)
2021-02-01 01:09:23 +01:00
libuefi.a: $(LIBOBJS)
2021-02-16 11:12:36 +01:00
ifneq ($(USE_GCC),)
2021-02-01 01:09:23 +01:00
@rm $@ 2>/dev/null || true
$(AR) -frsv $@ $(LIBOBJS) >/dev/null
2021-02-02 01:26:30 +01:00
else
2021-02-16 22:28:14 +01:00
@true
2021-02-02 01:26:30 +01:00
endif
2021-02-01 01:09:23 +01:00
2022-07-11 19:58:21 +02:00
$(TARGET): $(addprefix $(OUTDIR),$(TARGET).so)
2021-02-16 11:12:36 +01:00
ifneq ($(USE_GCC),)
2022-07-11 19:58:21 +02:00
$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target $(EFIARCH) --subsystem=10 $^ $(addprefix $(OUTDIR),$@) || echo target: $(EFIARCH)
@rm $(addprefix $(OUTDIR),$(TARGET).so)
2021-02-02 01:26:30 +01:00
endif
2021-02-01 01:09:23 +01:00
2022-07-11 19:58:21 +02:00
$(addprefix $(OUTDIR),$(TARGET).so): $(addprefix $(OUTDIR),$(OBJS)) $(EXTRA)
2021-02-02 01:26:30 +01:00
$(LD) $(LDFLAGS) $^ $(LIBS)
2022-07-11 19:58:21 +02:00
@rm $(addprefix $(OUTDIR),*.lib) 2>/dev/null || true
2021-02-01 01:09:23 +01:00
2022-07-11 19:58:21 +02:00
uefi/%.o: uefi/%.c
2021-02-01 01:09:23 +01:00
$(CC) $(CFLAGS) -c $< -o $@
2022-07-11 19:58:21 +02:00
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $(addprefix $(OUTDIR),$@)
2021-02-01 01:09:23 +01:00
%.o: %.S
2022-07-11 19:58:21 +02:00
$(CC) $(CFLAGS) -c $< -o $(addprefix $(OUTDIR),$@)
2021-02-01 01:09:23 +01:00
2022-07-11 19:58:21 +02:00
buildlib:
2021-02-01 01:09:23 +01:00
@mkdir ../build ../build/uefi 2>/dev/null || true
@cp crt_$(ARCH).o ../build/uefi/crt0.o
@cp elf_$(ARCH)_efi.lds ../build/uefi/link.ld
@cp libuefi.a uefi.h ../build/uefi
clean:
2022-07-11 19:58:21 +02:00
@rm $(addprefix $(OUTDIR),$(TARGET)) *.o *.a *.lib *.elf $(LIBOBJS) 2>/dev/null || true
ifneq ($(OUTDIR),)
@rm -rf $(OUTDIR)
endif
2021-02-01 01:09:23 +01:00
distclean: clean
ifeq ($(wildcard uefi/Makefile),)
@rm -rf ../build 2>/dev/null || true
else
@rm uefi/*.o uefi/libuefi.a 2>/dev/null || true
endif