Makefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Kohi.Tools makefile
  2. ASSEMBLY_NAME=kohi.tools
  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 -L../kohi.core/bin -L../kohi.runtime/bin -lkohi.core -lkohi.runtime
  11. LDFLAGS +=
  12. # Base include flags
  13. INCLUDE_FLAGS = -Isrc -I../kohi.core/src -I../kohi.runtime/src
  14. DEFINES =-DKEXPORT
  15. OUTPUT_NAME =
  16. UNAME_S :=
  17. ifneq ($(OS),Windows_NT)
  18. UNAME_S = $(shell uname -s)
  19. endif
  20. ifeq ($(OS),Windows_NT)
  21. PLATFORM := win32
  22. # Make does not offer a recursive wildcard function, and Windows needs one, so here it is:
  23. rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
  24. DIR := $(subst /,\,${CURDIR})
  25. SRC_FILES := $(call rwildcard,src,*.c)
  26. DIRECTORIES := \src $(subst $(DIR),,$(shell dir src /S /AD /B | findstr /i src))
  27. OBJ_FILES := $(SRC_FILES:%=obj/%.o)
  28. INCLUDE_FLAGS += -I'$(ASSIMP)\include'
  29. LDFLAGS += -L'$(ASSIMP)\lib\x64\'
  30. else ifeq ($(UNAME_S),Linux)
  31. PLATFORM := linux
  32. SRC_FILES := $(shell find src -name "*.c")
  33. DIRECTORIES := $(shell find src -type d)
  34. OBJ_FILES := $(SRC_FILES:%=obj/%.o)
  35. INCLUDE_FLAGS += -I/usr/include/assimp/
  36. LDFLAGS += -L/usr/lib/
  37. else ifeq ($(UNAME_S),Darwin)
  38. PLATFORM := macos
  39. SRC_FILES := $(shell find src -type f \( -name "*.c" \))
  40. DIRECTORIES := $(shell find src -type d)
  41. OBJ_FILES := $(SRC_FILES:%=obj/%.o)
  42. INCLUDE_FLAGS += -I/opt/homebrew/opt/assimp/include
  43. LDFLAGS += -L/opt/homebrew/opt/assimp/lib
  44. else
  45. $(error Unsupported platform)
  46. endif
  47. # =========================================================
  48. # BEGIN TARGET RECIPES
  49. # =========================================================
  50. .PHONY: all-release all-debug flags-debug flags-release
  51. .PHONY: genversion genversion-win32 genversion-linux genversion-macos
  52. .PHONY: scaffold scaffold-win32 scaffold-linux scaffold-macos
  53. .PHONY: link link-win32 link-linux link-macos
  54. all-release: scaffold genversion gen_compile_flags-release flags-release compile link
  55. all-debug: scaffold genversion gen_compile_flags-debug flags-debug compile link
  56. flags-debug: DEFINES += -D_DEBUG
  57. flags-debug: CFLAGS += -g -MD -O0 -fno-omit-frame-pointer
  58. flags-debug: LDFLAGS += -g
  59. flags-debug: link compile
  60. flags-release: DEFINES += -DKRELEASE
  61. flags-release: CFLAGS += -MD -O2
  62. flags-release: LDFLAGS +=
  63. flags-release: link compile
  64. scaffold: scaffold-$(PLATFORM)
  65. .NOTPARALLEL: scaffold
  66. scaffold-win32:
  67. -@mkdir $(addprefix obj\\, $(DIRECTORIES)) 2>nul
  68. @if not exist bin mkdir bin
  69. scaffold-linux scaffold-macos:
  70. @mkdir -p bin
  71. @mkdir -p $(addprefix obj/,$(DIRECTORIES))
  72. genversion: genversion-$(PLATFORM)
  73. genversion-win32:
  74. @..\misc\versiongen.exe version.txt -outfile=src\$(ASSEMBLY_NAME)_version.h
  75. genversion-linux genversion-macos:
  76. @../misc/versiongen version.txt -outfile=src/$(ASSEMBLY_NAME)_version.h
  77. .PHONY: clean
  78. clean: clean-$(PLATFORM)
  79. clean-win32:
  80. if exist bin del /s /q bin\*.*
  81. if exist obj del /s /q obj\*.*
  82. clean-linux clean-macos:
  83. @rm -rf bin/*
  84. @rm -rf obj/*
  85. link: link-$(PLATFORM)
  86. link-win32: LDFLAGS += -lassimp-vc143-mt -fdeclspec -Wno-cast-function-type-mismatch -Xlinker /INCREMENTAL
  87. link-win32: DEFINES += -D_CRT_SECURE_NO_WARNINGS -DUNICODE
  88. link-win32: OUTPUT_NAME = bin\$(ASSEMBLY_NAME).exe
  89. # NOTE: --no-undefined and --no-allow-shlib-undefined ensure that symbols linking against are resolved.
  90. # These are linux-specific, as the default behaviour is the opposite of this, allowing code to compile
  91. # here that would not on other platforms from not being exported (i.e. Windows)
  92. # Discovered the solution here for this: https://github.com/ziglang/zig/issues/8180
  93. link-linux: CFLAGS += -Wno-cast-function-type-mismatch -fPIC
  94. link-linux: LDFLAGS += -lm -lassimp -Wl,--no-undefined,--no-allow-shlib-undefined,-rpath='$$ORIGIN'
  95. link-linux: OUTPUT_NAME = bin/$(ASSEMBLY_NAME)
  96. link-macos: LDFLAGS += -Wl,-rpath,. -lassimp
  97. link-macos: OUTPUT_NAME = bin/$(ASSEMBLY_NAME)
  98. link-win32 link-linux link-macos: link-common
  99. link-common: $(OBJ_FILES)
  100. clang $(OBJ_FILES) -o $(OUTPUT_NAME) $(LDFLAGS)
  101. .PHONY: compile
  102. compile:
  103. -include $(OBJ_FILES:.o=.d)
  104. # compile .c to .o object for windows, linux and mac
  105. obj/%.c.o: %.c
  106. @echo $<...
  107. @clang $< $(CFLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  108. -include $(OBJ_FILES:.o=.d)
  109. .PHONY: gen_compile_flags-debug
  110. gen_compile_flags-debug: DEFINES += -D_DEBUG
  111. gen_compile_flags-debug: gen_compile_flags
  112. gen_compile_flags-release: gen_compile_flags
  113. .PHONY: gen_compile_flags
  114. gen_compile_flags:
  115. ifeq ($(BUILD_PLATFORM),windows)
  116. @..\misc\cfgen -outfile=compile_flags.txt $(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0
  117. else
  118. @../misc/cfgen -outfile=compile_flags.txt $(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0
  119. endif