common.mk 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ARCH := i686-elf
  20. # Directories
  21. SRCDIR := src
  22. OBJDIR := obj
  23. DEPDIR := dep
  24. TOOLSDIR := tools
  25. TOOLSROOTDIR := root
  26. PROJECT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
  27. export PATH := $(PROJECT_ROOT)/$(TOOLSDIR)/$(TOOLSROOTDIR)/bin:$(PATH)
  28. # Compilers and tools
  29. CC := $(ARCH)-gcc
  30. ASM := nasm
  31. LINK := $(ARCH)-ld
  32. ifeq ($(DEBUG), yes)
  33. CFLAGS += -g -DDEBUG
  34. else
  35. CFLAGS += -O3
  36. endif
  37. ifeq ($(LINK_WITH_LIBGCC), yes)
  38. LIBGCCDIR := $(shell PATH=$(PATH) $(CC) -print-file-name=)
  39. LDFLAGS += -L "$(LIBGCCDIR)" -lgcc
  40. endif
  41. # Output files
  42. OUTPUTS := $(OUTPUT_KERNEL) $(OUTPUT_PROGRAM) $(OUTPUT_DRIVER) $(OUTPUT_STATIC_LIB) $(OUTPUT_DYNAMIC_LIB) $(ADDITIONAL_OBJECTS)
  43. DEPENDS := $(shell find $(DEPDIR) -type f -name \*.d)
  44. OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(patsubst $(SRCDIR)/%.asm, $(OBJDIR)/%.o, $(SOURCES)))
  45. .PHONY: all clean
  46. all: $(OBJDIR) $(DEPDIR) $(OUTPUTS)
  47. -include $(DEPENDS)
  48. clean:
  49. rm -f $(OUTPUTS)
  50. find $(OBJDIR) -name \*.o -delete
  51. find $(DEPDIR) -name \*.d -delete
  52. $(OBJDIR):
  53. mkdir -p $(OBJDIR)
  54. $(DEPDIR):
  55. mkdir -p $(DEPDIR)
  56. $(OBJDIR)/%.o: $(SRCDIR)/%.c Makefile
  57. mkdir -p $(dir $@)
  58. mkdir -p $(dir $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d))
  59. $(CC) $(CFLAGS) -MMD -MP -MF $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d) -o $@ -c $<
  60. $(OBJDIR)/%.o: $(SRCDIR)/%.asm Makefile
  61. mkdir -p $(dir $@)
  62. $(ASM) $(ASMFLAGS) -o $@ $<
  63. $(OUTPUT_KERNEL): $(OBJECTS) $(ADDITIONAL_OBJECTS)
  64. $(LINK) -o $@ $(OBJECTS) $(LDFLAGS)
  65. $(OUTPUT_PROGRAM): $(OBJECTS)
  66. $(LINK) -eprocess_startup -o $@ $(OBJECTS) $(LDFLAGS)
  67. $(OUTPUT_STATIC_LIB): $(OBJECTS)
  68. $(AR) rcs $@ $^
  69. $(OUTPUT_DYNAMIC_LIB) $(OUTPUT_DRIVER): $(OBJECTS)
  70. $(LINK) -shared -o $@ $(OBJECTS) $(LDFLAGS)