Makefile.library.mak 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 -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 -luser32 -L$(OBJ_DIR)\$(ASSEMBLY) -L.\$(BUILD_DIR) $(ADDL_LINK_FLAGS)
  14. DEFINES += -D_CRT_SECURE_NO_WARNINGS
  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. COMPILER_FLAGS := -Wall -Werror -Wvla -Wgnu-folding-constant -Wno-missing-braces -fdeclspec -fPIC
  40. INCLUDE_FLAGS := -I./$(ASSEMBLY)/src -I$(VULKAN_SDK)/include $(ADDL_INC_FLAGS)
  41. LINKER_FLAGS := -shared -lvulkan -lxcb -lX11 -lX11-xcb -lxkbcommon -L$(VULKAN_SDK)/lib -L/usr/X11R6/lib -L./$(BUILD_DIR) $(ADDL_LINK_FLAGS)
  42. # .c files
  43. SRC_FILES := $(shell find $(ASSEMBLY) -name *.c)
  44. # directories with .h files
  45. DIRECTORIES := $(shell find $(ASSEMBLY) -type d)
  46. OBJ_FILES := $(SRC_FILES:%=$(OBJ_DIR)/%.o)
  47. endif
  48. ifeq ($(UNAME_S),Darwin)
  49. # OSX
  50. BUILD_PLATFORM := macos
  51. EXTENSION := .dylib
  52. COMPILER_FLAGS := -Wall -Werror -Wvla -Wgnu-folding-constant -Wno-missing-braces -fdeclspec -fPIC -ObjC
  53. INCLUDE_FLAGS := -I./$(ASSEMBLY)/src $(ADDL_INC_FLAGS)
  54. LINKER_FLAGS := -shared -dynamiclib -install_name @rpath/lib$(ASSEMBLY).dylib -lobjc -framework AppKit -framework QuartzCore -L./$(BUILD_DIR) $(ADDL_LINK_FLAGS)
  55. # .c and .m files
  56. SRC_FILES := $(shell find $(ASSEMBLY) -type f \( -name "*.c" -o -name "*.m" \))
  57. # directories with .h files
  58. DIRECTORIES := $(shell find $(ASSEMBLY) -type d)
  59. OBJ_FILES := $(SRC_FILES:%=$(OBJ_DIR)/%.o)
  60. endif
  61. UNAME_P := $(shell uname -p)
  62. ifeq ($(UNAME_P),x86_64)
  63. # AMD64
  64. endif
  65. ifneq ($(filter %86,$(UNAME_P)),)
  66. # IA32
  67. endif
  68. ifneq ($(filter arm%,$(UNAME_P)),)
  69. # ARM
  70. endif
  71. endif
  72. # Generate version
  73. ifeq ($(DO_VERSION),yes)
  74. ifeq ($(BUILD_PLATFORM),windows)
  75. KVERSION := $(shell $(DIR)\$(BUILD_DIR)\versiongen.exe $(VER_MAJOR) $(VER_MINOR))
  76. VERFILE := $(ASSEMBLY)\src\version.h
  77. else
  78. KVERSION := $(shell $(BUILD_DIR)/versiongen $(VER_MAJOR) $(VER_MINOR))
  79. VERFILE := $(ASSEMBLY)/src/version.h
  80. endif
  81. VER_COMMENT := // NOTE: This file is automatically generated by the build process and should not be checked in.
  82. endif
  83. # Generate numeric-only version for PDBs.
  84. ifeq ($(DO_VERSION),yes)
  85. ifeq ($(BUILD_PLATFORM),windows)
  86. KNUMERIC_VERSION := $(shell $(DIR)\$(BUILD_DIR)\versiongen.exe -n)
  87. else
  88. KNUMERIC_VERSION := $(shell $(BUILD_DIR)/versiongen -n)
  89. endif
  90. endif
  91. # Defaults to debug unless release is specified.
  92. ifeq ($(TARGET),release)
  93. # release
  94. else
  95. # debug
  96. DEFINES += -D_DEBUG
  97. COMPILER_FLAGS += -g -MD
  98. LINKER_FLAGS += -g
  99. endif
  100. all: scaffold compile link gen_compile_flags
  101. .PHONY: scaffold
  102. scaffold: # create build directory
  103. ifeq ($(BUILD_PLATFORM),windows)
  104. -@setlocal enableextensions enabledelayedexpansion && mkdir $(addprefix $(OBJ_DIR), $(DIRECTORIES)) 2>NUL || cd .
  105. -@setlocal enableextensions enabledelayedexpansion && mkdir $(BUILD_DIR) 2>NUL || cd .
  106. else
  107. @mkdir -p $(BUILD_DIR)
  108. @mkdir -p $(addprefix $(OBJ_DIR)/,$(DIRECTORIES))
  109. endif
  110. # TODO: re-enable this conditionally
  111. # # Generate version file
  112. # ifeq ($(BUILD_PLATFORM),windows)
  113. # @if exist $(VERFILE) del $(VERFILE)
  114. # # Write out the version file.
  115. # @echo $(VER_COMMENT)\n > $(VERFILE)
  116. # @echo #define KVERSION "$(KVERSION)" >> $(VERFILE)
  117. # else
  118. # @rm -rf $(VERFILE)
  119. # # Write out the version file.
  120. # @echo $(VER_COMMENT)\n > $(VERFILE)
  121. # @echo "#define KVERSION \"$(KVERSION)\"" >> $(VERFILE)
  122. # endif
  123. .PHONY: link
  124. link: scaffold $(OBJ_FILES) # link
  125. @echo Linking "$(ASSEMBLY)"...
  126. ifeq ($(BUILD_PLATFORM),windows)
  127. @clang $(OBJ_FILES) -o $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION) $(LINKER_FLAGS) -Xlinker /PDB:$(BUILD_DIR)\$(ASSEMBLY)_$(KNUMERIC_VERSION).pdb
  128. else
  129. @clang $(OBJ_FILES) -o $(BUILD_DIR)/lib$(ASSEMBLY)$(EXTENSION) $(LINKER_FLAGS)
  130. endif
  131. .PHONY: compile
  132. compile:
  133. @echo --- Performing "$(ASSEMBLY)" $(TARGET) build ---
  134. -include $(OBJ_FILES:.o=.d)
  135. .PHONY: clean
  136. clean: # clean build directory
  137. @echo --- Cleaning "$(ASSEMBLY)" ---
  138. ifeq ($(BUILD_PLATFORM),windows)
  139. @if exist $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION) del $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION)
  140. # Windows builds include a lot of files... get them all.
  141. @if exist $(BUILD_DIR)\$(ASSEMBLY).* del $(BUILD_DIR)\$(ASSEMBLY).*
  142. @if exist $(OBJ_DIR)\$(ASSEMBLY) rmdir /s /q $(OBJ_DIR)\$(ASSEMBLY)
  143. else
  144. @rm -rf $(BUILD_DIR)/lib$(ASSEMBLY)$(EXTENSION)
  145. @rm -rf $(OBJ_DIR)/$(ASSEMBLY)
  146. endif
  147. # compile .c to .o object for windows, linux and mac
  148. $(OBJ_DIR)/%.c.o: %.c
  149. @echo $<...
  150. @clang $< $(COMPILER_FLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  151. # compile .m to .o object only for macos
  152. ifeq ($(BUILD_PLATFORM),macos)
  153. $(OBJ_DIR)/%.m.o: %.m
  154. @echo $<...
  155. @clang $< $(COMPILER_FLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  156. endif
  157. -include $(OBJ_FILES:.o=.d)
  158. .PHONY: gen_compile_flags
  159. gen_compile_flags:
  160. ifeq ($(BUILD_PLATFORM),windows)
  161. $(shell powershell \"$(INCLUDE_FLAGS) $(DEFINES)\".replace('-I', '-I..\').replace(' ', \"`n\").replace('-I..\C:', '-IC:') > $(ASSEMBLY)/compile_flags.txt)
  162. else
  163. @echo $(INCLUDE_FLAGS) $(DEFINES) | tr " " "\n" | sed "s/\-I\.\//\-I\.\.\//g" > $(ASSEMBLY)/compile_flags.txt
  164. endif