Makefile.library.mak 7.6 KB

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