common.mk 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #
  2. # common.mk
  3. #
  4. # Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ifndef ARCH
  20. ARCH := i686-elf
  21. endif
  22. # Directories
  23. SRCDIR := src
  24. OBJDIR := obj
  25. DEPDIR := dep
  26. TOOLSDIR := tools
  27. TOOLSROOTDIR := root
  28. PROJECT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
  29. export PATH := $(PROJECT_ROOT)/$(TOOLSDIR)/$(TOOLSROOTDIR)/bin:$(PATH)
  30. # Compilers and tools
  31. ASM := nasm
  32. ifneq ($(ARCH), host)
  33. CC := $(ARCH)-gcc
  34. LINK := $(ARCH)-ld
  35. LDFLAGS_PROGRAM += -eprocess_startup
  36. else
  37. CC := gcc
  38. LINK := gcc
  39. endif
  40. ifeq ($(DEBUG), yes)
  41. CFLAGS += -g -DDEBUG
  42. else
  43. CFLAGS += -O3
  44. endif
  45. ifeq ($(LINK_WITH_LIBGCC), yes)
  46. LIBGCCDIR := $(shell PATH=$(PATH) $(CC) -print-file-name=)
  47. LDFLAGS += -L "$(LIBGCCDIR)" -lgcc
  48. endif
  49. # Output files
  50. OUTPUTS := $(OUTPUT_KERNEL) $(OUTPUT_PROGRAM) $(OUTPUT_DRIVER) $(OUTPUT_STATIC_LIB) $(OUTPUT_DYNAMIC_LIB) $(ADDITIONAL_OBJECTS)
  51. DEPENDS := $(shell find $(DEPDIR) -type f -name \*.d)
  52. OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(patsubst $(SRCDIR)/%.asm, $(OBJDIR)/%.o, $(SOURCES)))
  53. .PHONY: all clean
  54. all: $(OBJDIR) $(DEPDIR) $(OUTPUTS)
  55. -include $(DEPENDS)
  56. clean:
  57. rm -f $(OUTPUTS)
  58. find $(OBJDIR) -name \*.o -delete
  59. find $(DEPDIR) -name \*.d -delete
  60. $(OBJDIR):
  61. mkdir -p $(OBJDIR)
  62. $(DEPDIR):
  63. mkdir -p $(DEPDIR)
  64. $(OBJDIR)/%.o: $(SRCDIR)/%.c Makefile
  65. mkdir -p $(dir $@)
  66. mkdir -p $(dir $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d))
  67. $(CC) $(CFLAGS) -MMD -MP -MF $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d) -o $@ -c $<
  68. $(OBJDIR)/%.o: $(SRCDIR)/%.asm Makefile
  69. mkdir -p $(dir $@)
  70. $(ASM) $(ASMFLAGS) -o $@ $<
  71. $(OUTPUT_KERNEL): $(OBJECTS) $(ADDITIONAL_OBJECTS)
  72. $(LINK) -o $@ $(OBJECTS) $(LDFLAGS)
  73. $(OUTPUT_PROGRAM): $(OBJECTS)
  74. $(LINK) -o $@ $(OBJECTS) $(LDFLAGS) $(LDFLAGS_PROGRAM)
  75. $(OUTPUT_STATIC_LIB): $(OBJECTS)
  76. $(AR) rcs $@ $^
  77. $(OUTPUT_DYNAMIC_LIB) $(OUTPUT_DRIVER): $(OBJECTS)
  78. $(LINK) -shared -o $@ $(OBJECTS) $(LDFLAGS)