Makefile 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Kohi.Core makefile
  2. ASSEMBLY_NAME=kohi.core
  3. # All compiler flags/rules used across the board.
  4. CFLAGS =-std=gnu11 -Wall -Wextra -Werror -Wno-error=deprecated-declarations -Wno-error=unused-function
  5. CFLAGS +=-Wvla -Werror=vla -Wgnu-folding-constant -Wno-missing-braces -Wstrict-prototypes -Wno-unused-parameter
  6. CFLAGS +=-Wno-missing-field-initializers -Wno-tautological-compare
  7. # NOTE: -fvisibility=hidden hides all symbols by default, and only those that explicitly say otherwise are exported (i.e. via KAPI).
  8. CFLAGS +=-fvisibility=hidden
  9. # Base linker flags
  10. LDFLAGS = -Lbin -Llib
  11. # Base include flags
  12. INCLUDE_FLAGS = -Isrc
  13. DEFINES =-DKEXPORT
  14. OUTPUT_NAME =
  15. UNAME_S :=
  16. ifneq ($(OS),Windows_NT)
  17. UNAME_S = $(shell uname -s)
  18. endif
  19. ifeq ($(OS),Windows_NT)
  20. PLATFORM := win32
  21. # Make does not offer a recursive wildcard function, and Windows needs one, so here it is:
  22. rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
  23. DIR := $(subst /,\,${CURDIR})
  24. SRC_FILES := $(call rwildcard,src,*.c)
  25. DIRECTORIES := \src $(subst $(DIR),,$(shell dir src /S /AD /B | findstr /i src))
  26. OBJ_FILES := $(SRC_FILES:%=obj/%.o)
  27. else ifeq ($(UNAME_S),Linux)
  28. PLATFORM := linux
  29. SRC_FILES := $(shell find src -name "*.c")
  30. DIRECTORIES := $(shell find src -type d)
  31. OBJ_FILES := $(SRC_FILES:%=obj/%.o)
  32. else ifeq ($(UNAME_S),Darwin)
  33. PLATFORM := macos
  34. SRC_FILES := $(shell find src -type f \( -name "*.c" -o -name "*.m" \))
  35. DIRECTORIES := $(shell find src -type d)
  36. OBJ_FILES := $(SRC_FILES:%=obj/%.o)
  37. else
  38. $(error Unsupported platform)
  39. endif
  40. # =========================================================
  41. # BEGIN TARGET RECIPES
  42. # =========================================================
  43. .PHONY: all-release all-debug flags-debug flags-release
  44. .PHONY: genversion genversion-win32 genversion-linux genversion-macos
  45. .PHONY: scaffold scaffold-win32 scaffold-linux scaffold-macos
  46. .PHONY: link link-win32 link-linux link-macos
  47. .PHONY: clean clean-win32 clean-linux clean-macos
  48. all-release: scaffold genversion gen_compile_flags-release flags-release compile link
  49. all-debug: scaffold genversion gen_compile_flags-debug flags-debug compile link
  50. flags-debug: DEFINES += -D_DEBUG
  51. flags-debug: CFLAGS += -g -MD -O0 -fno-omit-frame-pointer
  52. flags-debug: LDFLAGS += -g
  53. flags-debug: link compile
  54. flags-release: DEFINES += -DKRELEASE
  55. flags-release: CFLAGS += -MD -O2
  56. flags-release: LDFLAGS +=
  57. flags-release: link compile
  58. scaffold: scaffold-$(PLATFORM)
  59. .NOTPARALLEL: scaffold
  60. scaffold-win32:
  61. @if not exist bin mkdir bin
  62. -@mkdir $(addprefix obj\\, $(DIRECTORIES)) 2>nul
  63. scaffold-linux scaffold-macos:
  64. @mkdir -p bin
  65. @mkdir -p $(addprefix obj/,$(DIRECTORIES))
  66. genversion: genversion-$(PLATFORM)
  67. genversion-win32:
  68. @echo -- Gen Version --
  69. @..\misc\versiongen.exe version.txt -outfile=src\$(ASSEMBLY_NAME)_version.h
  70. genversion-linux genversion-macos:
  71. @../misc/versiongen version.txt -outfile=src/$(ASSEMBLY_NAME)_version.h
  72. clean: clean-$(PLATFORM)
  73. clean-win32:
  74. @echo -- Cleaning --
  75. if exist bin del /s /q bin\*.*
  76. if exist obj del /s /q obj\*.*
  77. clean-linux clean-macos:
  78. @rm -rf bin/*
  79. @rm -rf obj/*
  80. link: link-$(PLATFORM)
  81. # link-linux: CFLAGS += -fsanitize=address
  82. # link-win32: LDFLAGS += -fsanitize=address
  83. link-win32: LDFLAGS += -fdeclspec -Wno-cast-function-type-mismatch -luser32 -lgdi32 -lwinmm -Xlinker /INCREMENTAL
  84. link-win32: DEFINES += -D_CRT_SECURE_NO_WARNINGS -DUNICODE
  85. link-win32: OUTPUT_NAME = bin\$(ASSEMBLY_NAME).dll
  86. # NOTE: --no-undefined and --no-allow-shlib-undefined ensure that symbols linking against are resolved.
  87. # These are linux-specific, as the default behaviour is the opposite of this, allowing code to compile
  88. # here that would not on other platforms from not being exported (i.e. Windows)
  89. # Discovered the solution here for this: https://github.com/ziglang/zig/issues/8180
  90. link-linux: CFLAGS += -Wno-cast-function-type-mismatch -fPIC
  91. link-linux: LDFLAGS += -lxcb -lX11 -lX11-xcb -lxkbcommon -lxcb-xkb -lm -ldl -L/usr/X11R6/lib -Wl,--no-undefined,--no-allow-shlib-undefined,-rpath='$$ORIGIN'
  92. link-linux: OUTPUT_NAME = bin/lib$(ASSEMBLY_NAME).so
  93. link-macos: LDFLAGS += -dynamiclib -install_name @rpath/lib$(ASSEMBLY_NAME).dylib -lobjc -framework AppKit -framework QuartzCore -framework DiskArbitration -framework CoreFoundation
  94. link-macos: OUTPUT_NAME = bin/lib$(ASSEMBLY_NAME).dylib
  95. link-win32 link-linux link-macos: link-common
  96. link-common: $(OBJ_FILES)
  97. @clang $(OBJ_FILES) -o $(OUTPUT_NAME) -shared $(LDFLAGS)
  98. .PHONY: compile
  99. compile:
  100. -include $(OBJ_FILES:.o=.d)
  101. # compile .c to .o object for windows, linux and mac
  102. obj/%.c.o: %.c
  103. @echo $<...
  104. @clang $< $(CFLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  105. # compile .m to .o object only for macos
  106. obj/%.m.o: %.m
  107. @echo $<...
  108. @clang $< $(CFLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  109. -include $(OBJ_FILES:.o=.d)
  110. .PHONY: gen_compile_flags-debug
  111. gen_compile_flags-debug: DEFINES += -D_DEBUG
  112. gen_compile_flags-debug: gen_compile_flags
  113. gen_compile_flags-release: gen_compile_flags
  114. .PHONY: gen_compile_flags
  115. gen_compile_flags:
  116. ifeq ($(BUILD_PLATFORM),windows)
  117. @..\misc\cfgen -outfile=compile_flags.txt $(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0
  118. else
  119. @../misc/cfgen -outfile=compile_flags.txt $(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0
  120. endif