Makefile.executable.mak 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. %:: %,v
  2. %:: RCS/%,v
  3. %:: RCS/%
  4. %:: s.%
  5. %:: SCCS/s.%
  6. BUILD_DIR := bin
  7. OBJ_DIR := obj
  8. # NOTE: ASSEMBLY must be set on calling this makefile
  9. DEFINES := -DKIMPORT
  10. # Detect OS and architecture.
  11. ifeq ($(OS),Windows_NT)
  12. # WIN32
  13. BUILD_PLATFORM := windows
  14. EXTENSION := .exe
  15. COMPILER_FLAGS := -Wall -Werror -Wno-error=deprecated-declarations -Wno-error=unused-function -Wvla -Werror=vla -Wgnu-folding-constant -Wno-missing-braces -fdeclspec -Wstrict-prototypes
  16. INCLUDE_FLAGS := -I$(ASSEMBLY)\src $(ADDL_INC_FLAGS)
  17. LINKER_FLAGS := -L$(BUILD_DIR) $(ADDL_LINK_FLAGS) -Xlinker /INCREMENTAL
  18. DEFINES += -D_CRT_SECURE_NO_WARNINGS
  19. # Make does not offer a recursive wildcard function, and Windows needs one, so here it is:
  20. rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
  21. DIR := $(subst /,\,${CURDIR})
  22. # .c files
  23. SRC_FILES := $(call rwildcard,$(ASSEMBLY)/,*.c)
  24. # directories with .h files
  25. DIRECTORIES := \$(ASSEMBLY)\src $(subst $(DIR),,$(shell dir $(ASSEMBLY)\src /S /AD /B | findstr /i src))
  26. OBJ_FILES := $(SRC_FILES:%=$(OBJ_DIR)/%.o)
  27. ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
  28. # AMD64
  29. else
  30. ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
  31. # AMD64
  32. endif
  33. ifeq ($(PROCESSOR_ARCHITECTURE),x86)
  34. # IA32
  35. endif
  36. endif
  37. else
  38. UNAME_S := $(shell uname -s)
  39. ifeq ($(UNAME_S),Linux)
  40. # LINUX
  41. BUILD_PLATFORM := linux
  42. EXTENSION :=
  43. # NOTE: -fvisibility=hidden hides all symbols by default, and only those that explicitly say
  44. # otherwise are exported (i.e. via KAPI).
  45. COMPILER_FLAGS :=-fvisibility=hidden -fpic -Wall -Wno-error=deprecated-declarations -Wno-error=unused-function -Werror -Wvla -Wno-missing-braces -fdeclspec
  46. INCLUDE_FLAGS := -I./$(ASSEMBLY)/src $(ADDL_INC_FLAGS)
  47. # NOTE: --no-undefined and --no-allow-shlib-undefined ensure that symbols linking against are resolved.
  48. # These are linux-specific, as the default behaviour is the opposite of this, allowing code to compile
  49. # here that would not on other platforms from not being exported (i.e. Windows)
  50. # Discovered the solution here for this: https://github.com/ziglang/zig/issues/8180
  51. LINKER_FLAGS :=-Wl,--no-undefined,--no-allow-shlib-undefined -L./$(BUILD_DIR) $(ADDL_LINK_FLAGS) -Wl,-rpath,. -lm -ldl
  52. # .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 :=
  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 -Wall -Wno-error=deprecated-declarations -Wno-error=unused-function -Werror -Wvla -Werror=vla -Wgnu-folding-constant -Wno-missing-braces -fdeclspec -fPIC
  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 -L./$(BUILD_DIR) $(ADDL_LINK_FLAGS) -Wl,-rpath,.
  69. # .c files
  70. SRC_FILES := $(shell find $(ASSEMBLY) -name *.c)
  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. # Defaults to debug unless release is specified.
  87. ifeq ($(TARGET),release)
  88. # release
  89. DEFINES += -DKRELEASE
  90. COMPILER_FLAGS += -MD -O2
  91. else
  92. # debug
  93. DEFINES += -D_DEBUG
  94. COMPILER_FLAGS += -g -MD -O0
  95. LINKER_FLAGS += -g
  96. endif
  97. all: scaffold compile link gen_compile_flags
  98. .NOTPARALLEL: scaffold
  99. .PHONY: scaffold
  100. scaffold: # create build directory
  101. ifeq ($(BUILD_PLATFORM),windows)
  102. -@setlocal enableextensions enabledelayedexpansion && mkdir $(addprefix $(OBJ_DIR), $(DIRECTORIES)) 2>NUL || cd .
  103. -@setlocal enableextensions enabledelayedexpansion && mkdir $(BUILD_DIR) 2>NUL || cd .
  104. else
  105. @mkdir -p $(BUILD_DIR)
  106. @mkdir -p $(addprefix $(OBJ_DIR)/,$(DIRECTORIES))
  107. endif
  108. .PHONY: link
  109. link: scaffold $(OBJ_FILES) # link
  110. @echo Linking "$(ASSEMBLY)"...
  111. ifeq ($(BUILD_PLATFORM),windows)
  112. @clang $(OBJ_FILES) -o $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION) $(LINKER_FLAGS)
  113. else
  114. @clang $(OBJ_FILES) -o $(BUILD_DIR)/$(ASSEMBLY)$(EXTENSION) $(LINKER_FLAGS)
  115. endif
  116. .PHONY: compile
  117. compile:
  118. @echo --- Performing "$(ASSEMBLY)" $(TARGET) build ---
  119. -include $(OBJ_FILES:.o=.d)
  120. .PHONY: clean
  121. clean: # clean build directory
  122. @echo --- Cleaning "$(ASSEMBLY)" ---
  123. ifeq ($(BUILD_PLATFORM),windows)
  124. @if exist $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION) del $(BUILD_DIR)\$(ASSEMBLY)$(EXTENSION)
  125. # Windows builds include a lot of files... get them all.
  126. @if exist $(BUILD_DIR)\$(ASSEMBLY).* del $(BUILD_DIR)\$(ASSEMBLY).*
  127. @if exist $(OBJ_DIR)\$(ASSEMBLY) rmdir /s /q $(OBJ_DIR)\$(ASSEMBLY)
  128. else
  129. @rm -rf $(BUILD_DIR)/$(ASSEMBLY)$(EXTENSION)
  130. @rm -rf $(OBJ_DIR)/$(ASSEMBLY)
  131. endif
  132. # compile .c to .o object for windows, linux and mac
  133. $(OBJ_DIR)/%.c.o: %.c
  134. @echo $<...
  135. @clang $< $(COMPILER_FLAGS) -c -o $@ $(DEFINES) $(INCLUDE_FLAGS)
  136. -include $(OBJ_FILES:.o=.d)
  137. .PHONY: gen_compile_flags
  138. gen_compile_flags:
  139. ifeq ($(BUILD_PLATFORM),windows)
  140. # Updated to handle not breaking up quoted strings (i.e. paths with spaces in them) but then afterward to remove said quotes.
  141. $(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)
  142. else
  143. @echo $(INCLUDE_FLAGS) $(DEFINES) -ferror-limit=0| tr " " "\n" | sed "s/\-I\.\//\-I\.\.\//g" > $(ASSEMBLY)/compile_flags.txt
  144. endif