Makefile 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # detect architecture
  2. MYARCH = $(shell uname -m | sed s,i[3456789]86,ia32, | sed s,amd,x86_,)
  3. ifeq ($(ARCH),)
  4. ARCH = $(MYARCH)
  5. endif
  6. # get source files, generate object names
  7. ifeq ($(SRCS),)
  8. SRCS = $(wildcard *.c) $(wildcard *.S)
  9. endif
  10. ifeq ($(OBJS),)
  11. TMP = $(SRCS:.c=.o)
  12. OBJS = $(TMP:.S=.o)
  13. endif
  14. ifneq ($(OUTDIR),)
  15. # OUTDIR is not used with uefi/*.o deliberately
  16. OUTDIR:=$(addsuffix /,$(OUTDIR))
  17. endif
  18. CFLAGS += -fshort-wchar -fno-strict-aliasing -ffreestanding -fno-stack-protector -fno-stack-check -I. -I./uefi \
  19. -I/usr/include -I/usr/include/efi -I/usr/include/efi/protocol -I/usr/include/efi/$(ARCH) -D__$(ARCH)__
  20. ifeq ($(ARCH),x86_64)
  21. CFLAGS += -DHAVE_USE_MS_ABI -mno-red-zone
  22. endif
  23. # for libuefi.a
  24. LIBSRCS = $(filter-out $(wildcard crt_*.c),$(wildcard *.c)) $(wildcard *.S)
  25. TMP2 = $(LIBSRCS:.c=.o)
  26. LIBOBJS = $(TMP2:.S=.o)
  27. MAKE ?= make
  28. # detect toolchain
  29. ifeq ($(wildcard /usr/bin/clang)$(wildcard /usr/local/bin/clang)$(wildcard /usr/lib/llvm/*/bin/clang)$(wildcard /gnu/store/*/bin/clang),)
  30. USE_GCC = 1
  31. endif
  32. ifneq ($(USE_GCC),)
  33. ifeq ($(ARCH),x86_64)
  34. CFLAGS += -maccumulate-outgoing-args
  35. endif
  36. CFLAGS += -Wno-builtin-declaration-mismatch -fpic -fPIC
  37. LDFLAGS += -nostdlib -shared -Bsymbolic -Luefi uefi/crt_$(ARCH).o
  38. LIBS += -o $(addprefix $(OUTDIR),$(TARGET).so) -luefi -T uefi/elf_$(ARCH)_efi.lds
  39. # see if we're cross-compiling
  40. ifneq ($(ARCH),$(MYARCH))
  41. CC = $(ARCH)-elf-gcc
  42. LD = $(ARCH)-elf-ld
  43. OBJCOPY ?= $(ARCH)-elf-objcopy
  44. else
  45. CC = gcc
  46. LD = ld
  47. OBJCOPY ?= objcopy
  48. endif
  49. ifeq ($(ARCH),aarch64)
  50. EFIARCH = pei-aarch64-little
  51. else
  52. ifeq ($(ARCH),riscv64)
  53. EFIARCH = pei-riscv64-little
  54. else
  55. EFIARCH = efi-app-$(ARCH)
  56. endif
  57. endif
  58. AR ?= ar
  59. else
  60. CFLAGS += --target=$(ARCH)-pc-win32-coff -Wno-builtin-requires-header -Wno-incompatible-library-redeclaration -Wno-long-long
  61. LDFLAGS += -subsystem:efi_application -nodefaultlib -dll -entry:uefi_init uefi/*.o
  62. LIBS = -out:$(addprefix $(OUTDIR),$(TARGET))
  63. CC = clang
  64. LD = lld -flavor link
  65. OBJCOPY = true
  66. endif
  67. # recipies
  68. ifeq ($(wildcard uefi/Makefile),)
  69. ALLTARGETS = crt_$(ARCH).o libuefi.a buildlib
  70. else
  71. ALLTARGETS = uefi/crt_$(ARCH).o uefi/libuefi.a $(OBJS) $(TARGET)
  72. endif
  73. all: $(OUTDIR) $(EXTRA) $(ALLTARGETS) $(ALSO)
  74. ifneq ($(OUTDIR),)
  75. $(OUTDIR):
  76. @mkdir -p $(OUTDIR)
  77. endif
  78. uefi/libuefi.a:
  79. @$(MAKE) --no-print-directory -C uefi libuefi.a USE_GCC=$(USE_GCC) ARCH=$(ARCH)
  80. libuefi.lib: $(LIBOBJS)
  81. libuefi.a: $(LIBOBJS)
  82. @rm $@ 2>/dev/null || true
  83. $(AR) -frsv $@ $(LIBOBJS) >/dev/null
  84. $(TARGET): $(addprefix $(OUTDIR),$(TARGET).so)
  85. ifneq ($(USE_GCC),)
  86. $(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)
  87. @rm $(addprefix $(OUTDIR),$(TARGET).so)
  88. endif
  89. $(addprefix $(OUTDIR),$(TARGET).so): $(addprefix $(OUTDIR),$(OBJS)) $(EXTRA)
  90. $(LD) $(LDFLAGS) $^ $(LIBS)
  91. @rm $(addprefix $(OUTDIR),*.lib) 2>/dev/null || true
  92. uefi/%.o: uefi/%.c
  93. $(CC) $(CFLAGS) -c $< -o $@
  94. %.o: %.c
  95. $(CC) $(CFLAGS) -c $< -o $(addprefix $(OUTDIR),$@)
  96. %.o: %.S
  97. $(CC) $(CFLAGS) -c $< -o $(addprefix $(OUTDIR),$@)
  98. buildlib:
  99. @mkdir ../build ../build/uefi 2>/dev/null || true
  100. @cp crt_$(ARCH).o ../build/uefi/crt0.o
  101. @cp elf_$(ARCH)_efi.lds ../build/uefi/link.ld
  102. ifneq ($(USE_GCC),)
  103. @cp libuefi.a uefi.h ../build/uefi
  104. else
  105. @cp uefi.h ../build/uefi
  106. @cp libuefi.a ../build/uefi/libuefi.dll.a
  107. endif
  108. clean:
  109. @rm $(addprefix $(OUTDIR),$(TARGET)) *.o *.a *.lib *.elf $(LIBOBJS) 2>/dev/null || true
  110. ifneq ($(OUTDIR),)
  111. @rm -rf $(OUTDIR)
  112. endif
  113. distclean: clean
  114. ifeq ($(wildcard uefi/Makefile),)
  115. @rm -rf ../build 2>/dev/null || true
  116. else
  117. @rm uefi/*.o uefi/libuefi.a 2>/dev/null || true
  118. endif