Makefile.library.mak 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. BUILD_DIR := bin
  2. OBJ_DIR := obj
  3. # Needs to be set by the caller.
  4. # ASSEMBLY := engine
  5. DEFINES := -DKEXPORT
  6. # Detect OS and architecture.
  7. ifeq ($(OS),Windows_NT)
  8. # WIN32
  9. BUILD_PLATFORM := windows
  10. EXTENSION := .dll
  11. COMPILER_FLAGS := -Wall -Wextra -Werror -Wno-error=deprecated-declarations -Wno-error=unused-function -Wvla -Wgnu-folding-constant -Wno-missing-braces -fdeclspec -Wstrict-prototypes -Wno-unused-parameter -Wno-missing-field-initializers
  12. INCLUDE_FLAGS := -I$(ASSEMBLY)\src $(ADDL_INC_FLAGS)
  13. LINKER_FLAGS := -shared -lwinmm -L$(OBJ_DIR)\$(ASSEMBLY) -L.\$(BUILD_DIR) $(ADDL_LINK_FLAGS)
  14. DEFINES += -D_CRT_SECURE_NO_WARNINGS -DUNICODE
  15. # Make does not offer a recursive wildcard function, and Windows needs one, so here it is:
  16. rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
  17. DIR := $(subst /,\,${CURDIR})
  18. # .c files
  19. SRC_FILES := $(call rwildcard,$(ASSEMBLY)/,*.c)
  20. # directories with .h files
  21. DIRECTORIES := \$(ASSEMBLY)\src $(subst $(DIR),,$(shell dir $(ASSEMBLY)\src /S /AD /B | findstr /i src))
  22. OBJ_FILES := $(SRC_FILES:%=$(OBJ_DIR)/%.o)
  23. ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
  24. # AMD64
  25. else
  26. ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
  27. # AMD64
  28. endif
  29. ifeq ($(PROCESSOR_ARCHITECTURE),x86)
  30. # IA32
  31. endif
  32. endif
  33. else
  34. UNAME_S := $(shell uname -s)
  35. ifeq ($(UNAME_S),Linux)
  36. # LINUX
  37. BUILD_PLATFORM := linux
  38. EXTENSION := .so
  39. # NOTE: -fvisibility=hidden hides all symbols by default, and only those that explicitly say
  40. # otherwise are exported (i.e. via KAPI).
  41. COMPILER_FLAGS :=-fvisibility=hidden -fpic -Wall -Wno-error=deprecated-declarations -Wno-error=unused-function -Werror -Wvla -Wno-missing-braces -fdeclspec
  42. INCLUDE_FLAGS := -I./$(ASSEMBLY)/src -I$(VULKAN_SDK)/include $(ADDL_INC_FLAGS)
  43. # NOTE: --no-undefined and --no-allow-shlib-undefined ensure that symbols linking against are resolved.
  44. # These are linux-specific, as the default behaviour is the opposite of this, allowing code to compile
  45. # here that would not on other platforms from not being exported (i.e. Windows)
  46. # Discovered the solution here for this: https://github.com/ziglang/zig/issues/8180
  47. LINKER_FLAGS :=-Wl,--no-undefined,--no-allow-shlib-undefined -shared -lvulkan -lxcb -lX11 -lX11-xcb -lxkbcommon -lm -L$(VULKAN_SDK)/lib -L/usr/X11R6/lib -L./$(BUILD_DIR) $(ADDL_LINK_FLAGS) # .c files
  48. SRC_FILES := $(shell find $(ASSEMBLY) -name *.c)
  49. # directories with .h files
  50. DIRECTORIES := $(shell find $(ASSEMBLY) -type d)
  51. OBJ_FILES := $(SRC_FILES:%=$(OBJ_DIR)/%.o)
  52. endif
  53. ifeq ($(UNAME_S),Darwin)
  54. # OSX
  55. BUILD_PLATFORM := macos
  56. EXTENSION := .dylib
  57. # NOTE: -fvisibility=hidden hides all symbols by default, and only those that explicitly say
  58. # otherwise are exported (i.e. via KAPI).
  59. COMPILER_FLAGS :=-fvisibility=hidden -fpic -Wall -Werror -Wvla -Wno-error=deprecated-declarations -Wno-error=unused-function -Wgnu-folding-constant -Wno-missing-braces -fdeclspec -ObjC
  60. INCLUDE_FLAGS := -I./$(ASSEMBLY)/src $(ADDL_INC_FLAGS)
  61. # NOTE: Equivalent of the linux version above, this ensures that symbols linking against are resolved.
  62. # Discovered this here: https://stackoverflow.com/questions/26971333/what-is-clangs-equivalent-to-no-undefined-gcc-flag
  63. LINKER_FLAGS :=-Wl,-undefined,error -shared -dynamiclib -install_name @rpath/lib$(ASSEMBLY).dylib -lobjc -framework AppKit -framework QuartzCore -L./$(BUILD_DIR) $(ADDL_LINK_FLAGS)
  64. # .c and .m files
  65. SRC_FILES := $(shell find $(ASSEMBLY) -type f \( -name "*.c" -o -name "*.m" \))
  66. # directories with .h files
  67. DIRECTORIES := $(shell find $(ASSEMBLY) -type d)
  68. OBJ_FILES := $(SRC_FILES:%=$(OBJ_DIR)/%.o)
  69. endif
  70. UNAME_P := $(shell uname -p)
  71. ifeq ($(UNAME_P),x86_64)
  72. # AMD64
  73. endif
  74. ifneq ($(filter %86,$(UNAME_P)),)
  75. # IA32
  76. endif
  77. ifneq ($(filter arm%,$(UNAME_P)),)
  78. # ARM
  79. endif
  80. endif
  81. # Generate version
  82. ifeq ($(DO_VERSION),yes)
  83. ifeq ($(BUILD_PLATFORM),windows)
  84. VERFILE := $(ASSEMBLY)\src\$(ASSEMBLY)_version.h
  85. VERFILE_TEXT := $(ASSEMBLY)\version.txt
  86. KVERSION := $(shell $(DIR)\$(BUILD_DIR)\kohi.tools.versiongen.exe "$(VERFILE_TEXT)")
  87. else
  88. VERFILE := $(ASSEMBLY)/src/$(ASSEMBLY)_version.h
  89. VERFILE_TEXT := $(ASSEMBLY)/version.txt
  90. KVERSION := $(shell $(BUILD_DIR)/kohi.tools.versiongen "$(VERFILE_TEXT)")
  91. endif
  92. VER_COMMENT := // NOTE: This file is automatically generated by the build process and should not be checked in.
  93. endif
  94. # Generate numeric-only version for PDBs.
  95. ifeq ($(DO_VERSION),yes)
  96. ifeq ($(BUILD_PLATFORM),windows)
  97. KNUMERIC_VERSION := $(shell $(DIR)\$(BUILD_DIR)\kohi.tools.versiongen.exe -n)
  98. else
  99. KNUMERIC_VERSION := $(shell $(BUILD_DIR)/kohi.tools.versiongen -n)
  100. endif
  101. endif
  102. # Defaults to debug unless release is specified.
  103. ifeq ($(TARGET),release)
  104. # release
  105. DEFINES += -DKRELEASE
  106. COMPILER_FLAGS += -MD -O2
  107. else
  108. # debug
  109. DEFINES += -D_DEBUG
  110. COMPILER_FLAGS += -g -MD -O0
  111. LINKER_FLAGS += -g
  112. endif
  113. all: scaffold compile link gen_compile_flags
  114. .NOTPARALLEL: scaffold
  115. .PHONY: scaffold
  116. scaffold: # create build directory
  117. ifeq ($(BUILD_PLATFORM),windows)
  118. -@setlocal enableextensions enabledelayedexpansion && mkdir $(addprefix $(OBJ_DIR), $(DIRECTORIES)) 2>NUL || cd .
  119. -@setlocal enableextensions enabledelayedexpansion && mkdir $(BUILD_DIR) 2>NUL || cd .
  120. else
  121. @mkdir -p $(BUILD_DIR)
  122. @mkdir -p $(addprefix $(OBJ_DIR)/,$(DIRECTORIES))
  123. endif
  124. # TODO: re-enable this conditionally
  125. # Generate version file
  126. ifeq ($(DO_VERSION),yes)
  127. ifeq ($(BUILD_PLATFORM),windows)
  128. @if exist $(VERFILE) del $(VERFILE)
  129. # Write out the version file.
  130. @echo $(VER_COMMENT)\n > $(VERFILE)
  131. @echo #define KVERSION "$(KVERSION)" >> $(VERFILE)
  132. else
  133. @rm -rf $(VERFILE)
  134. # Write out the version file.
  135. @echo "$(VER_COMMENT)\n" > $(VERFILE)
  136. @echo "#define KVERSION \"$(KVERSION)\"" >> $(VERFILE)
  137. endif
  138. endif
  139. .PHONY: link
  140. link: scaffold $(OBJ_FILES) # link
  141. @echo Linking "$(ASSEMBLY)"...
  142. ifeq ($(BUILD_PLATFORM),windows)
  143. @clang $(OBJ_FILES) -o $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION) $(LINKER_FLAGS) -Xlinker /INCREMENTAL
  144. else
  145. @clang $(OBJ_FILES) -o $(BUILD_DIR)/lib$(ASSEMBLY)$(EXTENSION) $(LINKER_FLAGS)
  146. endif
  147. .PHONY: compile
  148. compile:
  149. @echo --- Performing "$(ASSEMBLY)" $(TARGET) build ---
  150. -include $(OBJ_FILES:.o=.d)
  151. .PHONY: clean
  152. clean: # clean build directory
  153. @echo --- Cleaning "$(ASSEMBLY)" ---
  154. ifeq ($(BUILD_PLATFORM),windows)
  155. @if exist $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION) del $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION)
  156. # Windows builds include a lot of files... get them all.
  157. @if exist $(BUILD_DIR)\$(ASSEMBLY).* del $(BUILD_DIR)\$(ASSEMBLY).*
  158. @if exist $(OBJ_DIR)\$(ASSEMBLY) rmdir /s /q $(OBJ_DIR)\$(ASSEMBLY)
  159. else
  160. @rm -rf $(BUILD_DIR)/lib$(ASSEMBLY)$(EXTENSION)
  161. @rm -rf $(OBJ_DIR)/$(ASSEMBLY)
  162. endif
  163. # compile .c to .o object for windows, linux and mac
  164. $(OBJ_DIR)/%.c.o: %.c
  165. @echo $<...
  166. @clang $< $(COMPILER_FLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  167. # compile .m to .o object only for macos
  168. ifeq ($(BUILD_PLATFORM),macos)
  169. $(OBJ_DIR)/%.m.o: %.m
  170. @echo $<...
  171. @clang $< $(COMPILER_FLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  172. endif
  173. -include $(OBJ_FILES:.o=.d)
  174. .PHONY: gen_compile_flags
  175. gen_compile_flags:
  176. ifeq ($(BUILD_PLATFORM),windows)
  177. $(shell powershell \"$(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0\".replace('-I', '-I..\').replace(' ', \"`n\").replace('-I..\C:', '-IC:') > $(ASSEMBLY)/compile_flags.txt)
  178. else
  179. @echo $(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0 | tr " " "\n" | sed "s/\-I\.\//\-I\.\.\//g" > $(ASSEMBLY)/compile_flags.txt
  180. endif