2
0

Makefile.executable.mak 5.4 KB

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